[opt] 优化AOTGenericReferences生成,包含assembly列表及美化后的泛型类型及函数名

main v2.0.10
walon 2023-03-20 17:30:05 +08:00
parent c84f575a2e
commit 75cb53cb19
2 changed files with 63 additions and 4 deletions

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
@ -11,6 +12,52 @@ namespace HybridCLR.Editor.AOT
{ {
public class GenericReferenceWriter public class GenericReferenceWriter
{ {
private static readonly Dictionary<Type, string> _typeNameMapping = new Dictionary<Type, string>
{
{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<string, string> _typeSimpleNameMapping = new Dictionary<string, string>();
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<GenericClass> types, List<GenericMethod> methods, string outputFile) public void Write(List<GenericClass> types, List<GenericMethod> methods, string outputFile)
{ {
string parentDir = Directory.GetParent(outputFile).FullName; string parentDir = Directory.GetParent(outputFile).FullName;
@ -20,18 +67,30 @@ namespace HybridCLR.Editor.AOT
codes.Add("public class AOTGenericReferences : UnityEngine.MonoBehaviour"); codes.Add("public class AOTGenericReferences : UnityEngine.MonoBehaviour");
codes.Add("{"); codes.Add("{");
codes.Add("");
codes.Add("\t// {{ AOT assemblies");
List<dnlib.DotNet.ModuleDef> modules = new HashSet<dnlib.DotNet.ModuleDef>(
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("");
codes.Add("\t// {{ constraint implement type"); codes.Add("\t// {{ constraint implement type");
codes.Add("\t// }} "); codes.Add("\t// }} ");
codes.Add(""); 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)); types.Sort((a, b) => a.Type.FullName.CompareTo(b.Type.FullName));
foreach(var type in types) foreach(var type in types)
{ {
codes.Add($"\t//{type.ToTypeSig()}"); codes.Add($"\t// {PrettifyTypeSig(type.ToTypeSig().ToString())}");
} }
codes.Add("\t// }}"); codes.Add("\t// }}");
@ -51,7 +110,7 @@ namespace HybridCLR.Editor.AOT
}); });
foreach(var method in methods) foreach(var method in methods)
{ {
codes.Add($"\t\t// {method.ToMethodSpec()}"); codes.Add($"\t\t// {PrettifyMethodSig(method.ToMethodSpec().ToString())}");
} }
codes.Add("\t}"); codes.Add("\t}");

View File

@ -1,6 +1,6 @@
{ {
"name": "com.focus-creative-games.hybridclr_unity", "name": "com.focus-creative-games.hybridclr_unity",
"version": "2.0.9", "version": "2.0.10",
"displayName": "HybridCLR", "displayName": "HybridCLR",
"description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR", "description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR",
"category": "Runtime", "category": "Runtime",