From f1477e03da34357106146b668f33eb50027dd560 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 19 Feb 2023 21:59:05 +0800 Subject: [PATCH] promote som misc work --- MkMtlCfg.py => Scripts/MkMtlCfg.py | 21 +++--- SuperScriptDecorator/.vscode/launch.json | 14 ++++ SuperScriptDecorator/CustomConfig.py | 64 ++++++++++++++++-- SuperScriptDecorator/Progressbar.py | 66 ++++++++++++------- SuperScriptDecorator/SuperScriptDecorator.py | 56 ++++++++-------- .../SuperScriptDecorator.pyproj | 50 -------------- SuperScriptMaterializer.sln | 18 ----- SuperScriptViewer/SuperScriptViewer.pyproj | 38 ----------- example.txt | 2 + 9 files changed, 157 insertions(+), 172 deletions(-) rename MkMtlCfg.py => Scripts/MkMtlCfg.py (95%) create mode 100644 SuperScriptDecorator/.vscode/launch.json delete mode 100644 SuperScriptDecorator/SuperScriptDecorator.pyproj delete mode 100644 SuperScriptViewer/SuperScriptViewer.pyproj create mode 100644 example.txt diff --git a/MkMtlCfg.py b/Scripts/MkMtlCfg.py similarity index 95% rename from MkMtlCfg.py rename to Scripts/MkMtlCfg.py index a486156..200704d 100644 --- a/MkMtlCfg.py +++ b/Scripts/MkMtlCfg.py @@ -1,8 +1,14 @@ -import Scripts.VSProp as VSProp +import VSProp import os import sys -# ======================== const define +# =========== check work dir =========== + +if not os.path.isfile(os.path.join(os.getcwd(), 'README.md')): + print('Error! Please run this script at the root of this repository.') + sys.exit(1) + +# =========== const define =========== build_type_standalone = "standalone" build_type_plugin = "plugin" @@ -68,7 +74,7 @@ valid_vt21_reverse_work_type = ( 'doyagu' ) -# ======================== assist func +# =========== assist func =========== def get_executable_virtools(vt_ver): if vt_ver == '21': return 'Dev.exe' @@ -81,7 +87,7 @@ def get_executable_virtools(vt_ver): elif vt_ver == '50': return 'devr.exe' -# ======================== requirement get +# =========== requirement get =========== # get basic cfg, such as build type, and vt version while True: @@ -147,8 +153,7 @@ else: else: break -# ======================== construct some path -# .......todo +# =========== construct some path =========== # build sqlite related data sqlite_header_path = input_sqlite_header_path @@ -226,7 +231,7 @@ else: virtools_lib_path = os.path.join(input_virtools_root_path, 'Sdk/Lib/Win32/Release') -# ======================== create props +# =========== create props =========== props = VSProp.VSPropWriter() vcxproj = VSProp.VSVcxprojModifier('./SuperScriptMaterializer/SuperScriptMaterializer.vcxproj') @@ -270,7 +275,7 @@ props.AddMacro('VIRTOOLS_MODULE_DEFINE', virtools_module_define) props.Write2File('./SuperScriptMaterializer/Virtools.props') vcxproj.Write2File() -# ======================== create vt21 props +# =========== create vt21 props =========== # if we are using virtools 2.1. and we use gamepiaynmo as our # reverse library. we need enable project GPVirtoolsStatic and diff --git a/SuperScriptDecorator/.vscode/launch.json b/SuperScriptDecorator/.vscode/launch.json new file mode 100644 index 0000000..7712e61 --- /dev/null +++ b/SuperScriptDecorator/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "SSDecorator", + "type": "python", + "request": "launch", + "program": "SuperScriptDecorator.py", + "console": "integratedTerminal", + "args": ["-d", "-i", "../example.txt", "-o", "decorate.db"], + "justMyCode": false + } + ] +} \ No newline at end of file diff --git a/SuperScriptDecorator/CustomConfig.py b/SuperScriptDecorator/CustomConfig.py index 4692903..f8259a7 100644 --- a/SuperScriptDecorator/CustomConfig.py +++ b/SuperScriptDecorator/CustomConfig.py @@ -1,8 +1,58 @@ -import locale +import locale, sys, os, shlex -# encoding list -# https://docs.python.org/3/library/codecs.html#standard-encodings -database_encoding = locale.getpreferredencoding() -export_db = "import.txt" -decorated_db = "decorate.db" -debug_mode = False +class InputEntry(object): + def __init__(self, data: tuple[str]): + if len(data) != 4: + raise Exception(f"Input syntax error. Require 4 items but got {len(data)}") + self.m_Name: str = data[0] + self.m_VtFile: str = data[1] + self.m_ExportDb: str = data[2] + self.m_EnvDb: str = data[3] + +class CustomConfig(object): + def __init__(self): + # encoding list + # https://docs.python.org/3/library/codecs.html#standard-encodings + self.m_DatabaseEncoding: str = locale.getpreferredencoding() + self.m_DebugMode = False + + self.m_ImportTxt: str = None + self.m_DecoratedDb: str = None + + self.m_InputEntries: list[InputEntry] = [] + + def Regulate(self) -> bool: + # check input and output + if self.m_ImportTxt is None: + print("No input. Decorator exit.") + return False + if not os.path.isfile(self.m_ImportTxt): + print(f'No such input: "{self.m_ImportTxt}"') + return False + + if self.m_DecoratedDb is None: + print("No output. Decorator exit.") + return False + if os.path.isdir(self.m_DecoratedDb): + print("Output must be a file.") + return False + + # remove result database in debug mode + if self.m_DebugMode and os.path.isfile(self.m_DecoratedDb): + os.remove(self.m_DecoratedDb) + + # process input file + try: + with open(self.m_ImportTxt, 'r', encoding='utf-8') as f: + while True: + ln = f.readline() + if ln == '': break + ln.strip() + if ln == '': continue + + self.m_InputEntries.append(InputEntry(shlex.split(ln))) + except Exception as ex: + print("Errro when processing input file.") + print(ex) + + return True diff --git a/SuperScriptDecorator/Progressbar.py b/SuperScriptDecorator/Progressbar.py index b647777..d4134b6 100644 --- a/SuperScriptDecorator/Progressbar.py +++ b/SuperScriptDecorator/Progressbar.py @@ -1,29 +1,51 @@ import sys -value_All = 0 -value_Now = 0 -progressbar_span = 2 -progressbar_count = int(100/progressbar_span) +class Prograssbar(object): + def __init__(self, filecount: int): + if (filecount < 0): raise Exception("Progressbar can not hold minus length!") -def initProgressbar(all): - global value_Now, value_All - value_All = all - value_Now = 0 + self.__FileCount: int = filecount + self.__FileNow: int = 0 + self.__ContentCount: int = 0 + self.__ContentNow: int = 0 - sys.stdout.write('[{}] 0%'.format(progressbar_count * '=')) - sys.stdout.flush() + self.__PbarFullChar: int = 50 + self.__PercentPerFile: float = 1 / self.__FileCount + self.__CurFileName: str = None -def stepProgressbar(): - global value_Now, value_All - value_Now += 1 - if (value_Now > value_All): - value_Now = value_All + self.__Render() - percentage = int(value_Now / value_All * 100) - percentage_bar = int(value_Now / value_All * progressbar_count) - sys.stdout.write('\r[{}{}] {}%'.format(percentage_bar * '#',(progressbar_count - percentage_bar) * '=', percentage)) - sys.stdout.flush() + def StepFile(self, newfile: str, content_len: int): + if self.__CurFileName is not None: + # not first call, INC FileNow + # if first call, do not INC it + self.__FileNow += 1 + # apply others + self.__CurFileName = newfile + self.__ContentNow = 0 + self.__ContentCount = content_len -def finProgressbar(): - sys.stdout.write('\r[{}] 100%\n'.format(progressbar_count * '#')) - sys.stdout.flush() \ No newline at end of file + self.__Render() + + def StepContent(self): + self.__ContentNow += 1 + + self.__Render() + + def Finish(self): + sys.stdout.write('\n') + sys.stdout.flush() + + def __Render(self): + percentage_content: float = 0 if self.__ContentCount == 0 else (self.__ContentNow / self.__ContentCount) + percentage_full: float = (percentage_content + self.__FileNow) * self.__PercentPerFile + + percentage_bar = int(percentage_full * self.__PbarFullChar) + + sys.stdout.write('\r[{}{}] {:.2f}% - {}'.format( + percentage_bar * '#', + (self.__PbarFullChar - percentage_bar) * '=', + percentage_full * 100, + self.__CurFileName + )) + sys.stdout.flush() diff --git a/SuperScriptDecorator/SuperScriptDecorator.py b/SuperScriptDecorator/SuperScriptDecorator.py index 279c3b6..d873718 100644 --- a/SuperScriptDecorator/SuperScriptDecorator.py +++ b/SuperScriptDecorator/SuperScriptDecorator.py @@ -1,53 +1,51 @@ -import CustomConfig -import DecoratorCore -import os -import sys -import getopt -import logging +import CustomConfig, DecoratorCore, Progressbar +import os, sys, getopt, logging, time +# print banner +print('Super Script Decorator') +print('Homepage: https://github.com/yyc12345/SuperScriptMaterializer') +print('Report bug: https://github.com/yyc12345/SuperScriptMaterializer/issues') +print('') + +# try get args try: opts, args = getopt.getopt(sys.argv[1:], "hi:o:e:c:fd") except getopt.GetoptError: print('Wrong arguments!') print('python SuperScriptViewer.py -i -o -c -d') sys.exit(1) + +# analyze args +cfg: CustomConfig.CustomConfig = CustomConfig.CustomConfig() for opt, arg in opts: if opt == '-h': print('python SuperScriptViewer.py -i -o -c -d') sys.exit(0) elif opt == '-i': - CustomConfig.export_db = arg + cfg.m_ImportTxt = arg elif opt == '-o': - CustomConfig.decorated_db = arg + cfg.m_DecoratedDb = arg elif opt == '-c': - CustomConfig.database_encoding = arg + cfg.m_DatabaseEncoding = arg elif opt == '-d': - CustomConfig.debug_mode = True + cfg.m_DebugMode = True -print('Super Script Decorator') -print('Homepage: https://github.com/yyc12345/SuperScriptMaterializer') -print('Report bug: https://github.com/yyc12345/SuperScriptMaterializer/issues') -print('') - -# process input and output -if not os.path.isfile(CustomConfig.export_db): - print('No import.txt. Fail to generate. Exit app.') +# regulate data +if not cfg.Regulate(): + # failed. exit program sys.exit(1) -# real db generator func -def dc_wrapper(): - pass - -# generate db -if CustomConfig.debug_mode: - DecoratorCore.run() +# if in debug mode, run directly +# otherwise, run with a try wrapper. +if cfg.m_DebugMode: + DecoratorCore.run(cfg) else: try: - DecoratorCore.run() - except Exception, ex: + DecoratorCore.run(cfg) + except Exception as ex: print("!!! An error occurs. Please report follwoing error output and reproduce file to developer. !!!") logging.exception(ex) sys.exit(1) - -print('Decorated database generating done.') + +print('Decorated database generation done.') diff --git a/SuperScriptDecorator/SuperScriptDecorator.pyproj b/SuperScriptDecorator/SuperScriptDecorator.pyproj deleted file mode 100644 index f42fa3a..0000000 --- a/SuperScriptDecorator/SuperScriptDecorator.pyproj +++ /dev/null @@ -1,50 +0,0 @@ - - - Debug - 2.0 - 6d751bf5-87d6-4123-94b3-34721938cf04 - . - SuperScriptDecorator.py - - - . - . - SuperScriptDecorator - SuperScriptDecorator - Standard Python launcher - -f -d - False - - - true - false - - - true - false - - - - Code - - - Code - - - Code - - - Code - - - - - - - - - - - \ No newline at end of file diff --git a/SuperScriptMaterializer.sln b/SuperScriptMaterializer.sln index 5dc992e..e513fb2 100644 --- a/SuperScriptMaterializer.sln +++ b/SuperScriptMaterializer.sln @@ -3,40 +3,22 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29418.71 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "SuperScriptDecorator", "SuperScriptDecorator\SuperScriptDecorator.pyproj", "{6D751BF5-87D6-4123-94B3-34721938CF04}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SuperScriptMaterializer", "SuperScriptMaterializer\SuperScriptMaterializer.vcxproj", "{4D941003-020F-47FD-9FA2-FFC989E306B8}" EndProject -Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "SuperScriptViewer", "SuperScriptViewer\SuperScriptViewer.pyproj", "{0E4B5021-27EA-4F79-B87D-E123AFB3D788}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GPVirtoolsStatic", "GPVirtoolsStatic\GPVirtoolsStatic.vcxproj", "{38703AB6-BC5D-4062-BC5B-1BF195333B16}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU Debug|x86 = Debug|x86 - Release|Any CPU = Release|Any CPU Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {6D751BF5-87D6-4123-94B3-34721938CF04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6D751BF5-87D6-4123-94B3-34721938CF04}.Debug|x86.ActiveCfg = Debug|Any CPU - {6D751BF5-87D6-4123-94B3-34721938CF04}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6D751BF5-87D6-4123-94B3-34721938CF04}.Release|x86.ActiveCfg = Release|Any CPU - {4D941003-020F-47FD-9FA2-FFC989E306B8}.Debug|Any CPU.ActiveCfg = Debug|Win32 {4D941003-020F-47FD-9FA2-FFC989E306B8}.Debug|x86.ActiveCfg = Debug|Win32 {4D941003-020F-47FD-9FA2-FFC989E306B8}.Debug|x86.Build.0 = Debug|Win32 - {4D941003-020F-47FD-9FA2-FFC989E306B8}.Release|Any CPU.ActiveCfg = Release|Win32 {4D941003-020F-47FD-9FA2-FFC989E306B8}.Release|x86.ActiveCfg = Release|Win32 {4D941003-020F-47FD-9FA2-FFC989E306B8}.Release|x86.Build.0 = Release|Win32 - {0E4B5021-27EA-4F79-B87D-E123AFB3D788}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0E4B5021-27EA-4F79-B87D-E123AFB3D788}.Debug|x86.ActiveCfg = Debug|Any CPU - {0E4B5021-27EA-4F79-B87D-E123AFB3D788}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0E4B5021-27EA-4F79-B87D-E123AFB3D788}.Release|x86.ActiveCfg = Release|Any CPU - {38703AB6-BC5D-4062-BC5B-1BF195333B16}.Debug|Any CPU.ActiveCfg = Debug|Win32 {38703AB6-BC5D-4062-BC5B-1BF195333B16}.Debug|x86.ActiveCfg = Debug|Win32 {38703AB6-BC5D-4062-BC5B-1BF195333B16}.Debug|x86.Build.0 = Debug|Win32 - {38703AB6-BC5D-4062-BC5B-1BF195333B16}.Release|Any CPU.ActiveCfg = Release|Win32 {38703AB6-BC5D-4062-BC5B-1BF195333B16}.Release|x86.ActiveCfg = Release|Win32 {38703AB6-BC5D-4062-BC5B-1BF195333B16}.Release|x86.Build.0 = Release|Win32 EndGlobalSection diff --git a/SuperScriptViewer/SuperScriptViewer.pyproj b/SuperScriptViewer/SuperScriptViewer.pyproj deleted file mode 100644 index ab09565..0000000 --- a/SuperScriptViewer/SuperScriptViewer.pyproj +++ /dev/null @@ -1,38 +0,0 @@ - - - Debug - 2.0 - 0e4b5021-27ea-4f79-b87d-e123afb3d788 - . - SuperScriptViewer.py - - - . - . - SuperScriptViewer - SuperScriptViewer - - - true - false - - - true - false - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/example.txt b/example.txt new file mode 100644 index 0000000..d50b73f --- /dev/null +++ b/example.txt @@ -0,0 +1,2 @@ +Ballance/vt2obj "example.nmo" "export.db" "env.db" +"Ballance/vt2obj mirror" Gameplay.nmo export.db env.db \ No newline at end of file