Skip to content

Commit d23f247

Browse files
authored
Fix: abbarrslab_d textures in abbarracks w3d (#1405)
1 parent 666b46e commit d23f247

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.*
22
!.gitattributes
33
!.gitignore
4+
5+
__pycache__/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from w3dfilemanager import W3dFileManager
2+
3+
if __name__ == "__main__":
4+
manager = W3dFileManager()
5+
manager.rename_texture("ABBarracks_D.W3D", b"ATBarrSlab_E.tga", b"ATBarrSlab_D.tga")
6+
manager.rename_texture("ABBarracks_DN.W3D", b"ATBarrSlab_E.tga", b"ATBarrSlab_D.tga")
7+
manager.rename_texture("ABBarracks_DS.W3D", b"ATBarrSlab_ES.tga", b"ATBarrSlab_DS.tga")
8+
manager.rename_texture("ABBarracks_DNS.W3D", b"ATBarrSlab_ES.tga", b"ATBarrSlab_DS.tga")
9+
manager.write_out()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
3+
g_this_dir = os.path.dirname(os.path.abspath(__file__))
4+
5+
class W3dFile:
6+
path: str
7+
data: bytes
8+
9+
10+
class W3dFileManager:
11+
file_dict: dict[str, W3dFile]
12+
13+
14+
def __init__(self):
15+
self.file_dict = dict[str, W3dFile]()
16+
17+
18+
def get_or_create_w3d_file(self, file_path: str) -> W3dFile:
19+
if not os.path.exists(file_path):
20+
file_path = os.path.join(g_this_dir, file_path)
21+
if not os.path.exists(file_path):
22+
file_path = os.path.join(os.getcwd(), file_path)
23+
if not os.path.exists(file_path):
24+
raise FileNotFoundError(file_path)
25+
26+
w3dfile: W3dFile = self.file_dict.get(file_path)
27+
if w3dfile == None:
28+
w3dfile = W3dFile()
29+
with open(file_path, "rb") as file:
30+
w3dfile.path = file_path
31+
w3dfile.data = file.read()
32+
self.file_dict[file_path] = w3dfile
33+
34+
return w3dfile
35+
36+
37+
def rename_texture(self, file_path: str, replace_from: str, replace_to: str) -> None:
38+
w3dfile: W3dFile = self.get_or_create_w3d_file(file_path)
39+
if not w3dfile.data.find(replace_from):
40+
raise AttributeError(replace_from, "not found")
41+
if len(replace_from) != len(replace_to):
42+
raise AttributeError(replace_from, replace_to, "mismatching lengths")
43+
w3dfile.data = w3dfile.data.replace(replace_from, replace_to)
44+
45+
46+
def write_out(self):
47+
w3dfile: W3dFile
48+
for w3dfile in self.file_dict.values():
49+
old_file_root = os.path.dirname(w3dfile.path)
50+
old_file_name = os.path.basename(w3dfile.path)
51+
new_file_root = os.path.join(old_file_root, "edited")
52+
new_file_path = os.path.join(new_file_root, old_file_name)
53+
os.makedirs(new_file_root, exist_ok=True)
54+
with open(new_file_path, "wb") as new_file:
55+
new_file.write(w3dfile.data)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)