Files
Whiskerbound/data/save.py

104 lines
2.9 KiB
Python

import json
from data.cat import Cat
import os
import sys
import hashlib
import systems.ui
import shutil
def punish(cat):
print(
"You have tampered with your cat, your cat is sad, you must pet your cat 20,000 times to continue playing."
)
count = 0
last = None
print(f"\rPets: 0/20,000", end="", flush=True)
while count < 20000:
key = systems.ui.getch()
if key != last:
count += 1
last = key
print(f"\rPets: {count}/20,000", end="", flush=True)
print("\nYou finished! Your cat is a bit upset but you may continue playing.")
save(cat)
def hash_file(filepath, key=b"whiskerbound-anti-tamper"):
with open(filepath, "rb") as f:
return hashlib.blake2s(f.read(), key=key).hexdigest()
def get_data_dir():
if sys.platform == "win32":
return os.path.join(os.environ["APPDATA"], "Whiskerbound")
else:
return os.path.join(
os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share")),
"Whiskerbound",
)
def prepare_move():
data_dir = get_data_dir()
hash_path = os.path.join(data_dir, "dont hurt cats.json")
if os.path.exists(hash_path):
shutil.copy(hash_path, "saves/.moved")
print("Ready to move! Copy your saves folder to your new PC.")
else:
print("No save data to prepare.")
class SaveData:
def __init__(self, cat, money=100):
self.cat = cat
self.money = money
def save(cat):
currentjson = {}
data_dir = get_data_dir()
hash_path = os.path.join(data_dir, "dont hurt cats.json")
if not os.path.exists("saves"):
os.mkdir("saves")
with open(f"saves/{cat.name}.kitten", "w") as f:
json.dump(cat.to_dict(), f)
os.makedirs(data_dir, exist_ok=True)
if os.path.exists(hash_path):
with open(hash_path, "r") as f:
currentjson = json.load(f)
currentjson[f"saves/{cat.name}.kitten"] = hash_file(f"saves/{cat.name}.kitten")
with open(hash_path, "w") as f:
json.dump(currentjson, f)
def load(filepath, bypass_tamper_check=False, force_punish=False):
punished = False
data_dir = get_data_dir()
hash_path = os.path.join(data_dir, "dont hurt cats.json")
file_hash = hash_file(filepath)
if not os.path.exists(hash_path) and os.path.exists("saves/.moved"):
os.makedirs(data_dir, exist_ok=True)
shutil.move("saves/.moved", hash_path)
if os.path.exists(hash_path):
with open(hash_path, "r") as f:
hashes = json.load(f)
if not filepath in hashes:
punished = True
else:
if not hashes[filepath] == file_hash:
punished = True
else:
pass # ur good
else:
punished = True
with open(filepath) as f:
raw = json.load(f)
cat = Cat(**raw)
if punished or force_punish:
if not bypass_tamper_check:
punish(cat)
return cat