summaryrefslogtreecommitdiff
path: root/tools/update/update.cpp
blob: ee20076593e230e715941efbbe7dd7ecf054ece5 (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
320
321
322
323
#ifdef LC_WINDOWS
#include <windows.h>
#endif
#include <stdio.h>
#include "defines.h"
#include "file.h"
#include "update.h"

#define UPDATE_DELETE		0x00
#define UPDATE_DESCRIPTION	0x01
#define UPDATE_DRAWINFO		0x02
#define UPDATE_NEWPIECE		0x04

typedef struct
{
	char oldname[9];
	char newname[9];
} LC_MOVED_DATA;

typedef struct
{
    char name[9];
    char description[65];
    short bricksize[6];
    unsigned char flags;
    unsigned long group;
    unsigned long offset;
    unsigned long size;
} LC_PIECEINFO;

// ========================================================

static void message(File* f, char *fmt, ...)
{
	char s[300];
	va_list argptr;
	va_start(argptr, fmt);
	vsprintf(s, fmt, argptr);
	va_end(argptr);

	printf(s);
	f->Write(s, strlen(s));
}

// ========================================================

int main(int argc, char* argv[])
{
	char oldpath[LC_MAXPATH], newpath[LC_MAXPATH], str[LC_MAXPATH];
	FileDisk oldidx, oldbin, newidx, newbin, out, txt;
	int number;

	if (argc != 4)
	{
		printf("LeoCAD Libray Update\n");
		printf("Old path: ");
		scanf("%s", oldpath);
		printf("New path: ");
		scanf("%s", newpath);
		printf("Update number: ");
		scanf("%d", &number);
	}
	else
	{
		strcpy(oldpath, argv[1]);
		strcpy(newpath, argv[2]);
		sscanf(argv[3], "%d", &number);
	}

	if ((oldpath[strlen(oldpath)-1] != '/') &&
		(oldpath[strlen(oldpath)-1] != '\\'))
		strcat(oldpath, "/");

	if ((newpath[strlen(newpath)-1] != '/') &&
		(newpath[strlen(newpath)-1] != '\\'))
		strcat(newpath, "/");

	// Open files
	strcpy(str, oldpath);
	strcat(str, "pieces.bin");
	if (!oldbin.Open(str, "rb"))
		return 1;

	strcpy(str, oldpath);
	strcat(str, "pieces.idx");
	if (!oldidx.Open(str, "rb"))
		return 1;

	strcpy(str, newpath);
	strcat(str, "pieces.bin");
	if (!newbin.Open(str, "rb"))
		return 1;

	strcpy(str, newpath);
	strcat(str, "pieces.idx");
	if (!newidx.Open(str, "rb"))
		return 1;

	sprintf(str, "%supdate%02d.lup", newpath, number);
	if (!out.Open(str, "wb"))
		return 1;

	sprintf(str, "%supdate%02d.txt", newpath, number);
	if (!txt.Open(str, "wt"))
		return 1;

	// Now we start the update
	const char id[32] = "LeoCAD piece library update\0\0\0\0";
	unsigned short oldcount, newcount, changes = 0;
	LC_PIECEINFO *oldinfo, *newinfo;
	void *oldbuf, *newbuf;
	unsigned long sz;
	unsigned char bt;
	int i, j;

	out.Write(id, 32);
	bt = 2;	// version
	out.WriteByte(&bt, 1);
	bt = number; // update number
	out.WriteByte(&bt, 1);

	oldidx.Seek(-2, SEEK_END);
	oldidx.ReadShort(&oldcount, 1);
	oldidx.Seek(34, SEEK_SET);
	newidx.Seek (-2, SEEK_END);
	newidx.ReadShort(&newcount, 1);
	newidx.Seek(34, SEEK_SET);
	message(&txt, "Old count: %d, New count: %d\n\n", oldcount, newcount);

	oldinfo = (LC_PIECEINFO*)malloc(sizeof(LC_PIECEINFO)*oldcount);
	memset(oldinfo, 0, sizeof(LC_PIECEINFO)*oldcount);
	newinfo = (LC_PIECEINFO*)malloc(sizeof(LC_PIECEINFO)*newcount);
	memset(newinfo, 0, sizeof(LC_PIECEINFO)*newcount);

	for (i = 0; i < oldcount; i++)
	{
		oldidx.Read(&oldinfo[i].name, 8);
		oldidx.Read(&oldinfo[i].description, 64);
		oldidx.Read(&oldinfo[i].bricksize, 12);
		oldidx.ReadByte(&oldinfo[i].flags, 1);
		oldidx.ReadLong(&oldinfo[i].group, 1);
		oldidx.ReadLong(&oldinfo[i].offset, 1);
		oldidx.ReadLong(&oldinfo[i].size, 1);
	}

	for (i = 0; i < newcount; i++)
	{
		newidx.Read(&newinfo[i].name, 8);
		newidx.Read(&newinfo[i].description, 64);
		newidx.Read(&newinfo[i].bricksize, 12);
		newidx.ReadByte(&newinfo[i].flags, 1);
		newidx.ReadLong(&newinfo[i].group, 1);
		newidx.ReadLong(&newinfo[i].offset, 1);
		newidx.ReadLong(&newinfo[i].size, 1);
	}

	for (i = 0; i < oldcount; i++)
	{
		unsigned char update = 0;

		for (j = 0; j < newcount; j++)
		{
			if (strcmp(newinfo[j].name, oldinfo[i].name) == 0)
			{
				// Compare Descriptions
				if (strcmp(newinfo[j].description, oldinfo[i].description) != 0)
					update |= UPDATE_DESCRIPTION;

				// Compare drawinfo
				sz = newinfo[j].size;
				if (sz != oldinfo[i].size)
				{
					update |= UPDATE_DRAWINFO;
					newbuf = malloc(sz);
					newbin.Seek(newinfo[j].offset, SEEK_SET);
					newbin.Read(newbuf, sz);
				}
				else
				{
					oldbuf = malloc(sz);
					oldbin.Seek(oldinfo[i].offset, SEEK_SET);
					oldbin.Read(oldbuf, sz);
					newbuf = malloc(sz);
					newbin.Seek(newinfo[j].offset, SEEK_SET);
					newbin.Read(newbuf, sz);

					if (memcmp(newbuf, oldbuf, sz) != 0)
						update |= UPDATE_DRAWINFO;
					else
						free(newbuf);

					free(oldbuf);
				}
				break;
			}
		}

		// deleted
		if (j == newcount)
		{
			message(&txt, "%s (%s) deleted\n", oldinfo[i].name, oldinfo[i].description);
			out.Write(&oldinfo[i].name, 8);
			out.WriteByte(&update, 1);
			changes++;
		}
		else
		{
			if (update != 0)
			{
				message(&txt, "%s (%s) updated ", oldinfo[i].name, oldinfo[i].description);
				strcpy (str, "");
				if (update & UPDATE_DESCRIPTION)
				{
					strcat(str, "description");
					if (update & UPDATE_DRAWINFO)
						strcat(str, " & ");
				}
				if (update & UPDATE_DRAWINFO)
					strcat(str, "draw info");

				changes++;
				out.Write(&oldinfo[i].name, 8);
				out.WriteByte(&update, 1);

				if (update & UPDATE_DESCRIPTION)
				{
					out.Write(&newinfo[j].description, 64);
					out.WriteLong(&newinfo[j].group, 1);
				}

				if (update & UPDATE_DRAWINFO)
				{
					out.Write(&newinfo[j].bricksize, 12);
					out.WriteByte(&newinfo[j].flags, 1);
					out.WriteLong(&sz, 1);
					out.Write(newbuf, sz);
					free(newbuf);
				}

				message(&txt, "%s\n", str);
			}
		}
	}

	message(&txt, "------------\n");

	for (j = 0; j < newcount; j++)
	{
		for (i = 0; i < oldcount; i++)
			if (strcmp(newinfo[j].name, oldinfo[i].name) == 0)
				break;

		// found a new piece
		if (i == oldcount)
		{
			message(&txt, "%s (%s) added\n", newinfo[j].name, newinfo[j].description);

			bt = UPDATE_NEWPIECE;
			out.Write(&newinfo[j].name, 8);
			out.Write(&bt, 1);
			changes++;

			sz = newinfo[j].size;
			newbuf = malloc(sz);
			newbin.Seek(newinfo[j].offset, SEEK_SET);
			newbin.Read(newbuf, sz);
			out.Write(&newinfo[j].description, 64);
			out.Write(&newinfo[j].bricksize, 12);
			out.WriteLong(&newinfo[j].group, 1);
			out.WriteByte(&newinfo[j].flags, 1);
			out.WriteLong(&sz, 1);
			out.Write(newbuf, sz);
			free(newbuf);
		}
	}

	// Moved pieces list
	strcpy(str, newpath);
	strcat(str, "moved.txt");
	FILE* f = fopen(str, "rt");
	char buf[100], f1[9], f2[9], *ptr, *ptr2;
	unsigned short movecount = 0;

	while (fgets(buf, 100, f) != NULL)
	{
		if (ptr = strchr(buf, ' '))
		{
			*ptr = 0;
			memset(f1, 0, 9);
			strcpy(f1, buf);
			ptr++;
			if (ptr2 = strchr(ptr, ' '))
			{
				*ptr2 = 0;
				memset(f2, 0, 9);
				strcpy(f2, ptr);
				movecount++;
				strupr(f1);
				strupr(f2);
				out.Write(f1, 8);
				out.Write(f2, 8);
			}
		}
	}
	fclose(f);

	message(&txt, "\nTotal changes: %d\nMoved files: %d\n", changes, movecount);

	out.WriteShort(&movecount, 1);
	out.WriteShort(&changes, 1);

	free(oldinfo);
	free(newinfo);

	oldidx.Close();
	oldbin.Close();
	newidx.Close();
	newbin.Close();
	out.Close();
	txt.Close();

	return 0;
}