From 04856af894d97b5f1d98e8c601e5e6d0241e172a Mon Sep 17 00:00:00 2001 From: Haoyu Xu Date: Sun, 8 Aug 2021 15:01:04 -0400 Subject: [PATCH] feat(builder): use a new content processor --- config.yaml | 76 ++++++++++++++++++++++++++++++++++++++++ lib/builder.py | 23 +++--------- lib/config.py | 5 +-- lib/content_processor.py | 58 ++++++++++++++++++++++++++++++ template/index.html | 18 +++++----- 5 files changed, 151 insertions(+), 29 deletions(-) create mode 100644 lib/content_processor.py diff --git a/config.yaml b/config.yaml index 26d7572..d6601c8 100644 --- a/config.yaml +++ b/config.yaml @@ -10,6 +10,32 @@ operators: target_folder: ./operator/{name}/processed/ title: Ch'en the Holungday use_skel: true + config: + index.html: + operator_logo: logo_rhodes_override + title: Ch'en the Holungday + version: __get_version + operator_settings.js: + fallbackImage_height: 2048 + fallbackImage_width: 2048 + filename: dyn_illust_char_1013_chen2 + fps: 60 + opacity: 100 + viewport_bottom: 1 + viewport_left: 0 + viewport_right: 0 + viewport_top: 1 + project.json: + description: 'Arknights: Ch''en/Chen the Holungday Live 2D\n明日方舟:假日威龙陈 Live 2D\nThe model is extracted from game with Spine support.\n模型来自游戏内提取,支持Spine\nPlease set your FPS target in Wallpaper Engine > Settings > Performance > FPS\n请在 Wallpaper Engine > 设置 > 性能 > FPS 下设置FPS\n\nLive preview on: https://arknights.halyul.dev/chen\nGithub: https://github.com/Halyul/aklive2d' + title: 'Arknights: Ch''en/Chen the Holungday - 明日方舟:假日威龙陈' + ui_logo_opacity: 100 + ui_logo_ratio: 61.8 + ui_operator_logo: 'true' + ui_position_padding_bottom: 1 + ui_position_padding_left: 0 + ui_position_padding_right: 0 + ui_position_padding_top: 1 + workshopid: 2564643862 nian: common_name: dyn_illust_char_2014_nian fallback_name: char_2014_nian_2 @@ -21,6 +47,31 @@ operators: target_folder: ./operator/{name}/processed/ title: Nian use_skel: true + config: + index.html: + operator_logo: logo_sui + title: Nian + version: __get_version + operator_settings.js: + fallbackImage_height: 2048 + fallbackImage_width: 2048 + filename: dyn_illust_char_2014_nian + fps: 60 + viewport_left: 2 + viewport_right: 2 + viewport_top: 3 + viewport_bottom: 5 + project.json: + description: 'Arknights: Nian Live 2D\n明日方舟:年 Live 2D\nThe model is extracted from game with Spine support.\n模型来自游戏内提取,支持Spine\nPlease set your FPS target in Wallpaper Engine > Settings > Performance > FPS\n请在 Wallpaper Engine > 设置 > 性能 > FPS 下设置FPS\n\nLive preview on: https://arknights.halyul.dev/nian\nGithub: https://github.com/Halyul/aklive2d' + title: 'Arknights: Nian - 明日方舟:年' + ui_logo_opacity: 30 + ui_logo_ratio: 61.8 + ui_operator_logo: 'true' + ui_position_padding_left: 02 + ui_position_padding_right: 2 + ui_position_padding_top: 3 + ui_position_padding_bottom: 5 + workshopid: 2564642594 skadi: common_name: dyn_illust_char_1012_skadi2 fallback_name: char_1012_skadi2_2 @@ -32,6 +83,31 @@ operators: target_folder: ./operator/{name}/processed/ title: Skadi the Corrupting Heart use_skel: true + config: + index.html: + operator_logo: logo_egir + title: Skadi the Corrupting Heart + version: __get_version + operator_settings.js: + fallbackImage_height: 2048 + fallbackImage_width: 2048 + filename: dyn_illust_char_1012_skadi2 + fps: 60 + viewport_left: -5 + viewport_right: -10 + viewport_top: 0 + viewport_bottom: -12 + project.json: + description: 'Arknights: Skadi the Corrupting Heart Live 2Di\r\n明日方舟:浊心斯卡蒂 Live 2Di\r\nThe model is extracted from game with Spine support.\r\n模型来自游戏内提取,支持Spine\r\nPlease set your FPS target in Wallpaper Engine > Settings > Performance > FPS\r\n请在 Wallpaper Engine > 设置 > 性能 > FPS 下设置FPS\r\n\r\nLive preview on: https://arknights.halyul.dev/skadi\r\nGithub: https://github.com/Halyul/aklive2d' + title: 'Arknights: Skadi the Corrupting Heart - 明日方舟:浊心斯卡蒂' + ui_logo_opacity: 30 + ui_logo_ratio: 61.8 + ui_operator_logo: 'true' + ui_position_padding_left: -5 + ui_position_padding_right: -10 + ui_position_padding_top: 0 + ui_position_padding_bottom: -12 + workshopid: 2492307783 server: operator_folder: ./operator/ release_folder: ./release/ diff --git a/lib/builder.py b/lib/builder.py index a5ed374..968ed6b 100644 --- a/lib/builder.py +++ b/lib/builder.py @@ -4,14 +4,13 @@ from lib.skeleton_binary import SkeletonBinary from lib.alpha_composite import AlphaComposite from lib.atlas_reader import AtlasReader from lib.base64_util import * -from lib.html_processor import HtmlProcessor +from lib.content_processor import ContentProcessor class Builder: def __init__(self, config, operator_names=list(), rebuild=False) -> None: self.operator_names = operator_names self.config = config - self.html_processor = HtmlProcessor(config) self.rebuild = rebuild def start(self): @@ -191,6 +190,7 @@ class Builder: release_operator_assets_path = pathlib.Path.cwd().joinpath(operator_release_path, self.config["server"]["operator_folder"]) operator_assets_path = pathlib.Path.cwd().joinpath(self.config["operators"][operator_name]["target_folder"].format(name=operator_name), "..") template_path = pathlib.Path.cwd().joinpath(self.config["server"]["template_folder"]) + content_processor = ContentProcessor(self.config, operator_name) if operator_release_path.exists() is True: shutil.rmtree(operator_release_path) @@ -205,28 +205,15 @@ class Builder: else: file_path = pathlib.Path.cwd().joinpath(release_operator_assets_path, filename) - shutil.copyfile( - file, - file_path - ) + content_processor.build(file, file_path) for file in template_path.iterdir(): if file.is_file() is True: - __temp = file.name.split(".") - filename = __temp[0] - fileext = __temp[1] file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name) - if filename == "index" and fileext == "html": - self.html_processor.build(operator_name, file, file_path) - else: - shutil.copyfile( - file, - file_path - ) + content_processor.build(file, file_path) elif file.is_dir() is True: - filename = file.name - file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename) + file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name) shutil.copytree( file, diff --git a/lib/config.py b/lib/config.py index 003d570..fe30b05 100644 --- a/lib/config.py +++ b/lib/config.py @@ -25,6 +25,7 @@ class Config: preview=str, use_skel=bool, title=str, + config=dict, ) ) self.__read_config() @@ -48,8 +49,8 @@ class Config: for operator_name, operator_content in self.config[key].items(): self.__config_check(operator_name, operator_content, self.valid_keys[key]) - with open(self.config_path, 'w') as f: - yaml.safe_dump(self.config, f, allow_unicode=True) + # with open(self.config_path, 'w') as f: + # yaml.safe_dump(self.config, f, allow_unicode=True) def __config_check(self, block_name: str, contents: dict, required_keys: dict): checklist = [key for key in required_keys] diff --git a/lib/content_processor.py b/lib/content_processor.py new file mode 100644 index 0000000..98e1659 --- /dev/null +++ b/lib/content_processor.py @@ -0,0 +1,58 @@ +import pathlib +import shutil + +class ContentProcessor: + + def __init__(self, config, operator_name): + self.config = config["operators"][operator_name] + self.file_to_process = [key for key, value in self.config["config"].items()] + self.settings = self.config["config"] + self.evalable = [ + "__get_version" + ] + self.__process_value() + + def process(self, file_path): + with open(pathlib.Path.cwd().joinpath(file_path), "r") as f: + content = Formatter(f.read(), "{# ", " #}") + return content.format(**self.settings[file_path.name]) + + def build(self, source_path, target_path): + if source_path.name in self.file_to_process: + content = self.process(source_path) + with open(pathlib.Path.cwd().joinpath(target_path), "w") as f: + f.write(content) + else: + shutil.copyfile( + source_path, + target_path + ) + + def __process_value(self): + for item_key, item_value in self.settings.items(): + for key, value in item_value.items(): + replace_value = value + # if value in evalable + if value in self.evalable: + if value == "__get_version": + replace_value = self.__get_version() + else: + raise Exception("Unsupported function name: {}".format(value)) + self.settings[item_key][key] = replace_value + + def __get_version(self): + with open(pathlib.Path.cwd().joinpath("Version"), "r") as f: + version = f.read() + return version + +class Formatter: + def __init__(self, content, start, end): + self.content = content + self.start = start + self.end = end + + def format(self, **kwargs): + for key, value in kwargs.items(): + identifier = self.start + key + self.end + self.content = self.content.replace(identifier, str(value)) + return self.content \ No newline at end of file diff --git a/template/index.html b/template/index.html index 897f83d..c4ec0de 100644 --- a/template/index.html +++ b/template/index.html @@ -6,23 +6,23 @@ - {title} - - + {# title #} + +
- +
- - - - - + + + + + \ No newline at end of file