diff --git a/pythonlib/__init__.py b/pythonlib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/FileIO.py b/pythonlib/formats/FileIO.py similarity index 100% rename from FileIO.py rename to pythonlib/formats/FileIO.py diff --git a/pythonlib/formats/__init__.py b/pythonlib/formats/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fps4.py b/pythonlib/formats/fps4.py similarity index 100% rename from fps4.py rename to pythonlib/formats/fps4.py diff --git a/pak2.py b/pythonlib/formats/pak2.py similarity index 100% rename from pak2.py rename to pythonlib/formats/pak2.py diff --git a/scpk.py b/pythonlib/formats/scpk.py similarity index 94% rename from scpk.py rename to pythonlib/formats/scpk.py index 9dd72a2..f709e41 100644 --- a/scpk.py +++ b/pythonlib/formats/scpk.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -import FileIO -import comptolib +from ..formats.FileIO import FileIO +from ..utils import comptolib MAGIC = b"SCPK" diff --git a/theirsce.py b/pythonlib/formats/theirsce.py similarity index 100% rename from theirsce.py rename to pythonlib/formats/theirsce.py diff --git a/theirsce_funcs.py b/pythonlib/formats/theirsce_funcs.py similarity index 100% rename from theirsce_funcs.py rename to pythonlib/formats/theirsce_funcs.py diff --git a/theirsce_instructions.py b/pythonlib/formats/theirsce_instructions.py similarity index 100% rename from theirsce_instructions.py rename to pythonlib/formats/theirsce_instructions.py diff --git a/ToolsNDX.py b/pythonlib/games/ToolsNDX.py similarity index 99% rename from ToolsNDX.py rename to pythonlib/games/ToolsNDX.py index d3d064e..5c7ba61 100644 --- a/ToolsNDX.py +++ b/pythonlib/games/ToolsNDX.py @@ -937,7 +937,8 @@ class ToolsNDX(ToolsTales): if 'EBOOT' not in ele['File_Extract']: name = os.path.basename(ele['File_Extract']).split(".")[0] - self.make_Cab(name+".dat", (name+".cab").upper(), os.path.join(os.path.dirname(ele['File_Extract']), "..")) + self.make_Cab(name +".dat", (name+".cab").upper(), os.path.join(os.path.dirname(ele['File_Extract']), + "../..")) self.pack_Menu_Archives() @@ -955,7 +956,7 @@ class ToolsNDX(ToolsTales): # Extract the file all.dat to the different directorties - def extract_Main_Archive(self): + def extract_main_archive(self): #Clean files and folders shutil.rmtree("../Data/{}/Menu/New".format(self.repo_name)) diff --git a/ToolsTODDC.py b/pythonlib/games/ToolsTODDC.py similarity index 100% rename from ToolsTODDC.py rename to pythonlib/games/ToolsTODDC.py diff --git a/ToolsTOR.py b/pythonlib/games/ToolsTOR.py similarity index 98% rename from ToolsTOR.py rename to pythonlib/games/ToolsTOR.py index 670c396..fab7124 100644 --- a/ToolsTOR.py +++ b/pythonlib/games/ToolsTOR.py @@ -11,12 +11,12 @@ from pathlib import Path import lxml.etree as etree import pandas as pd from tqdm import tqdm -from scpk import Scpk +from pythonlib.formats.scpk import Scpk -import comptolib as comptolib -import pak2 as pak2lib -from theirsce import Theirsce -from theirsce_instructions import (AluOperation, InstructionType, +import pythonlib.utils.comptolib as comptolib +import pythonlib.formats.pak2 as pak2lib +from pythonlib.formats.theirsce import Theirsce +from pythonlib.formats.theirsce_instructions import (AluOperation, InstructionType, TheirsceBaseInstruction) from .ToolsTales import ToolsTales @@ -44,6 +44,7 @@ class ToolsTOR(ToolsTales): #Path to used + # fmt: off dat_bin_original = '../Data/Tales-Of-Rebirth/Disc/Original/DAT.BIN' dat_bin_new = '../Data/Tales-Of-Rebirth/Disc/New/DAT.BIN' elf_original = '../Data/Tales-Of-Rebirth/Disc/Original/SLPS_254.50' @@ -53,6 +54,7 @@ class ToolsTOR(ToolsTales): skit_XML_patch = '../Data/Tales-Of-Rebirth/Skits/' #Skits XML files will be extracted here skit_XML_new = '../Tales-Of-Rebirth/Data/TOR/Skits/' dat_archive_extract = '../Data/Tales-Of-Rebirth/DAT/' + # fmt: on def __init__(self, tbl): @@ -177,7 +179,6 @@ class ToolsTOR(ToolsTales): print("File {} skipped because file is not found".format(file)) # Extract the story files - def extract_All_Story(self,replace=False): def extract_all_story(self, replace=False) -> None: print("Extracting Story files...") @@ -586,7 +587,7 @@ class ToolsTOR(ToolsTales): for pointer_offset, fsize in file_size_dict.items(): data_compressed = scpk.read(fsize) - if self.is_compressed(data_compressed): + if comptolib.is_compressed(data_compressed): c_type = struct.unpack(" str: if data[:4] == b"SCPK": return "scpk" @@ -338,7 +338,7 @@ class ToolsTales: # Didn't match anything return "bin" - def get_pak_type(self,data): + def get_pak_type(self,data) -> Union[str, None]: is_aligned = False data_size = len(data) @@ -683,7 +683,7 @@ class ToolsTales: def extract_Google_Sheets(self, googlesheet_id, sheet_name): - creds_path = "..\gsheet.json" + creds_path = r"..\gsheet.json" if os.path.exists(creds_path): @@ -800,7 +800,7 @@ class ToolsTales: menu_file.seek( int(sections[section_id]['Text_Start']) ) pos = menu_file.tell() else: - break; + break #Add the PointerOffset and TextOffset @@ -1062,7 +1062,7 @@ class ToolsTales: def extractAllSkits(self): print("Extracting Skits") - def extract_Main_Archive(self): + def extract_main_archive(self): print("Main Archive") diff --git a/pythonlib/games/__init__.py b/pythonlib/games/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pythonlib/utils/__init__.py b/pythonlib/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/comptolib.dll b/pythonlib/utils/comptolib.dll similarity index 100% rename from comptolib.dll rename to pythonlib/utils/comptolib.dll diff --git a/comptolib.py b/pythonlib/utils/comptolib.py similarity index 67% rename from comptolib.py rename to pythonlib/utils/comptolib.py index 9a8c478..16b1f29 100644 --- a/comptolib.py +++ b/pythonlib/utils/comptolib.py @@ -1,4 +1,6 @@ -import ctypes, os, struct +import ctypes +import struct +from pathlib import Path # Error codes SUCCESS = 0 @@ -9,27 +11,35 @@ ERROR_BAD_INPUT = -4 ERROR_UNKNOWN_VERSION = -5 ERROR_FILES_MISMATCH = -6 + class ComptoFileInputError(Exception): pass + class ComptoFileOutputError(Exception): pass + class ComptoMemoryAllocationError(Exception): pass + class ComptoBadInputError(Exception): pass + class ComptoUnknownVersionError(Exception): pass + class ComptoMismatchedFilesError(Exception): pass + class ComptoUnknownError(Exception): pass + def RaiseError(error: int): if error == SUCCESS: return @@ -48,15 +58,28 @@ def RaiseError(error: int): else: raise ComptoUnknownError("Unknown error") -comptolib_path = os.path.dirname(os.path.abspath(__file__)) + "/comptolib.dll" -comptolib = ctypes.cdll.LoadLibrary(comptolib_path) +comptolib_path = Path(__file__).parent / "comptolib.dll" + +comptolib = ctypes.cdll.LoadLibrary(str(comptolib_path)) compto_decode = comptolib.Decode -compto_decode.argtypes = ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint) +compto_decode.argtypes = ( + ctypes.c_int, + ctypes.c_void_p, + ctypes.c_int, + ctypes.c_void_p, + ctypes.POINTER(ctypes.c_uint), +) compto_decode.restype = ctypes.c_int compto_encode = comptolib.Encode -compto_encode.argtypes = ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint) +compto_encode.argtypes = ( + ctypes.c_int, + ctypes.c_void_p, + ctypes.c_int, + ctypes.c_void_p, + ctypes.POINTER(ctypes.c_uint), +) compto_encode.restype = ctypes.c_int compto_fdecode = comptolib.DecodeFile @@ -67,39 +90,64 @@ compto_fencode = comptolib.EncodeFile compto_fencode.argtypes = ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_int compto_fencode.restype = ctypes.c_int -def compress_data(input: bytes, raw: bool=False, version: int=3): + +class ComptoFile: + def __init__(self, type: int, data: bytes) -> None: + self.type = type + self.data = data + + def __getitem__(self, item): + return self.data[item] + + def __len__(self): + return len(self.data) + + +def compress_data(input: bytes, raw: bool = False, version: int = 3) -> bytes: input_size = len(input) output_size = ((input_size * 9) // 8) + 10 output = b"\x00" * output_size output_size = ctypes.c_uint(output_size) error = compto_encode(version, input, input_size, output, ctypes.byref(output_size)) RaiseError(error) - + if not raw: - output = struct.pack("bytes: + +def decompress_data(input: bytes, raw: bool = False, version: int = 3) -> bytes: if raw: input_size = len(input) output_size = input_size * 10 else: - version ,= struct.unpack(" None: error = compto_fencode(input.encode("utf-8"), output.encode("utf-8"), raw, version) RaiseError(error) -def decompress_file(input: str, output: str, raw: bool=False, version: int=3): + +def decompress_file( + input: str, output: str, raw: bool = False, version: int = 3 +) -> None: error = compto_fdecode(input.encode("utf-8"), output.encode("utf-8"), raw, version) RaiseError(error) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..dee5900 --- /dev/null +++ b/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup, find_packages + +setup( + name='pythonlib', + version='0.1.0', + packages=find_packages(), +)