update asmdef

YooAsset版本宏定义
pull/499/head
何冠峰 2025-03-08 14:34:41 +08:00
parent 10cce48e71
commit cdbedee705
2 changed files with 48 additions and 59 deletions

View File

@ -3,87 +3,75 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Xml; using System.Xml;
using UnityEditor; using UnityEditor;
using UnityEngine;
namespace YooAsset.Editor namespace YooAsset.Editor
{ {
[InitializeOnLoad] [InitializeOnLoad]
class YooMacrosProcessor : AssetPostprocessor public class MacrosProcessor : AssetPostprocessor
{ {
static readonly List<string> YooAssertMacros = new List<string>() static MacrosProcessor()
{ {
"YOO_VERSION_2", }
"YOO_VERSION_2_3_4",
"YOO_VERSION_2_3_4_OR_NEWER", /// <summary>
/// YooAsset版本宏定义
/// </summary>
private static readonly List<string> YooAssetMacros = new List<string>()
{
"YOO_ASSET_2",
"YOO_ASSET_2_3",
"YOO_ASSET_2_3_OR_NEWER",
}; };
static string OnGeneratedCSProject(string path, string content) static string OnGeneratedCSProject(string path, string content)
{ {
XmlDocument xmlDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(content); xmlDoc.LoadXml(content);
if (!IsCSProjectReferenced(xmlDoc.DocumentElement)) if (IsCSProjectReferenced(xmlDoc.DocumentElement) == false)
{
return content; return content;
}
if (!ProcessDefineConstants(xmlDoc.DocumentElement, YooAssertMacros)) if (ProcessDefineConstants(xmlDoc.DocumentElement) == false)
{
return content; 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();
} }
StringWriter sw = new StringWriter(); /// <summary>
XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings() /// 处理宏定义
{ /// </summary>
Indent = true, private static bool ProcessDefineConstants(XmlElement element)
});
xmlDoc.WriteTo(writer);
writer.Flush();
return sw.ToString();
}
static bool ProcessDefineConstants(XmlElement element, List<string> macros)
{ {
if (element == null) if (element == null)
{
return false; return false;
}
bool processed = false; bool processed = false;
if (macros == null || macros.Count == 0)
{
return processed;
}
foreach (XmlNode node in element.ChildNodes) foreach (XmlNode node in element.ChildNodes)
{ {
if (node.Name != "PropertyGroup") if (node.Name != "PropertyGroup")
{
continue; continue;
}
foreach (XmlNode childNode in node.ChildNodes) foreach (XmlNode childNode in node.ChildNodes)
{ {
if (childNode.Name != "DefineConstants") if (childNode.Name != "DefineConstants")
{
continue; continue;
}
string[] defines = childNode.InnerText.Split(';'); string[] defines = childNode.InnerText.Split(';');
HashSet<string> hs = new HashSet<string>(defines); HashSet<string> hashSets = new HashSet<string>(defines);
foreach (string yooMacro in YooAssetMacros)
string tmpMacro = string.Empty;
foreach (string macro in macros)
{ {
tmpMacro = macro.Trim(); string tmpMacro = yooMacro.Trim();
if (string.IsNullOrEmpty(tmpMacro)) if (hashSets.Contains(tmpMacro) == false)
continue; hashSets.Add(tmpMacro);
//加入YooAsset定义的宏
hs.Add(tmpMacro);
} }
//更新节点InnerText childNode.InnerText = string.Join(";", hashSets.ToArray());
childNode.InnerText = string.Join(";", hs.ToArray());
processed = true; processed = true;
} }
} }
@ -91,36 +79,31 @@ namespace YooAsset.Editor
return processed; return processed;
} }
static bool IsCSProjectReferenced(XmlElement element) /// <summary>
/// 检测工程是否引用了YooAsset
/// </summary>
private static bool IsCSProjectReferenced(XmlElement element)
{ {
if (element == null) if (element == null)
{
return false; return false;
}
foreach (XmlNode node in element.ChildNodes) foreach (XmlNode node in element.ChildNodes)
{ {
if (node.Name != "ItemGroup") if (node.Name != "ItemGroup")
{
continue; continue;
}
foreach (XmlNode childNode in node.ChildNodes) foreach (XmlNode childNode in node.ChildNodes)
{ {
if (childNode.Name != "Reference" && childNode.Name != "ProjectReference") if (childNode.Name != "Reference" && childNode.Name != "ProjectReference")
{
continue; continue;
}
//工程引用了YooAsset
string include = childNode.Attributes["Include"].Value; string include = childNode.Attributes["Include"].Value;
if (include.Contains("YooAsset")) if (include.Contains("YooAsset"))
{
return true; return true;
} }
} }
}
return false; return false;
} }
} }
} }

View File

@ -12,12 +12,18 @@
"versionDefines": [ "versionDefines": [
{ {
"name": "com.tuyoogame.yooasset", "name": "com.tuyoogame.yooasset",
"define": "YOO_VERSION_2_3_4" "expression": "",
"define": "YOO_ASSET_2"
}, },
{ {
"name": "com.tuyoogame.yooasset", "name": "com.tuyoogame.yooasset",
"expression": "2.3.4", "expression": "2.3",
"define": "YOO_VERSION_2_3_4_OR_NEWER" "define": "YOO_ASSET_2_3"
},
{
"name": "com.tuyoogame.yooasset",
"expression": "2.3",
"define": "YOO_ASSET_2_3_OR_NEWER"
} }
], ],
"noEngineReferences": false "noEngineReferences": false