-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsppack_auto.py
343 lines (257 loc) · 9.57 KB
/
sppack_auto.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
import argparse
import csv
import json
import os.path
import re
import struct
from pydynamicpack.splashes_unwrapper import SplashesUnwrapper
from PIL import Image
DEBUG = True
IGNORE = [
".git",
".idea",
"__pycache__",
".DS_Store",
".gitignore",
"README.md",
".py",
]
SPLASHES_BUILDS = [
{
"name": "Default splashes",
"from": "splashes-with-metadata.txt",
"to": "sp_splashes/assets/minecraft/texts/splashes.txt"
},
{
"name": "2025 splashes",
"from": "splashes-with-metadata-2025.txt",
"to": "sp_splashes_2025/assets/minecraft/texts/splashes.txt"
}
]
def get_filepaths(directory):
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath.replace("\\", "/")) # Add it to the list.
debug(f"get_filepaths({directory}) return {file_paths}")
return file_paths # Self-explanatory.
def enablePrettyPrint():
rebuildPrettyPrint(True)
def disablePrettyPrint():
rebuildPrettyPrint(False)
def rebuildPrettyPrint(state: bool):
for e in get_filepaths("."):
isIgn = False
for ign in IGNORE:
ign = "./" + ign
if (e.startswith(ign)):
isIgn = True
if (isIgn):
continue
if (e.endswith(".json")):
cool_json = None
with open(e, "r", encoding='utf-8') as file:
try:
cool_json = json.load(file)
except Exception as err:
print(f"[ERROR] {err} while processing file {e}")
if (cool_json != None):
with open(e, "w", encoding='utf-8') as file:
if (state):
json.dump(cool_json, file, indent=4)
else:
json.dump(cool_json, file)
def isUpperCase(e: str):
for x in e:
if x.isupper():
return True
return False
def renameToLower(parent, path, x: str):
os.renames(parent + x, parent + x.lower())
print(f"RENAME {parent + x} -> {parent + x.lower()}")
def lowerCaseAll(init_dir):
for e in get_filepaths(init_dir):
upper = isUpperCase(e)
if (upper):
l = []
for x in e.split("/"):
path = "/".join(l) + "/" + x
parent = "/".join(l) + "/"
isDir = os.path.isdir(path)
if (isDir and isUpperCase(x)):
renameToLower(parent, path, x)
l.append(x)
def update_contents_csv():
dirs = open("content_directories.txt", "r", encoding='utf-8').read().split("\n")
with open("contents.csv", 'w', newline='\n', encoding='utf-8') as csvfile:
csv_writter = csv.writer(csvfile, delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writter.writerow(["id", "work_dir", "files_list", "files_list_hash"])
for dir in dirs:
#with open(dir + "/files.csv", 'rb', encoding='utf-8') as open_file:
# content = open_file.read()
csv_writter.writerow([dir.replace("/", "_"), dir, dir + "/files.csv", "ff00ffffffffffffffffffffffff"])
def debug(m):
if DEBUG:
print(f"DEBUG: {m}")
def analyze():
for path in get_filepaths("."):
if re.search("[^a-z1-90_\\-./]", path) != None:
print(path)
def fixPng_empty(path, nx, ny):
# Open the original image
original_image = Image.open(path)
# Create a new image with a solid color (white) background
new_image = Image.new("RGBA", (nx, ny), (0, 0, 0, 0))
# Paste the original image onto the center of the new image
new_image.paste(original_image, (0, 0))
# Save the result
new_image.save(path)
print(f"[Modify] Add empty in {nx}, {ny} {path}")
def fixPng_resize(path, nx, ny):
# Open the original image
original_image = Image.open(path)
original_image = original_image.resize((nx, ny), resample=Image.NEAREST)
# Save the result
original_image.save(path)
print(f"[Modify] Resized image in {nx}, {ny} {path}")
def findBadPngResolution():
for path in get_filepaths("."):
if (path.endswith(".png")):
#print(f"PNG FOUND {path}")
try:
with open(path, 'rb') as f:
data = f.read()
size = get_image_info(data)
x = size[0]
y = size[1]
if x % 16 == 0 and y % 16 == 0:
continue
print(f"Bad png {size} at {path}")
if (x == y and x >= 16) or (x % y == 0):
fixPng_resize(path, fix_png_dim(x), fix_png_dim(y))
else:
fixPng_empty(path, fix_png_dim(x), fix_png_dim(y))
## for manually...
#fixPng_resize(path, x*4, y*4)
except Exception as e:
print(f"PNG AT {path}: {e}")
def get_image_info(data):
if True:
w, h = struct.unpack('>LL', data[16:24])
width = int(w)
height = int(h)
else:
raise Exception('not a png image')
return width, height
def fix_png_dim(dim):
d = dim % 16
if d == 0:
return dim
return dim + (16 - d)
CIT_KEY_NAME = "components.custom_name"
CIT_KEY_LORE = "components.lore"
stat = {
"merged_to_components": {
"lore": 0,
"name": 0,
"remove_minecraft_namespace": 0,
"writings_on_disk": 0
},
"total_properties": 0,
"total_renames": 0
}
def processPropertiesFile(renamesFile, e):
print(e)
modified = []
stat["total_properties"] += 1
with open(e, 'r', newline='\n', encoding='utf-8') as propFile:
prop_content = propFile.read()
if "nbt.display.Name=" in prop_content:
prop_content = prop_content.replace("nbt.display.Name=", f"{CIT_KEY_NAME}=")
modified.append(f"nbt.display.Name -> {CIT_KEY_NAME}")
stat["merged_to_components"]["name"] += 1
if "nbt.display.Lore" in prop_content:
prop_content = prop_content.replace("nbt.display.Lore", f"{CIT_KEY_LORE}")
modified.append(f"replace 'nbt.display.Lore' -> {CIT_KEY_LORE}")
stat["merged_to_components"]["lore"] += 1
if "components.minecraft\\:custom_name=" in prop_content:
prop_content = prop_content.replace("components.minecraft\\:custom_name=", f"{CIT_KEY_NAME}=")
modified.append(f"remove unnecessary minecraft namespace 'components.minecraft\\:custom_name' -> {CIT_KEY_LORE}")
stat["merged_to_components"]["remove_minecraft_namespace"] += 1
lines = []
for x in prop_content.split("\n"):
if len(x.strip()) != 0:
if (not x.strip().startswith("#")):
lines.append(x.strip())
l = [line.split("=") for line in lines]
d = {r[0].strip(): r[1].strip() for r in l}
if CIT_KEY_NAME in d.keys():
if "matchItems" in d:
items = d["matchItems"]
else:
items = "nothing"
value = d[CIT_KEY_NAME].encode('raw_unicode_escape').decode('unicode_escape').replace("\"", "\"\"")
renamesFile.write(f"\"{value}\",{items},{e[2::]}\n")
stat["total_renames"] += 1
if len(modified) > 0:
print(f"Writing modified file: {modified}")
with open(e, 'w', newline='\n', encoding='utf-8') as propFile:
propFile.write(prop_content)
stat["merged_to_components"]["writings_on_disk"] += 1
def upgradeToComponentAndRenames():
with open("renames.csv", 'w', newline='\n', encoding='utf-8') as renamesFile:
for e in get_filepaths("."):
if e.endswith(".properties"):
processPropertiesFile(renamesFile, e)
print("== STAT ==")
print(json.dumps(stat, indent=2))
def splashesbuild():
sw = SplashesUnwrapper(DEBUG)
for s in SPLASHES_BUILDS:
print(f"Build splashes: {s['name']}")
sw.wrapped_file_to_unwrapped(s["from"], s["to"])
def run():
parser = argparse.ArgumentParser(description='SP Pack')
parser.add_argument('--mode', type=str, default="no_default", help='Automatically mode')
cmd = parser.parse_args().mode
print("SPPack automatization tool")
print("")
print("Select a hook")
print("[1] pretty-print optimize")
print("[2] lowercase all dirs")
print("[3] analyze")
print("[4] update_contents_csv")
print("[5] find all png with size % 16 != 0")
print("[6] Fix NBT -> Component && update renames.txt")
print("[7] splashes build")
if cmd == "no_default":
cmd = input(" ---> ")
print(f"Mode: {cmd}")
if (cmd == "1"):
i = input("[E]nable or [D]isable pretty print?\n ->> ")
if i.lower() == "e":
enablePrettyPrint()
elif (i.lower() == "d"):
disablePrettyPrint()
else:
print("failed to recognize command")
if (cmd == "2"):
print("Lowercase all dirs in ..optifine/cit")
lowerCaseAll(input("Init dir -> ") + "/assets/minecraft/optifine/cit")
if cmd == "3":
print("Analyzing...")
analyze()
if cmd == "4":
update_contents_csv()
if cmd == "5":
findBadPngResolution()
if cmd == "6":
upgradeToComponentAndRenames()
if cmd == "7":
splashesbuild()
if __name__ == "__main__":
run()