From 7b6b21ebaa1670674ca5ed4218104d56f7ab7f24 Mon Sep 17 00:00:00 2001 From: GoJean Date: Sat, 31 Oct 2026 13:37:49 +0800 Subject: [PATCH 1/5] fix #397 See issue #397 --- UnityPy/environment.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/UnityPy/environment.py b/UnityPy/environment.py index fa06ab91..38500dd4 100644 --- a/UnityPy/environment.py +++ b/UnityPy/environment.py @@ -86,15 +86,22 @@ def load(self, files: List[str]): {ntpath.basename(f): self.load_file(self.fs.open(f, "rb"), self, f) for f in files if self.fs.exists(f)} ) - def _load_split_file(self, basename: str) -> bytes: + def _load_split_file(self, basename: str, open_f: Callable[[str], BinaryIO] = None) -> bytes: file: List[bytes] = [] for i in range(0, 999): item = f"{basename}.split{i}" - if self.fs.exists(item): - with self.fs.open(item, "rb") as f: - file.append(f.read()) # type: ignore - elif i: - break + if open_f: + try: + file.append(open_f(item).read()) + except: + break + else: + if self.fs.exists(item): + with self.fs.open(item, "rb") as f: + file.append(f.read()) # type: ignore + else: + break + return b"".join(file) def load_file( @@ -311,7 +318,7 @@ def load_assets(self, assets: List[str], open_f: Callable[[str], BinaryIO]): continue split_files.append(basepath) - data = self._load_split_file(basepath) + data = self._load_split_file(basepath, open_f) path = basepath else: data = open_f(path) From dedbf052959d491be6dad338a76d109008602fda Mon Sep 17 00:00:00 2001 From: GoJean Date: Mon, 2 Nov 2026 16:46:27 +0800 Subject: [PATCH 2/5] fix: restore compatibility for loading splits from 1 As pointed out in the review by @isHarryh, the previous commit broke the old behavior where split files could start from either 0 or 1. This commit fixes the regression by only breaking the loop when `i > 0` (using `if i:`). If `split0` is not found, the loop will now correctly continue and try to load `split1`, preserving the compatibility introduced in PR #94. --- UnityPy/environment.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/UnityPy/environment.py b/UnityPy/environment.py index 38500dd4..efd904c6 100644 --- a/UnityPy/environment.py +++ b/UnityPy/environment.py @@ -94,13 +94,15 @@ def _load_split_file(self, basename: str, open_f: Callable[[str], BinaryIO] = No try: file.append(open_f(item).read()) except: - break + if i: + break else: if self.fs.exists(item): with self.fs.open(item, "rb") as f: file.append(f.read()) # type: ignore else: - break + if i: + break return b"".join(file) From df600ad6743ffc0ed400729b507b1878728922a2 Mon Sep 17 00:00:00 2001 From: GoJean Date: Sun, 8 Nov 2026 18:27:43 +0800 Subject: [PATCH 3/5] Refactor file loading methods in environment.py Refactor load and _load_split_file methods for better clarity and error handling. --- UnityPy/environment.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/UnityPy/environment.py b/UnityPy/environment.py index efd904c6..56cea286 100644 --- a/UnityPy/environment.py +++ b/UnityPy/environment.py @@ -84,26 +84,28 @@ def load(self, files: List[str]): """Loads all files into the Environment.""" self.files.update( {ntpath.basename(f): self.load_file(self.fs.open(f, "rb"), self, f) for f in files if self.fs.exists(f)} - ) + ) - def _load_split_file(self, basename: str, open_f: Callable[[str], BinaryIO] = None) -> bytes: + def _load_split_file( + self, + basename: str, + open_f: Optional[Callable[[str], BinaryIO]] = None, + ) -> bytes: file: List[bytes] = [] - for i in range(0, 999): + for i in range(0, 1000): item = f"{basename}.split{i}" - if open_f: + if open_f is not None: try: - file.append(open_f(item).read()) - except: - if i: - break - else: - if self.fs.exists(item): - with self.fs.open(item, "rb") as f: - file.append(f.read()) # type: ignore - else: - if i: - break - + f = open_f(item) + except (FileNotFoundError, KeyError): + if i: break + continue + with f: + file.append(f.read()) + elif self.fs.exists(item): + with self.fs.open(item, "rb") as f: + file.append(f.read()) # type: ignore + elif i: break return b"".join(file) def load_file( From f5dd292bb88e676ff8b688500abb54fbfb559e5f Mon Sep 17 00:00:00 2001 From: GoJean Date: Sun, 8 Nov 2026 18:36:33 +0800 Subject: [PATCH 4/5] Fix indentation and formatting in environment.py --- UnityPy/environment.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/UnityPy/environment.py b/UnityPy/environment.py index 56cea286..e62ce4c2 100644 --- a/UnityPy/environment.py +++ b/UnityPy/environment.py @@ -98,14 +98,16 @@ def _load_split_file( try: f = open_f(item) except (FileNotFoundError, KeyError): - if i: break + if i: + break continue with f: file.append(f.read()) elif self.fs.exists(item): with self.fs.open(item, "rb") as f: file.append(f.read()) # type: ignore - elif i: break + elif i: + break return b"".join(file) def load_file( From 8814dbf664595f727fe006da6193403103d29d48 Mon Sep 17 00:00:00 2001 From: GoJean Date: Sun, 8 Nov 2026 18:43:44 +0800 Subject: [PATCH 5/5] Fix indentation in load method --- UnityPy/environment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnityPy/environment.py b/UnityPy/environment.py index e62ce4c2..8d9cf90c 100644 --- a/UnityPy/environment.py +++ b/UnityPy/environment.py @@ -84,7 +84,7 @@ def load(self, files: List[str]): """Loads all files into the Environment.""" self.files.update( {ntpath.basename(f): self.load_file(self.fs.open(f, "rb"), self, f) for f in files if self.fs.exists(f)} - ) + ) def _load_split_file( self,