OnGenerateCSProjectProcessor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Xml;
  5. using System.IO;
  6. using System.Text;
  7. namespace ET
  8. {
  9. public class OnGenerateCSProjectProcessor: AssetPostprocessor
  10. {
  11. public static string OnGeneratedCSProject(string path, string content)
  12. {
  13. if (path.EndsWith("Unity.Core.csproj"))
  14. {
  15. return GenerateCustomProject(path, content);
  16. }
  17. if (Define.EnableCodes)
  18. {
  19. if (path.EndsWith("Unity.Hotfix.Codes.csproj"))
  20. {
  21. content = GenerateCustomProject(path, content);
  22. }
  23. if (path.EndsWith("Unity.Model.Codes.csproj"))
  24. {
  25. content = GenerateCustomProject(path, content);
  26. }
  27. if (path.EndsWith("Unity.HotfixView.Codes.csproj"))
  28. {
  29. content = GenerateCustomProject(path, content);
  30. }
  31. if (path.EndsWith("Unity.ModelView.Codes.csproj"))
  32. {
  33. content = GenerateCustomProject(path, content);
  34. }
  35. }
  36. else
  37. {
  38. if (path.EndsWith("Unity.Hotfix.csproj"))
  39. {
  40. content = content.Replace("<Compile Include=\"Assets\\Scripts\\Empty\\Hotfix\\Empty.cs\" />", string.Empty);
  41. content = content.Replace("<None Include=\"Assets\\Scripts\\Empty\\Hotfix\\Unity.Hotfix.asmdef\" />", string.Empty);
  42. content = GenerateCustomProject(path, content,
  43. @"Assets\Scripts\Codes\Hotfix\**\*.cs %(RecursiveDir)%(FileName)%(Extension)");
  44. }
  45. if (path.EndsWith("Unity.HotfixView.csproj"))
  46. {
  47. content = content.Replace("<Compile Include=\"Assets\\Scripts\\Empty\\HotfixView\\Empty.cs\" />", string.Empty);
  48. content = content.Replace("<None Include=\"Assets\\Scripts\\Empty\\HotfixView\\Unity.HotfixView.asmdef\" />", string.Empty);
  49. content = GenerateCustomProject(path, content,
  50. @"Assets\Scripts\Codes\HotfixView\**\*.cs %(RecursiveDir)%(FileName)%(Extension)");
  51. }
  52. if (path.EndsWith("Unity.Model.csproj"))
  53. {
  54. content = content.Replace("<Compile Include=\"Assets\\Scripts\\Empty\\Model\\Empty.cs\" />", string.Empty);
  55. content = content.Replace("<None Include=\"Assets\\Scripts\\Empty\\Model\\Unity.Model.asmdef\" />", string.Empty);
  56. content = GenerateCustomProject(path, content,
  57. @"Assets\Scripts\Codes\Model\Server\**\*.cs Server\%(RecursiveDir)%(FileName)%(Extension)",
  58. @"Assets\Scripts\Codes\Model\Client\**\*.cs Client\%(RecursiveDir)%(FileName)%(Extension)",
  59. @"Assets\Scripts\Codes\Model\Share\**\*.cs Share\%(RecursiveDir)%(FileName)%(Extension)",
  60. @"Assets\Scripts\Codes\Model\Generate\ClientServer\**\*.cs Generate\%(RecursiveDir)%(FileName)%(Extension)");
  61. }
  62. if (path.EndsWith("Unity.ModelView.csproj"))
  63. {
  64. content = content.Replace("<Compile Include=\"Assets\\Scripts\\Empty\\ModelView\\Empty.cs\" />", string.Empty);
  65. content = content.Replace("<None Include=\"Assets\\Scripts\\Empty\\ModelView\\Unity.ModelView.asmdef\" />", string.Empty);
  66. content = GenerateCustomProject(path, content,
  67. @"Assets\Scripts\Codes\ModelView\**\*.cs %(RecursiveDir)%(FileName)%(Extension)");
  68. }
  69. }
  70. return content;
  71. }
  72. private static string GenerateCustomProject(string path, string content, params string[] links)
  73. {
  74. XmlDocument doc = new XmlDocument();
  75. doc.LoadXml(content);
  76. var newDoc = doc.Clone() as XmlDocument;
  77. var rootNode = newDoc.GetElementsByTagName("Project")[0];
  78. XmlElement itemGroup = newDoc.CreateElement("ItemGroup", newDoc.DocumentElement.NamespaceURI);
  79. foreach (var s in links)
  80. {
  81. string[] ss = s.Split(' ');
  82. string p = ss[0];
  83. string linkStr = ss[1];
  84. XmlElement compile = newDoc.CreateElement("Compile", newDoc.DocumentElement.NamespaceURI);
  85. XmlElement link = newDoc.CreateElement("Link", newDoc.DocumentElement.NamespaceURI);
  86. link.InnerText = linkStr;
  87. //compile.AppendChild(link);
  88. compile.SetAttribute("Include", p);
  89. itemGroup.AppendChild(compile);
  90. }
  91. var projectReference = newDoc.CreateElement("ProjectReference", newDoc.DocumentElement.NamespaceURI);
  92. projectReference.SetAttribute("Include", @"..\Share\Analyzer\Share.Analyzer.csproj");
  93. projectReference.SetAttribute("OutputItemType", @"Analyzer");
  94. projectReference.SetAttribute("ReferenceOutputAssembly", @"false");
  95. var project = newDoc.CreateElement("Project", newDoc.DocumentElement.NamespaceURI);
  96. project.InnerText = @"{d1f2986b-b296-4a2d-8f12-be9f470014c3}";
  97. projectReference.AppendChild(project);
  98. var name = newDoc.CreateElement("Name", newDoc.DocumentElement.NamespaceURI);
  99. name.InnerText = "Analyzer";
  100. projectReference.AppendChild(project);
  101. itemGroup.AppendChild(projectReference);
  102. rootNode.AppendChild(itemGroup);
  103. using (StringWriter sw = new StringWriter())
  104. {
  105. using (XmlTextWriter tx = new XmlTextWriter(sw))
  106. {
  107. tx.Formatting = Formatting.Indented;
  108. newDoc.WriteTo(tx);
  109. tx.Flush();
  110. return sw.GetStringBuilder().ToString();
  111. }
  112. }
  113. }
  114. }
  115. }