mirror of https://github.com/tuyoogame/YooAsset
parent
10cce48e71
commit
cdbedee705
|
@ -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;
|
||||||
}
|
|
||||||
|
|
||||||
StringWriter sw = new StringWriter();
|
// 将修改后的XML结构重新输出为文本
|
||||||
XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings()
|
var stringWriter = new StringWriter();
|
||||||
{
|
var writerSettings = new XmlWriterSettings();
|
||||||
Indent = true,
|
writerSettings.Indent = true;
|
||||||
});
|
var xmlWriter = XmlWriter.Create(stringWriter, writerSettings);
|
||||||
xmlDoc.WriteTo(writer);
|
xmlDoc.WriteTo(xmlWriter);
|
||||||
writer.Flush();
|
xmlWriter.Flush();
|
||||||
|
return stringWriter.ToString();
|
||||||
return sw.ToString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ProcessDefineConstants(XmlElement element, List<string> macros)
|
/// <summary>
|
||||||
|
/// 处理宏定义
|
||||||
|
/// </summary>
|
||||||
|
private static bool ProcessDefineConstants(XmlElement element)
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue