Skip to content

Commit 635528a

Browse files
committed
fix K0lb3#158
1 parent 36285cb commit 635528a

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

UnityPy/config.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,17 @@
44
# disabling this will reduce the load time by a lot (half of the time is spend on parsing the typetrees)
55
# but it will also prevent saving an edited file
66
SERIALIZED_FILE_PARSE_TYPETREE = True
7-
# enable for chinese version with AssetBundle encryption
8-
USE_CN_ENCRYPTION = False
7+
8+
# GLOBAL WARNING SUPPRESSION
9+
FALLBACK_VERSION_WARNED = False # for FALLBACK_UNITY_VERSION
10+
11+
12+
# GET FUNCTIONS
13+
def get_fallback_version():
14+
global FALLBACK_VERSION_WARNED
15+
if not FALLBACK_VERSION_WARNED:
16+
print(
17+
f"Warning: 0.0.0 version found, defaulting to UnityPy.config.FALLBACK_UNITY_VERSION ({FALLBACK_UNITY_VERSION})" # noqa: E501
18+
)
19+
FALLBACK_VERSION_WARNED = True
20+
return FALLBACK_UNITY_VERSION

UnityPy/files/BundleFile.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from .. import config
1212

13-
1413
BlockInfo = namedtuple("BlockInfo", "uncompressedSize compressedSize flags")
1514
DirectoryInfoFS = namedtuple("DirectoryInfoFS", "offset size flags path")
1615
reVersion = re.compile(r"(\d+)\.(\d+)\.(\d+)\w.+")
@@ -92,7 +91,7 @@ def read_fs(self, reader: EndianBinaryReader):
9291
uncompressedSize = reader.read_u_int()
9392
self.dataflags = reader.read_u_int()
9493

95-
version = tuple(map(int, reVersion.match(self.version_engine).groups()))
94+
version = self.get_version_tuple()
9695
# https://issuetracker.unity3d.com/issues/files-within-assetbundles-do-not-start-on-aligned-boundaries-breaking-patching-on-nintendo-switch
9796
# Unity CN introduced encryption before the alignment fix was introduced.
9897
# Unity CN used the same flag for the encryption as later on the alignment fix,
@@ -414,3 +413,10 @@ def decompress_data(
414413
raise NotImplementedError("LZHAM decompression not implemented")
415414
else:
416415
return compressed_data
416+
417+
def get_version_tuple(self) -> Tuple[int, int, int]:
418+
"""Returns the version as a tuple."""
419+
version = self.version_engine
420+
if not version or version == "0.0.0":
421+
version = config.get_fallback_version()
422+
return tuple(map(int, reVersion.match(version).groups()))

UnityPy/files/SerializedFile.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
from .. import config
1212

13-
# only print the version warning once
14-
VERSION_WARNED = False
15-
1613

1714
class SerializedFileHeader:
1815
metadata_size: int
@@ -309,18 +306,12 @@ def container(self):
309306

310307
def set_version(self, string_version):
311308
self.unity_version = string_version
312-
if string_version == "0.0.0":
309+
if not string_version or string_version == "0.0.0":
313310
# weird case, but apparently can happen?
314311
# check "cant read Texture2D by 2020.3.13 f1 AssetBundle #77" for details
315312
string_version = self.parent.version_engine
316-
if string_version == "0.0.0":
317-
global VERSION_WARNED
318-
if not VERSION_WARNED:
319-
print(
320-
f"Warning: 0.0.0 version found, defaulting to UnityPy.config.FALLBACK_UNITY_VERSION\n{config.FALLBACK_UNITY_VERSION}"
321-
)
322-
VERSION_WARNED = True
323-
string_version = config.FALLBACK_UNITY_VERSION
313+
if not string_version or string_version == "0.0.0":
314+
string_version = config.get_fallback_version()
324315
build_type = re.findall(r"([^\d.])", string_version)
325316
self.build_type = BuildType(build_type[0] if build_type else "")
326317
version_split = re.split(r"\D", string_version)
@@ -578,7 +569,7 @@ def save_type_tree(self, nodes: list, writer: EndianBinaryWriter):
578569

579570
# calc children count
580571
children_count = 0
581-
for node2 in nodes[i + 1:]:
572+
for node2 in nodes[i + 1 :]:
582573
if node2.m_Level == node.m_Level:
583574
break
584575
if node2.m_Level == node.m_Level - 1:

0 commit comments

Comments
 (0)