private static void writeToAutoHotkeyFile()
 {
     if (!SettingsManager.settingExists("AutoHotkeySettings"))
     {
         return;
     }
     using (StreamWriter writer = new StreamWriter(Constants.AutohotkeyFile)) {
         writer.WriteLine("#SingleInstance force");
         if (ProcessManager.IsFlashClient())
         {
             Process p = ProcessManager.GetTibiaProcess();
             writer.WriteLine("SetTitleMatchMode 2");
             writer.WriteLine(String.Format("#IfWinActive Tibia Flash Client", p == null ? 0 : p.Id));
         }
         else
         {
             writer.WriteLine("#IfWinActive ahk_class TibiaClient");
         }
         foreach (string l in SettingsManager.getSetting("AutoHotkeySettings"))
         {
             string line = l.ToLower();
             if (line.Length == 0 || line[0] == ';')
             {
                 continue;
             }
             if (line.Contains("suspend"))
             {
                 // if the key is set to suspend the hotkey layout, we set it up so it sends a message to us
                 writer.WriteLine(modifyKeyString(line.ToLower().Split(new string[] { "suspend" }, StringSplitOptions.None)[0], l));
                 writer.WriteLine("suspend");
                 writer.WriteLine("if (A_IsSuspended)");
                 // message 32 is suspend
                 writer.WriteLine("PostMessage, 0x317,32,32,,Tibialyzer");
                 writer.WriteLine("else");
                 // message 33 is not suspended
                 writer.WriteLine("PostMessage, 0x317,33,33,,Tibialyzer");
                 writer.WriteLine("return");
             }
             else
             {
                 writer.WriteLine(modifyKeyString(line, l));
             }
         }
     }
 }
Exemple #2
0
        public static ReadMemoryResults ReadMemory()
        {
            ReadMemoryResults results = null;
            SYSTEM_INFO       sys_info;

            GetSystemInfo(out sys_info);

            IntPtr proc_min_address = sys_info.minimumApplicationAddress;
            IntPtr proc_max_address = sys_info.maximumApplicationAddress;

            long proc_min_address_l = (long)proc_min_address;
            long proc_max_address_l = (long)proc_max_address;

            Process[] processes = ProcessManager.GetTibiaProcesses();
            if (processes == null || processes.Length == 0)
            {
                return(null);
            }

            results               = new ReadMemoryResults();
            flashClient           = ProcessManager.IsFlashClient();
            skipDuplicateCommands = (flashClient || !SettingsManager.getSettingBool("ScanInternalTabStructure")) && SettingsManager.getSettingBool("SkipDuplicateCommands");
            Dictionary <int, HashSet <long> > newWhitelistedAddresses = null;

            Interlocked.Exchange(ref newWhitelistedAddresses, whiteListedAddresses);

            foreach (Process process in processes)
            {
                if (!flashClient && SettingsManager.getSettingBool("ScanInternalTabStructure"))
                {
                    ReadMemoryInternal(process, results);
                    UseInternalScan = true;
                }
                else
                {
                    ReadMemoryWhiteList(process, newWhitelistedAddresses, flashClient, results);
                    UseInternalScan = false;
                }
                process.Dispose();
            }
            FinalCleanup(results);
            return(results);
        }
Exemple #3
0
 private void ShowTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     if (alwaysShow)
     {
         return;
     }
     if (ProcessManager.IsFlashClient())
     {
         return;
     }
     try {
         if (showTimer == null || this.IsDisposed || this.Disposing)
         {
             return;
         }
         // only show the suspended window when tibia is active
         bool visible = GetActiveProcessFileName() == ProcessManager.GetTibiaProcess().ProcessName;
         this.BeginInvoke((MethodInvoker) delegate {
             this.Visible = visible;
         });
     } catch {
     }
 }
Exemple #4
0
 private void ShowTimer_Tick(object sender, EventArgs e)
 {
     if (alwaysShow)
     {
         return;
     }
     if (ProcessManager.IsFlashClient())
     {
         return;
     }
     try {
         if (showTimer == null || this.IsDisposed || this.Disposing)
         {
             return;
         }
         // only show the suspended window when tibia is active
         bool visible = ProcessManager.IsTibiaActive();
         this.BeginInvoke((MethodInvoker) delegate {
             this.Visible = alwaysShow ? true : visible;
         });
     } catch {
     }
 }
