Il2CppDefGenerator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 UnityVersionTemplateFile { get; set; }
  19. public string UnityVersionOutputFile { get; set; }
  20. public string AssemblyManifestTemplateFile { get; set; }
  21. public string AssemblyManifestOutputFile { get; set; }
  22. public string UnityVersion { 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.UnityVersionTemplateFile));
  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. frr.Replace("UNITY_VERSION", string.Join("\n", lines));
  69. frr.Commit(_options.UnityVersionOutputFile);
  70. Debug.Log($"[HybridCLR.Editor.Il2CppDef.Generator] output:{_options.UnityVersionOutputFile}");
  71. }
  72. private void GeneratePlaceHolderAssemblies()
  73. {
  74. var frr = new FileRegionReplace(File.ReadAllText(_options.AssemblyManifestTemplateFile));
  75. List<string> lines = new List<string>();
  76. foreach (var ass in _options.HotUpdateAssemblies)
  77. {
  78. lines.Add($"\t\t\"{ass}\",");
  79. }
  80. frr.Replace("PLACE_HOLDER", string.Join("\n", lines));
  81. frr.Commit(_options.AssemblyManifestOutputFile);
  82. Debug.Log($"[HybridCLR.Editor.Il2CppDef.Generator] output:{_options.AssemblyManifestOutputFile}");
  83. }
  84. }
  85. }