Example #1
0
        private void DoWork()
        {
            while (isHooked)
            {
                // do not do anything while we are in a loadingscreen
                // WoW doesn't like that and will crash
                if (AmeisenCore.IsInLoadingScreen())
                {
                    Thread.Sleep(50);
                    continue;
                }

                if (!hookJobs.IsEmpty)
                {
                    if (hookJobs.TryDequeue(out HookJob currentJob))
                    {
                        // process a hook job
                        InjectAndExecute(currentJob.Asm, currentJob.ReadReturnBytes, out bool wasJobSuccessful);

                        // if its a chained hook job, execute it too
                        if (currentJob.GetType() == typeof(ReturnHookJob) && wasJobSuccessful)
                        {
                            currentJob.ReturnValue = InjectAndExecute(
                                ((ReturnHookJob)currentJob).ChainedJob.Asm,
                                ((ReturnHookJob)currentJob).ChainedJob.ReadReturnBytes,
                                out bool wasChainedJobSuccessful);
                        }

                        currentJob.IsFinished = true;
                    }
                }
                Thread.Sleep(1);
            }
        }
Example #2
0
 public void Stop()
 {
     if (IsActive)
     {
         AmeisenCore.LuaDoString($"abFrame:UnregisterAllEvents();");
         AmeisenCore.LuaDoString("abFrame:SetScript(\"OnEvent\", nil)");
         IsActive = false;
         EventReader.Join();
     }
 }
Example #3
0
        private void ReadEvents()
        {
            while (IsActive)
            {
                string eventString = AmeisenCore.GetLocalizedText($"ameisenbotEvent = {LUA_EVENTNAME}(1)", "ameisenbotEvent");
                if (eventString != "")
                {
                    AmeisenLogger.Instance.Log(LogLevel.DEBUG, "LUA Event Fired: " + eventString, this);
                }

                Thread.Sleep(500);
            }
        }
