예제 #1
0
        public void GenericForwardBaseType(ModuleDefinition module)
        {
            var tamchild = module.GetType ("TamChild");

            Assert.IsNotNull (tamchild);
            Assert.IsNotNull (tamchild.BaseType);

            var generic_instance = tamchild.BaseType as GenericInstanceType;

            Assert.IsNotNull (generic_instance);

            Assert.AreEqual (1, generic_instance.GenericArguments.Count);
            Assert.AreEqual (module.GetType ("Tamtam"), generic_instance.GenericArguments [0]);
        }
예제 #2
0
		public void TypeLayout (ModuleDefinition module)
		{
			var foo = module.GetType ("Foo");
			Assert.IsNotNull (foo);
			Assert.IsTrue (foo.IsValueType);

			Assert.IsTrue (foo.HasLayoutInfo);
			Assert.AreEqual (16, foo.ClassSize);

			var babar = module.GetType ("Babar");
			Assert.IsNotNull (babar);
			Assert.IsFalse (babar.IsValueType);
			Assert.IsFalse (babar.HasLayoutInfo);
		}
		public void ArrayMarshaler (ModuleDefinition module)
		{
			var bar = module.GetType ("Bar");
			var pan = bar.GetMethod ("PanPan");

			var parameter = pan.Parameters [1];

			Assert.IsTrue (parameter.HasMarshalInfo);

			var info = (ArrayMarshalInfo) parameter.MarshalInfo;

			Assert.AreEqual (NativeType.I8, info.ElementType);
			Assert.AreEqual (66, info.Size);
			Assert.AreEqual (2, info.SizeParameterIndex);

			parameter = pan.Parameters [3];

			Assert.IsTrue (parameter.HasMarshalInfo);

			info = (ArrayMarshalInfo) parameter.MarshalInfo;

			Assert.AreEqual (NativeType.I2, info.ElementType);
			Assert.AreEqual (-1, info.Size);
			Assert.AreEqual (-1, info.SizeParameterIndex);
		}
예제 #4
0
		public void OtherMethod (ModuleDefinition module)
		{
			var type = module.GetType ("Others");

			Assert.IsTrue (type.HasEvents);

			var events = type.Events;

			Assert.AreEqual (1, events.Count);

			var @event = events [0];

			Assert.IsNotNull (@event);
			Assert.AreEqual ("Handler", @event.Name);
			Assert.IsNotNull (@event.EventType);
			Assert.AreEqual ("System.EventHandler", @event.EventType.FullName);

			Assert.IsTrue (@event.HasOtherMethods);

			Assert.AreEqual (2, @event.OtherMethods.Count);

			var other = @event.OtherMethods [0];
			Assert.AreEqual ("dang_Handler", other.Name);

			other = @event.OtherMethods [1];
			Assert.AreEqual ("fang_Handler", other.Name);
		}
예제 #5
0
        public void ArrayFields(ModuleDefinition module)
        {
            var types = module.GetType ("Types");
            Assert.IsNotNull (types);

            var rank_two = types.GetField ("rank_two");

            var array = rank_two.FieldType as ArrayType;
            Assert.IsNotNull (array);

            Assert.AreEqual (2, array.Rank);
            Assert.IsFalse (array.Dimensions [0].IsSized);
            Assert.IsFalse (array.Dimensions [1].IsSized);

            var rank_two_low_bound_zero = types.GetField ("rank_two_low_bound_zero");

            array = rank_two_low_bound_zero.FieldType as ArrayType;
            Assert.IsNotNull (array);

            Assert.AreEqual (2, array.Rank);
            Assert.IsTrue (array.Dimensions [0].IsSized);
            Assert.AreEqual (0, array.Dimensions [0].LowerBound);
            Assert.AreEqual (null, array.Dimensions [0].UpperBound);
            Assert.IsTrue (array.Dimensions [1].IsSized);
            Assert.AreEqual (0, array.Dimensions [1].LowerBound);
            Assert.AreEqual (null, array.Dimensions [1].UpperBound);
        }
 public override void InjectInto(ModuleDefinition gamedef, ModuleDefinition moddef)
 {
     // Expose some things in AnimalManager for easier use.
     TypeDefinition type = gamedef.GetType("SDG.Unturned.AnimalManager");
     MakePublic(GetMethod(type, "addAnimal", 4));
     MakePublic(GetField(type, "manager"));
 }
예제 #7
0
 public AssemblyUpdater(string assemblyFile)
 {
     _assemblyFile = assemblyFile;
     _module = ModuleDefinition.ReadModule(assemblyFile);
     _type = _module.GetType("TheIndex", "EntryPoint");
     _resolveUpdater = new AssemblyResolveUpdater(_module);
 }
예제 #8
0
파일: FieldTests.cs 프로젝트: jm3105/cecil
        public void ConstantCoalescing(ModuleDefinition module)
        {
            var fields = module.GetType ("Fields");

            var field = fields.GetField ("int32_int16");
            Assert.AreEqual ("System.Int32", field.FieldType.FullName);
            Assert.IsTrue (field.HasConstant);
            Assert.IsInstanceOfType (typeof (short), field.Constant);
            Assert.AreEqual ((short) 1, field.Constant);

            field = fields.GetField ("int16_int32");
            Assert.AreEqual ("System.Int16", field.FieldType.FullName);
            Assert.IsTrue (field.HasConstant);
            Assert.IsInstanceOfType (typeof (int), field.Constant);
            Assert.AreEqual (1, field.Constant);

            field = fields.GetField ("char_int16");
            Assert.AreEqual ("System.Char", field.FieldType.FullName);
            Assert.IsTrue (field.HasConstant);
            Assert.IsInstanceOfType (typeof (short), field.Constant);
            Assert.AreEqual ((short) 1, field.Constant);

            field = fields.GetField ("int16_char");
            Assert.AreEqual ("System.Int16", field.FieldType.FullName);
            Assert.IsTrue (field.HasConstant);
            Assert.IsInstanceOfType (typeof (char), field.Constant);
            Assert.AreEqual ('s', field.Constant);
        }
예제 #9
0
		public void FieldLayout (ModuleDefinition module)
		{
			var foo = module.GetType ("Foo");
			Assert.IsNotNull (foo);

			Assert.IsTrue (foo.HasFields);

			var fields = foo.Fields;

			var field = fields [0];

			Assert.AreEqual ("Bar", field.Name);
			Assert.IsTrue (field.HasLayoutInfo);
			Assert.AreEqual (0, field.Offset);

			field = fields [1];

			Assert.AreEqual ("Baz", field.Name);
			Assert.IsTrue (field.HasLayoutInfo);
			Assert.AreEqual (2, field.Offset);

			field = fields [2];

			Assert.AreEqual ("Gazonk", field.Name);
			Assert.IsTrue (field.HasLayoutInfo);
			Assert.AreEqual (4, field.Offset);
		}
예제 #10
0
		public void ReturnGenericInstance (ModuleDefinition module)
		{
			var bar = module.GetType ("Bar`1");

			var self = bar.GetMethod ("Self");
			Assert.IsNotNull (self);

			var bar_t = self.ReturnType;

			Assert.IsTrue (bar_t.IsGenericInstance);

			var bar_t_instance = (GenericInstanceType) bar_t;

			Assert.AreEqual (bar.GenericParameters [0], bar_t_instance.GenericArguments [0]);

			var self_str = bar.GetMethod ("SelfString");
			Assert.IsNotNull (self_str);

			var bar_str = self_str.ReturnType;
			Assert.IsTrue (bar_str.IsGenericInstance);

			var bar_str_instance = (GenericInstanceType) bar_str;

			Assert.AreEqual ("System.String", bar_str_instance.GenericArguments [0].FullName);
		}
