comparison mlk-run/mlk-run.c @ 642:05b585720d3b

core: reimplement most of js
author David Demelier <markand@malikania.fr>
date Sun, 17 Dec 2023 09:50:36 +0100
parents
children 993d9ccedcf6
comparison
equal deleted inserted replaced
641:fcd124e513ea 642:05b585720d3b
1 /*
2 * main.c -- main mlk-run file
3 *
4 * Copyright (c) 2020-2023 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 #include <limits.h>
22
23 #include <duktape.h>
24 #include <duk_module.h>
25
26 #include <mlk/util/util.h>
27
28 #include <mlk/core/core.h>
29 #include <mlk/core/err.h>
30 #include <mlk/core/panic.h>
31 #include <mlk/core/vfs-dir.h>
32 #include <mlk/core/vfs-zip.h>
33 #include <mlk/core/vfs.h>
34
35 union {
36 struct mlk_vfs_dir dir;
37
38 #if defined(MLK_WITH_ZIP)
39 struct mlk_vfs_zip zip;
40 #endif
41 } iface;
42
43 /* VFS loader to support zip and directories when loading game. */
44 static struct mlk_vfs *vfs;
45
46 /* Javascript context. */
47 static duk_context *ctx;
48
49 static duk_ret_t
50 modsearch(duk_context *ctx)
51 {
52 char path[MLK_PATH_MAX] = {}, *data;
53 struct mlk_vfs_file *file;
54 size_t datasz;
55
56 snprintf(path, sizeof (path), "%s.js", duk_require_string(ctx, 0));
57
58 if ((file = mlk_vfs_open(vfs, path, "r")) < 0)
59 return duk_error(ctx, DUK_ERR_ERROR, "%s", mlk_err());
60 if (!(data = mlk_vfs_file_read_all(file, &datasz))) {
61 mlk_vfs_file_finish(file);
62 return duk_error(ctx, DUK_ERR_ERROR, "%s", mlk_err());
63 }
64
65 mlk_vfs_file_finish(file);
66 duk_push_lstring(ctx, data, datasz);
67 free(data);
68
69 return 1;
70 }
71
72 static void
73 core_bind(duk_context *ctx)
74 {
75 #if 0
76 /* Brings Mlk global object. */
77 mlk_js_core_bind(ctx, &vfs);
78
79 js_action_bind(ctx);
80 js_action_stack_bind(ctx);
81 js_animation_bind(ctx);
82 js_clock_bind(ctx);
83 js_color_bind(ctx);
84 js_drawable_bind(ctx);
85 js_drawable_stack_bind(ctx);
86 js_event_bind(ctx);
87 js_font_bind(ctx);
88 js_game_bind(ctx);
89 js_music_bind(ctx);
90 js_painter_bind(ctx);
91 js_panic_bind(ctx);
92 js_sound_bind(ctx);
93 js_sprite_bind(ctx);
94 js_state_bind(ctx);
95 js_texture_bind(ctx);
96 js_trace_bind(ctx);
97 js_window_bind(ctx);
98 #endif
99 }
100
101 static void
102 init(void)
103 {
104 /* TODO: this is temporary. */
105 if (mlk_core_init("fr.malikania", "mlk-run") < 0)
106 mlk_panic();
107
108 /* Fireup Javascript. */
109 ctx = duk_create_heap_default();
110 core_bind(ctx);
111
112 /* Setup module loader. */
113 duk_module_duktape_init(ctx);
114 duk_get_global_string(ctx, "Duktape");
115 duk_push_c_function(ctx, modsearch, 4);
116 duk_put_prop_string(ctx, -2, "modSearch");
117 duk_pop(ctx);
118 }
119
120 static void
121 startup(void)
122 {
123 struct mlk_vfs_file *file;
124 char *code;
125
126 if (!(file = mlk_vfs_open(vfs, "main.js", "r")))
127 mlk_panic();
128 if (!(code = mlk_vfs_file_read_all(file, NULL))) {
129 mlk_vfs_file_finish(file);
130 mlk_panic();
131 }
132
133 mlk_vfs_file_finish(file);
134
135 if (duk_peval_string(ctx, code))
136 mlk_panicf("%s", duk_safe_to_string(ctx, -1));
137
138 free(code);
139 }
140
141 static void
142 finish(void)
143 {
144 mlk_vfs_finish(vfs);
145 duk_destroy_heap(ctx);
146 }
147
148 static void
149 loadzip(const char *path)
150 {
151 #if 0
152 if (vfs_zip(&vfs, path, "r") < 0)
153 panic();
154 #endif
155 }
156
157 static void
158 loaddirectory(const char *path)
159 {
160 #if 0
161 vfs_directory(&vfs, path);
162 #endif
163 }
164
165 static void
166 load(const char *path)
167 {
168 /* TODO: improve this. */
169 if (strstr(path, ".mlk"))
170 loadzip(path);
171 else
172 loaddirectory(path);
173
174 startup();
175 }
176
177 static void
178 run(int argc, char **argv)
179 {
180 if (argc < 0) {
181 fprintf(stderr, "usage: mlk-run game\n");
182 exit(1);
183 }
184
185 load(argv[0]);
186 }
187
188 int
189 main(int argc, char **argv)
190 {
191 --argc;
192 ++argv;
193
194 init();
195 run(argc, argv);
196 finish();
197 }