-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyles.py
More file actions
211 lines (187 loc) · 7.43 KB
/
styles.py
File metadata and controls
211 lines (187 loc) · 7.43 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
from extended_dict import ExtendedDict
from config import CONVERTER_PREFIX
from lua_file import LuaFile
import json
import log
import os
COLOR_OBJECTS = [
"main",
"player_color",
"text_color",
"wall_color"
]
CALCULATION_METHODS = [
{"dynamic": False},
{"dynamic": True, "main": True},
{"dynamic": True, "main": False, "dynamic_offset": True, "offset": 0},
{"dynamic": True, "main": False, "dynamic_offset": True},
{"dynamic": True, "main": False, "dynamic_offset": False, "dynamic_darkness": 0},
{"dynamic": True, "main": False, "dynamic_offset": False}
]
filepath = os.path.realpath(__file__)
colors3D = ExtendedDict()
id_file_mapping = ExtendedDict()
def ensure_item_count(items, count=4, default=0):
if not isinstance(items, list):
items = [items]
length = len(items)
if length > count:
return items[:count]
elif length < count:
while len(items) < count:
items.append(default)
return items
def convert_color(color):
# Set defaults to not get unexpected nils in lua
color["dynamic"] = color.get("dynamic", False)
color["dynamic_darkness"] = color.get("dynamic_darkness", 0)
color["dynamic_offset"] = color.get("dynamic_offset", False)
color["offset"] = color.get("offset", 0)
color["main"] = color.get("main", False)
color["value"] = ensure_item_count(color.get("value", [0, 0, 0, 0]))
color["pulse"] = ensure_item_count(color.get("pulse", [0, 0, 0, 0]))
color["hue_shift"] = color.get("hue_shift", 0)
calculation_method = 0
for method in CALCULATION_METHODS:
calculation_method += 1
got_match = True
for key in method:
if method[key] != color[key]:
got_match = False
if got_match:
break
color["calculation_method"] = calculation_method
if color["dynamic"] and color["dynamic_offset"] and not color["main"] and \
color["offset"] == 0:
# If these values are set this way the original color is due to division by 0
# which results in inf being added to the main color (except for the alpha
# component) reset to black
for i in range(3):
color["value"][i] = 0
# Let pulse values underflow/overflow like in 1.92
if color.get("pulse") is not None:
for i in range(4):
color["pulse"][i] %= 256
for i in range(4):
color["value"][i] %= 256
return color
def convert_style(style_json):
if style_json.get("id") is None:
log.error("Style file", style_json.path, "has no id!")
return
for color in COLOR_OBJECTS:
if color in style_json:
style_json[color] = convert_color(style_json[color])
has_none = False
no_colors = True
for i in range(len(style_json.get("colors", []))):
no_colors = False
if not isinstance(style_json["colors"][i], dict):
del style_json["colors"][i]
has_none = True
else:
style_json["colors"][i] = convert_color(style_json["colors"][i])
# Limit depth to 100 like 1.92 does with unmodified config
depth = style_json.get("3D_depth", 15)
if depth < 0:
depth = 0
if depth > 100:
depth = 100
style_json["3D_depth"] = depth
# Divide 3D_spacing by 1.4 because the steam version multiplies it by 1.4
style_json["3D_spacing"] = style_json.get("3D_spacing", 1) / 1.4
# This way the first 3D layer is rendered inside the main layer just like in 1.92
style_json["3D_layer_offset"] = -1
style_json["3D_alpha_mirror"] = True
# 1.92 casts float depths to int while the steam version just crashes
style_json["3D_depth"] = int(style_json["3D_depth"])
# Set defaults
style_json["max_swap_time"] = style_json.get("max_swap_time", 100)
style_json["3D_pulse_speed"] = style_json.get("3D_pulse_speed", 0.01)
style_json["3D_pulse_min"] = style_json.get("3D_pulse_min", 0)
style_json["3D_pulse_max"] = style_json.get("3D_pulse_max", 3.2)
style_json["pulse_min"] = style_json.get("pulse_min", 0)
style_json["pulse_max"] = style_json.get("pulse_max", 0)
style_json["pulse_increment"] = style_json.get("pulse_increment", 0)
style_json["hue_min"] = style_json.get("hue_min", 0)
style_json["hue_max"] = style_json.get("hue_max", 0)
style_json["hue_ping_pong"] = style_json.get("hue_ping_pong", False)
style_json["hue_increment"] = style_json.get("hue_increment", 0)
# Save style for use in lua
filename = os.path.basename(style_json.path)[:-5]
id_file_mapping[style_json["id"]] = filename
os.makedirs("Scripts/" + CONVERTER_PREFIX + "Styles", exist_ok=True)
lua_file = LuaFile()
lua_file.set_text(CONVERTER_PREFIX + "style=" + style_json.to_table())
lua_file.save("Scripts/" + CONVERTER_PREFIX + "Styles/" + filename + ".lua")
# Save it now for use in menu
menu_style = style_json.copy()
# Make styles with weird hues look correct most of the time (workaround for menu)
if menu_style["hue_min"] < 0 and menu_style["hue_max"] <= 0:
for color in [*menu_style.get("colors", []), menu_style["main"]]:
if color["dynamic"] and color["hue_shift"] == 0:
color["dynamic"] = False
color["value"] = [0, 0, 0, 0]
menu_style["id"] += "-menu"
os.makedirs("Styles", exist_ok=True)
with open("Styles/" + filename + "-menu.json", "w") as menu_style_file:
json.dump(menu_style, menu_style_file, indent=4)
# Set some properties to fixed values in order to remake them with lua
style_json["3D_override_color"] = [0, 0, 0, 0]
style_json["pulse_increment"] = 0
style_json["pulse_min"] = 0
style_json["pulse_max"] = 0
style_json["hue_increment"] = 0
style_json["hue_min"] = 0
style_json["hue_max"] = 0
style_json["max_swap_time"] = 0
for name in "text_color", "main":
style_json[name] = {
"dynamic": False,
"dynamic_offset": False,
"dynamic_darkness": 1,
"offset": 0,
"main": False,
"hue_shift": 0,
"value": [0, 0, 0, 0],
"pulse": [0, 0, 0, 0]
}
style_json["cap_color"] = {
"legacy": False,
"dynamic": False,
"dynamic_offset": False,
"dynamic_darkness": 1,
"offset": 0,
"main": False,
"hue_shift": 0,
"value": [0, 0, 0, 0],
"pulse": [0, 0, 0, 0]
}
if "main" in style_json:
style_json["main"]["dynamic"] = False
if has_none:
i -= 1
if no_colors:
i = 0
style_json["colors"] = [{
"dynamic": False,
"dynamic_offset": False,
"dynamic_darkness": 1,
"offset": 0,
"main": False,
"hue_shift": 0,
"value": [0, 0, 0, 0],
"pulse": [0, 0, 0, 0]
}]
for i in range(len(style_json.get("colors", []))):
style_json["colors"][i]["value"] = [0, 0, 0, 0]
style_json["colors"][i]["pulse"] = [0, 0, 0, 0]
style_json["colors"][i]["dynamic"] = False
def convert_lua(level_lua, level_json):
level_lua.mixin_line(CONVERTER_PREFIX + "style_id=\"" + level_json["styleId"] +
"\"", CONVERTER_PREFIX + "onInit")
level_json["styleId"] += "-menu"
def save(packdata):
packdata.mixin_line(CONVERTER_PREFIX + "STYLE_ID_FILE_MAPPING=" + id_file_mapping
.to_table(), line=1)
packdata.save("Scripts/" + CONVERTER_PREFIX + "packdata.lua")