Skip to content

Commit 244638c

Browse files
committed
* resolve generater errors
* copy struct/delegate/enum * ObjectStore
1 parent 7f2d329 commit 244638c

20 files changed

Lines changed: 671 additions & 283 deletions

BindGenerater/BindGenerater.csproj

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
3535
<ItemGroup>
36+
<Reference Include="Humanizer, Version=2.2.0.0, Culture=neutral, PublicKeyToken=979442b78dfc278e, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Humanizer.Core.2.2.0\lib\netstandard1.0\Humanizer.dll</HintPath>
38+
</Reference>
39+
<Reference Include="ICSharpCode.Decompiler, Version=6.2.1.6137, Culture=neutral, PublicKeyToken=d4bfe873e7598c49, processorArchitecture=MSIL">
40+
<HintPath>..\packages\ICSharpCode.Decompiler.6.2.1.6137\lib\netstandard2.0\ICSharpCode.Decompiler.dll</HintPath>
41+
</Reference>
3642
<Reference Include="Mono.Cecil">
3743
<HintPath>ThirdParty\Mono.Cecil.dll</HintPath>
3844
</Reference>
@@ -46,7 +52,18 @@
4652
<HintPath>ThirdParty\nunit.framework.dll</HintPath>
4753
</Reference>
4854
<Reference Include="System" />
55+
<Reference Include="System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
56+
<HintPath>..\packages\System.Collections.Immutable.1.5.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
57+
</Reference>
4958
<Reference Include="System.Core" />
59+
<Reference Include="System.Reflection.Metadata, Version=1.4.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
60+
<HintPath>..\packages\System.Reflection.Metadata.1.6.0\lib\netstandard2.0\System.Reflection.Metadata.dll</HintPath>
61+
</Reference>
62+
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
63+
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
64+
<Private>True</Private>
65+
<Private>True</Private>
66+
</Reference>
5067
<Reference Include="System.Xml.Linq" />
5168
<Reference Include="System.Data.DataSetExtensions" />
5269
<Reference Include="Microsoft.CSharp" />
@@ -74,11 +91,11 @@
7491
</ItemGroup>
7592
<ItemGroup>
7693
<Compile Include="Generater\Binder.cs" />
94+
<Compile Include="Generater\CustomOutputVisitor.cs" />
7795
<Compile Include="Generater\MethodResolver.cs" />
7896
<Compile Include="Generater\TypeResolver.cs" />
7997
<Compile Include="Generater\ClassGenerater.cs" />
8098
<Compile Include="Generater\CodeGenerater.cs" />
81-
<Compile Include="Generater\CopyGenerater.cs" />
8299
<Compile Include="Generater\DelegateGenerater.cs" />
83100
<Compile Include="Generater\GenerateBindings.cs" />
84101
<Compile Include="Generater\MethodGenerater.cs" />
Binary file not shown.
121 KB
Binary file not shown.
-1.5 KB
Binary file not shown.
-1 KB
Binary file not shown.
8 KB
Binary file not shown.

BindGenerater/Generater/Binder.cs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Mono.Cecil;
1+
using ICSharpCode.Decompiler;
2+
using ICSharpCode.Decompiler.CSharp;
3+
using Mono.Cecil;
24
using System;
35
using System.Collections.Generic;
46
using System.IO;
@@ -18,6 +20,8 @@ public static class Binder
1820
public static CodeWriter FuncDefineWriter;
1921
public static CodeWriter FuncSerWriter;
2022
public static CodeWriter FuncDeSerWriter;
23+
public static CSharpDecompiler Decompiler;
24+
public static DecompilerSettings DecompilerSetting;
2125

2226
private static string[] BindTypes = new string[]
2327
{
@@ -28,11 +32,14 @@ public static class Binder
2832
};
2933
private static string[] IgnorTypes = new string[]
3034
{
31-
"Unity.Collections.NativeArray`1<T>",
35+
// "Unity.Collections.NativeArray`1<T>",
36+
"UnityEngine.WSA",
37+
// "Unity.Collections.LowLevel.Unsafe",
38+
"System.Collections",
39+
"UnityEditor"
3240
};
3341

