forked from GothicKit/ZenKit4Py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaedalus_script.py
More file actions
289 lines (232 loc) · 8.56 KB
/
daedalus_script.py
File metadata and controls
289 lines (232 loc) · 8.56 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
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
__all__ = [
"DaedalusScript",
"DaedalusSymbol",
"DaedalusInstruction",
"DaedalusDataType",
"DaedalusOpcode",
]
from abc import abstractmethod
from ctypes import Structure
from ctypes import c_float
from ctypes import c_int
from ctypes import c_int32
from ctypes import c_size_t
from ctypes import c_uint16
from ctypes import c_uint32
from ctypes import c_void_p
from enum import IntEnum
from typing import Any
from typing import ClassVar
from zenkit import _native
from zenkit._core import DLL
from zenkit._core import PathOrFileLike
from zenkit._native import ZkPointer
from zenkit._native import ZkString
from zenkit.daedalus.base import DaedalusInstance
class DaedalusOpcode(IntEnum):
ADD = 0
SUB = 1
MUL = 2
DIV = 3
MOD = 4
OR = 5
ANDB = 6
LT = 7
GT = 8
MOVI = 9
ORR = 11
AND = 12
LSL = 13
LSR = 14
LTE = 15
EQ = 16
NEQ = 17
GTE = 18
ADDMOVI = 19
SUBMOVI = 20
MULMOVI = 21
DIVMOVI = 22
PLUS = 30
NEGATE = 31
NOT = 32
CMPL = 33
NOP = 45
RSR = 60
BL = 61
BE = 62
PUSHI = 64
PUSHV = 65
PUSHVI = 67
MOVS = 70
MOVSS = 71
MOVVF = 72
MOVF = 73
MOVVI = 74
B = 75
BZ = 76
GMOVI = 80
PUSHVV = 245
class DaedalusDataType(IntEnum):
VOID = 0
FLOAT = 1
INT = 2
STRING = 3
CLASS = 4
FUNCTION = 5
PROTOTYPE = 6
INSTANCE = 7
DLL.ZkDaedalusSymbol_getString.restype = ZkString
DLL.ZkDaedalusSymbol_getInt.restype = c_int32
DLL.ZkDaedalusSymbol_getFloat.restype = c_float
DLL.ZkDaedalusSymbol_getInstance.restype = ZkPointer
DLL.ZkDaedalusSymbol_getIsConst.restype = c_int
DLL.ZkDaedalusSymbol_getIsMember.restype = c_int
DLL.ZkDaedalusSymbol_getIsExternal.restype = c_int
DLL.ZkDaedalusSymbol_getIsMerged.restype = c_int
DLL.ZkDaedalusSymbol_getIsGenerated.restype = c_int
DLL.ZkDaedalusSymbol_getHasReturn.restype = c_int
DLL.ZkDaedalusSymbol_getName.restype = ZkString
DLL.ZkDaedalusSymbol_getAddress.restype = c_int32
DLL.ZkDaedalusSymbol_getParent.restype = c_int32
DLL.ZkDaedalusSymbol_getSize.restype = c_int32
DLL.ZkDaedalusSymbol_getType.restype = c_int
DLL.ZkDaedalusSymbol_getIndex.restype = c_uint32
DLL.ZkDaedalusSymbol_getReturnType.restype = c_int
class DaedalusSymbol:
__slots__ = ("_handle", "_keepalive")
def __init__(self, **kwargs: Any) -> None:
self._handle = c_void_p(None)
if "_handle" in kwargs:
self._handle: c_void_p = kwargs.pop("_handle")
self._keepalive = kwargs.pop("_keepalive", DLL)
@property
def handle(self) -> c_void_p:
return self._handle
def get_string(self, i: int = 0, ctx: DaedalusInstance | None = None) -> str:
return DLL.ZkDaedalusSymbol_getString(self._handle, c_uint16(i), ctx.handle if ctx else None).value
def set_string(self, val: str, i: int = 0, ctx: DaedalusInstance | None = None) -> None:
DLL.ZkDaedalusSymbol_setString(
self._handle, val.encode("windows-1252"), c_uint16(i), ctx.handle if ctx else None
)
def get_int(self, i: int = 0, ctx: DaedalusInstance | None = None) -> int:
return DLL.ZkDaedalusSymbol_getInt(self._handle, c_uint16(i), ctx.handle if ctx else None)
def set_int(self, val: int, i: int = 0, ctx: DaedalusInstance | None = None) -> None:
DLL.ZkDaedalusSymbol_setInt(self._handle, c_int32(val), c_uint16(i), ctx.handle if ctx else None)
def get_float(self, i: int = 0, ctx: DaedalusInstance | None = None) -> float:
return DLL.ZkDaedalusSymbol_getFloat(self._handle, c_uint16(i), ctx.handle if ctx else None).value
def set_float(self, val: float, i: int = 0, ctx: DaedalusInstance | None = None) -> None:
DLL.ZkDaedalusSymbol_setFloat(self._handle, c_float(val), c_uint16(i), ctx.handle if ctx else None)
def get_instance(self) -> DaedalusInstance:
value = DLL.ZkDaedalusSymbol_getInstance(self._handle)
return DaedalusInstance.from_native(value)
@property
def is_const(self) -> bool:
return DLL.ZkDaedalusSymbol_getIsConst(self._handle) != 0
@property
def is_member(self) -> bool:
return DLL.ZkDaedalusSymbol_getIsMember(self._handle) != 0
@property
def is_external(self) -> bool:
return DLL.ZkDaedalusSymbol_getIsExternal(self._handle) != 0
@property
def is_merged(self) -> bool:
return DLL.ZkDaedalusSymbol_getIsMerged(self._handle) != 0
@property
def is_generated(self) -> bool:
return DLL.ZkDaedalusSymbol_getIsGenerated(self._handle) != 0
@property
def has_return(self) -> bool:
return DLL.ZkDaedalusSymbol_getHasReturn(self._handle) != 0
@property
def name(self) -> str:
return DLL.ZkDaedalusSymbol_getName(self._handle).value
@property
def address(self) -> int:
return DLL.ZkDaedalusSymbol_getAddress(self._handle)
@property
def parent(self) -> int:
return DLL.ZkDaedalusSymbol_getParent(self._handle)
@property
def size(self) -> int:
return DLL.ZkDaedalusSymbol_getSize(self._handle)
@property
def type(self) -> DaedalusDataType:
return DaedalusDataType(DLL.ZkDaedalusSymbol_getType(self._handle))
@property
def index(self) -> int:
return DLL.ZkDaedalusSymbol_getIndex(self._handle)
@property
def return_type(self) -> DaedalusDataType:
return DaedalusDataType(DLL.ZkDaedalusSymbol_getReturnType(self._handle))
def __repr__(self) -> str:
return f"<{self.__class__.__name__} handle={self._handle} name={self.name!r} type={self.type.name}>"
class DaedalusInstruction(Structure):
_fields_: ClassVar[tuple[str, Any]] = [
("_op", c_int),
("_size", c_int32),
("_addr_sym_imm", c_int32),
("_index", c_int32),
]
@property
def op(self) -> DaedalusOpcode:
return DaedalusOpcode(self._op)
@property
def size(self) -> int:
return self._size
@property
def address(self) -> int:
return c_uint32(self._addr_sym_imm).value
@property
def symbol(self) -> int:
return c_uint32(self._addr_sym_imm).value
@property
def immediate(self) -> int:
return self._addr_sym_imm
@property
def index(self) -> int:
return self._index
DLL.ZkDaedalusScript_getInstruction.restype = DaedalusInstruction
DLL.ZkDaedalusScript_getSymbolCount.restype = c_uint32
DLL.ZkDaedalusScript_getSymbolByIndex.restype = ZkPointer
DLL.ZkDaedalusScript_getSymbolByAddress.restype = ZkPointer
DLL.ZkDaedalusScript_getSymbolByName.restype = ZkPointer
class DaedalusScript:
__slots__ = ("_handle", "_delete", "_keepalive")
def __init__(self, **kwargs: Any) -> None:
self._handle = c_void_p(None)
if "_handle" in kwargs:
self._handle: c_void_p = kwargs.pop("_handle")
self._delete: bool = kwargs.pop("_delete", False)
self._keepalive = kwargs.pop("_keepalive", DLL)
@staticmethod
def load(path_or_file_like: PathOrFileLike) -> "DaedalusScript":
handle = _native.load("ZkDaedalusScript_load", path_or_file_like)
return DaedalusScript(_handle=handle, _delete=True)
@property
def symbols(self) -> list[DaedalusSymbol]:
count = DLL.ZkDaedalusScript_getSymbolCount(self._handle)
return [self.get_symbol_by_index(i) for i in range(count)]
def get_instruction(self, address: int) -> DaedalusInstruction:
return DLL.ZkDaedalusScript_getInstruction(self._handle, c_size_t(address))
def get_symbol_by_index(self, i: int) -> DaedalusSymbol | None:
handle = DLL.ZkDaedalusScript_getSymbolByIndex(self._handle, c_uint32(i)).value
if handle is None or handle.value is None:
return None
return DaedalusSymbol(_handle=handle, _keepalive=self)
def get_symbol_by_address(self, i: int) -> DaedalusSymbol | None:
handle = DLL.ZkDaedalusScript_getSymbolByAddress(self._handle, c_size_t(i)).value
if handle is None or handle.value is None:
return None
return DaedalusSymbol(_handle=handle, _keepalive=self)
def get_symbol_by_name(self, name: str) -> DaedalusSymbol | None:
handle = DLL.ZkDaedalusScript_getSymbolByName(self._handle, name.encode("windows-1252")).value
if handle is None or handle.value is None:
return None
return DaedalusSymbol(_handle=handle, _keepalive=self)
def __del__(self) -> None:
self._deleter()
@abstractmethod
def _deleter(self) -> None:
DLL.ZkDaedalusScript_del(self._handle)
def __repr__(self) -> str:
return f"<{self.__class__.__name__} handle={self._handle}>"