Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor calculating file hashes #567

Merged
merged 6 commits into from
Dec 6, 2024

Conversation

SadPencil
Copy link
Member

@SadPencil SadPencil commented Oct 4, 2024

This PR reworks the file hash calculator to:

  • Fix a bug introduced in .NET 6 Cross platform #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 801bee2

Copy link

github-actions bot commented Oct 4, 2024

Nightly build for this pull request:

@SadPencil SadPencil force-pushed the refactor-file-hash-calculator branch from 7c2709d to 0fd8721 Compare October 4, 2024 10:10
@SadPencil SadPencil added this to the 2.11.2.0 milestone Oct 4, 2024
@Metadorius
Copy link
Member

  • 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?

@SadPencil
Copy link
Member Author

  • 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.

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

        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;
        }

private const string CONFIGNAME = "FHCConfig.ini";
private bool calculateGameExeHash = true;

string[] fileNamesToCheck = new string[]
private static readonly IReadOnlyList<string> knownTextFileExtensions = [".txt", ".ini", ".json", ".xml"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is a robust impl, what do we risking if a used text file is not included here?

Copy link
Member Author

@SadPencil SadPencil Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This design is mostly for preventing an honest-but-curious user viewing a text file bundled with a mod, and their text editor (probably with an auto saving, or one could accidentally hit Ctrl+S even if they think they haven't change any single character) might save that file with the line breaks normalized (they might be non-consistent due to, e.g., collaboration in Git).

If a text file does not have the known extensions here, the behavior should be similar with the one as if this PR is not merged, i.e., be treated as a binary file.

@SadPencil SadPencil merged commit 10882a5 into CnCNet:develop Dec 6, 2024
3 checks passed
@SadPencil SadPencil deleted the refactor-file-hash-calculator branch December 8, 2024 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants