-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.py
132 lines (99 loc) · 3.23 KB
/
util.py
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
128
129
130
131
132
#encoding = utf-8
import os
import sys
import struct
from typing import Tuple
import numpy as np
import math
from enum import Enum
def to_float(bs) -> float:
return struct.unpack("<f", bs)[0]
def to_float16(bs) -> float:
return float(np.frombuffer(bs, np.float16)[0])
def to_int(bs) -> int:
return (int.from_bytes(bs, byteorder='little', signed=True))
def to_uint(bs) -> int:
return (int.from_bytes(bs, byteorder='little', signed=False))
def to_ushort(bs) -> int:
return struct.unpack("<H", bs)[0]
def to_string(bs, encoding = 'utf8') -> str:
return bs.split(b'\x00')[0].decode(encoding, 'replace')
def alignRelative(openFile, relativeStart, alignment):
alignOffset = (((openFile.tell() - relativeStart) // alignment) + 1) * alignment
openFile.seek(relativeStart + alignOffset)
def str_to_bytes(var):
return bytearray(var, 'utf-8')
def uint32_to_bytes(var):
return var.to_bytes(4, byteorder='little', signed=False)
def int32_to_bytes(var):
return var.to_bytes(4, byteorder='little', signed=True)
def readFloatX3(f) -> Tuple[float, float, float]:
return struct.unpack("<fff", f.read(12))
def readFloatX4(f) -> Tuple[float, float, float, float]:
return struct.unpack("<ffff", f.read(16))
def readString(f) -> str:
byteStr = b""
while True:
byte = f.read(1)
if byte == b'\x00':
break
byteStr += byte
return byteStr.decode("utf-8", "replace")
class XonSurfaceDXGIFormat(Enum):
UNKNOWN = 0
R8G8B8A8_UNORM_STRAIGHT= 0x00010700
R8G8B8A8_UNORM = 0x00010800
R8G8B8A8_UNORM_SRGB = 0x00010B00
BC1_UNORM = 0x00010F00
BC1_UNORM_SRGB = 0x00011000
BC2_UNORM = 0x00011100
BC2_UNORM_SRGB = 0x00011200
BC3_UNORM = 0x00011300
BC3_UNORM_SRGB = 0x00011400
BC4_UNORM = 0x00011500
BC5_UNORM = 0x00011600
BC7_UNORM = 0x00011900
R32G32B32A32_FLOAT = 0x00030000
A8_UNORM = 0x00031700
def get_DXGI_format(surfaceFormat):
if (surfaceFormat == XonSurfaceDXGIFormat.UNKNOWN):
return "UNKNOWN"
if (surfaceFormat == XonSurfaceDXGIFormat.R32G32B32A32_FLOAT):
return 3
if (surfaceFormat == XonSurfaceDXGIFormat.R8G8B8A8_UNORM_STRAIGHT):
return 28
if (surfaceFormat == XonSurfaceDXGIFormat.R8G8B8A8_UNORM):
return 28
if (surfaceFormat == XonSurfaceDXGIFormat.R8G8B8A8_UNORM_SRGB):
return 29
if (surfaceFormat == XonSurfaceDXGIFormat.A8_UNORM):
return 65
if (surfaceFormat == XonSurfaceDXGIFormat.BC1_UNORM):
return 71
if (surfaceFormat == XonSurfaceDXGIFormat.BC1_UNORM_SRGB):
return 72
if (surfaceFormat == XonSurfaceDXGIFormat.BC2_UNORM):
return 74
if (surfaceFormat == XonSurfaceDXGIFormat.BC2_UNORM_SRGB):
return 75
if (surfaceFormat == XonSurfaceDXGIFormat.BC3_UNORM):
return 77
if (surfaceFormat == XonSurfaceDXGIFormat.BC3_UNORM_SRGB):
return 78
if (surfaceFormat == XonSurfaceDXGIFormat.BC4_UNORM):
return 80
if (surfaceFormat == XonSurfaceDXGIFormat.BC5_UNORM):
return 83
if (surfaceFormat == XonSurfaceDXGIFormat.BC7_UNORM):
return 98
return None
def get_alpha_mode(surfaceFormat):
if (surfaceFormat == XonSurfaceDXGIFormat.R8G8B8A8_UNORM_STRAIGHT):
return 1
return 2
def search_texture(textures_dir, texture_filename):
for root, dirs, files in os.walk(textures_dir):
for file in files:
if file == texture_filename:
return os.path.join(root, file)
return None