WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions UnityPy/helpers/PackedBitVector.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from typing import TYPE_CHECKING, List, Optional, Tuple
from typing import TYPE_CHECKING, Any, List, Optional, Tuple

if TYPE_CHECKING:
from ..classes.generated import PackedBitVector


def reshape(data: list, shape: Optional[Tuple[int, ...]] = None) -> list:
def reshape(data: list, shape: Optional[Tuple[int, ...]] = None) -> List[Any]:
if shape is None:
return data
if len(shape) == 1:
m = shape[0]
return [data[i : i + m] for i in range(0, len(data), m)]
elif len(shape) == 2:
m, n = shape
return [[[data[i + j : i + j + n] for j in range(0, m * n, n)]] for i in range(0, len(data), m * n)]
return [[data[i + j : i + j + n] for j in range(0, m * n, n)] for i in range(0, len(data), m * n)]
else:
raise ValueError("Invalid shape")

Expand All @@ -22,7 +22,7 @@ def unpack_ints(
start: int = 0,
count: Optional[int] = None,
shape: Optional[Tuple[int, ...]] = None,
) -> List[int]:
) -> List[Any]:
assert packed.m_BitSize is not None

m_BitSize = packed.m_BitSize
Expand Down Expand Up @@ -70,13 +70,18 @@ def unpack_floats(
start: int = 0,
count: Optional[int] = None,
shape: Optional[Tuple[int, ...]] = None,
) -> List[float]:
) -> List[Any]:
assert packed.m_BitSize is not None and packed.m_Range is not None and packed.m_Start is not None

# read as int and cast up to double to prevent loss of precision
quantized_f64 = unpack_ints(packed, start, count)
scale = packed.m_Range / ((1 << packed.m_BitSize) - 1)
quantized = [x * scale + packed.m_Start for x in quantized_f64]
# avoid zero division of scale
if packed.m_BitSize == 0:
quantized = [packed.m_Start] * (packed.m_NumItems if count is None else count)
else:
# read as int and cast up to double to prevent loss of precision
quantized_f64 = unpack_ints(packed, start, count)
scale = packed.m_Range / ((1 << packed.m_BitSize) - 1)
quantized = [x * scale + packed.m_Start for x in quantized_f64]

return reshape(quantized, shape)


Expand Down
Loading