예제 #11
0
		public void DirectNestedType (ModuleDefinition module)
		{
			var bingo = module.GetType ("Bingo");
			var get_fuel = bingo.GetMethod ("GetFuel");

			Assert.AreEqual ("Bingo/Fuel", get_fuel.ReturnType.FullName);
		}
 public override void InjectInto(ModuleDefinition gamedef, ModuleDefinition moddef)
 {
     // This injects a call to the mod's static AnimalDamaged method for the AnimalDamagedScriptEvent
     TypeDefinition modtype = moddef.GetType("UnturnedFrenetic.UnturnedFreneticMod");
     MethodReference eventmethod = gamedef.ImportReference(GetMethod(modtype, "AnimalDamaged", 4));
     TypeDefinition animaltype = gamedef.GetType("SDG.Unturned.Animal");
     MethodDefinition damagemethod = GetMethod(animaltype, "askDamage", 4);
     MethodBody damagebody = damagemethod.Body;
     InjectInstructions(damagebody, 0, new Instruction[]
     {
             // Load "this" onto the stack.
             Instruction.Create(OpCodes.Ldarg_0),
             // Load "amount" onto the stack.
             Instruction.Create(OpCodes.Ldarga_S, damagemethod.Parameters[0]),
             // Load "newRagdoll" onto the stack.
             Instruction.Create(OpCodes.Ldarga_S, damagemethod.Parameters[1]),
             // Load "xp" onto the stack
             Instruction.Create(OpCodes.Ldarga_S, damagemethod.Parameters[3]),
             // Call the AnimalDamaged method with the above parameters and return a bool.
             Instruction.Create(OpCodes.Call, eventmethod),
             // If the return is false, jump ahead to the original 0th instruction.
             Instruction.Create(OpCodes.Brfalse, damagebody.Instructions[0]),
             // Otherwise, return now.
             Instruction.Create(OpCodes.Ret)
     });
 }
예제 #13
0
 public AssemblyResolveUpdater(ModuleDefinition module)
 {
     _module = module;
     var type = _module.GetType("TheIndex", "Resolver");
     _initMethod = type.Methods.Single(m => m.Name == "DictionaryInitialization");
     _addResolveMethod = type.Methods.Single(m => m.Name == "Add");
     _proc = _initMethod.Body.GetILProcessor();
 }
예제 #14
0
        public void FilterMaxStack(ModuleDefinition module)
        {
            var type = module.GetType ("Foo");
            var method = type.GetMethod ("TestFilter");

            Assert.IsNotNull (method);
            Assert.AreEqual (2, method.Body.MaxStackSize);
        }
예제 #15
0
        public override void Apply(ModuleDefinition module)
        {
            var type = module.GetType("Aura.Channel.Util.GmCommandManager");

            var proc = type.GetMethod("Process");

            proc.IsVirtual = true;
            proc.IsNewSlot = true;
        }
 public override void InjectInto(ModuleDefinition gamedef, ModuleDefinition moddef)
 {
     // Expose the "addZombie" method in ZombieManager for easier use.
     TypeDefinition type = gamedef.GetType("SDG.Unturned.ZombieManager");
     MethodDefinition method = GetMethod(type, "addZombie", 12);
     method.IsPrivate = false;
     method.IsPublic = true;
     FieldDefinition field = GetField(type, "manager");
     field.IsPrivate = false;
     field.IsPublic = true;
     TypeDefinition zombie = gamedef.GetType("SDG.Unturned.Zombie");
     FieldDefinition health = GetField(zombie, "health");
     health.IsPrivate = false;
     health.IsPublic = true;
     FieldDefinition maxHealth = GetField(zombie, "maxHealth");
     maxHealth.IsPrivate = false;
     maxHealth.IsPublic = true;
 }
예제 #17
0
        public void BoxedDefaultArgumentValue(ModuleDefinition module)
        {
            var foo = module.GetType ("Foo");
            var bar = foo.GetMethod ("Bar");
            var baz = bar.Parameters [0];

            Assert.IsTrue (baz.HasConstant);
            Assert.AreEqual (-1, baz.Constant);
        }
예제 #18
0
        public void ArrayConstant(ModuleDefinition module)
        {
            var panpan = module.GetType ("PanPan");
            Assert.IsNotNull (panpan);

            var field = panpan.GetField ("ints");
            Assert.IsTrue (field.HasConstant);
            Assert.IsNull (field.Constant);
        }
예제 #19
0
        private static MethodDefinition GetDelegateWrapper(ModuleDefinition module, TypeReference multicastDelegateType)
        {
            // Find DelegateWrappers type in assembly
            var delegateWrappersType = module.GetType("DelegateWrappers");
            if (delegateWrappersType == null)
                return null;

            // Find if any method match the requested type
            var multicastDelegateName = multicastDelegateType.MangledName();
            return delegateWrappersType.Methods.FirstOrDefault(x => x.Name == multicastDelegateName);
        }
 public override void InjectInto(ModuleDefinition gamedef, ModuleDefinition moddef)
 {
     // Expose the "manager" method in StructureManager for easier use.
     TypeDefinition type = gamedef.GetType("SDG.Unturned.StructureManager");
     FieldDefinition field = GetField(type, "manager");
     field.IsPrivate = false;
     field.IsPublic = true;
     FieldDefinition regions = GetField(type, "regions");
     regions.IsPrivate = false;
     regions.IsPublic = true;
 }
		public void SafeArrayMarshaler (ModuleDefinition module)
		{
			var bar = module.GetType ("Bar");
			var pan = bar.GetMethod ("PanPan");

			Assert.IsTrue (pan.MethodReturnType.HasMarshalInfo);

			var info = (SafeArrayMarshalInfo) pan.MethodReturnType.MarshalInfo;

			Assert.AreEqual (VariantType.Dispatch, info.ElementType);
		}
