[Add] FishNet
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4929a85ce2b13ee4799e6f2a768bd5e9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using System;
|
||||
|
||||
// [assembly: AssemblyTitle ("MonoFN.Cecil.Rocks")]
|
||||
[assembly: CLSCompliant(false)]
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9ceb0221cb78b247b676d0ea5d57fc8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/AssemblyInfo.cs
|
||||
uploadId: 866910
|
||||
@@ -0,0 +1,269 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
public class DocCommentId
|
||||
{
|
||||
private StringBuilder id;
|
||||
|
||||
private DocCommentId()
|
||||
{
|
||||
id = new();
|
||||
}
|
||||
|
||||
private void WriteField(FieldDefinition field)
|
||||
{
|
||||
WriteDefinition('F', field);
|
||||
}
|
||||
|
||||
private void WriteEvent(EventDefinition @event)
|
||||
{
|
||||
WriteDefinition('E', @event);
|
||||
}
|
||||
|
||||
private void WriteType(TypeDefinition type)
|
||||
{
|
||||
id.Append('T').Append(':');
|
||||
WriteTypeFullName(type);
|
||||
}
|
||||
|
||||
private void WriteMethod(MethodDefinition method)
|
||||
{
|
||||
WriteDefinition('M', method);
|
||||
|
||||
if (method.HasGenericParameters)
|
||||
{
|
||||
id.Append('`').Append('`');
|
||||
id.Append(method.GenericParameters.Count);
|
||||
}
|
||||
|
||||
if (method.HasParameters)
|
||||
WriteParameters(method.Parameters);
|
||||
|
||||
if (IsConversionOperator(method))
|
||||
WriteReturnType(method);
|
||||
}
|
||||
|
||||
private static bool IsConversionOperator(MethodDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
return self.IsSpecialName && (self.Name == "op_Explicit" || self.Name == "op_Implicit");
|
||||
}
|
||||
|
||||
private void WriteReturnType(MethodDefinition method)
|
||||
{
|
||||
id.Append('~');
|
||||
WriteTypeSignature(method.ReturnType);
|
||||
}
|
||||
|
||||
private void WriteProperty(PropertyDefinition property)
|
||||
{
|
||||
WriteDefinition('P', property);
|
||||
|
||||
if (property.HasParameters)
|
||||
WriteParameters(property.Parameters);
|
||||
}
|
||||
|
||||
private void WriteParameters(IList<ParameterDefinition> parameters)
|
||||
{
|
||||
id.Append('(');
|
||||
WriteList(parameters, p => WriteTypeSignature(p.ParameterType));
|
||||
id.Append(')');
|
||||
}
|
||||
|
||||
private void WriteTypeSignature(TypeReference type)
|
||||
{
|
||||
switch (type.MetadataType)
|
||||
{
|
||||
case MetadataType.Array:
|
||||
WriteArrayTypeSignature((ArrayType)type);
|
||||
break;
|
||||
case MetadataType.ByReference:
|
||||
WriteTypeSignature(((ByReferenceType)type).ElementType);
|
||||
id.Append('@');
|
||||
break;
|
||||
case MetadataType.FunctionPointer:
|
||||
WriteFunctionPointerTypeSignature((FunctionPointerType)type);
|
||||
break;
|
||||
case MetadataType.GenericInstance:
|
||||
WriteGenericInstanceTypeSignature((GenericInstanceType)type);
|
||||
break;
|
||||
case MetadataType.Var:
|
||||
id.Append('`');
|
||||
id.Append(((GenericParameter)type).Position);
|
||||
break;
|
||||
case MetadataType.MVar:
|
||||
id.Append('`').Append('`');
|
||||
id.Append(((GenericParameter)type).Position);
|
||||
break;
|
||||
case MetadataType.OptionalModifier:
|
||||
WriteModiferTypeSignature((OptionalModifierType)type, '!');
|
||||
break;
|
||||
case MetadataType.RequiredModifier:
|
||||
WriteModiferTypeSignature((RequiredModifierType)type, '|');
|
||||
break;
|
||||
case MetadataType.Pointer:
|
||||
WriteTypeSignature(((PointerType)type).ElementType);
|
||||
id.Append('*');
|
||||
break;
|
||||
default:
|
||||
WriteTypeFullName(type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteGenericInstanceTypeSignature(GenericInstanceType type)
|
||||
{
|
||||
if (type.ElementType.IsTypeSpecification())
|
||||
throw new NotSupportedException();
|
||||
|
||||
WriteTypeFullName(type.ElementType, stripGenericArity: true);
|
||||
id.Append('{');
|
||||
WriteList(type.GenericArguments, WriteTypeSignature);
|
||||
id.Append('}');
|
||||
}
|
||||
|
||||
private void WriteList<T>(IList<T> list, Action<T> action)
|
||||
{
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
id.Append(',');
|
||||
|
||||
action(list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteModiferTypeSignature(IModifierType type, char id)
|
||||
{
|
||||
WriteTypeSignature(type.ElementType);
|
||||
this.id.Append(id);
|
||||
WriteTypeSignature(type.ModifierType);
|
||||
}
|
||||
|
||||
private void WriteFunctionPointerTypeSignature(FunctionPointerType type)
|
||||
{
|
||||
id.Append("=FUNC:");
|
||||
WriteTypeSignature(type.ReturnType);
|
||||
|
||||
if (type.HasParameters)
|
||||
WriteParameters(type.Parameters);
|
||||
}
|
||||
|
||||
private void WriteArrayTypeSignature(ArrayType type)
|
||||
{
|
||||
WriteTypeSignature(type.ElementType);
|
||||
|
||||
if (type.IsVector)
|
||||
{
|
||||
id.Append("[]");
|
||||
return;
|
||||
}
|
||||
|
||||
id.Append("[");
|
||||
|
||||
WriteList(type.Dimensions, dimension =>
|
||||
{
|
||||
if (dimension.LowerBound.HasValue)
|
||||
id.Append(dimension.LowerBound.Value);
|
||||
|
||||
id.Append(':');
|
||||
|
||||
if (dimension.UpperBound.HasValue)
|
||||
id.Append(dimension.UpperBound.Value - (dimension.LowerBound.GetValueOrDefault() + 1));
|
||||
});
|
||||
|
||||
id.Append("]");
|
||||
}
|
||||
|
||||
private void WriteDefinition(char id, IMemberDefinition member)
|
||||
{
|
||||
this.id.Append(id).Append(':');
|
||||
|
||||
WriteTypeFullName(member.DeclaringType);
|
||||
this.id.Append('.');
|
||||
WriteItemName(member.Name);
|
||||
}
|
||||
|
||||
private void WriteTypeFullName(TypeReference type, bool stripGenericArity = false)
|
||||
{
|
||||
if (type.DeclaringType != null)
|
||||
{
|
||||
WriteTypeFullName(type.DeclaringType);
|
||||
id.Append('.');
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(type.Namespace))
|
||||
{
|
||||
id.Append(type.Namespace);
|
||||
id.Append('.');
|
||||
}
|
||||
|
||||
string name = type.Name;
|
||||
|
||||
if (stripGenericArity)
|
||||
{
|
||||
int index = name.LastIndexOf('`');
|
||||
if (index > 0)
|
||||
name = name.Substring(0, index);
|
||||
}
|
||||
|
||||
id.Append(name);
|
||||
}
|
||||
|
||||
private void WriteItemName(string name)
|
||||
{
|
||||
id.Append(name.Replace('.', '#').Replace('<', '{').Replace('>', '}'));
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return id.ToString();
|
||||
}
|
||||
|
||||
public static string GetDocCommentId(IMemberDefinition member)
|
||||
{
|
||||
if (member == null)
|
||||
throw new ArgumentNullException("member");
|
||||
|
||||
DocCommentId documentId = new();
|
||||
|
||||
switch (member.MetadataToken.TokenType)
|
||||
{
|
||||
case TokenType.Field:
|
||||
documentId.WriteField((FieldDefinition)member);
|
||||
break;
|
||||
case TokenType.Method:
|
||||
documentId.WriteMethod((MethodDefinition)member);
|
||||
break;
|
||||
case TokenType.TypeDef:
|
||||
documentId.WriteType((TypeDefinition)member);
|
||||
break;
|
||||
case TokenType.Event:
|
||||
documentId.WriteEvent((EventDefinition)member);
|
||||
break;
|
||||
case TokenType.Property:
|
||||
documentId.WriteProperty((PropertyDefinition)member);
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException(member.FullName);
|
||||
}
|
||||
|
||||
return documentId.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 370e40931a1b9cf478e08e66fd2a9eac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/DocCommentId.cs
|
||||
uploadId: 866910
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
internal static class Functional
|
||||
{
|
||||
public static Func<A, R> Y<A, R>(Func<Func<A, R>, Func<A, R>> f)
|
||||
{
|
||||
Func<A, R> g = null;
|
||||
g = f(a => g(a));
|
||||
return g;
|
||||
}
|
||||
|
||||
public static IEnumerable<TSource> Prepend<TSource>(this IEnumerable<TSource> source, TSource element)
|
||||
{
|
||||
if (source == null)
|
||||
throw new ArgumentNullException("source");
|
||||
|
||||
return PrependIterator(source, element);
|
||||
}
|
||||
|
||||
private static IEnumerable<TSource> PrependIterator<TSource>(IEnumerable<TSource> source, TSource element)
|
||||
{
|
||||
yield return element;
|
||||
|
||||
foreach (TSource item in source)
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b481a942e0bdf8649b6b5b20db207190
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/Functional.cs
|
||||
uploadId: 866910
|
||||
@@ -0,0 +1,236 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using MonoFN.Cecil.Cil;
|
||||
using MonoFN.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
interface IILVisitor
|
||||
{
|
||||
void OnInlineNone(OpCode opcode);
|
||||
void OnInlineSByte(OpCode opcode, sbyte value);
|
||||
void OnInlineByte(OpCode opcode, byte value);
|
||||
void OnInlineInt32(OpCode opcode, int value);
|
||||
void OnInlineInt64(OpCode opcode, long value);
|
||||
void OnInlineSingle(OpCode opcode, float value);
|
||||
void OnInlineDouble(OpCode opcode, double value);
|
||||
void OnInlineString(OpCode opcode, string value);
|
||||
void OnInlineBranch(OpCode opcode, int offset);
|
||||
void OnInlineSwitch(OpCode opcode, int[] offsets);
|
||||
void OnInlineVariable(OpCode opcode, VariableDefinition variable);
|
||||
void OnInlineArgument(OpCode opcode, ParameterDefinition parameter);
|
||||
void OnInlineSignature(OpCode opcode, CallSite callSite);
|
||||
void OnInlineType(OpCode opcode, TypeReference type);
|
||||
void OnInlineField(OpCode opcode, FieldReference field);
|
||||
void OnInlineMethod(OpCode opcode, MethodReference method);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
static class ILParser
|
||||
{
|
||||
private class ParseContext
|
||||
{
|
||||
public CodeReader Code { get; set; }
|
||||
public int Position { get; set; }
|
||||
public MetadataReader Metadata { get; set; }
|
||||
public Collection<VariableDefinition> Variables { get; set; }
|
||||
public IILVisitor Visitor { get; set; }
|
||||
}
|
||||
|
||||
public static void Parse(MethodDefinition method, IILVisitor visitor)
|
||||
{
|
||||
if (method == null)
|
||||
throw new ArgumentNullException("method");
|
||||
if (visitor == null)
|
||||
throw new ArgumentNullException("visitor");
|
||||
if (!method.HasBody || !method.HasImage)
|
||||
throw new ArgumentException();
|
||||
|
||||
method.Module.Read(method, (m, _) =>
|
||||
{
|
||||
ParseMethod(m, visitor);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private static void ParseMethod(MethodDefinition method, IILVisitor visitor)
|
||||
{
|
||||
ParseContext context = CreateContext(method, visitor);
|
||||
CodeReader code = context.Code;
|
||||
|
||||
byte flags = code.ReadByte();
|
||||
|
||||
switch (flags & 0x3)
|
||||
{
|
||||
case 0x2: // tiny
|
||||
int code_size = flags >> 2;
|
||||
ParseCode(code_size, context);
|
||||
break;
|
||||
case 0x3: // fat
|
||||
code.Advance(-1);
|
||||
ParseFatMethod(context);
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
code.MoveBackTo(context.Position);
|
||||
}
|
||||
|
||||
private static ParseContext CreateContext(MethodDefinition method, IILVisitor visitor)
|
||||
{
|
||||
CodeReader code = method.Module.Read(method, (_, reader) => reader.code);
|
||||
int position = code.MoveTo(method);
|
||||
|
||||
return new()
|
||||
{
|
||||
Code = code,
|
||||
Position = position,
|
||||
Metadata = code.reader,
|
||||
Visitor = visitor
|
||||
};
|
||||
}
|
||||
|
||||
private static void ParseFatMethod(ParseContext context)
|
||||
{
|
||||
CodeReader code = context.Code;
|
||||
|
||||
code.Advance(4);
|
||||
int code_size = code.ReadInt32();
|
||||
MetadataToken local_var_token = code.ReadToken();
|
||||
|
||||
if (local_var_token != MetadataToken.Zero)
|
||||
context.Variables = code.ReadVariables(local_var_token);
|
||||
|
||||
ParseCode(code_size, context);
|
||||
}
|
||||
|
||||
private static void ParseCode(int code_size, ParseContext context)
|
||||
{
|
||||
CodeReader code = context.Code;
|
||||
MetadataReader metadata = context.Metadata;
|
||||
IILVisitor visitor = context.Visitor;
|
||||
|
||||
int start = code.Position;
|
||||
int end = start + code_size;
|
||||
|
||||
while (code.Position < end)
|
||||
{
|
||||
byte il_opcode = code.ReadByte();
|
||||
OpCode opcode = il_opcode != 0xfe ? OpCodes.OneByteOpCode[il_opcode] : OpCodes.TwoBytesOpCode[code.ReadByte()];
|
||||
|
||||
switch (opcode.OperandType)
|
||||
{
|
||||
case OperandType.InlineNone:
|
||||
visitor.OnInlineNone(opcode);
|
||||
break;
|
||||
case OperandType.InlineSwitch:
|
||||
int length = code.ReadInt32();
|
||||
int[] branches = new int [length];
|
||||
for (int i = 0; i < length; i++)
|
||||
branches[i] = code.ReadInt32();
|
||||
visitor.OnInlineSwitch(opcode, branches);
|
||||
break;
|
||||
case OperandType.ShortInlineBrTarget:
|
||||
visitor.OnInlineBranch(opcode, code.ReadSByte());
|
||||
break;
|
||||
case OperandType.InlineBrTarget:
|
||||
visitor.OnInlineBranch(opcode, code.ReadInt32());
|
||||
break;
|
||||
case OperandType.ShortInlineI:
|
||||
if (opcode == OpCodes.Ldc_I4_S)
|
||||
visitor.OnInlineSByte(opcode, code.ReadSByte());
|
||||
else
|
||||
visitor.OnInlineByte(opcode, code.ReadByte());
|
||||
break;
|
||||
case OperandType.InlineI:
|
||||
visitor.OnInlineInt32(opcode, code.ReadInt32());
|
||||
break;
|
||||
case OperandType.InlineI8:
|
||||
visitor.OnInlineInt64(opcode, code.ReadInt64());
|
||||
break;
|
||||
case OperandType.ShortInlineR:
|
||||
visitor.OnInlineSingle(opcode, code.ReadSingle());
|
||||
break;
|
||||
case OperandType.InlineR:
|
||||
visitor.OnInlineDouble(opcode, code.ReadDouble());
|
||||
break;
|
||||
case OperandType.InlineSig:
|
||||
visitor.OnInlineSignature(opcode, code.GetCallSite(code.ReadToken()));
|
||||
break;
|
||||
case OperandType.InlineString:
|
||||
visitor.OnInlineString(opcode, code.GetString(code.ReadToken()));
|
||||
break;
|
||||
case OperandType.ShortInlineArg:
|
||||
visitor.OnInlineArgument(opcode, code.GetParameter(code.ReadByte()));
|
||||
break;
|
||||
case OperandType.InlineArg:
|
||||
visitor.OnInlineArgument(opcode, code.GetParameter(code.ReadInt16()));
|
||||
break;
|
||||
case OperandType.ShortInlineVar:
|
||||
visitor.OnInlineVariable(opcode, GetVariable(context, code.ReadByte()));
|
||||
break;
|
||||
case OperandType.InlineVar:
|
||||
visitor.OnInlineVariable(opcode, GetVariable(context, code.ReadInt16()));
|
||||
break;
|
||||
case OperandType.InlineTok:
|
||||
case OperandType.InlineField:
|
||||
case OperandType.InlineMethod:
|
||||
case OperandType.InlineType:
|
||||
IMetadataTokenProvider member = metadata.LookupToken(code.ReadToken());
|
||||
switch (member.MetadataToken.TokenType)
|
||||
{
|
||||
case TokenType.TypeDef:
|
||||
case TokenType.TypeRef:
|
||||
case TokenType.TypeSpec:
|
||||
visitor.OnInlineType(opcode, (TypeReference)member);
|
||||
break;
|
||||
case TokenType.Method:
|
||||
case TokenType.MethodSpec:
|
||||
visitor.OnInlineMethod(opcode, (MethodReference)member);
|
||||
break;
|
||||
case TokenType.Field:
|
||||
visitor.OnInlineField(opcode, (FieldReference)member);
|
||||
break;
|
||||
case TokenType.MemberRef:
|
||||
FieldReference field_ref = member as FieldReference;
|
||||
if (field_ref != null)
|
||||
{
|
||||
visitor.OnInlineField(opcode, field_ref);
|
||||
break;
|
||||
}
|
||||
|
||||
MethodReference method_ref = member as MethodReference;
|
||||
if (method_ref != null)
|
||||
{
|
||||
visitor.OnInlineMethod(opcode, method_ref);
|
||||
break;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static VariableDefinition GetVariable(ParseContext context, int index)
|
||||
{
|
||||
return context.Variables[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2916628c81c279046adb746612d6067b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/ILParser.cs
|
||||
uploadId: 866910
|
||||
@@ -0,0 +1,423 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using MonoFN.Cecil.Cil;
|
||||
using System;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
static class MethodBodyRocks
|
||||
{
|
||||
public static void SimplifyMacros(this MethodBody self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
foreach (Instruction instruction in self.Instructions)
|
||||
{
|
||||
if (instruction.OpCode.OpCodeType != OpCodeType.Macro)
|
||||
continue;
|
||||
|
||||
switch (instruction.OpCode.Code)
|
||||
{
|
||||
case Code.Ldarg_0:
|
||||
ExpandMacro(instruction, OpCodes.Ldarg, self.GetParameter(0));
|
||||
break;
|
||||
case Code.Ldarg_1:
|
||||
ExpandMacro(instruction, OpCodes.Ldarg, self.GetParameter(1));
|
||||
break;
|
||||
case Code.Ldarg_2:
|
||||
ExpandMacro(instruction, OpCodes.Ldarg, self.GetParameter(2));
|
||||
break;
|
||||
case Code.Ldarg_3:
|
||||
ExpandMacro(instruction, OpCodes.Ldarg, self.GetParameter(3));
|
||||
break;
|
||||
case Code.Ldloc_0:
|
||||
ExpandMacro(instruction, OpCodes.Ldloc, self.Variables[0]);
|
||||
break;
|
||||
case Code.Ldloc_1:
|
||||
ExpandMacro(instruction, OpCodes.Ldloc, self.Variables[1]);
|
||||
break;
|
||||
case Code.Ldloc_2:
|
||||
ExpandMacro(instruction, OpCodes.Ldloc, self.Variables[2]);
|
||||
break;
|
||||
case Code.Ldloc_3:
|
||||
ExpandMacro(instruction, OpCodes.Ldloc, self.Variables[3]);
|
||||
break;
|
||||
case Code.Stloc_0:
|
||||
ExpandMacro(instruction, OpCodes.Stloc, self.Variables[0]);
|
||||
break;
|
||||
case Code.Stloc_1:
|
||||
ExpandMacro(instruction, OpCodes.Stloc, self.Variables[1]);
|
||||
break;
|
||||
case Code.Stloc_2:
|
||||
ExpandMacro(instruction, OpCodes.Stloc, self.Variables[2]);
|
||||
break;
|
||||
case Code.Stloc_3:
|
||||
ExpandMacro(instruction, OpCodes.Stloc, self.Variables[3]);
|
||||
break;
|
||||
case Code.Ldarg_S:
|
||||
instruction.OpCode = OpCodes.Ldarg;
|
||||
break;
|
||||
case Code.Ldarga_S:
|
||||
instruction.OpCode = OpCodes.Ldarga;
|
||||
break;
|
||||
case Code.Starg_S:
|
||||
instruction.OpCode = OpCodes.Starg;
|
||||
break;
|
||||
case Code.Ldloc_S:
|
||||
instruction.OpCode = OpCodes.Ldloc;
|
||||
break;
|
||||
case Code.Ldloca_S:
|
||||
instruction.OpCode = OpCodes.Ldloca;
|
||||
break;
|
||||
case Code.Stloc_S:
|
||||
instruction.OpCode = OpCodes.Stloc;
|
||||
break;
|
||||
case Code.Ldc_I4_M1:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, -1);
|
||||
break;
|
||||
case Code.Ldc_I4_0:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 0);
|
||||
break;
|
||||
case Code.Ldc_I4_1:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 1);
|
||||
break;
|
||||
case Code.Ldc_I4_2:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 2);
|
||||
break;
|
||||
case Code.Ldc_I4_3:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 3);
|
||||
break;
|
||||
case Code.Ldc_I4_4:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 4);
|
||||
break;
|
||||
case Code.Ldc_I4_5:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 5);
|
||||
break;
|
||||
case Code.Ldc_I4_6:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 6);
|
||||
break;
|
||||
case Code.Ldc_I4_7:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 7);
|
||||
break;
|
||||
case Code.Ldc_I4_8:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, 8);
|
||||
break;
|
||||
case Code.Ldc_I4_S:
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, (int)(sbyte)instruction.Operand);
|
||||
break;
|
||||
case Code.Br_S:
|
||||
instruction.OpCode = OpCodes.Br;
|
||||
break;
|
||||
case Code.Brfalse_S:
|
||||
instruction.OpCode = OpCodes.Brfalse;
|
||||
break;
|
||||
case Code.Brtrue_S:
|
||||
instruction.OpCode = OpCodes.Brtrue;
|
||||
break;
|
||||
case Code.Beq_S:
|
||||
instruction.OpCode = OpCodes.Beq;
|
||||
break;
|
||||
case Code.Bge_S:
|
||||
instruction.OpCode = OpCodes.Bge;
|
||||
break;
|
||||
case Code.Bgt_S:
|
||||
instruction.OpCode = OpCodes.Bgt;
|
||||
break;
|
||||
case Code.Ble_S:
|
||||
instruction.OpCode = OpCodes.Ble;
|
||||
break;
|
||||
case Code.Blt_S:
|
||||
instruction.OpCode = OpCodes.Blt;
|
||||
break;
|
||||
case Code.Bne_Un_S:
|
||||
instruction.OpCode = OpCodes.Bne_Un;
|
||||
break;
|
||||
case Code.Bge_Un_S:
|
||||
instruction.OpCode = OpCodes.Bge_Un;
|
||||
break;
|
||||
case Code.Bgt_Un_S:
|
||||
instruction.OpCode = OpCodes.Bgt_Un;
|
||||
break;
|
||||
case Code.Ble_Un_S:
|
||||
instruction.OpCode = OpCodes.Ble_Un;
|
||||
break;
|
||||
case Code.Blt_Un_S:
|
||||
instruction.OpCode = OpCodes.Blt_Un;
|
||||
break;
|
||||
case Code.Leave_S:
|
||||
instruction.OpCode = OpCodes.Leave;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ExpandMacro(Instruction instruction, OpCode opcode, object operand)
|
||||
{
|
||||
instruction.OpCode = opcode;
|
||||
instruction.Operand = operand;
|
||||
}
|
||||
|
||||
private static void MakeMacro(Instruction instruction, OpCode opcode)
|
||||
{
|
||||
instruction.OpCode = opcode;
|
||||
instruction.Operand = null;
|
||||
}
|
||||
|
||||
public static void Optimize(this MethodBody self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
OptimizeLongs(self);
|
||||
OptimizeMacros(self);
|
||||
}
|
||||
|
||||
private static void OptimizeLongs(this MethodBody self)
|
||||
{
|
||||
for (int i = 0; i < self.Instructions.Count; i++)
|
||||
{
|
||||
Instruction instruction = self.Instructions[i];
|
||||
if (instruction.OpCode.Code != Code.Ldc_I8)
|
||||
continue;
|
||||
long l = (long)instruction.Operand;
|
||||
if (l >= int.MaxValue || l <= int.MinValue)
|
||||
continue;
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4, (int)l);
|
||||
self.Instructions.Insert(++i, Instruction.Create(OpCodes.Conv_I8));
|
||||
}
|
||||
}
|
||||
|
||||
public static void OptimizeMacros(this MethodBody self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
MethodDefinition method = self.Method;
|
||||
|
||||
foreach (Instruction instruction in self.Instructions)
|
||||
{
|
||||
int index;
|
||||
switch (instruction.OpCode.Code)
|
||||
{
|
||||
case Code.Ldarg:
|
||||
index = ((ParameterDefinition)instruction.Operand).Index;
|
||||
if (index == -1 && instruction.Operand == self.ThisParameter)
|
||||
index = 0;
|
||||
else if (method.HasThis)
|
||||
index++;
|
||||
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
MakeMacro(instruction, OpCodes.Ldarg_0);
|
||||
break;
|
||||
case 1:
|
||||
MakeMacro(instruction, OpCodes.Ldarg_1);
|
||||
break;
|
||||
case 2:
|
||||
MakeMacro(instruction, OpCodes.Ldarg_2);
|
||||
break;
|
||||
case 3:
|
||||
MakeMacro(instruction, OpCodes.Ldarg_3);
|
||||
break;
|
||||
default:
|
||||
if (index < 256)
|
||||
ExpandMacro(instruction, OpCodes.Ldarg_S, instruction.Operand);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Code.Ldloc:
|
||||
index = ((VariableDefinition)instruction.Operand).Index;
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
MakeMacro(instruction, OpCodes.Ldloc_0);
|
||||
break;
|
||||
case 1:
|
||||
MakeMacro(instruction, OpCodes.Ldloc_1);
|
||||
break;
|
||||
case 2:
|
||||
MakeMacro(instruction, OpCodes.Ldloc_2);
|
||||
break;
|
||||
case 3:
|
||||
MakeMacro(instruction, OpCodes.Ldloc_3);
|
||||
break;
|
||||
default:
|
||||
if (index < 256)
|
||||
ExpandMacro(instruction, OpCodes.Ldloc_S, instruction.Operand);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Code.Stloc:
|
||||
index = ((VariableDefinition)instruction.Operand).Index;
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
MakeMacro(instruction, OpCodes.Stloc_0);
|
||||
break;
|
||||
case 1:
|
||||
MakeMacro(instruction, OpCodes.Stloc_1);
|
||||
break;
|
||||
case 2:
|
||||
MakeMacro(instruction, OpCodes.Stloc_2);
|
||||
break;
|
||||
case 3:
|
||||
MakeMacro(instruction, OpCodes.Stloc_3);
|
||||
break;
|
||||
default:
|
||||
if (index < 256)
|
||||
ExpandMacro(instruction, OpCodes.Stloc_S, instruction.Operand);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Code.Ldarga:
|
||||
index = ((ParameterDefinition)instruction.Operand).Index;
|
||||
if (index == -1 && instruction.Operand == self.ThisParameter)
|
||||
index = 0;
|
||||
else if (method.HasThis)
|
||||
index++;
|
||||
if (index < 256)
|
||||
ExpandMacro(instruction, OpCodes.Ldarga_S, instruction.Operand);
|
||||
break;
|
||||
case Code.Ldloca:
|
||||
if (((VariableDefinition)instruction.Operand).Index < 256)
|
||||
ExpandMacro(instruction, OpCodes.Ldloca_S, instruction.Operand);
|
||||
break;
|
||||
case Code.Ldc_I4:
|
||||
int i = (int)instruction.Operand;
|
||||
switch (i)
|
||||
{
|
||||
case -1:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_M1);
|
||||
break;
|
||||
case 0:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_0);
|
||||
break;
|
||||
case 1:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_1);
|
||||
break;
|
||||
case 2:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_2);
|
||||
break;
|
||||
case 3:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_3);
|
||||
break;
|
||||
case 4:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_4);
|
||||
break;
|
||||
case 5:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_5);
|
||||
break;
|
||||
case 6:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_6);
|
||||
break;
|
||||
case 7:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_7);
|
||||
break;
|
||||
case 8:
|
||||
MakeMacro(instruction, OpCodes.Ldc_I4_8);
|
||||
break;
|
||||
default:
|
||||
if (i >= -128 && i < 128)
|
||||
ExpandMacro(instruction, OpCodes.Ldc_I4_S, (sbyte)i);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
OptimizeBranches(self);
|
||||
}
|
||||
|
||||
private static void OptimizeBranches(MethodBody body)
|
||||
{
|
||||
ComputeOffsets(body);
|
||||
|
||||
foreach (Instruction instruction in body.Instructions)
|
||||
{
|
||||
if (instruction.OpCode.OperandType != OperandType.InlineBrTarget)
|
||||
continue;
|
||||
|
||||
if (OptimizeBranch(instruction))
|
||||
ComputeOffsets(body);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool OptimizeBranch(Instruction instruction)
|
||||
{
|
||||
int offset = ((Instruction)instruction.Operand).Offset - (instruction.Offset + instruction.OpCode.Size + 4);
|
||||
if (!(offset >= -128 && offset <= 127))
|
||||
return false;
|
||||
|
||||
switch (instruction.OpCode.Code)
|
||||
{
|
||||
case Code.Br:
|
||||
instruction.OpCode = OpCodes.Br_S;
|
||||
break;
|
||||
case Code.Brfalse:
|
||||
instruction.OpCode = OpCodes.Brfalse_S;
|
||||
break;
|
||||
case Code.Brtrue:
|
||||
instruction.OpCode = OpCodes.Brtrue_S;
|
||||
break;
|
||||
case Code.Beq:
|
||||
instruction.OpCode = OpCodes.Beq_S;
|
||||
break;
|
||||
case Code.Bge:
|
||||
instruction.OpCode = OpCodes.Bge_S;
|
||||
break;
|
||||
case Code.Bgt:
|
||||
instruction.OpCode = OpCodes.Bgt_S;
|
||||
break;
|
||||
case Code.Ble:
|
||||
instruction.OpCode = OpCodes.Ble_S;
|
||||
break;
|
||||
case Code.Blt:
|
||||
instruction.OpCode = OpCodes.Blt_S;
|
||||
break;
|
||||
case Code.Bne_Un:
|
||||
instruction.OpCode = OpCodes.Bne_Un_S;
|
||||
break;
|
||||
case Code.Bge_Un:
|
||||
instruction.OpCode = OpCodes.Bge_Un_S;
|
||||
break;
|
||||
case Code.Bgt_Un:
|
||||
instruction.OpCode = OpCodes.Bgt_Un_S;
|
||||
break;
|
||||
case Code.Ble_Un:
|
||||
instruction.OpCode = OpCodes.Ble_Un_S;
|
||||
break;
|
||||
case Code.Blt_Un:
|
||||
instruction.OpCode = OpCodes.Blt_Un_S;
|
||||
break;
|
||||
case Code.Leave:
|
||||
instruction.OpCode = OpCodes.Leave_S;
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void ComputeOffsets(MethodBody body)
|
||||
{
|
||||
int offset = 0;
|
||||
foreach (Instruction instruction in body.Instructions)
|
||||
{
|
||||
instruction.Offset = offset;
|
||||
offset += instruction.GetSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c47d4756ed8120429bad0887cff0366
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/MethodBodyRocks.cs
|
||||
uploadId: 866910
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using System;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
static class MethodDefinitionRocks
|
||||
{
|
||||
public static MethodDefinition GetBaseMethod(this MethodDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
if (!self.IsVirtual)
|
||||
return self;
|
||||
if (self.IsNewSlot)
|
||||
return self;
|
||||
|
||||
TypeDefinition base_type = ResolveBaseType(self.DeclaringType);
|
||||
while (base_type != null)
|
||||
{
|
||||
MethodDefinition @base = GetMatchingMethod(base_type, self);
|
||||
if (@base != null)
|
||||
return @base;
|
||||
|
||||
base_type = ResolveBaseType(base_type);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
public static MethodDefinition GetOriginalBaseMethod(this MethodDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
while (true)
|
||||
{
|
||||
MethodDefinition @base = self.GetBaseMethod();
|
||||
if (@base == self)
|
||||
return self;
|
||||
|
||||
self = @base;
|
||||
}
|
||||
}
|
||||
|
||||
private static TypeDefinition ResolveBaseType(TypeDefinition type)
|
||||
{
|
||||
if (type == null)
|
||||
return null;
|
||||
|
||||
TypeReference base_type = type.BaseType;
|
||||
if (base_type == null)
|
||||
return null;
|
||||
|
||||
return base_type.Resolve();
|
||||
}
|
||||
|
||||
private static MethodDefinition GetMatchingMethod(TypeDefinition type, MethodDefinition method)
|
||||
{
|
||||
return MetadataResolver.GetMethod(type.Methods, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a5d7596633ddb043b9785a7fa7bfa6f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/MethodDefinitionRocks.cs
|
||||
uploadId: 866910
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
static class ModuleDefinitionRocks
|
||||
{
|
||||
public static IEnumerable<TypeDefinition> GetAllTypes(this ModuleDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
// it was fun to write, but we need a somewhat less convoluted implementation
|
||||
return self.Types.SelectMany(Functional.Y<TypeDefinition, IEnumerable<TypeDefinition>>(f => type => type.NestedTypes.SelectMany(f).Prepend(type)));
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d90334a1cd085c047b9b8eddaa65cf09
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/ModuleDefinitionRocks.cs
|
||||
uploadId: 866910
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
public static class ParameterReferenceRocks
|
||||
{
|
||||
public static int GetSequence(this ParameterReference self)
|
||||
{
|
||||
return self.Index + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d276fe9256961b847b51a9bfc735384b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/ParameterReferenceRocks.cs
|
||||
uploadId: 866910
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
#if !NET_CORE
|
||||
using System;
|
||||
using System.Security;
|
||||
using System.Security.Permissions;
|
||||
using SSP = System.Security.Permissions;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
static class SecurityDeclarationRocks
|
||||
{
|
||||
public static PermissionSet ToPermissionSet(this SecurityDeclaration self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
PermissionSet set;
|
||||
if (TryProcessPermissionSetAttribute(self, out set))
|
||||
return set;
|
||||
|
||||
return CreatePermissionSet(self);
|
||||
}
|
||||
|
||||
private static bool TryProcessPermissionSetAttribute(SecurityDeclaration declaration, out PermissionSet set)
|
||||
{
|
||||
set = null;
|
||||
|
||||
if (!declaration.HasSecurityAttributes && declaration.SecurityAttributes.Count != 1)
|
||||
return false;
|
||||
|
||||
SecurityAttribute security_attribute = declaration.SecurityAttributes[0];
|
||||
if (!security_attribute.AttributeType.IsTypeOf("System.Security.Permissions", "PermissionSetAttribute"))
|
||||
return false;
|
||||
|
||||
PermissionSetAttribute attribute = new((SSP.SecurityAction)declaration.Action);
|
||||
|
||||
CustomAttributeNamedArgument named_argument = security_attribute.Properties[0];
|
||||
string value = (string)named_argument.Argument.Value;
|
||||
switch (named_argument.Name)
|
||||
{
|
||||
case "XML":
|
||||
attribute.XML = value;
|
||||
break;
|
||||
case "Name":
|
||||
attribute.Name = value;
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException(named_argument.Name);
|
||||
}
|
||||
|
||||
set = attribute.CreatePermissionSet();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static PermissionSet CreatePermissionSet(SecurityDeclaration declaration)
|
||||
{
|
||||
PermissionSet set = new(SSP.PermissionState.None);
|
||||
|
||||
foreach (SecurityAttribute attribute in declaration.SecurityAttributes)
|
||||
{
|
||||
IPermission permission = CreatePermission(declaration, attribute);
|
||||
set.AddPermission(permission);
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
private static IPermission CreatePermission(SecurityDeclaration declaration, SecurityAttribute attribute)
|
||||
{
|
||||
Type attribute_type = Type.GetType(attribute.AttributeType.FullName);
|
||||
if (attribute_type == null)
|
||||
throw new ArgumentException("attribute");
|
||||
|
||||
System.Security.Permissions.SecurityAttribute security_attribute = CreateSecurityAttribute(attribute_type, declaration);
|
||||
if (security_attribute == null)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
CompleteSecurityAttribute(security_attribute, attribute);
|
||||
|
||||
return security_attribute.CreatePermission();
|
||||
}
|
||||
|
||||
private static void CompleteSecurityAttribute(SSP.SecurityAttribute security_attribute, SecurityAttribute attribute)
|
||||
{
|
||||
if (attribute.HasFields)
|
||||
CompleteSecurityAttributeFields(security_attribute, attribute);
|
||||
|
||||
if (attribute.HasProperties)
|
||||
CompleteSecurityAttributeProperties(security_attribute, attribute);
|
||||
}
|
||||
|
||||
private static void CompleteSecurityAttributeFields(SSP.SecurityAttribute security_attribute, SecurityAttribute attribute)
|
||||
{
|
||||
Type type = security_attribute.GetType();
|
||||
|
||||
foreach (CustomAttributeNamedArgument named_argument in attribute.Fields)
|
||||
type.GetField(named_argument.Name).SetValue(security_attribute, named_argument.Argument.Value);
|
||||
}
|
||||
|
||||
private static void CompleteSecurityAttributeProperties(SSP.SecurityAttribute security_attribute, SecurityAttribute attribute)
|
||||
{
|
||||
Type type = security_attribute.GetType();
|
||||
|
||||
foreach (CustomAttributeNamedArgument named_argument in attribute.Properties)
|
||||
type.GetProperty(named_argument.Name).SetValue(security_attribute, named_argument.Argument.Value, null);
|
||||
}
|
||||
|
||||
private static SSP.SecurityAttribute CreateSecurityAttribute(Type attribute_type, SecurityDeclaration declaration)
|
||||
{
|
||||
SSP.SecurityAttribute security_attribute;
|
||||
try
|
||||
{
|
||||
security_attribute = (SSP.SecurityAttribute)Activator.CreateInstance(attribute_type, new object[] { (SSP.SecurityAction)declaration.Action });
|
||||
}
|
||||
catch (MissingMethodException)
|
||||
{
|
||||
security_attribute = (SSP.SecurityAttribute)Activator.CreateInstance(attribute_type, new object [0]);
|
||||
}
|
||||
|
||||
return security_attribute;
|
||||
}
|
||||
|
||||
public static SecurityDeclaration ToSecurityDeclaration(this PermissionSet self, SecurityAction action, ModuleDefinition module)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
if (module == null)
|
||||
throw new ArgumentNullException("module");
|
||||
|
||||
SecurityDeclaration declaration = new(action);
|
||||
|
||||
SecurityAttribute attribute = new(module.TypeSystem.LookupType("System.Security.Permissions", "PermissionSetAttribute"));
|
||||
|
||||
attribute.Properties.Add(new("XML", new(module.TypeSystem.String, self.ToXml().ToString())));
|
||||
|
||||
declaration.SecurityAttributes.Add(attribute);
|
||||
|
||||
return declaration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9d89d937b712004089aafb3a1391907
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/SecurityDeclarationRocks.cs
|
||||
uploadId: 866910
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
static class TypeDefinitionRocks
|
||||
{
|
||||
public static IEnumerable<MethodDefinition> GetConstructors(this TypeDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
if (!self.HasMethods)
|
||||
return Empty<MethodDefinition>.Array;
|
||||
|
||||
return self.Methods.Where(method => method.IsConstructor);
|
||||
}
|
||||
|
||||
public static MethodDefinition GetStaticConstructor(this TypeDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
if (!self.HasMethods)
|
||||
return null;
|
||||
|
||||
return self.GetConstructors().FirstOrDefault(ctor => ctor.IsStatic);
|
||||
}
|
||||
|
||||
public static IEnumerable<MethodDefinition> GetMethods(this TypeDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
|
||||
if (!self.HasMethods)
|
||||
return Empty<MethodDefinition>.Array;
|
||||
|
||||
return self.Methods.Where(method => !method.IsConstructor);
|
||||
}
|
||||
|
||||
public static TypeReference GetEnumUnderlyingType(this TypeDefinition self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
if (!self.IsEnum)
|
||||
throw new ArgumentException();
|
||||
|
||||
return Mixin.GetEnumUnderlyingType(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4d5393ac3307a84ca5babfbdcad9eea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/TypeDefinitionRocks.cs
|
||||
uploadId: 866910
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2015 Jb Evain
|
||||
// Copyright (c) 2008 - 2011 Novell, Inc.
|
||||
//
|
||||
// Licensed under the MIT/X11 license.
|
||||
//
|
||||
|
||||
using System;
|
||||
|
||||
namespace MonoFN.Cecil.Rocks
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
public
|
||||
#endif
|
||||
static class TypeReferenceRocks
|
||||
{
|
||||
public static ArrayType MakeArrayType(this TypeReference self)
|
||||
{
|
||||
return new(self);
|
||||
}
|
||||
|
||||
public static ArrayType MakeArrayType(this TypeReference self, int rank)
|
||||
{
|
||||
if (rank == 0)
|
||||
throw new ArgumentOutOfRangeException("rank");
|
||||
|
||||
ArrayType array = new(self);
|
||||
|
||||
for (int i = 1; i < rank; i++)
|
||||
array.Dimensions.Add(new());
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
public static PointerType MakePointerType(this TypeReference self)
|
||||
{
|
||||
return new(self);
|
||||
}
|
||||
|
||||
public static ByReferenceType MakeByReferenceType(this TypeReference self)
|
||||
{
|
||||
return new(self);
|
||||
}
|
||||
|
||||
public static OptionalModifierType MakeOptionalModifierType(this TypeReference self, TypeReference modifierType)
|
||||
{
|
||||
return new(modifierType, self);
|
||||
}
|
||||
|
||||
public static RequiredModifierType MakeRequiredModifierType(this TypeReference self, TypeReference modifierType)
|
||||
{
|
||||
return new(modifierType, self);
|
||||
}
|
||||
|
||||
public static GenericInstanceType MakeGenericInstanceType(this TypeReference self, params TypeReference[] arguments)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException("self");
|
||||
if (arguments == null)
|
||||
throw new ArgumentNullException("arguments");
|
||||
if (arguments.Length == 0)
|
||||
throw new ArgumentException();
|
||||
if (self.GenericParameters.Count != arguments.Length)
|
||||
throw new ArgumentException();
|
||||
|
||||
GenericInstanceType instance = new(self, arguments.Length);
|
||||
|
||||
foreach (TypeReference argument in arguments)
|
||||
instance.GenericArguments.Add(argument);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static PinnedType MakePinnedType(this TypeReference self)
|
||||
{
|
||||
return new(self);
|
||||
}
|
||||
|
||||
public static SentinelType MakeSentinelType(this TypeReference self)
|
||||
{
|
||||
return new(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25d089a650c8e0f45a6f9086aaf0004a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 207815
|
||||
packageName: 'FishNet: Networking Evolved'
|
||||
packageVersion: 4.6.22R
|
||||
assetPath: Assets/FishNet/CodeGenerating/cecil-0.11.4/rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs
|
||||
uploadId: 866910
|
||||
Reference in New Issue
Block a user