Skip to content

Commit

Permalink
implemented a very experimental function for packs
Browse files Browse the repository at this point in the history
  • Loading branch information
Faraphel committed Jan 18, 2022
1 parent fd99771 commit 6042ca4
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 146 deletions.
File renamed without changes.
58 changes: 29 additions & 29 deletions source/CT_Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@
from source.Track import Track, HiddenTrackAttr


def get_cup_icon(cup_id: [str, int], font_path: str = "./file/SuperMario256.ttf",
cup_icon_dir: str = "./file/cup_icon") -> Image:
"""
:param cup_id: id of the cup
:param cup_icon_dir: directory to cup icon
:param font_path: path to the font used to generate icon
:return: cup icon
"""
if os.path.exists(f"{cup_icon_dir}/{cup_id}.png"):
cup_icon = Image.open(f"{cup_icon_dir}/{cup_id}.png").resize((128, 128))

else:
cup_icon = Image.new("RGBA", (128, 128))
draw = ImageDraw.Draw(cup_icon)
font = ImageFont.truetype(font_path, 90)
draw.text((4, 4), "CT", (255, 165, 0), font=font, stroke_width=2, stroke_fill=(0, 0, 0))
font = ImageFont.truetype(font_path, 60)
draw.text((5, 80), "%03i" % cup_id, (255, 165, 0), font=font, stroke_width=2, stroke_fill=(0, 0, 0))

return cup_icon


