LunaTranslator/.github/scripts/build_lunatranslator.py

363 lines
13 KiB
Python
Raw Normal View History

2024-08-07 22:00:27 +08:00
import os, sys, re, json
2024-04-27 19:56:54 +08:00
import shutil, json
2024-05-09 16:58:24 +08:00
import subprocess, time
2024-05-01 17:40:57 +08:00
import urllib.request
2024-08-07 22:09:20 +08:00
from urllib.parse import urljoin
2024-04-26 23:13:46 +08:00
2024-08-07 22:18:52 +08:00
rootDir = os.path.dirname(__file__)
2024-11-02 19:55:32 +08:00
if not rootDir:
rootDir = os.path.abspath(".")
else:
rootDir = os.path.abspath(rootDir)
2024-11-15 01:33:17 +08:00
rootthisfiledir = rootDir
rootDir = os.path.abspath(os.path.join(rootDir, "../../py"))
2024-08-07 22:18:52 +08:00
if sys.argv[1] == "loadversion":
os.chdir(rootDir)
2024-11-06 08:36:33 +08:00
with open("../cpp/version.cmake", "r", encoding="utf8") as ff:
2024-08-07 22:18:52 +08:00
pattern = r"set\(VERSION_MAJOR\s*(\d+)\s*\)\nset\(VERSION_MINOR\s*(\d+)\s*\)\nset\(VERSION_PATCH\s*(\d+)\s*\)"
match = re.findall(pattern, ff.read())[0]
version_major, version_minor, version_patch = match
versionstring = f"v{version_major}.{version_minor}.{version_patch}"
print("version=" + versionstring)
exit()
2024-09-16 14:37:29 +08:00
2024-11-02 19:55:32 +08:00
print(sys.version)
print(__file__)
print(rootDir)
2024-09-16 14:37:29 +08:00
mylinks = {
"ocr_models": {
2024-09-16 14:38:56 +08:00
"ja.zip": "https://github.com/test123456654321/RESOURCES/releases/download/ocr_models/ja.zip",
2024-08-07 04:29:08 +08:00
},
2024-11-17 03:03:26 +08:00
"mecab_xp.zip": "https://github.com/HIllya51/RESOURCES/releases/download/common/mecab_xp.zip",
2024-09-16 14:37:29 +08:00
"mecab.zip": "https://github.com/HIllya51/RESOURCES/releases/download/common/mecab.zip",
"magpie.zip": "https://github.com/HIllya51/RESOURCES/releases/download/common/magpie.zip",
2024-10-20 00:37:51 +08:00
"themes.zip": "https://github.com/HIllya51/RESOURCES/releases/download/common/themes.zip",
2024-08-07 04:29:08 +08:00
}
2024-11-06 06:46:35 +08:00
pluginDirs = ["DLL32", "DLL64", "Locale_Remulator", "Magpie", "NTLEAS"]
2024-11-15 19:40:53 +08:00
pluginDirs_1 = ["DLL32", "DLL64", "NTLEAS"]
vcltlFile = "https://github.com/Chuyu-Team/VC-LTL5/releases/download/v5.0.9/VC-LTL-5.0.9-Binary.7z"
2024-09-16 14:37:29 +08:00
brotliFile32 = "https://github.com/google/brotli/releases/latest/download/brotli-x86-windows-dynamic.zip"
brotliFile64 = "https://github.com/google/brotli/releases/latest/download/brotli-x64-windows-dynamic.zip"
2024-09-16 14:37:29 +08:00
localeEmulatorFile = "https://github.com/xupefei/Locale-Emulator/releases/download/v2.5.0.1/Locale.Emulator.2.5.0.1.zip"
ntleaFile = "https://github.com/zxyacb/ntlea/releases/download/0.46/ntleas046_x64.7z"
2024-09-16 14:37:29 +08:00
LocaleRe = "https://github.com/InWILL/Locale_Remulator/releases/download/v1.5.3-beta.1/Locale_Remulator.1.5.3-beta.1.zip"
2024-07-17 03:00:09 +08:00
curlFile32 = "https://curl.se/windows/dl-8.8.0_3/curl-8.8.0_3-win32-mingw.zip"
curlFile64 = "https://curl.se/windows/dl-8.8.0_3/curl-8.8.0_3-win64-mingw.zip"
availableLocales = ["cht", "en", "ja", "ko", "ru", "zh"]
2024-07-01 02:21:03 +08:00
2024-11-15 19:35:30 +08:00
def createPluginDirs(which):
2024-10-18 20:03:38 +08:00
os.chdir(rootDir + "\\files")
if not os.path.exists("plugins"):
os.mkdir("plugins")
os.chdir("plugins")
2024-11-15 19:35:30 +08:00
for pluginDir in [pluginDirs_1, pluginDirs][which]:
if not os.path.exists(pluginDir):
os.mkdir(pluginDir)
def installVCLTL():
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO {vcltlFile}")
2024-09-16 14:37:29 +08:00
subprocess.run(f"7z x {vcltlFile.split('/')[-1]} -oVC-LTL5")
os.chdir("VC-LTL5")
subprocess.run("cmd /c Install.cmd")
def downloadBrotli():
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO {brotliFile32}")
subprocess.run(f"curl -LO {brotliFile64}")
2024-09-16 14:37:29 +08:00
subprocess.run(f"7z x {brotliFile32.split('/')[-1]} -obrotli32")
subprocess.run(f"7z x {brotliFile64.split('/')[-1]} -obrotli64")
2024-10-18 20:03:38 +08:00
shutil.move("brotli32/brotlicommon.dll", f"{rootDir}/files/plugins/DLL32")
shutil.move("brotli32/brotlidec.dll", f"{rootDir}/files/plugins/DLL32")
shutil.move("brotli64/brotlicommon.dll", f"{rootDir}/files/plugins/DLL64")
shutil.move("brotli64/brotlidec.dll", f"{rootDir}/files/plugins/DLL64")
2024-05-03 19:44:40 +08:00
def downloadlr():
2024-07-01 02:21:03 +08:00
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO {LocaleRe}")
subprocess.run(f"7z x {LocaleRe.split('/')[-1]} -oLR")
os.makedirs(
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/Locale_Remulator",
2024-07-01 02:21:03 +08:00
exist_ok=True,
)
for _dir, _, _fs in os.walk("LR"):
for f in _fs:
if f in ["LRHookx64.dll", "LRHookx32.dll"]:
shutil.move(
os.path.join(_dir, f),
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/Locale_Remulator",
2024-07-01 02:21:03 +08:00
)
2024-05-03 19:44:40 +08:00
2024-05-29 02:09:31 +08:00
def move_directory_contents(source_dir, destination_dir):
contents = os.listdir(source_dir)
for item in contents:
if item == ".git":
continue
item_path = os.path.join(source_dir, item)
try:
shutil.move(item_path, destination_dir)
except:
for k in os.listdir(item_path):
shutil.move(
os.path.join(item_path, k), os.path.join(destination_dir, item)
)
2024-11-15 19:08:41 +08:00
def downloadmecab():
2024-04-26 05:53:33 +08:00
os.chdir(rootDir + "\\temp")
2024-09-16 14:37:29 +08:00
subprocess.run(f"curl -LO {mylinks['mecab.zip']}")
2024-04-26 23:13:46 +08:00
subprocess.run(f"7z x mecab.zip -oALL")
2024-11-15 19:46:39 +08:00
move_directory_contents("ALL/ALL", f"{rootDir}/files/plugins")
2024-11-17 03:03:26 +08:00
def downloadmecabxp():
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO {mylinks['mecab_xp.zip']}")
subprocess.run(f"7z x mecab_xp.zip -oALL")
move_directory_contents("ALL/ALL", f"{rootDir}/files/plugins")
2024-11-15 19:08:41 +08:00
def downloadmapie():
os.chdir(rootDir + "\\temp")
2024-09-16 14:37:29 +08:00
subprocess.run(f"curl -LO {mylinks['magpie.zip']}")
2024-04-26 23:13:46 +08:00
subprocess.run(f"7z x magpie.zip -oALL")
2024-11-15 19:46:39 +08:00
move_directory_contents("ALL/ALL", f"{rootDir}/files/plugins")
2024-04-26 23:13:46 +08:00
2024-04-26 05:53:33 +08:00
def downloadLocaleEmulator():
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO {localeEmulatorFile}")
2024-09-16 14:37:29 +08:00
subprocess.run(f"7z x {localeEmulatorFile.split('/')[-1]} -oLocaleEmulator")
shutil.move(
"LocaleEmulator/LoaderDll.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/LoaderDll.dll",
)
shutil.move(
"LocaleEmulator/LocaleEmulator.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/LocaleEmulator.dll",
)
def downloadNtlea():
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO {ntleaFile}")
2024-09-16 14:37:29 +08:00
subprocess.run(f"7z x {ntleaFile.split('/')[-1]} -ontlea")
shutil.move(
"ntlea/x86/ntleai.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/NTLEAS/ntleai.dll",
)
shutil.move(
"ntlea/x64/ntleak.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/NTLEAS/ntleak.dll",
)
def downloadCurl():
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO {curlFile32}")
subprocess.run(f"curl -LO {curlFile64}")
2024-09-16 14:37:29 +08:00
subprocess.run(f"7z x {curlFile32.split('/')[-1]}")
subprocess.run(f"7z x {curlFile64.split('/')[-1]}")
2024-09-16 14:38:56 +08:00
outputDirName32 = curlFile32.split("/")[-1].replace(".zip", "")
shutil.move(
f"{outputDirName32}/bin/libcurl.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/DLL32",
)
2024-09-16 14:38:56 +08:00
outputDirName64 = curlFile64.split("/")[-1].replace(".zip", "")
shutil.move(
f"{outputDirName64}/bin/libcurl-x64.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/DLL64",
)
2024-08-07 04:29:08 +08:00
def downloadOCRModel():
2024-10-18 20:03:38 +08:00
os.chdir(rootDir + "\\files")
if not os.path.exists("ocr"):
os.mkdir("ocr")
os.chdir("ocr")
2024-09-16 14:37:29 +08:00
subprocess.run(f"curl -LO {mylinks['ocr_models']['ja.zip']}")
2024-08-07 04:29:08 +08:00
subprocess.run(f"7z x ja.zip")
os.remove(f"ja.zip")
2024-05-01 17:40:57 +08:00
def get_url_as_json(url):
2024-05-09 16:58:24 +08:00
for i in range(10):
try:
response = urllib.request.urlopen(url)
data = response.read().decode("utf-8")
json_data = json.loads(data)
return json_data
except:
time.sleep(3)
2024-11-15 01:33:17 +08:00
2024-11-02 19:55:32 +08:00
def buildPlugins(arch):
2024-11-06 06:46:35 +08:00
os.chdir(rootDir + "\\..\\cpp\\scripts")
2024-06-05 04:00:10 +08:00
subprocess.run("python fetchwebview2.py")
2024-11-02 19:55:32 +08:00
if arch == "x86":
subprocess.run(
f'cmake ../CMakeLists.txt -G "Visual Studio 17 2022" -A win32 -T host=x86 -B ../build/x86 -DCMAKE_SYSTEM_VERSION=10.0.26621.0'
)
subprocess.run(
f"cmake --build ../build/x86 --config Release --target ALL_BUILD -j 14"
)
# subprocess.run(f"python copytarget.py 1")
2024-11-15 04:03:18 +08:00
elif arch == "x64":
2024-11-02 19:55:32 +08:00
subprocess.run(
f'cmake ../CMakeLists.txt -G "Visual Studio 17 2022" -A x64 -T host=x64 -B ../build/x64 -DCMAKE_SYSTEM_VERSION=10.0.26621.0'
)
subprocess.run(
f"cmake --build ../build/x64 --config Release --target ALL_BUILD -j 14"
)
# subprocess.run(f"python copytarget.py 0")
2024-11-15 04:03:18 +08:00
elif arch == "xp":
2024-11-15 19:08:41 +08:00
# url = "https://github.com/Chuyu-Team/YY-Thunks/releases/download/v1.0.7/YY-Thunks-1.0.7-Binary.zip"
# os.system(rf"curl -SLo YY-Thunks-1.0.7-Binary.zip " + url)
# os.system(rf"7z x -y YY-Thunks-1.0.7-Binary.zip -o../libs/YY-Thunks")
2024-11-15 03:04:53 +08:00
with open("do.bat", "w") as ff:
ff.write(
2024-11-15 04:03:18 +08:00
rf"""
2024-11-15 03:04:53 +08:00
cmake -DWINXP=ON ../CMakeLists.txt -G "Visual Studio 16 2019" -A win32 -T v141_xp -B ../build/x86_xp
cmake --build ../build/x86_xp --config Release --target ALL_BUILD -j 14
"""
2024-11-15 04:03:18 +08:00
)
2024-11-15 03:04:53 +08:00
os.system(f"cmd /c do.bat")
2024-11-15 04:03:18 +08:00
2024-08-10 20:17:15 +08:00
def downloadbass():
os.chdir(rootDir + "\\temp")
subprocess.run(f"curl -LO https://www.un4seen.com/files/bass24.zip")
subprocess.run(f"7z x bass24.zip -oALL")
2024-08-10 20:43:38 +08:00
shutil.move(
"ALL/bass.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/DLL32",
2024-08-10 20:43:38 +08:00
)
shutil.move(
"ALL/x64/bass.dll",
2024-10-18 20:03:38 +08:00
f"{rootDir}/files/plugins/DLL64",
2024-08-07 04:29:08 +08:00
)
2024-05-29 02:09:31 +08:00
if __name__ == "__main__":
os.chdir(rootDir)
2024-04-26 23:13:46 +08:00
os.makedirs("temp", exist_ok=True)
2024-11-02 19:55:32 +08:00
arch = sys.argv[2]
if sys.argv[1] == "cpp":
installVCLTL()
buildPlugins(arch)
elif sys.argv[1] == "pyrt":
version = sys.argv[3]
if arch == "x86":
py37Path = (
f"C:\\hostedtoolcache\\windows\\Python\\{version}\\x86\\python.exe"
)
else:
py37Path = (
f"C:\\hostedtoolcache\\windows\\Python\\{version}\\x64\\python.exe"
)
os.chdir(rootDir)
subprocess.run(f"{py37Path} -m pip install --upgrade pip")
subprocess.run(f"{py37Path} -m pip install -r requirements.txt")
# 3.8之后会莫名其妙引用这个b东西然后这个b东西会把一堆没用的东西导入进来
shutil.rmtree(os.path.join(os.path.dirname(py37Path), "Lib\\test"))
shutil.rmtree(os.path.join(os.path.dirname(py37Path), "Lib\\unittest"))
# 放弃3.8需要安装KB2533623才能运行3.7用不着。
2024-11-15 01:33:17 +08:00
subprocess.run(
f"{py37Path} {os.path.join(rootthisfiledir,'collectpyruntime.py')}"
)
2024-11-02 19:55:32 +08:00
elif sys.argv[1] == "merge":
2024-11-15 19:35:30 +08:00
createPluginDirs(0 if arch == "xp" else 1)
2024-11-02 19:55:32 +08:00
downloadNtlea()
downloadbass()
os.chdir(rootDir)
2024-11-15 04:03:18 +08:00
if arch == "xp":
2024-11-17 03:09:10 +08:00
downloadmecabxp()
2024-11-15 04:03:18 +08:00
shutil.copytree(
f"{rootDir}/../build/cpp_xp",
f"{rootDir}/../cpp/builds",
dirs_exist_ok=True,
)
shutil.copytree(
f"{rootDir}/../build/hook_xp",
f"{rootDir}/files/plugins/LunaHook",
dirs_exist_ok=True,
)
os.chdir(rootDir + "/../cpp/scripts")
os.makedirs("../../py/files/plugins/DLL32", exist_ok=True)
shutil.copy("../builds/_x86/shareddllproxy32.exe", "../../py/files/plugins")
shutil.copy(
"../builds/_x86/winsharedutils32.dll", "../../py/files/plugins/DLL32"
)
os.chdir(rootDir)
os.system(f"python {os.path.join(rootthisfiledir,'collectall_xp.py')}")
exit()
2024-11-17 03:03:26 +08:00
downloadmecab()
2024-11-15 19:19:47 +08:00
downloadLocaleEmulator()
downloadBrotli()
downloadCurl()
downloadOCRModel()
downloadmapie()
downloadlr()
2024-11-15 19:35:30 +08:00
2024-11-15 19:22:57 +08:00
os.chdir(rootDir)
2024-11-02 19:55:32 +08:00
shutil.copytree(
2024-11-06 06:46:35 +08:00
f"{rootDir}/../build/hook_64",
f"{rootDir}/files/plugins/LunaHook",
dirs_exist_ok=True,
)
shutil.copytree(
f"{rootDir}/../build/hook_32",
f"{rootDir}/files/plugins/LunaHook",
2024-11-02 19:55:32 +08:00
dirs_exist_ok=True,
)
shutil.copytree(
f"{rootDir}/../build/cpp_x64",
2024-11-06 06:46:35 +08:00
f"{rootDir}/../cpp/builds",
dirs_exist_ok=True,
)
shutil.copytree(
f"{rootDir}/../build/cpp_x86",
f"{rootDir}/../cpp/builds",
2024-11-02 19:55:32 +08:00
dirs_exist_ok=True,
)
2024-11-06 06:46:35 +08:00
os.chdir(rootDir + "/../cpp/scripts")
2024-11-02 19:55:32 +08:00
2024-11-15 01:33:17 +08:00
os.makedirs("../../py/files/plugins/DLL32", exist_ok=True)
shutil.copy("../builds/_x86/shareddllproxy32.exe", "../../py/files/plugins")
shutil.copy("../builds/_x86/winrtutils32.dll", "../../py/files/plugins/DLL32")
shutil.copy(
"../builds/_x86/winsharedutils32.dll", "../../py/files/plugins/DLL32"
)
shutil.copy("../builds/_x86/wcocr.dll", "../../py/files/plugins/DLL32")
shutil.copy("../builds/_x86/LunaOCR32.dll", "../../py/files/plugins/DLL32")
os.makedirs("../../py/files/plugins/DLL64", exist_ok=True)
shutil.copy("../builds/_x64/shareddllproxy64.exe", "../../py/files/plugins")
shutil.copy("../builds/_x64/hookmagpie.dll", "../../py/files/plugins")
shutil.copy("../builds/_x64/winrtutils64.dll", "../../py/files/plugins/DLL64")
shutil.copy(
"../builds/_x64/winsharedutils64.dll", "../../py/files/plugins/DLL64"
)
shutil.copy("../builds/_x64/wcocr.dll", "../../py/files/plugins/DLL64")
shutil.copy("../builds/_x64/LunaOCR64.dll", "../../py/files/plugins/DLL64")
2024-11-02 19:55:32 +08:00
if arch == "x86":
os.chdir(rootDir)
2024-11-08 17:07:24 +08:00
os.system(f"python {os.path.join(rootthisfiledir,'collectall.py')} 32")
2024-11-02 19:55:32 +08:00
else:
os.chdir(rootDir)
2024-11-08 17:07:24 +08:00
os.system(f"python {os.path.join(rootthisfiledir,'collectall.py')} 64")