359 lines
12 KiB
C#
359 lines
12 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
using UnityEngine.Windows;
|
|
using HybridCLR.Editor.Commands;
|
|
using PhxhSDK.AOT.VersionUpdate;
|
|
using Debug = UnityEngine.Debug;
|
|
using System.Collections.Generic;
|
|
using AutoBuild.Helper;
|
|
using HybridCLR.Editor.Installer;
|
|
using UnityEditor.SceneManagement;
|
|
using BuildTarget = UnityEditor.BuildTarget;
|
|
|
|
/// <summary>
|
|
/// 编辑器下打包
|
|
/// </summary>
|
|
class BuildLauncher
|
|
{
|
|
private const string HybirdCLRGenerateDir = "/HybridCLRGenerate";
|
|
private const string HybirdCLRGenerateLinkPath = "Assets/HybridCLRGenerate/link.xml";
|
|
private const string GameLauncherSceneName = "Assets/Scenes/GameLauncher.unity";
|
|
|
|
//"/var/www/html/nld/Build/$WORKSPACE/$PLATFORM/$VERSION"
|
|
private const string RemotePath = "/var/www/html/nld/Build/{0}/{1}/{2}";
|
|
private const string RemoteHost = "132.232.113.98";
|
|
private const string RemoteUser = "root";
|
|
|
|
private static bool enableHotUpdate;
|
|
private static bool hasYooAsset = true;
|
|
|
|
/// <summary>
|
|
/// 开启热更
|
|
/// </summary>
|
|
public static void SetEnableHotUpdate(bool isEnable)
|
|
{
|
|
Debug.Log($"是否开启热更新:{isEnable}");
|
|
var gameLauncherScene = EditorSceneManager.OpenScene(GameLauncherSceneName, OpenSceneMode.Additive);
|
|
if (!gameLauncherScene.IsValid())
|
|
{
|
|
Debug.LogError($"无法加载场景{GameLauncherSceneName}");
|
|
return;
|
|
}
|
|
|
|
var gameLauncher = UnityEngine.Object.FindObjectOfType<GameLauncher>();
|
|
if (gameLauncher == null)
|
|
{
|
|
Debug.LogError("找不到GameLauncher");
|
|
return;
|
|
}
|
|
|
|
gameLauncher.enableHotUpdate = isEnable;
|
|
EditorSceneManager.MarkSceneDirty(gameLauncherScene);
|
|
EditorSceneManager.SaveScene(gameLauncherScene);
|
|
EditorSceneManager.CloseScene(gameLauncherScene, false);
|
|
HybridCLR.Editor.SettingsUtil.Enable = isEnable;
|
|
if (!isEnable)
|
|
{
|
|
//删除HybirdCLR生成的link文件
|
|
AssetDatabase.DeleteAsset(HybirdCLRGenerateLinkPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构建热更
|
|
/// </summary>
|
|
public static void BuildHotUpdateDlls()
|
|
{
|
|
var controller = new InstallerController();
|
|
if (!controller.HasInstalledHybridCLR())
|
|
controller.InstallDefaultHybridCLR();
|
|
BuildUtils.SetFileReadable(Application.dataPath + HybirdCLRGenerateDir);
|
|
PrebuildCommand.GenerateAll();
|
|
HotUpadeEditorHelper.CopyHotUpdateDll();
|
|
HotUpadeEditorHelper.CopyMetaDataDll();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打包面板初始化
|
|
/// </summary>
|
|
public static void Init(BuildTarget buildTarget,
|
|
string workSpace,
|
|
string appVersion,
|
|
string profileName,
|
|
bool developmentBuild,
|
|
bool hotUpdate)
|
|
{
|
|
EditorUserBuildSettings.development = developmentBuild;
|
|
YooAssetBuildBase.BuildTargetPlatform = buildTarget;
|
|
YooAssetBuildBase.profileName = profileName;
|
|
YooAssetBuildBase.JudgmentPlatform(buildTarget.ToString());
|
|
YooAssetBuildBase.workSpace = string.IsNullOrEmpty(workSpace) ? "WorkSpace" : workSpace;
|
|
PlayerSettings.bundleVersion = appVersion;
|
|
YooAssetBuildBase.appVersion = appVersion;
|
|
enableHotUpdate = hotUpdate;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑器下构建资源
|
|
/// </summary>
|
|
public static void BuildResource()
|
|
{
|
|
if (hasYooAsset)
|
|
{
|
|
var info = new UpdateLocalInfo();
|
|
info.workSpace = YooAssetBuildBase.workSpace;
|
|
info.version = YooAssetBuildBase.appVersion;
|
|
info.serverUrl = "http://132.232.113.98";
|
|
info.buildTicks = DateTime.Now.Ticks;
|
|
PackDataInst.CreateResourceJson(info);
|
|
|
|
ExtBuildInfoHelper.CreateFirstExtBuildInfo(info.buildTicks, info.workSpace, info.version);
|
|
}
|
|
|
|
SetEnableHotUpdate(enableHotUpdate);
|
|
if (enableHotUpdate)
|
|
{
|
|
BuildHotUpdateDlls();
|
|
}
|
|
|
|
YooAssetBuildBase.BuildResourceInEditor();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑器下构建资源和Player
|
|
/// </summary>
|
|
public static void BuildResourceAndPlayer()
|
|
{
|
|
if (hasYooAsset)
|
|
{
|
|
var info = new UpdateLocalInfo();
|
|
info.workSpace = YooAssetBuildBase.workSpace;
|
|
info.version = YooAssetBuildBase.appVersion;
|
|
info.serverUrl = "http://132.232.113.98";
|
|
info.buildTicks = DateTime.Now.Ticks;
|
|
PackDataInst.CreateResourceJson(info);
|
|
|
|
ExtBuildInfoHelper.CreateFirstExtBuildInfo(info.buildTicks, info.workSpace, info.version);
|
|
}
|
|
|
|
SetEnableHotUpdate(enableHotUpdate);
|
|
if (enableHotUpdate)
|
|
{
|
|
BuildHotUpdateDlls();
|
|
}
|
|
|
|
YooAssetBuildBase.BuildContentAndPlayerInEditor(RemoteUser, RemoteHost, RemotePath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 只构建Player
|
|
/// </summary>
|
|
public static void BuildPlayer(BuildTarget buildTarget,
|
|
bool isDev = false)
|
|
{
|
|
YooAssetBuildBase.BuildPlayerInEditor();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑器下热更
|
|
/// </summary>
|
|
public static void UpdatePreviousBuild(string workSpace,
|
|
string version,
|
|
BuildTarget buildTarget)
|
|
{
|
|
try
|
|
{
|
|
if (enableHotUpdate)
|
|
BuildHotUpdateDlls();
|
|
YooAssetBuildBase.BuildOnlyResUpdate();
|
|
YooAssetBuildBase.UploadToServerYooAsset(workSpace, version, buildTarget, RemoteUser, RemoteHost,
|
|
RemotePath);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError($"[Build Exception]:{e.Message} \r\n {e.StackTrace}");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Jenkins打包
|
|
/// </summary>
|
|
internal class BatchBuild
|
|
{
|
|
/// <summary>
|
|
/// 批处理更新设置
|
|
/// jenkins打包新包时使用该方法
|
|
/// </summary>
|
|
public static void ApplyPlayerSetting()
|
|
{
|
|
YooAssetBuildBase.Init();
|
|
}
|
|
|
|
private static string _oldDefines;
|
|
|
|
private const string DefineListKey = "Define_list:";
|
|
private const string PackageNameKey = "PackageName:";
|
|
private const string NewPlayerSettingsPath = "/../PlayerSettings.txt";
|
|
|
|
//[MenuItem("Tools/Modify Settings from Text File")]
|
|
private static void _InitScriptDefines()
|
|
{
|
|
var hasYooAsset = false;
|
|
// 判断本地是否有define_list.txt文件
|
|
string filePath = Application.dataPath + NewPlayerSettingsPath;
|
|
List<string> appendDefineList = new();
|
|
string newPackageName = "";
|
|
if (File.Exists(filePath))
|
|
{
|
|
string fileContents = System.IO.File.ReadAllText(filePath);
|
|
string[] lines = fileContents.Split('\n');
|
|
|
|
foreach (string line in lines)
|
|
{
|
|
if (line.StartsWith(DefineListKey))
|
|
{
|
|
// 解析宏定义
|
|
string definePart = line[DefineListKey.Length..].Trim(); // 去掉 "definde:" 后的部分
|
|
string[] defines = definePart.Split(',');
|
|
|
|
foreach (string define in defines)
|
|
{
|
|
string cleanDefine = define.Trim(new char[]
|
|
{
|
|
'"', ' ',
|
|
'\r', '\n'
|
|
});
|
|
|
|
if (!string.IsNullOrEmpty(cleanDefine))
|
|
{
|
|
appendDefineList.Add(cleanDefine);
|
|
Debug.Log("追加宏定义:" + cleanDefine);
|
|
}
|
|
}
|
|
}
|
|
else if (line.StartsWith(PackageNameKey))
|
|
{
|
|
// 解析包名
|
|
int prefixLength = PackageNameKey.Length;
|
|
newPackageName = line[prefixLength..].Trim();
|
|
Debug.Log("设置新包名:" + newPackageName);
|
|
}
|
|
}
|
|
}
|
|
|
|
var currentTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
|
|
var oldDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentTargetGroup);
|
|
_oldDefines = oldDefines;
|
|
// 判断是否有需要追加的宏
|
|
if (appendDefineList.Count > 0)
|
|
{
|
|
var newDefineList = new List<string>();
|
|
newDefineList.AddRange(oldDefines.Split(';'));
|
|
for (var i = 0; i < newDefineList.Count; i++)
|
|
{
|
|
var define = newDefineList[i];
|
|
if (string.IsNullOrEmpty(define))
|
|
continue;
|
|
if (define == "USE_YOO")
|
|
{
|
|
hasYooAsset = true;
|
|
}
|
|
|
|
if (appendDefineList.Contains(define))
|
|
appendDefineList.Remove(define);
|
|
}
|
|
|
|
newDefineList.AddRange(appendDefineList);
|
|
PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTargetGroup, string.Join(";", newDefineList));
|
|
}
|
|
//只有安卓平台下修改
|
|
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android && !string.IsNullOrEmpty(newPackageName))
|
|
{
|
|
PlayerSettings.applicationIdentifier = newPackageName;
|
|
//PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, newPackageName);
|
|
}
|
|
|
|
// 现已修改为使用YooAsset
|
|
}
|
|
|
|
private static void _RevertScriptDefines()
|
|
{
|
|
var currentTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
|
|
PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTargetGroup, _oldDefines);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批处理构建Resources和Player
|
|
/// jenkins打包新包时使用该方法
|
|
/// </summary>
|
|
public static void BuildContentAndPlayer()
|
|
{
|
|
try
|
|
{
|
|
_InitScriptDefines();
|
|
|
|
var enableHotUpdate = YooAssetBuildBase.Init();
|
|
|
|
// 写入版本信息到Resources/LocalInfo.json
|
|
var info = new UpdateLocalInfo();
|
|
info.workSpace = YooAssetBuildBase.workSpace;
|
|
info.version = YooAssetBuildBase.appVersion;
|
|
info.serverUrl = "http://132.232.113.98";
|
|
info.buildTicks = DateTime.Now.Ticks;
|
|
PackDataInst.CreateResourceJson(info);
|
|
|
|
// 写入打包信息到Yoo_Build_Cache
|
|
ExtBuildInfoHelper.CreateFirstExtBuildInfo(info.buildTicks, info.workSpace, info.version);
|
|
|
|
BuildLauncher.SetEnableHotUpdate(enableHotUpdate);
|
|
if (enableHotUpdate)
|
|
{
|
|
BuildLauncher.BuildHotUpdateDlls();
|
|
}
|
|
|
|
YooAssetBuildBase.BuildContentAndPlayer();
|
|
|
|
// 还原宏
|
|
_RevertScriptDefines();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
//Build Exception用于在日志文件中定位此次打包的异常
|
|
Debug.LogError($"[Build Exception]:{e.Message} \r\n {e.StackTrace}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批处理增量更新资源
|
|
/// jenkins打包增量更新时使用该方法
|
|
/// </summary>
|
|
public static void UpdatePreviousBuild()
|
|
{
|
|
try
|
|
{
|
|
_InitScriptDefines();
|
|
|
|
var enableHotUpdate = YooAssetBuildBase.Init();
|
|
if (enableHotUpdate)
|
|
BuildLauncher.BuildHotUpdateDlls();
|
|
|
|
// 写入打包信息到Yoo_Build_Cache
|
|
ExtBuildInfoHelper.UpdateExtBuildInfo(YooAssetBuildBase.workSpace, YooAssetBuildBase.appVersion);
|
|
|
|
YooAssetBuildBase.BuildOnlyResUpdate();
|
|
|
|
_RevertScriptDefines();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
//Build Exception用于在日志文件中定位此次打包的异常
|
|
Debug.LogError($"[Build Exception]:{e.Message} \r\n {e.StackTrace}");
|
|
throw;
|
|
}
|
|
}
|
|
}
|