예제 #22
0
        public void GenericInterfaceReference(ModuleDefinition module)
        {
            var type = module.GetType ("Program");
            var iface = type.Interfaces [0];

            var instance = (GenericInstanceType) iface;
            var owner = instance.ElementType;

            Assert.AreEqual (1, instance.GenericArguments.Count);
            Assert.AreEqual (1, owner.GenericParameters.Count);
        }
예제 #23
0
		static TypeDefinition GetType (ModuleDefinition module, TypeReference type)
		{
			if (!type.IsNested)
				return module.GetType (type.Namespace, type.Name);

			var declaring_type = type.DeclaringType.Resolve ();
			if (declaring_type == null)
				return null;

			return declaring_type.GetNestedType (type.Name);
		}
예제 #24
0
		public void GenericMethodDefinition (ModuleDefinition module)
		{
			var baz = module.GetType ("Baz");

			var gazonk = baz.GetMethod ("Gazonk");

			Assert.IsNotNull (gazonk);

			Assert.IsTrue (gazonk.HasGenericParameters);
			Assert.AreEqual (1, gazonk.GenericParameters.Count);
			Assert.AreEqual ("TBang", gazonk.GenericParameters [0].Name);
		}
예제 #25
0
파일: MethodTests.cs 프로젝트: jm3105/cecil
        public void MethodRefDeclaredOnGenerics(ModuleDefinition module)
        {
            var type = module.GetType ("Tamtam");
            var beta = type.GetMethod ("Beta");
            var charlie = type.GetMethod ("Charlie");

            var new_list_beta = (MethodReference) beta.Body.Instructions [0].Operand;
            var new_list_charlie = (MethodReference) charlie.Body.Instructions [0].Operand;

            Assert.AreEqual ("System.Collections.Generic.List`1<TBeta>", new_list_beta.DeclaringType.FullName);
            Assert.AreEqual ("System.Collections.Generic.List`1<TCharlie>", new_list_charlie.DeclaringType.FullName);
        }
예제 #26
0
        public void GenericBaseType(ModuleDefinition module)
        {
            var child = module.GetType ("Child`1");

            var child_t = child.GenericParameters [0];
            Assert.IsNotNull (child_t);

            var instance = child.BaseType as GenericInstanceType;
            Assert.IsNotNull (instance);

            Assert.AreEqual (child_t, instance.GenericArguments [0]);
        }
예제 #27
0
        public void ArrayMarshalerSized(ModuleDefinition module)
        {
            var delegate_type = module.GetType ("SomeMethod");
            var parameter = delegate_type.GetMethod ("Invoke").Parameters [1];

            Assert.IsTrue (parameter.HasMarshalInfo);
            var array_info = (ArrayMarshalInfo) parameter.MarshalInfo;

            Assert.IsNotNull (array_info);

            Assert.AreEqual (0, array_info.SizeParameterMultiplier);
        }
예제 #28
0
		public void SimplePInvoke (ModuleDefinition module)
		{
			var bar = module.GetType ("Bar");
			var pan = bar.GetMethod ("Pan");

			Assert.IsTrue (pan.IsPInvokeImpl);
			Assert.IsNotNull (pan.PInvokeInfo);

			Assert.AreEqual ("Pan", pan.PInvokeInfo.EntryPoint);
			Assert.IsNotNull (pan.PInvokeInfo.Module);
			Assert.AreEqual ("foo.dll", pan.PInvokeInfo.Module.Name);
		}
