Il2CppDefGenerator.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }
  22. private readonly Options _options;
  23. public Il2CppDefGenerator(Options options)
  24. {
  25. _options = options;
  26. }
  27. private static readonly Regex s_unityVersionPat = new Regex(@"(\d+)\.(\d+)\.(\d+)");
  28. public void Generate()
  29. {
  30. GenerateIl2CppConfig();
  31. GeneratePlaceHolderAssemblies();
  32. }
  33. private void GenerateIl2CppConfig()
  34. {
  35. var frr = new FileRegionReplace(File.ReadAllText(_options.OutputFile));
  36. List<string> lines = new List<string>();
  37. var match = s_unityVersionPat.Matches(_options.UnityVersion)[0];
  38. int majorVer = int.Parse(match.Groups[1].Value);
  39. int minorVer1 = int.Parse(match.Groups[2].Value);
  40. int minorVer2 = int.Parse(match.Groups[3].Value);
  41. lines.Add($"#define HYBRIDCLR_UNITY_VERSION {majorVer}{minorVer1.ToString("D2")}{minorVer2.ToString("D2")}");
  42. lines.Add($"#define HYBRIDCLR_UNITY_{majorVer} 1");
  43. for (int ver = 2019; ver <= 2024; ver++)
  44. {
  45. if (majorVer >= ver)
  46. {
  47. lines.Add($"#define HYBRIDCLR_UNITY_{ver}_OR_NEW 1");
  48. }
  49. }
  50. frr.Replace("UNITY_VERSION", string.Join("\n", lines));
  51. frr.Commit(_options.OutputFile);
  52. Debug.Log($"[HybridCLR.Editor.Il2CppDef.Generator] output:{_options.OutputFile}");
  53. }
  54. private void GeneratePlaceHolderAssemblies()
  55. {
  56. var frr = new FileRegionReplace(File.ReadAllText(_options.OutputFile2));
  57. List<string> lines = new List<string>();
  58. foreach (var ass in _options.HotUpdateAssemblies)
  59. {
  60. lines.Add($"\t\t\"{ass}\",");
  61. }
  62. frr.Replace("PLACE_HOLDER", string.Join("\n", lines));
  63. frr.Commit(_options.OutputFile2);
  64. Debug.Log($"[HybridCLR.Editor.Il2CppDef.Generator] output:{_options.OutputFile2}");
  65. }
  66. }
  67. }