Пример #1
0
        /// <summary>Default constructor</summary>
        public DelegateTypeFactory()
        {
            counter++;
            var name     = $"HarmonyDTFAssembly{counter}";
            var assembly = PatchTools.DefineDynamicAssembly(name);

            module = assembly.DefineDynamicModule($"HarmonyDTFModule{counter}");
        }
Пример #2
0
        /// <summary>Default constructor</summary>
        public DelegateTypeFactory()
        {
            counter++;
            var name     = "HarmonyDTFAssembly" + counter;
            var assembly = PatchTools.DefineDynamicAssembly(name);

            module = assembly.DefineDynamicModule("HarmonyDTFModule" + counter);
        }
Пример #3
0
        static Dictionary <MethodBase, byte[]> GetState()
        {
            lock (name)
            {
                var assembly = SharedStateAssembly();
                if (assembly == null)
                {
                    var assemblyBuilder = PatchTools.DefineDynamicAssembly(name);
                    var moduleBuilder   = assemblyBuilder.DefineDynamicModule(name);
                    var typeAttributes  = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Abstract;
                    var typeBuilder     = moduleBuilder.DefineType(name, typeAttributes);
                    typeBuilder.DefineField("state", typeof(Dictionary <MethodBase, byte[]>), FieldAttributes.Static | FieldAttributes.Public);
                    typeBuilder.DefineField("version", typeof(int), FieldAttributes.Static | FieldAttributes.Public).SetConstant(internalVersion);
#if NETSTANDARD2_0
                    typeBuilder.CreateTypeInfo().AsType();
#else
                    typeBuilder.CreateType();
#endif

                    assembly = SharedStateAssembly();
                    if (assembly == null)
                    {
                        throw new Exception("Cannot find or create harmony shared state");
                    }
                }

                var type = assembly.GetType(name);

                var versionField = type.GetField("version");
                if (versionField == null)
                {
                    throw new Exception("Cannot find harmony state version field");
                }
                actualVersion = (int)versionField.GetValue(null);

                var stateField = type.GetField("state");
                if (stateField == null)
                {
                    throw new Exception("Cannot find harmony state field");
                }
                if (stateField.GetValue(null) == null)
                {
                    stateField.SetValue(null, new Dictionary <MethodBase, byte[]>());
                }

                return((Dictionary <MethodBase, byte[]>)stateField.GetValue(null));
            }
        }