예제 #29
0
        public void EnumFieldsConstant(ModuleDefinition module)
        {
            var pim = module.GetType ("Pim");
            Assert.IsNotNull (pim);

            var field = pim.GetField ("Pam");
            Assert.IsTrue (field.HasConstant);
            Assert.AreEqual (1, (int) field.Constant);

            field = pim.GetField ("Poum");
            Assert.AreEqual (2, (int) field.Constant);
        }
 public override void InjectInto(ModuleDefinition gamedef, ModuleDefinition moddef)
 {
     // This injects a call to the mod's static ZombieDamaged method for the ZombieDamagedScriptEvent. Also exposes some zombie variables. Also, disable its AI optionally.
     TypeDefinition zombietype = gamedef.GetType("SDG.Unturned.Zombie");
     FieldDefinition field = GetField(zombietype, "target");
     field.IsPrivate = false;
     field.IsPublic = true;
     FieldDefinition fieldistick = GetField(zombietype, "isTicking");
     fieldistick.IsPrivate = false;
     fieldistick.IsPublic = true;
     FieldDefinition fieldseeker = GetField(zombietype, "seeker");
     fieldseeker.IsPrivate = false;
     fieldseeker.IsPublic = true;
     FieldDefinition fieldpath = GetField(zombietype, "path");
     fieldpath.IsPrivate = false;
     fieldpath.IsPublic = true;
     FieldDefinition aidisabledfield = new FieldDefinition("UFM_AIDisabled", FieldAttributes.Public, gamedef.TypeSystem.Boolean);
     zombietype.Fields.Add(aidisabledfield);
     foreach (MethodDefinition tmethod in zombietype.Methods)
     {
         switch (tmethod.Name)
         {
             case "alert":
                 DisableAI(tmethod.Body, aidisabledfield);
                 break;
             default:
                 break;
         }
     }
     // TODO: Disable things in the update methods too.
     TypeDefinition modtype = moddef.GetType("UnturnedFrenetic.UnturnedFreneticMod");
     MethodReference eventmethod = gamedef.ImportReference(GetMethod(modtype, "ZombieDamaged", 4));
     MethodDefinition damagemethod = GetMethod(zombietype, "askDamage", 4);
     MethodBody damagebody = damagemethod.Body;
     InjectInstructions(damagebody, 0, new Instruction[]
     {
             // Load "this" onto the stack.
             Instruction.Create(OpCodes.Ldarg_0),
             // Load "amount" onto the stack.
             Instruction.Create(OpCodes.Ldarga_S, damagemethod.Parameters[0]),
             // Load "newRagdoll" onto the stack.
             Instruction.Create(OpCodes.Ldarga_S, damagemethod.Parameters[1]),
             // Load "xp" onto the stack
             Instruction.Create(OpCodes.Ldarga_S, damagemethod.Parameters[3]),
             // Call the ZombieDamaged method with the above parameters and return a bool.
             Instruction.Create(OpCodes.Call, eventmethod),
             // If the return is false, jump ahead to the original 0th instruction.
             Instruction.Create(OpCodes.Brfalse, damagebody.Instructions[0]),
             // Otherwise, return now.
             Instruction.Create(OpCodes.Ret)
     });
 }
예제 #31
0
        private static bool TryGetDefinition(ModuleDefinition module, TypeParser.Type type_info, out TypeReference type)
        {
            type = null;
            if (!TypeParser.TryCurrentModule(module, type_info))
            {
                return(false);
            }
            TypeDefinition nestedType = module.GetType(type_info.type_fullname);

            if (nestedType == null)
            {
                return(false);
            }
            string[] nestedNames = type_info.nested_names;
            if (!nestedNames.IsNullOrEmpty <string>())
            {
                for (int i = 0; i < (int)nestedNames.Length; i++)
                {
                    nestedType = nestedType.GetNestedType(nestedNames[i]);
                }
            }
            type = nestedType;
            return(true);
        }
예제 #32
0
        private static bool TryGetDefinition(ModuleDefinition module, Type type_info, out TypeReference type)
        {
            type = null;
            if (!TryCurrentModule(module, type_info))
            {
                return(false);
            }
            TypeDefinition self = module.GetType(type_info.type_fullname);

            if (self == null)
            {
                return(false);
            }
            string[] strArray = type_info.nested_names;
            if (!strArray.IsNullOrEmpty <string>())
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    self = self.GetNestedType(strArray[i]);
                }
            }
            type = self;
            return(true);
        }