about summary refs log tree commit diff
path: root/src/tools/map_version.cpp
blob: 4acc51e04724ce5ca30e22c79dae8be765a384b8 (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
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com.                */
#include <base/math.h>
#include <base/system.h>

#include <engine/kernel.h>
#include <engine/map.h>
#include <engine/storage.h>


static IOHANDLE s_File = 0;
static IStorage *s_pStorage = 0;
static IEngineMap *s_pEngineMap = 0;

int MaplistCallback(const char *pName, int IsDir, int DirType, void *pUser)
{
	int l = str_length(pName);
	if(l < 4 || IsDir || str_comp(pName+l-4, ".map") != 0)
		return 0;

	char aBuf[128];
	str_format(aBuf, sizeof(aBuf), "maps/%s", pName);
	if(!s_pEngineMap->Load(aBuf))
		return 0;

	unsigned MapCrc = s_pEngineMap->Crc();
	s_pEngineMap->Unload();

	IOHANDLE MapFile = s_pStorage->OpenFile(aBuf, IOFLAG_READ, DirType);
	unsigned MapSize = io_length(MapFile);
	io_close(MapFile);

	char aMapName[8];
	str_copy(aMapName, pName, min((int)sizeof(aMapName),l-3));

	str_format(aBuf, sizeof(aBuf), "\t{\"%s\", {0x%02x, 0x%02x, 0x%02x, 0x%02x}, {0x%02x, 0x%02x, 0x%02x, 0x%02x}},\n", aMapName,
		(MapCrc>>24)&0xff, (MapCrc>>16)&0xff, (MapCrc>>8)&0xff, MapCrc&0xff,
		(MapSize>>24)&0xff, (MapSize>>16)&0xff, (MapSize>>8)&0xff, MapSize&0xff);
	io_write(s_File, aBuf, str_length(aBuf));

	return 0;
}

int main(int argc, const char **argv) // ignore_convention
{
	IKernel *pKernel = IKernel::Create();
	s_pStorage = CreateStorage("Teeworlds", argc, argv);
	s_pEngineMap = CreateEngineMap();

	bool RegisterFail = !pKernel->RegisterInterface(s_pStorage);
	RegisterFail |= !pKernel->RegisterInterface(s_pEngineMap);

	if(RegisterFail)
		return -1;

	s_File = s_pStorage->OpenFile("map_version.txt", IOFLAG_WRITE, 1);
	if(s_File)
	{
		io_write(s_File, "static CMapVersion s_aMapVersionList[] = {\n", str_length("static CMapVersion s_aMapVersionList[] = {\n"));
		s_pStorage->ListDirectory(1, "maps", MaplistCallback, 0);
		io_write(s_File, "};\n", str_length("};\n"));
		io_close(s_File);
	}

	return 0;
}