Ejemplo n.º 1
0
        public void Test_AccessTools2_Method2()
        {
            var classType = typeof(AccessTools2Class);

            Assert.NotNull(AccessTools2.Method(classType, "Method1"));
            Assert.NotNull(AccessTools2.DeclaredMethod(classType, "Method1"));
            Assert.Null(AccessTools2.Method(classType, "unknown"));
            Assert.Null(AccessTools2.DeclaredMethod(classType, "unknown"));

            var subclassType = typeof(AccessTools2SubClass);

            Assert.NotNull(AccessTools2.Method(subclassType, "Method1"));
            Assert.Null(AccessTools2.DeclaredMethod(subclassType, "Method1"));
            Assert.Null(AccessTools2.Method(subclassType, "unknown"));
            Assert.Null(AccessTools2.DeclaredMethod(subclassType, "unknown"));

            var structType = typeof(AccessTools2Struct);

            Assert.NotNull(AccessTools2.Method(structType, "Method1"));
            Assert.NotNull(AccessTools2.DeclaredMethod(structType, "Method1"));
            Assert.Null(AccessTools2.Method(structType, "unknown"));
            Assert.Null(AccessTools2.DeclaredMethod(structType, "unknown"));

            var interfaceType = typeof(IAccessTools2Type);

            Assert.NotNull(AccessTools2.Method(interfaceType, "Method1"));
            Assert.NotNull(AccessTools2.DeclaredMethod(interfaceType, "Method1"));
            Assert.Null(AccessTools2.Method(interfaceType, "unknown"));
            Assert.Null(AccessTools2.DeclaredMethod(interfaceType, "unknown"));
        }
Ejemplo n.º 2
0
 public void Test_AccessTools2_GetDelegate_InvalidDelegates()
 {
     Assert.IsNull(AccessTools2.GetDelegate <Action>(interfaceTest));
     Assert.IsNull(AccessTools2.GetDelegate <Func <bool> >(baseTest));
     Assert.IsNull(AccessTools2.GetDelegate <Action <string> >(derivedTest));
     Assert.IsNull(AccessTools2.GetDelegate <Func <int, float, string> >(structTest));
 }
Ejemplo n.º 3
0
        public void Test_AccessTools2_Property2()
        {
            var classType = typeof(AccessTools2Class);

            Assert.NotNull(AccessTools2.Property(classType, "Property1"));
            Assert.NotNull(AccessTools2.DeclaredProperty(classType, "Property1"));
            Assert.Null(AccessTools2.Property(classType, "unknown"));
            Assert.Null(AccessTools2.DeclaredProperty(classType, "unknown"));

            var subclassType = typeof(AccessTools2SubClass);

            Assert.NotNull(AccessTools2.Property(subclassType, "Property1"));
            Assert.Null(AccessTools2.DeclaredProperty(subclassType, "Property1"));
            Assert.Null(AccessTools2.Property(subclassType, "unknown"));
            Assert.Null(AccessTools2.DeclaredProperty(subclassType, "unknown"));

            var structType = typeof(AccessTools2Struct);

            Assert.NotNull(AccessTools2.Property(structType, "Property1"));
            Assert.NotNull(AccessTools2.DeclaredProperty(structType, "Property1"));
            Assert.Null(AccessTools2.Property(structType, "unknown"));
            Assert.Null(AccessTools2.DeclaredProperty(structType, "unknown"));

            var interfaceType = typeof(IAccessTools2Type);

            Assert.NotNull(AccessTools2.Property(interfaceType, "Property1"));
            Assert.NotNull(AccessTools2.DeclaredProperty(interfaceType, "Property1"));
            Assert.Null(AccessTools2.Property(interfaceType, "unknown"));
            Assert.Null(AccessTools2.DeclaredProperty(interfaceType, "unknown"));
        }
