Exemplo n.º 1
0
        public static string GetText(Bitmap imgsource)
        {
            try
            {
                if (useNetworkOCR)
                {
                    return(Networking.NetworkOCR(imgsource, networkOCRIP, networkOCRPort));
                }
            }
            catch (SocketException)
            {
                PluginAPI.WriteLine("Remote OCR server error. Defaulting to the local engine.");
                if (eng == null)
                {
                    PluginAPI.WriteLine("Local OCR engine is null!");
                    throw new Exception("OCR engine is null!");
                }
            }

            var ocrtext = string.Empty;

            using (var img = PixConverter.ToPix(imgsource))
            {
                lock (RSTools._lockObj)                 // You can only process one image at a time
                {
                    using (var page = eng.Process(img)) // Program.engine
                    {
                        ocrtext = page.GetText();
                    }
                }
            }

            //imgsource.Dispose();
            return(ocrtext);
        }
Exemplo n.º 2
0
        public Config(String fileName)
        {
            List <String> fileContents = new List <String>(File.ReadAllLines(fileName));

            pluginSettings = new Dictionary <string, string>();
            bool foundWindowName = false;

            foreach (String line in fileContents)
            {
                String[] splitLine = line.Split(' ');
                String   reLine    = "";
                for (int i = 1; i < splitLine.Length; i++)
                {
                    reLine += splitLine[i] + " ";
                }
                if (reLine.Length > 0)
                {
                    reLine.Substring(reLine.Length - 1);
                }
                if (splitLine[0].Equals("UseNetworkOCR"))
                {
                    try
                    {
                        networkOCRIP   = splitLine[1];
                        networkOCRPort = Convert.ToInt32(splitLine[2]);
                        PluginAPI.WriteLine("Network OCR Enabled: " + networkOCRIP + ":" + networkOCRPort);
                        useNetworkOCR = true;
                    }
                    catch (Exception)
                    {
                        PluginAPI.WriteLine("Error reading Network OCR settings. Using integrated OCR.");
                        useNetworkOCR = false;
                    }
                }
                else if (splitLine[0].Equals("OverlayLogMaxTime"))
                {
                    try
                    {
                        overlayLogTime = Convert.ToInt32(splitLine[1]);
                        PluginAPI.WriteLine("Overlay Max Log Line Time (ms): " + overlayLogTime);
                    }
                    catch (Exception)
                    {
                        PluginAPI.WriteLine("Error reading overlay max log time. Using default size.");
                        overlayLogTime = defaultLogTime;
                    }
                }
                else if (splitLine[0].Equals("OverlayLogMaxSize"))
                {
                    try
                    {
                        overlayLogSize = Convert.ToInt32(splitLine[1]);
                        PluginAPI.WriteLine("Overlay Max Log Height: " + overlayLogSize);
                    }
                    catch (Exception)
                    {
                        PluginAPI.WriteLine("Error reading overlay max log size. Using default size.");
                        overlayLogSize = defaultLogSize;
                    }
                }
                else if (splitLine[0].Equals("OverlayIgnoreTopWindow"))
                {
                    overlayIgnoreTopWindow = true;
                    PluginAPI.WriteLine("Ignoring top window for overlay!");
                }
                else if (splitLine[0].Equals("GameWindow"))
                {
                    gameWindowName = string.Join("", splitLine.Skip(1).ToArray());
                    PluginAPI.WriteLine("Game Window Name: " + gameWindowName);

                    try
                    {
                        Win32.RECT rect;
                        IntPtr     handle = Win32.FindWindow(null, gameWindowName);
                        Win32.GetWindowRect(handle, out rect);
                        gameResolution[0] = rect.right - rect.left;
                        gameResolution[1] = rect.bottom - rect.top;
                        if (gameResolution[0] == 0 || gameResolution[1] == 0)
                        {
                            throw new Exception();
                        }
                        PluginAPI.WriteLine("Window Resolution: " + gameResolution[0] + "x" + gameResolution[1]);

                        xOffset = rect.left;
                        yOffset = rect.top;
                        PluginAPI.WriteLine("Window Offsets: X:" + xOffset + " Y:" + yOffset);

                        foundWindowName = true;
                    } catch (Exception)
                    {
                        PluginAPI.WriteLine("Couldn't get data from window!");
                    }
                }
                else if (splitLine[0].Equals("Resolution") && !foundWindowName)
                {
                    PluginAPI.WriteLine("Window Resolution: " + splitLine[1] + "x" + splitLine[2]);
                    gameResolution[0] = Convert.ToInt32(splitLine[1]);
                    gameResolution[1] = Convert.ToInt32(splitLine[2]);
                }
                else if (splitLine[0].Equals("WindowOffsets") && !foundWindowName)
                {
                    PluginAPI.WriteLine("Window Offsets: X:" + splitLine[1] + " Y:" + splitLine[2]);
                    xOffset = Convert.ToInt32(splitLine[1]);
                    yOffset = Convert.ToInt32(splitLine[2]);
                }
                else if (line.StartsWith("//") || line.StartsWith("#"))
                {
                    // Comment line, do nothing.
                }
                else
                {
                    pluginSettings.Add(splitLine[0], reLine);
                    PluginAPI.WriteLine("Found config: " + splitLine[0] + " - " + reLine);
                }
            }
        }
Exemplo n.º 3
0
        //[STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("RS Tools by gnmmarechal");

            Config cfg = new Config("config.cfg");

            if (cfg.useNetworkOCR)
            {
                Display.useNetworkOCR  = true;
                Display.networkOCRIP   = cfg.networkOCRIP;
                Display.networkOCRPort = cfg.networkOCRPort;
            }
            else
            {
                Display.eng = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default);
                Display.eng.SetVariable("debug_file", "nul");
                Console.WriteLine("\nTesseract Version: " + Display.eng.Version);
            }
            //Display.eng.SetVariable("classify_enable_learning", "false");

            if (args.Length == 1)
            {
                cfg = new Config(args[0]);
            }
            cfg.SetBootTime(_bootTime);
            _overlayIgnoreTopWindow = cfg.overlayIgnoreTopWindow;


            /*Thread screenshotThread = new Thread(() =>
             * {
             *
             * });*/

            //overlayThread.SetApartmentState(ApartmentState.STA);


            Thread overlayThread = new Thread(() =>
            {
                //Application.SetCompatibleTextRenderingDefault(false);
                overlayForm = new PluginAPIOverlay();
                overlayForm.StartPosition   = FormStartPosition.Manual;
                overlayForm.Top             = cfg.yOffset;
                overlayForm.Left            = cfg.xOffset;
                overlayForm.Size            = new Size(cfg.gameResolution[0], cfg.gameResolution[1]);
                overlayForm.gameWindowTitle = cfg.gameWindowName;
                overlayForm.maxLogHeight    = cfg.overlayLogSize;
                overlayForm.logTime         = cfg.overlayLogTime;
                overlayForm.ShowDialog();
            });

            overlayThread.SetApartmentState(ApartmentState.STA);
            overlayThread.Start();


            //Display.CropBitmap(Display.GetWholeDisplayBitmap(), 3703, 966, 3735-3703, 988-966).Save("lastInvSlot.bmp");
            Console.WriteLine("\n===Hit ENTER to load all plugins===");
            Console.ReadKey();


            Console.WriteLine("Starting screen capture and loading all plugins...");


            int loopCount = 0;

            Display d1 = new Display();

            // Load Plugins
            PluginLoader loader;

            try
            {
                loader = new PluginLoader();
                loader.loadPlugins();
                PluginAPI.WriteLine("Loaded plugins:");
                foreach (IRSToolsPluginBase plugin in PluginLoader.Plugins)
                {
                    string pluginInfo = plugin.PluginName + " | " + plugin.PluginPackage + " | v" + plugin.PluginVersion;
                    PluginAPI.WriteLine(pluginInfo);
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(string.Format("Plugins couldn't be loaded: {0}", e.Message));
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                Environment.Exit(0);
            }

            PluginAPI.WriteLine("Plugin setup started.");
            foreach (IRSToolsPluginBase plugin in PluginLoader.Plugins)
            {
                plugin.Setup(cfg);
            }

            List <String> disabledPluginList = new List <String>();

            PluginAPI.WriteLine("Starting plugin execution loop.");
            while (isRunning)
            {
                // New System

                //Console.WriteLine("Game Area BMP {0}: " + Convert.ToString(gameAreaScreenshot.Width) + "x" + Convert.ToString(gameAreaScreenshot.Height), Convert.ToString(loopCount));


                // Run Plugins
                List <Thread> threadList = new List <Thread>();
                bool          runWorker  = true;
                runOverlay = true;

                foreach (IRSToolsPluginBase plugin in PluginLoader.Plugins)
                {
                    if (!disabledPluginList.Contains(plugin.PluginPackage))
                    {
                        Thread t = null;


                        // Here add a check to see if a plugin is both ("hybrid") and check flags to see what to run it as

                        if (plugin is IRSToolsPlugin)
                        {
                            t = new Thread(() =>
                            {
                                while (runWorker)
                                {
                                    Bitmap completeScreenshot = Display.GetWholeDisplayBitmap();
                                    Bitmap gameAreaScreenshot = Display.CropBitmap(completeScreenshot, cfg.xOffset, cfg.yOffset, cfg.gameResolution[0], cfg.gameResolution[1]);
                                    completeScreenshot.Dispose();
                                    ((IRSToolsPlugin)plugin).Run(gameAreaScreenshot);
                                    gameAreaScreenshot.Dispose();
                                    GC.Collect();
                                }
                            });
                        }
                        else if (plugin is IMiniRSToolsPlugin) // Plugins that don't require a game bitmap
                        {
                            t = new Thread(() =>
                            {
                                while (runWorker)
                                {
                                    ((IMiniRSToolsPlugin)plugin).Run();
                                    GC.Collect();
                                }
                            });
                        }

                        t.Start();
                        threadList.Add(t);
                    }
                }

                // Read user commands

                string input;
                do
                {
                    input = Console.ReadLine();
                    string arg     = string.Join("", input.Split(' ').Skip(1).ToArray());
                    String command = input.Split(' ')[0];
                    command = Regex.Replace(command, @"[^0-9a-zA-Z]+", "");
                    if (command.Equals("disable"))
                    {
                        if (!disabledPluginList.Contains(arg))
                        {
                            PluginAPI.WriteLine("Adding plugin to the blacklist...");
                            disabledPluginList.Add(arg);
                        }
                    }
                    else if (command.Equals("enable"))
                    {
                        if (disabledPluginList.Contains(arg))
                        {
                            PluginAPI.WriteLine("Adding plugin to the blacklist...");
                            disabledPluginList.Remove(arg);
                        }
                    }
                    else if (command.Equals("report"))
                    {
                        PluginAPI.WriteLine("Blacklisted packages:\n");
                        foreach (String plugPackage in disabledPluginList)
                        {
                            PluginAPI.WriteLine(plugPackage);
                        }
                    }
                    else if (command.Equals("quit") || command.Equals("exit"))
                    {
                        PluginAPI.WriteLine("Shutting down RSTools...");
                        isRunning = false;
                    }
                } while (!String.IsNullOrWhiteSpace(input));
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(false);
                }

                PluginAPI.WriteLine("Restarting plugin threads...\n");
                runWorker  = false;
                runOverlay = false;

                foreach (Thread t in threadList) // This might be unnecessary right now.
                {
                    t.Join();
                }

                loopCount++;
            }


            Console.ReadLine();
            //engine.Dispose();
            Display.eng.Dispose();
        }