Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();

            // Check for admin rights.
            WindowsIdentity  identity   = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal  = new WindowsPrincipal(identity);
            bool             isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);

            // names and paths
            string toolboxdir = Environment.GetEnvironmentVariable("LocalAppData") + Path.DirectorySeparatorChar + "GWToolboxpp" + Path.DirectorySeparatorChar;
            string inifile    = toolboxdir + "GWToolbox.ini";
            string dllfile    = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + DLL_NAME;

            // Install resources
            ResInstaller installer = new ResInstaller();

            installer.Install();

            // Download or update if needed. If dll file exists in current directory, use it.
            if (!File.Exists(dllfile))
            {
                dllfile = toolboxdir + DLL_NAME;
            }
            if (File.Exists(dllfile) && (new FileInfo(dllfile).Length) < 1)
            {
                File.Delete(dllfile); // Delete file if exists with 0 length
            }
            if (!File.Exists(dllfile))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                WebClient host = new WebClient();
                host.Headers.Add(HttpRequestHeader.UserAgent, "GWToolboxpp Launcher");
                string remoteversion = GetLatestVersion();
                if (remoteversion.Length == 0)
                {
                    MessageBox.Show("Failed to fetch latest GWToolbox++ version.\n Check your internet connection and try again",
                                    "GWToolbox++ Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
                string dllurl = "https://github.com/" + GITHUB_USER + "/GWToolboxpp/releases/download/" + remoteversion + "_Release/" + DLL_NAME;
                int    tries  = 0;
                // This bit will take a while...
                while (tries < 3 && dllurl.Length > 0 && !File.Exists(dllfile))
                {
                    try
                    {
                        host.DownloadFile(dllurl, dllfile);
                        if (File.Exists(dllfile) && (new System.IO.FileInfo(dllfile).Length) < 1)
                        {
                            File.Delete(dllfile); // Delete file if exists with 0 length
                        }
                    }
                    catch (Exception)
                    {
                        // todo
                    }
                    tries++;
                }
                if (!File.Exists(dllfile))
                {
                    MessageBox.Show("Failed to download GWToolbox++ dll.\n Check your internet connection and try again",
                                    "GWToolbox++ Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
            }
            // check again after download/update/build
            if (!File.Exists(dllfile))
            {
                MessageBox.Show("Cannot find " + DLL_NAME, "GWToolbox++ Launcher Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Look for gw processes.
            List <Process> processesToInject = new List <Process>();

            CharSelector chargui = new CharSelector();

            Process[] gwprocs = chargui.GetValidProcesses();
            switch (gwprocs.Length)
            {
            case 0:     // No gw processes found.
                if (!isElevated && hasElevatedGWProcesses())
                {
                    MessageBox.Show("Guild Wars is running as Admin.\n" +
                                    "Restart Guild Wars without Admin, or run this launcher as Admin to run GWToolbox++",
                                    "GWToolbox++ Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
                MessageBox.Show("No Guild Wars clients found.\n" +
                                "Please log into Guild Wars first.",
                                "GWToolbox++ Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                break;

            case 1:     // Only one process found, injecting.
                processesToInject.Add(gwprocs[0]);
                break;

            default:     // More than one found, make user select client.
                Application.Run(chargui);
                processesToInject.AddRange(chargui.SelectedProcesses);
                break;
            }

            if (processesToInject.Count == 0)
            {
                return;
            }

            for (int i = 0; i < processesToInject.Count; ++i)
            {
                IntPtr     dll_return;
                GWCAMemory mem = new GWCAMemory(processesToInject[i]);
                GWCAMemory.LOADMODULERESULT result = mem.LoadModule(dllfile, out dll_return);
                if (result == GWCAMemory.LOADMODULERESULT.SUCCESSFUL && dll_return != IntPtr.Zero)
                {
                    // continue
                }
                else if (result == GWCAMemory.LOADMODULERESULT.SUCCESSFUL)
                {
                    MessageBox.Show("Error loading DLL: ExitCode " + dll_return,
                                    "GWToolbox++ Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Module Load Error.\n" +
                                    LOADMODULE_RESULT_MESSAGES[(uint)result] + "\n",
                                    "GWToolbox++ Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();

            // Check for admin rights.
            WindowsIdentity  identity   = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal  = new WindowsPrincipal(identity);
            bool             isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);

            if (!isElevated)
            {
                MessageBox.Show("Please run the launcher as Admin.",
                                "GWToolbox++ Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            // names and paths
            string localappdata   = Environment.GetEnvironmentVariable("LocalAppData");
            string settingsfolder = localappdata + "\\GWToolboxpp\\";
            string inifile        = settingsfolder + "GWToolbox.ini";

            // Install resources
            ResInstaller installer = new ResInstaller();

            installer.Install();

#if DEBUG
            // do nothing, we'll use GWToolbox.dll in /Debug
            string dllfile = "GWToolbox.dll"; // same folder where the launcher is built
#else
            // Download or update if needed
            string dllfile = settingsfolder + "GWToolbox.dll";
            if (!File.Exists(dllfile))
            {
                string    toolboxdir    = Environment.GetEnvironmentVariable("LocalAppData") + "\\GWToolboxpp\\";
                WebClient host          = new WebClient();
                string    remoteversion = host.DownloadString(
                    "https://raw.githubusercontent.com/HasKha/GWToolboxpp/master/resources/toolboxversion.txt");
                string dllurl = "https://github.com/HasKha/GWToolboxpp/releases/download/"
                                + remoteversion + "_Release/GWToolbox.dll";
                host.DownloadFile(dllurl, toolboxdir + "GWToolbox.dll");
            }
#endif
            // check again after download/update/build
            if (!File.Exists(dllfile))
            {
                MessageBox.Show("Cannot find GWToolbox.dll", "GWToolbox++ Launcher Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Look for gw processes.
            Process[] gwprocs = Process.GetProcessesByName("Gw");

            switch (gwprocs.Length)
            {
            case 0:     // No gw processes found.
                MessageBox.Show("No Guild Wars clients found.\n" +
                                "Please log into Guild Wars first.",
                                "GWToolbox++ Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                break;

            case 1:     // Only one process found, injecting.
                proctoinject = gwprocs[0];
                break;

            default:     // More than one found, make user select client.

                CharSelector chargui = new CharSelector();

                Application.Run(chargui);

                proctoinject = chargui.SelectedProcess;
                break;
            }

            if (proctoinject == null)
            {
                return;
            }

            IntPtr     dll_return;
            GWCAMemory mem = new GWCAMemory(proctoinject);
            GWCAMemory.LOADMODULERESULT result = mem.LoadModule(dllfile, out dll_return);
            if (result == GWCAMemory.LOADMODULERESULT.SUCCESSFUL && dll_return != IntPtr.Zero)
            {
                return;
            }
            if (result == GWCAMemory.LOADMODULERESULT.SUCCESSFUL)
            {
                MessageBox.Show("Error loading DLL: ExitCode " + dll_return,
                                "GWToolbox++ Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("Module Load Error.\n" +
                                LOADMODULE_RESULT_MESSAGES[(uint)result] + "\n",
                                "GWToolbox++ Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }