Esempio n. 1
0
        private static void Init(Action orig)
        {
            orig();

            HookAttribute.DisableHooks(typeof(SceneCatalogHooks));

            var lookups = SceneCatalog.allSceneDefs.ToDictionary(sd => sd.baseSceneName);

            Logger.LogInfo("Lodded dictionary for sceneNameOverride doping");

            var sceneDefinitions = RainOfStages.Instance.SceneDefinitions.OfType <SceneDefinition>();

            var overrideMapping     = MakeLinks(sceneDefinitions, def => def.reverseSceneNameOverrides);
            var destinationsMapping = MakeLinks(sceneDefinitions, def => def.destionationInjections);

            Weave(lookups, overrideMapping,
                  sd => sd.Destination.baseSceneName,
                  sd => sd.sceneNameOverrides,
                  (sd, data) => sd.sceneNameOverrides = data.ToList());

            Weave(lookups, destinationsMapping,
                  sd => sd.Destination,
                  sd => sd.destinations,
                  (sd, data) => sd.destinations = data.ToArray());
        }
Esempio n. 2
0
        public override void SetHook(HookInfo info)
        {
            if (HookInfos.Count > 0)
            {
                throw new InvalidOperationException("JumpHook can not mix with other hook");
            }

            base.SetHook(info);

            HookAttribute hook = info.GetHookAttribute();

            switch (hook.Type)
            {
            case HookType.SimpleJumpToRet:
                int address = (int)info.GetReturnValue();
                Logger.Log("jump to address: 0x{0:X}", address);
                ASMWriter.WriteJump(new JumpStruct(hook.Address, address));
                break;

            case HookType.DirectJumpToHook:
                int callable = (int)info.GetCallable();
                Logger.Log("jump to callable: 0x{0:X}", callable);
                ASMWriter.WriteJump(new JumpStruct(hook.Address, callable));
                break;

            default:
                Logger.LogError("found unkwnow jump hook: " + info.Member.Name);
                break;
            }
        }
Esempio n. 3
0
 static SceneDefReference()
 {
     if (Application.isEditor)
     {
         return;
     }
     HookAttribute.ApplyHooks <SceneDefReference>();
 }
Esempio n. 4
0
 private HookMetaData BuildMethodHookType(HookAttribute hookAttrib, MethodInfo method)
 {
     if (method.GetParameters().Any())
     {
         var message = string.Format("Method {0} on class {1} may not have any parameters.", method.Name, method.DeclaringType.Name);
         throw new ArgumentException(message, "method");
     }
     return(new MethodHookMetaData(method, hookAttrib));
 }
Esempio n. 5
0
        private HookMetaData BuildDelegateHookType(HookAttribute hookAttrib, FieldInfo @delegate)
        {
            if (@delegate.FieldType != typeof(Action))
            {
                var message = string.Format("Field {0} with attribute {1} must be of type SystemAction.", @delegate.Name, hookAttrib.GetType().Name);
                throw new ArgumentException(message, "delegate");
            }
            var obj    = GetInstance(@delegate);
            var action = (Action)@delegate.GetValue(obj);

            return(new DelegateHookMetaData(action, hookAttrib));
        }
Esempio n. 6
0
        public override void SetHook(HookInfo info)
        {
            if (HookInfos.Count > 0)
            {
                throw new InvalidOperationException("WriteBytesHook can not mix with other hook");
            }

            base.SetHook(info);

            HookAttribute hook  = info.GetHookAttribute();
            var           bytes = info.GetReturnValue() as byte[];

            Logger.Log("write bytes: [{0}]", string.Join(", ", bytes));
            MemoryHelper.Write(hook.Address, bytes, bytes.Length);
        }
Esempio n. 7
0
        private void BuildEventBindingFromMethod(MethodInfo method, HookAttribute hookAttr)
        {
            CheckEventBindingMethod(hookAttr.Event, method);

            ApplyForScope(method,
                          scope =>
            {
                var eventBinding = bindingFactory.CreateEventBinding(method, scope);
                GetEvents(hookAttr.Event).Add(eventBinding);
            },
                          hookAttr.Tags == null
                              ? null
                              : hookAttr.Tags.Select(tag => new ScopeAttribute {
                Tag = tag
            })
                          );
        }
Esempio n. 8
0
        public override void SetHook(HookInfo info)
        {
            base.SetHook(info);

            HookAttribute hook = MaxHookInfo.GetHookAttribute();

            var callable = (int)MaxHookInfo.GetCallable();

            Logger.Log("ares hook callable: 0x{0:X}", callable);

            int pMemory = (int)GetMemory(code_call.Length + hook.Size + ASM.Jmp.Length);

            Logger.Log("AresHookTransferStation alloc: 0x{0:X}", pMemory);

            if (pMemory != (int)IntPtr.Zero)
            {
                MemoryHelper.Write(pMemory, code_call, code_call.Length);

                MemoryHelper.Write(pMemory + 3, hook.Address);
                ASMWriter.WriteCall(new JumpStruct(pMemory + 0xF, callable));

                var origin_code_offset = pMemory + code_call.Length;

                if (hook.Size > 0)
                {                 // write origin code
                    MemoryHelper.Write(origin_code_offset, code_over, hook.Size);
                    // protect relative jmp or call
                    if (code_over[0] == ASM.Jmp[0] || code_over[0] == ASM.Call[0])
                    {
                        int destination = 0;
                        MemoryHelper.Read(hook.Address + 1, ref destination);
                        destination = hook.Address + 5 + destination;

                        MemoryHelper.Write(origin_code_offset + 1, new JumpStruct(origin_code_offset, destination).Offset);
                    }
                }

                var jmp_back_offset = origin_code_offset + hook.Size;
                ASMWriter.WriteJump(new JumpStruct(jmp_back_offset, hook.Address + hook.Size));

                ASMWriter.WriteJump(new JumpStruct(hook.Address, pMemory));

                ASMWriter.FlushInstructionCache(pMemory, memoryHandle.Size);
            }
        }
