-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
210 lines (165 loc) · 7.39 KB
/
build.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
# If you are confused about this code, please contact me on Discord (@jaegerwald)
from xmltodict import parse as parse_xml
from PIL import Image as image
from colorama import *
import os, time
if os.name == "nt": just_fix_windows_console()
xml_files = []
output = "[ MCWEBGUI BUILDER LOG FILE ]\n"
def intify_list(string_list):
integer_list = []
for item in string_list:
integer_list.append(int(item))
return integer_list
def convert2xy(position_list):
return (position_list[0], position_list[1])
def write_output(write):
global output
output += write + "\n"
with open("build.log.txt", "w") as log_file:
log_file.write(output)
log_file.close()
def create_dist():
print(Back.YELLOW + Fore.BLACK + " WRN " + Style.RESET_ALL + " Please create a " + Fore.BLACK + Style.BRIGHT + "dist " + Style.RESET_ALL + "folder before running this program.")
write_output("[WRN] Please create a \"dist\" folder before running this program.")
def dist_filled():
print(Back.YELLOW + Fore.BLACK + " WRN " + Style.RESET_ALL + " The " + Fore.BLACK + Style.BRIGHT + "dist " + Style.RESET_ALL + "folder has some files in it. Please clear it first.")
write_output("[WRN] The \"dist\" folder has some files in it. Please clear it first.")
def no_source():
print(Back.RED + Fore.BLACK + " ERR " + Style.RESET_ALL + " The " + Fore.BLACK + Style.BRIGHT + "source " + Style.RESET_ALL + "folder doesn't seem to exist. Cannot continue.")
write_output("[ERR] The \"source\" folder doesn't seem to exist. Cannot continue.")
def no_files(format):
print(Back.RED + Fore.BLACK + " ERR " + Style.RESET_ALL + " No " + Fore.BLACK + Style.BRIGHT + "." + format + Style.RESET_ALL + " files were found in the " + Fore.BLACK + Style.BRIGHT + "source" + Style.RESET_ALL + " folder!")
write_output("[ERR] No \"." + format + "\" files were found in the \"source\" folder!")
def got_file(file):
print(Back.GREEN + Fore.BLACK + " GOT " + Style.RESET_ALL + " Found the file " + Fore.BLACK + Style.BRIGHT + file + Style.RESET_ALL + ".")
write_output("[GOT] Found the file \"" + file + "\".")
def require_file(file):
print(Back.MAGENTA + Fore.BLACK + " REQ " + Style.RESET_ALL + " Requested the file " + Fore.BLACK + Style.BRIGHT + file + Style.RESET_ALL + ".")
write_output("[REQ] Requested the file \"" + file + "\".")
def pillow_action(string):
print(Back.CYAN + Fore.BLACK + " PIL " + Style.RESET_ALL + " " + string)
write_output("[PIL] " + string)
def end():
print(Back.WHITE + Fore.BLACK + " END " + Style.RESET_ALL + " All files have been processed.")
write_output("[END] All files have been processed.")
def invalid_xml():
print(Back.RED + Fore.BLACK + " ERR " + Style.RESET_ALL + " This XML file is invalid, it cannot be processed.")
write_output("[ERR] This XML file is invalid, it cannot be processed.")
def removed_empty_folders():
print(Back.WHITE + Fore.BLACK + " CLR " + Style.RESET_ALL + " Removed empty folders.")
write_output("[CLR] Removed empty folders.")
def missing_information():
print(Back.RED + Fore.BLACK + " ERR " + Style.RESET_ALL + " There seems to be some information missing. Cannot continue.")
write_output("[ERR] There seems to be some information missing. Cannot continue.")
def split_spritesheet(spritesheet, tree):
size = tree["size"].split(",")
size = intify_list(size)
sprites = []
for y in range(0, spritesheet.height, int(size[1])):
for x in range(0, spritesheet.width, size[0]):
crop_area = (
x,
y,
x + size[0],
y + size[1]
)
sprite = spritesheet.crop(crop_area)
sprite = sprite.convert("RGBA")
sprites.append(sprite)
return sprites
def make_apng(tree):
require_file(tree["file"])
spritesheet = image.open("source/" + tree["file"])
pillow_action("Opened file to make APNG.")
try:
frames = split_spritesheet(spritesheet, tree)
pillow_action("Split all APNG frames.")
frame_time = int(tree["frame-time"])
if tree["loop"] == "none":
frames[0].save("dist/" + tree["result-file"] + ".png", format="PNG", save_all=True, append_images=frames[1:], duration=frame_time, loop=1)
else:
frames[0].save("dist/" + tree["result-file"] + ".png", format="PNG", save_all=True, append_images=frames[1:], duration=frame_time, loop=int(tree["loop"]))
pillow_action("Saved APNG image.")
except: missing_information()
def split(tree):
require_file(tree["file"])
spritesheet = image.open("source/" + tree["file"])
pillow_action("Opened file for image splitting.")
try:
files = tree["files"].split(",")
images = split_spritesheet(spritesheet, tree)
pillow_action("Split all images.")
for i in range(0, len(images)):
try:
images[i].save("dist/" + files[i] + ".png", format="PNG")
except: continue
pillow_action("Saved splitted images.")
except: missing_information()
def rescale(tree):
require_file(tree["file"])
original = image.open("source/" + tree["file"])
pillow_action("Opened image for rescaling.")
try:
size = tree["size"].split(",")
size = convert2xy(intify_list(size))
rescaled_image = original.resize(size, image.NEAREST)
pillow_action("Rescaled image.")
rescaled_image.save("dist/" + tree["result-file"] + ".png", format="PNG")
pillow_action("Saved rescaled image.")
except: missing_information()
# "Indent hell is kinda gone, you're welcome." - Jaegerwald
def do_action(action, tree):
match action:
case "make-apng": make_apng(tree)
case "split": split(tree)
case "rescale": rescale(tree)
def execute_xml_actions(xml_file):
for action in xml_file["build"]:
is_list = isinstance(xml_file["build"][action], list)
if is_list:
for item in xml_file["build"][action]:
do_action(action, item)
else:
do_action(action, xml_file["build"][action])
def go_through_xml_file(xml_file):
got_file(xml_file)
os.makedirs("dist/" + xml_file.replace(".xml", ""))
try:
with open("source/" + xml_file, "r") as file:
file_content = file.read()
file.close()
file_content = file_content.replace(" ", "").replace("\n", "")
xml_file = parse_xml(file_content)
execute_xml_actions(xml_file)
except:
invalid_xml()
def remove_empty_folders():
for root, directorys, files in os.walk("dist/", topdown=False):
for directory in directorys:
directory_path = os.path.join(root, directory)
if not os.listdir(directory_path): os.rmdir(directory_path)
removed_empty_folders()
def get_xml_files():
for file in os.listdir("source"):
if file.endswith(".xml"):
xml_files.append(file)
dont_continue = False
try:
if len(os.listdir("dist")) > 0:
dist_filled()
dont_continue = True
if not dont_continue:
try:
get_xml_files()
if xml_files == []:
no_files("xml")
for xml_file in xml_files:
go_through_xml_file(xml_file)
remove_empty_folders()
end()
except:
no_source()
except:
create_dist()
time.sleep(15)