Skip to content

Commit

Permalink
Better J3D type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
LagoLunatic committed Oct 12, 2023
1 parent 6fd8bc4 commit 96406ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
25 changes: 14 additions & 11 deletions asset_dumper.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@

import os
import re
from PIL import Image

from gclib.gcm import GCM
from gclib.rarc import RARC
from gclib.j3d import J3D
from gclib.j3d_chunks.tex1 import TEX1
from gclib.bti import BTI

class AssetDumper:
def __init__(self):
self.succeeded_file_count = 0
self.failed_file_paths = []

def get_all_gcm_file_paths(self, gcm):
def get_all_gcm_file_paths(self, gcm: GCM):
all_file_paths = list(gcm.files_by_path.keys())

# Sort the file names for determinism. And use natural sorting so the room numbers are in order.
Expand All @@ -20,7 +23,7 @@ def get_all_gcm_file_paths(self, gcm):

return all_file_paths

def get_all_rarc_file_paths(self, rarc):
def get_all_rarc_file_paths(self, rarc: RARC):
all_file_paths = []
for file_entry in rarc.file_entries:
if file_entry.is_dir:
Expand All @@ -31,7 +34,7 @@ def get_all_rarc_file_paths(self, rarc):

return all_file_paths

def dump_all_textures_in_gcm(self, gcm, out_dir):
def dump_all_textures_in_gcm(self, gcm: GCM, out_dir):
all_file_paths = self.get_all_gcm_file_paths(gcm)

files_checked = 0
Expand All @@ -55,7 +58,8 @@ def dump_all_textures_in_gcm(self, gcm, out_dir):
elif file_ext in [".bmd", ".bdl", ".bmt"]:
out_path = os.path.join(out_dir, rel_dir, base_name + file_ext)
j3d_file = J3D(gcm.get_changed_file_data(file_path))
self.dump_all_textures_in_j3d_file(j3d_file, out_path)
if j3d_file.tex1 is not None:
self.dump_all_textures_in_tex1(j3d_file.tex1, out_path)
except Exception as e:
display_path = file_path
self.failed_file_paths.append(display_path)
Expand Down Expand Up @@ -87,7 +91,8 @@ def dump_all_textures_in_rarc(self, rarc: RARC, out_dir, display_path_prefix=Non
elif file_ext in [".bmd", ".bdl", ".bmt"]:
out_path = os.path.join(out_dir, rel_dir, base_name + file_ext)
j3d_file = rarc.get_file(file_entry.name, J3D)
self.dump_all_textures_in_j3d_file(j3d_file, out_path)
if j3d_file.tex1 is not None:
self.dump_all_textures_in_tex1(j3d_file.tex1, out_path)
elif file_ext == ".arc":
out_path = os.path.join(out_dir, rel_dir, base_name + file_ext)
inner_rarc = rarc.get_file(file_entry.name, RARC)
Expand All @@ -99,17 +104,15 @@ def dump_all_textures_in_rarc(self, rarc: RARC, out_dir, display_path_prefix=Non
files_checked += 1
yield(display_path, files_checked)

def dump_all_textures_in_j3d_file(self, j3d_file, out_dir):
if not hasattr(j3d_file, "tex1"):
return
def dump_all_textures_in_tex1(self, tex1: TEX1, out_dir):
if not os.path.isdir(out_dir):
os.makedirs(out_dir)

for texture_name, textures in j3d_file.tex1.textures_by_name.items():
for texture_name, textures in tex1.textures_by_name.items():
if len(textures) == 0:
continue

images = []
images: list[Image.Image] = []
for i, texture in enumerate(textures):
is_duplicate = False
for prev_texture in textures[:i]:
Expand Down Expand Up @@ -141,7 +144,7 @@ def dump_all_textures_in_j3d_file(self, j3d_file, out_dir):

self.succeeded_file_count += 1

def dump_texture(self, bti, out_path):
def dump_texture(self, bti: BTI, out_path):
out_dir = os.path.dirname(out_path)
if not os.path.isdir(out_dir):
os.makedirs(out_dir)
Expand Down
6 changes: 3 additions & 3 deletions gcft_ui/j3d_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from gclib.animation import AnimationKeyframe
from gclib.j3d_chunks.inf1 import INF1, INF1Node, INF1NodeType
from gclib.j3d_chunks.vtx1 import VTX1, VertexFormat
# from gclib.j3d_chunks.evp1 import EVP1
# from gclib.j3d_chunks.drw1 import DRW1
from gclib.j3d_chunks.evp1 import EVP1
from gclib.j3d_chunks.drw1 import DRW1
from gclib.j3d_chunks.jnt1 import JNT1
from gclib.j3d_chunks.shp1 import SHP1
from gclib.j3d_chunks.mat3 import MAT3, Material
Expand All @@ -36,7 +36,7 @@ def __init__(self):
self.ui = Ui_J3DTab()
self.ui.setupUi(self)

self.j3d = None
self.j3d: J3D = None
self.j3d_name = None
self.model_loaded = False
self.anim_paused = True
Expand Down
2 changes: 1 addition & 1 deletion gclib

0 comments on commit 96406ff

Please sign in to comment.