255 lines
11 KiB
Python
255 lines
11 KiB
Python
# 传入参数 0:SDK_PATH 1:ChannelPath 2:Obfuz(可选, true/false)
|
||
import sys
|
||
import os
|
||
import yaml
|
||
|
||
def main():
|
||
if len(sys.argv) < 3:
|
||
print("Usage: python PreHandle.py <SDK_PATH> <ChannelPath> [Obfuz]")
|
||
sys.exit(1) # 参数不足时返回错误码1
|
||
|
||
sdk_path = sys.argv[1]
|
||
channel_path = sys.argv[2]
|
||
obfuz = sys.argv[3].lower() == 'true' if len(sys.argv) >= 4 else None
|
||
|
||
if not os.path.exists(sdk_path):
|
||
print(f"Error: SDK path '{sdk_path}' does not exist.")
|
||
sys.exit(2) # SDK路径不存在时返回错误码2
|
||
|
||
if not os.path.exists(channel_path):
|
||
print(f"Error: Channel path '{channel_path}' does not exist.")
|
||
sys.exit(3) # Channel路径不存在时返回错误码3
|
||
|
||
# 这里可以添加更多的处理逻辑
|
||
print(f"SDK Path: {sdk_path}")
|
||
print(f"Channel Path: {channel_path}")
|
||
|
||
# 读取Channel Path/ChannelSetting.yaml
|
||
channelSettingPath = os.path.join(channel_path, "ChannelSetting.yaml")
|
||
if not os.path.exists(channelSettingPath):
|
||
print(f"Error: Channel setting file '{channelSettingPath}' does not exist.")
|
||
sys.exit(4) # Channel设置文件不存在时返回错误码4
|
||
with open(channelSettingPath, 'r', encoding='utf-8') as file:
|
||
channel_setting = file.read()
|
||
print(f"Channel Setting: {channel_setting}")
|
||
# 使用yaml解析
|
||
try:
|
||
channel_data = yaml.safe_load(channel_setting)
|
||
print(f"Parsed Channel Data: {channel_data}")
|
||
except yaml.YAMLError as e:
|
||
print(f"Error parsing YAML: {e}")
|
||
sys.exit(5)
|
||
|
||
# 检查Packages是否存在
|
||
# 这里假设Packages是一个列表 内容是 packageName@version
|
||
# 例如: ["package1@1.0.0", "package2@latest"]
|
||
packageList = channel_data.get("Packages", [])
|
||
if not packageList:
|
||
print("Info: PackageList is empty or not found in ChannelSetting.yaml.")
|
||
# 如果PackageList为空,则不处理
|
||
return
|
||
for package in packageList:
|
||
HandlePackage(package, sdk_path, channel_path)
|
||
|
||
# 同步 USE_OBFUZ 宏(如果传入了 Obfuz 参数)
|
||
if obfuz is not None:
|
||
SyncUseObfuzDefine(channel_path, obfuz)
|
||
|
||
# 处理完成,正常退出
|
||
print("All packages processed successfully.")
|
||
sys.exit(0) # 改为成功退出
|
||
|
||
def SyncUseObfuzDefine(channel_path, enable):
|
||
"""根据 enable 参数在 ChannelSetting.yaml 的 Define_list 中添加或移除 USE_OBFUZ"""
|
||
channel_setting_path = os.path.join(channel_path, "ChannelSetting.yaml")
|
||
if not os.path.exists(channel_setting_path):
|
||
print(f"[PreHandle] WARNING: ChannelSetting.yaml not found at {channel_setting_path}, skip USE_OBFUZ sync")
|
||
return
|
||
|
||
try:
|
||
with open(channel_setting_path, 'r', encoding='utf-8') as f:
|
||
data = yaml.safe_load(f) or {}
|
||
except (IOError, yaml.YAMLError) as e:
|
||
print(f"[PreHandle] ERROR: Failed to read ChannelSetting.yaml: {e}")
|
||
return
|
||
|
||
defines = data.get('Define_list') or []
|
||
changed = False
|
||
if enable:
|
||
if 'USE_OBFUZ' not in defines:
|
||
defines.append('USE_OBFUZ')
|
||
data['Define_list'] = defines
|
||
changed = True
|
||
print('[PreHandle] Added USE_OBFUZ to Define_list')
|
||
else:
|
||
print('[PreHandle] USE_OBFUZ already in Define_list, skip')
|
||
else:
|
||
if 'USE_OBFUZ' in defines:
|
||
defines.remove('USE_OBFUZ')
|
||
data['Define_list'] = defines
|
||
changed = True
|
||
print('[PreHandle] Removed USE_OBFUZ from Define_list')
|
||
else:
|
||
print('[PreHandle] USE_OBFUZ not in Define_list, skip')
|
||
|
||
if changed:
|
||
try:
|
||
with open(channel_setting_path, 'w', encoding='utf-8') as f:
|
||
yaml.dump(data, f, allow_unicode=True)
|
||
except IOError as e:
|
||
print(f"[PreHandle] ERROR: Failed to write ChannelSetting.yaml: {e}")
|
||
|
||
|
||
def HandlePackage(packageDefineName, sdk_path, channel_path):
|
||
# 处理包定义名称
|
||
if '@' not in packageDefineName:
|
||
print(f"Error: Invalid package format '{packageDefineName}'. Expected format 'packageName@version'.")
|
||
sys.exit(8) # 返回错误码8
|
||
packageName, version = packageDefineName.split('@', 1)
|
||
print(f"Start Handle Package Name: {packageName}, Version: {version}")
|
||
# 检查sdk_path/packageName 是否存在
|
||
package_path = os.path.join(sdk_path, packageName)
|
||
if not os.path.exists(package_path):
|
||
print(f"Error: Package path '{package_path}' does not exist.")
|
||
sys.exit(7) # 包路径不存在时返回错误码7
|
||
|
||
# 如果version是latest, 则读取package_path/latest.txt
|
||
if version == "latest":
|
||
latest_file_path = os.path.join(package_path, "latest.txt")
|
||
if not os.path.exists(latest_file_path):
|
||
print(f"Error: Latest file '{latest_file_path}' does not exist.")
|
||
sys.exit(9)
|
||
with open(latest_file_path, 'r', encoding='utf-8') as file:
|
||
latest_version = file.read().strip()
|
||
print(f"Latest Version: {latest_version}")
|
||
version = latest_version # 添加这行:更新version变量
|
||
|
||
#判断package_path/version文件夹是否存在
|
||
version_path = os.path.join(package_path, version)
|
||
if not os.path.exists(version_path):
|
||
print(f"Error: Version path '{version_path}' does not exist.")
|
||
sys.exit(10)
|
||
# 把version_path/Assets下所有的文件和文件夹复制到channel_path/Assets
|
||
srcAssetPath = os.path.join(version_path, "Assets")
|
||
targetAssetPath = os.path.join(channel_path, "Assets")
|
||
CopyAssets(srcAssetPath, targetAssetPath)
|
||
ModifyDefineList(version_path, channel_path)
|
||
|
||
def ModifyDefineList(versionPath, channel_path):
|
||
# 读取versionPath/OtherConfig.yaml
|
||
other_config_path = os.path.join(versionPath, "OtherConfig.yaml")
|
||
if not os.path.exists(other_config_path):
|
||
print(f"Info: OtherConfig file '{other_config_path}' does not exist.")
|
||
return
|
||
|
||
try:
|
||
with open(other_config_path, 'r', encoding='utf-8') as file:
|
||
other_config = yaml.safe_load(file)
|
||
print(f"Other Config: {other_config}")
|
||
except (IOError, yaml.YAMLError) as e:
|
||
print(f"Error reading OtherConfig file '{other_config_path}': {e}")
|
||
return
|
||
|
||
# 检查是否有DefineList
|
||
define_list = other_config.get("Define_list", [])
|
||
if not define_list:
|
||
print("Info: Define_list is empty or not found in OtherConfig.yaml.")
|
||
return
|
||
|
||
# 修改channel_path/ChannelSetting.yaml中的DefineList
|
||
channel_setting_path = os.path.join(channel_path, "ChannelSetting.yaml")
|
||
if not os.path.exists(channel_setting_path):
|
||
print(f"Error: Channel setting file '{channel_setting_path}' does not exist.")
|
||
sys.exit(11)
|
||
|
||
try:
|
||
with open(channel_setting_path, 'r', encoding='utf-8') as file:
|
||
channel_setting = yaml.safe_load(file)
|
||
# print(f"Channel Setting Before Modification: {channel_setting}")
|
||
except (IOError, yaml.YAMLError) as e:
|
||
print(f"Error reading channel setting file '{channel_setting_path}': {e}")
|
||
sys.exit(11)
|
||
|
||
print(f"Old Define_List: {channel_setting.get('Define_list', [])}")
|
||
|
||
# 修改DefineList(添加去重功能)
|
||
# 注意:Define_list 可能不存在,或存在但值为 None(YAML 中写了 key 但没有值)
|
||
if not channel_setting.get("Define_list"):
|
||
channel_setting["Define_list"] = []
|
||
|
||
# 合并并去重
|
||
current_defines = set(channel_setting["Define_list"])
|
||
new_defines = set(define_list)
|
||
|
||
# 输出将要添加的新定义
|
||
added_defines = new_defines - current_defines
|
||
if added_defines:
|
||
print(f"Adding new defines: {list(added_defines)}")
|
||
|
||
# 重复项警告
|
||
duplicate_defines = new_defines & current_defines
|
||
if duplicate_defines:
|
||
print(f"Warning: Duplicate defines found and will be ignored: {list(duplicate_defines)}")
|
||
|
||
# 更新定义列表(保持原有顺序,添加新项)
|
||
channel_setting["Define_list"] = list(current_defines | new_defines)
|
||
|
||
try:
|
||
with open(channel_setting_path, 'w', encoding='utf-8') as file:
|
||
yaml.dump(channel_setting, file, allow_unicode=True)
|
||
# print(f"Channel Setting After Modification: {channel_setting}")
|
||
except IOError as e:
|
||
print(f"Error writing channel setting file '{channel_setting_path}': {e}")
|
||
sys.exit(15)
|
||
|
||
print(f"New Define_List: {channel_setting['Define_list']}")
|
||
|
||
def CopyAssets(srcAssetPath, targetAssetPath):
|
||
# 如果srcAssetPath不存在,则返回
|
||
if not os.path.exists(srcAssetPath):
|
||
print(f"Info: Source asset path '{srcAssetPath}' does not exist.")
|
||
return
|
||
# 如果targetAssetPath不存在,则创建
|
||
if not os.path.exists(targetAssetPath):
|
||
try:
|
||
os.makedirs(targetAssetPath)
|
||
# print(f"Created target asset path: {targetAssetPath}")
|
||
except OSError as e:
|
||
print(f"Error: Failed to create directory '{targetAssetPath}': {e}")
|
||
sys.exit(12)
|
||
|
||
# print(f"Start copying assets from '{srcAssetPath}' to '{targetAssetPath}'")
|
||
|
||
# 遍历srcAssetPath下的所有文件和文件夹
|
||
try:
|
||
for item in os.listdir(srcAssetPath):
|
||
src_item_path = os.path.join(srcAssetPath, item)
|
||
target_item_path = os.path.join(targetAssetPath, item)
|
||
# 如果是文件,则复制
|
||
if os.path.isfile(src_item_path):
|
||
# 检查目标文件是否已存在,如果存在则输出警告
|
||
if os.path.exists(target_item_path):
|
||
print(f"Warning: Target file '{target_item_path}' already exists and will be overwritten.")
|
||
|
||
try:
|
||
with open(src_item_path, 'rb') as src_file:
|
||
with open(target_item_path, 'wb') as target_file:
|
||
target_file.write(src_file.read())
|
||
# print(f"Copied file: {src_item_path} to {target_item_path}")
|
||
except IOError as e:
|
||
print(f"Error: Failed to copy file '{src_item_path}': {e}")
|
||
sys.exit(13)
|
||
# 如果是文件夹,则递归调用CopyAssets
|
||
elif os.path.isdir(src_item_path):
|
||
# 检查目标目录是否已存在,如果存在则输出警告
|
||
if os.path.exists(target_item_path):
|
||
print(f"Warning: Target directory '{target_item_path}' already exists, merging contents.")
|
||
CopyAssets(src_item_path, target_item_path)
|
||
except OSError as e:
|
||
print(f"Error: Failed to list directory '{srcAssetPath}': {e}")
|
||
sys.exit(14)
|
||
|
||
# print(f"Assets copied successfully from '{srcAssetPath}' to '{targetAssetPath}'")
|
||
|
||
if __name__ == "__main__":
|
||
main() |