1 fcb97c9f 2020-04-04 op #include "adventure.h"
3 fcb97c9f 2020-04-04 op #include <stdint.h>
4 fcb97c9f 2020-04-04 op #include <stdio.h>
5 fcb97c9f 2020-04-04 op #include <stdlib.h>
6 fcb97c9f 2020-04-04 op #include <string.h>
8 fcb97c9f 2020-04-04 op #include <readline/readline.h>
10 fcb97c9f 2020-04-04 op /* the last 32 bits are a version number to be increased on breaking changes.
12 fcb97c9f 2020-04-04 op static const char magic[]
13 fcb97c9f 2020-04-04 op = { 0, 0, 0, 0, 'T', 'E', 'X', 'T', 'A', 'D', 'V', 0, 0, 0, 0 };
15 fcb97c9f 2020-04-04 op #define MLEN sizeof(magic)
17 fcb97c9f 2020-04-04 op /* Every object is serialized in a buffer with the following format:
19 fcb97c9f 2020-04-04 op * | offset | location | weight | capacity | health |
21 fcb97c9f 2020-04-04 op * with each field 32 bits long, for a total of 160 bytes. Each field is a
22 fcb97c9f 2020-04-04 op * SIGNED 32 bit integer. The meaning of the fields is as follows:
24 fcb97c9f 2020-04-04 op * - offset : the current object, that is objs[offset]
25 fcb97c9f 2020-04-04 op * - location : the offset of the location, if NULL -1
26 fcb97c9f 2020-04-04 op * - weight, capacity, health : the propriety of the object.
28 fcb97c9f 2020-04-04 op * This should make cheating a little bit harder.
36 fcb97c9f 2020-04-04 op struct object *obj;
38 fcb97c9f 2020-04-04 op line = readline("Save to file: ");
39 fcb97c9f 2020-04-04 op if (!line || !*line) {
40 fcb97c9f 2020-04-04 op printf("Invalid filename\n");
42 fcb97c9f 2020-04-04 op if (line != NULL)
45 fcb97c9f 2020-04-04 op return save();
48 fcb97c9f 2020-04-04 op /* b = binary mode, for windows compatibility I guess */
49 fcb97c9f 2020-04-04 op if ((f = fopen(line, "wb")) == NULL) {
50 fcb97c9f 2020-04-04 op printf("Cannot write file!\n");
54 fcb97c9f 2020-04-04 op /* write magic header */
55 fcb97c9f 2020-04-04 op fwrite(magic, 1, MLEN, f);
57 fcb97c9f 2020-04-04 op /* write the objects */
58 fcb97c9f 2020-04-04 op for (obj = objs; obj < obj_end; ++obj) {
59 fcb97c9f 2020-04-04 op int32_t offset, location, weight, capacity, health;
61 fcb97c9f 2020-04-04 op offset = obj - objs;
62 fcb97c9f 2020-04-04 op if (obj->location == NULL)
63 fcb97c9f 2020-04-04 op location = -1;
65 fcb97c9f 2020-04-04 op location = obj->location - objs;
66 fcb97c9f 2020-04-04 op weight = obj->weight;
67 fcb97c9f 2020-04-04 op capacity = obj->capacity;
68 fcb97c9f 2020-04-04 op health = obj->health;
70 fcb97c9f 2020-04-04 op printf("saved: %d %d %d %d %d\n", offset, location, weight,
71 fcb97c9f 2020-04-04 op capacity, health);
73 fcb97c9f 2020-04-04 op fwrite(&offset, 4, 1, f);
74 fcb97c9f 2020-04-04 op fwrite(&location, 4, 1, f);
75 fcb97c9f 2020-04-04 op fwrite(&weight, 4, 1, f);
76 fcb97c9f 2020-04-04 op fwrite(&capacity, 4, 1, f);
77 fcb97c9f 2020-04-04 op fwrite(&health, 4, 1, f);
88 fcb97c9f 2020-04-04 op char *line, m[MLEN];
92 fcb97c9f 2020-04-04 op line = readline("Load from file: ");
93 fcb97c9f 2020-04-04 op if (!line || !*line) {
94 fcb97c9f 2020-04-04 op printf("Invalid filename\n");
96 fcb97c9f 2020-04-04 op if (line != NULL)
99 fcb97c9f 2020-04-04 op return load();
102 fcb97c9f 2020-04-04 op if ((f = fopen(line, "rb")) == NULL) {
103 fcb97c9f 2020-04-04 op printf("Cannot open file %s\n", line);
108 fcb97c9f 2020-04-04 op /* read the magic */
109 fcb97c9f 2020-04-04 op fread(m, MLEN, 1, f);
110 fcb97c9f 2020-04-04 op if (memcmp(m, magic, MLEN)) {
111 fcb97c9f 2020-04-04 op printf("Wrong magic in file, cannot load.\n");
115 fcb97c9f 2020-04-04 op /* read all the objects */
117 fcb97c9f 2020-04-04 op int32_t offset, location, weight, capacity, health;
119 fcb97c9f 2020-04-04 op if ((r = fread(&offset, 1, 4, f)) != 4)
121 fcb97c9f 2020-04-04 op if ((r = fread(&location, 1, 4, f)) != 4)
123 fcb97c9f 2020-04-04 op if ((r = fread(&weight, 1, 4, f)) != 4)
125 fcb97c9f 2020-04-04 op if ((r = fread(&capacity, 1, 4, f)) != 4)
127 fcb97c9f 2020-04-04 op if ((r = fread(&health, 1, 4, f)) != 4)
130 fcb97c9f 2020-04-04 op if (objs + offset >= obj_end)
133 fcb97c9f 2020-04-04 op if (location == -1)
134 fcb97c9f 2020-04-04 op objs[offset].location = NULL;
136 fcb97c9f 2020-04-04 op if (objs + location >= obj_end)
139 fcb97c9f 2020-04-04 op objs[offset].location = objs + location;
142 fcb97c9f 2020-04-04 op objs[offset].weight = weight;
143 fcb97c9f 2020-04-04 op objs[offset].capacity = capacity;
144 fcb97c9f 2020-04-04 op objs[offset].health = health;
147 fcb97c9f 2020-04-04 op if (ferror(f)) {
148 fcb97c9f 2020-04-04 op perror(line);
152 fcb97c9f 2020-04-04 op printf("OK!\n");
158 fcb97c9f 2020-04-04 op printf("Error in loading file!\n");