2023-02-17 06:02:21 +00:00
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import yaml
|
2023-03-05 02:58:51 +00:00
|
|
|
from os import path
|
2023-03-15 20:03:22 +00:00
|
|
|
import logging
|
|
|
|
import coloredlogs
|
2023-02-17 06:02:21 +00:00
|
|
|
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
from typing import List, Optional
|
|
|
|
|
2023-03-12 06:00:51 +00:00
|
|
|
from core import CoreConfig, Utils
|
2023-02-17 06:02:21 +00:00
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
|
|
|
|
class BaseReader:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config: CoreConfig,
|
|
|
|
version: int,
|
|
|
|
bin_dir: Optional[str],
|
|
|
|
opt_dir: Optional[str],
|
|
|
|
extra: Optional[str],
|
|
|
|
) -> None:
|
2023-02-17 06:02:21 +00:00
|
|
|
self.logger = logging.getLogger("reader")
|
|
|
|
self.config = config
|
|
|
|
self.bin_dir = bin_dir
|
|
|
|
self.opt_dir = opt_dir
|
|
|
|
self.version = version
|
|
|
|
self.extra = extra
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
def get_data_directories(self, directory: str) -> List[str]:
|
|
|
|
ret: List[str] = []
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(directory):
|
2023-03-09 16:38:58 +00:00
|
|
|
for dir in dirs:
|
|
|
|
if re.fullmatch("[A-Z0-9]{4,4}", dir) is not None:
|
|
|
|
ret.append(f"{root}/{dir}")
|
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
return ret
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
if __name__ == "__main__":
|
2023-03-09 16:38:58 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Import Game Information")
|
2023-02-17 06:02:21 +00:00
|
|
|
parser.add_argument(
|
2023-08-30 15:19:13 +00:00
|
|
|
"--game",
|
2023-03-09 16:38:58 +00:00
|
|
|
action="store",
|
2023-02-17 06:02:21 +00:00
|
|
|
type=str,
|
|
|
|
required=True,
|
2023-08-30 15:19:13 +00:00
|
|
|
help="The game we are importing.",
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-03-09 16:38:58 +00:00
|
|
|
"--version",
|
|
|
|
dest="version",
|
|
|
|
action="store",
|
2023-02-17 06:02:21 +00:00
|
|
|
type=int,
|
|
|
|
required=True,
|
2023-03-09 16:38:58 +00:00
|
|
|
help="The game version we are importing.",
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-03-09 16:38:58 +00:00
|
|
|
"--binfolder",
|
|
|
|
dest="bin",
|
|
|
|
action="store",
|
2023-02-17 06:02:21 +00:00
|
|
|
type=str,
|
2023-03-09 16:38:58 +00:00
|
|
|
help="Folder containing A000 base data",
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-03-09 16:38:58 +00:00
|
|
|
"--optfolder",
|
|
|
|
dest="opt",
|
|
|
|
action="store",
|
2023-02-17 06:02:21 +00:00
|
|
|
type=str,
|
2023-03-09 16:38:58 +00:00
|
|
|
help="Folder containing Option data folders",
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--config",
|
|
|
|
type=str,
|
|
|
|
default="config",
|
|
|
|
help="Folder containing the core configuration for importing to DB. Defaults to 'config'.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--extra",
|
|
|
|
type=str,
|
|
|
|
help="Any extra data that a reader might require.",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Parse args, validate invariants.
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
config = CoreConfig()
|
2023-03-05 02:58:51 +00:00
|
|
|
if path.exists(f"{args.config}/core.yaml"):
|
|
|
|
config.update(yaml.safe_load(open(f"{args.config}/core.yaml")))
|
2023-02-17 06:02:21 +00:00
|
|
|
|
|
|
|
log_fmt_str = "[%(asctime)s] Reader | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
|
|
|
logger = logging.getLogger("reader")
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(config.server.log_dir, "reader"), when="d", backupCount=10
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
logger.addHandler(fileHandler)
|
|
|
|
logger.addHandler(consoleHandler)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-24 19:10:41 +00:00
|
|
|
log_lv = logging.DEBUG if config.server.is_develop else logging.INFO
|
|
|
|
logger.setLevel(log_lv)
|
|
|
|
coloredlogs.install(level=log_lv, logger=logger, fmt=log_fmt_str)
|
2023-02-17 06:02:21 +00:00
|
|
|
|
2023-08-30 15:19:13 +00:00
|
|
|
if args.game is None or args.version is None:
|
2023-02-17 06:02:21 +00:00
|
|
|
logger.error("Game or version not specified")
|
|
|
|
parser.print_help()
|
2023-03-09 16:38:58 +00:00
|
|
|
exit(1)
|
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
if args.bin is None and args.opt is None:
|
|
|
|
logger.error("Must specify either bin or opt directory")
|
|
|
|
parser.print_help()
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if args.bin is not None and (args.bin.endswith("\\") or args.bin.endswith("/")):
|
|
|
|
bin_arg = args.bin[:-1]
|
|
|
|
else:
|
|
|
|
bin_arg = args.bin
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
if args.opt is not None and (args.opt.endswith("\\") or args.opt.endswith("/")):
|
|
|
|
opt_arg = args.opt[:-1]
|
|
|
|
else:
|
|
|
|
opt_arg = args.opt
|
|
|
|
|
|
|
|
logger.info("Starting importer...")
|
|
|
|
|
|
|
|
titles = Utils.get_all_titles()
|
|
|
|
|
|
|
|
for dir, mod in titles.items():
|
2023-08-30 15:19:13 +00:00
|
|
|
if args.game in mod.game_codes:
|
2023-04-24 01:04:52 +00:00
|
|
|
handler = mod.reader(config, args.version, bin_arg, opt_arg, args.extra)
|
2023-02-17 06:02:21 +00:00
|
|
|
handler.read()
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
logger.info("Done")
|