Skip to content

Commit 29cc677

Browse files
committed
AssetStudio - Prepend MonoBehaviour layouts to MonoBehaviour and ScriptableObject based types
AssetStudio - Add recusion checks to kill recursive types AssetStudio - Use UnityCsReference sources directly w/o submodule AssetStudio - Releax UnityCsReference checks to include errornously omitted fields CLI - Add `--output` , `-o` option to produce JSON like the orignial TypetreeGenerator
1 parent 2d508ea commit 29cc677

File tree

18 files changed

+1182
-70
lines changed

18 files changed

+1182
-70
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ dotnet restore
2424
dotnet publish -c Release -r win-x64
2525
# Build Python bindings
2626
cd bindings\python
27-
# Install as an editable package
28-
pip install -e .
27+
# Install
28+
pip install .
2929
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Unity C# reference source
2+
// Copyright (c) Unity Technologies. For terms of use, see
3+
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Mono.Cecil;
9+
using Unity.CecilTools.Extensions;
10+
11+
namespace Unity.CecilTools
12+
{
13+
public static class CecilUtils
14+
{
15+
public static MethodDefinition FindInTypeExplicitImplementationFor(MethodDefinition interfaceMethod, TypeDefinition typeDefinition)
16+
{
17+
return typeDefinition.Methods.SingleOrDefault(m => m.Overrides.Any(o => o.CheckedResolve().SameAs(interfaceMethod)));
18+
}
19+
20+
public static IEnumerable<TypeDefinition> AllInterfacesImplementedBy(TypeDefinition typeDefinition)
21+
{
22+
return TypeAndBaseTypesOf(typeDefinition).SelectMany(t => t.Interfaces).Select(i => i.InterfaceType.CheckedResolve()).Distinct();
23+
}
24+
25+
public static IEnumerable<TypeDefinition> TypeAndBaseTypesOf(TypeReference typeReference)
26+
{
27+
while (typeReference != null)
28+
{
29+
var typeDefinition = typeReference.CheckedResolve();
30+
yield return typeDefinition;
31+
typeReference = typeDefinition.BaseType;
32+
}
33+
}
34+
35+
public static IEnumerable<TypeDefinition> BaseTypesOf(TypeReference typeReference)
36+
{
37+
return TypeAndBaseTypesOf(typeReference).Skip(1);
38+
}
39+
40+
public static bool IsGenericList(TypeReference type)
41+
{
42+
return type.Name == "List`1" && type.SafeNamespace() == "System.Collections.Generic";
43+
}
44+
45+
public static bool IsGenericDictionary(TypeReference type)
46+
{
47+
if (type is GenericInstanceType)
48+
type = ((GenericInstanceType)type).ElementType;
49+
50+
return type.Name == "Dictionary`2" && type.SafeNamespace() == "System.Collections.Generic";
51+
}
52+
53+
public static TypeReference ElementTypeOfCollection(TypeReference type)
54+
{
55+
var at = type as ArrayType;
56+
if (at != null)
57+
return at.ElementType;
58+
59+
if (IsGenericList(type))
60+
return ((GenericInstanceType)type).GenericArguments.Single();
61+
62+
throw new ArgumentException();
63+
}
64+
}
65+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Unity C# reference source
2+
// Copyright (c) Unity Technologies. For terms of use, see
3+
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4+
5+
using System;
6+
using Mono.Cecil;
7+
8+
namespace Unity.CecilTools
9+
{
10+
static public class ElementType
11+
{
12+
public static TypeReference For(TypeReference byRefType)
13+
{
14+
var refType = byRefType as TypeSpecification;
15+
if (refType != null)
16+
return refType.ElementType;
17+
18+
throw new ArgumentException(string.Format("TypeReference isn't a TypeSpecification {0} ", byRefType));
19+
}
20+
}
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Unity C# reference source
2+
// Copyright (c) Unity Technologies. For terms of use, see
3+
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4+
5+
using Mono.Cecil;
6+
7+
namespace Unity.CecilTools.Extensions
8+
{
9+
static class MethodDefinitionExtensions
10+
{
11+
public static bool SameAs(this MethodDefinition self, MethodDefinition other)
12+
{
13+
// FIXME: should be able to compare MethodDefinition references directly
14+
return self.FullName == other.FullName;
15+
}
16+
17+
public static string PropertyName(this MethodDefinition self)
18+
{
19+
return self.Name.Substring(4);
20+
}
21+
22+
public static bool IsConversionOperator(this MethodDefinition method)
23+
{
24+
if (!method.IsSpecialName)
25+
return false;
26+
27+
return method.Name == "op_Implicit" || method.Name == "op_Explicit";
28+
}
29+
30+
public static bool IsSimpleSetter(this MethodDefinition original)
31+
{
32+
return original.IsSetter && original.Parameters.Count == 1;
33+
}
34+
35+
public static bool IsSimpleGetter(this MethodDefinition original)
36+
{
37+
return original.IsGetter && original.Parameters.Count == 0;
38+
}
39+
40+
public static bool IsSimplePropertyAccessor(this MethodDefinition method)
41+
{
42+
return method.IsSimpleGetter() || method.IsSimpleSetter();
43+
}
44+
45+
public static bool IsDefaultConstructor(MethodDefinition m)
46+
{
47+
return m.IsConstructor && !m.IsStatic && m.Parameters.Count == 0;
48+
}
49+
}
50+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Unity C# reference source
2+
// Copyright (c) Unity Technologies. For terms of use, see
3+
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4+
5+
using System;
6+
using Mono.Cecil;
7+
8+
namespace Unity.CecilTools.Extensions
9+
{
10+
public static class ResolutionExtensions
11+
{
12+
public static TypeDefinition CheckedResolve(this TypeReference type)
13+
{
14+
return Resolve(type, reference => reference.Resolve());
15+
}
16+
17+
public static MethodDefinition CheckedResolve(this MethodReference method)
18+
{
19+
return Resolve(method, reference => reference.Resolve());
20+
}
21+
22+
private static TDefinition Resolve<TReference, TDefinition>(TReference reference, Func<TReference, TDefinition> resolve)
23+
where TReference : MemberReference
24+
where TDefinition : class, IMemberDefinition
25+
{
26+
if (reference.Module == null)
27+
throw new ResolutionException(reference);
28+
29+
var definition = resolve(reference);
30+
if (definition == null)
31+
throw new ResolutionException(reference);
32+
33+
return definition;
34+
}
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Unity C# reference source
2+
// Copyright (c) Unity Technologies. For terms of use, see
3+
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using Mono.Cecil;
10+
11+
namespace Unity.CecilTools.Extensions
12+
{
13+
public static class TypeDefinitionExtensions
14+
{
15+
public static bool IsSubclassOf(this TypeDefinition type, string baseTypeName)
16+
{
17+
var baseType = type.BaseType;
18+
if (baseType == null)
19+
return false;
20+
if (baseType.FullName == baseTypeName)
21+
return true;
22+
23+
var baseTypeDef = baseType.Resolve();
24+
if (baseTypeDef == null)
25+
return false;
26+
27+
return IsSubclassOf(baseTypeDef, baseTypeName);
28+
}
29+
30+
public static bool IsSubclassOf(this TypeDefinition type, params string[] baseTypeNames)
31+
{
32+
var baseType = type.BaseType;
33+
if (baseType == null)
34+
return false;
35+
36+
for (int i = 0; i < baseTypeNames.Length; i++)
37+
if (baseType.FullName == baseTypeNames[i])
38+
return true;
39+
40+
var baseTypeDef = baseType.Resolve();
41+
if (baseTypeDef == null)
42+
return false;
43+
44+
return IsSubclassOf(baseTypeDef, baseTypeNames);
45+
}
46+
}
47+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Unity C# reference source
2+
// Copyright (c) Unity Technologies. For terms of use, see
3+
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4+
5+
using Mono.Cecil;
6+
7+
namespace Unity.CecilTools.Extensions
8+
{
9+
public static class TypeReferenceExtensions
10+
{
11+
public static string SafeNamespace(this TypeReference type)
12+
{
13+
if (type.IsGenericInstance)
14+
return ((GenericInstanceType)type).ElementType.SafeNamespace();
15+
if (type.IsNested)
16+
return type.DeclaringType.SafeNamespace();
17+
return type.Namespace;
18+
}
19+
20+
public static bool IsAssignableTo(this TypeReference typeRef, string typeName)
21+
{
22+
try
23+
{
24+
if (typeRef.IsGenericInstance)
25+
return ElementType.For(typeRef).IsAssignableTo(typeName);
26+
27+
if (typeRef.FullName == typeName)
28+
return true;
29+
30+
return typeRef.CheckedResolve().IsSubclassOf(typeName);
31+
}
32+
catch (AssemblyResolutionException) // If we can't resolve our typeref or one of its base types,
33+
{ // let's assume it is not assignable to our target type
34+
return false;
35+
}
36+
}
37+
38+
public static bool IsEnum(this TypeReference type)
39+
{
40+
return type.IsValueType && !type.IsPrimitive && type.CheckedResolve().IsEnum;
41+
}
42+
43+
public static bool IsStruct(this TypeReference type)
44+
{
45+
return type.IsValueType && !type.IsPrimitive && !type.IsEnum() && !IsSystemDecimal(type);
46+
}
47+
48+
private static bool IsSystemDecimal(TypeReference type)
49+
{
50+
return type.FullName == "System.Decimal";
51+
}
52+
}
53+
}

TypeTreeGeneratorAPI/TypeTreeGenerator/AssetStudio/AssetStudioUtility/SerilizedTypeHelper.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ public void AddSphericalHarmonicsL2(List<TypeTreeNode> nodes, string name, int i
261261
nodes.Add(new TypeTreeNode("float", "sh[26]", indent + 1, false));
262262
}
263263

264+
public void AddMonoBehaviour(List<TypeTreeNode> nodes, int indent)
265+
{
266+
nodes.Add(new TypeTreeNode("MonoBehaviour", "Base", indent, false));
267+
AddPPtr(nodes, "GameObject", "m_GameObject", indent + 1);
268+
nodes.Add(new TypeTreeNode("UInt8", "m_Enabled", indent + 1, true));
269+
AddPPtr(nodes, "MonoScript", "m_Script", indent + 1);
270+
AddString(nodes, "m_Name", indent + 1);
271+
}
264272
public void AddPropertyName(List<TypeTreeNode> nodes, string name, int indent)
265273
{
266274
nodes.Add(new TypeTreeNode("PropertyName", name, indent, false));

0 commit comments

Comments
 (0)