Ejemplo n.º 4
0
        public void OutputRuntimeInfo()
        {
            var runtimeInformationType = AccessTools2.TypeByName("System.Runtime.InteropServices.RuntimeInformation");
            var executingAssembly      = Assembly.GetExecutingAssembly();

            TestTools.Log("Environment.OSVersion: " + Environment.OSVersion);
            TestTools.Log("RuntimeInformation.OSDescription: " + GetProperty(runtimeInformationType, "OSDescription"));

            TestTools.Log("IntPtr.Size: " + IntPtr.Size);
            TestTools.Log("Environment.Is64BitProcess: " + GetProperty(typeof(Environment), "Is64BitProcess"));
            TestTools.Log("Environment.Is64BitOperatingSystem: " + GetProperty(typeof(Environment), "Is64BitOperatingSystem"));
            TestTools.Log("RuntimeInformation.ProcessArchitecture: " + GetProperty(runtimeInformationType, "ProcessArchitecture"));
            TestTools.Log("RuntimeInformation.OSArchitecture: " + GetProperty(runtimeInformationType, "OSArchitecture"));

            TestTools.Log("RuntimeInformation.FrameworkDescription: " + GetProperty(runtimeInformationType, "FrameworkDescription"));
            TestTools.Log("Mono.Runtime.DisplayName: " + CallGetter(Type.GetType("Mono.Runtime"), "GetDisplayName"));
            TestTools.Log("RuntimeEnvironment.RuntimeDirectory: " + RuntimeEnvironment.GetRuntimeDirectory());
            TestTools.Log("RuntimeEnvironment.SystemVersion: " + RuntimeEnvironment.GetSystemVersion());
            TestTools.Log("Environment.Version: " + Environment.Version);

            TestTools.Log("Core Assembly: " + typeof(object).Assembly);
            TestTools.Log("Executing Assembly's ImageRuntimeVersion: " + executingAssembly.ImageRuntimeVersion);
            TestTools.Log("Executing Assembly's TargetFrameworkAttribute: " + (executingAssembly.GetCustomAttributes(true)
                                                                               .Where(attr => attr.GetType().Name is "TargetFrameworkAttribute")
                                                                               .Select(attr => Traverse2.Create(attr).Property("FrameworkName").GetValue <string>())
                                                                               .FirstOrDefault() ?? "null"));
        }
Ejemplo n.º 5
0
        public MCMv3GlobalSettingsContainer()
        {
            if (Settings is null)
            {
                Settings = new List <GlobalSettings>();

                var allTypes = AccessTools2.AllAssemblies()
                               .Where(a => !a.IsDynamic)
                               .SelectMany(AccessTools2.GetTypesFromAssembly)
                               .Where(t => t.IsClass && !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) is not null)
                               .ToList();

                var mbOptionScreenSettings = allTypes
                                             .Where(t => typeof(v3::MCM.Abstractions.Settings.Base.Global.GlobalSettings).IsAssignableFrom(t))
                                             .Where(t => !typeof(v3::MCM.Abstractions.Settings.Base.Global.EmptyGlobalSettings).IsAssignableFrom(t))
                                             .Where(t => !ReflectionUtils.ImplementsOrImplementsEquivalent(t, "MCM.MCMSettings"))
                                             .Where(t => !typeof(v3::MCM.Abstractions.IWrapper).IsAssignableFrom(t))
                                             .Select(obj => Activator.CreateInstance(obj) is { } val ? new MCMv3GlobalSettingsWrapper(val) : null)
                                             .Where(t => t is not null)
                                             .Cast <GlobalSettings>();
                Settings.AddRange(mbOptionScreenSettings);
            }

            foreach (var setting in Settings)
            {
                RegisterSettings(setting);
            }
        }
Ejemplo n.º 6
0
        protected ViewModelWrapper(object @object, bool clearOriginalProperties = true)
        {
            Object = @object;

            // Copy Object properties
            var field       = AccessTools2.Field(typeof(ViewModel), "_propertyInfos");
            var propsObject = field?.GetValue(Object) as Dictionary <string, PropertyInfo> ?? new Dictionary <string, PropertyInfo>();
            var propsThis   = field?.GetValue(this) as Dictionary <string, PropertyInfo> ?? new Dictionary <string, PropertyInfo>();

            if (clearOriginalProperties)
            {
                propsThis.Clear(); // clear properties
            }
            foreach (var(key, value) in propsObject)
            {
                propsThis[key] = new WrappedPropertyInfo(value, Object, () =>
                {
                    OnPropertyChangedWithValue(value.GetValue(Object), value.Name);
                    OnPropertyChanged(value.Name);
                });
            }

            // Trigger OnPropertyChanged from Object
            if (Object is INotifyPropertyChanged notifyPropertyChanged)
            {
                notifyPropertyChanged.PropertyChanged += OnPropertyChangedEventHandler;
            }
            if (Object is ViewModel viewModel)
            {
                viewModel.PropertyChangedWithValue += OnPropertyChangedWithValueEventHandler;
            }
        }
Ejemplo n.º 7
0
        public PropertyDefinitionWithMinMaxWrapper(object @object) : base(@object)
        {
            var type = @object.GetType();

            MinValue = AccessTools2.Property(type, nameof(MinValue))?.GetValue(@object) as decimal? ?? 0;
            MaxValue = AccessTools2.Property(type, nameof(MaxValue))?.GetValue(@object) as decimal? ?? 0;
        }
        private static IEnumerable <SettingsPropertyGroupDefinition> GetSettingPropertyGroups(object @object)
        {
            var settingPropertyGroups = AccessTools2.Property(@object.GetType(), "SettingPropertyGroups")?.GetValue(@object) as IEnumerable <MCMv3SettingsPropertyGroupDefinition>;

            settingPropertyGroups ??= Enumerable.Empty <MCMv3SettingsPropertyGroupDefinition>();
            return(settingPropertyGroups.Select(x => new SettingsPropertyGroupDefinitionWrapper(x)));
        }
Ejemplo n.º 9
0
        public void Test_AccessTools2_GetDelegate_StaticDelegates_InterfaceMethod()
        {
            var f = 789f;

            Assert.AreEqual("static test 456 790 1", AccessTools2.GetDelegate <MethodDel>(staticTest) !(456, ref f));
            Assert.IsNull(AccessTools2.GetDelegate <MethodDel>(new AccessTools2MethodDelegate.Base(), staticTest)?.Invoke(456, ref f));
        }
Ejemplo n.º 10
0
        public void Test_AccessTools2_Method1()
        {
            var type = typeof(AccessTools2Class);

            Assert.Null(AccessTools2.Method(null !));
            Assert.Null(AccessTools2.Method(type, null !));
            Assert.Null(AccessTools2.Method(null !, "Method1"));
            Assert.Null(AccessTools2.Method(type, "unknown"));

            var m1 = AccessTools2.Method(type, "Method1");

            Assert.NotNull(m1);
            Assert.AreEqual(type, m1.DeclaringType);
            Assert.AreEqual("Method1", m1.Name);

            var m2 = AccessTools2.Method($"{typeof(AccessTools2Class).FullName}:Method1");

            Assert.NotNull(m2);
            Assert.AreEqual(type, m2.DeclaringType);
            Assert.AreEqual("Method1", m2.Name);

            var m3 = AccessTools2.Method(type, "Method1", new Type[] { });

            Assert.NotNull(m3);

            var m4 = AccessTools2.Method(type, "SetField", new Type[] { typeof(string) });

            Assert.NotNull(m4);
        }
