Skip to content

Commit ecb9d5e

Browse files
committed
use importlib instead of manual os.path
1 parent 41d044a commit ecb9d5e

12 files changed

Lines changed: 26 additions & 24 deletions

File tree

‎UnityPy/export/AudioClipConverter.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ctypes
44
import os
55
import platform
6+
from importlib.resources import path
67
from typing import TYPE_CHECKING, Dict
78

89
from UnityPy.streams import EndianBinaryWriter
@@ -25,8 +26,6 @@ def import_pyfmodex():
2526
if pyfmodex is not None:
2627
return
2728

28-
ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
29-
3029
# determine system - Windows, Darwin, Linux, Android
3130
system = platform.system()
3231
if system == "Linux" and "ANDROID_BOOTLOGO" in os.environ:
@@ -62,17 +61,21 @@ def import_pyfmodex():
6261
)
6362

6463
# build path and load library
65-
LIB_PATH = os.path.join(ROOT, "lib", "FMOD", system, arch)
66-
6764
# prepare the environment for pyfmodex
6865
if system == "Windows":
69-
os.environ["PYFMODEX_DLL_PATH"] = os.path.join(LIB_PATH, "fmod.dll")
66+
libname = "fmod.dll"
7067
else:
7168
ext = "dylib" if system == "Darwin" else "so"
72-
os.environ["PYFMODEX_DLL_PATH"] = os.path.join(LIB_PATH, f"libfmod.{ext}")
69+
libname = f"libfmod.{ext}"
70+
# hotfix ctypes for pyfmodex for non windows systems
71+
ctypes.windll = None
72+
73+
fp = path(f"UnityPy.lib.FMOD.{system}.{arch}", libname)
74+
if hasattr(fp, "args"):
75+
# some newer version doesn't directly return a path, but instead a generator
76+
fp = fp.args[0]
7377

74-
# hotfix ctypes for pyfmodex for non windows
75-
ctypes.windll = getattr(ctypes, "windll", None)
78+
os.environ["PYFMODEX_DLL_PATH"] = str(fp.absolute())
7679

7780
import pyfmodex
7881

‎UnityPy/helpers/Tpk.py‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
from __future__ import annotations
2+
23
from enum import IntEnum, IntFlag
3-
from struct import Struct
4+
from importlib.resources import open_binary
45
from io import BytesIO
5-
from typing import List, Tuple, Any, Dict
6+
from struct import Struct
7+
from typing import Any, Dict, List, Tuple
8+
69
from .TypeTreeHelper import TypeTreeNode
710

811
TPKTYPETREE: TpkTypeTreeBlob = None
912
NODES_CACHE: dict = {}
1013

1114

1215
def init():
13-
import os
16+
with open_binary("UnityPy.resources", "uncompressed.tpk") as f:
17+
data = f.read()
1418

15-
with open(
16-
os.path.join(
17-
os.path.dirname(os.path.dirname(__file__)), "resources", "uncompressed.tpk"
18-
),
19-
"rb",
20-
) as f:
21-
global TPKTYPETREE
22-
TPKTYPETREE = TpkFile(f).GetDataBlob()
19+
global TPKTYPETREE
20+
with BytesIO(data) as stream:
21+
TPKTYPETREE = TpkFile(stream).GetDataBlob()
2322

2423

2524
def get_typetree_node(class_id: int, version: tuple):
@@ -172,12 +171,12 @@ def GetDataBlob(self) -> TpkDataBlob:
172171
elif self.CompressionType == TpkCompressionType.Lzma:
173172
import lzma
174173

175-
raise Exception("LZMA compression not implemented")
174+
decompressed = lzma.decompress(self.CompressedBytes)
176175

177176
elif self.CompressionType == TpkCompressionType.Brotli:
178177
import brotli
179178

180-
decompressed = brotli.decompress(self.CompressedBytes)
179+
decompressed: bytes = brotli.decompress(self.CompressedBytes)
181180

182181
else:
183182
raise Exception("Invalid compression type")
@@ -193,7 +192,7 @@ def GetDataBlob(self) -> TpkDataBlob:
193192

194193

195194
class TpkDataBlob:
196-
__slots__ = "DataType"
195+
__slots__ = ("DataType",)
197196
DataType: TpkDataType
198197

199198
def __init__(self, stream: BytesIO) -> None:
@@ -247,7 +246,7 @@ def __init__(self, stream: BytesIO) -> None:
247246

248247

249248
class TpkFileSystemBlob(TpkDataBlob):
250-
__slots__ = "Files"
249+
__slots__ = ("Files",)
251250
# TODO: check if dict might be better
252251
Files: List[Tuple[str, bytes]]
253252

‎UnityPy/lib/FMOD/Darwin/x64/__init__.py‎

Whitespace-only changes.

‎UnityPy/lib/FMOD/Linux/arm/__init__.py‎

Whitespace-only changes.

‎UnityPy/lib/FMOD/Linux/armhf/__init__.py‎

Whitespace-only changes.

‎UnityPy/lib/FMOD/Linux/x86/__init__.py‎

Whitespace-only changes.

‎UnityPy/lib/FMOD/Linux/x86_64/__init__.py‎

Whitespace-only changes.

‎UnityPy/lib/FMOD/Windows/x64/__init__.py‎

Whitespace-only changes.

‎UnityPy/lib/FMOD/Windows/x86/__init__.py‎

Whitespace-only changes.

‎UnityPy/lib/FMOD/__init__.py‎

Whitespace-only changes.

0 commit comments

Comments
 (0)