summaryrefslogtreecommitdiff
path: root/src/map/mapreg_sql.c
blob: 16d8b0ff6e05f1b3d5f9fda14e3470d15f3dccd6 (plain) (blame)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2012-2020 Hercules Dev Team
 * Copyright (C) Athena Dev Teams
 *
 * Hercules 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#define HERCULES_CORE

#include "mapreg.h"

#include "map/map.h"
#include "map/script.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/db.h"
#include "common/ers.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/showmsg.h"
#include "common/sql.h"
#include "common/strlib.h"
#include "common/timer.h"

#include <stdlib.h>
#include <string.h>

static struct mapreg_interface mapreg_s; //!< Private interface structure.
struct mapreg_interface *mapreg; //!< Public interface structure.

/**
 * Looks up the value of a global integer variable using its unique ID.
 *
 * @param uid The variable's unique ID.
 * @return The variable's value or 0 if the variable does not exist.
 *
 **/
static int mapreg_get_num_reg(int64 uid)
{
	struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid);
	return (var != NULL) ? var->u.i : 0;
}

/**
 * Looks up the value of a global string variable using its unique ID.
 *
 * @param uid The variable's unique ID.
 * @return The variable's value or NULL if the variable does not exist.
 *
 **/
static char *mapreg_get_str_reg(int64 uid)
{
	struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid);
	return (var != NULL) ? var->u.str : NULL;
}

/**
 * Sets the value of a global integer variable.
 *
 * @param uid The variable's unique ID.
 * @param name The variable's name.
 * @param index The variable's array index.
 * @param value The variable's new value.
 * @return True on success, otherwise false.
 *
 **/
static bool mapreg_set_num_db(int64 uid, const char *name, unsigned int index, int value)
{
	nullpo_retr(false, name);
	Assert_retr(false, *name != '\0');
	Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH);

	if (value == 0)
		return mapreg->delete_num_db(uid, name, index);

	struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid);

	// Update variable.
	if (var != NULL) {
		var->u.i = value;

		if (script->is_permanent_variable(name)) {
			var->save = true;
			mapreg->dirty = true;
		}

		return true;
	}

	// Add new variable.
	if (index != 0)
		script->array_update(&mapreg->regs, uid, false);

	var = ers_alloc(mapreg->ers, struct mapreg_save);
	var->u.i = value;
	var->uid = uid;
	var->save = false;
	var->is_string = false;
	i64db_put(mapreg->regs.vars, uid, var);

	if (script->is_permanent_variable(name) && !mapreg->skip_insert) {
		struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

		if (stmt == NULL) {
			SqlStmt_ShowDebug(stmt);
			return false;
		}

		const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)";

		if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db)
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name))
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index))
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &value, sizeof(value))
		    || SQL_ERROR == SQL->StmtExecute(stmt)) {
			SqlStmt_ShowDebug(stmt);
			SQL->StmtFree(stmt);
			return false;
		}

		SQL->StmtFree(stmt);
	}

	return true;
}

/**
 * Deletes a global integer variable.
 *
 * @param uid The variable's unique ID.
 * @param name The variable's name.
 * @param index The variable's array index.
 * @return True on success, otherwise false.
 *
 **/
static bool mapreg_delete_num_db(int64 uid, const char *name, unsigned int index)
{
	nullpo_retr(false, name);
	Assert_retr(false, *name != '\0');
	Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH);

	struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid);

	if (var != NULL)
		ers_free(mapreg->ers, var);

	if (index != 0)
		script->array_update(&mapreg->regs, uid, true);

	i64db_remove(mapreg->regs.vars, uid);

	if (script->is_permanent_variable(name)) {
		struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

		if (stmt == NULL) {
			SqlStmt_ShowDebug(stmt);
			return false;
		}

		const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?";

		if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db)
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name))
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index))
		    || SQL_ERROR == SQL->StmtExecute(stmt)) {
			SqlStmt_ShowDebug(stmt);
			SQL->StmtFree(stmt);
			return false;
		}

		SQL->StmtFree(stmt);
	}

	return true;
}