Ejemplo n.º 11
0
        public MBOPropertyDefinitionIntegerWrapper(object @object) : base(@object)
        {
            var type = @object.GetType();

            MinValue    = (decimal)(AccessTools2.Property(type, "MinValue")?.GetValue(@object) as int? ?? 0);
            MaxValue    = (decimal)(AccessTools2.Property(type, "MaxValue")?.GetValue(@object) as int? ?? 0);
            ValueFormat = AccessTools2.Property(type, "ValueFormat")?.GetValue(@object) as string ?? "0";
        }
        public MCMv3FluentGlobalSettingsWrapper(object @object)
            : base(@object, GetId(@object), GetDisplayName(@object), GetFolderName(@object), GetSubFolder(@object), GetFormat(@object),
                   GetUIVersion(@object), GetSubGroupDelimiter(@object), null, GetSettingPropertyGroups(@object), new Dictionary <string, ISettingsPresetBuilder>())
        {
            var type = @object.GetType();

            OnPropertyChangedMethod = AccessTools2.Method(type, "OnPropertyChanged");
        }
        static MCMSelectorVM()
        {
            var methodInfo = AccessTools2.Property(typeof(TSelectorItemVM), "CanBeSelected")?.GetMethod;

            _canBeSelectedDelegate = methodInfo is not null
                ? AccessTools2.GetDelegate <CanBeSelectedDelegate>(methodInfo) !
                : _ => false;
        }
 public void Test_AccessTools2_Utilities()
 {
     Assert.IsNull(AccessTools2.TypeByName(null !));
     Assert.IsNull(AccessTools2.FindIncludingBaseTypes <TestClass>(null !, null !));
     Assert.IsNull(AccessTools2.FindIncludingBaseTypes <TestClass>(typeof(TestClass), null !));
     Assert.IsEmpty(AccessTools2.GetTypesFromAssembly(null !));
     Assert.IsEmpty(AccessTools2.GetTypesFromAssemblyIfValid(null !));
 }
 public void Test_AccessTools2_FieldRef()
 {
     Assert.IsNull(AccessTools2.FieldRefAccess <TestClass, TestDelegate>(((FieldInfo)null) !));
     Assert.IsNull(AccessTools2.FieldRefAccess <TestClass, TestDelegate>(((string)null) !));
     Assert.IsNull(AccessTools2.FieldRefAccess <TestDelegate>(((FieldInfo)null) !));
     Assert.IsNull(AccessTools2.FieldRefAccess <TestDelegate>(typeof(TestClass), null !));
     Assert.IsNull(AccessTools2.FieldRefAccess <TestDelegate>(typeof(TestStruct), null !));
     Assert.IsNull(AccessTools2.FieldRefAccess <TestDelegate>(null !, null !));
 }
        protected virtual BaseSettings CreateNew()
        {
            var type        = GetType();
            var constructor = AccessTools2.Constructor(type, Type.EmptyTypes);

            return(constructor is not null
                ? (BaseSettings)constructor.Invoke(null)
                : (BaseSettings)FormatterServices.GetUninitializedObject(type));
        }
        public ModLibGlobalSettingsWrapper(object @object) : base(@object)
        {
            var type = @object.GetType();

            _getIdDelegate          = AccessTools2.GetPropertyGetterDelegate <GetIdDelegate>(@object, type, nameof(LegacyBaseSettings.ID));
            _getFolderNameDelegate  = AccessTools2.GetPropertyGetterDelegate <GetFolderNameDelegate>(@object, type, nameof(LegacyBaseSettings.ModuleFolderName));
            _getDisplayNameDelegate = AccessTools2.GetPropertyGetterDelegate <GetDisplayNameDelegate>(@object, type, nameof(LegacyBaseSettings.ModName));
            _getSubFolderDelegate   = AccessTools2.GetPropertyGetterDelegate <GetSubFolderDelegate>(@object, type, nameof(LegacyBaseSettings.SubFolder));
        }
 static Helper()
 {
     DynamicMethodDefinitionCtor = AccessTools2.GetConstructorDelegate <DynamicMethodDefinitionCtorDelegate>("MonoMod.Utils.DynamicMethodDefinition", new[] { typeof(string), typeof(Type), typeof(Type[]) });
     GetILGenerator = AccessTools2.GetDelegateObjectInstance <GetILGeneratorDelegate>("MonoMod.Utils.DynamicMethodDefinition:GetILGenerator", Type.EmptyTypes);
     Emit1          = AccessTools2.GetDelegateObjectInstance <Emit1Delegate>("System.Reflection.Emit.ILGenerator:Emit", new[] { typeof(OpCode) });
     Emit2          = AccessTools2.GetDelegateObjectInstance <Emit2Delegate>("System.Reflection.Emit.ILGenerator:Emit", new[] { typeof(OpCode), typeof(FieldInfo) });
     Emit3          = AccessTools2.GetDelegateObjectInstance <Emit3Delegate>("System.Reflection.Emit.ILGenerator:Emit", new[] { typeof(OpCode), typeof(Type) });
     Generate       = AccessTools2.GetDelegateObjectInstance <GenerateDelegate>("MonoMod.Utils.DynamicMethodDefinition:Generate", Type.EmptyTypes);
 }
