aboutsummaryrefslogtreecommitdiff
path: root/files.c
blob: 3ab157ed9ef9c686aee456a549aa148dd77acdce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 *
 * Copyright (C) 2022 Jeremias Stotter <jeremias@stotter.eu>
 *
 * This file is part of network-simulator.
 *
 * network-simulator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * network-simulator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with network-simulator. If not, see <https://www.gnu.org/licenses/>. 
*/

#include "files.h"

#include <stdio.h>
#include <stdlib.h>

#include <json.h>
#include <lua.h>

#include "shared.h"
#include "device.h"
#include "interface.h"
#include "connection.h"
#include "decorations.h"

#include "lua_functions.h"

int save_file(char* filename) {
	if(!filename) {
		fprintf(stderr, "filename is NULL!\n");
		return -1;
	}
	/*
	  Saving is going to work like this:
	  For each device do the following:
	  1. Save properties about the device iteself (Template, Position, ...)
	  2. Save the interfaces the device has connected
	  3. Save the connections of each interface in a seperate json array
	  4. Call a function defined in the template of the device which is responsible for saving anything device specific and save that

	  Then save the decorations texts and posistions
	*/
	json_object* root_object = json_object_new_object();
	json_object* device_array = json_object_new_array();
	json_object* connection_array = json_object_new_array();
	LL_FOR(struct device, start_device, cur_device) {
		// Save device properties
		json_object* device_object = json_object_new_object();

		json_object* dev_name = json_object_new_string(cur_device->name);
		json_object_object_add(device_object, "NAME", dev_name);

		json_object* template_name = json_object_new_string(cur_device->template->name);
		json_object_object_add(device_object, "TEMPLATE", template_name);

		json_object* dev_X = json_object_new_double(cur_device->X);
		json_object* dev_Y = json_object_new_double(cur_device->Y);
		json_object_object_add(device_object, "X", dev_X);
		json_object_object_add(device_object, "Y", dev_Y);

		// Save the interfaces
		json_object* interface_array = json_object_new_array_ext(cur_device->template->slots);
		for(int i = 0; i < cur_device->template->slots; i++) {
			if(cur_device->interfaces[i].template) {
				json_object* interface_template_name = json_object_new_string(cur_device->interfaces[i].template->name);
				json_object_array_add(interface_array, interface_template_name);
				// Check for connection
				if(cur_device->interfaces[i].connection &&
				   &(cur_device->interfaces[i]) == cur_device->interfaces[i].connection->interface1 /* Make sure we only save the connection on one side so it only gets saved once */) {
					json_object* connection_object = json_object_new_object();

					json_object* dev1_name = json_object_new_string(cur_device->name);
					json_object* dev1_index = json_object_new_int(i);
					json_object* dev2_name = json_object_new_string(cur_device->interfaces[i].connection->interface2->parent_device->name);
					json_object* dev2_index = json_object_new_int(cur_device->interfaces[i].connection->interface2->parent_index);
					json_object* con_template = json_object_new_string(cur_device->interfaces[i].connection->template->name);
					json_object_object_add(connection_object, "DEV1", dev1_name);
					json_object_object_add(connection_object, "INT1", dev1_index);
					json_object_object_add(connection_object, "DEV2", dev2_name);
					json_object_object_add(connection_object, "INT2", dev2_index);
					json_object_object_add(connection_object, "TEMPLATE", con_template);

					json_object_array_add(connection_array, connection_object);
				}
			} else { // There is no interface in this slot
				json_object_array_add(interface_array, json_object_new_null());
			}
		}
		json_object_object_add(device_object, "SLOTS", interface_array);

		// Save any device specific data
		if(my_lua_call(cur_device->lua_vm, "save_state", 1, -1)) {
			// Get the result
			const char* state = lua_tostring(cur_device->lua_vm, -1);
			if(state) {
				json_object_object_add(device_object, "STATE", json_object_new_string(state));
			}
		}
		
		json_object_array_add(device_array, device_object);
	}

	json_object_object_add(root_object, "DEV", device_array);
	json_object_object_add(root_object, "CON", connection_array);

	json_object* deco_array = json_object_new_array();
	LL_FOR(struct deco, start_deco, cur_deco) {
		json_object* deco_object = json_object_new_object();

		json_object* deco_string = json_object_new_string(cur_deco->text);
		json_object_object_add(deco_object, "TEXT", deco_string);

		json_object* deco_type = NULL;
		switch(cur_deco->type) {
			case deco_type_text:
				deco_type = json_object_new_string("TEXT");
				break;
			case deco_type_rect:
				deco_type = json_object_new_string("RECT");
				break;
			case deco_type_circle:
				deco_type = json_object_new_string("CIRCLE");
				break;
		}
		json_object_object_add(deco_object, "TYPE", deco_type);
		
		json_object* deco_X = json_object_new_double(cur_deco->X);
		json_object* deco_Y = json_object_new_double(cur_deco->Y);
		json_object_object_add(deco_object, "X", deco_X);
		json_object_object_add(deco_object, "Y", deco_Y);

		json_object* deco_width = json_object_new_double(cur_deco->width);
		json_object* deco_height = json_object_new_double(cur_deco->height);
		json_object_object_add(deco_object, "WIDTH", deco_width);
		json_object_object_add(deco_object, "HEIGHT", deco_height);

		json_object_array_add(deco_array, deco_object);
	}

	json_object_object_add(root_object, "DECO", deco_array);
	
	int result = json_object_to_file_ext(filename, root_object, JSON_C_TO_STRING_PRETTY);
	json_object_put(root_object);
	return result;
}

int load_file(char* filename) {
	if(!filename) {
		fprintf(stderr, "filename is NULL!\n");
		return -1;
	}
	/*
	  Loading is going to work like this:
	  Go through the saved devices in the file and do the following for each:
	  1. Create the new device with the right template and properties
	  2. Create the interfaces of the device
	  3. Call a specific load function and pass the data the device emmited when saving

	  After that create the connections saved in the seperate table
	*/
	json_object* root_object = json_object_from_file(filename);
	if(!root_object) {
		fprintf(stderr, "Couldn't load savefile: %s\n", json_util_get_last_err());
		return -1;
	}

	json_object *device_array, *connection_array, *deco_array;

	if(!json_object_object_get_ex(root_object, "DEV", &device_array)) {
		fprintf(stderr, "Couldn't find DEV object in savefile\n");
		return -1;
	}
	if(!json_object_object_get_ex(root_object, "CON", &connection_array)) {
		fprintf(stderr, "Couldn't find CON object in savefile\n");
		return -1;
	}

	if(!json_object_is_type(device_array, json_type_array) || !json_object_is_type(connection_array, json_type_array)) {
		fprintf(stderr, "Etiher DEV or CON are not an array in savefile\n");
		return -1;
	}

	size_t dev_n = json_object_array_length(device_array);
	for(int i = 0; i < dev_n; i++) {
		json_object* device_object = json_object_array_get_idx(device_array, i);
		json_object *device_name, *device_template;
		if(!json_object_object_get_ex(device_object, "NAME", &device_name)) {
			fprintf(stderr, "Couldn't get device NAME of index %d in savefile\n", i);
			continue;
		}
		if(!json_object_object_get_ex(device_object, "TEMPLATE", &device_template)) {
			fprintf(stderr, "Couldn't get device TEMPLATE of index %d in savefile\n", i);
			continue;
		}
		struct device* load_dev = new_device_from_template_name(&start_device,
											 (char*)json_object_get_string(device_name),
											 (char*)json_object_get_string(device_template),
											 false);
		if(!load_dev) {
			fprintf(stderr, "Couldn't create device (index %d ) from savefile\n", i);
			continue;
		}

		// Restore device coordinates
		json_object *X, *Y;
		if(!json_object_object_get_ex(device_object, "X", &X) ||
		   !json_object_object_get_ex(device_object, "Y", &Y)) {
			fprintf(stderr, "Couldn't find X or Y or both in device (index %d) from savefile, placing at 0, 0\n", i);
		} else {
			load_dev->X = json_object_get_double(X);
			load_dev->Y = json_object_get_double(Y);
		}

		// Restore interfaces
		json_object* slots;
		if(json_object_object_get_ex(device_object, "SLOTS", &slots) && json_object_is_type(slots, json_type_array)) {
			size_t slots_n = json_object_array_length(slots);
			for(int j = 0; j < slots_n && j < load_dev->template->slots; j++) {
				json_object* int_name_json = json_object_array_get_idx(slots, j);
				const char* int_name = json_object_get_string(int_name_json);
				if(!int_name) continue;
				new_interface_from_template_name(load_dev, j, int_name);
			}
		}
		
		// Restore device lua state
		json_object* state;
		if(json_object_object_get_ex(device_object, "STATE", &state)) {
			const char* state_str = json_object_get_string(state);
			if(my_lua_call(load_dev->lua_vm, "load_state", 1, LUA_TSTRING, strlen(state_str), state_str, -1)) {
				int result = lua_tointeger(load_dev->lua_vm, -1);
				if(result != 0) {
					fprintf(stderr, "Device (index %d) state loading failed\n", i);
				}
			}
		}
	}

	// Restore connections
	size_t con_n = json_object_array_length(connection_array);
	for(int i = 0; i < con_n; i++) {
		json_object* con_object = json_object_array_get_idx(connection_array, i);
		json_object *dev1_name, *index1, *dev2_name, *index2, *template;

		if(!json_object_object_get_ex(con_object, "DEV1", &dev1_name) ||
		   !json_object_object_get_ex(con_object, "INT1", &index1) ||
		   !json_object_object_get_ex(con_object, "DEV2", &dev2_name) ||
		   !json_object_object_get_ex(con_object, "INT2", &index2) ||
		   !json_object_object_get_ex(con_object, "TEMPLATE", &template)) {
			fprintf(stderr, "Error restoring connection (index %d) in savefile, a required field is missing\n", i);
			continue;
		}

		struct device* dev1 = dev_from_name(start_device, (char*)json_object_get_string(dev1_name));
		struct device* dev2 = dev_from_name(start_device, (char*)json_object_get_string(dev2_name));
		if(!dev1 || !dev2) {
			fprintf(stderr, "Error restroing connection (index %d) in savefile, couldn't find one of the required devices\n", i);
			continue;
		}
		
		new_connection_from_template_name(&(dev1->interfaces[json_object_get_int(index1)]),
					   &(dev2->interfaces[json_object_get_int(index2)]),
					   (char*)json_object_get_string(template)); 
	}

	if(!json_object_object_get_ex(root_object, "DECO", &deco_array)) {
		fprintf(stderr, "Couldn't find DECO object in savefile\n");
		return -1;
	}
	if(!json_object_is_type(deco_array, json_type_array)) {
		fprintf(stderr, "DECO is not an array in savefile\n");
		return -1;
	}
	// Restore decorations
	size_t deco_n = json_object_array_length(deco_array);
	for(int i = 0; i < deco_n; i++) {
		json_object* deco_object = json_object_array_get_idx(deco_array, i);
		json_object *deco_string = NULL,
			*deco_X = NULL,
			*deco_Y = NULL,
			*deco_height = NULL,
			*deco_width = NULL,
			*deco_type = NULL;
		
		if(!json_object_object_get_ex(deco_object, "TEXT", &deco_string) ||
		   !json_object_object_get_ex(deco_object, "X", &deco_X) ||
		   !json_object_object_get_ex(deco_object, "Y", &deco_Y) ||
		   !json_object_object_get_ex(deco_object, "HEIGHT", &deco_height) ||
		   !json_object_object_get_ex(deco_object, "WIDTH", &deco_width) ||
		   !json_object_object_get_ex(deco_object, "TYPE", &deco_type)) {
			fprintf(stderr, "Error restoring decorations (index %d) in savefile, a required field is missing\n", i);
			continue;
		}

		int deco_type_num = deco_type_text;
		const char* deco_type_string = json_object_get_string(deco_type);
		if(strcmp(deco_type_string, "TEXT") == 0) {
			deco_type_num = deco_type_text;
		} else if(strcmp(deco_type_string, "RECT") == 0) {
			deco_type_num = deco_type_rect;
		} else if(strcmp(deco_type_string, "CIRCLE") == 0) {
			deco_type_num = deco_type_circle;
		} else {
			fprintf(stderr, "Invalid type %s for decoration index %d, using text!",
					deco_type_string, i);
		}
		
		struct deco* new_deco = deco_new(deco_type_num);
		new_deco->X = json_object_get_double(deco_X);
		new_deco->Y = json_object_get_double(deco_Y);
		new_deco->width = json_object_get_double(deco_width);
		new_deco->height = json_object_get_double(deco_height);
		deco_text_set(new_deco, json_object_get_string(deco_string));
	}
	
	return 0;
}
Jeremias Stotters git repositories generated by CGIT