From 79467bbf13750b6dc748e0f38f55896d7e256acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Wed, 12 Mar 2025 18:32:28 +0800 Subject: [PATCH] update editor assembly --- .gitignore | 6 +- .../YooAsset/Editor/Assembly/MacroDefine.cs | 17 ++ .../Editor/Assembly/MacroDefine.cs.meta | 11 + .../Editor/Assembly/MacroProcessor.cs | 96 ++++++++ ...ocessor.cs.meta => MacroProcessor.cs.meta} | 0 .../Editor/Assembly/MacrosProcessor.cs | 214 ------------------ .../YooAsset/Editor/Assembly/RspGenerator.cs | 115 ++++++++++ .../Editor/Assembly/RspGenerator.cs.meta | 11 + 8 files changed, 253 insertions(+), 217 deletions(-) create mode 100644 Assets/YooAsset/Editor/Assembly/MacroDefine.cs create mode 100644 Assets/YooAsset/Editor/Assembly/MacroDefine.cs.meta create mode 100644 Assets/YooAsset/Editor/Assembly/MacroProcessor.cs rename Assets/YooAsset/Editor/Assembly/{MacrosProcessor.cs.meta => MacroProcessor.cs.meta} (100%) delete mode 100644 Assets/YooAsset/Editor/Assembly/MacrosProcessor.cs create mode 100644 Assets/YooAsset/Editor/Assembly/RspGenerator.cs create mode 100644 Assets/YooAsset/Editor/Assembly/RspGenerator.cs.meta diff --git a/.gitignore b/.gitignore index 9764a0ca..65247de6 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,8 @@ /Assets/StreamingAssets.meta /Assets/Samples /Assets/Samples.meta +/Assets/csc.rsp +/Assets/csc.rsp.meta /UserSettings @@ -72,6 +74,4 @@ sysinfo.txt # Crashlytics generated file crashlytics-build.properties -*.vsconfig -Assets/csc.rsp -Assets/csc.rsp.meta +*.vsconfig \ No newline at end of file diff --git a/Assets/YooAsset/Editor/Assembly/MacroDefine.cs b/Assets/YooAsset/Editor/Assembly/MacroDefine.cs new file mode 100644 index 00000000..a647b43d --- /dev/null +++ b/Assets/YooAsset/Editor/Assembly/MacroDefine.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace YooAsset.Editor +{ + public class MacroDefine + { + /// + /// YooAsset版本宏定义 + /// + public static readonly List Macros = new List() + { + "YOO_ASSET_2", + "YOO_ASSET_2_3", + "YOO_ASSET_2_3_OR_NEWER", + }; + } +} diff --git a/Assets/YooAsset/Editor/Assembly/MacroDefine.cs.meta b/Assets/YooAsset/Editor/Assembly/MacroDefine.cs.meta new file mode 100644 index 00000000..4224cd37 --- /dev/null +++ b/Assets/YooAsset/Editor/Assembly/MacroDefine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a61e5c2ca04aab647b1ed0492086aa8f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Editor/Assembly/MacroProcessor.cs b/Assets/YooAsset/Editor/Assembly/MacroProcessor.cs new file mode 100644 index 00000000..265fc1e6 --- /dev/null +++ b/Assets/YooAsset/Editor/Assembly/MacroProcessor.cs @@ -0,0 +1,96 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Xml; +using UnityEditor; + +namespace YooAsset.Editor +{ + [InitializeOnLoad] + public class MacroProcessor : AssetPostprocessor + { + static string OnGeneratedCSProject(string path, string content) + { + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(content); + + if (IsCSProjectReferenced(xmlDoc.DocumentElement) == false) + return content; + + if (ProcessDefineConstants(xmlDoc.DocumentElement) == false) + return content; + + // 将修改后的XML结构重新输出为文本 + var stringWriter = new StringWriter(); + var writerSettings = new XmlWriterSettings(); + writerSettings.Indent = true; + var xmlWriter = XmlWriter.Create(stringWriter, writerSettings); + xmlDoc.WriteTo(xmlWriter); + xmlWriter.Flush(); + return stringWriter.ToString(); + } + + /// + /// 处理宏定义 + /// + private static bool ProcessDefineConstants(XmlElement element) + { + if (element == null) + return false; + + bool processed = false; + foreach (XmlNode node in element.ChildNodes) + { + if (node.Name != "PropertyGroup") + continue; + + foreach (XmlNode childNode in node.ChildNodes) + { + if (childNode.Name != "DefineConstants") + continue; + + string[] defines = childNode.InnerText.Split(';'); + HashSet hashSets = new HashSet(defines); + foreach (string yooMacro in MacroDefine.Macros) + { + string tmpMacro = yooMacro.Trim(); + if (hashSets.Contains(tmpMacro) == false) + hashSets.Add(tmpMacro); + } + childNode.InnerText = string.Join(";", hashSets.ToArray()); + processed = true; + } + } + + return processed; + } + + /// + /// 检测工程是否引用了YooAsset + /// + private static bool IsCSProjectReferenced(XmlElement element) + { + if (element == null) + return false; + + foreach (XmlNode node in element.ChildNodes) + { + if (node.Name != "ItemGroup") + continue; + + foreach (XmlNode childNode in node.ChildNodes) + { + if (childNode.Name != "Reference" && childNode.Name != "ProjectReference") + continue; + + string include = childNode.Attributes["Include"].Value; + if (include.Contains("YooAsset")) + return true; + } + } + + return false; + } + } +} diff --git a/Assets/YooAsset/Editor/Assembly/MacrosProcessor.cs.meta b/Assets/YooAsset/Editor/Assembly/MacroProcessor.cs.meta similarity index 100% rename from Assets/YooAsset/Editor/Assembly/MacrosProcessor.cs.meta rename to Assets/YooAsset/Editor/Assembly/MacroProcessor.cs.meta diff --git a/Assets/YooAsset/Editor/Assembly/MacrosProcessor.cs b/Assets/YooAsset/Editor/Assembly/MacrosProcessor.cs deleted file mode 100644 index 17e24a28..00000000 --- a/Assets/YooAsset/Editor/Assembly/MacrosProcessor.cs +++ /dev/null @@ -1,214 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Xml; -using UnityEditor; -using UnityEditor.PackageManager; -using UnityEngine; -using static UnityEditor.PlayerSettings; - -namespace YooAsset.Editor -{ - [InitializeOnLoad] - public class MacrosProcessor : AssetPostprocessor - { - // csc.rsp 文件路径 - private static string RspFilePath => Path.Combine(Application.dataPath, "csc.rsp"); - /// - /// YooAsset版本宏定义 - /// - private static readonly List YooAssetMacros = new List() - { - "YOO_ASSET_2", - "YOO_ASSET_2_3", - "YOO_ASSET_2_3_OR_NEWER", - }; - - static MacrosProcessor() - { - //添加到全局宏定义 - UpdateRspFile(YooAssetMacros); - } - - static string OnGeneratedCSProject(string path, string content) - { - XmlDocument xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(content); - - if (IsCSProjectReferenced(xmlDoc.DocumentElement) == false) - return content; - - if (ProcessDefineConstants(xmlDoc.DocumentElement) == false) - return content; - - // 将修改后的XML结构重新输出为文本 - var stringWriter = new StringWriter(); - var writerSettings = new XmlWriterSettings(); - writerSettings.Indent = true; - var xmlWriter = XmlWriter.Create(stringWriter, writerSettings); - xmlDoc.WriteTo(xmlWriter); - xmlWriter.Flush(); - return stringWriter.ToString(); - } - - /// - /// 处理宏定义 - /// - private static bool ProcessDefineConstants(XmlElement element) - { - if (element == null) - return false; - - bool processed = false; - foreach (XmlNode node in element.ChildNodes) - { - if (node.Name != "PropertyGroup") - continue; - - foreach (XmlNode childNode in node.ChildNodes) - { - if (childNode.Name != "DefineConstants") - continue; - - string[] defines = childNode.InnerText.Split(';'); - HashSet hashSets = new HashSet(defines); - foreach (string yooMacro in YooAssetMacros) - { - string tmpMacro = yooMacro.Trim(); - if (hashSets.Contains(tmpMacro) == false) - hashSets.Add(tmpMacro); - } - childNode.InnerText = string.Join(";", hashSets.ToArray()); - processed = true; - } - } - - return processed; - } - - /// - /// 检测工程是否引用了YooAsset - /// - private static bool IsCSProjectReferenced(XmlElement element) - { - if (element == null) - return false; - - foreach (XmlNode node in element.ChildNodes) - { - if (node.Name != "ItemGroup") - continue; - - foreach (XmlNode childNode in node.ChildNodes) - { - if (childNode.Name != "Reference" && childNode.Name != "ProjectReference") - continue; - - string include = childNode.Attributes["Include"].Value; - if (include.Contains("YooAsset")) - return true; - } - } - - return false; - } - /// - /// 更新csc.rsp文件 - /// - /// - /// - private static void UpdateRspFile(List macrosToAdd = null, List macrosToRemove = null) - { - HashSet existingDefines = new HashSet(); - List otherLines = new List(); - // 1. 读取现有内容 - ReadRspFile(ref existingDefines, ref otherLines); - // 2. 添加新宏 - if (macrosToAdd != null && macrosToAdd.Count > 0) - { - macrosToAdd.ForEach(x => - { - if (existingDefines.Contains(x) == false) - existingDefines.Add(x); - }); - } - // 3. 移除指定宏 - if (macrosToRemove != null && macrosToRemove.Count > 0) - { - macrosToRemove.ForEach(x => - { - existingDefines.Remove(x); - }); - } - // 4. 重新生成内容 - WriteRspFile(existingDefines, otherLines); - // 5. 刷新AssetDatabase - AssetDatabase.Refresh(); - EditorUtility.RequestScriptReload(); - } - /// - /// 读取csc.rsp文件,返回宏定义和其他行 - /// - /// - /// - private static void ReadRspFile(ref HashSet defines, ref List others) - { - if (defines == null) - { - defines = new HashSet(); - } - - if (others == null) - { - others = new List(); - } - - if (File.Exists(RspFilePath) == false) - { - return; - } - - foreach (string line in File.ReadAllLines(RspFilePath)) - { - if (line.StartsWith("-define:")) - { - string[] parts = line.Split(new[] { ':' }, 2); - if (parts.Length == 2) - { - defines.Add(parts[1].Trim()); - } - } - else - { - others.Add(line); - } - } - } - /// - /// 重写入出csc.rsp文件 - /// - /// - /// - private static void WriteRspFile(HashSet defines, List others) - { - - StringBuilder sb = new StringBuilder(); - if (others != null && others.Count > 0) - { - others.ForEach(o => sb.AppendLine(o)); - } - - if (defines != null && defines.Count > 0) - { - foreach (string define in defines) - { - sb.AppendLine($"-define:{define}"); - } - } - - File.WriteAllText(RspFilePath, sb.ToString()); - } - - } -} diff --git a/Assets/YooAsset/Editor/Assembly/RspGenerator.cs b/Assets/YooAsset/Editor/Assembly/RspGenerator.cs new file mode 100644 index 00000000..7b199f04 --- /dev/null +++ b/Assets/YooAsset/Editor/Assembly/RspGenerator.cs @@ -0,0 +1,115 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Xml; +using UnityEditor; +using UnityEngine; + +#if YOO_ASSET_EXPERIMENT +namespace YooAsset.Editor.Experiment +{ + [InitializeOnLoad] + public class RspGenerator + { + // csc.rsp文件路径 + private static string RspFilePath => Path.Combine(Application.dataPath, "csc.rsp"); + + static RspGenerator() + { + UpdateRspFile(MacroDefine.Macros, null); + } + + /// + /// 更新csc.rsp文件 + /// + private static void UpdateRspFile(List addMacros, List removeMacros) + { + var existingDefines = new HashSet(); + var otherLines = new List(); + + // 1. 读取现有内容 + ReadRspFile(existingDefines, otherLines); + + // 2. 添加新宏 + if (addMacros != null && addMacros.Count > 0) + { + addMacros.ForEach(x => + { + if (existingDefines.Contains(x) == false) + existingDefines.Add(x); + }); + } + + // 3. 移除指定宏 + if (removeMacros != null && removeMacros.Count > 0) + { + removeMacros.ForEach(x => + { + existingDefines.Remove(x); + }); + } + + // 4. 重新生成内容 + WriteRspFile(existingDefines, otherLines); + + // 5. 刷新AssetDatabase + AssetDatabase.Refresh(); + EditorUtility.RequestScriptReload(); + } + + /// + /// 读取csc.rsp文件,返回宏定义和其他行 + /// + private static void ReadRspFile(HashSet defines, List others) + { + if (defines == null) + defines = new HashSet(); + + if (others == null) + others = new List(); + + if (File.Exists(RspFilePath) == false) + return; + + foreach (string line in File.ReadAllLines(RspFilePath)) + { + if (line.StartsWith("-define:")) + { + string[] parts = line.Split(new[] { ':' }, 2); + if (parts.Length == 2) + { + defines.Add(parts[1].Trim()); + } + } + else + { + others.Add(line); + } + } + } + + /// + /// 重新写入csc.rsp文件 + /// + private static void WriteRspFile(HashSet defines, List others) + { + StringBuilder sb = new StringBuilder(); + if (others != null && others.Count > 0) + { + others.ForEach(o => sb.AppendLine(o)); + } + + if (defines != null && defines.Count > 0) + { + foreach (string define in defines) + { + sb.AppendLine($"-define:{define}"); + } + } + + File.WriteAllText(RspFilePath, sb.ToString()); + } + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Editor/Assembly/RspGenerator.cs.meta b/Assets/YooAsset/Editor/Assembly/RspGenerator.cs.meta new file mode 100644 index 00000000..dc436a4a --- /dev/null +++ b/Assets/YooAsset/Editor/Assembly/RspGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c2662e1d33b1eea469695b68d18b1739 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: