Change timestamp check (prevent false positive)

This commit is contained in:
tretrauit 2022-02-18 21:30:24 +07:00
parent d5e292eb2d
commit 659befc8f8
Signed by: tretrauit
GPG Key ID: 862760FF1903319E
2 changed files with 22 additions and 2 deletions

View File

@ -1,5 +1,4 @@
aiohttp==3.8.1
appdirs~=1.4.4
aiopath~=0.6.10
setuptools==60.9.3
xdelta3~=0.0.5

View File

@ -1,3 +1,6 @@
import os
import platform
import xdelta3
import tarfile
import appdirs
@ -164,11 +167,29 @@ class Patcher:
if crash_fix:
self.apply_xlua_patch(fallback=fallback)
@staticmethod
def _creation_date(file_path: Path):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(file_path)
else:
stat = file_path.stat()
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
def _revert_file(self, original_file: str, base_file: Path, ignore_error=False):
original_path = self._gamedir.joinpath(original_file + ".bak").resolve()
target_file = self._gamedir.joinpath(original_file).resolve()
if original_path.exists():
if abs(base_file.stat().st_mtime_ns - original_path.stat().st_mtime_ns) > 3600:
if abs(self._creation_date(base_file) - self._creation_date(original_path)) > 3600:
if not ignore_error:
raise RuntimeError("{} is not for this game version.".format(original_path.name))
original_path.unlink(missing_ok=True)