Folder structure

Trying to make it more package-y
This commit is contained in:
Mc-muffin
2023-05-19 08:07:41 -05:00
parent ded5772a25
commit 6e40568d9d
18 changed files with 92 additions and 42 deletions

0
pythonlib/__init__.py Normal file
View File

View File

View File

@@ -1,6 +1,6 @@
from dataclasses import dataclass
import FileIO
import comptolib
from ..formats.FileIO import FileIO
from ..utils import comptolib
MAGIC = b"SCPK"

View File

@@ -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))

View File

@@ -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("<b", data_compressed[:1])[0]
#print("File {} size: {} ctype: {}".format(i, fsize,c_type))
data_uncompressed = comptolib.decompress_data(data_compressed)
@@ -645,7 +646,6 @@ class ToolsTOR(ToolsTales):
pak2_data = f_pak2.read()
#Create the pak2 object
pak2_obj = pak2lib.pak2_file()
pak2_obj = pak2lib.get_data(pak2_data)
#Generate the new Theirsce based on the XML and replace the original one
@@ -658,7 +658,7 @@ class ToolsTOR(ToolsTales):
with open(self.skit_XML_patch+ "New/" + pak2_file, "wb") as f2:
f2.write(pak2lib.create_pak2(pak2_obj))
return pak2lib.create_pak2(pak2_obj)
return
def pack_All_Skits(self):
@@ -769,7 +769,7 @@ class ToolsTOR(ToolsTales):
dummies = 0
for file in sorted(file_list, key=self.get_file_name):
for file in tqdm(sorted(file_list, key=self.get_file_name)):
size = 0
remainder = 0
@@ -816,12 +816,6 @@ class ToolsTOR(ToolsTales):
sectors.append(buffer)
previous += 1
#print(
# "Writing file %05d/%05d..." % (current - dummies, len(file_list)), end="\r"
#)
print("Writing file %05d/%05d..." % (current - dummies, len(file_list)))
#Use the new SLPS updated and update the pointers for the SCPK
with open("../Data/{}/Disc/New/SLPS_254.50".format(self.repo_name), "r+b") as output_elf:
output_elf.seek(self.POINTERS_BEGIN)

View File

@@ -17,7 +17,7 @@ import pygsheets
from googleapiclient.errors import HttpError
from tqdm import tqdm
import fps4 as fps4
import pythonlib.formats.fps4 as fps4
class ToolsTales:
@@ -277,7 +277,7 @@ class ToolsTales:
return pointers_offset, texts_offset
def get_extension(self, data):
def get_extension(self, data) -> 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")

View File

View File

View File

@@ -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("<b", version) + struct.pack("<L", output_size.value) + struct.pack("<L", input_size) + output[:output_size.value]
output = (
struct.pack("<b", version)
+ struct.pack("<L", output_size.value)
+ struct.pack("<L", input_size)
+ output[: output_size.value]
)
return output
def decompress_data(input: bytes, raw: bool=False, version: int=3)->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("<b", input[:1])
version = struct.unpack("<b", input[:1])[0]
input_size, output_size = struct.unpack("<2L", input[1:9])
output = b"\x00" * output_size
input = input[9:]
error = compto_decode(version, input, input_size, output, ctypes.byref(ctypes.c_uint(output_size)))
error = compto_decode(
version, input, input_size, output, ctypes.byref(ctypes.c_uint(output_size))
)
RaiseError(error)
return output
def compress_file(input: str, output: str, raw: bool=False, version: int=3):
def compress_file(input: str, output: str, raw: bool = False, version: int = 3) -> 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)

7
setup.py Normal file
View File

@@ -0,0 +1,7 @@
from setuptools import setup, find_packages
setup(
name='pythonlib',
version='0.1.0',
packages=find_packages(),
)