-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy3.py
407 lines (338 loc) · 11 KB
/
proxy3.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
import random
import time
import json
import traceback
import sys
from sys import stdin
from sys import stdout
from threading import Thread
_logging = False
_log_file = None
def _std_print(arg, *args):
stdout.write(_unite(arg, *args) + "\n")
stdout.flush()
_print = _std_print
def set_logging(val):
global _print
_logging = (val == True)
if _logging:
_print = _logging_print
else:
_print = _std_print
def _assert_log():
global _log_file
if _log_file is not None: return
import codecs
_log_file = codecs.open('plugin.log', 'w', encoding='utf8')
_log_file.write("Launched plugin\n")
_log_file.flush()
def log_warn(data):
_assert_log()
_log_file.write("=== " + str(data)+"\n")
_log_file.flush()
def log_info(data):
_assert_log()
_log_file.write(str(data)+"\n")
_log_file.flush()
def _unite(arg, *args):
arg = str(arg)
for i in args: arg += " " + str(i)
return arg
def _logging_print(arg, *args):
arg = _unite(arg, *args)
_assert_log()
_log_file.write(arg + "\n")
_log_file.flush()
_std_print(arg)
def _log_err(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
Proxy.log(e)
return wrapper
class _ZERO:
def __init__(self): pass
def _convert(s):
return str(s).replace('\t', ' ').replace('\n', '\t')
def _serialize(data):
t = type(data)
if data is None:
return "null"
if t is int or t is float or t is str:
return _convert(data)
if t is list or t is dict:
if t is list:
data = [ _convert(i) for i in data ]
elif t is dict:
data = { _convert(i) : _convert(k) for i, k in data.items() }
return json.dumps(data)
raise Exception('Wrong data to send: '+str(t)+', data: '+str(data))
def _deserialize(data):
data = data.replace('\t', '\n')
try: return float(data)
except: pass
try: return int(data)
except: pass
try: return boolean(data)
except: pass
try: return json.loads(data)
except: pass
return data
class _ServerHandler(Thread):
def __init__(self):
Thread.__init__(self)
self.doneInfo = None
self.work = True
self.buffer = []
def run(self):
while self.work:
self.parse(stdin.readline()[0:-1])
#self.__out = codecs.getwriter("utf-8")(sys.stdin.readline()[0:-1])
#self.parse(codecs.getreader("utf-8")(sys.stdin.readline()[0:-1]))
time.sleep(0.05)
def lock(self):
while self.doneInfo is None:
time.sleep(0.05)
pass
obj = self.doneInfo
self.doneInfo = None
return obj
def stop(self):
self.work = False
def parse(self, data):
data = data.strip()
# Ist für die Anzeige des commandName "cities:play" verantwortlich
log_info("~ "+data)
#log_info("- "+data)
if data != '#':
self.buffer.append(data)
return
log_warn("parsing received message")
data = self.buffer
self.buffer = []
first_line = data[0].split(' ',1)
Type = first_line[0]
if Type == 'confirm':
if len(first_line) > 1:
self.doneInfo = first_line[1]
else:
self.doneInfo = _ZERO()
return
if Type == 'unload':
self.doneInfo = _ZERO()
Proxy.unload()
return
if Type == 'setInfo':
self.doneInfo = first_line[1]
return
if Type == 'call':
if len(first_line) == 1: return
args = {}
for line in data[1:]:
line = line.split(' ', 1)
args[line[0]] = line[1]
first_line = first_line[1].split(' ', 2)
args['seq'] = int(first_line[0])
args['sender'] = first_line[1]
args['data'] = _deserialize(first_line[2])
Proxy.call(args['seq'], args['sender'], args['tag'] if 'tag' in args else None, args['data'])
return
print('unknown message type: '+Type)
class Proxy:
_seq = 0
_listeners = {}
_pluginData = {}
_server = None
_unload = None
def _setListener(listener, persistent):
seq = Proxy._seq
Proxy._listeners[seq] = (listener, persistent)
Proxy._seq += 1
return seq
def call(id, sender, tag, data):
for i, l in Proxy._listeners.items():
if i == id:
class HandleThread(Thread):
def run(self):
try:
l[0](sender, tag, data)
except TypeError as e:
try:
l[0](sender, data)
except Exception as e:
Proxy.log(e)
except Exception as e:
Proxy.log(e)
if not l[1]:
del Proxy._listeners[i]
HandleThread().start()
return
def _wait():
_print('#')
try:
return Proxy._server.lock()
except:
return _ZERO()
@_log_err
def init():
#log_file(stdin.readline())
Proxy._server = _ServerHandler()
log_warn('server init')
Proxy._server.start()
log_warn('server started')
Proxy._pluginData = _deserialize(Proxy._wait())
log_warn('got data from server')
@_log_err
def endInit():
_print("initializationCompleted")
Proxy._wait()
Proxy._server.join()
@_log_err
def getId():
return Proxy._pluginData['id']
@_log_err
def sendMessage(id, data, responseListener=None, returnListener=None):
_print('sendMessage', id, _serialize(data))
if responseListener is not None:
_print('response', Proxy._setListener(responseListener, True))
if returnListener is not None:
_print('return', Proxy._setListener(returnListener, False))
Proxy._wait()
@_log_err
def addMessageListener(tag, listener):
_print('addMessageListener', tag, Proxy._setListener(listener, True))
Proxy._wait()
@_log_err
def removeMessageListener(id, listener):
for i, l in Proxy._listeners.items():
if l == listener and i[0] == id:
del Proxy._listener[i]
_print('removeMessageListener', id, i[1])
_print('#')
Proxy._wait()
@_log_err
def setAlternative(srcTag, dstTag, priority):
print('setAlternative', tag, srcTag, dstTag, priority)
Proxy._wait()
@_log_err
def deleteAlternative(srcTag, dstTag):
print('deleteAlternative', tag, srcTag, dstTag)
Proxy._wait()
@_log_err
def callNextAlternative(sender, tag, current, data):
print('deleteAlternative', sender, tag, current, _serialize(data))
Proxy._wait()
@_log_err
def isAskingAnswer(sender):
return ('#' in sender)
@_log_err
def setTimer(delay, listener, count=1):
_print('setTimer', delay, count, Proxy._setListener(listener, True))
return Proxy._wait()
@_log_err
def cancelTimer(id):
_print('cancelTimer', id)
Proxy._wait()
@_log_err
def getProperty(name):
_print('getProperty', name)
return Proxy._wait()
@_log_err
def setProperty(name, value):
_print('setProperty', name, value)
Proxy._wait()
@_log_err
def getConfigField(name):
_print('getConfigField', name)
return Proxy._wait()
@_log_err
def setConfigField(name, value):
_print('setConfigField', name, value)
Proxy._wait()
_bundle = None
@_log_err
def setResourceBundle(filename):
with codecs.open(filename, 'r', encoding='utf8') as f:
Proxy._bundle = {}
for line in f.readlines():
line = line[:-1].strip('=',2)
Proxy._bundle[line[0]] = line[1]
@_log_err
def getString(id):
if Proxy._bundle is not None and id in Proxy._bundle:
return Proxy._bundle[id]
_print('getString', id)
return Proxy._wait()
def log(arg, *args):
if 'error' in type(arg).__name__.lower():
_print('error', arg.args[0])
_print('class', type(arg).__name__)
_print('stacktrace', _serialize(traceback.format_exc().splitlines()))
else:
arg = _unite(arg, *args)
_print('log', arg)
Proxy._wait()
@_log_err
def getDataDirPath(): return Proxy._pluginData['dataDirPath']
@_log_err
def getPluginDirPath(): return Proxy._pluginData['pluginDirPath']
@_log_err
def getAssetsDirPath(): return Proxy._pluginData['assetsDirPath']
@_log_err
def getRootDirPath(): return Proxy._pluginData['rootDirPath']
@_log_err
def addCleanupHelper(func):
Proxy._unload = func
def unload():
if Proxy._unload is not None: Proxy._unload()
Proxy._server.stop()
_log_file.close()
log = Proxy.log
init = Proxy.init
getId = Proxy.getId
get_id = Proxy.getId
unload = Proxy.unload
endInit = Proxy.endInit
end_init = Proxy.endInit
setTimer = Proxy.setTimer
set_timer = Proxy.setTimer
sendMessage = Proxy.sendMessage
send_message = Proxy.sendMessage
cancelTimer = Proxy.cancelTimer
cancel_timer = Proxy.cancelTimer
getProperty = Proxy.getProperty
get_property = Proxy.getProperty
setProperty = Proxy.setProperty
set_property = Proxy.setProperty
getString = Proxy.getString
get_string = Proxy.getString
isAskingAnswer = Proxy.isAskingAnswer
is_asking_answer = Proxy.isAskingAnswer
getConfigField = Proxy.getConfigField
get_config_field = Proxy.getConfigField
setConfigField = Proxy.setConfigField
set_config_field = Proxy.setConfigField
getRootDirPath = Proxy.getRootDirPath
get_root_dir_path = Proxy.getRootDirPath
getDataDirPath = Proxy.getDataDirPath
get_data_dir_path = Proxy.getDataDirPath
setAlternative = Proxy.setAlternative
set_alternative = Proxy.setAlternative
deleteAlternative = Proxy.deleteAlternative
delete_alternative = Proxy.deleteAlternative
getPluginDirPath = Proxy.getPluginDirPath
get_plugin_dir_path = Proxy.getPluginDirPath
getAssetsDirPath = Proxy.getAssetsDirPath
get_assets_dir_path = Proxy.getAssetsDirPath
setResourceBundle = Proxy.setResourceBundle
set_resource_bundle = Proxy.setResourceBundle
addCleanupHelper = Proxy.addCleanupHelper
add_cleanup_helper = Proxy.addCleanupHelper
addMessageListener = Proxy.addMessageListener
add_message_listener = Proxy.addMessageListener
callNextAlternative = Proxy.callNextAlternative
call_next_alternative = Proxy.callNextAlternative
removeMessageListener = Proxy.removeMessageListener
remove_message_listener = Proxy.removeMessageListener