comparison src/mlk-run/main.c @ 337:68b9d010e081

js: add minimal Javascript binding
author David Demelier <markand@malikania.fr>
date Thu, 14 Oct 2021 12:52:41 +0200
parents
children 94828af916bb
comparison
equal deleted inserted replaced
336:f5a5bbb77122 337:68b9d010e081
1 /*
2 * main.c -- main mlk-run file
3 *
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <duktape.h>
23
24 #include <core/panic.h>
25 #include <core/vfs-directory.h>
26 #include <core/vfs-zip.h>
27 #include <core/vfs.h>
28
29 #include <core/js-window.h>
30
31 /* VFS loader to support zip and directories when loading game. */
32 static struct vfs vfs;
33
34 /* Javascript context. */
35 static duk_context *ctx;
36
37 static void
38 core_bind(duk_context *ctx)
39 {
40 js_window_bind(ctx);
41 }
42
43 static void
44 init(void)
45 {
46 /* TODO: this is temporary. */
47 if (core_init("fr.malikania", "mlk-run") < 0)
48 panic();
49
50 /* Fireup Javascript. */
51 ctx = duk_create_heap_default();
52 core_bind(ctx);
53 }
54
55 static char *
56 extract(struct vfs_file *file)
57 {
58 FILE *fp;
59 char *out, buf[BUFSIZ];
60 size_t len, nr;
61
62 if (!(fp = open_memstream(&out, &len)))
63 panic();
64
65 while ((nr = vfs_file_read(file, buf, sizeof (buf))) > 0)
66 if (fwrite(buf, 1, nr, fp) <= 0)
67 panic();
68
69 fclose(fp);
70
71 return out;
72 }
73
74 static void
75 startup(void)
76 {
77 struct vfs_file main;
78 char *code;
79
80 if (vfs_open(&vfs, &main, "main.js", "r") < 0)
81 panic();
82
83 code = extract(&main);
84 vfs_file_finish(&main);
85
86 if (duk_peval_string(ctx, code))
87 panicf("%s", duk_safe_to_string(ctx, -1));
88
89 free(code);
90 }
91
92 static void
93 finish(void)
94 {
95 vfs_finish(&vfs);
96 duk_destroy_heap(ctx);
97 }
98
99 static void
100 loadzip(const char *path)
101 {
102 if (vfs_zip(&vfs, path, "r") < 0)
103 panic();
104 }
105
106 static void
107 loaddirectory(const char *path)
108 {
109 vfs_directory(&vfs, path);
110 }
111
112 static void
113 load(const char *path)
114 {
115 /* TODO: improve this. */
116 if (strstr(path, ".mlk"))
117 loadzip(path);
118 else
119 loaddirectory(path);
120
121 startup();
122 }
123
124 static void
125 run(int argc, char **argv)
126 {
127 if (argc < 0) {
128 fprintf(stderr, "usage: mlk-run game\n");
129 exit(1);
130 }
131
132 load(argv[0]);
133 }
134
135 int
136 main(int argc, char **argv)
137 {
138 --argc;
139 ++argv;
140
141 init();
142 run(argc, argv);
143 finish();
144 }