Skip to content

Commit 7e7542b

Browse files
isHarryhK0lb3
authored andcommitted
feat(config): unset default fallback version
1 parent a70736a commit 7e7542b

3 files changed

Lines changed: 47 additions & 16 deletions

File tree

‎UnityPy/config.py‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
1-
# used when no version is defined by the SerializedFile or its BundleFile
2-
FALLBACK_UNITY_VERSION = "2.5.0f5"
3-
# determines if the typetree structures for the Object types will be parsed
4-
# disabling this will reduce the load time by a lot (half of the time is spend on parsing the typetrees)
5-
# but it will also prevent saving an edited file
1+
import warnings
2+
3+
from .exceptions import UnityVersionFallbackError, UnityVersionFallbackWarning
4+
5+
6+
FALLBACK_UNITY_VERSION = None
7+
"""The Unity version to use when no version is defined
8+
by the SerializedFile or its BundleFile.
9+
10+
You may manually configure this value to a version string, e.g. `2.5.0f5`.
11+
"""
12+
613
SERIALIZED_FILE_PARSE_TYPETREE = True
14+
"""Determines if the typetree structures for the Object types will be parsed.
715
8-
# GLOBAL WARNING SUPPRESSION
9-
FALLBACK_VERSION_WARNED = False # for FALLBACK_UNITY_VERSION
16+
Disabling this will reduce the load time by a lot (half of the time is spend on parsing the typetrees),
17+
but it will also prevent saving an edited file.
18+
"""
19+
20+
21+
# WARNINGS CONTROL
22+
warnings.simplefilter("once", UnityVersionFallbackWarning)
1023

1124

1225
# GET FUNCTIONS
1326
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
27+
global FALLBACK_UNITY_VERSION
28+
29+
if not isinstance(FALLBACK_UNITY_VERSION, str):
30+
raise UnityVersionFallbackError(
31+
"No valid Unity version found, and the fallback version is not correctly configured. "
32+
+ "Please explicitly set the value of UnityPy.config.FALLBACK_UNITY_VERSION."
1833
)
19-
FALLBACK_VERSION_WARNED = True
34+
35+
warnings.warn(
36+
f"No valid Unity version found, defaulting to UnityPy.config.FALLBACK_UNITY_VERSION ({FALLBACK_UNITY_VERSION})", # noqa: E501
37+
category=UnityVersionFallbackWarning,
38+
stacklevel=2
39+
)
40+
2041
return FALLBACK_UNITY_VERSION

‎UnityPy/exceptions.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
class TypeTreeError(Exception):
2-
def __init__(self, message, nodes):
3-
# Call the base class constructor with the parameters it needs
2+
def __init__(self, message, nodes):
3+
super().__init__(message)
4+
self.nodes = nodes
5+
6+
7+
class UnityVersionFallbackError(Exception):
8+
def __init__(self, message):
9+
super().__init__(message)
10+
11+
12+
class UnityVersionFallbackWarning(UserWarning):
13+
def __init__(self, message):
414
super().__init__(message)
5-
self.nodes = nodes

‎UnityPy/files/BundleFile.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from . import File
77
from ..enums import ArchiveFlags, ArchiveFlagsOld, CompressionFlags
8+
from ..exceptions import UnityVersionFallbackError
89
from ..helpers import ArchiveStorageManager, CompressionHelper
910
from ..streams import EndianBinaryReader, EndianBinaryWriter
1011

@@ -531,6 +532,6 @@ def get_version_tuple(self) -> Tuple[int, int, int]:
531532
if not match:
532533
match = reVersion.match(config.get_fallback_version())
533534
if not match or len(match.groups()) < 3:
534-
raise ValueError("Illegal fallback version format")
535+
raise UnityVersionFallbackError("Illegal fallback version format")
535536
map_ = map(int, match.groups())
536537
return (next(map_), next(map_), next(map_))

0 commit comments

Comments
 (0)