Esempio n. 1
0
        private void btnModsPathAutodetect_Click(object sender, EventArgs e)
        {
            bool foundExe  = false;
            bool foundMods = false;

            #region EXE detection

            #region build possible paths - for check use: possibleExecuteablePaths

            List <string> autobuildExecuteablePaths = new List <string>();

            #region set var values

            List <string> varsPf = new List <string>();
            if (Environment.GetEnvironmentVariable("ProgramFiles") != null)
            {
                varsPf.Add(Environment.GetEnvironmentVariable("ProgramFiles"));
            }
            if (Environment.GetEnvironmentVariable("ProgramFiles(x86)") != null)
            {
                varsPf.Add(Environment.GetEnvironmentVariable("ProgramFiles(x86)"));
            }

            List <string> varsArch = new List <string>();
            varsArch.Add("x64");
            varsArch.Add("x86");

            #endregion

            List <string> possibleExecuteablePaths = new List <string>();
            //maybe there is a better/fast way but for now it's working
            AutodetectExecuteablePathTemplates.ForEach(aP =>
            {
                varsPf.ForEach(pfRep =>
                {
                    varsArch.ForEach(archRep =>
                    {
                        string path = aP;
                        path        = path.Replace("%PF%", pfRep);
                        path        = path.Replace("%ARCH%", archRep);
                        path        = path.Replace("%APPDATA%", Environment.GetEnvironmentVariable("APPDATA"));
                        if (!possibleExecuteablePaths.Contains(path))
                        {
                            possibleExecuteablePaths.Add(path);
                        }
                    });
                });
            });

            #endregion

            #region check for existance

            foreach (string possibleExePath in possibleExecuteablePaths)
            {
                try
                {
                    if (Directory.Exists(Path.GetDirectoryName(possibleExePath)))
                    {
                        if (File.Exists(possibleExePath))
                        {
                            //got a match. that's it
                            txtSettingsPathExecuteable.Text = possibleExePath;
                            foundExe = true;
                            break;
                        }
                    }
                }
                catch
                {
                }
            }

            #endregion

            #endregion

            #region MODS detection

            #region build possible paths - for check use: possibleModsPaths

            List <string> possibleModsPaths = new List <string>();
            AutodetectModsPathTemplates.ForEach(aP =>
            {
                varsPf.ForEach(pfRep =>
                {
                    varsArch.ForEach(archRep =>
                    {
                        string path = aP;
                        path        = path.Replace("%PF%", pfRep);
                        path        = path.Replace("%ARCH%", archRep);
                        path        = path.Replace("%APPDATA%", Environment.GetEnvironmentVariable("APPDATA"));
                        if (!possibleModsPaths.Contains(path))
                        {
                            possibleModsPaths.Add(path);
                        }
                    });
                });
            });

            #endregion

            #region check for existance

            foreach (string possibleModPath in possibleModsPaths)
            {
                try
                {
                    if (Directory.Exists(possibleModPath))
                    {
                        if (File.Exists(Path.Combine(possibleModPath, "mod-list.json")))
                        {
                            txtSettingsModPath.Text = possibleModPath;
                            foundMods = true;
                            break;
                        }
                    }
                }
                catch
                {
                }
            }

            #endregion

            #endregion

            //check temp & cache
            if (string.IsNullOrWhiteSpace(txtPathTemp.Text))
            {
                txtPathTemp.Text = Path.Combine(@".\temp");
            }
            if (string.IsNullOrWhiteSpace(txtPathCache.Text))
            {
                txtPathCache.Text = Path.Combine(@".\cache");
            }

            //depending on founds throw single messages
            if (!foundMods)
            {
                MessageBox.Show(
                    "Mods path couldn't be found automatically. Make sure you've started Factorio at least once.\r\n If you did already and still getting this message try setting manually.");
            }
            if (!foundExe)
            {
                MessageBox.Show(
                    "Executeable path couldn't be found automatically. Most likely did you install Factorio in a custom location. Please set manually.");
            }

            if (foundExe)
            {
                FactorioVersion fv = ReadFactorioVersion(txtSettingsPathExecuteable.Text);
                lblSettingsExeVersion.Text = fv.GetVersionString();
            }
        }