Repository navigation
Expand file tree
/
Copy patheditor_prototype.py
More file actions
245 lines (177 loc) · 6.25 KB
/
Copy patheditor_prototype.py
File metadata and controls
245 lines (177 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import getopt
import os
import re
import sys
str_template = {
"help":
"""usage: editor_prototype.py [-option] [args...]
note: The following options are also available in interactive mode.
no option : Enter interactive mode.
-h --help : Print this help message.
-p <name>
--project <name> : Open project by name. Create a new one if not exist.
-q --quit : Quit editor.
-s <name>
--script <name> : Create new script by name. Project should be specified in advance.
""",
"main.cpp":
"""#include "../MixEngine.h"
#include <SDL2/SDL_main.h>
#include "{proj_name}.h"
extern "C" int main(int argc, char** argv) {{
Mix::MixEngine::Initialize("Mix", argc, argv);
return Mix::MixEngine::Instance().execute(std::make_shared<{proj_name}>());
}}
""",
"project.h":
"""#pragma once
#ifndef {proj_name_upper}_H_
#define {proj_name_upper}_H_
#include "../MxApplicationBase.h"
using namespace Mix;
class {proj_name} final : public ApplicationBase {{
public:
std::string getAppName() const override;
Version getAppVersion() const override;
private:
void onMainSceneCreated() override;
}};
#endif
""",
"project.cpp":
"""#include "{proj_name}.h"
#include "../Mx/GameObject/MxGameObject.h"
std::string {proj_name}::getAppName() const {{ return std::string("{proj_name}"); }}
Version {proj_name}::getAppVersion() const {{ return Version(1, 0, 0); }}
void {proj_name}::onMainSceneCreated() {{
}}
""",
"script.h":
"""#pragma once
#ifndef {script_name_upper}_H_
#define {script_name_upper}_H_
#include "../../../../Mx/Component/Script/MxScript.h"
using namespace Mix;
class {script_name} final : public Script {{
MX_DECLARE_RTTI
public:
{script_name}() = default;
~{script_name}() = default;
private:
void start() override;
void update() override;
}};
using H{script_name} = SceneObjectHandle<{script_name}>;
#endif
""",
"script.cpp":
"""#include "{script_name}.h"
MX_IMPLEMENT_RTTI({script_name}, Script)
void {script_name}::start() {{
}}
void {script_name}::update() {{
}}
"""
}
def check_validation(cpp_idf):
return re.match("^[a-zA-Z_][a-zA-Z0-9_]*$", cpp_idf)
def write_if_not_exist(path, string):
if os.path.exists(path):
if os.path.isfile(path):
print("file '{path}' already exists".format(path=path))
else:
print("file name '{path}' occupied by a directory".format(path=path))
return False
try:
file = open(path, "w")
file.write(string)
file.close()
except Exception as e:
print(str(e))
return False
print("create file '{path}'".format(path=path))
return True
def help_impl():
global str_template
print(str_template["help"])
def project_impl(proj_name):
if not check_validation(proj_name):
print("invalid project name '{proj_name}'".format(proj_name=proj_name))
return ""
os.makedirs("./" + proj_name, exist_ok=True)
global str_template
# .h
write_if_not_exist("./{proj_name}/{proj_name}.h".format(proj_name=proj_name),
str_template["project.h"].format(proj_name=proj_name, proj_name_upper=proj_name.upper()))
# .cpp
write_if_not_exist("./{proj_name}/{proj_name}.cpp".format(proj_name=proj_name),
str_template["project.cpp"].format(proj_name=proj_name))
# main.cpp
write_if_not_exist("./{proj_name}/main.cpp".format(proj_name=proj_name),
str_template["main.cpp"].format(proj_name=proj_name))
print("open project '{proj_name}'".format(proj_name=proj_name))
return proj_name
def script_impl(proj_name, script_name):
if not check_validation(script_name):
print("invalid script name '{script_name}'".format(script_name=script_name))
return
if not proj_name:
print("project not specified for script '{script_name}'".format(script_name=script_name))
return
# todo: specify output path
script_path = "./{proj_name}/Assets/Scripts/{script_name}".format(proj_name=proj_name, script_name=script_name)
os.makedirs(script_path, exist_ok=True)
global str_template
# .h
write_if_not_exist("{script_path}/{script_name}.h".format(script_path=script_path, script_name=script_name),
str_template["script.h"].format(script_name=script_name, script_name_upper=script_name.upper()))
# .cpp
write_if_not_exist("{script_path}/{script_name}.cpp".format(script_path=script_path, script_name=script_name),
str_template["script.cpp"].format(script_name=script_name))
print("create script '{script_name}' in project '{proj_name}'".format(proj_name=proj_name, script_name=script_name))
def main(argv):
active_proj = ""
if not len(argv):
while True:
commands = input().split()
if not len(commands):
continue
# func_dict.get(commands[0], lambda *_: print("invalid command"))(*commands[1:])
opt = commands[0]
if opt in ("h", "help"):
help_impl()
elif opt in ("p", "project"):
if len(commands) == 2:
active_proj = project_impl(commands[1])
else:
active_proj = ""
print("invalid syntax")
elif opt in ("s", "script"):
if len(commands) == 2:
script_impl(active_proj, commands[1])
else:
print("invalid syntax")
elif opt in ("q", "quit"):
break
else:
print("invalid command")
print()
else:
# todo: support multiple arguments
try:
args, _ = getopt.getopt(argv, "hp:qs:", ["help", "project=", "quit", "script="])
except getopt.GetoptError:
print("invalid command")
return
for opt, val in args:
if opt in ("-h", "--help"):
help_impl()
break
elif opt in ("-p", "--project"):
active_proj = project_impl(val)
elif opt in ("-s", "--script"):
script_impl(active_proj, val)
elif opt in ("-q", "--quit"):
break
if __name__ == "__main__":
main(sys.argv[1:])