/**
 * Sets the value of a global integer variable or deletes it if passed value is 0.
 *
 * @param uid The variable's unique ID.
 * @param val The variable's new value.
 * @return True on success, otherwise false.
 *
 **/
static bool mapreg_set_num(int64 uid, int val)
{
	unsigned int index = script_getvaridx(uid);
	const char *name = script->get_str(script_getvarid(uid));

	if (val != 0)
		return mapreg->set_num_db(uid, name, index, val);
	else
		return mapreg->delete_num_db(uid, name, index);
}

/**
 * Sets the value of a global string variable.
 *
 * @param uid The variable's unique ID.
 * @param name The variable's name.
 * @param index The variable's array index.
 * @param value The variable's new value.
 * @return True on success, otherwise false.
 *
 **/
static bool mapreg_set_str_db(int64 uid, const char *name, unsigned int index, const char *value)
{
	nullpo_retr(false, name);
	Assert_retr(false, *name != '\0');
	Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH);

	if (value == NULL || *value == '\0')
		return mapreg->delete_str_db(uid, name, index);

	if (script->is_permanent_variable(name))
		Assert_retr(false, strlen(value) <= SCRIPT_STRING_VAR_LENGTH);

	struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid);

	// Update variable.
	if (var != NULL) {
		if (var->u.str != NULL)
			aFree(var->u.str);

		var->u.str = aStrdup(value);

		if (script->is_permanent_variable(name)) {
			var->save = true;
			mapreg->dirty = true;
		}

		return true;
	}

	// Add new variable.
	if (index != 0)
		script->array_update(&mapreg->regs, uid, false);

	var = ers_alloc(mapreg->ers, struct mapreg_save);
	var->u.str = aStrdup(value);
	var->uid = uid;
	var->save = false;
	var->is_string = true;
	i64db_put(mapreg->regs.vars, uid, var);

	if (script->is_permanent_variable(name) && !mapreg->skip_insert) {
		struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

		if (stmt == NULL) {
			SqlStmt_ShowDebug(stmt);
			return false;
		}

		const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)";

		if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db)
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name))
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index))
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_STRING, value, strlen(value))
		    || SQL_ERROR == SQL->StmtExecute(stmt)) {
			SqlStmt_ShowDebug(stmt);
			SQL->StmtFree(stmt);
			return false;
		}

		SQL->StmtFree(stmt);
	}

	return true;
}

/**
 * Deletes a global string variable.
 *
 * @param uid The variable's unique ID.
 * @param name The variable's name.
 * @param index The variable's array index.
 * @return True on success, otherwise false.
 *
 **/
static bool mapreg_delete_str_db(int64 uid, const char *name, unsigned int index)
{
	nullpo_retr(false, name);
	Assert_retr(false, *name != '\0');
	Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH);

	struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid);

	if (var != NULL) {
		if (var->u.str != NULL)
			aFree(var->u.str);

		ers_free(mapreg->ers, var);
	}

	if (index != 0)
		script->array_update(&mapreg->regs, uid, true);

	i64db_remove(mapreg->regs.vars, uid);

	if (script->is_permanent_variable(name)) {
		struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

		if (stmt == NULL) {
			SqlStmt_ShowDebug(stmt);
			return false;
		}

		const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?";

		if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db)
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name))
		    || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index))
		    || SQL_ERROR == SQL->StmtExecute(stmt)) {
			SqlStmt_ShowDebug(stmt);
			SQL->StmtFree(stmt);
			return false;
		}

		SQL->StmtFree(stmt);
	}

	return true;
}

/**
 * Sets the value of a global string variable or deletes it if passed value is NULL or an empty string.
 *
 * @param uid The variable's unique ID.
 * @param str The variable's new value.
 * @return True on success, otherwise false.
 *
 **/
static bool mapreg_set_str(int64 uid, const char *str)
{
	unsigned int index = script_getvaridx(uid);
	const char *name = script->get_str(script_getvarid(uid));

	if (str != NULL && *str != '\0')
		return mapreg->set_str_db(uid, name, index, str);
	else
		return mapreg->delete_str_db(uid, name, index);
}

