-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
setup.py
142 lines (120 loc) · 4.06 KB
/
setup.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
133
134
135
136
137
138
139
140
141
142
import os
import re
import subprocess
from typing import Union
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist
try:
from setuptools.command.bdist_wheel import bdist_wheel
except ImportError:
from wheel.bdist_wheel import bdist_wheel
INSTALL_DIR = os.path.dirname(os.path.realpath(__file__))
UNITYPYBOOST_DIR = os.path.join(INSTALL_DIR, "UnityPyBoost")
class BuildExt(build_ext):
def build_extensions(self):
cpp_version_flag: str
compiler = self.compiler
# msvc - only ever used c++20, never c++2a
if compiler.compiler_type == "msvc":
cpp_version_flag = "/std:c++20"
# gnu & clang
elif compiler.compiler_type == "unix":
res = subprocess.run(
[compiler.compiler[0], "-v"],
capture_output=True,
)
# for some reason g++ and clang++ return this as error
text = (res.stdout or res.stderr).decode("utf-8")
version = re.search(r"version\s+(\d+)\.", text)
if version is None:
raise Exception("Failed to determine compiler version")
version = int(version.group(1))
if version < 10:
cpp_version_flag = "-std=c++2a"
else:
cpp_version_flag = "-std=c++20"
else:
cpp_version_flag = "-std=c++20"
for ext in self.extensions:
ext.extra_compile_args = [cpp_version_flag]
build_ext.build_extensions(self)
class SDist(sdist):
def make_distribution(self) -> None:
# add all fmod libraries to the distribution
for root, dirs, files in os.walk("UnityPy/lib/FMOD"):
for file in files:
fp = f"{root}/{file}"
if fp not in self.filelist.files:
self.filelist.files.append(fp)
return super().make_distribution()
BDIST_TAG_FMOD_MAP = {
# Windows
"win32": "x86",
"win_amd64": "x64",
"win_arm64": "arm",
# Linux and Mac endings
"arm64": "arm", # Mac
"x86_64": "x64",
"aarch64": "arm", # Linux
"i686": "x86",
"armv7l": "arm", # armhf
}
def get_fmod_path(
system: Union["Windows", "Linux", "Darwin"], arch: ["x64", "x86", "arm"]
) -> str:
if system == "Darwin":
# universal dylib
return "lib/FMOD/Darwin/libfmod.dylib"
if system == "Windows":
return f"lib/FMOD/Windows/{arch}/fmod.dll"
if system == "Linux":
if arch == "x64":
arch = "x86_64"
return f"lib/FMOD/Linux/{arch}/libfmod.so"
raise NotImplementedError(f"Unsupported system: {system}")
class BDistWheel(bdist_wheel):
def run(self):
platform_tag = self.get_tag()[2]
if platform_tag.startswith("win"):
system = "Windows"
arch = BDIST_TAG_FMOD_MAP[platform_tag]
else:
arch = next(
(v for k, v in BDIST_TAG_FMOD_MAP.items() if platform_tag.endswith(k)),
None,
)
if platform_tag.startswith("macosx"):
system = "Darwin"
else:
system = "Linux"
try:
self.distribution.package_data["UnityPy"].append(
get_fmod_path(system, arch)
)
except NotImplementedError:
pass
super().run()
setup(
name="UnityPy",
packages=find_packages(),
package_data={"UnityPy": ["resources/uncompressed.tpk"]},
ext_modules=[
Extension(
"UnityPy.UnityPyBoost",
[
f"UnityPyBoost/{f}"
for f in os.listdir(UNITYPYBOOST_DIR)
if f.endswith(".cpp")
],
depends=[
f"UnityPyBoost/{f}"
for f in os.listdir(UNITYPYBOOST_DIR)
if f.endswith(".hpp")
],
language="c++",
include_dirs=[UNITYPYBOOST_DIR],
)
],
cmdclass={"build_ext": BuildExt, "sdist": SDist, "bdist_wheel": BDistWheel},
)