Exemplo n.º 1
0
        static void Main(string[] args)
        {
            DateTime        start = DateTime.UtcNow, now = DateTime.MinValue, checkednames = DateTime.MinValue;
            LaMulanaRemake  remake  = new LaMulanaRemake();
            LaMulanaClassic classic = new LaMulanaClassic();

            System.IO.FileInfo rxmlfi1 = null, rxmlfi2, cxmlfi1 = null, cxmlfi2;

            MemoryWatcherList.MemoryWatcherDataChangedEventHandler changerhandler =
                (MemoryWatcher w) => changedr(w.Current, w.Old, w.Name, now - start);
            remake.vars.OnWatcherDataChanged += changerhandler;
            MemoryWatcherList.MemoryWatcherDataChangedEventHandler changechandler =
                (MemoryWatcher w) => changedc(w.Current, w.Old, w.Name, now - start);
            classic.vars.OnWatcherDataChanged += changechandler;

            byte[] rbytes_old = new byte[0x1000], rbytes_new;
            byte[] rwords_old = new byte[510], rwords_new;

            byte[] cflags_old = new byte[875], cflags_new;
            byte[] croms_old  = new byte[1344], croms_new;

            while (true)
            {
                Thread.Sleep(5);
                if (DateTime.UtcNow - checkednames > TimeSpan.FromSeconds(1))
                {
                    rxmlfi2 = new System.IO.FileInfo("names.xml");
                    if (rxmlfi1 == null || rxmlfi1.LastWriteTimeUtc != rxmlfi2.LastWriteTimeUtc)
                    {
                        remakenames = loadnames("names.xml");
                    }
                    cxmlfi2 = new System.IO.FileInfo("cnames.xml");
                    if (cxmlfi1 == null || cxmlfi1.LastWriteTimeUtc != cxmlfi2.LastWriteTimeUtc)
                    {
                        classicnames = loadnames("cnames.xml");
                    }
                    cxmlfi1      = cxmlfi2;
                    checkednames = DateTime.UtcNow;
                }
                try
                {
                    now = DateTime.UtcNow;
                    if (remake.Attach())
                    {
                        // I knew that using the MemoryWatchers for over 4000 variables would be slow but god damn it's slow
                        // let's not do it
                        remake.vars.RemoveAll(x => x.Name.StartsWith("byte") || x.Name.StartsWith("word"));
                        remake.vars.UpdateAll(remake.proc);
                        rbytes_new = remake.readbytes();
                        rwords_new = remake.readwords();
                        for (int i = 100; i < 0x1000; i++)
                        {
                            if (rbytes_new[i] != rbytes_old[i])
                            {
                                changedr(rbytes_new[i], rbytes_old[i], String.Format("byte-{0:x3}", i), now - start);
                            }
                        }
                        for (int i = 0; i < 510; i += 2)
                        {
                            ushort oldval = BitConverter.ToUInt16(rwords_old, i);
                            ushort newval = BitConverter.ToUInt16(rwords_new, i);
                            if (newval != oldval)
                            {
                                changedr(newval, oldval, String.Format("word-{0:x3}", i >> 1), now - start);
                            }
                        }
                        rbytes_old = rbytes_new;
                        rwords_old = rwords_new;
                    }
                    else if (classic.Attach())
                    {
                        classic.vars.UpdateAll(classic.proc);
                        if ((int)classic.vars["igt"].Current <= 0)
                        {
                            continue;
                        }
                        cflags_new = classic.readflags();
                        croms_new  = classic.readroms();
                        for (int i = 5; i < 875; i++)
                        {
                            if (cflags_new[i] != cflags_old[i])
                            {
                                for (int j = 0; j < 8; j++)
                                {
                                    if (((cflags_new[i] ^ cflags_old[i]) & 1 << j) != 0)
                                    {
                                        changedc((cflags_new[i] >> j & 1) != 0, (cflags_old[i] >> j & 1) != 0, String.Format("f-{0:x3}-{1}", i, j), now - start);
                                    }
                                }
                            }
                        }
                        for (int i = 0; i < 1344; i += 4)
                        {
                            if ((i & 4) != 0)
                            {
                                continue;
                            }
                            uint oldval = BitConverter.ToUInt32(croms_old, i);
                            uint newval = BitConverter.ToUInt32(croms_new, i);
                            if (newval != oldval)
                            {
                                changedc(newval, oldval, String.Format("rom-{0:x3}", i), now - start);
                            }
                        }
                        cflags_old = cflags_new;
                        croms_old  = croms_new;
                    }
                }
                catch (Win32Exception e) {
                    if (e.NativeErrorCode == 5 && e.TargetSite.ToString().StartsWith("Microsoft.Win32.SafeHandles.SafeProcessHandle OpenProcess"))
                    {
                        if (!warnedaboutaccess)
                        {
                            System.Console.WriteLine("Unable to access LaMulanaWin.exe, please check the compatibility settings\n"
                                                     + "and uncheck \"Run this program as an administrator\" if it is checked.");
                        }
                        warnedaboutaccess = true;
                    }
                }
                catch (ArgumentNullException) { }
            }
        }
