Created
November 6, 2020 22:43
-
-
Save abaines/e0c193e9605070a9cda6a3c09a0a5f9c to your computer and use it in GitHub Desktop.
Detect Introversion\Prison Architect autosave.prison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Alan Baines | |
import os.path | |
import os | |
import threading | |
import hashlib | |
import shutil | |
import winsound | |
import time | |
import glob | |
import re | |
# TODO: put this on its own git repository | |
files2watch = dict.fromkeys([ | |
r".\saves\autosave.prison", | |
]) | |
prefix = "autosave." | |
extension = ".prison" | |
def lastModified(path): | |
return os.path.getmtime(path) | |
def hashSha1(readFile): | |
return hashlib.sha1(readFile).hexdigest() | |
def findSave(name): | |
#print(name) | |
dirname = os.path.dirname(name) | |
maxFound = -1 | |
search = os.path.join( dirname , prefix+'*'+extension) | |
for file in glob.glob(search): | |
basename = os.path.basename(file) | |
digits = re.findall(r'\d+',basename) | |
if (len(digits)==1): | |
num = int(digits[0]) | |
if num>maxFound: | |
maxFound = num | |
maxFound = 1 + maxFound | |
print("maxFound :" , maxFound) | |
maxFound = str(maxFound).zfill(6) | |
newFile = prefix + maxFound + extension | |
print(maxFound,newFile) | |
new = os.path.join( dirname , newFile ) | |
return new | |
def scan(): | |
for key, value in files2watch.items(): | |
#print(key) | |
lastMod = lastModified(key) | |
currentFile = open(key,'rb').read() | |
#print(type(currentFile)) | |
sha1 = hashSha1(currentFile) | |
#print( sha1,lastMod, len(currentFile) ) | |
if (value is None): | |
files2watch[key] = sha1 | |
elif (value != sha1): | |
winsound.Beep(120,60) | |
print( "change detected :", key ) | |
newsave = findSave(key) | |
print( "new save name :" + newsave ) | |
#shutil.copy2(key,newsave) | |
fout = open(newsave, 'wb') | |
fout.write(currentFile) | |
fout.close() | |
files2watch[key] = sha1 | |
winsound.Beep(320,60) | |
def scanThread(): | |
threading.Timer(3.0,scanThread).start() | |
scan() | |
scanThread() | |
print("Scanning started") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment