diff --git a/Editor/AOT/GenericReferenceWriter.cs b/Editor/AOT/GenericReferenceWriter.cs index 7fc577d..ccbebc6 100644 --- a/Editor/AOT/GenericReferenceWriter.cs +++ b/Editor/AOT/GenericReferenceWriter.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using UnityEngine; @@ -11,6 +12,52 @@ namespace HybridCLR.Editor.AOT { public class GenericReferenceWriter { + private static readonly Dictionary _typeNameMapping = new Dictionary + { + {typeof(bool), "bool" }, + {typeof(byte), "byte" }, + {typeof(sbyte), "sbyte" }, + {typeof(short), "short" }, + {typeof(ushort), "ushort" }, + {typeof(int), "int" }, + {typeof(uint), "uint" }, + {typeof(long), "long" }, + {typeof(ulong), "ulong" }, + {typeof(float), "float" }, + {typeof(double), "double" }, + {typeof(object), "object" }, + {typeof(string), "string" }, + }; + + private readonly Dictionary _typeSimpleNameMapping = new Dictionary(); + private readonly Regex _systemTypePattern; + private readonly Regex _genericPattern = new Regex(@"`\d+"); + + public GenericReferenceWriter() + { + foreach (var e in _typeNameMapping) + { + _typeSimpleNameMapping.Add(e.Key.FullName, e.Value); + } + _systemTypePattern = new Regex(string.Join("|", _typeSimpleNameMapping.Keys.Select (k => $@"\b{k}\b"))); + } + + public string PrettifyTypeSig(string typeSig) + { + string s = _genericPattern.Replace(typeSig, "").Replace('/', '.'); + return _systemTypePattern.Replace(s, m => _typeSimpleNameMapping[m.Groups[0].Value]); + } + + public string PrettifyMethodSig(string methodSig) + { + string s = PrettifyTypeSig(methodSig).Replace("::", "."); + if (s.Contains(".ctor(")) + { + s = "new " + s.Replace(".ctor(", "("); + } + return s; + } + public void Write(List types, List methods, string outputFile) { string parentDir = Directory.GetParent(outputFile).FullName; @@ -20,18 +67,30 @@ namespace HybridCLR.Editor.AOT codes.Add("public class AOTGenericReferences : UnityEngine.MonoBehaviour"); codes.Add("{"); + codes.Add(""); + codes.Add("\t// {{ AOT assemblies"); + List modules = new HashSet( + types.Select(t => t.Type.Module).Concat(methods.Select(m => m.Method.Module))).ToList(); + modules.Sort((a, b) => a.Name.CompareTo(b.Name)); + foreach (dnlib.DotNet.ModuleDef module in modules) + { + codes.Add($"\t// {module.Name}"); + } + codes.Add("\t// }}"); + + codes.Add(""); codes.Add("\t// {{ constraint implement type"); codes.Add("\t// }} "); codes.Add(""); - codes.Add("\t// {{ AOT generic type"); + codes.Add("\t// {{ AOT generic types"); types.Sort((a, b) => a.Type.FullName.CompareTo(b.Type.FullName)); foreach(var type in types) { - codes.Add($"\t//{type.ToTypeSig()}"); + codes.Add($"\t// {PrettifyTypeSig(type.ToTypeSig().ToString())}"); } codes.Add("\t// }}"); @@ -51,7 +110,7 @@ namespace HybridCLR.Editor.AOT }); foreach(var method in methods) { - codes.Add($"\t\t// {method.ToMethodSpec()}"); + codes.Add($"\t\t// {PrettifyMethodSig(method.ToMethodSpec().ToString())}"); } codes.Add("\t}"); diff --git a/package.json b/package.json index 35e8028..eb49360 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.focus-creative-games.hybridclr_unity", - "version": "2.0.9", + "version": "2.0.10", "displayName": "HybridCLR", "description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR", "category": "Runtime",