class CT_Config:
def __init__(self, version: str = None, name: str = None, nickname: str = None,
game_variant: str = "01", gui=None, region: int = None, cheat_region: int = None,
Expand Down Expand Up @@ -117,6 +95,27 @@ def create_ctfile(self, directory: str = "./file/", highlight_version: str = Non
ctfile.write(cup.get_ctfile_cup(race=False, **kwargs))
rctfile.write(cup.get_ctfile_cup(race=True, **kwargs))

def get_cup_icon(self, cup_id: [str, int], font_path: str = "./assets/SuperMario256.ttf",
cup_icon_dir: str = "./file/cup_icon") -> Image:
"""
:param cup_id: id of the cup
:param cup_icon_dir: directory to cup icon
:param font_path: path to the font used to generate icon
:return: cup icon
"""
if os.path.exists(f"{cup_icon_dir}/{cup_id}.png"):
cup_icon = Image.open(f"{cup_icon_dir}/{cup_id}.png").resize((128, 128))

else:
cup_icon = Image.new("RGBA", (128, 128))
draw = ImageDraw.Draw(cup_icon)
font = ImageFont.truetype(font_path, 90)
draw.text((4, 4), "CT", (255, 165, 0), font=font, stroke_width=2, stroke_fill=(0, 0, 0))
font = ImageFont.truetype(font_path, 60)
draw.text((5, 80), "%03i" % cup_id, (255, 165, 0), font=font, stroke_width=2, stroke_fill=(0, 0, 0))

return cup_icon

def get_cticon(self) -> Image:
"""
get all cup icon into a single image
Expand All @@ -133,7 +132,7 @@ def get_cticon(self) -> Image:

for index, cup_id in enumerate(icon_files):
# index is a number, id can be string or number ("left", 0, 12, ...)
cup_icon = get_cup_icon(cup_id)
cup_icon = self.get_cup_icon(cup_id, cup_icon_dir=self.pack_path+"/file/cup_icon/")
ct_icon.paste(cup_icon, (0, index * CT_ICON_WIDTH))

return ct_icon
Expand All @@ -159,19 +158,20 @@ def load_ctconfig_json(self, ctconfig_json: dict, pack_path: str):
self.unordered_tracks = []
self.all_tracks = []

self.pack_path = pack_path

# default track
self.default_track = Track()
self.default_track = Track(track_wu8_dir=f"{self.pack_path}/file/Track-WU8/")
if "default_track" in ctconfig_json: self.default_track.load_from_json(ctconfig_json["default_track"])

for cup_json in ctconfig_json["cup"] if "cup" in ctconfig_json else []: # tracks with defined order
cup = Cup(default_track=self.default_track)
cup.load_from_json(cup_json)
if not cup.locked: # locked cup are not useful (they are original track or random track)
self.ordered_cups.append(cup)
self.all_tracks.extend(cup.tracks)
self.ordered_cups.append(cup)
self.all_tracks.extend(cup.tracks)

for track_json in ctconfig_json["tracks_list"] if "tracks_list" in ctconfig_json else []: # unordered tracks
track = Track()
track = Track(track_wu8_dir=f"{self.pack_path}/file/Track-WU8/")
track.load_from_json(track_json)
self.unordered_tracks.append(track)
self.all_tracks.append(track)
Expand All @@ -182,7 +182,7 @@ def load_ctconfig_json(self, ctconfig_json: dict, pack_path: str):
self.nickname = ctconfig_json["nickname"] if "nickname" in ctconfig_json else self.name
if "game_variant" in ctconfig_json: self.game_variant = ctconfig_json["game_variant"]

for param in ["region", "cheat_region", "tags_color", "prefix_list", "suffix_list", "tag_retro", "pack_path"]:
for param in ["region", "cheat_region", "tags_color", "prefix_list", "suffix_list", "tag_retro"]:
setattr(self, param, ctconfig_json.get(param))

return self
Expand Down
5 changes: 1 addition & 4 deletions source/Cup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def __init__(self,
track2: Track = None,
track3: Track = None,
track4: Track = None,
locked: bool = False,
*args, **kwargs):
"""
class of a cup
Expand All @@ -18,13 +17,11 @@ class of a cup
:param track2: second track
:param track3: third track
:param track4: fourth track
:param locked: is the track locked (used to load ctconfig in CT_Config)
:param args: other args that I could add in the future
:param kwargs: other kwargs that I could add in the future
"""

self.name = name
self.locked = locked
self.tracks = [
track1 if track1 else default_track.copy(),
track2 if track2 else default_track.copy(),
Expand All @@ -51,7 +48,7 @@ def load_from_json(self, cup: dict):
for key, value in cup.items(): # load all value in the json as class attribute
if key == "tracks": # if the key is tracks
for i, track_json in enumerate(value): # load all tracks from their json
self.tracks[int(i)].load_from_json(track_json)
self.tracks[i].load_from_json(track_json)
else:
setattr(self, key, value)

Expand Down
45 changes: 27 additions & 18 deletions source/Game.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ def replace_file(path, file, subpath="/") -> None:
szs_extract_path = path + ".d"
if os.path.exists(szs_extract_path + subpath):
if subpath[-1] == "/":
shutil.copyfile(f"./file/{file}", szs_extract_path + subpath + file)
shutil.copyfile(f"{self.ctconfig.pack_path}/file/{file}", szs_extract_path + subpath + file)
else:
shutil.copyfile(f"./file/{file}", szs_extract_path + subpath)
shutil.copyfile(f"{self.ctconfig.pack_path}/file/{file}", szs_extract_path + subpath)

elif path[-1] == "/":
shutil.copyfile(f"./file/{file}", path + file)
shutil.copyfile(f"{self.ctconfig.pack_path}/file/{file}", path + file)
else:
shutil.copyfile(f"./file/{file}", path)
shutil.copyfile(f"{self.ctconfig.pack_path}/file/{file}", path)

for fp in fs:
for f in glob.glob(self.path + "/files/" + fp, recursive=True):
Expand Down Expand Up @@ -282,10 +282,11 @@ def install_patch_lecode(self) -> None:
"""
self.gui.progress(statut=self.gui.translate("Patch lecode.bin"), add=1)

lpar_path = "./file/lpar-debug.txt" if self.gui.boolvar_use_debug_mode.get() else "./file/lpar-default.txt"
lpar_path = f"{self.ctconfig.pack_path}/file/lpar-debug.txt" \
if self.gui.boolvar_use_debug_mode.get() else f"{self.ctconfig.pack_path}/file/lpar-default.txt"

lec.patch(
lecode_file=f"./file/lecode-{self.region}.bin",
lecode_file=f"{self.ctconfig.pack_path}/file/lecode-{self.region}.bin",
dest_lecode_file=f"{self.path}/files/rel/lecode-{self.region}.bin",
game_track_path=f"{self.path}/files/Race/Course/",
copy_track_paths=[f"./file/Track/"],
Expand Down Expand Up @@ -422,13 +423,12 @@ def process_bmg_replacement(bmg_content: str, bmg_language: str) -> str:
:param bmg_language: language of the bmg file
:return: the replaced bmg file
"""
with open("./file_process.json", encoding="utf8") as fp_file:
with open(f"{self.ctconfig.pack_path}/file_process.json", encoding="utf8") as fp_file:
file_process = json.load(fp_file)

for bmg_process in file_process["bmg"]:
if "language" in bmg_process:
if bmg_language not in bmg_process["language"]:
continue
if "language" in bmg_process and bmg_language not in bmg_process["language"]:
continue

for data, data_replacement in bmg_process["data"].items():
for key, replacement in bmg_replacement.items():
Expand Down Expand Up @@ -462,17 +462,18 @@ def save_bmg(file: str, bmg_content: str) -> None:
bmg.encode(file)
os.remove(file)

save_bmg(f"./file/Menu_{bmglang}.txt", process_bmg_replacement(bmgmenu, bmglang))
save_bmg(f"./file/Common_{bmglang}.txt", process_bmg_replacement(bmgcommon, bmglang))
save_bmg(f"./file/Common_R{bmglang}.txt", process_bmg_replacement(rbmgcommon, bmglang))
save_bmg(f"{self.ctconfig.pack_path}/file/Menu_{bmglang}.txt", process_bmg_replacement(bmgmenu, bmglang))
save_bmg(f"{self.ctconfig.pack_path}/file/Common_{bmglang}.txt", process_bmg_replacement(bmgcommon, bmglang))
save_bmg(f"{self.ctconfig.pack_path}/file/Common_R{bmglang}.txt", process_bmg_replacement(rbmgcommon, bmglang))

def patch_file(self):
"""
Prepare all files to install the mod (track, bmg text, descriptive image, ...)
"""
try:
if not (os.path.exists("./file/Track-WU8/")): os.makedirs("./file/Track-WU8/")
with open("./file_process.json", encoding="utf8") as fp_file:
os.makedirs(f"{self.ctconfig.pack_path}/file/Track-WU8/", exist_ok=True)

with open(f"{self.ctconfig.pack_path}/file_process.json", encoding="utf8") as fp_file:
file_process = json.load(fp_file)
max_step = len(file_process["img"]) + len(self.ctconfig.all_tracks) + 3 + len("EGFIS")

Expand All @@ -486,10 +487,13 @@ def patch_file(self):

self.gui.progress(statut=self.gui.translate("Creating ct_icon.png"), add=1)
ct_icon = self.ctconfig.get_cticon()
ct_icon.save("./file/ct_icons.tpl.png")
ct_icon.save(f"{self.ctconfig.pack_path}/file/ct_icons.tpl.png")

self.gui.progress(statut=self.gui.translate("Creating descriptive images"), add=1)
self.patch_img_desc()
self.patch_img_desc(
img_desc_path=self.ctconfig.pack_path+"/file/img_desc/",
dest_dir=self.ctconfig.pack_path+"/file/"
)
self.patch_image(file_process["img"])
for file in glob.glob(self.path + "/files/Scene/UI/MenuSingle_?.szs"): self.patch_bmg(file)
# MenuSingle could be any other file, Common and Menu are all the same in all other files.
Expand All @@ -511,7 +515,12 @@ def patch_image(self, fp_img: dict) -> None:
for i, file in enumerate(fp_img):
self.gui.progress(statut=self.gui.translate("Converting images") + f"\n({i + 1}/{len(fp_img)}) {file}",
add=1)
img.encode(file="./file/" + file, format=fp_img[file])
# TODO: IMG DESC AND THIS PART REALLY NEED A REWRITE !

img.encode(
file=f"{self.ctconfig.pack_path}/file/{file}",
format=fp_img[file]
)

def patch_img_desc(self, img_desc_path: str = "./file/img_desc/", dest_dir: str = "./file/") -> None:
"""
Expand Down
Loading

0 comments on commit 6042ca4

Please sign in to comment.