Debian Codegraph @

terps/jacl/glk_saver.c

raw symbols SCIP
1 /* glk_saver.c --- Functions to save and load the game state to disk
2 * using the GLK API.
3 (C) 2008 Stuart Allen, distribute and use
4 * according to GNU GPL, see file COPYING for details.
5 */
6
7 #include "jacl.h"
8 #include "language.h"
9 #include "types.h"
10 #include "prototypes.h"
11
12 static void write_integer(strid_t stream, int x);
13 static void write_long(strid_t stream, long x);
14 static int read_integer(strid_t stream);
15 static long read_long(strid_t stream);
16
17 int
18 save_game(frefid_t saveref)
19 {
20 struct integer_type *current_integer = integer_table;
21 struct function_type *current_function = function_table;
22 struct string_type *current_string = string_table;
23
24 int index, counter;
25 strid_t bookmark = NULL;
26
27 bookmark = glk_stream_open_file(saveref, filemode_Write, 0);
28
29 if (bookmark == NULL) {
30 return (FALSE);
31 }
32
33 /* WE'RE DONE WITH THE FILE REFERENCE NOW THAT THE STREAM
34 * HAS BEEN SUCCESSFULLY OPENED */
35 glk_fileref_destroy (saveref);
36
37 /* THIS IS WRITTEN TO HELP VALIDATE THE SAVED GAME
38 * BEFORE CONTINUING TO LOAD IT */
39 write_integer (bookmark, objects);
40 write_integer (bookmark, integers);
41 write_integer (bookmark, functions);
42 write_integer (bookmark, strings);
43
44 while (current_integer != NULL) {
45 write_integer (bookmark, current_integer->value);
46 current_integer = current_integer->next_integer;
47 }
48
49 while (current_function != NULL) {
50 write_integer (bookmark, current_function->call_count);
51 current_function = current_function->next_function;
52 }
53
54 for (index = 1; index <= objects; index++) {
55 if (object[index]->nosave)
56 continue;
57
58 for (counter = 0; counter < 16; counter++) {
59 write_integer (bookmark, object[index]->integer[counter]);
60 }
61
62 write_long (bookmark, object[index]->attributes);
63 write_long (bookmark, object[index]->user_attributes);
64 }
65
66 /* WRITE OUT ALL THE CURRENT VALUES OF THE STRING VARIABLES */
67 while (current_string != NULL) {
68 for (index = 0; index < 1024; index++) {
69 glk_put_char_stream(bookmark, current_string->value[index]);
70 }
71 current_string = current_string->next_string;
72 }
73
74 write_integer (bookmark, player);
75 write_integer (bookmark, noun[3]);
76
77 /* SAVE THE CURRENT VOLUME OF EACH OF THE SOUND CHANNELS */
78 for (index = 0; index < 8; index++) {
79 sprintf(temp_buffer, "volume[%d]", index);
80 write_integer (bookmark, cinteger_resolve(temp_buffer)->value);
81 }
82
83 /* SAVE THE CURRENT VALUE OF THE GLK TIMER */
84 write_integer (bookmark, cinteger_resolve("event_timer")->value);
85
86 /* CLOSE THE STREAM */
87 glk_stream_close(bookmark, NULL);
88
89 TIME->value = FALSE;
90 return (TRUE);
91 }
92
93 int
94 restore_game(frefid_t saveref, int warn)
95 {
96 struct integer_type *current_integer = integer_table;
97 struct function_type *current_function = function_table;
98 struct string_type *current_string = string_table;
99
100 int index, counter;
101 int file_objects,
102 file_integers,
103 file_functions,
104 file_strings;
105 strid_t bookmark;
106
107 bookmark = glk_stream_open_file(saveref, filemode_Read, 0);
108
109 if (!bookmark) {
110 return (FALSE);
111 }
112
113 /* WE'RE DONE WITH THE FILE REFERENCE NOW THAT THE STREAM
114 * HAS BEEN SUCCESSFULLY OPENED */
115 glk_fileref_destroy (saveref);
116
117 /* THIS IS WRITTEN TO HELP VALIDATE THE SAVED GAME
118 * BEFORE CONTINUING TO LOAD IT */
119 file_objects = read_integer(bookmark);
120 file_integers = read_integer(bookmark);
121 file_functions = read_integer(bookmark);
122 file_strings = read_integer(bookmark);
123
124 if (file_objects != objects
125 || file_integers != integers
126 || file_functions != functions
127 || file_strings != strings) {
128 if (warn == FALSE) {
129 log_error(cstring_resolve("BAD_SAVED_GAME")->value, PLUS_STDOUT);
130 }
131 glk_stream_close(bookmark, NULL);
132 return (FALSE);
133 }
134
135 while (current_integer != NULL) {
136 current_integer->value = read_integer (bookmark);
137 current_integer = current_integer->next_integer;
138 }
139
140 while (current_function != NULL) {
141 current_function->call_count = read_integer (bookmark);
142 current_function = current_function->next_function;
143 }
144
145 for (index = 1; index <= objects; index++) {
146 if (object[index]->nosave)
147 continue;
148
149 for (counter = 0; counter < 16; counter++) {
150 object[index]->integer[counter] = read_integer(bookmark);
151 }
152
153 object[index]->attributes = read_integer(bookmark);
154 object[index]->user_attributes = read_integer(bookmark);
155 }
156
157 while (current_string != NULL) {
158 for (index = 0; index < 1024; index++) {
159 current_string->value[index] = glk_get_char_stream(bookmark);
160 }
161 current_string = current_string->next_string;
162 }
163
164 player = read_integer(bookmark);
165 noun[3] = read_integer(bookmark);
166
167 /* RESTORE THE CURRENT VOLUME OF EACH OF THE SOUND CHANNELS */
168 for (index = 0; index < 8; index++) {
169 sprintf(temp_buffer, "volume[%d]", index);
170 counter = read_integer(bookmark);
171 cinteger_resolve(temp_buffer)->value = counter;
172
173 if (SOUND_SUPPORTED->value) {
174 /* SET THE GLK VOLUME */
175 glk_schannel_set_volume(sound_channel[index], (glui32) counter);
176 }
177 }
178
179 /* RESTORE THE CURRENT VALUE OF THE GLK TIMER */
180 counter = read_integer(bookmark);
181 cinteger_resolve("event_timer")->value = counter;
182
183 /* SET THE GLK TIMER */
184 glk_request_timer_events((glui32) counter);
185
186 /* CLOSE THE STREAM */
187 glk_stream_close(bookmark, NULL);
188
189 TIME->value = FALSE;
190 return (TRUE);
191 }
192
193 void
194 write_integer(strid_t stream, int x)
195 {
196 unsigned char c;
197
198 c = (unsigned char) (x) & 0xFF;
199 glk_put_char_stream(stream, c);
200 c = (unsigned char) (x >> 8) & 0xFF;
201 glk_put_char_stream(stream, c);
202 c = (unsigned char) (x >> 16) & 0xFF;
203 glk_put_char_stream(stream, c);
204 c = (unsigned char) (x >> 24) & 0xFF;
205 glk_put_char_stream(stream, c);
206 }
207
208 int
209 read_integer(strid_t stream)
210 {
211 int a, b, c, d;
212 a = (int) glk_get_char_stream(stream);
213 b = (int) glk_get_char_stream(stream);
214 c = (int) glk_get_char_stream(stream);
215 d = (int) glk_get_char_stream(stream);
216 return a | (b << 8) | (c << 16) | (d << 24);
217 }
218
219 void
220 write_long(strid_t stream, long x)
221 {
222 unsigned char c;
223
224 c = (unsigned char) (x) & 0xFF;
225 glk_put_char_stream(stream, c);
226 c = (unsigned char) (x >> 8) & 0xFF;
227 glk_put_char_stream(stream, c);
228 c = (unsigned char) (x >> 16) & 0xFF;
229 glk_put_char_stream(stream, c);
230 c = (unsigned char) (x >> 24) & 0xFF;
231 glk_put_char_stream(stream, c);
232 }
233
234 long
235 read_long(strid_t stream)
236 {
237 long a, b, c, d;
238 a = (long) glk_get_char_stream(stream);
239 b = (long) glk_get_char_stream(stream);
240 c = (long) glk_get_char_stream(stream);
241 d = (long) glk_get_char_stream(stream);
242 return a | (b << 8) | (c << 16) | (d << 24);
243 }