Esempio n. 9
0
        private static void Init(Action orig)
        {
            orig();
            HookAttribute.DisableHooks(typeof(SceneCatalogHooks));

            var lookups = SceneCatalog.allSceneDefs.ToDictionary(sd => sd.baseSceneName);

            Logger.LogInfo("Lodded dictionary for sceneNameOverride doping");

            var sceneDefinitions = RainOfStages.Instance.SceneDefinitions.OfType <SceneDefinition>();

            {
                var maps      = sceneDefinitions.SelectMany(destination => destination.destionationInjections.Select(origin => (destination, origin)));
                var mapGroups = maps.GroupBy(map => map.origin.baseSceneName);

                foreach (var mapGroup in mapGroups)
                {
                    var destinations = lookups[mapGroup.Key].destinations = lookups[mapGroup.Key].destinations.Union(mapGroup.Select(map => map.destination as SceneDef)).Distinct().ToArray();
                    foreach (var destination in destinations)
                    {
                        Logger.LogMessage($"Added destination {destination.baseSceneName} to SceneDef {mapGroup.Key}");
                    }
                }
            }

            {
                var maps      = sceneDefinitions.SelectMany(loadedSceneDef => loadedSceneDef.reverseSceneNameOverrides.Select(overridedScene => (loadedSceneDef, overridedScene)));
                var mapGroups = maps.GroupBy(map => map.overridedScene.baseSceneName);

                foreach (var mapGroup in mapGroups)
                {
                    var overridingScenes = lookups[mapGroup.Key].sceneNameOverrides = lookups[mapGroup.Key].sceneNameOverrides.Union(mapGroup.Select(map => map.loadedSceneDef.baseSceneName)).Distinct().ToList();

                    foreach (var overridingScene in overridingScenes)
                    {
                        Logger.LogMessage($"Added override {overridingScene} to SceneDef {mapGroup.Key}");
                    }
                }
            }
        }
Esempio n. 10
0
        public static void Init()
        {
            if (IsInitialized)
            {
                return;
            }
            IsInitialized = true;

            const BindingFlags bf_All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

            MethodInfo[] methods         = ModContent.Types.ModContentHooks.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
            Type         t_Hooks         = typeof(ModContentHooks);
            Type         t_HookAttribute = typeof(HookAttribute);

            for (int i = 0; i < methods.Length; i++)
            {
                MethodInfo mHook      = methods[i];
                object[]   mHookInfos = mHook.GetCustomAttributes(t_HookAttribute, true);
                if (mHookInfos.Length == 0)
                {
                    continue;
                }
                HookAttribute mHookInfo = (HookAttribute)mHookInfos[0];

                ParameterInfo[] args     = mHook.GetParameters();
                Type[]          argTypes = new Type[args.Length];
                for (int ai = 0; ai < args.Length; ai++)
                {
                    argTypes[ai] = args[ai].ParameterType;
                }

                MethodInfo mTarget     = mHookInfo.Type.GetMethod(mHook.Name, bf_All, null, argTypes, null);
                MethodInfo mTrampoline = t_Hooks.GetMethod("t_" + mHook.Name, bf_All, null, argTypes, null);

                mTarget.Detour(mHook);
                mTrampoline.Detour(mTarget.CreateOrigTrampoline());
            }
        }
Esempio n. 11
0
        private MethodInfo GetTestHook(Type attributeType)
        {
            HookAttribute hook = (HookAttribute)Attribute.GetCustomAttribute(_type, attributeType);

            return(_type.GetMethod(hook.Method));
        }
Esempio n. 12
0
 protected HookMetaData(HookAttribute hookAttrib)
 {
     HookAttrib = hookAttrib;
 }
Esempio n. 13
0
 public MethodHookMetaData(MethodInfo method, HookAttribute hookAttrib)
     : base(hookAttrib)
 {
     this.method = method;
     instance    = Activator.CreateInstance(method.DeclaringType);
 }
Esempio n. 14
0
 public DelegateHookMetaData(Action action, HookAttribute hookAttrib)
     : base(hookAttrib)
 {
     this.action = action;
 }
Esempio n. 15
0
        private void BuildEventBindingFromMethod(MethodInfo method, HookAttribute hookAttr)
        {
            CheckEventBindingMethod(hookAttr.Event, method);

            ApplyForScope(method,
                          scope =>
                              {
                                  var eventBinding = bindingFactory.CreateEventBinding(new RuntimeBindingMethod(method), scope);
                                  GetHookList(hookAttr.Event).Add(eventBinding);
                              },
                          hookAttr.Tags == null
                              ? null
                              : hookAttr.Tags.Select(tag => new ScopeAttribute { Tag = tag })
                );
        }