-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.py
320 lines (283 loc) · 11.1 KB
/
box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#
#
# BBBBBBBBBBBBBBBBB OOOOOOOOO XXXXXXX XXXXXXX
# B::::::::::::::::B OO:::::::::OO X:::::X X:::::X
# B::::::BBBBBB:::::B OO:::::::::::::OO X:::::X X:::::X
# BB:::::B B:::::BO:::::::OOO:::::::OX::::::X X::::::X
# B::::B B:::::BO::::::O O::::::OXXX:::::X X:::::XXX
# B::::B B:::::BO:::::O O:::::O X:::::X X:::::X
# B::::BBBBBB:::::B O:::::O O:::::O X:::::X:::::X
# B:::::::::::::BB O:::::O O:::::O X:::::::::X
# B::::BBBBBB:::::B O:::::O O:::::O X:::::::::X
# B::::B B:::::BO:::::O O:::::O X:::::X:::::X
# B::::B B:::::BO:::::O O:::::O X:::::X X:::::X
# B::::B B:::::BO::::::O O::::::OXXX:::::X X:::::XXX
# BB:::::BBBBBB::::::BO:::::::OOO:::::::OX::::::X X::::::X
# B:::::::::::::::::B OO:::::::::::::OO X:::::X X:::::X
# B::::::::::::::::B OO:::::::::OO X:::::X X:::::X
# BBBBBBBBBBBBBBBBB OOOOOOOOO XXXXXXX XXXXXXX
#
#
# Assetto Corsa framework created by Marco 'Marocco2' Mollace
#
# version 0.2
#
# Usage of this library is under LGPLv3. Be careful :)
#
#
import ac
import traceback
import os
import sys
import platform
import update
try:
import ctypes
except:
ac.log('BOX: error loading ctypes: ' + traceback.format_exc())
raise
# TODO: read from config file for filters | IMPORTS
from os.path import dirname, realpath
# import configparser
import functools
import threading
import zipfile
import time
def async(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
t = threading.Thread(target=func, args=args, kwargs=kwargs)
t.daemon = True
t.start()
return t
return wrapper
if platform.architecture()[0] == "64bit":
dllfolder = "stdlib64"
dllfolder = os.path.join(os.path.dirname(__file__), dllfolder)
fmodex = "fmodex64.dll"
else:
dllfolder = "stdlib"
dllfolder = os.path.join(os.path.dirname(__file__), dllfolder)
fmodex = "fmodex.dll"
sys.path.insert(0, dllfolder)
os.environ['PATH'] = os.environ['PATH'] + ";."
ctypes.windll[os.path.join(dllfolder, fmodex)]
box_lib_folder = os.path.join(os.path.dirname(__file__), 'box_lib')
sys.path.insert(0, box_lib_folder)
try:
import pyfmodex
except Exception as e:
ac.log('BOX: error loading pyfmodex: ' + traceback.format_exc())
raise
try:
import requests
except Exception as e:
ac.log('BOX: error loading requests: ' + traceback.format_exc())
raise
# A useful push notification via Telegram if I need send some news
def notification(telegram_bot_oauth):
try:
telegram_api_url = "https://api.telegram.org/bot" + telegram_bot_oauth + "/getUpdates"
r = requests.get(telegram_api_url)
message = r.json()
if message["ok"]:
var_notify = message["result"][-1]["message"]["text"]
ac.log('BOX: Notification from Telegram: ' + var_notify)
return var_notify
else:
var_notify = "No Telegram connection"
ac.log('BOX: ' + var_notify)
except:
ac.log('BOX: No Internet connection')
var_notify = ""
return var_notify
def updatepyBox(sha=''):
result = 0
check_link = "https://api.github.com/repos/Marocco2/BOX/commits/shipping"
headers = {'Accept': 'application/vnd.github.VERSION.sha'}
r = requests.get(check_link, headers=headers)
if sha == "":
try:
with open("sha.txt", 'r') as g:
sha = g.read()
g.close()
except:
update_status = "No SHA available"
return update_status
if r.text != sha: # Check if server version and client version is the same
download_link = "https://raw.githubusercontent.com/Marocco2/BOX/update.py"
p = requests.get(download_link)
update_status = "update.py updated"
with open("update.py", 'w') as j:
j.write(p.text)
j.close()
return update_status
else:
update_status = "No new update"
return update_status
def updateBox(sha=''):
updatepyBox()
update.box()
# It downloads a zip file and extract it in a folder
def get_zipfile(download_link, dir_path='', absolute_path=False):
try:
local_filename = download_link.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(download_link, stream=True)
log_getZipFile = "Download of " + local_filename + " completed"
where_is_zip = os.path.join(os.path.dirname(__file__), local_filename)
ac.log("BOX: " + log_getZipFile)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# f.flush() commented by recommendation from J.F.Sebastian
ac.log("BOX: " + where_is_zip)
try:
with zipfile.ZipFile(local_filename, "r") as z:
if dir_path == "" and not absolute_path:
z.extractall(os.path.dirname(__file__)) # Extracting files
elif absolute_path:
z.extractall(dir_path) # Extracting files
else:
z.extractall(os.path.join(os.path.dirname(__file__), dir_path)) # Extracting files
# os.remove(local_filename)
log_getZipFile = "Files extracted"
return log_getZipFile
except:
log_getZipFile = "Error extracting files"
return log_getZipFile
except:
log_getZipFile = "Error downloading zip file"
ac.log('BOX: error downloading zip file: ' + traceback.format_exc())
return log_getZipFile
# A new function to automatize app updates for AC
# WORK IN PROGRESS
# TODO: make reorder files logic
def newupdate(version, check_link, download_link, dir_path=''):
try:
r = requests.get(check_link)
if r.json() != version: # Check if server version and client version is the same
update_status = get_zipfile(download_link, dir_path)
return update_status
else:
update_status = "No new update"
ac.log('BOX: ' + update_status)
return update_status
except:
update_status = "Error checking new update"
ac.log('BOX: error checking new update: ' + traceback.format_exc())
return update_status
# Uses GitHub to check updates
# WORK IN PROGRESS
# TODO: make reorder files logic
def github_newupdate(git_repo, branch='master', sha='', dir_path=''):
try:
updateBox()
check_link = "https://api.github.com/repos/" + git_repo + "/commits/" + branch
headers = {'Accept': 'application/vnd.github.VERSION.sha'}
r = requests.get(check_link, headers=headers)
if sha == "":
try:
with open("apps\\python\\" + git_repo.split('/')[-1] + "\sha.txt", 'r') as g:
sha = g.read()
g.close()
except:
update_status = "No SHA available"
ac.log('BOX: ' + update_status)
return update_status
if r.text != sha: # Check if server version and client version is the same
download_link = "https://github.com/" + git_repo + "/archive/" + branch + ".zip"
update_status = get_zipfile(download_link, dir_path)
with open("apps\\python\\" + git_repo.split('/')[-1] + "\sha.txt", 'w') as j:
j.write(r.text)
j.close()
return update_status
else:
update_status = "No new update"
ac.log('BOX: ' + update_status)
return update_status
except:
update_status = "Error checking new update"
ac.log('BOX: error checking new update: ' + traceback.format_exc())
return update_status
from threading import Thread, Event
class SoundPlayer(object):
def __init__(self, player):
self._play_event = Event()
self.player = player
self.playbackpos = [0.0, 0.0, 0.0]
self.playbackvol = 1.0
self.EQ = []
self.initEq()
self.sound_mode = pyfmodex.constants.FMOD_CREATECOMPRESSEDSAMPLE
self.speaker_mix = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
for i in self.EQ:
self.player.add_dsp(i)
self.channel = self.player.get_channel(0)
self.queue = []
self.thread = Thread(target=self._worker)
self.thread.daemon = True
self.thread.start()
def initEq(self):
freq = [16.0, 31.5, 63.0, 125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0, 16000.0]
for i in freq:
dsp = self.player.create_dsp_by_type(pyfmodex.constants.FMOD_DSP_TYPE_PARAMEQ)
dsp.set_param(pyfmodex.constants.FMOD_DSP_PARAMEQ_GAIN, 1.0)
dsp.set_param(pyfmodex.constants.FMOD_DSP_PARAMEQ_BANDWIDTH, 1.0)
dsp.set_param(pyfmodex.constants.FMOD_DSP_PARAMEQ_CENTER, i)
self.EQ.append(dsp)
def set_volume(self, volume):
self.playbackvol = volume
def set_sound_mode(self, sound_mode):
self.sound_mode = sound_mode
def set_position(self, position):
self.playbackpos = position
def set_gain(self, gain):
if self.sound_mode == pyfmodex.constants.FMOD_3D:
for i in self.EQ:
i.set_param(pyfmodex.constants.FMOD_DSP_PARAMEQ_GAIN, gain)
elif self.sound_mode == pyfmodex.constants.FMOD_2D:
volume = gain
self.speaker_mix = [volume, volume, volume, 1.0, volume, volume, volume, volume]
@async
def stop(self):
try:
self.channel.paused = 1
# self.queue.pop(0)
except:
ac.log('BOX: stop() error ' + traceback.format_exc())
@async
def queueSong(self, filename=None):
try:
if filename is not None:
if os.path.isfile(filename):
sound = self.player.create_sound(bytes(filename, encoding='utf-8'), self.sound_mode)
self.queue.append({'sound': sound, 'mode': self.sound_mode})
state = self._play_event.is_set()
if state == False:
self._play_event.set()
return 1 # mp3 loaded
else:
ac.log('BOX: File not found : %s' % filename)
except:
ac.log('BOX: queueSong() error ' + traceback.format_exc())
def lenQueue(self):
leng = self.queue.__len__()
return leng
def _worker(self):
while True:
self._play_event.wait()
queue_len = len(self.queue)
while queue_len > 0:
self.player.play_sound(self.queue[0]['sound'], False, 0)
self.channel.spectrum_mix = self.speaker_mix
self.channel.volume = self.playbackvol
self.player.update()
while self.channel.paused == 0 and self.channel.is_playing == 1:
time.sleep(0.1)
self.queue[0]['sound'].release()
self.queue.pop(0)
queue_len = len(self.queue)
self._play_event.clear()
FModSystem = pyfmodex.System()