summaryrefslogtreecommitdiff
path: root/src/map/mapreg_txt.c
blob: 555b773eeccb8cc978fb83fd67720b7ac6aa4246 (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
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder

#include "../common/cbasetypes.h"
#include "../common/db.h"
#include "../common/lock.h"
#include "../common/malloc.h"
#include "../common/showmsg.h"
#include "../common/strlib.h"
#include "../common/timer.h"
#include "script.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static DBMap* mapreg_db = NULL; // int var_id -> int value
static DBMap* mapregstr_db = NULL; // int var_id -> char* value

static char mapreg_txt[256] = "save/mapreg.txt";
static bool mapreg_dirty = false;
#define MAPREG_AUTOSAVE_INTERVAL (300*1000)


/// Looks up the value of an integer variable using its uid.
int mapreg_readreg(int uid)
{
	return (int)idb_get(mapreg_db, uid);
}

/// Looks up the value of a string variable using its uid.
char* mapreg_readregstr(int uid)
{
	return (char*)idb_get(mapregstr_db, uid);
}

/// Modifies the value of an integer variable.
bool mapreg_setreg(int uid, int val)
{
	if( val != 0 )
		idb_put(mapreg_db,uid,(void*)val);
	else
		idb_remove(mapreg_db,uid);

	mapreg_dirty = true;
	return true;
}

/// Modifies the value of a string variable.
bool mapreg_setregstr(int uid, const char* str)
{
	if( str == NULL || *str == 0 )
		idb_remove(mapregstr_db,uid);
	else
		idb_put(mapregstr_db,uid,aStrdup(str));

	mapreg_dirty = true;
	return true;
}

/// Loads permanent variables from savefile
static void script_load_mapreg(void)
{
	FILE* fp;
	char line[1024];

	fp = fopen(mapreg_txt,"rt");
	if( fp == NULL )
		return;

	while( fgets(line,sizeof(line),fp) )
	{
		char varname[32+1];
		char value[255+1];
		int n,s,i;

		// read name and index
		if( sscanf(line, "%32[^,],%d\t%n", varname,&i,&n) != 2 &&
			(i = 0, sscanf(line,"%[^\t]\t%n", varname,&n) != 1) )
			continue;

		// read value
		if( sscanf(line + n, "%[^\n\r]", value) != 1 )
		{
			ShowError("%s: %s broken data !\n", mapreg_txt, varname);
			continue;
		}

		s = add_str(varname);
		if( varname[strlen(varname)-1] == '$' )
			idb_put(mapregstr_db, (i<<24)|s, aStrdup(value));
		else
			idb_put(mapreg_db, (i<<24)|s, (void*)atoi(value));
	}
	fclose(fp);

	mapreg_dirty = false;
}

/// Saves permanent variables to savefile
static void script_save_mapreg(void)
{
	FILE *fp;
	int lock;
	DBIterator* iter;
	void* data;
	DBKey key;

	fp = lock_fopen(mapreg_txt,&lock);
	if( fp == NULL )
	{
		ShowError("script_save_mapreg: Unable to lock-open file [%s]!\n", mapreg_txt);
		return;
	}

	iter = mapreg_db->iterator(mapreg_db);
	for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) )
	{
		int num = (key.i & 0x00ffffff);
		int i   = (key.i & 0xff000000) >> 24;
		const char* name = get_str(num);

		if( name[1] == '@' )
			continue;

		if( i == 0 )
			fprintf(fp, "%s\t%d\n", name, (int)data);
		else
			fprintf(fp, "%s,%d\t%d\n", name, i, (int)data);
	}
	iter->destroy(iter);

	iter = mapregstr_db->iterator(mapregstr_db);
	for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) )
	{
		int num = (key.i & 0x00ffffff);
		int i   = (key.i & 0xff000000) >> 24;
		const char* name = get_str(num);

		if( name[1] == '@' )
			continue;

		if( i == 0 )
			fprintf(fp, "%s\t%s\n", name, (char *)data);
		else
			fprintf(fp, "%s,%d\t%s\n", name, i, (char *)data);
	}
	iter->destroy(iter);

	lock_fclose(fp,mapreg_txt,&lock);

	mapreg_dirty = false;
}

static int script_autosave_mapreg(int tid, unsigned int tick, int id, intptr data)
{
	if( mapreg_dirty )
		script_save_mapreg();

	return 0;
}


void mapreg_reload(void)
{
	if( mapreg_dirty )
		script_save_mapreg();

	mapreg_db->clear(mapreg_db, NULL);
	mapregstr_db->clear(mapregstr_db, NULL);

	script_load_mapreg();
}

void mapreg_final(void)
{
	if( mapreg_dirty )
		script_save_mapreg();

	mapreg_db->destroy(mapreg_db,NULL);
	mapregstr_db->destroy(mapregstr_db,NULL);
}

void mapreg_init(void)
{
	mapreg_db = idb_alloc(DB_OPT_BASE);
	mapregstr_db = idb_alloc(DB_OPT_RELEASE_DATA);

	script_load_mapreg();

	add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
	add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
}

bool mapreg_config_read(const char* w1, const char* w2)
{
	if(!strcmpi(w1, "mapreg_txt"))
		safestrncpy(mapreg_txt, w2, sizeof(mapreg_txt));
	else
		return false;

	return true;
}