34-
35-
public static void Bind(string dllPath, string outDir)
42+
public static void Init(string outDir)
3643
{
3744
OutDir = outDir;
3845
if (!Directory.Exists(outDir))
@@ -41,28 +48,49 @@ public static void Bind(string dllPath, string outDir)
4148
FuncDefineWriter = new CodeWriter(File.CreateText(Path.Combine(outDir, "Binder.define.cs")));
4249
FuncSerWriter = new CodeWriter(File.CreateText(Path.Combine(outDir, "Binder.funcser.cs")));
4350
FuncDeSerWriter = new CodeWriter(File.CreateText(Path.Combine(outDir, "Binder.funcdeser.cs")));
51+
}
52+
53+
public static void End()
54+
{
55+
56+
}
57+
58+
public static void Bind(string dllPath)
59+
{
60+
DecompilerSetting = new DecompilerSettings(LanguageVersion.CSharp7);
61+
Decompiler = new CSharpDecompiler(dllPath, DecompilerSetting);
62+
var dir = Path.GetDirectoryName(dllPath);
63+
DefaultAssemblyResolver resolver = new DefaultAssemblyResolver();
64+
resolver.AddSearchDirectory(dir);
65+
ReaderParameters parameters = new ReaderParameters()
66+
{
67+
AssemblyResolver = resolver,
68+
ReadSymbols = false,
69+
};
70+
71+
ModuleDefinition module = ModuleDefinition.ReadModule(dllPath, parameters);
4472

45-
ModuleDefinition module = ModuleDefinition.ReadModule(dllPath);
4673
moduleTypes = new HashSet<TypeReference>(module.Types);
4774
var ignorSet = Utils.IgnoreTypeSet;
4875
foreach(var type in IgnorTypes)
4976
{
5077
ignorSet.Add(type);
5178
}
5279

53-
var typeSet = new HashSet<string>(BindTypes);
80+
//var typeSet = new HashSet<string>(BindTypes);
5481

5582
foreach (TypeDefinition type in moduleTypes)
5683
{
5784
if (!type.IsPublic)
5885
continue;
5986

60-
Console.WriteLine(type.FullName);
87+
AddType(type);
6188

89+
/* Console.WriteLine(type.FullName);
6290
if (typeSet.Contains(type.Name))
6391
{
6492
AddType(type);
65-
}
93+
}*/
6694
}
6795

6896
TypeResolver.WrapperSide = true;
@@ -84,20 +112,21 @@ public static void Bind(string dllPath, string outDir)
84112
FuncSerWriter.EndAll();
85113
FuncDeSerWriter.EndAll();
86114

87-
CopyGenerater.GenAsm();
88115
}
89116

90117
public static void AddType(TypeDefinition type)
91118
{
92-
if (type.Name == "UnityAction")
93-
Utils.Log("");
94-
95119
if (type == null || !moduleTypes.Contains(type))
96120
return;
97121
if (!types.Add(type))
98122
return;
99123

100-
CodeGenerater gener = null;
124+
if (!Utils.Filter(type))
125+
return;
126+
127+
generaters.Enqueue(new ClassGenerater(type));
128+
129+
/*CodeGenerater gener = null;
101130
if (type.IsValueType || type.IsEnum || type.IsDelegate())
102131
{
103132
CopyGenerater.AddTpe(type);
@@ -123,7 +152,7 @@ public static void AddType(TypeDefinition type)
123152
}
124153
125154
if(gener != null)
126-
generaters.Enqueue(gener);
155+
generaters.Enqueue(gener);*/
127156
}
128157
}
129158
}

BindGenerater/Generater/ClassGenerater.cs

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
using System.Collections.Generic;
55
using System.IO;
66

