Utility scripts for a TES3MP server administrator.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

133 lines
4.0 KiB

#!/usr/bin/env python3
import argparse
import datetime
import os
import sys
DESC = "Remove all cells except those whitelisted or used by RealEstate.lua"
PROG = "tes3mp-reset-cells"
VERSION = 3
# TODO: dehardcode this
TES3MP_DIR = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "..")
)
VISUALHARVEST_DATA = os.path.join(
TES3MP_DIR, "lua", "CoreScripts", "data", "custom", "__data_VisualHarvesting.json"
)
# TODO: dehardcode this
REALESTATE_CELLS_FILE = os.path.join(
TES3MP_DIR, "lua", "CoreScripts", "data", "custom", "RealEstate", "cells.txt"
)
# TODO: dehardcode this
WHITELIST = [
"Abandoned Flat, Training Facility",
"Archery Arena",
"Character Stuff Wonderland",
"Enchanting Storage",
"Hidden Lab",
"Thaumaturgical Studies and Practices",
"Underground Conservatory",
"Mournhold",
]
def parse_argv():
"""Set up args and parse them."""
parser = argparse.ArgumentParser(description=DESC, prog=PROG)
parser.add_argument(
"--version", action="version", version=VERSION, help=argparse.SUPPRESS
)
options = parser.add_argument_group("Options")
options.add_argument(
"-d", "--corescripts", required=True, help="Path to your CoreScripts/mp-stuff."
)
options.add_argument(
"--with-visualharvest",
action="store_true",
help="Also delete VisualHarvesting pick data.",
)
options.add_argument(
"-R", "--with-realestate", action="store_true", help="Omit RealEstate files."
)
return parser.parse_args()
def remove_cells(celldir: str, realestate_cells_file: str, whitelist: list):
cells_to_rm = []
for f in os.listdir(cells_dir):
cells_to_rm.append(f.split(".json")[0])
if realestate_cells_file:
realestate_cells = []
try:
with open(realestate_cells_file, "r") as f:
for line in f.readlines():
realestate_cells.append(line.split(":")[0])
for cell in WHITELIST:
realestate_cells.append(cell)
for re_cell in realestate_cells:
if re_cell in cells_to_rm:
print(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ " Ignoring cell: "
+ re_cell
)
cells_to_rm.remove(re_cell)
except FileNotFoundError:
sys.stderr.write(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ " RealEstate files were not found.\n"
)
sys.exit(1)
for c in cells_to_rm:
if c != ".gitkeep":
print(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ " Deleting cell: "
+ c
)
try:
os.remove(os.path.join(cells_dir, c + ".json"))
except FileNotFoundError:
print(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ " Couildn't delete {}, is it a real file?".format(c + ".json")
)
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " CELL RESET DONE!")
def reset_qh_data():
try:
with open(VISUALHARVEST_DATA, "w") as f:
f.write("{}")
print(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ " VisualHarvesting data has been reset!"
)
except FileNotFoundError:
sys.stderr.write(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ " VisualHarvesting pick data was not found.\n"
)
sys.exit(1)
parsed = parse_argv()
cells_dir = os.path.join(parsed.corescripts, "data", "cell")
with_visualharvest = parsed.with_visualharvest
with_realestate = parsed.with_realestate
if with_visualharvest:
reset_qh_data()
if with_realestate:
remove_cells(cells_dir, REALESTATE_CELLS_FILE, WHITELIST)
else:
remove_cells(cells_dir, None, WHITELIST)