-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcstructs.py
234 lines (190 loc) · 10.3 KB
/
mcstructs.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
import os
import re
import subprocess
via_nbt_version = '5.1.0'
version_prefix = '5'
# All of this would work better with bytecode rewriting, but here we go
replacements = {
# Gradle build script changes (less chance of running into conflicts by putting it here instead of the patch)
'name = "lenni0451"': 'name = "viaversion"',
'url = "https://maven.lenni0451.net/everything"': 'url = "https://repo.viaversion.com/"',
'api project(":MCStructs-nbt")': f'api "com.viaversion:nbt:{via_nbt_version}"',
'maven_group=net.lenni0451.mcstructs': 'maven_group=com.viaversion.mcstructs',
'maven_version=': f'maven_version={version_prefix}-',
# Code changes
'import net.lenni0451.mcstructs.nbt.tags.': 'import com.viaversion.nbt.tag.',
'import net.lenni0451.mcstructs.nbt.': 'import com.viaversion.nbt.tag.',
'import com.viaversion.nbt.tag.utils.NbtCodecUtils;': '',
'NbtTag': 'Tag',
'NbtNumber': 'NumberTag',
'NbtArray': 'NumberArrayTag',
'tag.getNbtType()': 'tag',
'value.getNbtType()': 'value',
'element.getNbtType()': 'element',
'ArrayTag.getLength()': 'ArrayTag.length()',
'type.name()': 'type.getClass().getSimpleName()',
'NbtType': 'Tag',
'import com.viaversion.nbt.tag.Tag': 'import com.viaversion.nbt.tag.*',
'import com.viaversion.nbt.tag.CompoundTag': 'import com.viaversion.nbt.tag.*',
'import static net.lenni0451.mcstructs.nbt.utils.NbtCodecUtils.MARKER_KEY;': '//',
'MARKER_KEY': '""',
# A special one
' if (!list.canAdd(tag)) throw new SNbtDeserializeException("Unable to insert " + tag.getClass().getSimpleName() + " into ListTag of type " + list.getType().name());': '',
'return NbtCodecUtils.wrapIfRequired(list);': 'return null /*Not required by ViaVersion at the moment*/;'
}
# Apply these separately to avoid changing json calls
extra_replacements = {
'.addString(': '.putString(',
'.addBoolean(': '.putBoolean(',
'.addByte(': '.putByte(',
'.addLong(': '.putLong(',
'.addInt(': '.putInt(',
'.addDouble(': '.putDouble(',
'.addShort(': '.putShort(',
'.addCompound(': '.put(',
'.addAll(': '.putAll(',
'.getTag().name()': '.getClass()',
'.intValue()': '.asInt()',
'.byteValue()': '.asByte()',
'.longValue()': '.asLong()',
'tag.name()': 'tag.getClass()',
'tag.getLength()': 'tag.length()',
'super.serialize(object).asCompoundTag()': '((CompoundTag) super.serialize(object))',
'list.getType().isNumber()': 'list.getElementType().isAssignableFrom(com.viaversion.nbt.tag.NumberTag.class)',
'this.styleSerializer.serialize(object.getStyle()).asCompoundTag()': '(CompoundTag) this.styleSerializer.serialize(object.getStyle())',
'JsonNbtConverter.toNbt(json).asCompoundTag()': '((CompoundTag) JsonNbtConverter.toNbt(json))',
'Tag.COMPOUND.equals(list.getType())': 'CompoundTag.class == list.getElementType()',
'Tag.COMPOUND.equals(listTag.getType())': 'CompoundTag.class == listTag.getElementType()',
'compound.add(': 'compound.put(',
'.numberValue()': '.getValue()',
'entry.getKey().isStringTag()': '(entry.getKey() instanceof StringTag)',
'entry.getKey().asStringTag()': '((StringTag) entry.getKey())',
'tags.get(0).getTag()': 'tags.get(0)',
'Tag::isByteTag': 'tag -> tag instanceof ByteTag',
'Tag::isIntTag': 'tag -> tag instanceof IntTag',
'Tag::isLongTag': 'tag -> tag instanceof LongTag',
'Tag::isNumberTag': 'tag -> tag instanceof NumberTag',
'elements.get(i).asByteTag()': '((ByteTag) elements.get(i))',
'elements.get(i).asIntTag()': '((IntTag) elements.get(i))',
'elements.get(i).asLongTag()': '((LongTag) elements.get(i))',
'list.get(i).asNumberTag()': '((NumberTag) list.get(i))',
'values.get(i).asNumberTag().asInt()': '((NumberTag) values.get(i)).asInt()',
'values.get(i).asNumberTag().asByte()': '((NumberTag) values.get(i)).asByte()',
'values.get(i).asNumberTag().asLong()': '((NumberTag) values.get(i)).asLong()',
'(ACTION)': '("action")',
'(CONTENTS, ': '("contents", ',
'(CONTENTS)': '("contents")',
# Special ones
'!type.equals(tags.get(i).getTag())': "type.getClass() != tags.get(i).getClass()",
'String type = ((StringTag) tag.get("type")).getValue()': 'String type = tag.contains("type") ? ((StringTag) tag.get("type")).getValue() : null'
}
def main():
os.chdir('MCStructs')
handle_file('build.gradle')
handle_file('gradle.properties')
deep('MCStructs-snbt')
deep('MCStructs-text')
deep('MCStructs-converter')
# Apply additional manual changes
apply_patch('../patch.patch')
def apply_patch(patch_file):
try:
# git apply --reject --ignore-whitespace --ignore-space-change ../patch.patch
subprocess.run(['git', 'apply', '--reject', '--ignore-whitespace', '--ignore-space-change', patch_file],
check=True)
print('Applied patch')
except subprocess.CalledProcessError as e:
print(f'Error applying patch: {e}')
def deep(path):
for p in os.listdir(path):
p = os.path.join(path, p)
if os.path.isdir(p):
deep(p)
elif p.endswith(".java") or p.endswith(".gradle"):
handle_file(p)
def to_camel_case(s):
words = s.split('_')
camel_case_words = [words[0].capitalize()] + [word.capitalize() for word in words[1:]]
return ''.join(camel_case_words)
def replace_get_value(content, obj):
# First apply nullable replacements with the generic replacement, THEN the rest
content = replace_nullable_get(obj, 'Compound', '', 'null', content)
content = replace_nullable_get(obj, 'List', '', 'null', content)
content = re.sub(fr'{obj}\.get(\w+)\(([^,)]+), null\)',
fr'({obj}.get(\2) instanceof \1Tag ? ((\1Tag) {obj}.get(\2)).getValue() : null)',
content)
content = replace_nonnull_get(obj, 'Compound', '', 'new CompoundTag()', content)
content = replace_nonnull_get(obj, 'List', '', 'new ListTag<>()', content)
content = replace_nonnull_get(obj, 'String', '.getValue()', '""', content)
content = replace_nonnull_get(obj, 'ByteArray', '.getValue()', 'new byte[0]', content)
content = replace_nonnull_get(obj, 'IntArray', '.getValue()', 'new int[0]', content)
content = replace_nonnull_get(obj, 'LongArray', '.getValue()', 'new long[0]', content)
# Booleans are stored as byte tags
content = re.sub(fr'{obj}\.getBoolean\(([^)]+)\)',
fr'({obj}.get(\1) instanceof ByteTag ? ((ByteTag) {obj}.get(\1)).asBoolean() : false)',
content)
# something.get(y).asXTag() -> ((XTag) something.get(y))
content = re.sub(fr'{obj}\.get\(([^)]+)\).as(\w+Tag)\(\)',
fr'((\2) {obj}.get(\1))',
content)
# Tag::asXTag -> tag -> ((XTag) tag)
content = re.sub(r'Tag::as(\w+Tag)', r'tag -> ((\1) tag)', content)
numeric_types = {'Byte', 'Short', 'Int', 'Long', 'Float', 'Double'}
for numeric_type in numeric_types:
content = replace_nonnull_get(obj, numeric_type, f'.as{numeric_type}()', '0', content)
return content
def replace_nullable_get(obj, tag_type, value_method, default, content):
pattern = re.compile(fr'{obj}\.get{tag_type}\(([^,)]+), null\)')
replacement = fr'({obj}.get(\1) instanceof {tag_type}Tag ? (({tag_type}Tag) {obj}.get(\1)){value_method} : {default})'
return pattern.sub(replacement, content)
def replace_nonnull_get(obj, tag_type, value_method, default, content):
pattern = re.compile(fr'{obj}\.get{tag_type}\(([^)]+)\)')
replacement = fr'({obj}.get(\1) instanceof {tag_type}Tag ? (({tag_type}Tag) {obj}.get(\1)){value_method} : {default})'
return pattern.sub(replacement, content)
def handle_file(path):
with open(path, 'r') as file:
content = file.read()
changed_content = content
for old, new in replacements.items():
changed_content = changed_content.replace(old, new)
name = os.path.basename(path)
if 'CompoundTag' in changed_content:
# Some are very explicit, so that they don't break random code elsewhere/make it look like it worked fine
for old, new in extra_replacements.items():
changed_content = changed_content.replace(old, new)
if 'HoverEventSerializer' not in name:
changed_content = changed_content.replace('.add("', '.put("')
# tag.isXTag() -> (tag instanceof XTag)
# Special case ArrayTag
changed_content = re.sub(r'(\w+)\.isArrayTag\(\)', r'(\1 instanceof NumberArrayTag)', changed_content)
changed_content = re.sub(r'(\w+)\.is(\w+Tag)\(\)', r'(\1 instanceof \2)', changed_content)
# asXTag() -> cast
# Deal with the base ArrayTag separately
changed_content = re.sub(r'(\w+)\.asArrayTag\(\)', r'((NumberArrayTag) \1)', changed_content)
changed_content = re.sub(r'(\w+)\.as(\w+Tag)\(\)', r'((\2) \1)', changed_content)
changed_content = re.sub(r'tags.get\(i\)\.as(\w+Tag)\(\)', r'((\1) tags.get(i))', changed_content)
# tag.contains(s, Tag.X) -> tag.get(s) instanceof XTag
changed_content = re.sub(r'(\w+)\.contains\(([^,)]+), Tag\.(\w+)\)',
lambda m: f'({m.group(1)}.get({m.group(2)}) instanceof {to_camel_case(m.group(3))}Tag)',
changed_content)
changed_content = re.sub(r'\.contains\(([^,)]+), Tag\.(\w+)\)',
lambda m: f'.get({m.group(1)}) instanceof {to_camel_case(m.group(2))}Tag',
changed_content)
# Tag.X.equals(tagType) -> tagType instance XTag
changed_content = re.sub(r'Tag\.(\w+).equals\((\w+)\)',
lambda m: f'({m.group(2)} instanceof {to_camel_case(m.group(1))}Tag)',
changed_content)
# tag.getX -> cast to tag with default value
changed_content = replace_get_value(changed_content, 'tag')
changed_content = replace_get_value(changed_content, 'rawTag')
changed_content = replace_get_value(changed_content, 'rawEntity')
changed_content = replace_get_value(changed_content, 'score')
changed_content = replace_get_value(changed_content, 'clickEvent')
changed_content = replace_get_value(changed_content, 'parsed')
if content == changed_content:
return
with open(path, 'w') as file:
file.write(changed_content)
print('Wrote to', path)
if __name__ == '__main__':
main()