7+
using ICSharpCode.Decompiler;
8+
using ICSharpCode.Decompiler.CSharp;
9+
using ICSharpCode.Decompiler.TypeSystem;
10+
using ICSharpCode.Decompiler.CSharp.Syntax;
11+
712
namespace Generater
813
{
914
public class ClassGenerater : CodeGenerater
@@ -12,14 +17,22 @@ public class ClassGenerater : CodeGenerater
1217

1318
List<PropertyGenerater> properties = new List<PropertyGenerater>();
1419
List<MethodGenerater> methods = new List<MethodGenerater>();
15-
20+
List<TypeDefinition> nestType = new List<TypeDefinition>();
1621
HashSet<string> refNameSpace = new HashSet<string>();
17-
22+
bool hasDefaultConstructor = false;
1823

1924
public ClassGenerater(TypeDefinition type)
2025
{
2126
genType = type;
2227

28+
foreach (var t in type.NestedTypes)
29+
{
30+
if (CopyOrign(t))
31+
nestType.Add(t);
32+
}
33+
if (CopyOrign(genType))
34+
return;
35+
2336
foreach (FieldDefinition field in genType.Fields)
2437
{
2538
properties.Add(new PropertyGenerater(field));
@@ -37,11 +50,13 @@ public ClassGenerater(TypeDefinition type)
3750

3851
foreach (MethodDefinition method in genType.Methods)
3952
{
40-
if(method.IsPublic && !method.IsGetter && !method.IsSetter && Utils.Filter(method))
53+
if((method.IsPublic || genType.IsInterface) && !method.IsGetter && !method.IsSetter && Utils.Filter(method))
4154
{
4255
methods.Add(new MethodGenerater(method));
4356
refNameSpace.UnionWith(Utils.GetNameSpaceRef(method));
4457
}
58+
if (method.IsConstructor && method.Parameters.Count == 0 && method.IsPublic)
59+
hasDefaultConstructor = true;
4560
}
4661
}
4762

@@ -52,34 +67,42 @@ public override string TypeFullName()
5267

5368
public override void Gen()
5469
{
70+
71+
5572
var filePath = Path.Combine(Binder.OutDir, $"Binder.{TypeFullName()}.cs");
5673
using (new CS(new CodeWriter(File.CreateText(filePath))))
5774
{
5875
base.Gen();
5976

60-
77+
if (CopyOrign(genType))
78+
{
79+
CS.Writer.WriteLine(CopyGen(genType,false), false);
80+
CS.Writer.EndAll();
81+
return;
82+
}
83+
6184
foreach (var ns in refNameSpace)
6285
{
6386
if (!string.IsNullOrEmpty(ns))
6487
{
6588
CS.Writer.WriteLine($"using {ns}");
66-
if(!ns.StartsWith("System"))
67-
CS.Writer.WriteLine($"using PS_{ns}");
89+
// if(!ns.StartsWith("System"))
90+
// CS.Writer.WriteLine($"using PS_{ns}");
6891
}
6992
}
7093
CS.Writer.WriteLine("using System.Runtime.InteropServices");
7194
CS.Writer.WriteLine("using Object = UnityEngine.Object");
7295

7396
if (!string.IsNullOrEmpty(genType.Namespace))
7497
{
75-
CS.Writer.Start($"namespace PS_{genType.Namespace}");
98+
CS.Writer.Start($"namespace {genType.Namespace}");
7699
}
77100

78101
var classDefine = $"public class {genType.Name}";
79102

80103
if (genType.IsInterface)
81104
{
82-
classDefine += $" : WObject";
105+
// classDefine += $" : WObject";
83106
}
84107
else if (genType.BaseType != null)
85108
{
@@ -94,15 +117,24 @@ public override void Gen()
94117

95118
CS.Writer.Start(classDefine);
96119

120+
foreach(var t in nestType)
121+
{
122+
CS.Writer.WriteLine(CopyGen(t,true),false);
123+
}
97124

98-
CS.Writer.Start($"internal {genType.Name}(int handle,IntPtr ptr): base(handle, ptr)");
99-
CS.Writer.End();
125+
/*CS.Writer.Start($"internal {genType.Name}(int handle,IntPtr ptr): base(handle, ptr)");
126+
CS.Writer.End();*/
100127

101128
foreach (var p in properties)
102129
{
103130
p.Gen();
104131
}
105132

133+
134+
if(!hasDefaultConstructor && !genType.IsSealed)
135+
{
136+
CS.Writer.WriteLine($"internal {genType.Name}()" + " { }", false);
137+
}
106138
foreach (var m in methods)
107139
{
108140
m.Gen();
@@ -111,5 +143,43 @@ public override void Gen()
111143
CS.Writer.EndAll();
112144
}
113145
}
146+
147+
bool CopyOrign(TypeDefinition type)
148+
{
149+
if (!type.IsPublic && !type.IsNestedPublic)
150+
return false;
151+
return type.IsValueType || type.IsEnum || type.IsDelegate() || type.IsInterface;
152+
}
153+
154+
string CopyGen(TypeDefinition type ,bool isNested)
155+
{
156+
var tName = type.FullName.Replace("/", "+");
157+
var name = new FullTypeName(tName);
158+
SyntaxTree syntaxTree;
159+
160+
if (isNested)
161+
{
162+
ITypeDefinition typeInfo = Binder.Decompiler.TypeSystem.MainModule.Compilation.FindType(name).GetDefinition();
163+
var tokenOfFirstMethod = typeInfo.MetadataToken;
164+
syntaxTree = Binder.Decompiler.Decompile(tokenOfFirstMethod);
165+
}
166+
else
167+
{
168+
syntaxTree = Binder.Decompiler.DecompileType(name);
169+
}
170+
171+
StringWriter w = new StringWriter();
172+
var outVisitor = new CustomOutputVisitor(isNested, w, Binder.DecompilerSetting.CSharpFormattingOptions);
173+
syntaxTree.AcceptVisitor(outVisitor);
174+
175+
foreach(var ns in outVisitor.nestedUsing)
176+
{
177+
CS.Writer.WriteHead($"using {ns}");
178+
}
179+
180+
var txt = w.ToString();
181+
return txt;
182+
183+
}
114184
}
115185
}

BindGenerater/Generater/CopyGenerater.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)