Ejemplo n.º 19
0
        private ConstructorInfo?GetWrappedConstructor()
        {
            var constructorInfo = AccessTools2.Constructor(typeof(SubModuleLua), new[] { typeof(string), typeof(string), typeof(string) });

            if (constructorInfo is null)
            {
                return(null);
            }
            return(new WrappedConstructorInfo(constructorInfo, new object[] { _moduleFolder, _scriptName, _subModuleClassType }));
        }
Ejemplo n.º 20
0
 public void Test_AccessTools2_TypeByName_CurrentAssemblies()
 {
     Assert.NotNull(AccessTools2.TypeByName(typeof(Harmony).FullName !));
     Assert.NotNull(AccessTools2.TypeByName(typeof(Test_AccessTools2).FullName !));
     Assert.Null(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyA.Class1"));
     Assert.Null(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyB.Class1"));
     Assert.Null(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyB.Class2"));
     Assert.Null(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyC.Class1"));
     Assert.Null(AccessTools2.TypeByName("IAmALittleTeaPot.ShortAndStout"));
 }
Ejemplo n.º 21
0
        private static void ReplaceSubModuleCreation()
        {
            var onSubModuleLoad = AccessTools2.GetDelegate <OnSubModuleLoadDelegate>(typeof(MBSubModuleBase), "OnSubModuleLoad");

            var loadedSubmoduleTypesField = AccessTools2.FieldRefAccess <TWModule, Dictionary <string, Type> >("_loadedSubmoduleTypes");
            var submodulesField           = AccessTools2.FieldRefAccess <TWModule, List <MBSubModuleBase> >("_submodules");
            var dictVersionField          = AccessTools2.FieldRefAccess <Dictionary <string, Type>, int>("version");

            if (onSubModuleLoad is null || loadedSubmoduleTypesField is null || submodulesField is null || dictVersionField is null)
            {
                return;
            }

            var loadedSubmoduleTypes = loadedSubmoduleTypesField(TWModule.CurrentModule);
            var submodules           = submodulesField(TWModule.CurrentModule);
            var dictVersion          = dictVersionField(loadedSubmoduleTypes);

            var loadedSubmoduleTypesCopy = new Dictionary <string, Type>(loadedSubmoduleTypes);
            var preLoadedSubModules      = submodules.ToDictionary(k => k.GetType().FullName, v => v.GetType());

            loadedSubmoduleTypes.Clear();
            dictVersionField(loadedSubmoduleTypes) = dictVersion;

            var subModules = new Dictionary <string, MBSubModuleBase>();

            foreach (var(name, type) in loadedSubmoduleTypesCopy.Except(preLoadedSubModules))
            {
                var constructor = AccessTools2.GetConstructorDelegate <ConstructorDelegate>(type, Array.Empty <Type>());
                if (constructor is null)
                {
                    continue;
                }

                subModules.Add(name, constructor());
            }
            submodules.AddRange(subModules.Values);
            foreach (var(name, instance) in subModules)
            {
                var onServiceRegistration = AccessTools2.GetDelegate <OnServiceRegistrationDelegate>(instance, AccessTools.Method(instance.GetType(), "OnServiceRegistration"));
                if (onServiceRegistration is null)
                {
                    continue;
                }

                onServiceRegistration();
            }
            foreach (var(name, instance) in subModules)
            {
                onSubModuleLoad(instance);
            }

            loadedSubmoduleTypesCopy.Clear();
            subModules.Clear();
        }
Ejemplo n.º 22
0
        public MBOv1GlobalSettingsWrapper(object @object) : base(@object)
        {
            var type = @object.GetType();

            _getIdDelegate                = AccessTools2.GetDelegate <GetIdDelegate>(@object, type, nameof(MCMv1BaseSettings.Id));
            _getFolderNameDelegate        = AccessTools2.GetDelegate <GetFolderNameDelegate>(@object, type, nameof(MCMv1BaseSettings.ModuleFolderName));
            _getDisplayNameDelegate       = AccessTools2.GetDelegate <GetDisplayNameDelegate>(@object, type, nameof(MCMv1BaseSettings.ModName));
            _getUIVersionDelegate         = AccessTools2.GetDelegate <GetUIVersionDelegate>(@object, type, nameof(MCMv1BaseSettings.UIVersion));
            _getSubFolderDelegate         = AccessTools2.GetDelegate <GetSubFolderDelegate>(@object, type, nameof(MCMv1BaseSettings.SubFolder));
            _getSubGroupDelimiterDelegate = AccessTools2.GetDelegate <GetSubGroupDelimiterDelegate>(@object, type, "SubGroupDelimiter");
        }
Ejemplo n.º 23
0
 static void TestTypeByNameWithNoInvalidAssembly(ITestIsolationContext context)
 {
     context.AssemblyLoad("HarmonyTestsDummyAssemblyC");
     Assert.NotNull(AccessTools2.TypeByName(typeof(Harmony).FullName !));
     Assert.NotNull(AccessTools2.TypeByName(typeof(Test_AccessTools2).FullName !));
     Assert.Null(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyA.Class1"));
     Assert.Null(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyB.Class1"));
     Assert.Null(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyB.Class2"));
     Assert.NotNull(AccessTools2.TypeByName("HarmonyTestsDummyAssemblyC.Class1"));
     Assert.Null(AccessTools2.TypeByName("IAmALittleTeaPot.ShortAndStout"));
 }
Ejemplo n.º 24
0
        public void Test_AccessTools2_GetDelegate_ClosedInstanceDelegates_InterfaceMethod()
        {
            var f               = 789f;
            var baseInstance    = new AccessTools2MethodDelegate.Base();
            var derivedInstance = new AccessTools2MethodDelegate.Derived();
            var structInstance  = new AccessTools2MethodDelegate.Struct();

            Assert.AreEqual("base test 456 790 1", AccessTools2.GetDelegate <MethodDel>(baseInstance, interfaceTest) !(456, ref f));
            Assert.AreEqual("derived test 456 791 1", AccessTools2.GetDelegate <MethodDel>(derivedInstance, interfaceTest) !(456, ref f));
            Assert.AreEqual("struct result 456 792 1", AccessTools2.GetDelegate <MethodDel>(structInstance, interfaceTest) !(456, ref f));
        }
Ejemplo n.º 25
0
        public MBSubModuleBaseWrapper(MBSubModuleBase subModule)
        {
            SubModule = subModule;

            OnSubModuleLoadInstance     = AccessTools2.GetDelegate <OnSubModuleLoadDelegate, MBSubModuleBase>(subModule, "OnSubModuleLoad");
            OnSubModuleUnloadedInstance = AccessTools2.GetDelegate <OnSubModuleUnloadedDelegate, MBSubModuleBase>(subModule, "OnSubModuleUnloaded");
            OnBeforeInitialModuleScreenSetAsRootInstance = AccessTools2.GetDelegate <OnBeforeInitialModuleScreenSetAsRootDelegate, MBSubModuleBase>(subModule, "OnBeforeInitialModuleScreenSetAsRoot");
            OnGameStartInstance           = AccessTools2.GetDelegate <OnGameStartDelegate, MBSubModuleBase>(subModule, "OnGameStart");
            OnApplicationTickInstance     = AccessTools2.GetDelegate <OnApplicationTickDelegate, MBSubModuleBase>(subModule, "OnApplicationTick");
            OnServiceRegistrationInstance = AccessTools2.GetDelegate <OnServiceRegistrationDelegate, MBSubModuleBase>(subModule, "OnServiceRegistration");
        }
        public void Test_AccessTools2_Methods()
        {
            Assert.IsNull(AccessTools2.DeclaredMethod(typeof(TestClass), null !));
            Assert.IsNull(AccessTools2.DeclaredMethod(typeof(TestClass), "NonExistingMethod"));
            Assert.IsNull(AccessTools2.Method(typeof(TestClass), null !));
            Assert.IsNull(AccessTools2.Method(typeof(TestClass), "NonExistingMethod"));
            //Assert.IsNull(AccessTools2.Method(null!, null!));
            Assert.IsNull(AccessTools2.Method((null) !, "NonExistingMethod"));

            Assert.IsNull(AccessTools2.DeclaredMethod((string)null !, null !));
            Assert.IsNull(AccessTools2.Method(null !));
        }
Ejemplo n.º 27
0
        public static void Patch(Harmony harmony)
        {
            harmony.Patch(
                AccessTools2.Method("TaleWorlds.Core.ItemObject:Deserialize"),
                postfix: new HarmonyMethod(typeof(ItemObjectPatch), nameof(Postfix)));

#if CRAFTED
            harmony.Patch(
                AccessTools2.Method(typeof(ItemObject), "InitCraftedItemObject"),
                postfix: new HarmonyMethod(typeof(ItemObjectPatch), nameof(Postfix2)));
#endif
        }
Ejemplo n.º 28
0
        private static IEnumerable <ISettingsPropertyDefinition> GetSettingProperties(object @object)
        {
            var settingPropertiesProperty = AccessTools2.Property(@object.GetType(), nameof(SettingProperties));

            if (settingPropertiesProperty?.GetValue(@object) is IEnumerable list)
            {
                foreach (var obj in list)
                {
                    yield return(new SettingsPropertyDefinitionWrapper(obj));
                }
            }
        }
Ejemplo n.º 29
0
        private static IEnumerable <SettingsPropertyGroupDefinition> GetSubGroups(object @object)
        {
            var subGroupsProperty = AccessTools2.Property(@object.GetType(), nameof(SubGroups)) ??
                                    AccessTools2.Property(@object.GetType(), "SettingPropertyGroups");

            if (subGroupsProperty?.GetValue(@object) is IEnumerable list)
            {
                foreach (var obj in list)
                {
                    yield return(new SettingsPropertyGroupDefinitionWrapper(obj));
                }
            }
        }
Ejemplo n.º 30
0
        public void Test_AccessTools2_GetDelegate_OpenInstanceDelegates_DelegateInterfaceInstanceType()
        {
            var f               = 789f;
            var baseInstance    = new AccessTools2MethodDelegate.Base();
            var derivedInstance = new AccessTools2MethodDelegate.Derived();
            var structInstance  = new AccessTools2MethodDelegate.Struct();

            Assert.AreEqual("base test 456 790 1", AccessTools2.GetDelegate <OpenMethodDel <AccessTools2MethodDelegate.IInterface> >(baseTest) !(baseInstance, 456, ref f));
            Assert.AreEqual("derived test 456 791 1", AccessTools2.GetDelegate <OpenMethodDel <AccessTools2MethodDelegate.IInterface> >(baseTest) !(derivedInstance, 456, ref f));
            _ = Assert.Throws(typeof(InvalidCastException), () => AccessTools2.GetDelegate <OpenMethodDel <AccessTools2MethodDelegate.IInterface> >(derivedTest) !(baseInstance, 456, ref f));
            Assert.AreEqual("derived test 456 792 2", AccessTools2.GetDelegate <OpenMethodDel <AccessTools2MethodDelegate.IInterface> >(derivedTest) !(derivedInstance, 456, ref f));
            Assert.AreEqual("struct result 456 793 1", AccessTools2.GetDelegate <OpenMethodDel <AccessTools2MethodDelegate.IInterface> >(structTest) !(structInstance, 456, ref f));
        }