1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include <base/logger.h>
5#include <base/system.h>
6
7#include <engine/shared/datafile.h>
8#include <engine/storage.h>
9
10static const char *TOOL_NAME = "map_resave";
11
12static int ResaveMap(const char *pSourceMap, const char *pDestinationMap, IStorage *pStorage)
13{
14 CDataFileReader Reader;
15 if(!Reader.Open(pStorage, pFilename: pSourceMap, StorageType: IStorage::TYPE_ABSOLUTE))
16 {
17 log_error(TOOL_NAME, "Failed to open source map '%s' for reading", pSourceMap);
18 return -1;
19 }
20
21 CDataFileWriter Writer;
22 if(!Writer.Open(pStorage, pFilename: pDestinationMap))
23 {
24 log_error(TOOL_NAME, "Failed to open destination map '%s' for writing", pDestinationMap);
25 Reader.Close();
26 return -1;
27 }
28
29 // add all items
30 for(int Index = 0; Index < Reader.NumItems(); Index++)
31 {
32 int Type, Id;
33 CUuid Uuid;
34 const void *pPtr = Reader.GetItem(Index, pType: &Type, pId: &Id, pUuid: &Uuid);
35
36 // Filter ITEMTYPE_EX items, they will be automatically added again.
37 if(Type == ITEMTYPE_EX)
38 {
39 continue;
40 }
41
42 int Size = Reader.GetItemSize(Index);
43 Writer.AddItem(Type, Id, Size, pData: pPtr, pUuid: &Uuid);
44 }
45
46 // add all data
47 for(int Index = 0; Index < Reader.NumData(); Index++)
48 {
49 const void *pPtr = Reader.GetData(Index);
50 int Size = Reader.GetDataSize(Index);
51 Writer.AddData(Size, pData: pPtr);
52 }
53
54 Reader.Close();
55 Writer.Finish();
56 log_info(TOOL_NAME, "Resaved '%s' to '%s'", pSourceMap, pDestinationMap);
57 return 0;
58}
59
60int main(int argc, const char **argv)
61{
62 CCmdlineFix CmdlineFix(&argc, &argv);
63 log_set_global_logger_default();
64
65 if(argc != 3)
66 {
67 log_error(TOOL_NAME, "Usage: %s <source map> <destination map>", TOOL_NAME);
68 return -1;
69 }
70
71 IStorage *pStorage = CreateStorage(StorageType: IStorage::STORAGETYPE_BASIC, NumArgs: argc, ppArguments: argv);
72 if(!pStorage)
73 {
74 log_error(TOOL_NAME, "Error creating basic storage");
75 return -1;
76 }
77
78 return ResaveMap(pSourceMap: argv[1], pDestinationMap: argv[2], pStorage);
79}
80