Exemplo n.º 2
0
        public static object DoStuff(LMRItemTracker.LaMulanaItemTrackerForm laMulanaItemTrackerForm, Stream namesXml)
        {
            DateTime       now    = DateTime.MinValue;
            LaMulanaRemake remake = new LaMulanaRemake();

            MemoryWatcherList.MemoryWatcherDataChangedEventHandler changerhandler =
                (MemoryWatcher w) => changed(w.Current, w.Old, w.Name, laMulanaItemTrackerForm);
            remake.vars.OnWatcherDataChanged += changerhandler;

            byte[] rbytes_old = new byte[0x1000], rbytes_new;
            byte[] rwords_old = new byte[510], rwords_new;
            remakenames = loadnames(namesXml);

            while (true)
            {
                Thread.Sleep(5);
                try
                {
                    now = DateTime.UtcNow;
                    if (remake.Attach())
                    {
                        // I knew that using the MemoryWatchers for over 4000 variables would be slow but god damn it's slow
                        // let's not do it
                        remake.vars.RemoveAll(x => x.Name.StartsWith("byte") || x.Name.StartsWith("word"));
                        remake.vars.UpdateAll(remake.proc);
                        rbytes_new = remake.readbytes();
                        rwords_new = remake.readwords();
                        for (int i = 100; i < 0x1000; i++)
                        {
                            if (rbytes_new[i] != rbytes_old[i])
                            {
                                try
                                {
                                    changed(rbytes_new[i], rbytes_old[i], String.Format("byte-{0:x3}", i), laMulanaItemTrackerForm);
                                }
                                catch (Exception ex)
                                {
                                    System.Console.WriteLine(ex.StackTrace);
                                }
                            }
                        }
                        for (int i = 0; i < 510; i += 2)
                        {
                            ushort oldval = BitConverter.ToUInt16(rwords_old, i);
                            ushort newval = BitConverter.ToUInt16(rwords_new, i);
                            if (newval != oldval)
                            {
                                try
                                {
                                    changed(newval, oldval, String.Format("word-{0:x3}", i >> 1), laMulanaItemTrackerForm);
                                }
                                catch (Exception ex)
                                {
                                    System.Console.WriteLine(ex.StackTrace);
                                }
                            }
                        }
                        rbytes_old = rbytes_new;
                        rwords_old = rwords_new;
                    }
                }
                catch (Win32Exception e)
                {
                    if (e.NativeErrorCode == 5 && e.TargetSite.ToString().StartsWith("Microsoft.Win32.SafeHandles.SafeProcessHandle OpenProcess"))
                    {
                        if (!warnedaboutaccess)
                        {
                            System.Console.WriteLine("Unable to access LaMulanaWin.exe, please check the compatibility settings\n"
                                                     + "and uncheck \"Run this program as an administrator\" if it is checked.");
                        }
                        warnedaboutaccess = true;
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.StackTrace);
                }
            }
        }