/**
 * Loads permanent global interger variables from the database.
 *
 **/
static void mapreg_load_num_db(void)
{
	struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

	if (stmt == NULL) {
		SqlStmt_ShowDebug(stmt);
		return;
	}

	const char *query = "SELECT `key`, `index`, `value` FROM `%s`";
	char name[SCRIPT_VARNAME_LENGTH + 1];
	unsigned int index;
	int value;

	if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db)
	    || SQL_ERROR == SQL->StmtExecute(stmt)
	    || SQL_ERROR == SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name, sizeof(name), NULL, NULL)
	    || SQL_ERROR == SQL->StmtBindColumn(stmt, 1, SQLDT_UINT32, &index, sizeof(index), NULL, NULL)
	    || SQL_ERROR == SQL->StmtBindColumn(stmt, 2, SQLDT_INT32, &value, sizeof(value), NULL, NULL)) {
		SqlStmt_ShowDebug(stmt);
		SQL->StmtFree(stmt);
		return;
	}

	if (SQL->StmtNumRows(stmt) < 1) {
		SQL->StmtFree(stmt);
		return;
	}

	mapreg->skip_insert = true;

	while (SQL_SUCCESS == SQL->StmtNextRow(stmt)) {
		int var_key = script->add_variable(name);
		int64 uid = reference_uid(var_key, index);

		if (i64db_exists(mapreg->regs.vars, uid)) {
			ShowWarning("mapreg_load_num_db: Duplicate! '%s' => '%d' Skipping...\n", name, value);
			continue;
		}

		mapreg->setreg(uid, value);
	}

	mapreg->skip_insert = false;
	SQL->StmtFree(stmt);
}

/**
 * Loads permanent global string variables from the database.
 *
 **/
static void mapreg_load_str_db(void)
{
	struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

	if (stmt == NULL) {
		SqlStmt_ShowDebug(stmt);
		return;
	}

	const char *query = "SELECT `key`, `index`, `value` FROM `%s`";
	char name[SCRIPT_VARNAME_LENGTH + 1];
	unsigned int index;
	char value[SCRIPT_STRING_VAR_LENGTH + 1];

	if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db)
	    || SQL_ERROR == SQL->StmtExecute(stmt)
	    || SQL_ERROR == SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name, sizeof(name), NULL, NULL)
	    || SQL_ERROR == SQL->StmtBindColumn(stmt, 1, SQLDT_UINT32, &index, sizeof(index), NULL, NULL)
	    || SQL_ERROR == SQL->StmtBindColumn(stmt, 2, SQLDT_STRING, &value, sizeof(value), NULL, NULL)) {
		SqlStmt_ShowDebug(stmt);
		SQL->StmtFree(stmt);
		return;
	}

	if (SQL->StmtNumRows(stmt) < 1) {
		SQL->StmtFree(stmt);
		return;
	}

	mapreg->skip_insert = true;

	while (SQL_SUCCESS == SQL->StmtNextRow(stmt)) {
		int var_key = script->add_variable(name);
		int64 uid = reference_uid(var_key, index);

		if (i64db_exists(mapreg->regs.vars, uid)) {
			ShowWarning("mapreg_load_str_db: Duplicate! '%s' => '%s' Skipping...\n", name, value);
			continue;
		}

		mapreg->setregstr(uid, value);
	}

	mapreg->skip_insert = false;
	SQL->StmtFree(stmt);
}

/**
 * Loads permanent global variables from the database.
 *
 **/
static void mapreg_load(void)
{
	mapreg->load_num_db();
	mapreg->load_str_db();
	mapreg->dirty = false;
}

/**
 * Saves a permanent global integer variable to the database.
 *
 * @param name The variable's name.
 * @param index The variable's array index.
 * @param value The variable's value.
 *
 **/