Exemple #5
0
        public static Bitmap takeScreenshot()
        {
            try {
                RECT Rect = new RECT();
                if (ProcessManager.IsFlashClient())
                {
                    Screen screen = Screen.FromControl(MainForm.mainForm);

                    Rect.left   = screen.Bounds.Left;
                    Rect.right  = screen.Bounds.Right;
                    Rect.top    = screen.Bounds.Top;
                    Rect.bottom = screen.Bounds.Bottom;
                }
                else
                {
                    Process tibia_process = ProcessManager.GetTibiaProcess();
                    if (tibia_process == null)
                    {
                        return(null);                       //no tibia to take screenshot of
                    }
                    if (!GetWindowRect(tibia_process.MainWindowHandle, ref Rect))
                    {
                        return(null);
                    }
                }

                Bitmap bitmap = new Bitmap(Rect.right - Rect.left, Rect.bottom - Rect.top);
                using (Graphics gr = Graphics.FromImage(bitmap)) {
                    gr.CopyFromScreen(new Point(Rect.left, Rect.top), Point.Empty, bitmap.Size);
                }
                return(bitmap);
            } catch (Exception ex) {
                MainForm.mainForm.DisplayWarning("Failed to take screenshot: " + ex.Message);
                return(null);
            }
        }
        /// <summary>
        /// Scan the memory for any chunks that are missing from the whitelist table
        /// </summary>
        public static void ScanMissingChunks()
        {
            if (UseInternalScan)
            {
                return;                  // If we are scanning the internal tabs structure, we do not need to scan missing chunks
            }
            SYSTEM_INFO sys_info;

            GetSystemInfo(out sys_info);

            IntPtr proc_min_address = sys_info.minimumApplicationAddress;
            IntPtr proc_max_address = sys_info.maximumApplicationAddress;

            long proc_min_address_l = (long)proc_min_address;
            long proc_max_address_l = (long)proc_max_address;

            long sys_min_address_l = (long)proc_min_address;

            Process[] processes = ProcessManager.GetTibiaProcesses();
            if (processes == null || processes.Length == 0)
            {
                return;
            }
            flashClient = ProcessManager.IsFlashClient();
            var newWhitelistedAddresses = whiteListedAddresses.ToDictionary(x => x.Key, x => new HashSet <long>(x.Value));

            foreach (Process process in processes)
            {
                HashSet <long> whitelist;
                if (!newWhitelistedAddresses.TryGetValue(process.Id, out whitelist))
                {
                    whitelist = new HashSet <long>();
                    newWhitelistedAddresses[process.Id] = whitelist;
                }

                proc_min_address_l = sys_min_address_l;

                IntPtr processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_WM_READ, false, process.Id);
                MEMORY_BASIC_INFORMATION mem_basic_info;
                List <int> stamps    = TimestampManager.getLatestStamps(3, ignoreStamp);
                int        bytesRead = 0; // number of bytes read with ReadProcessMemory

                try {
                    while (proc_min_address_l < proc_max_address_l)
                    {
                        proc_min_address = new IntPtr(proc_min_address_l);
                        // 28 = sizeof(MEMORY_BASIC_INFORMATION)
                        VirtualQueryEx(processHandle, proc_min_address, out mem_basic_info, 28);

                        long addr = (long)proc_min_address;
                        // check if this memory chunk is accessible
                        if (mem_basic_info.Protect == PAGE_READWRITE && mem_basic_info.State == MEM_COMMIT)
                        {
                            if (!whitelist.Contains(addr))
                            {
                                if (missingChunksBuffer == null || missingChunksBuffer.Length < mem_basic_info.RegionSize)
                                {
                                    missingChunksBuffer = new byte[mem_basic_info.RegionSize];
                                }

                                // read everything in the buffer above
                                ReadProcessMemory((int)processHandle, mem_basic_info.BaseAddress, missingChunksBuffer, mem_basic_info.RegionSize, ref bytesRead);
                                // scan the memory for strings that start with timestamps and end with the null terminator ('\0')
                                IEnumerable <string> timestampLines;
                                if (!flashClient)
                                {
                                    timestampLines = Parser.FindTimestamps(missingChunksBuffer, bytesRead);
                                    // if there are any timestamps found, add the address to the list of whitelisted addresses
                                    if (timestampLines.Any(x => stamps.Contains(TimestampManager.getStamp(int.Parse(x.Substring(0, 2)), int.Parse(x.Substring(3, 2))))))
                                    {
                                        whitelist.Add(addr);
                                    }
                                }
                                else
                                {
                                    if (Parser.HasAnyValidTimestampsFlash(missingChunksBuffer, bytesRead, stamps))
                                    {
                                        whitelist.Add(addr);
                                    }
                                }
                            }
                        }
                        // move to the next memory chunk
                        proc_min_address_l += mem_basic_info.RegionSize;
                    }
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            Interlocked.Exchange(ref whiteListedAddresses, newWhitelistedAddresses);
        }
Exemple #7
0
        public static ReadMemoryResults ReadMemory()
        {
            ReadMemoryResults results  = null;
            SYSTEM_INFO       sys_info = new SYSTEM_INFO();

            GetSystemInfo(out sys_info);

            IntPtr proc_min_address = sys_info.minimumApplicationAddress;
            IntPtr proc_max_address = sys_info.maximumApplicationAddress;

            long proc_min_address_l = (long)proc_min_address;
            long proc_max_address_l = (long)proc_max_address;

            Process[] processes = ProcessManager.GetTibiaProcesses();
            if (processes == null || processes.Length == 0)
            {
                // Tibia process could not be found, wait for a bit and return
                Thread.Sleep(250);
                return(null);
            }
            int scanSpeed = SettingsManager.getSettingInt("ScanSpeed");

            results     = new ReadMemoryResults();
            flashClient = ProcessManager.IsFlashClient();
            foreach (Process process in processes)
            {
                if (!whitelistedAddresses.ContainsKey(process.Id))
                {
                    continue;
                }
                List <long> whitelist = whitelistedAddresses[process.Id];

                IntPtr processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_WM_READ, false, process.Id);

                int bytesRead = 0;  // number of bytes read with ReadProcessMemory
                for (int i = 0; i < whitelist.Count; i++)
                {
                    long addr = whitelist[i];
                    if (addr < 0)
                    {
                        continue;
                    }

                    proc_min_address = new IntPtr(addr);

                    MEMORY_BASIC_INFORMATION mem_basic_info;
                    VirtualQueryEx(processHandle, proc_min_address, out mem_basic_info, 28);

                    if (mem_basic_info.Protect == PAGE_READWRITE && mem_basic_info.State == MEM_COMMIT)
                    {
                        if (memoryBuffer == null || memoryBuffer.Length < mem_basic_info.RegionSize)
                        {
                            memoryBuffer = new byte[mem_basic_info.RegionSize];
                        }

                        // read everything in the buffer above
                        ReadProcessMemory((int)processHandle, mem_basic_info.BaseAddress, memoryBuffer, mem_basic_info.RegionSize, ref bytesRead);
                        // scan the memory for strings that start with timestamps and end with the null terminator ('\0')
                        IEnumerable <string> timestampLines;
                        if (!flashClient)
                        {
                            timestampLines = Parser.FindTimestamps(memoryBuffer, bytesRead);
                        }
                        else
                        {
                            timestampLines = Parser.FindTimestampsFlash(memoryBuffer, bytesRead);
                        }

                        if (!SearchChunk(timestampLines, results))
                        {
                            whitelist[i] = -1;
                        }

                        // performance throttling sleep after every scan (depending on scanSpeed setting)
                        if (scanSpeed > 0)
                        {
                            Thread.Sleep(scanSpeed);
                        }
                    }
                }
                process.Dispose();
            }

            FinalCleanup(results);
            return(results);
        }
Exemple #8
0
        /// <summary>
        /// Scan the memory for any chunks that are missing from the whitelist table
        /// </summary>
        public static void ScanMissingChunks()
        {
            SYSTEM_INFO sys_info;

            GetSystemInfo(out sys_info);

            IntPtr proc_min_address = sys_info.minimumApplicationAddress;
            IntPtr proc_max_address = sys_info.maximumApplicationAddress;

            long proc_min_address_l = (long)proc_min_address;
            long proc_max_address_l = (long)proc_max_address;

            long sys_min_address_l = (long)proc_min_address;


            Process[] processes = ProcessManager.GetTibiaProcesses();
            if (processes == null || processes.Length == 0)
            {
                // Tibia process could not be found, wait for a bit and return
                Thread.Sleep(250);
                return;
            }
            flashClient = ProcessManager.IsFlashClient();
            foreach (Process process in processes)
            {
                if (!whitelistedAddresses.ContainsKey(process.Id))
                {
                    whitelistedAddresses.Add(process.Id, new List <long>());
                }
                List <long> whitelist = whitelistedAddresses[process.Id];

                proc_min_address_l = sys_min_address_l;

                IntPtr processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_WM_READ, false, process.Id);
                MEMORY_BASIC_INFORMATION mem_basic_info = new MEMORY_BASIC_INFORMATION();
                int bytesRead = 0;  // number of bytes read with ReadProcessMemory
                int scanSpeed = SettingsManager.getSettingInt("ScanSpeed");
                try {
                    while (proc_min_address_l < proc_max_address_l)
                    {
                        proc_min_address = new IntPtr(proc_min_address_l);
                        // 28 = sizeof(MEMORY_BASIC_INFORMATION)
                        VirtualQueryEx(processHandle, proc_min_address, out mem_basic_info, 28);

                        long addr = (long)proc_min_address;
                        // check if this memory chunk is accessible
                        if (mem_basic_info.Protect == PAGE_READWRITE && mem_basic_info.State == MEM_COMMIT)
                        {
                            if (!whitelist.Contains(addr))
                            {
                                if (missingChunksBuffer == null || missingChunksBuffer.Length < mem_basic_info.RegionSize)
                                {
                                    missingChunksBuffer = new byte[mem_basic_info.RegionSize];
                                }

                                // read everything in the buffer above
                                ReadProcessMemory((int)processHandle, mem_basic_info.BaseAddress, missingChunksBuffer, mem_basic_info.RegionSize, ref bytesRead);
                                // scan the memory for strings that start with timestamps and end with the null terminator ('\0')
                                IEnumerable <string> timestampLines;
                                if (!flashClient)
                                {
                                    timestampLines = Parser.FindTimestamps(missingChunksBuffer, bytesRead);
                                }
                                else
                                {
                                    timestampLines = Parser.FindTimestampsFlash(missingChunksBuffer, bytesRead);
                                }

                                // if there are any timestamps found, add the address to the list of whitelisted addresses
                                foreach (string str in timestampLines)
                                {
                                    whitelist.Add(addr);
                                    break;
                                }

                                // performance throttling sleep after every scan (depending on scanSpeed setting)
                                if (!initialScan)
                                {
                                    Thread.Sleep(10 + scanSpeed);
                                }
                                else if (scanSpeed > 50)
                                {
                                    Thread.Sleep(scanSpeed - 50);
                                }
                            }
                        }
                        // move to the next memory chunk
                        proc_min_address_l += mem_basic_info.RegionSize;
                    }
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }
            initialScan = false;
        }
