auto_build_launcher/scripts/project_interface.py

128 lines
6.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from scripts.task import Task
import re
import os
import hashlib
from utils import FileUtils
def string_to_md5(text):
# 将字符串编码为UTF-8字节
text_bytes = text.encode('utf-8')
# 创建MD5哈希对象并更新字节数据
md5_hash = hashlib.md5(text_bytes)
# 返回十六进制哈希字符串
return md5_hash.hexdigest()
class ProjectInterface(Task):
def __init__(self, context):
super().__init__(context)
self.target_code_package_name = ""
self.unity_proxy_api_file = ""
self.i_android_callback_unity_file = ""
self.target_unity_proxy_api_file = ""
self.target_i_android_callback_unity_file = ""
def generate_encryption_key(self, key: str, s_len: int = -1) -> str:
# if game_editor == "Cocos":
# return key
handle_key = self.context.package_name + key + self.context.package_name
processed_key = string_to_md5(handle_key)
while processed_key[0].isdigit():
processed_key = processed_key[1:] # 移除首字符
# 计算目标长度
if s_len > 0:
target_length = s_len
else:
target_length = len(key)
# 取前N位根据原始key长度不足则全部保留
# 如果处理后的key为空极小概率则返回空字符串
proguard_value = processed_key[:target_length] if processed_key else ""
self.context.proguard_key[key] = proguard_value
return proguard_value
def handle_common_proguard_game_mapping(self, path: str):
pattern = re.compile(r'const val (\w+) = "([^"]+)"')
lines = open(path, 'r', encoding='utf-8').readlines()
with (open(path, 'w', encoding='utf-8') as f_out):
for line in lines:
match = pattern.match(line.strip())
# print(line, match)
if match:
# 提取变量名和原始值
var_name, original_value = match.groups()
# 生成MD5并替换
encrypted_value = self.generate_encryption_key(original_value)
new_line = f'const val {var_name} = "{encrypted_value}"\n'
f_out.write(new_line)
else:
# 保留非匹配行
f_out.write(line)
def execute(self):
self.unity_proxy_api_file = os.path.join(self.context.temp_project_path,
"launcher-game/src/com/game/hachisdk/unity/UnityProxyApi.java".replace(
"/", os.sep))
self.i_android_callback_unity_file = os.path.join(self.context.temp_project_path,
"launcher-game/src/com/game/hachisdk/unity/IAndroidCallbackUnity.java".replace(
"/", os.sep))
self.target_code_package_name = self.context.package_name + "." + self.generate_encryption_key(
self.context.package_name, len(self.context.package_name.split('.')[-1]))
self.handle_common_proguard_game_mapping(
os.path.join(self.context.temp_project_path,
"launcher-game/src/com/game/hachisdk/game/ApiKeys.kt".replace("/", os.sep)))
self.handle_common_proguard_game_mapping(
os.path.join(self.context.temp_project_path,
"launcher-game/src/com/game/hachisdk/game/RespKeys.kt".replace("/", os.sep)))
self.handle_common_proguard_game_mapping(
os.path.join(self.context.temp_project_path,
"launcher-game/src/com/game/launcher/GLMaping.kt".replace("/", os.sep)))
target_unity_proxy_api_class = self.target_code_package_name + "." + self.generate_encryption_key(
"UnityProxyApi")
target_android_callback_unity_class = self.target_code_package_name + "." + self.generate_encryption_key(
"IAndroidCallbackUnity")
self.target_unity_proxy_api_file = os.path.join(self.context.temp_project_path, "launcher-game", "src",
target_unity_proxy_api_class.replace(".", os.sep) + ".java")
FileUtils.move(self.unity_proxy_api_file, self.target_unity_proxy_api_file)
self.target_i_android_callback_unity_file = os.path.join(self.context.temp_project_path, "launcher-game", "src",
target_android_callback_unity_class.replace(".",
os.sep) + ".java")
FileUtils.move(self.i_android_callback_unity_file, self.target_i_android_callback_unity_file)
text = open(self.target_unity_proxy_api_file, "r", encoding="utf-8").read()
text = text.replace("com.game.hachisdk.unity", self.target_code_package_name)
text = text.replace("UnityProxyApi", self.generate_encryption_key("UnityProxyApi"))
text = text.replace("proxyApi", self.generate_encryption_key("proxyApi"))
text = text.replace("initCallback", self.generate_encryption_key("initCallback"))
text = text.replace("IAndroidCallbackUnity", self.generate_encryption_key("IAndroidCallbackUnity"))
text = text.replace("callback", self.generate_encryption_key("callback"))
open(self.target_unity_proxy_api_file, "w", encoding="utf-8").write(text)
text = open(self.target_i_android_callback_unity_file, "r", encoding="utf-8").read()
text = text.replace("com.game.hachisdk.unity", self.target_code_package_name)
text = text.replace("IAndroidCallbackUnity", self.generate_encryption_key("IAndroidCallbackUnity"))
text = text.replace("callback", self.generate_encryption_key("callback"))
open(self.target_i_android_callback_unity_file, "w", encoding="utf-8").write(text)
proxy_api = self.generate_encryption_key("proxyApi")
init_callback = self.generate_encryption_key("initCallback")
callback = self.generate_encryption_key("callback")
print("target_unity_proxy_api_class", target_unity_proxy_api_class)
print("target_android_callback_unity_class", target_android_callback_unity_class)
print("proxy_api", proxy_api)
print("init_callback", init_callback)
print("callback", callback)
pass