static void mapreg_save_num_db(const char *name, unsigned int index, int value)
{
	nullpo_retv(name);
	Assert_retv(*name != '\0');
	Assert_retv(strlen(name) <= SCRIPT_VARNAME_LENGTH);

	struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

	if (stmt == NULL) {
		SqlStmt_ShowDebug(stmt);
		return;
	}

	const char *query = "UPDATE `%s` SET `value`=? WHERE `key`=? AND `index`=? LIMIT 1";

	if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db)
	    || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &value, sizeof(value))
	    || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, name, strlen(name))
	    || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_UINT32, &index, sizeof(index))
	    || SQL_ERROR == SQL->StmtExecute(stmt)) {
		SqlStmt_ShowDebug(stmt);
	}

	SQL->StmtFree(stmt);
}

/**
 * Saves a permanent global string variable to the database.
 *
 * @param name The variable's name.
 * @param index The variable's array index.
 * @param value The variable's value.
 *
 **/
static void mapreg_save_str_db(const char *name, unsigned int index, const char *value)
{
	nullpo_retv(name);
	nullpo_retv(value);
	Assert_retv(*name != '\0');
	Assert_retv(strlen(name) <= SCRIPT_VARNAME_LENGTH);
	Assert_retv(*value != '\0');
	Assert_retv(strlen(value) <= SCRIPT_STRING_VAR_LENGTH);

	struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);

	if (stmt == NULL) {
		SqlStmt_ShowDebug(stmt);
		return;
	}

	const char *query = "UPDATE `%s` SET `value`=? WHERE `key`=? AND `index`=? LIMIT 1";

	if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db)
	    || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, value, strlen(value))
	    || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, name, strlen(name))
	    || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_UINT32, &index, sizeof(index))
	    || SQL_ERROR == SQL->StmtExecute(stmt)) {
		SqlStmt_ShowDebug(stmt);
	}

	SQL->StmtFree(stmt);
}

/**
 * Saves permanent global variables to the database.
 *
 **/
static void mapreg_save(void)
{
	if (mapreg->dirty) {
		struct DBIterator *iter = db_iterator(mapreg->regs.vars);
		struct mapreg_save *var = NULL;

		for (var = dbi_first(iter); dbi_exists(iter); var = dbi_next(iter)) {
			if (var->save) {
				int index = script_getvaridx(var->uid);
				const char *name = script->get_str(script_getvarid(var->uid));

				if (!var->is_string)
					mapreg->save_num_db(name, index, var->u.i);
				else
					mapreg->save_str_db(name, index, var->u.str);

				var->save = false;
			}
		}

		dbi_destroy(iter);
		mapreg->dirty = false;
	}
}

/**
 * Timer event to auto-save permanent global variables.
 *
 * @see timer->do_timer()
 *
 * @param tid Unused.
 * @param tick Unused.
 * @param id Unused.
 * @param data Unused.
 * @return Always 0.
 *
 **/
static int mapreg_save_timer(int tid, int64 tick, int id, intptr_t data)
{
	mapreg->save();
	return 0;
}

/**
 * Destroys a mapreg_save structure and frees the contained string, if any.
 *
 * @see DBApply
 *
 * @param key Unused.
 * @param data The DB data holding the mapreg_save data.
 * @param ap Unused.
 * @return 0 on success, otherwise 1.
 *
 **/
static int mapreg_destroy_reg(union DBKey key, struct DBData *data, va_list ap)
{
	nullpo_retr(1, data);

	if (data->type != DB_DATA_PTR) // Sanity check
		return 1;

	struct mapreg_save *var = DB->data2ptr(data);

	if (var == NULL)
		return 1;

	if (var->is_string && var->u.str != NULL)
		aFree(var->u.str);

	ers_free(mapreg->ers, var);
	return 0;
}

/**
 * Reloads permanent global variables, saving them to the database beforehand.
 *
 * This has the effect of clearing the temporary global variables and reloading the permanent ones.
 *
 **/
static void mapreg_reload(void)
{
	mapreg->save();
	mapreg->regs.vars->clear(mapreg->regs.vars, mapreg->destroyreg);

	if (mapreg->regs.arrays != NULL) {
		mapreg->regs.arrays->destroy(mapreg->regs.arrays, script->array_free_db);
		mapreg->regs.arrays = NULL;
	}

	mapreg->load();
}

