summaryrefslogtreecommitdiff
path: root/src/txt-converter/char-converter.c
blob: 63c2616048396f922fb259fcde49c65976d32656 (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
// (c) eAthena Dev Team - Licensed under GNU GPL
// For more information, see LICENCE in the main folder

#include "../common/cbasetypes.h"
#include "../common/mmo.h"
#include "../common/core.h"
#include "../common/strlib.h"
#include "../common/showmsg.h"
#include "../common/mapindex.h"

#include "../char/char.h"
#include "../char/int_storage.h"
#include "../char/int_pet.h"
#include "../char/int_party.h"
#include "../char/int_guild.h"
#include "../char/inter.h"

#include "../char_sql/char.h"
#include "../char_sql/int_storage.h"
#include "../char_sql/int_pet.h"
#include "../char_sql/int_party.h"
#include "../char_sql/int_guild.h"
#include "../char_sql/inter.h"

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

#define CHAR_CONF_NAME "conf/char_athena.conf"
#define SQL_CONF_NAME "conf/inter_athena.conf"
#define INTER_CONF_NAME "conf/inter_athena.conf"
//--------------------------------------------------------

int convert_init(void)
{
	char line[65536];
	int ret;
	int set,tmp_int[2], lineno, count;
	char input;
	FILE *fp;

	ShowWarning("Make sure you backup your databases before continuing!\n");
	printf("\n");

	ShowNotice("Do you wish to convert your Character Database to SQL? (y/n) : ");
	input = getchar();
	if(input == 'y' || input == 'Y')
	{
		struct character_data char_dat;
		struct accreg reg;

		ShowStatus("Converting Character Database...\n");
		if( (fp = fopen(char_txt, "r")) == NULL )
		{
			ShowError("Unable to open file [%s]!\n", char_txt);
			return 0;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			memset(&char_dat, 0, sizeof(struct character_data));
			ret=mmo_char_fromstr(line, &char_dat.status, char_dat.global, &char_dat.global_num);
			if(ret > 0) {
				count++;
				parse_friend_txt(&char_dat.status); //Retrieve friends.
				mmo_char_tosql(char_dat.status.char_id , &char_dat.status);

				memset(&reg, 0, sizeof(reg));
				reg.account_id = char_dat.status.account_id;
				reg.char_id = char_dat.status.char_id;
				reg.reg_num = char_dat.global_num;
				memcpy(&reg.reg, &char_dat.global, reg.reg_num*sizeof(struct global_reg));
				inter_accreg_tosql(reg.account_id, reg.char_id, &reg, 3); //Type 3: Character regs
			} else {
				ShowError("Error %d converting character line [%s] (at %s:%d).\n", ret, line, char_txt, lineno);
			}
		}
		ShowStatus("Converted %d characters.\n", count);
		fclose(fp);
		ShowStatus("Converting Account variables Database...\n");
		if( (fp = fopen(accreg_txt, "r")) == NULL )
		{
			ShowError("Unable to open file %s!", accreg_txt);
			return 1;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			memset (&reg, 0, sizeof(struct accreg));
			if(inter_accreg_fromstr(line, &reg) == 0 && reg.account_id > 0) {
				count++;
				inter_accreg_tosql(reg.account_id, 0, &reg, 2); //Type 2: Account regs
			} else {
				ShowError("accreg reading: broken data [%s] at %s:%d\n", line, accreg_txt, lineno);
			}
		}
		ShowStatus("Converted %d account registries.\n", count);
		fclose(fp);
	}
	
	while(getchar() != '\n');
	printf("\n");
	ShowNotice("Do you wish to convert your Storage Database to SQL? (y/n) : ");
	input = getchar();
	if(input == 'y' || input == 'Y')
	{
		struct storage storage_;
		printf("\n");
		ShowStatus("Converting Storage Database...\n");
		if( (fp = fopen(storage_txt,"r")) == NULL )
		{
			ShowError("can't read : %s\n", storage_txt);
			return 0;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			set=sscanf(line,"%d,%d",&tmp_int[0],&tmp_int[1]);
			if(set==2) {
				memset(&storage_, 0, sizeof(struct storage));
				storage_.account_id=tmp_int[0];
				if (storage_fromstr(line,&storage_) == 0) {
					count++;
					storage_tosql(storage_.account_id,&storage_); //to sql. (dump)
				} else {
					ShowError("Error parsing storage line [%s] (at %s:%d)\n", line, storage_txt, lineno);
				}
			}
		}
		ShowStatus("Converted %d storages.\n", count);
		fclose(fp);
	}

	//FIXME: CONVERT STATUS DATA HERE!!!

	while(getchar() != '\n');
	printf("\n");
	ShowNotice("Do you wish to convert your Pet Database to SQL? (y/n) : ");
	input=getchar();
	if(input == 'y' || input == 'Y')
	{
		struct s_pet p;
		printf("\n");
		ShowStatus("Converting Pet Database...\n");
		if( (fp = fopen(pet_txt, "r")) == NULL )
		{
			ShowError("Unable to open file %s!", pet_txt);
			return 1;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			memset (&p, 0, sizeof(struct s_pet));
			if(inter_pet_fromstr(line, &p)==0 && p.pet_id>0) {
				count++;
				inter_pet_tosql(p.pet_id,&p);
			} else {
				ShowError("pet reading: broken data [%s] at %s:%d\n", line, pet_txt, lineno);
			}
		}
		ShowStatus("Converted %d pets.\n", count);
		fclose(fp);
	}

	//FIXME: CONVERT HOMUNCULUS DATA AND SKILLS HERE!!!

	while(getchar() != '\n');
	printf("\n");
	ShowNotice("Do you wish to convert your Party Database to SQL? (y/n) : ");
	input=getchar();
	if(input == 'y' || input == 'Y')
	{
		struct party p;
		printf("\n");
		ShowStatus("Converting Party Database...\n");
		if( (fp = fopen(party_txt, "r")) == NULL )
		{
			ShowError("Unable to open file %s!", party_txt);
			return 1;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			memset (&p, 0, sizeof(struct party));
			if(inter_party_fromstr(line, &p) == 0 &&
				p.party_id > 0 &&
				inter_party_tosql(&p, PS_CREATE, 0))
				count++;
			else{
				ShowError("party reading: broken data [%s] at %s:%d\n", line, pet_txt, lineno);
			}
		}
		ShowStatus("Converted %d parties.\n", count);
		fclose(fp);
	}

	while(getchar() != '\n');
	printf("\n");
	ShowNotice("Do you wish to convert your Guilds and Castles Database to SQL? (y/n) : ");
	input=getchar();
	if(input == 'y' || input == 'Y')
	{
		struct guild g;
		struct guild_castle gc;
		printf("\n");
		ShowStatus("Converting Guild Database...\n");
		if( (fp = fopen(guild_txt, "r")) == NULL )
		{
			ShowError("Unable to open file %s!", guild_txt);
			return 1;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			memset (&g, 0, sizeof(struct guild));
			if (inter_guild_fromstr(line, &g) == 0 &&
				g.guild_id > 0 &&
				inter_guild_tosql(&g,GS_MASK))
				count++;
			else
				ShowError("guild reading: broken data [%s] at %s:%d\n", line, guild_txt, lineno);
		}
		ShowStatus("Converted %d guilds.\n", count);
		fclose(fp);
		ShowStatus("Converting Guild Castles Database...\n");
		if( (fp = fopen(castle_txt, "r")) == NULL )
		{
			ShowError("Unable to open file %s!", castle_txt);
			return 1;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			memset(&gc, 0, sizeof(struct guild_castle));
			if (inter_guildcastle_fromstr(line, &gc) == 0) {
				inter_guildcastle_tosql(&gc);
				count++;
			}
			else
				ShowError("guild castle reading: broken data [%s] at %s:%d\n", line, castle_txt, lineno);
		}
		ShowStatus("Converted %d guild castles.\n", count);
		fclose(fp);
	}

	while(getchar() != '\n');
	printf("\n");
	ShowNotice("Do you wish to convert your Guild Storage Database to SQL? (y/n) : ");
	input=getchar();
	if(input == 'y' || input == 'Y')
	{
		struct guild_storage storage_;
		printf("\n");
		ShowStatus("Converting Guild Storage Database...\n");
		if( (fp = fopen(guild_storage_txt, "r")) == NULL )
		{
			ShowError("can't read : %s\n", guild_storage_txt);
			return 0;
		}
		lineno = count = 0;
		while(fgets(line, sizeof(line), fp))
		{
			lineno++;
			memset(&storage_, 0, sizeof(struct guild_storage));
			if (sscanf(line,"%d",&storage_.guild_id) == 1 &&
				storage_.guild_id > 0 &&
				guild_storage_fromstr(line,&storage_) == 0
			) {
				count++;
				guild_storage_tosql(storage_.guild_id, &storage_);
			} else
				ShowError("Error parsing guild storage line [%s] (at %s:%d)\n", line, guild_storage_txt, lineno);
		}
		ShowStatus("Converted %d guild storages.\n", count);
		fclose(fp);
	}

	//FIXME: CONVERT MAPREG HERE! (after enforcing MAPREGSQL for sql)

	return 0;
}

int do_init(int argc, char** argv)
{
	char_config_read( (argc > 1) ? argv[1] : CHAR_CONF_NAME);
	mapindex_init();
	sql_config_read( (argc > 2) ? argv[2] : SQL_CONF_NAME);
	inter_init_txt( (argc > 3) ? argv[3] : INTER_CONF_NAME);
	inter_init_sql( (argc > 3) ? argv[3] : INTER_CONF_NAME);
	convert_init();
	ShowStatus("Everything's been converted!\n");
	mapindex_final();
	return 0;
}

void do_abort(void) {}

void do_final(void) {}