NLDClient-yudde/ProjectNLD/Assets/Editor/Common/BuildLauncher.cs

280 lines
9.0 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using HybridCLR.Editor.Commands;
2024-01-22 18:26:03 +08:00
using HybridCLR.Editor.Installer;
using PhxhSDK.AOT.VersionUpdate;
2023-10-19 15:00:19 +08:00
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
2023-11-23 14:41:25 +08:00
using UnityEngine.Windows;
2023-10-19 15:00:19 +08:00
using BuildTarget = UnityEditor.BuildTarget;
using Debug = UnityEngine.Debug;
class BuildLauncher
{
private const string HybirdCLRGenerateDir = "/HybridCLRGenerate";
private const string HybirdCLRGenerateLinkPath = "Assets/HybridCLRGenerate/link.xml";
2023-11-23 14:44:42 +08:00
2023-11-07 19:07:35 +08:00
private const string GameLauncherSceneName = "Assets/Scenes/GameLauncher.unity";
2023-11-23 14:44:42 +08:00
2023-11-08 13:53:45 +08:00
private const string RemoteUser = "root";
private const string RemoteHost = "132.232.113.98";
private const string RemotePath = "/var/www/html/nld/{0}/{1}/{2}";
2023-11-23 14:44:42 +08:00
2023-11-07 19:07:35 +08:00
public static void BuildHotUpdateDlls()
2023-10-19 15:00:19 +08:00
{
2024-01-22 18:26:03 +08:00
var controller = new InstallerController();
if(!controller.HasInstalledHybridCLR())
controller.InstallDefaultHybridCLR();
2023-11-23 14:44:42 +08:00
// VersionControlSystem.Checkout(HybirdCLRGenerateDir);
BuildLauncherBase.SetFileReadable(Application.dataPath + HybirdCLRGenerateDir);
2023-11-07 19:07:35 +08:00
PrebuildCommand.GenerateAll();
HotUpadeEditorHelper.CopyHotUpdateDll();
HotUpadeEditorHelper.CopyMetaDataDll();
2023-10-19 15:00:19 +08:00
}
2023-11-07 19:07:35 +08:00
public static void SetEnableHotUpdate(bool isEnable)
2023-10-19 15:00:19 +08:00
{
2023-11-07 19:07:35 +08:00
Debug.Log($"是否开启热更新:{isEnable}");
var gameLauncherScene = EditorSceneManager.OpenScene(GameLauncherSceneName, OpenSceneMode.Additive);
if (!gameLauncherScene.IsValid())
2023-10-19 15:00:19 +08:00
{
2023-11-07 19:07:35 +08:00
Debug.LogError($"无法加载场景{GameLauncherSceneName}");
return;
2023-10-19 15:00:19 +08:00
}
2023-11-07 19:07:35 +08:00
var gameLauncher = UnityEngine.Object.FindObjectOfType<GameLauncher>();
if (gameLauncher == null)
2023-10-19 15:00:19 +08:00
{
2023-11-07 19:07:35 +08:00
Debug.LogError("找不到GameLauncher");
return;
2023-10-19 15:00:19 +08:00
}
2023-11-07 19:07:35 +08:00
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);
}
2023-10-19 15:00:19 +08:00
}
2024-02-27 15:07:59 +08:00
#if USE_ADDRESSABLES
2023-11-23 14:44:42 +08:00
public static bool BuildAddressables(BuildTarget buildTarget,
string profileName,
string version,
2023-10-19 15:00:19 +08:00
string workSpace,
bool ignoreHotUpdate,
bool needClearCached = false,
List<AddressableAssetGroup> groups = null)
{
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildPipeline.GetBuildTargetGroup(buildTarget), buildTarget);
2023-11-28 13:00:46 +08:00
SetEnableHotUpdate(!ignoreHotUpdate);
if (!ignoreHotUpdate)
{
BuildHotUpdateDlls();
}
2024-02-22 16:56:16 +08:00
#if USE_YOO
return BuildLauncherBase.BuildYooAsset(buildTarget, profileName, version, workSpace, RemoteUser, RemoteHost, RemotePath);
#else
2023-11-07 19:07:35 +08:00
return BuildLauncherBase.BuildAddressables(buildTarget, profileName, version, workSpace, needClearCached,
2023-11-08 13:53:45 +08:00
groups, RemoteUser, RemoteHost, RemotePath);
2024-02-22 16:56:16 +08:00
#endif
2023-11-08 13:53:45 +08:00
2023-10-19 15:00:19 +08:00
}
2024-02-27 15:07:59 +08:00
#endif
2023-11-23 14:44:42 +08:00
public static void UpdatePreviousBuild(string profileName,
bool ignoreHotUpdate,
BuildTarget buildTarget,
string workSpace)
2023-10-19 15:00:19 +08:00
{
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildPipeline.GetBuildTargetGroup(buildTarget), buildTarget);
if (!ignoreHotUpdate)
{
BuildHotUpdateDlls();
}
2024-02-27 15:07:59 +08:00
#if USE_ADDRESSABLES
2023-11-08 13:53:45 +08:00
BuildLauncherBase.UpdatePreviousBuild(profileName, buildTarget, workSpace, RemoteUser, RemoteHost, RemotePath);
2024-02-27 15:07:59 +08:00
#endif
2023-10-19 15:00:19 +08:00
}
2023-11-23 14:44:42 +08:00
public static void BuildAddressablesAndPlayer(BuildTarget buildTarget,
string profileName,
string version,
2023-10-19 15:00:19 +08:00
string workSpace,
bool ignoreHotUpdate,
2024-01-22 17:51:43 +08:00
bool needClearCached = false,
bool isDev = false)
2023-10-19 15:00:19 +08:00
{
2024-02-27 15:07:59 +08:00
#if USE_ADDRESSABLES
2023-11-08 17:33:46 +08:00
if (BuildAddressables(buildTarget, profileName, version, workSpace, ignoreHotUpdate, needClearCached))
{
2024-01-22 17:51:43 +08:00
BuildLauncherBase.BuildPlayer(buildTarget,isDev);
2023-11-08 17:33:46 +08:00
}
2024-02-27 15:07:59 +08:00
#endif
2023-10-19 15:00:19 +08:00
}
2024-01-22 17:51:43 +08:00
public static void BuildPlayer(BuildTarget buildTarget,bool isDev = false)
2023-10-19 15:00:19 +08:00
{
2024-01-22 17:51:43 +08:00
BuildLauncherBase.BuildPlayer(buildTarget,isDev);
2023-10-19 15:00:19 +08:00
}
}
internal class BatchBuild
{
/// <summary>
/// 批处理构建Addressables
/// </summary>
public static void BuildAddressables()
{
2023-11-07 19:07:35 +08:00
var enableHotUpdate = BatchBuildBase.Init();
2023-11-23 14:44:42 +08:00
if (enableHotUpdate)
2023-10-19 15:00:19 +08:00
BuildLauncher.BuildHotUpdateDlls();
2023-11-07 19:07:35 +08:00
BatchBuildBase.BuildAddressables();
2023-10-19 15:00:19 +08:00
}
private static string _oldDefines;
private static bool _hasYooAsset;
private static void _InitScriptDefines()
2023-10-19 15:00:19 +08:00
{
var hasYooAsset = false;
// 判断本地是否有define_list.txt文件
var defineListPath = Application.dataPath + "/../define_list.txt";
var appendDefineList = new List<string>();
if (File.Exists(defineListPath))
2023-11-23 14:41:25 +08:00
{
var defineList = System.IO.File.ReadAllLines(defineListPath);
foreach (var define in defineList)
2023-11-23 14:41:25 +08:00
{
if (string.IsNullOrEmpty(define))
continue;
appendDefineList.Add(define);
Debug.Log("追加宏定义:" + define);
if (define == "USE_YOO")
2024-01-24 13:18:47 +08:00
{
hasYooAsset = true;
2024-01-24 13:18:47 +08:00
}
2023-11-23 14:41:25 +08:00
}
}
2024-01-24 13:18:47 +08:00
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++)
2024-01-24 13:18:47 +08:00
{
var define = newDefineList[i];
if (string.IsNullOrEmpty(define))
continue;
if (define == "USE_YOO")
2024-01-24 13:18:47 +08:00
{
hasYooAsset = true;
2024-01-24 13:18:47 +08:00
}
if (appendDefineList.Contains(define))
appendDefineList.Remove(define);
2024-01-24 13:18:47 +08:00
}
newDefineList.AddRange(appendDefineList);
PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTargetGroup, string.Join(";", newDefineList));
}
2024-02-27 15:56:29 +08:00
// 现已修改为使用YooAsset
_hasYooAsset = hasYooAsset;
2024-02-27 15:56:29 +08:00
_hasYooAsset = true;
}
private static void _RevertScriptDefines()
{
var currentTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTargetGroup, _oldDefines);
}
/// <summary>
/// 批处理构建Addressables和Player
/// jenkins打包新包时使用该方法
/// </summary>
public static void BuildContentAndPlayer()
{
try
{
_InitScriptDefines();
2024-01-24 13:18:47 +08:00
var enableHotUpdate = BatchBuildBase.Init();
if (_hasYooAsset)
{
// 写入版本信息到Resources/LocalInfo.json
var info = new UpdateLocalInfo();
info.workSpace = BatchBuildBase.workSpace;
info.version = BatchBuildBase.appVersion;
info.buildCount = 1;
info.serverUrl = "http://132.232.113.98";
PackDataInst.CreateResourceJson(info);
2024-02-26 14:07:57 +08:00
// 写入打包次数
PackDataInst.CreateBuildCount(info.workSpace, info.version, info.buildCount);
}
2024-01-24 13:18:47 +08:00
BuildLauncher.SetEnableHotUpdate(enableHotUpdate);
if (enableHotUpdate)
2023-11-23 14:41:25 +08:00
{
2024-01-24 13:18:47 +08:00
BuildLauncher.BuildHotUpdateDlls();
2023-11-23 14:41:25 +08:00
}
2024-01-24 13:18:47 +08:00
BatchBuildBase.BuildContentAndPlayer();
// 还原宏
_RevertScriptDefines();
2023-11-23 14:41:25 +08:00
}
2024-01-24 13:18:47 +08:00
catch (Exception e)
{
2024-01-24 15:51:08 +08:00
//Build Exception用于在日志文件中定位此次打包的异常
2024-01-24 13:18:47 +08:00
Debug.LogError($"[Build Exception]:{e.Message} \r\n {e.StackTrace}");
throw;
}
2023-10-19 15:00:19 +08:00
}
/// <summary>
/// 批处理增量更新资源
/// jenkins打包增量更新时使用该方法
2023-10-19 15:00:19 +08:00
/// </summary>
public static void UpdatePreviousBuild()
{
2024-01-24 15:51:08 +08:00
try
{
_InitScriptDefines();
2024-01-24 15:51:08 +08:00
var enableHotUpdate = BatchBuildBase.Init();
if (enableHotUpdate)
BuildLauncher.BuildHotUpdateDlls();
2024-02-22 19:01:55 +08:00
BatchBuildBase.BuildOnlyResUpdate();
_RevertScriptDefines();
2024-01-24 15:51:08 +08:00
}
catch (Exception e)
{
//Build Exception用于在日志文件中定位此次打包的异常
Debug.LogError($"[Build Exception]:{e.Message} \r\n {e.StackTrace}");
throw;
}
2023-10-19 15:00:19 +08:00
}
2023-11-23 14:44:42 +08:00
}