Example #4
0
        private void ReadEvents()
        {
            while (IsActive)
            {
                if (AmeisenCore.IsInLoadingScreen())
                {
                    Thread.Sleep(50);
                    continue;
                }

                // Unminified lua code can be found im my github repo "WowLuaStuff"
                string eventJson = AmeisenCore.GetLocalizedText("abEventJson='['for a,b in pairs(abEventTable)do abEventJson=abEventJson..'{'for c,d in pairs(b)do if type(d)==\"table\"then abEventJson=abEventJson..'\"args\": ['for e,f in pairs(d)do abEventJson=abEventJson..'\"'..f..'\"'if e<=table.getn(d)then abEventJson=abEventJson..','end end;abEventJson=abEventJson..']}'if a<table.getn(abEventTable)then abEventJson=abEventJson..','end else if type(d)==\"string\"then abEventJson=abEventJson..'\"event\": \"'..d..'\",'else abEventJson=abEventJson..'\"time\": \"'..d..'\",'end end end end;abEventJson=abEventJson..']'abEventTable={}", "abEventJson");
                AmeisenLogger.Instance.Log(LogLevel.VERBOSE, $"LUA Events Json: {eventJson}", this);

                List <RawEvent> rawEvents = new List <RawEvent>();
                try
                {
                    // parse the events from JSON
                    List <RawEvent> finalEvents = new List <RawEvent>();
                    rawEvents = JsonConvert.DeserializeObject <List <RawEvent> >(eventJson);

                    foreach (RawEvent rawEvent in rawEvents)
                    {
                        if (!finalEvents.Contains(rawEvent))
                        {
                            finalEvents.Add(rawEvent);
                        }
                    }

                    // Fire the events
                    AmeisenLogger.Instance.Log(LogLevel.VERBOSE, $"Parsed {finalEvents.Count} events", this);
                    if (finalEvents.Count > 0)
                    {
                        foreach (RawEvent rawEvent in finalEvents)
                        {
                            if (EventDictionary.ContainsKey(rawEvent.@event))
                            {
                                EventDictionary[rawEvent.@event].Invoke(rawEvent.time, rawEvent.args);
                                AmeisenLogger.Instance.Log(LogLevel.VERBOSE, $"Fired OnEventFired: {rawEvent.@event}", this);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    AmeisenLogger.Instance.Log(LogLevel.ERROR, $"Failed to parse events Json: {e}", this);
                }

                Thread.Sleep(1000);
            }
        }
Example #5
0
        public void Init()
        {
            //StringBuilder luaStuff = new StringBuilder();
            AmeisenCore.LuaDoString($"{LUA_FRAME} = CreateFrame('Frame','{LUA_FRAME}');{LUA_FRAME}:SetScript('OnEvent',{LUA_EVENTRECEIVED});{LUA_TABLE}={"{}"};");
            AmeisenCore.LuaDoString($"function {LUA_REGISTER}(e){LUA_FRAME}:RegisterEvent(e);end");
            AmeisenCore.LuaDoString($"function {LUA_UNREGISTER}(e){LUA_FRAME}:UnregisterEvent(e);end");
            AmeisenCore.LuaDoString($"function {LUA_INFO}(e,d)table.insert({LUA_TABLE}, {"{e,time(),d}"});end");
            AmeisenCore.LuaDoString($"function {LUA_EVENTRECEIVED}(s,e,...){LUA_INFO}(e,{"{...}"});end");
            AmeisenCore.LuaDoString($"function {LUA_EVENTCOUNT}()return {LUA_TABLE}.count;end");
            AmeisenCore.LuaDoString($"function {LUA_EVENTREMOVE}()table.wipe({LUA_TABLE});end");
            AmeisenCore.LuaDoString($"function {LUA_EVENTNAME}(i)local ret;ret={LUA_TABLE}[i];{LUA_TABLE}.remove(i);return ret;end");
            //AmeisenCore.LuaDoString(luaStuff.ToString());

            IsActive = true;
            EventReader.Start();
        }
Example #6
0
        /// <summary>
        /// Start to receive events to our event table
        /// and start the event reader that will fire
        /// events if they apper in our event table.
        ///
        /// Events will get read every 1000 ms by now.
        /// </summary>
        public void Init()
        {
            StringBuilder luaStuff = new StringBuilder();

            luaStuff.Append("abFrame = CreateFrame(\"FRAME\", \"AbotEventFrame\") ");
            luaStuff.Append("abEventTable = {} ");
            luaStuff.Append("function abEventHandler(self, event, ...) ");
            luaStuff.Append("table.insert(abEventTable, {time(), event, {...}}) end ");
            luaStuff.Append("if abFrame:GetScript(\"OnEvent\") == nil then ");
            luaStuff.Append("abFrame:SetScript(\"OnEvent\", abEventHandler) end");
            AmeisenCore.LuaDoString(luaStuff.ToString());

            IsActive = true;

            EventReader.Start();
            // if we equip an item confirm the dialog
            AmeisenCore.EnableAutoBoPConfirm();
        }
Example #7
0
        public AmeisenHook(BlackMagic blackmagic)
        {
            BlackMagic = blackmagic;
            Hook();
            hookJobs   = new ConcurrentQueue <HookJob>();
            hookWorker = new Thread(new ThreadStart(DoWork));

            // wait for the world to be loaded
            while (!AmeisenCore.IsWorldLoaded())
            {
                Thread.Sleep(200);
            }

            IsNotInWorld = false;

            if (isHooked)
            {
                hookWorker.Start();
            }
        }
Example #8
0
 public void Unsubscribe(string eventName)
 {
     AmeisenCore.LuaDoString($"{LUA_UNREGISTER}('{eventName}')");
 }
Example #9
0
 /// <summary>
 /// Unsubscribe from an event
 /// </summary>
 /// <param name="eventName">event name</param>
 public void Unsubscribe(string eventName)
 {
     AmeisenCore.LuaDoString($"abFrame:UnregisterEvent(\"{eventName}\");");
     EventDictionary.Remove(eventName);
 }
Example #10
0
 /// <summary>
 /// Subscribe to an event
 /// </summary>
 /// <param name="eventName">event name</param>
 /// <param name="onEventFired">method to fire when the event appered in WoW</param>
 public void Subscribe(string eventName, OnEventFired onEventFired)
 {
     AmeisenCore.LuaDoString($"abFrame:RegisterEvent(\"{eventName}\");");
     EventDictionary.Add(eventName, onEventFired);
 }
Example #11
0
        /// <summary>
        /// Inject assembly code on our hook
        /// </summary>
        /// <param name="asm">assembly to execute</param>
        /// <param name="readReturnBytes">should the return bytes get read</param>
        /// <param name="successful">if the reading of return bytes was successful</param>
        /// <returns></returns>
        private byte[] InjectAndExecute(string[] asm, bool readReturnBytes, out bool successful)
        {
            List <byte> returnBytes = new List <byte>();

            try
            {
                // wait for the code to be executed
                while (isInjectionUsed || AmeisenCore.IsInLoadingScreen() || BlackMagic.ReadInt(codeToExecute) > 0)
                {
                    Thread.Sleep(5);
                }

                isInjectionUsed = true;

                // preparing to inject the given ASM
                BlackMagic.Asm.Clear();
                // add all lines
                foreach (string s in asm)
                {
                    BlackMagic.Asm.AddLine(s);
                }

                // now there is code to be executed
                BlackMagic.WriteInt(codeToExecute, 1);
                // inject it
                BlackMagic.Asm.Inject(codeCaveForInjection);

                // we don't need this atm
                //AmeisenManager.Instance().GetBlackMagic().Asm.AddLine("JMP " + (endsceneReturnAddress));
                //int asmLenght = BlackMagic.Asm.Assemble().Length;

                // wait for the code to be executed
                while (BlackMagic.ReadInt(codeToExecute) > 0)
                {
                    Thread.Sleep(1);
                }

                // if we want to read the return value do it otherwise we're done
                if (readReturnBytes)
                {
                    byte buffer = new byte();
                    try
                    {
                        // get our return parameter address
                        uint dwAddress = BlackMagic.ReadUInt(returnAdress);

                        // read all parameter-bytes until we the buffer is 0
                        buffer = BlackMagic.ReadByte(dwAddress);
                        while (buffer != 0)
                        {
                            returnBytes.Add(buffer);
                            dwAddress = dwAddress + 1;
                            buffer    = BlackMagic.ReadByte(dwAddress);
                        }
                    }
                    catch (Exception e)
                    {
                        AmeisenLogger.Instance.Log(
                            LogLevel.ERROR,
                            $"Crash at reading returnAddress: {e.ToString()}",
                            this);
                    }
                }
            }
            catch (Exception e)
            {
                // now there is no more code to be executed
                BlackMagic.WriteInt(codeToExecute, 0);
                successful = false;

                AmeisenLogger.Instance.Log(
                    LogLevel.ERROR,
                    $"Crash at InjectAndExecute: {e.ToString()}",
                    this);

                foreach (string s in asm)
                {
                    AmeisenLogger.Instance.Log(
                        LogLevel.ERROR,
                        $"ASM Content: {s}",
                        this);
                }

                AmeisenLogger.Instance.Log(
                    LogLevel.ERROR,
                    $"ReadReturnBytes: {readReturnBytes}",
                    this);
            }

            // now we can use the hook again
            isInjectionUsed = false;
            successful      = true;

            return(returnBytes.ToArray());
        }