Skip to content

Commit 94b9696

Browse files
committed
Separate entry script from builder script
1 parent 3453601 commit 94b9696

File tree

2 files changed

+91
-92
lines changed

2 files changed

+91
-92
lines changed

builder.py

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -228,95 +228,7 @@ def addPackage(zf, name, path, orig, distInfo=False):
228228
if file.endswith(".dll"):
229229
shutil.copy(os.path.join(datafolder, file), ".")
230230

231-
print("WRITE pyunity-editor.c", flush=True)
232-
with open("pyunity-editor.c", "w+") as f:
233-
f.write(textwrap.dedent("""
234-
#define PY_SSIZE_T_CLEAN
235-
#define Py_LIMITED_API 0x03060000
236-
#include <Python.h>
237-
238-
#ifdef NOCONSOLE
239-
#include <windows.h>
240-
#define CHECK_ERROR(n) if (PyErr_Occurred() != NULL) { showError(); exit(1); }
241-
242-
void showError() {
243-
printf("Error encountered\n");
244-
PyObject *type, *value, *traceback;
245-
PyErr_Fetch(&type, &value, &traceback);
246-
PyErr_Print();
247-
248-
PyObject *tracebackModule = PyImport_ImportModule("traceback");
249-
PyObject *formatFunc = PyObject_GetAttrString(tracebackModule, "format_exception");
250-
Py_DecRef(tracebackModule);
251-
252-
PyErr_NormalizeException(&type, &value, &traceback);
253-
PyException_SetTraceback(value, traceback);
254-
255-
PyObject *lines = PyObject_CallFunctionObjArgs(formatFunc, value, NULL);
256-
PyObject *sep = PyUnicode_FromString("");
257-
PyObject *joined = PyUnicode_Join(sep, lines);
258-
Py_DecRef(sep);
259-
Py_DecRef(lines);
260-
261-
wchar_t *msg = PyUnicode_AsWideCharString(joined, NULL);
262-
MessageBoxW(NULL, msg, L"Error loading PyUnity Editor", 0x10L);
263-
PyMem_Free(msg);
264-
265-
PyErr_Restore(type, value, traceback);
266-
}
267-
#else
268-
#define CHECK_ERROR() if (PyErr_Occurred() != NULL) { PyErr_Print(); exit(1); }
269-
#endif
270-
271-
int main(int argc, char **argv) {
272-
wchar_t *path = Py_DecodeLocale("Lib\\python.zip;Lib;Lib\\win32;Lib\\win32\\lib", NULL);
273-
Py_SetPath(path);
274-
wchar_t **program = (wchar_t**)PyMem_Malloc(sizeof(wchar_t**) * argc);
275-
for (int i = 0; i < argc; i++) {
276-
program[i] = Py_DecodeLocale(argv[i], NULL);
277-
}
278-
if (program[0] == NULL) {
279-
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
280-
exit(1);
281-
}
282-
Py_SetProgramName(program[0]);
283-
Py_Initialize();
284-
PySys_SetArgvEx(argc, program, 0);
285-
CHECK_ERROR();
286-
287-
PyObject *editor = PyImport_ImportModule("pyunity_editor.cli");
288-
CHECK_ERROR();
289-
290-
#ifdef NOCONSOLE
291-
PyObject *func = PyObject_GetAttrString(editor, "gui");
292-
#else
293-
PyObject *func = PyObject_GetAttrString(editor, "run");
294-
#endif
295-
CHECK_ERROR();
296-
297-
PyObject *res = PyObject_CallFunction(func, NULL);
298-
CHECK_ERROR();
299-
300-
if (Py_FinalizeEx() < 0) {
301-
exit(1);
302-
}
303-
for (int i = 0; i < argc; i++) {
304-
PyMem_Free((void*)program[i]);
305-
}
306-
PyMem_Free((void*)program);
307-
PyMem_Free((void*)path);
308-
printf("Safely freed memory\n");
309-
return 0;
310-
}
311-
312-
#ifdef NOCONSOLE
313-
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
314-
char* pCmdLine, int nShowCmd) {
315-
return main(__argc, __argv);
316-
}
317-
#endif
318-
"""))
319-
231+
shutil.copy(orig + "\\standalone\\pyunity-editor.c", "..")
320232
shutil.copy(orig + "\\standalone\\icons.ico", "..")
321233
shutil.copy(orig + "\\standalone\\icons.rc", "..")
322234
shutil.copy(orig + "\\standalone\\version.rc", "..")
@@ -334,7 +246,7 @@ def addPackage(zf, name, path, orig, distInfo=False):
334246
print("COMPILE pyunity-editor.exe", flush=True)
335247
subprocess.call([
336248
"cl.exe", "/nologo", "/O2", "/Wall",
337-
"/Tcpyunity-editor.c", "/Fo..\\pyunity-editor.obj",
249+
"/Tc..\\pyunity-editor.c", "/Fo..\\pyunity-editor.obj",
338250
f"/I{sys.base_prefix}\\include", "/DNOCONSOLE",
339251
"/link", "..\\icons.res", "..\\version.res", "/subsystem:windows",
340252
f"/libpath:{sys.base_prefix}\\libs",
@@ -355,10 +267,9 @@ def addPackage(zf, name, path, orig, distInfo=False):
355267
print("COMPILE pyunity-editor.exe", flush=True)
356268
subprocess.call([
357269
"gcc.exe", "-O2", "-Wall", "-mwindows", "-DNOCONSOLE",
358-
"-o", "pyunity-editor.exe", "pyunity-editor.c", "..\\icons.o", "..\\version.o",
270+
"-o", "pyunity-editor.exe", "..\\pyunity-editor.c", "..\\icons.o", "..\\version.o",
359271
"-L.", f"-l{zipname}", f"-I{sys.base_prefix}\\include",
360272
], stdout=sys.stdout, stderr=sys.stderr)
361-
os.remove("pyunity-editor.c")
362273

363274
print(f"ZIP pyunity-editor.zip", flush=True)
364275
os.chdir(tmp)

standalone/pyunity-editor.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#define Py_LIMITED_API 0x03060000
3+
#include <Python.h>
4+
5+
#ifdef NOCONSOLE
6+
#include <windows.h>
7+
#define CHECK_ERROR(n) if (PyErr_Occurred() != NULL) { showError(); exit(1); }
8+
9+
void showError() {
10+
printf("Error encountered\n");
11+
PyObject *type, *value, *traceback;
12+
PyErr_Fetch(&type, &value, &traceback);
13+
PyErr_Print();
14+
15+
PyObject *tracebackModule = PyImport_ImportModule("traceback");
16+
PyObject *formatFunc = PyObject_GetAttrString(tracebackModule, "format_exception");
17+
Py_DecRef(tracebackModule);
18+
19+
PyErr_NormalizeException(&type, &value, &traceback);
20+
PyException_SetTraceback(value, traceback);
21+
22+
PyObject *lines = PyObject_CallFunctionObjArgs(formatFunc, value, NULL);
23+
PyObject *sep = PyUnicode_FromString("");
24+
PyObject *joined = PyUnicode_Join(sep, lines);
25+
Py_DecRef(sep);
26+
Py_DecRef(lines);
27+
28+
wchar_t *msg = PyUnicode_AsWideCharString(joined, NULL);
29+
MessageBoxW(NULL, msg, L"Error loading PyUnity Editor", 0x10L);
30+
PyMem_Free(msg);
31+
32+
PyErr_Restore(type, value, traceback);
33+
}
34+
#else
35+
#define CHECK_ERROR() if (PyErr_Occurred() != NULL) { PyErr_Print(); exit(1); }
36+
#endif
37+
38+
int main(int argc, char **argv) {
39+
wchar_t *path = Py_DecodeLocale("Lib\\python.zip;Lib;Lib\\win32;Lib\\win32\\lib", NULL);
40+
Py_SetPath(path);
41+
wchar_t **program = (wchar_t**)PyMem_Malloc(sizeof(wchar_t**) * argc);
42+
for (int i = 0; i < argc; i++) {
43+
program[i] = Py_DecodeLocale(argv[i], NULL);
44+
}
45+
if (program[0] == NULL) {
46+
#ifdef NOCONSOLE
47+
MessageBoxW(NULL, L"Fatal error: cannot decode argv[0]", L"Error loading PyUnity Editor", 0x10L);
48+
#else
49+
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
50+
#endif
51+
exit(1);
52+
}
53+
Py_SetProgramName(program[0]);
54+
Py_Initialize();
55+
PySys_SetArgvEx(argc, program, 0);
56+
CHECK_ERROR();
57+
58+
PyObject *editor = PyImport_ImportModule("pyunity_editor.cli");
59+
CHECK_ERROR();
60+
61+
#ifdef NOCONSOLE
62+
PyObject *func = PyObject_GetAttrString(editor, "gui");
63+
#else
64+
PyObject *func = PyObject_GetAttrString(editor, "run");
65+
#endif
66+
CHECK_ERROR();
67+
68+
PyObject *res = PyObject_CallFunction(func, NULL);
69+
CHECK_ERROR();
70+
71+
if (Py_FinalizeEx() < 0) {
72+
exit(1);
73+
}
74+
for (int i = 0; i < argc; i++) {
75+
PyMem_Free((void*)program[i]);
76+
}
77+
PyMem_Free((void*)program);
78+
PyMem_Free((void*)path);
79+
printf("Safely freed memory\n");
80+
return 0;
81+
}
82+
83+
#ifdef NOCONSOLE
84+
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
85+
char* pCmdLine, int nShowCmd) {
86+
return main(__argc, __argv);
87+
}
88+
#endif

0 commit comments

Comments
 (0)