-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathremote.py
More file actions
238 lines (181 loc) · 6.49 KB
/
remote.py
File metadata and controls
238 lines (181 loc) · 6.49 KB
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
import configparser
import importlib
import json
import logging
import os
import platform
import sys
import time
from _remote import ffi, lib
util = None
VERSION = 'v1.7.0'
LOGFILE = 'remote.log'
CONFIGFILE = 'config.ini'
# non-function imported symbol types
SYMTYPES = {
'stage': 'struct Stage **',
'window_': 'void **',
'canvasW_': 'int *',
'canvasH_': 'int *',
'userData_': 'void **',
'WorldClient::handleWindowEvent': 'void *',
'UIElementContainer::handleWindowEvent': 'void *'
}
class dotdict(dict):
'''dict with dot notation accessors'''
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
# various references to pass around (symbols, constants, etc)
refs = dotdict(
tops=[], # collection of direct children of stage
topTypes=[], # the class names of tops
MainMenu=ffi.NULL,
GameClient=ffi.NULL,
WorldClient=ffi.NULL,
ClientWorld=ffi.NULL,
WorldView=ffi.NULL,
overrideW=0, overrideH=0, # fake values to return from XDL_GetWindowSize
windowW=0, windowH=0, # current window size
scaleX=1, scaleY=1, # current window scale
lastMove=0 # timestamp of last mouse movement
)
refs.VERSION = VERSION
refs.SYSINFO = '{0} v{1} {2} @ {3}'.format(
platform.python_implementation(), platform.python_version(),
platform.architecture()[0], platform.platform())
refs.config = configparser.ConfigParser()
refs.config.optionxform = str
refs.manager = None
# hooks ######################################################################
ORIGS = {}
@ffi.def_extern()
def hook_DoEvents():
refs.manager.run()
refs.manager.runCallbacks('beforeUpdate')
ORIGS['XDL_DoEvents']()
@ffi.def_extern()
def hook_Clear(color):
util.updateState()
refs.manager.runCallbacks('afterUpdate')
ORIGS['XDL_Clear'](refs.stage[0].backgroundColor)
@ffi.def_extern()
def hook_Present():
refs.manager.runCallbacks('onPresent')
ORIGS['XDL_Present']()
@ffi.def_extern()
def hook_GetWindowSize(wptr, hptr):
if refs.overrideW > 0:
wptr[0] = refs.overrideW
hptr[0] = refs.overrideH
else:
lib.SDL_GetWindowSize(refs.window_[0], wptr, hptr)
@ffi.def_extern()
def hook_TrackGAEvent(category, action, label, value):
pass
# logging.debug('GA: {}.{} "{}" {}'.format(
# ffi.string(category).decode(), ffi.string(action).decode(),
# ffi.string(label).decode(), value))
@ffi.def_extern()
def hook_mouseMove(x, y, userData):
x = round(x * refs.scaleX)
y = round(y * refs.scaleY)
ORIGS['mouseMoveCallback'](x, y, userData)
refs.lastMove = time.perf_counter()
@ffi.def_extern()
def hook_mouseButton(x, y, button, down, userData):
x = round(x * refs.scaleX)
y = round(y * refs.scaleY)
ORIGS['mouseButtonCallback'](x, y, button, down, userData)
@ffi.def_extern()
def hook_LoadTextureFile(name, callback, userData):
refs._tfname = ffi.string(name).decode()
hook = lib.subhook_new(callback, lib.hook_textureCallback, 0)
refs._orig_callback = ffi.cast(
'XDL_LoadTextureDoneCallback', lib.subhook_get_trampoline(hook))
lib.subhook_install(hook)
ORIGS['XDL_LoadTextureFile'](name, callback, userData)
lib.subhook_remove(hook)
lib.subhook_free(hook)
@ffi.def_extern()
def hook_textureCallback(texture, sptr):
refs._orig_callback(texture, sptr)
tname = refs._tfname.split('/')
pname, sid = tname[1].split('.', 1)
sid = sid.split('.')
if tname[0] != 'texture' or sid[0] != 'm0':
return
util.loadMipmaps(pname, sid[1])
def initHooks():
def addhook(fname, hookfunc, ret=False):
hook = lib.subhook_new(refs[fname], hookfunc, 1)
orig = ffi.cast('p' + fname, lib.subhook_get_trampoline(hook))
if orig != ffi.NULL:
ORIGS[fname] = orig
else:
logging.info('{}: no trampoline, using fallback'.format(fname))
def call_orig(*args):
lib.subhook_remove(hook)
res = refs[fname](*args)
lib.subhook_install(hook)
if ret:
return res
ORIGS[fname] = call_orig
lib.subhook_install(hook)
if not lib.subhook_is_installed(hook):
logging.error('failed to hook {}'.format(fname))
addhook('XDL_DoEvents', lib.hook_DoEvents)
addhook('XDL_Clear', lib.hook_Clear)
addhook('XDL_Present', lib.hook_Present)
addhook('XDL_GetWindowSize', lib.hook_GetWindowSize)
addhook('XDL_TrackGAEvent', lib.hook_TrackGAEvent)
addhook('mouseMoveCallback', lib.hook_mouseMove)
addhook('mouseButtonCallback', lib.hook_mouseButton)
if refs.config.getboolean('general', 'mipmaps', fallback=False):
addhook('XDL_LoadTextureFile', lib.hook_LoadTextureFile)
# startup ####################################################################
@ffi.def_extern()
def kickstart():
global refs, util
SYMFILE = os.environ['SBPE_SYMFILE']
with open(SYMFILE, 'rb') as f:
offsets = json.loads(f.read().decode('utf-8'))
refs.SCRIPTPATH = os.path.dirname(SYMFILE)
sys.path.insert(1, refs.SCRIPTPATH)
sys.path.insert(1, os.path.join(refs.SCRIPTPATH, 'pypy', 'site-packages'))
logging.basicConfig(
level=logging.INFO,
filename=os.path.join(refs.SCRIPTPATH, LOGFILE), filemode='w',
format='%(asctime)s %(module)s [%(levelname)s] %(message)s')
logging.info('SBPE ' + refs.VERSION)
logging.info('platform: ' + refs.SYSINFO)
refs.CONFIGFILE = os.path.join(refs.SCRIPTPATH, CONFIGFILE)
refs.config.read(refs.CONFIGFILE)
# import native functions/objects
for sname in offsets:
offset = offsets[sname]
if sname in SYMTYPES:
# objects, variables
refs[sname] = ffi.cast(SYMTYPES[sname], offset)
elif sname.startswith('flags::'):
# flags
refs[sname[7:]] = ffi.cast('uint8_t *', offset)
else:
# functions
refs[sname] = ffi.cast('p' + sname, offset)
# import object inheritance info
with open(os.path.join(refs.SCRIPTPATH, 'cdefs/proto.json'), 'r') as f:
d = json.load(f)
refs.CHAINS = d['chains']
refs.CASTABLE = d['castable']
# import util
util = importlib.import_module('util')
util.refs = refs
# import plugins
plugpath = os.path.join(refs.SCRIPTPATH, 'plugins')
man = importlib.import_module('manager')
refs.manager = man.Manager(path=plugpath, refs=refs)
# init hooks
initHooks()
logging.info('startup ok')
return 0