Exemple #1
0
        private void StartGameScanner()
        {
            _presence.Details = "In-Launcher: " + Application.ProductVersion;
            _presence.State   = "Validating Game Files!";
            _presence.Assets  = new Assets
            {
                LargeImageText = "SBRW",
                LargeImageKey  = "nfsw"
            };
            if (MainScreen.discordRpcClient != null)
            {
                MainScreen.discordRpcClient.SetPresence(_presence);
            }

            Log.Info("VERIFY HASH: Checking and Deleting '.orig' Files");

            DirectoryInfo InstallationDirectory = new DirectoryInfo(FileSettingsSave.GameInstallation);

            foreach (var foundFolders in InstallationDirectory.GetDirectories())
            {
                foreach (var file in InstallationDirectory.EnumerateFiles("*.orig"))
                {
                    LogVerify.Deleted("File: " + file);
                    file.Delete();
                }

                foreach (var file in foundFolders.EnumerateFiles("*.orig"))
                {
                    LogVerify.Deleted("File: " + file);
                    file.Delete();
                }
            }
            Log.Info("VERIFY HASH: Completed check for '.orig' Files");

            try
            {
                String[] getFilesToCheck;

                if (File.Exists("checksums.dat"))
                {
                    /* Read Local checksums.dat */
                    getFilesToCheck = File.ReadAllLines("checksums.dat");
                }
                else
                {
                    /* Fetch and Read Remote checksums.dat */
                    ScanProgressText.Text = "Downloading Checksums File";
                    getFilesToCheck       = new WebClient().DownloadString(FinalCDNURL + "/unpacked/checksums.dat").Split('\n');
                    File.WriteAllLines("checksums.dat", getFilesToCheck);
                }

                scannedHashes = new string[getFilesToCheck.Length][];
                for (var i = 0; i < getFilesToCheck.Length; i++)
                {
                    scannedHashes[i] = getFilesToCheck[i].Split(' ');
                }
                filesToScan       = scannedHashes.Length;
                totalFilesScanned = 0;

                /* START Show Warning Text */
                VerifyHashText.ForeColor = Theming.WinFormWarningTextForeColor;
                VerifyHashText.Location  = new Point(61, 292);
                VerifyHashText.Size      = new Size(287, 70);
                VerifyHashText.Text      = "Warning:\n Stopping the Scan before it is complete\nWill result in needing to start over!";
                /* END Show Warning Text */

                foreach (string[] file in scannedHashes)
                {
                    String FileHash       = file[0].Trim();
                    String FileName       = file[1].Trim();
                    String RealPathToFile = FileSettingsSave.GameInstallation + FileName;

                    if (!File.Exists(RealPathToFile))
                    {
                        InvalidFileList.Add(FileName);
                        LogVerify.Missing("File: " + FileName);
                    }
                    else
                    {
                        if (FileHash != SHA.HashFile(RealPathToFile).Trim())
                        {
                            InvalidFileList.Add(FileName);
                            LogVerify.Invalid("File: " + FileName);
                        }
                        else
                        {
                            LogVerify.Valid("File: " + FileName);
                        }
                    }
                    totalFilesScanned++;
                    ScanProgressText.Text = "Scanning Files: " + (totalFilesScanned * 100 / getFilesToCheck.Length) + "%";
                    ScanProgressBar.Value = totalFilesScanned * 100 / getFilesToCheck.Length;
                }

                Log.Info("VERIFY HASH: Scan Completed");

                if (InvalidFileList.Any() != true)
                {
                    GameScanner(false);
                    StartScanner.Visible  = false;
                    StopScanner.Visible   = false;
                    ScanProgressText.Text = "Scan Complete. No Files Missing or Invalid!";
                    /* Hide the DownloadProgressBar as un-needed */
                    DownloadProgressBar.Visible  = false;
                    DownloadProgressText.Visible = false;
                    /* Update the player messaging that we're done */
                    VerifyHashText.ForeColor = Theming.WinFormSuccessTextForeColor;
                    VerifyHashText.Location  = new System.Drawing.Point(99, 300);
                    VerifyHashText.Size      = new System.Drawing.Size(215, 28);
                    VerifyHashText.Text      = "Excellent News! There are ZERO\nmissing or invalid Gamefiles!";
                }
                else
                {
                    ScanProgressText.Text = "Found Invalid or Missing Files";
                    File.WriteAllLines("invalidfiles.dat", InvalidFileList);
                    Log.Info("Found Invalid or Missing Files and will Start File Downloader");
                    CorruptedFilesFound();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
        }
        private void StartGameScanner()
        {
            DiscordLauncherPresence.Status("Verify Scan", null);
            Log.Info("VERIFY HASH: Checking and Deleting '.orig' Files and Symbolic Folders");
            ScanProgressText.SafeInvokeAction(() => ScanProgressText.Text = "Removing any '.orig' Files in Game Directory");

            /* START Show Warning Text */
            VerifyHashText.SafeInvokeAction(() =>
            {
                VerifyHashText.ForeColor = Theming.WinFormWarningTextForeColor;
                VerifyHashText.Text      = "Warning:\nIf '.orig' Files Exist\nIt will be Removed Permanently";
            });
            /* END Show Warning Text */

            try
            {
                DirectoryInfo InstallationDirectory = new DirectoryInfo(FileSettingsSave.GameInstallation);

                foreach (DirectoryInfo FoldersWeFound in InstallationDirectory.GetDirectories())
                {
                    if (!ForceStopScan)
                    {
                        foreach (FileInfo FoundFile in InstallationDirectory.EnumerateFiles("*.orig", SearchOption.AllDirectories))
                        {
                            if (!ForceStopScan)
                            {
                                try
                                {
                                    FoundFile.Delete();
                                    LogVerify.Deleted("File: " + FoundFile.Name);
                                }
                                catch (Exception Error)
                                {
                                    DeletionError++;
                                    LogVerify.Error("File: " + FoundFile.Name + " Error: " + Error.Message);
                                    LogVerify.ErrorIC("File: " + FoundFile.Name + " Error: " + Error.HResult);
                                    LogVerify.ErrorFR("File: " + FoundFile.Name + " Error: " + Error.ToString());
                                }
                            }
                            else
                            {
                                break;
                            }
                        }

                        foreach (FileInfo FoundFile in FoldersWeFound.EnumerateFiles("*.orig", SearchOption.AllDirectories))
                        {
                            if (!ForceStopScan)
                            {
                                try
                                {
                                    FoundFile.Delete();
                                    LogVerify.Deleted("File: " + FoundFile.Name);
                                }
                                catch (Exception Error)
                                {
                                    DeletionError++;
                                    LogVerify.Error("File: " + FoundFile.Name + " Error: " + Error.Message);
                                    LogVerify.ErrorIC("File: " + FoundFile.Name + " Error: " + Error.HResult);
                                    LogVerify.ErrorFR("File: " + FoundFile.Name + " Error: " + Error.ToString());
                                }
                            }
                            else
                            {
                                break;
                            }
                        }

                        foreach (DirectoryInfo FoundDirectory in InstallationDirectory.EnumerateDirectories())
                        {
                            if (!ForceStopScan)
                            {
                                if (ModNetHandler.IsSymbolic(FoundDirectory.FullName))
                                {
                                    if (Directory.Exists(FoundDirectory.FullName))
                                    {
                                        try
                                        {
                                            Directory.Delete(FoundDirectory.FullName, true);
                                            LogVerify.Deleted("Folder: " + FoundDirectory.Name);
                                        }
                                        catch (Exception Error)
                                        {
                                            DeletionError++;
                                            LogVerify.Error("Folder: " + FoundDirectory.Name + " Error: " + Error.Message);
                                            LogVerify.ErrorIC("Folder: " + FoundDirectory.Name + " Error: " + Error.HResult);
                                            LogVerify.ErrorFR("Folder: " + FoundDirectory.Name + " Error: " + Error.ToString());
                                        }
                                    }
                                    else if (File.Exists(FoundDirectory.FullName))
                                    {
                                        try
                                        {
                                            File.Delete(FoundDirectory.FullName);
                                            LogVerify.Deleted("File: " + FoundDirectory.Name);
                                        }
                                        catch (Exception Error)
                                        {
                                            DeletionError++;
                                            LogVerify.Error("File: " + FoundDirectory.Name + " Error: " + Error.Message);
                                            LogVerify.ErrorIC("File: " + FoundDirectory.Name + " Error: " + Error.HResult);
                                            LogVerify.ErrorFR("File: " + FoundDirectory.Name + " Error: " + Error.ToString());
                                        }
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }

                        foreach (DirectoryInfo FoundDirectory in FoldersWeFound.EnumerateDirectories())
                        {
                            if (!ForceStopScan)
                            {
                                if (ModNetHandler.IsSymbolic(FoundDirectory.FullName))
                                {
                                    if (Directory.Exists(FoundDirectory.FullName))
                                    {
                                        try
                                        {
                                            Directory.Delete(FoundDirectory.FullName, true);
                                            LogVerify.Deleted("Folder: " + FoundDirectory.Name);
                                        }
                                        catch (Exception Error)
                                        {
                                            DeletionError++;
                                            LogVerify.Error("Folder: " + FoundDirectory.Name + " Error: " + Error.Message);
                                            LogVerify.ErrorIC("Folder: " + FoundDirectory.Name + " Error: " + Error.HResult);
                                            LogVerify.ErrorFR("Folder: " + FoundDirectory.Name + " Error: " + Error.ToString());
                                        }
                                    }
                                    else if (File.Exists(FoundDirectory.FullName))
                                    {
                                        try
                                        {
                                            File.Delete(FoundDirectory.FullName);
                                            LogVerify.Deleted("File: " + FoundDirectory.Name);
                                        }
                                        catch (Exception Error)
                                        {
                                            DeletionError++;
                                            LogVerify.Error("File: " + FoundDirectory.Name + " Error: " + Error.Message);
                                            LogVerify.ErrorIC("File: " + FoundDirectory.Name + " Error: " + Error.HResult);
                                            LogVerify.ErrorFR("File: " + FoundDirectory.Name + " Error: " + Error.ToString());
                                        }
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (Directory.Exists(Path.Combine(FileSettingsSave.GameInstallation, "scripts")) && !ForceStopScan)
                {
                    DirectoryInfo ScriptsFolder = new DirectoryInfo(Path.Combine(FileSettingsSave.GameInstallation, "scripts"));

                    if (ScriptsFolder.EnumerateFiles().Count() > 1)
                    {
                        if (MessageBox.Show("Verify Hash has found files in the Scripts folder.\n" +
                                            "If you have installed custom Scripts or have not installed any Scripts" +
                                            "\n\nClick Yes, to Allow Deletion of Files" +
                                            "\nClick No, to Skip Deletion of Files", "VerifyHash", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            foreach (FileInfo FoundFile in ScriptsFolder.EnumerateFiles())
                            {
                                if (FoundFile.Name != "LangPicker.ini")
                                {
                                    try
                                    {
                                        File.Delete(FoundFile.FullName);
                                        LogVerify.Deleted("File: " + FoundFile.Name);
                                    }
                                    catch (Exception Error)
                                    {
                                        DeletionError++;
                                        LogVerify.Error("File: " + FoundFile.Name + " Error: " + Error.Message);
                                        LogVerify.ErrorIC("File: " + FoundFile.Name + " Error: " + Error.HResult);
                                        LogVerify.ErrorFR("File: " + FoundFile.Name + " Error: " + Error.ToString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception Error)
            {
                LogToFileAddons.OpenLog("VERIFY HASH", null, Error, null, true);
            }

            if (DeletionError != 0)
            {
                Log.Info("VERIFY HASH: Completed check for '.orig' Files and Symbolic Folders, BUT Encounterd a File or Folder Deletion Error. " +
                         "Check Verify.log for More Details");

                if (MessageBox.Show("Verify Hash has encountered File or Folder Deletion Errors.\n" +
                                    "Would you like to Open Verify.Log and Stop the Scanner?", "VerifyHash", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string LogFile = Strings.Encode(Locations.LogVerify);
                    if (File.Exists(LogFile))
                    {
                        Process.Start(LogFile);
                    }

                    StopScanner_Click(null, null);
                }
            }
            else
            {
                Log.Info("VERIFY HASH: Completed check for '.orig' Files and Symbolic Folders");
            }

            if (!ForceStopScan)
            {
                try
                {
                    FunctionStatus.IsVerifyHashDisabled = true;

                    String[] getFilesToCheck = { };

                    if (File.Exists("checksums.dat") && EnableInsiderDeveloper.Allowed())
                    {
                        /* Read Local checksums.dat */
                        getFilesToCheck = File.ReadAllLines("checksums.dat");
                    }
                    else
                    {
                        /* Fetch and Read Remote checksums.dat */
                        ScanProgressText.SafeInvokeAction(() => ScanProgressText.Text = "Downloading Checksums File");

                        Uri URLCall = new Uri(FinalCDNURL + "/unpacked/checksums.dat");
                        ServicePointManager.FindServicePoint(URLCall).ConnectionLeaseTimeout = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;
                        var Client = new WebClient
                        {
                            Encoding = Encoding.UTF8
                        };
                        if (!WebCalls.Alternative())
                        {
                            Client = new WebClientWithTimeout {
                                Encoding = Encoding.UTF8
                            };
                        }
                        else
                        {
                            Client.Headers.Add("user-agent", "SBRW Launcher " +
                                               Application.ProductVersion + " (+https://github.com/SoapBoxRaceWorld/GameLauncher_NFSW)");
                        }

                        bool ErrorFree = true;

                        try
                        {
                            getFilesToCheck = Client.DownloadString(URLCall).Split('\n');
                        }
                        catch (Exception Error)
                        {
                            LogToFileAddons.OpenLog("VERIFY HASH CHECKSUMS", "Downloading of the Checksums File has Encountered an Error", Error, "Error", false);
                            ErrorFree = false;
                        }
                        finally
                        {
                            if (Client != null)
                            {
                                Client.Dispose();
                            }
                        }

                        if (ErrorFree)
                        {
                            File.WriteAllLines("checksums.dat", getFilesToCheck);
                        }
                        else
                        {
                            StopScanner_Click(null, null);
                        }
                    }

                    scannedHashes = new string[getFilesToCheck.Length][];
                    for (var i = 0; i < getFilesToCheck.Length; i++)
                    {
                        if (!ForceStopScan)
                        {
                            scannedHashes[i] = getFilesToCheck[i].Split(' ');
                        }
                        else
                        {
                            break;
                        }
                    }
                    filesToScan       = scannedHashes.Length;
                    totalFilesScanned = 0;

                    /* START Show Warning Text */
                    VerifyHashText.SafeInvokeAction(() =>
                    {
                        VerifyHashText.ForeColor = Theming.WinFormWarningTextForeColor;
                        VerifyHashText.Text      = "Warning:\n Stopping the Scan before it is complete\nWill result in needing to start over!";
                    });
                    /* END Show Warning Text */

                    foreach (string[] file in scannedHashes)
                    {
                        if (!ForceStopScan)
                        {
                            String FileHash       = file[0].Trim();
                            String FileName       = file[1].Trim();
                            String RealPathToFile = FileSettingsSave.GameInstallation + FileName;

                            if (!File.Exists(RealPathToFile))
                            {
                                InvalidFileList.Add(FileName);
                                LogVerify.Missing("File: " + FileName);
                            }
                            else
                            {
                                if (FileHash != SHA.Files(RealPathToFile).Trim())
                                {
                                    InvalidFileList.Add(FileName);
                                    LogVerify.Invalid("File: " + FileName);
                                }
                                else
                                {
                                    LogVerify.Valid("File: " + FileName);
                                }
                            }
                            totalFilesScanned++;
                            ScanProgressText.SafeInvokeAction(() => ScanProgressText.Text = "Scanning Files: " + (totalFilesScanned * 100 / getFilesToCheck.Length) + "%");
                            ScanProgressBar.SafeInvokeAction(() => ScanProgressBar.Value  = totalFilesScanned * 100 / getFilesToCheck.Length);
                        }
                        else
                        {
                            break;
                        }
                    }

                    Log.Info("VERIFY HASH: Scan Completed");
                    if (!InvalidFileList.Any() || ForceStopScan)
                    {
                        StartScanner.SafeInvokeAction(() => StartScanner.Visible      = false);
                        StopScanner.SafeInvokeAction(() => StopScanner.Visible        = false);
                        ScanProgressText.SafeInvokeAction(() => ScanProgressText.Text = ForceStopScan ? "User Stopped Scan." : "Scan Complete. No Files Missing or Invalid!");

                        /* Hide the DownloadProgressBar as un-needed */
                        DownloadProgressBar.SafeInvokeAction(() => DownloadProgressBar.Visible   = false);
                        DownloadProgressText.SafeInvokeAction(() => DownloadProgressText.Visible = false);
                        /* Update the player messaging that we're done */
                        VerifyHashText.SafeInvokeAction(() =>
                        {
                            if (!ForceStopScan)
                            {
                                VerifyHashText.ForeColor = Theming.WinFormSuccessTextForeColor;
                            }
                            VerifyHashText.Text = ForceStopScan ? "Verify Hash Scan Process has been Terminated" : "Excellent News! There are ZERO\nmissing or invalid Gamefiles!";
                        });

                        Integrity();
                        GameScanner(false);
                    }
                    else
                    {
                        ScanProgressText.SafeInvokeAction(() => ScanProgressText.Text = "Found Invalid or Missing Files");

                        File.WriteAllLines("invalidfiles.dat", InvalidFileList);
                        Log.Info("VERIFY HASH: Found Invalid or Missing Files and will Start File Downloader");
                        CorruptedFilesFound();
                    }
                }
                catch (Exception Error)
                {
                    LogToFileAddons.OpenLog("VERIFY HASH", null, Error, null, true);
                }
            }
        }