fishnet installed
This commit is contained in:
@ -0,0 +1,20 @@
|
||||
using FishNet.CodeGenerating.Helping.Extension;
|
||||
using MonoFN.Cecil.Cil;
|
||||
|
||||
namespace FishNet.CodeGenerating.Extension
|
||||
{
|
||||
|
||||
|
||||
internal static class ILProcessorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a variable type within the body and returns it's VariableDef.
|
||||
/// </summary>
|
||||
internal static VariableDefinition CreateVariable(this ILProcessor processor, CodegenSession session, System.Type variableType)
|
||||
{
|
||||
return processor.Body.Method.CreateVariable(session, variableType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 127d8312da53b3e49ba9e3e4c6348857
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,131 @@
|
||||
using FishNet.CodeGenerating.Helping.Extension;
|
||||
using MonoFN.Cecil;
|
||||
using MonoFN.Cecil.Rocks;
|
||||
using MonoFN.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FishNet.CodeGenerating.Extension
|
||||
{
|
||||
|
||||
|
||||
internal static class MethodDefinitionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the proper OpCode to use for call methods.
|
||||
/// </summary>
|
||||
public static MonoFN.Cecil.Cil.OpCode GetCallOpCode(this MethodDefinition md)
|
||||
{
|
||||
if (md.Attributes.HasFlag(MethodAttributes.Virtual))
|
||||
return MonoFN.Cecil.Cil.OpCodes.Callvirt;
|
||||
else
|
||||
return MonoFN.Cecil.Cil.OpCodes.Call;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns the proper OpCode to use for call methods.
|
||||
/// </summary>
|
||||
public static MonoFN.Cecil.Cil.OpCode GetCallOpCode(this MethodReference mr, CodegenSession session)
|
||||
{
|
||||
return mr.CachedResolve(session).GetCallOpCode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds otherMd parameters to thisMR and returns added parameters.
|
||||
/// </summary>
|
||||
public static List<ParameterDefinition> CreateParameters(this MethodReference thisMr, CodegenSession session, MethodDefinition otherMd)
|
||||
{
|
||||
return thisMr.CachedResolve(session).CreateParameters(session, otherMd);
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds otherMr parameters to thisMR and returns added parameters.
|
||||
/// </summary>
|
||||
public static List<ParameterDefinition> CreateParameters(this MethodReference thisMr, CodegenSession session, MethodReference otherMr)
|
||||
{
|
||||
return thisMr.CachedResolve(session).CreateParameters(session, otherMr.CachedResolve(session));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds otherMd parameters to thisMd and returns added parameters.
|
||||
/// </summary>
|
||||
public static List<ParameterDefinition> CreateParameters(this MethodDefinition thisMd, CodegenSession session, MethodDefinition otherMd)
|
||||
{
|
||||
List<ParameterDefinition> results = new List<ParameterDefinition>();
|
||||
|
||||
foreach (ParameterDefinition pd in otherMd.Parameters)
|
||||
{
|
||||
session.ImportReference(pd.ParameterType);
|
||||
int currentCount = thisMd.Parameters.Count;
|
||||
string name = (pd.Name + currentCount);
|
||||
ParameterDefinition parameterDef = new ParameterDefinition(name, pd.Attributes, pd.ParameterType);
|
||||
//Set any default values.
|
||||
parameterDef.Constant = pd.Constant;
|
||||
parameterDef.IsReturnValue = pd.IsReturnValue;
|
||||
parameterDef.IsOut = pd.IsOut;
|
||||
foreach (CustomAttribute item in pd.CustomAttributes)
|
||||
parameterDef.CustomAttributes.Add(item);
|
||||
parameterDef.HasConstant = pd.HasConstant;
|
||||
parameterDef.HasDefault = pd.HasDefault;
|
||||
|
||||
thisMd.Parameters.Add(parameterDef);
|
||||
|
||||
results.Add(parameterDef);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a method reference while considering if declaring type is generic.
|
||||
/// </summary>
|
||||
public static MethodReference GetMethodReference(this MethodDefinition md, CodegenSession session)
|
||||
{
|
||||
MethodReference methodRef = session.ImportReference(md);
|
||||
|
||||
//Is generic.
|
||||
if (md.DeclaringType.HasGenericParameters)
|
||||
{
|
||||
GenericInstanceType git = methodRef.DeclaringType.MakeGenericInstanceType();
|
||||
MethodReference result = new MethodReference(md.Name, md.ReturnType)
|
||||
{
|
||||
HasThis = md.HasThis,
|
||||
ExplicitThis = md.ExplicitThis,
|
||||
DeclaringType = git,
|
||||
CallingConvention = md.CallingConvention,
|
||||
};
|
||||
foreach (ParameterDefinition pd in md.Parameters)
|
||||
{
|
||||
session.ImportReference(pd.ParameterType);
|
||||
result.Parameters.Add(pd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return methodRef;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns a method reference for a generic method.
|
||||
/// </summary>
|
||||
public static MethodReference GetMethodReference(this MethodDefinition md, CodegenSession session, TypeReference typeReference)
|
||||
{
|
||||
MethodReference methodRef = session.ImportReference(md);
|
||||
return methodRef.GetMethodReference(session, typeReference);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns a method reference for a generic method.
|
||||
/// </summary>
|
||||
public static MethodReference GetMethodReference(this MethodDefinition md, CodegenSession session, TypeReference[] typeReferences)
|
||||
{
|
||||
MethodReference methodRef = session.ImportReference(md);
|
||||
return methodRef.GetMethodReference(session, typeReferences);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 866ed457fe28c3e4b9698d87b9abd709
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,254 @@
|
||||
using FishNet.CodeGenerating.Helping.Extension;
|
||||
using MonoFN.Cecil;
|
||||
|
||||
namespace FishNet.CodeGenerating.Extension
|
||||
{
|
||||
|
||||
|
||||
internal static class TypeDefinitionExtensions
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns if a TypeDefinition is nullable.
|
||||
/// </summary>
|
||||
public static bool IsNullable(this TypeDefinition td)
|
||||
{
|
||||
return (td.Name == typeof(System.Nullable<>).Name);
|
||||
}
|
||||
|
||||
|
||||
public static MethodReference GetMethodReferenceInBase(this TypeDefinition td, CodegenSession session, string methodName)
|
||||
{
|
||||
MethodDefinition baseMd = td.GetMethodDefinitionInBase(session, methodName);
|
||||
if (baseMd == null)
|
||||
return null;
|
||||
|
||||
|
||||
MethodReference baseMr;
|
||||
TypeReference baseTr = td.BaseType;
|
||||
if (baseTr.CachedResolve(session).HasGenericParameters)
|
||||
{
|
||||
GenericInstanceType git = (GenericInstanceType)baseTr;
|
||||
baseMr = new MethodReference(baseMd.Name, baseMd.ReturnType, git)
|
||||
{
|
||||
HasThis = baseMd.HasThis,
|
||||
CallingConvention = baseMd.CallingConvention,
|
||||
ExplicitThis = baseMd.ExplicitThis,
|
||||
};
|
||||
foreach (ParameterDefinition pd in baseMd.Parameters)
|
||||
{
|
||||
session.ImportReference(pd.ParameterType);
|
||||
baseMr.Parameters.Add(pd);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
baseMr = session.ImportReference(baseMd);
|
||||
}
|
||||
|
||||
return baseMr;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a method in the next base class.
|
||||
/// </summary>
|
||||
public static MethodDefinition GetMethodDefinitionInBase(this TypeDefinition td, CodegenSession session, string methodName)
|
||||
{
|
||||
if (td.BaseType == null)
|
||||
{
|
||||
session.LogError($"BaseType for {td.FullName} is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
TypeDefinition baseTd = td.BaseType.CachedResolve(session);
|
||||
return baseTd.GetMethod(methodName);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns a method in the next base class.
|
||||
/// </summary>
|
||||
public static MethodReference GetMethodReference(this TypeDefinition td, CodegenSession session, string methodName)
|
||||
{
|
||||
MethodDefinition md = td.GetMethod(methodName);
|
||||
//Not found.
|
||||
if (md == null)
|
||||
return null;
|
||||
|
||||
return md.GetMethodReference(session);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a MethodReference or creates one if missing.
|
||||
/// </summary>
|
||||
public static MethodReference GetOrCreateMethodReference(this TypeDefinition td, CodegenSession session, string methodName, MethodAttributes attributes, TypeReference returnType, out bool created)
|
||||
{
|
||||
MethodDefinition md = td.GetMethod(methodName);
|
||||
//Not found.
|
||||
if (md == null)
|
||||
{
|
||||
md = new MethodDefinition(methodName, attributes, returnType);
|
||||
td.Methods.Add(md);
|
||||
created = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
created = false;
|
||||
}
|
||||
|
||||
return md.GetMethodReference(session);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a MethodDefinition or creates one if missing.
|
||||
/// </summary>
|
||||
public static MethodDefinition GetOrCreateMethodDefinition(this TypeDefinition td, CodegenSession session, string methodName, MethodAttributes attributes, TypeReference returnType, out bool created)
|
||||
{
|
||||
MethodDefinition md = td.GetMethod(methodName);
|
||||
//Not found.
|
||||
if (md == null)
|
||||
{
|
||||
md = new MethodDefinition(methodName, attributes, returnType);
|
||||
td.Methods.Add(md);
|
||||
created = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
created = false;
|
||||
}
|
||||
|
||||
return md;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a MethodDefinition or creates one if missing.
|
||||
/// </summary>
|
||||
public static MethodDefinition GetOrCreateMethodDefinition(this TypeDefinition td, CodegenSession session, string methodName, MethodDefinition methodTemplate, bool copyParameters, out bool created)
|
||||
{
|
||||
MethodDefinition md = td.GetMethod(methodName);
|
||||
//Not found.
|
||||
if (md == null)
|
||||
{
|
||||
TypeReference returnType = session.ImportReference(methodTemplate.ReturnType);
|
||||
md = new MethodDefinition(methodName, methodTemplate.Attributes, returnType)
|
||||
{
|
||||
ExplicitThis = methodTemplate.ExplicitThis,
|
||||
AggressiveInlining = methodTemplate.AggressiveInlining,
|
||||
Attributes = methodTemplate.Attributes,
|
||||
CallingConvention = methodTemplate.CallingConvention,
|
||||
HasThis = methodTemplate.HasThis,
|
||||
};
|
||||
md.Body.InitLocals = methodTemplate.Body.InitLocals;
|
||||
|
||||
if (copyParameters)
|
||||
{
|
||||
foreach (ParameterDefinition pd in methodTemplate.Parameters)
|
||||
md.Parameters.Add(pd);
|
||||
}
|
||||
|
||||
td.Methods.Add(md);
|
||||
created = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
created = false;
|
||||
}
|
||||
|
||||
return md;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns a method in any inherited classes. The first found method is returned.
|
||||
/// </summary>
|
||||
public static MethodDefinition GetMethodDefinitionInAnyBase(this TypeDefinition td, CodegenSession session, string methodName)
|
||||
{
|
||||
while (td != null)
|
||||
{
|
||||
foreach (MethodDefinition md in td.Methods)
|
||||
{
|
||||
if (md.Name == methodName)
|
||||
return md;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
td = td.GetNextBaseTypeDefinition(session);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the next base type.
|
||||
/// </summary>
|
||||
public static TypeDefinition GetNextBaseTypeDefinition(this TypeDefinition typeDef, CodegenSession session)
|
||||
{
|
||||
return (typeDef.BaseType == null) ? null : typeDef.BaseType.CachedResolve(session);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a FieldReference.
|
||||
/// </summary>
|
||||
public static FieldReference CreateFieldReference(this FieldDefinition fd, CodegenSession session)
|
||||
{
|
||||
FieldReference fr;
|
||||
TypeDefinition declaringType = fd.DeclaringType;
|
||||
//Is generic.
|
||||
if (declaringType.HasGenericParameters)
|
||||
{
|
||||
GenericInstanceType git = new GenericInstanceType(declaringType);
|
||||
foreach (GenericParameter item in declaringType.GenericParameters)
|
||||
git.GenericArguments.Add(item);
|
||||
fr = new FieldReference(fd.Name, fd.FieldType, git);
|
||||
return fr;
|
||||
}
|
||||
//Not generic.
|
||||
else
|
||||
{
|
||||
return session.ImportReference(fd);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a FieldReference or creates it if missing.
|
||||
/// </summary>
|
||||
public static FieldReference GetOrCreateFieldReference(this TypeDefinition td, CodegenSession session, string fieldName, FieldAttributes attributes, TypeReference fieldTypeRef, out bool created)
|
||||
{
|
||||
FieldReference fr = td.GetFieldReference(fieldName, session);
|
||||
if (fr == null)
|
||||
{
|
||||
fr = td.CreateFieldDefinition(session, fieldName, attributes, fieldTypeRef);
|
||||
created = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
created = false;
|
||||
}
|
||||
|
||||
return fr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a FieldReference.
|
||||
/// </summary>
|
||||
public static FieldReference CreateFieldDefinition(this TypeDefinition td, CodegenSession session, string fieldName, FieldAttributes attributes, TypeReference fieldTypeRef)
|
||||
{
|
||||
FieldDefinition fd = new FieldDefinition(fieldName, attributes, fieldTypeRef);
|
||||
td.Fields.Add(fd);
|
||||
return fd.CreateFieldReference(session);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9f00cf3dc8b90b469c3c9cb8b87fc88
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,59 @@
|
||||
|
||||
using FishNet.CodeGenerating.Helping;
|
||||
using FishNet.CodeGenerating.Helping.Extension;
|
||||
using MonoFN.Cecil;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.CodeGenerating.Extension
|
||||
{
|
||||
|
||||
|
||||
internal static class TypeReferenceExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Returns if a TypeReference is nullable.
|
||||
/// </summary>
|
||||
public static bool IsNullable(this TypeReference tr, CodegenSession session)
|
||||
{
|
||||
TypeDefinition td = tr.CachedResolve(session);
|
||||
return td.IsNullable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the fullname of a TypeReference without <>.
|
||||
/// </summary>
|
||||
/// <param name="tr"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFullnameWithoutBrackets(this TypeReference tr)
|
||||
{
|
||||
string str = tr.FullName;
|
||||
str = str.Replace("<", "");
|
||||
str = str.Replace(">", "");
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a method in the next base class.
|
||||
/// </summary>
|
||||
public static MethodReference GetMethodInBase(this TypeReference tr, CodegenSession session, string methodName)
|
||||
{
|
||||
return tr.CachedResolve(session).GetMethodInBase(session, methodName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes a GenericInstanceType.
|
||||
/// </summary>
|
||||
public static GenericInstanceType MakeGenericInstanceType(this TypeReference self)
|
||||
{
|
||||
GenericInstanceType instance = new GenericInstanceType(self);
|
||||
foreach (GenericParameter argument in self.GenericParameters)
|
||||
instance.GenericArguments.Add(argument);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 239b1b10db80c594d93b7ba4ee2c1ec5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user