-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_dump.py
168 lines (148 loc) · 5.12 KB
/
clean_dump.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
import argparse
import json
import re
from pathlib import Path
from typing import Dict, Union
bad_line_starts = ("/*", "//", "|", "*/")
bad_line_contains = ("PrivateImplementationDetails", "{}")
enum_re = re.compile(r"\tpublic const [\w\.]+ (\w+) = (\d+);")
def main(input_dump: Union[str, Path], output_dump: Union[str, Path]) -> None:
input_path = Path(input_dump).resolve()
output_path = Path(output_dump).resolve()
with open(input_path / "dump.cs", "r", encoding="utf-8") as fp:
lines = fp.readlines()
output_lines = []
for line in lines:
if not any(
[line.strip().startswith(bad_start) for bad_start in bad_line_starts]
+ [bad_contain in line for bad_contain in bad_line_contains]
):
cleaned_line = line.split("//")[0].split("/*")[0].rstrip()
if not (
cleaned_line.strip().startswith("[")
and cleaned_line.strip().endswith("]")
):
cleaned_line = (
cleaned_line if cleaned_line.endswith("\n") else cleaned_line + "\n"
)
output_lines.append(cleaned_line)
with open(
output_path / "01_clean_dump.cs", "w", encoding="utf-8", newline=""
) as fp:
fp.writelines(output_lines)
enum_lines = []
is_enum_field = False
for line in output_lines:
if is_enum_field:
if line.startswith("}"):
enum_lines.append(line + "\n")
is_enum_field = False
elif line == "{\n":
enum_lines.append("{\n")
elif "public int value__;" not in line:
matched = enum_re.match(line)
if matched:
name, value = matched.groups()
enum_lines.append(f"\t{name} = {value},\n")
elif " enum " in line:
enum_lines.append(line)
is_enum_field = True
with open(
output_path / "07_clean_dump_enum.cs", "w", encoding="utf-8", newline=""
) as fp:
fp.writelines(enum_lines)
with open(input_path / "stringliteral.json", "r", encoding="utf-8") as fp:
literal_lines = json.load(fp)
with open(
output_path / "02_stringliteral.txt", "w", encoding="utf-8", newline=""
) as fp:
fp.writelines(
[
item["value"] + "\n"
for item in literal_lines
if "\u0000" not in item["value"]
]
)
with open(input_path / "script.json", "r", encoding="utf-8") as fp:
script = json.load(fp)
def remove_address(input_dict: Dict[str, str]) -> Dict[str, str]:
return {k: v for k, v in input_dict.items() if k != "Address"}
BAD_METHOD_STARTS = {
"UnityEngine",
"System",
"<>",
"Firebase",
"PlayMaker",
"HutongGames",
"CriStructMemory",
"CriMana",
"FingerEventDetector",
"GestureRecognizerTS",
"PlatformSupport",
"JsonManager",
"Org.BouncyCastle",
"LitJson",
"TweenFOV",
"XWeaponTrail",
"Xft",
"WellFired",
"UniWebView",
"ContinuousGestureRecognizer",
"CriAtom",
"SimpleAnimation",
"Mono",
"Microsoft",
"DiscreteGestureRecognizer",
"FingerGestures",
"TBQuickSetup",
"CStateManager",
"GameObjectExtensions",
"BetterList",
"NGUITools",
"UITweener",
"CFSM",
}
def bad_method(method: str) -> bool:
return any([method.startswith(bad_start) for bad_start in BAD_METHOD_STARTS])
ScriptMethod = [
remove_address(item)
for item in script["ScriptMethod"]
if not bad_method(item["Name"])
]
ScriptMetadata = [
remove_address(item)
for item in script["ScriptMethod"]
if not bad_method(item["Name"])
]
ScriptString = [
item["Value"] + "\n"
for item in script["ScriptString"]
if "\u0000" not in item["Value"]
]
ScriptMetadataMethod = [
item["Name"].replace("Method$", "") + "\n"
for item in script["ScriptMetadataMethod"]
]
ScriptMetadataMethod = [
item for item in ScriptMetadataMethod if not bad_method(item)
]
for file_name, item in (
("04_ScriptMethod.json", ScriptMethod),
("05_ScriptMetadata.json", ScriptMetadata),
):
with open(output_path / file_name, "w", encoding="utf-8") as fp:
json.dump(item, fp, indent=2, ensure_ascii=False)
for file_name, item in (
("03_ScriptString.txt", ScriptString),
("06_ScriptMetadataMethod.txt", ScriptMetadataMethod),
):
with open(output_path / file_name, "w", encoding="utf-8", newline="") as fp:
fp.writelines(item)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Extract enums valus from the dump.cs file."
)
parser.add_argument("input", type=str, help="Path to the dump.cs folder")
parser.add_argument("output", type=str, help="Path to the output folder")
args = parser.parse_args()
main(args.input, args.output)