-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshp_color_remapper.py
More file actions
345 lines (271 loc) · 9.98 KB
/
shp_color_remapper.py
File metadata and controls
345 lines (271 loc) · 9.98 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
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
344
345
#!/usr/bin/env python3
"""
SHP Color Index Remapper - Korrekte Version
Ändert nur Pixel-Farbindizes in OpenRA .shp Dateien, ohne Header/Struktur zu beschädigen
"""
import struct
import os
import sys
from typing import List, Tuple, Dict
class SHPFrame:
def __init__(self):
self.x = 0
self.y = 0
self.width = 0
self.height = 0
self.data = b''
def read_uint16(data, offset):
return struct.unpack('<H', data[offset:offset+2])[0], offset + 2
def read_uint32(data, offset):
return struct.unpack('<I', data[offset:offset+4])[0], offset + 4
def read_int16(data, offset):
return struct.unpack('<h', data[offset:offset+2])[0], offset + 2
def write_uint16(value):
return struct.pack('<H', value)
def write_uint32(value):
return struct.pack('<I', value)
def write_int16(value):
return struct.pack('<h', value)
def decode_shp_frame(data, offset, width, height):
"""Dekodiert einen SHP Frame (Format 80h - RLE komprimiert)"""
pixels = bytearray(width * height)
pos = 0
data_pos = offset
# Zeilen-Offsets lesen
line_offsets = []
for _ in range(height):
if data_pos + 2 > len(data):
return pixels
line_offset, data_pos = read_uint16(data, data_pos)
line_offsets.append(line_offset + offset)
# Jede Zeile dekodieren
for y in range(height):
if line_offsets[y] >= len(data):
continue
line_pos = line_offsets[y]
x = 0
while x < width and line_pos < len(data):
cmd = data[line_pos]
line_pos += 1
if cmd == 0: # End of line
break
elif cmd & 0x80: # Skip transparent pixels
x += cmd & 0x7F
elif cmd & 0x40: # RLE compressed pixels
if line_pos >= len(data):
break
count = cmd & 0x3F
color = data[line_pos]
line_pos += 1
for _ in range(count):
if x < width:
pixels[y * width + x] = color
x += 1
else: # Raw pixels
count = cmd
for _ in range(count):
if line_pos >= len(data) or x >= width:
break
pixels[y * width + x] = data[line_pos]
line_pos += 1
x += 1
return pixels
def encode_shp_frame(pixels, width, height):
"""Enkodiert Pixel-Daten zurück ins SHP RLE Format"""
lines_data = []
for y in range(height):
line_data = bytearray()
x = 0
while x < width:
# Transparent pixels zählen (Index 0)
transparent_count = 0
while x + transparent_count < width and pixels[y * width + x + transparent_count] == 0:
transparent_count += 1
if transparent_count > 0:
# Skip transparent pixels (max 127 per command)
while transparent_count > 0:
skip = min(127, transparent_count)
line_data.append(0x80 | skip)
transparent_count -= skip
x += skip
continue
# Nicht-transparente Pixels
start_x = x
while x < width and pixels[y * width + x] != 0:
x += 1
pixel_count = x - start_x
# RLE oder Raw encoding wählen
if pixel_count > 1:
# Schauen ob RLE lohnt sich
current_color = pixels[y * width + start_x]
rle_count = 1
while start_x + rle_count < x and pixels[y * width + start_x + rle_count] == current_color:
rle_count += 1
if rle_count >= 3: # RLE lohnt sich
rle_count = min(63, rle_count) # Max 63 per RLE command
line_data.append(0x40 | rle_count)
line_data.append(current_color)
start_x += rle_count
x = start_x
continue
# Raw pixels (max 63 per command)
raw_count = min(63, pixel_count)
line_data.append(raw_count)
for i in range(raw_count):
line_data.append(pixels[y * width + start_x + i])
start_x += raw_count
x = start_x
line_data.append(0) # End of line
lines_data.append(bytes(line_data))
# Zeilen-Offsets berechnen
offset_table_size = height * 2
current_offset = offset_table_size
offsets = []
for line in lines_data:
offsets.append(current_offset)
current_offset += len(line)
# Zusammenbauen
result = bytearray()
# Offset-Tabelle
for offset in offsets:
result.extend(write_uint16(offset))
# Zeilen-Daten
for line in lines_data:
result.extend(line)
return bytes(result)
def parse_shp_file(data):
"""Parsed eine SHP-Datei und gibt Frames zurück"""
if len(data) < 2:
return []
frame_count, pos = read_uint16(data, 0)
frames = []
for i in range(frame_count):
if pos + 6 > len(data):
break
frame_offset, pos = read_uint32(data, pos)
format_byte = data[pos]
pos += 1
width = data[pos]
pos += 1
height = data[pos]
pos += 1
if format_byte != 0x80:
print(f"Warnung: Unbekanntes Format {format_byte:02x} in Frame {i}")
continue
if frame_offset >= len(data):
print(f"Warnung: Ungültiger Offset {frame_offset} in Frame {i}")
continue
frame = SHPFrame()
frame.width = width
frame.height = height
# Frame dekodieren
pixels = decode_shp_frame(data, frame_offset, width, height)
frame.data = bytes(pixels)
frames.append((frame_offset, format_byte, frame))
return frames
def remap_colors_in_pixels(pixels, color_map):
"""Ändert Farbindizes in Pixel-Daten"""
result = bytearray(pixels)
changes = 0
for i in range(len(result)):
if result[i] in color_map:
result[i] = color_map[result[i]]
changes += 1
return bytes(result), changes
def rebuild_shp_file(frames, original_data):
"""Baut SHP-Datei mit geänderten Frames neu auf"""
if not frames:
return original_data
frame_count = len(frames)
# Header aufbauen
result = bytearray()
result.extend(write_uint16(frame_count))
# Platz für Frame-Einträge reservieren
header_size = 2 + frame_count * 7 # 2 für count + 7 bytes per frame
frame_data_start = header_size
frame_entries = []
current_offset = frame_data_start
# Frame-Daten enkodieren
encoded_frames = []
for original_offset, format_byte, frame in frames:
# Pixel-Daten zu RLE enkodieren
encoded_data = encode_shp_frame(frame.data, frame.width, frame.height)
encoded_frames.append(encoded_data)
# Frame-Eintrag
frame_entries.append({
'offset': current_offset,
'format': format_byte,
'width': frame.width,
'height': frame.height
})
current_offset += len(encoded_data)
# Frame-Einträge schreiben
for entry in frame_entries:
result.extend(write_uint32(entry['offset']))
result.append(entry['format'])
result.append(entry['width'])
result.append(entry['height'])
# Frame-Daten anhängen
for encoded_data in encoded_frames:
result.extend(encoded_data)
return bytes(result)
def process_shp_file(file_path, color_map):
"""Verarbeitet eine SHP-Datei"""
print(f"Verarbeite: {file_path}")
# Backup erstellen
backup_path = file_path + ".backup"
if not os.path.exists(backup_path):
print(f"Erstelle Backup: {backup_path}")
with open(file_path, 'rb') as src, open(backup_path, 'wb') as dst:
dst.write(src.read())
# Datei laden
with open(file_path, 'rb') as f:
data = f.read()
# SHP parsen
frames = parse_shp_file(data)
if not frames:
print("Keine gültigen Frames gefunden")
return False
print(f"Gefundene Frames: {len(frames)}")
# Farbindizes in jedem Frame ändern
total_changes = 0
modified_frames = []
for original_offset, format_byte, frame in frames:
new_pixels, changes = remap_colors_in_pixels(frame.data, color_map)
frame.data = new_pixels
total_changes += changes
modified_frames.append((original_offset, format_byte, frame))
print(f"Farbindizes geändert: {total_changes}")
if total_changes > 0:
# SHP-Datei neu aufbauen
new_data = rebuild_shp_file(modified_frames, data)
# Speichern
with open(file_path, 'wb') as f:
f.write(new_data)
print("Datei gespeichert")
return True
else:
print("Keine Änderungen nötig")
return False
def main():
# Farbindex-Mapping: 229-239 -> 80-90
color_map = {}
for i in range(11):
old_index = 229 + i
new_index = 80 + i
color_map[old_index] = new_index
print("Farbindex-Mapping:")
for old, new in color_map.items():
print(f" {old} -> {new}")
shp_path = r"E:\src\YMCA - cameo engine integration - Kopie\mods\ca\bits\misc\parachute.shp"
if not os.path.exists(shp_path):
print(f"FEHLER: Datei nicht gefunden: {shp_path}")
return 1
success = process_shp_file(shp_path, color_map)
if success:
print("Erfolgreich abgeschlossen!")
else:
print("Verarbeitung fehlgeschlagen")
return 0
if __name__ == "__main__":
sys.exit(main())