xna-cncnet-client icon indicating copy to clipboard operation
xna-cncnet-client copied to clipboard

Refactor calculating file hashes

Open SadPencil opened this issue 1 year ago • 1 comments

This PR reworks the file hash calculator to:

  • Fix a bug introduced in https://github.com/CnCNet/xna-cncnet-client/pull/341/ (codes here, diff here), where all files in "Map Code" and "Game Options" folders are not included. This bug should be a threat for anti-cheating detection.

    TLDR: the bug is introduced because:

    string folderName = @"E:\SteamLibrary";
    string fileName1 = Directory.GetFiles(folderName).First().Dump(); // "E:\SteamLibrary\libraryfolder.vdf"
    string fileName2 = new DirectoryInfo(folderName).EnumerateFiles("*").First().Name.Dump(); // "libraryfolder.vdf"
    
  • Fix a potential bug introduced in implementing multi-language support. Files in ClientConfiguration.Instance.TranslationGameFiles.Where(tgf => tgf.Checked) (these files should be checked) are not sorted, leaving the file order as uncertain, especially in cross-platform scenarios. Now, all enumerated files will be sorted with path separators normalized.

  • For known text file extensions like .ini, when computing hashes, the client will try to normalize the difference between Windows (\r\n in the end of every line except for the last) and UNIX (\n in the end of every line).

  • Remove redundancy parameter in CalculateHashes(List<GameMode> gameModes) that has been unused since 801bee2b39a55640055fee01cf9e86622748d102

SadPencil avatar Oct 04 '24 09:10 SadPencil

Nightly build for this pull request:

github-actions[bot] avatar Oct 04 '24 09:10 github-actions[bot]

  • Fix a potential bug introduced in implementing multi-language support. Files in ClientConfiguration.Instance.TranslationGameFiles.Where(tgf => tgf.Checked) (these files should be checked) are not sorted, leaving the file order as uncertain, especially in cross-platform scenarios. Now, all enumerated files will be sorted with path separators normalized.

I am not sure, IIRC they are order-independent?

Metadorius avatar Nov 03 '24 22:11 Metadorius

  • Fix a potential bug introduced in implementing multi-language support. Files in ClientConfiguration.Instance.TranslationGameFiles.Where(tgf => tgf.Checked) (these files should be checked) are not sorted, leaving the file order as uncertain, especially in cross-platform scenarios. Now, all enumerated files will be sorted with path separators normalized.

I am not sure, IIRC they are order-independent?

No. They are order-dependent.

https://github.com/CnCNet/xna-cncnet-client/blob/6dc4a6dd284d7cc5e12715a6832bb225d7f0697a/DXMainClient/Online/FileHashCalculator.cs#L131

        fh.INIHashes = AddToStringIfFileExists(fh.INIHashes, filePath);

        string AddToStringIfFileExists(string str, string path)
        {
            if (File.Exists(path))
                return str + Utilities.CalculateSHA1ForFile(SafePath.CombineFilePath(ProgramConstants.GamePath, path));

            return str;
        }

SadPencil avatar Nov 04 '24 01:11 SadPencil