Exemple #9
0
        public static void ParseLootMessages(Hunt h, Dictionary <string, List <string> > newDrops, List <Tuple <Creature, List <Tuple <Item, int> >, string> > newItems, bool commit = true, bool switchHunt = false, bool addEverything = false)
        {
            SQLiteTransaction transaction = null;

            if (commit)
            {
                transaction = LootDatabaseManager.BeginTransaction();
            }

            int stamp = TimestampManager.getDayStamp();
            Dictionary <string, List <string> > itemDrops = addEverything ? new Dictionary <string, List <string> >() : globalMessages;
            bool skipDuplicateLoot = (ProcessManager.IsFlashClient() || !SettingsManager.getSettingBool("ScanInternalTabStructure")) && SettingsManager.getSettingBool("SkipDuplicateLoot");

            // now the big one: parse the log messages and check the dropped items
            foreach (KeyValuePair <string, List <string> > kvp in newDrops)
            {
                string        t        = kvp.Key;
                List <string> itemList = skipDuplicateLoot ? kvp.Value.Distinct().ToList() : kvp.Value;
                if (!itemDrops.ContainsKey(t))
                {
                    itemDrops.Add(t, new List <string>());
                }
                if (itemList.Count > itemDrops[t].Count)
                {
                    int hour   = int.Parse(t.Substring(0, 2));
                    int minute = int.Parse(t.Substring(3, 2));
                    foreach (string message in itemList)
                    {
                        if (!itemDrops[t].Contains(message))
                        {
                            // new log message, scan it for new items
                            Tuple <Creature, List <Tuple <Item, int> > > resultList = ParseLootMessage(message);
                            if (resultList == null)
                            {
                                continue;
                            }

                            Creature cr = resultList.Item1;

                            if (switchHunt && commit)
                            {
                                h = HuntManager.CheckTrackedHunts(h, resultList, t, message, stamp, hour, minute, transaction);
                            }

                            HuntManager.AddKillToHunt(h, resultList, t, message, stamp, hour, minute, transaction);
                            if (newItems != null && MainForm.fileWriter != null && SettingsManager.getSettingBool("AutomaticallyWriteLootToFile"))
                            {
                                MainForm.fileWriter.WriteLine(message);
                                MainForm.fileWriter.Flush();
                            }

                            if (newItems != null)
                            {
                                newItems.Add(new Tuple <Creature, List <Tuple <Item, int> >, string>(resultList.Item1, resultList.Item2, message));
                            }
                        }
                        else
                        {
                            itemDrops[t].Remove(message);
                        }
                    }
                    itemDrops[t] = itemList;
                }
            }
            if (transaction != null)
            {
                transaction.Commit();
            }
        }