Exemplo n.º 3
0
        public static object DoStuff(LMItemTracker.LaMulanaItemTrackerForm laMulanaItemTrackerForm, Stream namesXml)
        {
            LaMulanaRemake remake = new LaMulanaRemake();

            MemoryWatcherList.MemoryWatcherDataChangedEventHandler changerhandler =
                (MemoryWatcher w) => changed(w.Current, w.Old, w.Name, laMulanaItemTrackerForm);
            remake.vars.OnWatcherDataChanged += changerhandler;

            byte[] rbytes_old = new byte[0x1000], rbytes_new;
            byte[] rwords_old = new byte[510], rwords_new;
            remakenames = loadnames(namesXml);
            int startupCounter = 1;

            while (true)
            {
                DateTime sleeptarget;
                if (startupCounter == 2)
                {
                    sleeptarget = DateTime.UtcNow.AddMilliseconds(5);
                }
                else
                {
                    sleeptarget = DateTime.UtcNow.AddMilliseconds(100);
                }

                try
                {
                    if (remake.Attach())
                    {
                        // I knew that using the MemoryWatchers for over 4000 variables would be slow but god damn it's slow
                        // let's not do it
                        remake.vars.RemoveAll(x => x.Name.StartsWith("byte") || x.Name.StartsWith("word"));
                        remake.vars.UpdateAll(remake.proc);
                        rbytes_new = remake.readbytes();
                        rwords_new = remake.readwords();
                        if (rbytes_new[824] == 0)
                        {
                            // Player is dead or hasn't started a game.
                            laMulanaItemTrackerForm.setGameStarted(false);
                            sleeptarget    = DateTime.UtcNow.AddMilliseconds(20);
                            startupCounter = 1;
                        }

                        if (startupCounter == 3)
                        {
                            laMulanaItemTrackerForm.setGameStarted(true);
                        }

                        for (int i = 100; i < 0x1000; i++)
                        {
                            if (rbytes_new[i] != rbytes_old[i])
                            {
                                try
                                {
                                    changed(rbytes_new[i], rbytes_old[i], String.Format("byte-{0:x3}", i), laMulanaItemTrackerForm);
                                }
                                catch (Exception ex)
                                {
                                    System.Console.WriteLine(ex.StackTrace);
                                }
                            }
                        }
                        for (int i = 0; i < 510; i += 2)
                        {
                            ushort oldval = BitConverter.ToUInt16(rwords_old, i);
                            ushort newval = BitConverter.ToUInt16(rwords_new, i);
                            if (newval != oldval)
                            {
                                try
                                {
                                    changed(newval, oldval, String.Format("word-{0:x3}", i >> 1), laMulanaItemTrackerForm);
                                }
                                catch (Exception ex)
                                {
                                    System.Console.WriteLine(ex.StackTrace);
                                }
                            }
                        }
                        rbytes_old = rbytes_new;
                        rwords_old = rwords_new;
                        if (startupCounter < 3 && rbytes_new[824] != 0)
                        {
                            ++startupCounter;
                        }
                    }
                }
                catch (Win32Exception e)
                {
                    if (e.NativeErrorCode == 5 && e.TargetSite.ToString().StartsWith("Microsoft.Win32.SafeHandles.SafeProcessHandle OpenProcess"))
                    {
                        if (!warnedaboutaccess)
                        {
                            System.Console.WriteLine("Unable to access LaMulanaWin.exe, please check the compatibility settings\n"
                                                     + "and uncheck \"Run this program as an administrator\" if it is checked.");
                        }
                        warnedaboutaccess = true;

                        //if (!warnedaboutaccess)
                        //{
                        //    laMulanaItemTrackerForm.ShowMessage("Unable to access LaMulanaWin.exe, please check the compatibility settings"
                        //        + " and uncheck \"Run this program as an administrator\" if it is checked.");
                        //    warnedaboutaccess = true;
                        //}
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.StackTrace);
//                    laMulanaItemTrackerForm.ShowMessage("Unexpected error: " + ex.Message);
                }

                TimeSpan sleeptime = sleeptarget - DateTime.UtcNow;
                if (sleeptime > TimeSpan.Zero)
                {
                    Thread.Sleep(sleeptime);
                }
            }
        }