Debian Codegraph @

terps/frankendrift/FrankenDrift.GlkRunner/FrankenDrift.GlkRunner.Gargoyle/Main.cs

raw symbols SCIP
1 using FrankenDrift.GlkRunner.Glk;
2 using System.Reflection;
3 using System.Runtime.CompilerServices;
4 using System.Runtime.InteropServices;
5
6 namespace FrankenDrift.GlkRunner.Gargoyle
7 {
8 static partial class Garglk_Pinvoke
9 {
10 // Universal Glk functions.
11 [DllImport("garglk")]
12 internal static extern BlorbError giblorb_set_resource_map(StreamHandle fileStream);
13 [DllImport("garglk")]
14 internal static extern void glk_cancel_hyperlink_event(WindowHandle winId);
15 [DllImport("garglk")]
16 internal static extern void glk_cancel_line_event(WindowHandle winId, ref Event ev);
17 [LibraryImport("garglk")]
18 internal static partial void glk_exit();
19 [DllImport("garglk")]
20 internal static extern FileRefHandle glk_fileref_create_by_name(FileUsage usage, [MarshalAs(UnmanagedType.LPStr)] string name, Glk.FileMode fmode, uint rock);
21 [DllImport("garglk")]
22 internal static extern FileRefHandle glk_fileref_create_by_prompt(FileUsage usage, Glk.FileMode fmode, uint rock);
23 [DllImport("garglk")]
24 internal static extern FileRefHandle glk_fileref_create_temp(FileUsage usage, uint rock);
25 [DllImport("garglk")]
26 internal static extern void glk_fileref_destroy(FileRefHandle fref);
27 [DllImport("garglk")]
28 internal static extern uint glk_image_draw(WindowHandle winid, uint imageId, int val1, int val2);
29 [LibraryImport("garglk")]
30 internal static partial uint glk_image_get_info(uint imageId, ref uint width, ref uint height);
31 [LibraryImport("garglk")]
32 internal static partial void glk_put_buffer([MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)] byte[] s, uint len);
33 [DllImport("garglk")]
34 internal static extern void glk_put_buffer_stream(StreamHandle streamId, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)] byte[] s, uint len);
35 [LibraryImport("garglk")]
36 internal static partial void glk_put_buffer_uni([MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] uint[] s, uint len);
37 [DllImport("garglk")]
38 internal static extern void glk_request_char_event(WindowHandle winId);
39 [DllImport("garglk")]
40 internal static extern void glk_request_hyperlink_event(WindowHandle winId);
41 [DllImport("garglk")]
42 internal static extern unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen);
43 [DllImport("garglk")]
44 internal static extern unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen);
45 [DllImport("garglk")]
46 internal static extern SoundChannel glk_schannel_create(uint rock);
47 [DllImport("garglk")]
48 internal static extern void glk_schannel_destroy(SoundChannel chan);
49 [DllImport("garglk")]
50 internal static extern void glk_schannel_pause(SoundChannel chan);
51 [DllImport("garglk")]
52 internal static extern uint glk_schannel_play(SoundChannel chan, uint sndId);
53 [DllImport("garglk")]
54 internal static extern uint glk_schannel_play_ext(SoundChannel chan, uint sndId, uint repeats, uint notify);
55 [DllImport("garglk")]
56 internal static extern void glk_schannel_set_volume(SoundChannel chan, uint vol);
57 [DllImport("garglk")]
58 internal static extern void glk_schannel_stop(SoundChannel chan);
59 [DllImport("garglk")]
60 internal static extern void glk_schannel_unpause(SoundChannel chan);
61 [DllImport("garglk")]
62 internal static extern void glk_select(ref Event ev);
63 [LibraryImport("garglk")]
64 internal static partial void glk_set_hyperlink(uint linkval);
65 [DllImport("garglk")]
66 internal static extern void glk_set_style(Style s);
67 [DllImport("garglk")]
68 internal static extern void glk_set_window(WindowHandle winId);
69 [DllImport("garglk")]
70 internal static extern StreamHandle glk_stream_open_file(FileRefHandle fileref, Glk.FileMode fmode, uint rock);
71 [DllImport("garglk")]
72 internal static unsafe extern StreamHandle glk_stream_open_memory(byte* buf, uint buflen, Glk.FileMode mode, uint rock);
73 [DllImport("garglk")]
74 internal static extern void glk_stream_close(StreamHandle stream, ref StreamResult streamResult);
75 [DllImport("garglk")]
76 internal static extern void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode);
77 [DllImport("garglk")]
78 internal static extern void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val);
79 [DllImport("garglk")]
80 internal static extern uint glk_style_measure(WindowHandle winid, Style styl, StyleHint hint, ref uint result);
81 [LibraryImport("garglk")]
82 internal static partial void glk_tick();
83 [DllImport("garglk")]
84 internal static extern void glk_window_clear(WindowHandle winId);
85 [DllImport("garglk")]
86 internal static extern void glk_window_close(WindowHandle winId, ref StreamResult streamResult);
87 [DllImport("garglk")]
88 internal static extern void glk_window_flow_break(WindowHandle winId);
89 [DllImport("garglk")]
90 internal static extern void glk_window_get_size(WindowHandle winId, out uint width, out uint height);
91 [DllImport("garglk")]
92 internal static extern StreamHandle glk_window_get_stream(WindowHandle winId);
93 [DllImport("garglk")]
94 internal static extern void glk_window_move_cursor(WindowHandle winId, uint xpos, uint ypos);
95 [DllImport("garglk")]
96 internal static extern void glk_window_set_echo_stream(WindowHandle winId, StreamHandle stream);
97 [DllImport("garglk")]
98 internal static extern StreamHandle glk_window_get_echo_stream(WindowHandle winId);
99 [DllImport("garglk")]
100 internal static extern WindowHandle glk_window_open(WindowHandle split, WinMethod method, uint size, WinType wintype, uint rock);
101 [LibraryImport("garglk")]
102 internal static partial void garglk_set_zcolors(uint fg, uint bg);
103 [DllImport("garglk", EntryPoint = "garglk_fileref_get_name")]
104 internal static extern IntPtr glkunix_fileref_get_name(FileRefHandle fileref);
105 [DllImport("garglk")]
106 internal static extern uint glk_gestalt(Gestalt sel, uint val);
107 [DllImport("garglk")]
108 internal static extern unsafe uint glk_gestalt_ext(Gestalt sel, uint val, uint* arr, uint arrlen);
109 [LibraryImport("garglk")]
110 internal static partial void glk_request_timer_events(uint millisecs);
111 [LibraryImport("garglk")]
112 internal static partial uint garglk_unput_string_count_uni([MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] uint[] str);
113
114 // Garglk initialization functions.
115 [LibraryImport("garglk")]
116 internal static partial void garglk_set_program_name([MarshalAs(UnmanagedType.LPStr)] string name);
117 [LibraryImport("garglk")]
118 internal static partial void garglk_set_story_name([MarshalAs(UnmanagedType.LPStr)] string name);
119 #if !GarglkStatic
120 [DllImport("libgarglk", EntryPoint = "_Z11gli_startupiPPc")]
121 internal static extern void gli_startup_m_(int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] argv);
122 [DllImport("garglk")]
123 internal static extern void gli_startup(int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] argv);
124 #endif
125 [LibraryImport("garglk")]
126 internal static partial void garglk_startup(int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] argv);
127 }
128
129 class GarGlk: IGlk
130 {
131 public BlorbError giblorb_set_resource_map(StreamHandle fileStream) => Garglk_Pinvoke.giblorb_set_resource_map(fileStream);
132 public void glk_cancel_hyperlink_event(WindowHandle winId) => Garglk_Pinvoke.glk_cancel_hyperlink_event(winId);
133 public void glk_cancel_line_event(WindowHandle winId, ref Event ev) => Garglk_Pinvoke.glk_cancel_line_event(winId, ref ev);
134 public void glk_exit() => Garglk_Pinvoke.glk_exit();
135 public FileRefHandle glk_fileref_create_by_name(FileUsage usage, string name, Glk.FileMode fmode, uint rock) => Garglk_Pinvoke.glk_fileref_create_by_name(usage, name, fmode, rock);
136 public FileRefHandle glk_fileref_create_by_prompt(FileUsage usage, Glk.FileMode fmode, uint rock) => Garglk_Pinvoke.glk_fileref_create_by_prompt(usage, fmode, rock);
137 public FileRefHandle glk_fileref_create_temp(FileUsage usage, uint rock) => Garglk_Pinvoke.glk_fileref_create_temp(usage, rock);
138 public void glk_fileref_destroy(FileRefHandle fref) => Garglk_Pinvoke.glk_fileref_destroy(fref);
139 public uint glk_image_draw(WindowHandle winid, uint imageId, int val1, int val2) => Garglk_Pinvoke.glk_image_draw(winid, imageId, val1, val2);
140 public uint glk_image_get_info(uint imageId, ref uint width, ref uint height) => Garglk_Pinvoke.glk_image_get_info(imageId, ref width, ref height);
141 public void glk_put_buffer(byte[] s, uint len) => Garglk_Pinvoke.glk_put_buffer(s, len);
142 public void glk_put_buffer_stream(StreamHandle streamId, byte[] s, uint len) => Garglk_Pinvoke.glk_put_buffer_stream(streamId, s, len);
143 public void glk_put_buffer_uni(uint[] s, uint len) => Garglk_Pinvoke.glk_put_buffer_uni(s, len);
144 public void glk_request_char_event(WindowHandle winId) => Garglk_Pinvoke.glk_request_char_event(winId);
145 public void glk_request_hyperlink_event(WindowHandle winId) => Garglk_Pinvoke.glk_request_hyperlink_event(winId);
146 public unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen) => Garglk_Pinvoke.glk_request_line_event(win, buf, maxlen, initlen);
147 public unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen) => Garglk_Pinvoke.glk_request_line_event_uni(win, buf, maxlen, initlen);
148 public SoundChannel glk_schannel_create(uint rock) => Garglk_Pinvoke.glk_schannel_create(rock);
149 public void glk_schannel_destroy(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_destroy(chan);
150 public void glk_schannel_pause(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_pause(chan);
151 public uint glk_schannel_play(SoundChannel chan, uint sndId) => Garglk_Pinvoke.glk_schannel_play(chan, sndId);
152 public uint glk_schannel_play_ext(SoundChannel chan, uint sndId, uint repeats, uint notify) => Garglk_Pinvoke.glk_schannel_play_ext(chan, sndId, repeats, notify);
153 public void glk_schannel_set_volume(SoundChannel chan, uint vol) => Garglk_Pinvoke.glk_schannel_set_volume(chan, vol);
154 public void glk_schannel_stop(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_stop(chan);
155 public void glk_schannel_unpause(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_unpause(chan);
156 public void glk_select(ref Event ev) => Garglk_Pinvoke.glk_select(ref ev);
157 public void glk_set_hyperlink(uint linkval) => Garglk_Pinvoke.glk_set_hyperlink(linkval);
158 public void glk_set_style(Style s) => Garglk_Pinvoke.glk_set_style(s);
159 public void glk_set_window(WindowHandle winId) => Garglk_Pinvoke.glk_set_window(winId);
160 public StreamHandle glk_stream_open_file(FileRefHandle fileref, Glk.FileMode fmode, uint rock) => Garglk_Pinvoke.glk_stream_open_file(fileref, fmode, rock);
161 public unsafe StreamHandle glk_stream_open_memory(byte* buf, uint buflen, Glk.FileMode mode, uint rock) => Garglk_Pinvoke.glk_stream_open_memory(buf, buflen, mode, rock);
162 public void glk_stream_close(StreamHandle stream, ref StreamResult streamResult) => Garglk_Pinvoke.glk_stream_close(stream, ref streamResult);
163 public void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode) => Garglk_Pinvoke.glk_stream_set_position(stream, pos, seekMode);
164 public void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val) => Garglk_Pinvoke.glk_stylehint_set(wintype, styl, hint, val);
165 public uint glk_style_measure(WindowHandle winid, Style styl, StyleHint hint, ref uint result) => Garglk_Pinvoke.glk_style_measure(winid, styl, hint, ref result);
166 public void glk_tick() => Garglk_Pinvoke.glk_tick();
167 public void glk_window_clear(WindowHandle winId) => Garglk_Pinvoke.glk_window_clear(winId);
168 public void glk_window_close(WindowHandle winId, ref StreamResult streamResult) => Garglk_Pinvoke.glk_window_close(winId, ref streamResult);
169 public void glk_window_flow_break(WindowHandle winId) => Garglk_Pinvoke.glk_window_flow_break(winId);
170 public void glk_window_get_size(WindowHandle winId, out uint width, out uint height) => Garglk_Pinvoke.glk_window_get_size(winId, out width, out height);
171 public StreamHandle glk_window_get_stream(WindowHandle winId) => Garglk_Pinvoke.glk_window_get_stream(winId);
172 public void glk_window_move_cursor(WindowHandle winId, uint xpos, uint ypos) => Garglk_Pinvoke.glk_window_move_cursor(winId, xpos, ypos);
173 public void glk_window_set_echo_stream(WindowHandle winId, StreamHandle stream) => Garglk_Pinvoke.glk_window_set_echo_stream(winId, stream);
174 public StreamHandle glk_window_get_echo_stream(WindowHandle winId) => Garglk_Pinvoke.glk_window_get_echo_stream(winId);
175 public WindowHandle glk_window_open(WindowHandle split, WinMethod method, uint size, WinType wintype, uint rock) => Garglk_Pinvoke.glk_window_open(split, method, size, wintype, rock);
176 public void garglk_set_zcolors(uint fg, uint bg) => Garglk_Pinvoke.garglk_set_zcolors(fg, bg);
177 public string? glkunix_fileref_get_name(FileRefHandle fileref) => Marshal.PtrToStringAnsi(Garglk_Pinvoke.glkunix_fileref_get_name(fileref));
178 public uint glk_gestalt(Gestalt sel, uint val) => Garglk_Pinvoke.glk_gestalt(sel, val);
179 public unsafe uint glk_gestalt_ext(Gestalt sel, uint val, uint* arr, uint arrlen) => Garglk_Pinvoke.glk_gestalt_ext(sel, val, arr, arrlen);
180 public void glk_request_timer_events(uint millisecs) => Garglk_Pinvoke.glk_request_timer_events(millisecs);
181 public uint garglk_unput_string_count_uni(uint[] str) => Garglk_Pinvoke.garglk_unput_string_count_uni(str);
182
183 public void SetGameName(string game) => Garglk_Pinvoke.garglk_set_story_name(game);
184
185 internal void GarglkInit(string[] argv)
186 {
187 #if GarglkStatic
188 Garglk_Pinvoke.garglk_startup(argv.Length, argv);
189 #else
190 // Depending on the Garglk library version, the startup function could have
191 // a number of names. We need to try them all.
192 try // First attempt.
193 {
194 Garglk_Pinvoke.garglk_startup(argv.Length, argv);
195 return;
196 } catch (EntryPointNotFoundException) { } // do nothing
197
198 try // Second attempt.
199 {
200 Garglk_Pinvoke.gli_startup_m_(argv.Length, argv);
201 return;
202 } catch (EntryPointNotFoundException) { } // do nothing
203
204 // Last attempt.
205 // This must succeed, or the Garglk library would go uninitialized,
206 // so we let the program crash and burn if this one fails as well.
207 Garglk_Pinvoke.gli_startup(argv.Length, argv);
208 #endif
209 }
210 }
211
212 public class GarGlkRunner
213 {
214 [STAThread]
215 public static int Main(string[] args)
216 {
217 if (args.Length == 0)
218 {
219 Console.WriteLine("Error: No file selected!");
220 return 1;
221 }
222
223 GarGlk GlkApi = new GarGlk();
224 Startup(args, GlkApi);
225
226 var sess = new MainSession(args[^1], GlkApi);
227 sess.Run();
228
229 return 0;
230 }
231
232 private static void Startup(string[] args, GarGlk api)
233 {
234 string[] argv = new string[args.Length + 1];
235 //argv[0] = Assembly.GetEntryAssembly().Location;
236 // seems legit -- this is really only needed so garglk can figure out the config overrides
237 argv[0] = "FrankenDrift.GlkRunner.Gargoyle";
238 for (int i = 1; i <= args.Length; i++)
239 {
240 argv[i] = args[i - 1];
241 }
242 api.GarglkInit(argv);
243 Garglk_Pinvoke.garglk_set_program_name("FrankenDrift for Gargoyle");
244 }
245 }
246 }