99
1010- python3 build_code_from_template.py VisualDetector PokemonSV Map PokeCenter Icon
1111 Generates PokemonSV_MapPokeCenterIconDetector.h and PokemonSV_MapPokeCenterIconDetector.cpp.
12+ - python3 build_code_from_template.py Program PokemonSV Auto Story
13+ Generates PokemonSV_AutoStory.h and PokemonSV_AutoStory.cpp.
1214"""
1315
1416import sys
1517import os
1618
17- from typing import Dict
19+ from typing import Dict , List
20+
21+
22+ def apply_line_replacement (line : str , mapping : Dict [str , str ]) -> str :
23+ for source , target in mapping .items ():
24+ line = line .replace (source , target )
25+ return line
26+
27+
28+ def build_file_from_template (
29+ mapping : Dict [str , str ],
30+ template_folder : str ,
31+ template_filename : str ,
32+ ) -> None :
33+ """
34+ generate a file from a template file by mapping: template str -> target str
35+ The file is saved at the current working dir.
36+ """
37+
38+ file_ext : str = template_filename .split ('.' )[- 1 ]
39+ template_filepath : str = os .path .join (template_folder , template_filename )
40+
41+ target_filename : str = apply_line_replacement (template_filename , mapping )
42+
43+ with open (template_filepath , "r" ) as f :
44+ lines = f .readlines ()
45+ lines = [apply_line_replacement (line , mapping ) for line in lines ]
46+
47+ with open (target_filename , "w" , newline = '\r \n ' ) as f :
48+ f .writelines (lines )
49+
50+ print (f"Saved template { file_ext } file to { target_filename } " )
51+
52+
53+ def build_files_from_templates (
54+ mapping : Dict [str , str ],
55+ template_folder : str ,
56+ template_filenames : List [str ],
57+ ) -> None :
58+ """
59+ generate files from template files by a mapping: template str -> target str
60+ The files are saved at the current working dir.
61+ """
62+
63+ for filename in template_filenames :
64+ build_file_from_template (
65+ mapping = mapping ,
66+ template_folder = template_folder ,
67+ template_filename = filename ,
68+ )
69+
70+
71+ def create_cpp_class_name (name : str ) -> str :
72+ """
73+ Given a name (e.g. "Three-Segment Dudunsparce Finder"), convert it into a C++ class name (like "ThreeSegmentDudunsparceFinder")
74+ """
75+ return name .replace (" " , "" ).replace ("-" , "" ).replace ("_" , "" )
76+
1877
1978if len (sys .argv ) == 1 :
2079 print (
3695template_folder = os .path .join (code_root_path , "SerialPrograms" , "Scripts" , "CodeTemplates" )
3796print (f"Template folder: { template_folder } " )
3897
39-
40- def apply_replacement (line : str , mapping : Dict [str , str ]) -> str :
41- for source , target in mapping .items ():
42- line = line .replace (source , target )
43- return line
98+ template_folder = os .path .join (template_folder , sys .argv [1 ])
4499
45100if sys .argv [1 ] == "VisualDetector" :
46101 assert len (sys .argv ) >= 4
@@ -51,38 +106,38 @@ def apply_replacement(line: str, mapping: Dict[str, str]) -> str:
51106 mapping = {
52107 "GameName" : game_name ,
53108 "Object Name" : object_name ,
54- "ObjectName" : object_name . replace ( " " , "" ),
109+ "ObjectName" : create_cpp_class_name ( object_name )
55110 }
56111
57- template_h_filename = "GameName_ObjectNameDetector.h"
58- template_cpp_filename = "GameName_ObjectNameDetector.cpp"
59-
60- template_folder = os .path .join (template_folder , sys .argv [1 ])
61-
62- template_h_filepath = os .path .join (template_folder , template_h_filename )
63- template_cpp_filepath = os .path .join (template_folder , template_cpp_filename )
64-
65- target_h_filename = apply_replacement (template_h_filename , mapping )
66- target_cpp_filename = apply_replacement (template_cpp_filename , mapping )
67-
68-
69- with open (template_h_filepath , "r" ) as f :
70- lines = f .readlines ()
71- lines = [apply_replacement (line , mapping ) for line in lines ]
72-
73- with open (target_h_filename , "w" , newline = '\r \n ' ) as f :
74- f .writelines (lines )
112+ build_files_from_templates (
113+ mapping = mapping ,
114+ template_folder = template_folder ,
115+ template_filenames = [
116+ "GameName_ObjectNameDetector.h" ,
117+ "GameName_ObjectNameDetector.cpp" ,
118+ ]
119+ )
120+ elif sys .argv [1 ] == "Program" :
121+ assert len (sys .argv ) >= 4
122+ game_name = sys .argv [2 ] # e.g. "PokemonSV"
123+ program_name = " " .join (sys .argv [3 :]) # e.g. "Auto Story"
124+ print (f"Building a Program with name { program_name } in game { game_name } ." )
75125
76- print (f"Saved template h file to { target_h_filename } " )
126+ mapping = {
127+ "GameName" : game_name ,
128+ "Program Name" : program_name ,
129+ "ProgramName" : create_cpp_class_name (program_name )
130+ }
77131
78- with open (template_cpp_filepath , "r" ) as f :
79- lines = f .readlines ()
80- lines = [apply_replacement (line , mapping ) for line in lines ]
81-
82- with open (target_cpp_filename , "w" , newline = '\r \n ' ) as f :
83- f .writelines (lines )
132+ build_files_from_templates (
133+ mapping = mapping ,
134+ template_folder = template_folder ,
135+ template_filenames = [
136+ "GameName_ProgramName.h" ,
137+ "GameName_ProgramName.cpp" ,
138+ ]
139+ )
84140
85- print (f"Saved template cpp file to { target_cpp_filename } " )
86141
87142
88143
0 commit comments