organization

This commit is contained in:
Mc-muffin
2024-11-25 22:23:54 -05:00
parent a90cb95897
commit 880795d239
9 changed files with 75 additions and 11 deletions

View File

@@ -1,4 +1,6 @@
import os, struct, sys
import os
import struct
import sys
def dump_fps4(name, name2, base_path):

View File

@@ -186,6 +186,9 @@ class Pak:
def __getitem__(self, item):
return self.files[item]
def __setitem__(self, item, data):
self.files[item] = data
def __len__(self):
return len(self.files)

View File

View File

@@ -2,9 +2,9 @@ import struct
from dataclasses import dataclass
from pathlib import Path
from ..formats.FileIO import FileIO
from ..formats.pak import Pak
from ..utils import comptolib
from ..FileIO import FileIO
from ..pak import Pak
from ...utils import comptolib
MAGIC = b"SCPK"

View File

@@ -1,10 +1,29 @@
from dataclasses import dataclass
from io import BytesIO
from pathlib import Path
from typing import Generator, Union
from .FileIO import FileIO
from .theirsce_funcs import *
from .theirsce_instructions import *
from ..FileIO import FileIO
from .theirsce_funcs import SYSCALL_NAMES
from .theirsce_instructions import (
AluOperation,
BranchType,
InstructionType,
ReferenceScope,
TheirsceAcquireInstruction,
TheirsceAluInstruction,
TheirsceBaseInstruction,
TheirsceBranchInstruction,
TheirsceBreakInstruction,
TheirsceLocalCallInstruction,
TheirscePushInstruction,
TheirsceReferenceInstruction,
TheirsceReturnInstruction,
TheirsceSpecialReferenceInstruction,
TheirsceStringInstruction,
TheirsceSyscallInstruction,
VariableType,
)
# I followed other project when making this class
# not sure how if it's the best approach
@@ -17,6 +36,12 @@ class subsection:
unk2: int
off: int
@dataclass
class function:
arguments: list
body: list
visited: bool = False
class Theirsce(FileIO):
def __init__(self, path: Union[Path, str, BytesIO, bytes] ="") -> None:
@@ -48,6 +73,8 @@ class Theirsce(FileIO):
subsections.append(sub)
self.sections.append(subsections)
self.seek(pos)
self.functions = self.get_functions()
def __enter__(self):
return super().__enter__()
@@ -122,6 +149,36 @@ class Theirsce(FileIO):
return data
def get_functions(self):
funs: dict[int, function] = {}
ops = list(self.walk_code())
bd = []
fn_st = ops[0].position; ops.pop(0)
for val in ops:
if val.type == InstructionType.BREAK:
if bd[0].type == InstructionType.ACQUIRE:
funs[fn_st] = function(bd[0].params, bd)
bd.pop(0)
else:
funs[fn_st] = function([], bd)
fn_st = val.position
bd = []
else:
bd.append(val)
if bd[0].type == InstructionType.ACQUIRE:
funs[fn_st] = function(bd[0].params, bd)
bd.pop(0)
else:
funs[fn_st] = function([], bd)
return funs
def reset_seen_functions(self):
for fun in self.functions.values():
fun.visited = False
def read_opcode(self) -> TheirsceBaseInstruction:
pos = self.tell()
opcode = self.read_uint8()

View File

@@ -83,6 +83,7 @@ class AluOperation(Enum):
@dataclass
class TheirsceBaseInstruction:
position: int
mnemonic: str = field(default=False, init=False) # type: ignore
type: InstructionType = field(default=False, init=False) # type: ignore
#size: int
@@ -182,6 +183,7 @@ class TheirsceReferenceInstruction(TheirsceBaseInstruction):
offset: int
shift: int
position: int
is_array: bool = False
mnemonic = "REF"
type = InstructionType.REFERENCE

View File

@@ -16,13 +16,13 @@ import pyjson5 as json
from tqdm import tqdm
import io
import pythonlib.formats.pak2 as pak2lib
import pythonlib.formats.rebirth.pak2 as pak2lib
import pythonlib.utils.comptolib as comptolib
from pythonlib.formats.FileIO import FileIO
from pythonlib.formats.pak import Pak
from pythonlib.formats.scpk import Scpk
from pythonlib.formats.theirsce import Theirsce
from pythonlib.formats.theirsce_instructions import (AluOperation,
from pythonlib.formats.rebirth.scpk import Scpk
from pythonlib.formats.rebirth.theirsce import Theirsce
from pythonlib.formats.rebirth.theirsce_instructions import (AluOperation,
InstructionType,
TheirsceBaseInstruction)