Exemple #10
0
        public static ReadMemoryResults ReadMemory()
        {
            ReadMemoryResults results = null;
            SYSTEM_INFO       sys_info;

            GetSystemInfo(out sys_info);

            IntPtr proc_min_address = sys_info.minimumApplicationAddress;
            IntPtr proc_max_address = sys_info.maximumApplicationAddress;

            long proc_min_address_l = (long)proc_min_address;
            long proc_max_address_l = (long)proc_max_address;

            Process[] processes = ProcessManager.GetTibiaProcesses();
            if (processes == null || processes.Length == 0)
            {
                return(null);
            }

            results     = new ReadMemoryResults();
            flashClient = ProcessManager.IsFlashClient();
            Dictionary <int, HashSet <long> > newWhitelistedAddresses = null;

            Interlocked.Exchange(ref newWhitelistedAddresses, whiteListedAddresses);
            foreach (Process process in processes)
            {
                HashSet <long> whitelist;
                if (!newWhitelistedAddresses.TryGetValue(process.Id, out whitelist))
                {
                    continue;
                }

                IntPtr processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_WM_READ, false, process.Id);

                int bytesRead = 0;  // number of bytes read with ReadProcessMemory
                foreach (long addr in whitelist)
                {
                    proc_min_address = new IntPtr(addr);

                    MEMORY_BASIC_INFORMATION mem_basic_info;
                    VirtualQueryEx(processHandle, proc_min_address, out mem_basic_info, 28);

                    if (mem_basic_info.Protect == PAGE_READWRITE && mem_basic_info.State == MEM_COMMIT)
                    {
                        if (memoryBuffer == null || memoryBuffer.Length < mem_basic_info.RegionSize)
                        {
                            memoryBuffer = new byte[mem_basic_info.RegionSize];
                        }

                        // read everything in the buffer above
                        ReadProcessMemory((int)processHandle, mem_basic_info.BaseAddress, memoryBuffer, mem_basic_info.RegionSize, ref bytesRead);
                        // scan the memory for strings that start with timestamps and end with the null terminator ('\0')
                        IEnumerable <string> timestampLines;
                        if (!flashClient)
                        {
                            timestampLines = Parser.FindTimestamps(memoryBuffer, bytesRead);
                        }
                        else
                        {
                            timestampLines = Parser.FindTimestampsFlash(memoryBuffer, bytesRead);
                        }

                        SearchChunk(timestampLines, results);
                    }
                }

                process.Dispose();
            }
            FinalCleanup(results);
            return(results);
        }