refactor: remove support for unity 2019-2021. refactor generator.

This commit is contained in:
walon
2026-10-08 21:01:52 +08:00
parent d17a727fac
commit 44057b0107
246 changed files with 3676 additions and 9330 deletions
@@ -1,22 +1,30 @@
using HybridCLR.Editor.UnityBinFileReader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
// Copyright 2026 Code Philosophy
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.IO;
using UnityEditor.Android;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.UnityLinker;
using UnityEngine;
using UnityFS;
#if !UNITY_2023_1_OR_NEWER
using UnityEditor.Il2Cpp;
#endif
namespace HybridCLR.Editor.BuildProcessors
namespace HybridCLR.BuildProcessors
{
public class PatchScriptingAssemblyList :
#if UNITY_ANDROID
@@ -25,9 +33,6 @@ namespace HybridCLR.Editor.BuildProcessors
UnityEditor.OpenHarmony.IPostGenerateOpenHarmonyProject,
#endif
IPostprocessBuildWithReport
#if !UNITY_2021_1_OR_NEWER && UNITY_WEBGL
, IIl2CppProcessor
#endif
#if UNITY_PS5
, IUnityLinkerProcessor
@@ -38,9 +43,9 @@ namespace HybridCLR.Editor.BuildProcessors
public void OnPostGenerateGradleAndroidProject(string path)
{
// 如果直接打包apk,没有机会在PostprocessBuild中修改ScriptingAssemblies.json。
// 因此需要在这个时机处理
// Unity有bug,偶然情况下会传入apk的路径,导致替换失败
// When building apk directly there is no chance to patch ScriptingAssemblies.json in PostprocessBuild.
// Therefore patch it at this stage
// Unity bug: sometimes an apk path is passed in and replacement fails
if (Directory.Exists(path))
{
PathScriptingAssembilesFile(path);
@@ -62,8 +67,8 @@ namespace HybridCLR.Editor.BuildProcessors
public void OnPostprocessBuild(BuildReport report)
{
// 如果target为Android,由于已经在OnPostGenerateGradelAndroidProject中处理过,
// 这里不再重复处理
// For Android targets this was already handled in OnPostGenerateGradleAndroidProject,
// so skip duplicate processing here
#if !UNITY_ANDROID && !UNITY_WEBGL && !UNITY_OPENHARMONY
PathScriptingAssembilesFile(report.summary.outputPath);
#endif
@@ -71,7 +76,7 @@ namespace HybridCLR.Editor.BuildProcessors
#if UNITY_PS5
/// <summary>
/// 打包模式如果是 Package 需要在这个阶段提前处理 .json , PC Hosted 和 GP5 模式不受影响
/// For Package build mode, patch .json at this stage; PC Hosted and GP5 modes are unaffected
/// </summary>
public string GenerateAdditionalLinkXmlFile(UnityEditor.Build.Reporting.BuildReport report, UnityEditor.UnityLinker.UnityLinkerBuildPipelineData data)
@@ -94,26 +99,24 @@ namespace HybridCLR.Editor.BuildProcessors
path = Path.GetDirectoryName(path);
Debug.Log($"[PatchScriptingAssemblyList] get path parent:{path}");
}
#if UNITY_2020_1_OR_NEWER
AddHotFixAssembliesToScriptingAssembliesJson(path);
#else
AddHotFixAssembliesToBinFile(path);
#endif
}
private const string scriptingAssembliesJsonFile = "ScriptingAssemblies.json";
private void AddHotFixAssembliesToScriptingAssembliesJson(string path)
{
Debug.Log($"[PatchScriptingAssemblyList]. path:{path}");
/*
* ScriptingAssemblies.json 文件中记录了所有的dll名称,此列表在游戏启动时自动加载,
* 不在此列表中的dll在资源反序列化时无法被找到其类型
* 因此 OnFilterAssemblies 中移除的条目需要再加回来
* ScriptingAssemblies.json lists all dll names that are loaded automatically at startup;
* dlls missing from this list cannot resolve types during asset deserialization
* therefore entries removed by OnFilterAssemblies must be added back
*/
string[] jsonFiles = Directory.GetFiles(path, SettingsUtil.ScriptingAssembliesJsonFile, SearchOption.AllDirectories);
string[] jsonFiles = Directory.GetFiles(path, scriptingAssembliesJsonFile, SearchOption.AllDirectories);
if (jsonFiles.Length == 0)
{
Debug.LogWarning($"can not find file {SettingsUtil.ScriptingAssembliesJsonFile}");
Debug.LogWarning($"can not find file {scriptingAssembliesJsonFile}");
return;
}
@@ -125,65 +128,5 @@ namespace HybridCLR.Editor.BuildProcessors
patcher.Save(file);
}
}
private void AddHotFixAssembliesToBinFile(string path)
{
#if UNITY_STANDALONE_OSX
path = Path.GetDirectoryName(path);
#endif
if (AddHotFixAssembliesToGlobalgamemanagers(path))
{
return;
}
if (AddHotFixAssembliesTodataunity3d(path))
{
return;
}
Debug.LogError($"[PatchScriptingAssemblyList] can not find file '{SettingsUtil.GlobalgamemanagersBinFile}' or '{SettingsUtil.Dataunity3dBinFile}' in '{path}'");
}
private bool AddHotFixAssembliesToGlobalgamemanagers(string path)
{
string[] binFiles = Directory.GetFiles(path, SettingsUtil.GlobalgamemanagersBinFile, SearchOption.AllDirectories);
if (binFiles.Length == 0)
{
return false;
}
foreach (string binPath in binFiles)
{
var binFile = new UnityBinFile();
binFile.Load(binPath);
binFile.AddScriptingAssemblies(SettingsUtil.HotUpdateAssemblyFilesIncludePreserved);
binFile.Save(binPath);
Debug.Log($"[PatchScriptingAssemblyList] patch {binPath}");
}
return true;
}
private bool AddHotFixAssembliesTodataunity3d(string path)
{
string[] binFiles = Directory.GetFiles(path, SettingsUtil.Dataunity3dBinFile, SearchOption.AllDirectories);
if (binFiles.Length == 0)
{
return false;
}
foreach (string binPath in binFiles)
{
var patcher = new Dataunity3dPatcher();
patcher.ApplyPatch(binPath, SettingsUtil.HotUpdateAssemblyFilesIncludePreserved);
Debug.Log($"[PatchScriptingAssemblyList] patch {binPath}");
}
return true;
}
#if UNITY_WEBGL && !UNITY_2022_3_OR_NEWER
public void OnBeforeConvertRun(BuildReport report, Il2CppBuildPipelineData data)
{
PathScriptingAssembilesFile($"{SettingsUtil.ProjectDir}/Temp/StagingArea/Data");
}
#endif
}
}