Il2CppDefGenerator.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using HybridCLR.Editor.ABI;
  2. using HybridCLR.Editor.Template;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using UnityEngine;
  11. namespace HybridCLR.Editor.Il2CppDef
  12. {
  13. public class Il2CppDefGenerator
  14. {
  15. public class Options
  16. {
  17. public List<string> HotUpdateAssemblies { get; set; }
  18. public string OutputFile { get; set; }
  19. public string OutputFile2 { get; set; }
  20. public string UnityVersion { get; set; }
  21. public bool EnableProfilerInReleaseBuild { get; set; }
  22. public bool EnableStraceTraceInWebGLReleaseBuild { get; set; }
  23. }
  24. private readonly Options _options;
  25. public Il2CppDefGenerator(Options options)
  26. {
  27. _options = options;
  28. }
  29. private static readonly Regex s_unityVersionPat = new Regex(@"(\d+)\.(\d+)\.(\d+)");
  30. public void Generate()
  31. {
  32. GenerateIl2CppConfig();
  33. GeneratePlaceHolderAssemblies();
  34. }
  35. private void GenerateIl2CppConfig()
  36. {
  37. var frr = new FileRegionReplace(File.ReadAllText(_options.OutputFile));
  38. List<string> lines = new List<string>();
  39. var match = s_unityVersionPat.Matches(_options.UnityVersion)[0];
  40. int majorVer = int.Parse(match.Groups[1].Value);
  41. int minorVer1 = int.Parse(match.Groups[2].Value);
  42. int minorVer2 = int.Parse(match.Groups[3].Value);
  43. lines.Add($"#define HYBRIDCLR_UNITY_VERSION {majorVer}{minorVer1.ToString("D2")}{minorVer2.ToString("D2")}");
  44. lines.Add($"#define HYBRIDCLR_UNITY_{majorVer} 1");
  45. for (int ver = 2019; ver <= 2023; ver++)
  46. {
  47. if (majorVer >= ver)
  48. {
  49. lines.Add($"#define HYBRIDCLR_UNITY_{ver}_OR_NEW 1");
  50. }
  51. }
  52. for (int ver = 6000; ver <= 6100; ver++)
  53. {
  54. if (majorVer >= ver)
  55. {
  56. lines.Add($"#define HYBRIDCLR_UNITY_{ver}_OR_NEW 1");
  57. }
  58. }
  59. #if TUANJIE_1_1_OR_NEWER
  60. var tuanjieMatch = Regex.Matches(Application.tuanjieVersion, @"(\d+)\.(\d+)\.(\d+)");
  61. int tuanjieMajorVer = int.Parse(tuanjieMatch[0].Groups[1].Value);
  62. int tuanjieMinorVer1 = int.Parse(tuanjieMatch[0].Groups[2].Value);
  63. int tuanjieMinorVer2 = int.Parse(tuanjieMatch[0].Groups[3].Value);
  64. lines.Add($"#define HYBRIDCLR_TUANJIE_VERSION {tuanjieMajorVer}{tuanjieMinorVer1.ToString("D2")}{tuanjieMinorVer2.ToString("D2")}");
  65. #elif TUANJIE_2022_3_OR_NEWER
  66. lines.Add($"#define HYBRIDCLR_TUANJIE_VERSION 10000");
  67. #endif
  68. if (_options.EnableProfilerInReleaseBuild)
  69. {
  70. lines.Add("#define HYBRIDCLR_ENABLE_PROFILER_IN_RELEASE_BUILD 1");
  71. }
  72. if (_options.EnableStraceTraceInWebGLReleaseBuild)
  73. {
  74. lines.Add("#define HYBRIDCLR_ENABLE_STRACE_TRACE_IN_WEBGL_RELEASE_BUILD 1");
  75. }
  76. frr.Replace("UNITY_VERSION", string.Join("\n", lines));
  77. frr.Commit(_options.OutputFile);
  78. Debug.Log($"[HybridCLR.Editor.Il2CppDef.Generator] output:{_options.OutputFile}");
  79. }
  80. private void GeneratePlaceHolderAssemblies()
  81. {
  82. var frr = new FileRegionReplace(File.ReadAllText(_options.OutputFile2));
  83. List<string> lines = new List<string>();
  84. foreach (var ass in _options.HotUpdateAssemblies)
  85. {
  86. lines.Add($"\t\t\"{ass}\",");
  87. }
  88. frr.Replace("PLACE_HOLDER", string.Join("\n", lines));
  89. frr.Commit(_options.OutputFile2);
  90. Debug.Log($"[HybridCLR.Editor.Il2CppDef.Generator] output:{_options.OutputFile2}");
  91. }
  92. }
  93. }