/**
 * Loads the mapreg database table names from configuration file.
 *
 * @param filename Path to configuration file. (Used in error and warning messages).
 * @param config The current config being parsed.
 * @param imported Whether the current config is imported from another file.
 * @return True on success, otherwise false.
 *
 **/
static bool mapreg_config_read_registry(const char *filename, const struct config_setting_t *config, bool imported)
{
	nullpo_retr(false, filename);
	nullpo_retr(false, config);
	
	bool ret_val = true;
	size_t sz = sizeof(mapreg->num_db);
	int result = libconfig->setting_lookup_mutable_string(config, "map_reg_num_db", mapreg->num_db, sz);

	if (result != CONFIG_TRUE && !imported) {
		ShowError("%s: inter_configuration/database_names/registry/map_reg_num_db was not found in %s!\n",
			  __func__, filename);
		ret_val = false;
	}

	sz = sizeof(mapreg->str_db);
	result = libconfig->setting_lookup_mutable_string(config, "map_reg_str_db", mapreg->str_db, sz);

	if (result != CONFIG_TRUE && !imported) {
		ShowError("%s: inter_configuration/database_names/registry/map_reg_str_db was not found in %s!\n",
			  __func__, filename);
		ret_val = false;
	}

	return ret_val;
}

/**
 * Saves permanent global variables to the database and frees all the memory they use afterwards.
 *
 **/
static void mapreg_final(void)
{
	mapreg->save();
	mapreg->regs.vars->destroy(mapreg->regs.vars, mapreg->destroyreg);
	ers_destroy(mapreg->ers);

	if (mapreg->regs.arrays != NULL)
		mapreg->regs.arrays->destroy(mapreg->regs.arrays, script->array_free_db);
}

/**
 * Allocates memory for permanent global variables, loads them from the database and initializes the auto-save timer.
 *
 **/
static void mapreg_init(void)
{
	mapreg->regs.vars = i64db_alloc(DB_OPT_BASE);
	mapreg->ers = ers_new(sizeof(struct mapreg_save), "mapreg_sql.c::mapreg_ers", ERS_OPT_CLEAN);
	mapreg->load();
	timer->add_func_list(mapreg->save_timer, "mapreg_save_timer");
	timer->add_interval(timer->gettick() + MAPREG_AUTOSAVE_INTERVAL, mapreg->save_timer, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
}

/**
 * Initializes the mapreg interface defaults.
 *
 **/
void mapreg_defaults(void)
{
	/** Interface structure. **/
	mapreg = &mapreg_s;

	/** Interface variables. **/
	mapreg->ers = NULL;
	mapreg->regs.vars = NULL;
	mapreg->regs.arrays = NULL;
	mapreg->dirty = false;
	mapreg->skip_insert = false;
	safestrncpy(mapreg->num_db, "map_reg_num_db", sizeof(mapreg->num_db));
	safestrncpy(mapreg->str_db, "map_reg_str_db", sizeof(mapreg->str_db));

	/** Interface functions. **/
	mapreg->readreg = mapreg_get_num_reg;
	mapreg->readregstr = mapreg_get_str_reg;
	mapreg->set_num_db = mapreg_set_num_db;
	mapreg->delete_num_db = mapreg_delete_num_db;
	mapreg->setreg = mapreg_set_num;
	mapreg->set_str_db = mapreg_set_str_db;
	mapreg->delete_str_db = mapreg_delete_str_db;
	mapreg->setregstr = mapreg_set_str;
	mapreg->load_num_db = mapreg_load_num_db;
	mapreg->load_str_db = mapreg_load_str_db;
	mapreg->load = mapreg_load;
	mapreg->save_num_db = mapreg_save_num_db;
	mapreg->save_str_db = mapreg_save_str_db;
	mapreg->save = mapreg_save;
	mapreg->save_timer = mapreg_save_timer;
	mapreg->destroyreg = mapreg_destroy_reg;
	mapreg->reload = mapreg_reload;
	mapreg->config_read_registry = mapreg_config_read_registry;
	mapreg->final = mapreg_final;
	mapreg->init = mapreg_init;
}