forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityVersion.py
More file actions
127 lines (99 loc) · 4.03 KB
/
Copy pathUnityVersion.py
File metadata and controls
127 lines (99 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from __future__ import annotations
import re
from enum import IntEnum
from typing import Optional, Tuple, Union
VersionPattern = re.compile(r"^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<build>\d+)(?P<type_str>.+?)?(?P<type_number>\d+)?$")
class UnityVersionType(IntEnum):
a = 0 # Alpha
b = 1 # Beta
c = 2 # China
f = 3 # Final
p = 4 # Patch
x = 5 # Experimental
u = 255 # Unknown
class UnityVersion(int):
# https://github.com/AssetRipper/VersionUtilities/blob/master/VersionUtilities/UnityVersion.cs
_type_str: Optional[str]
@property
def major(self):
return (self >> 48) & 0xFFFF
@property
def minor(self):
return (self >> 32) & 0xFFFF
@property
def build(self):
return (self >> 16) & 0xFFFF
@property
def type(self):
return UnityVersionType((self >> 8) & 0xFF)
@property
def type_str(self):
return getattr(self, "_type_str", self.type.name)
@property
def type_number(self):
return self & 0xFF
@classmethod
def from_list(
cls, major: int = 0, minor: int = 0, build: int = 0, type: int = UnityVersionType.f, type_number: int = 0
):
return cls((major << 48) | (minor << 32) | (build << 16) | (type << 8) | type_number)
@classmethod
def from_str(cls, version: str):
# formats:
# old: 5.0.0, <major>.<minor>.<build>
# new: 2018.1.1f2 <major>.<minor>.<build><type_str><type_number>
match = VersionPattern.match(version)
if not match:
raise ValueError(f"Invalid version string: {version}")
major = int(match.group("major"))
minor = int(match.group("minor"))
build = int(match.group("build"))
type_str = match.group("type_str")
type_number = int(match.group("type_number") or 0)
if type_str is None:
return cls.from_list(major, minor, build)
type = getattr(UnityVersionType, type_str.lower(), UnityVersionType.u)
obj = cls.from_list(major, minor, build, type, type_number)
if type is UnityVersionType.u:
obj._type_str = type_str
return obj
def __repr__(self) -> str:
return f"UnityVersion {self.major}.{self.minor}{self.type_str}{self.type_number}"
def __getitem__(self, idx: Union[int, slice]) -> Union[int, Tuple[int, ...]]:
values = (
self.major,
self.minor,
self.build,
self.type.value,
self.type_number,
)
return values[idx]
def as_tuple(self) -> Tuple[int, int, int, int, int]:
return (self.major, self.minor, self.build, self.type.value, self.type_number)
def __eq__(self, other: Union[int, UnityVersion, Tuple[int, ...]]) -> bool:
if isinstance(other, int):
return super().__eq__(other)
elif isinstance(other, tuple):
return self.as_tuple() == other
raise NotImplementedError("Unsupported comparison")
def __ne__(self, other: Union[int, UnityVersion, Tuple[int, ...]]) -> bool:
return not self.__eq__(other)
def __lt__(self, other: Union[int, UnityVersion, Tuple[int, ...]]) -> bool:
if isinstance(other, int):
return super().__lt__(other)
elif isinstance(other, tuple):
return self.as_tuple() < other
raise NotImplementedError("Unsupported comparison")
def __le__(self, other: Union[int, UnityVersion, Tuple[int, ...]]) -> bool:
return self.__lt__(other) or self.__eq__(other)
def __gt__(self, other: Union[int, UnityVersion, Tuple[int, ...]]) -> bool:
if isinstance(other, int):
return super().__gt__(other)
elif isinstance(other, tuple):
return self.as_tuple() > other
raise NotImplementedError("Unsupported comparison")
def __ge__(self, other: Union[int, UnityVersion, Tuple[int, ...]]) -> bool:
return self.__gt__(other) or self.__eq__(other)
def __hash__(self) -> int:
return super().__hash__()
__all__ = ["UnityVersion", "UnityVersionType"]