-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustom_gates.py
189 lines (156 loc) · 5.9 KB
/
custom_gates.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
# pyinstaller.exe --onefile --paths=./ --add-data "assets/all_signs_24.png;assets/" custom_gates.py
import sys
import os
import datetime
from pathlib import Path
from copy import deepcopy
from src.parser import parse_node, generate_node
from src.utils import update_surf
from construct import Container, ListContainer
from PIL import Image, ImageDraw
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
BASE_GATE = "Turbo"
GATES = [
"Turbo",
"Turbo2",
"SlowMotion",
"Reset",
"NoSteering",
"NoEngine",
"NoBrake",
"Fragile",
"Cruise",
"Boost",
"Boost2",
]
GATES_TO_GAMEPLAYID = {
"Turbo": "Turbo",
"Turbo2": "Turbo2",
"SlowMotion": "SlowMotion",
"Reset": "Reset",
"NoSteering": "NoSteering",
"NoEngine": "FreeWheeling",
"NoBrake": "NoBrakes",
"Fragile": "Fragile",
"Cruise": "Cruise",
"Boost": "ReactorBoost_Oriented",
"Boost2": "ReactorBoost2_Oriented",
}
TEXTURES_LINK_REMAPPING = {
"TriggerFXTurbo": lambda gate: "Stadium\\Media\\Modifier\\" + gate + "\\TriggerFX",
"SpecialFXTurbo": lambda gate: "Stadium\\Media\\Modifier\\" + gate + "\\SpecialFX",
"SpecialSignTurbo": lambda gate: "Stadium\\Media\\Modifier\\" + gate + "\\Sign",
"SpecialSignOff": lambda gate: "Stadium\\Media\\Modifier\\"
+ gate
+ "\\Sign"
+ ("Off" if gate in ["Turbo", "Turbo2", "Boost", "Boost2"] else ""),
"DecalSpecialTurbo": lambda gate: "Stadium\\Media\\Modifier\\" + gate + "\\Decal",
}
TEXTURES_NAME_REMAPPING = {
"TriggerFXTurbo": lambda gate: gate + "_TriggerFX",
"SpecialFXTurbo": lambda gate: gate + "_SpecialFX",
"SpecialSignTurbo": lambda gate: gate + "_Sign",
"SpecialSignOff": lambda gate: gate + "_SignOff",
"DecalSpecialTurbo": lambda gate: gate + "_Decal",
}
def add_gate_from_trigger(data):
entity_model_idx = data.body[12].chunk.EntityModel
static_object_idx = data.nodes[entity_model_idx].body[0].chunk.staticObject
trigger_idx = data.nodes[entity_model_idx].body[0].chunk.props.triggerArea
# Create the gate
gate_index = len(data.nodes)
data.nodes.append(
Container(
header=Container(class_id=0x09179000),
body=Container(
version=1,
surf=trigger_idx,
),
)
)
# Replace the static object
data.nodes[entity_model_idx] = Container(
header=Container(class_id=0x09145000),
body=Container(
version=11,
updatedTime=datetime.datetime.now(),
url="",
u01=b"\x00\x00\x00\x00",
u02=b"\x00\x00\x00\x00",
Ents=ListContainer(
[
Container(
model=static_object_idx,
rot=Container(x=0, y=0, z=0, w=1),
pos=Container(x=0, y=0, z=0),
LodGroupId=-1,
name="",
),
Container(
model=gate_index,
rot=Container(x=0, y=0, z=0, w=1),
pos=Container(x=0, y=0, z=0),
LodGroupId=-1,
name="",
),
]
),
),
)
return data
def change_textures(data, gate):
for node in data.nodes:
if type(node) == Container:
if node.header.class_id == 0x090FD000:
for key in TEXTURES_LINK_REMAPPING:
if key in node.body[0].chunk.link:
node.body[0].chunk.isUsingGameMaterial = True
node.body[0].chunk.materialName = TEXTURES_NAME_REMAPPING[key](
gate
)
node.body[0].chunk.link = TEXTURES_LINK_REMAPPING[key](gate)
if __name__ == "__main__":
file = sys.argv[1]
data, nb_nodes, raw_bytes = parse_node(file)
data.body[16].chunk.u08 = 0
export_dir = Path(os.path.dirname(os.path.abspath(file)))
data = add_gate_from_trigger(data)
entity_model_idx = data.body[12].chunk.EntityModel
gate_idx = data.nodes[entity_model_idx].body.Ents[1].model
surf_idx = data.nodes[gate_idx].body.surf
with Image.open(resource_path("assets/all_signs_24.png")) as im_signs:
for gate_idx, gate in enumerate(GATES):
data_gate = deepcopy(data)
# modify item name
file_name_suffix = "_" + gate
data_gate.header.chunks.data[0].file_name += file_name_suffix
data_gate.body[2].chunk.name += file_name_suffix
# update the gameplay
update_surf(
data_gate.nodes[surf_idx], "NotCollidable", GATES_TO_GAMEPLAYID[gate]
)
# update the textures
if gate != BASE_GATE:
change_textures(data_gate, gate)
# update the icon
icon_chunk = data_gate.header.chunks.data[1]
if not icon_chunk.webp:
for y in range(24):
for x in range(24):
dest_idx = (24 - y) * 64 + (40 + x)
px = im_signs.getpixel((gate_idx * 24 + x, y))
icon_chunk.data[dest_idx] = Container(
r=px[0], g=px[1], b=px[2], a=255
)
# save the new item
new_bytes = generate_node(data_gate, True)
new_file_name = os.path.basename(file).split(".")
new_file_name[-3] += file_name_suffix
new_file_name = ".".join(new_file_name)
export_file_name = export_dir / new_file_name
with open(export_file_name, "wb") as f:
f.write(new_bytes)
print(export_file_name)