1#include <base/hash.h>
2#include <base/logger.h>
3#include <base/os.h>
4#include <base/system.h>
5
6#include <engine/shared/datafile.h>
7#include <engine/storage.h>
8
9static const char *TOOL_NAME = "map_test";
10
11static int TestMap(const char *pMap, bool CalcHashes, IStorage *pStorage)
12{
13 log_info(TOOL_NAME, "Testing map '%s'...", pMap);
14
15 CDataFileReader Reader;
16 if(!Reader.Open(pStorage, pPath: pMap, StorageType: IStorage::TYPE_ABSOLUTE))
17 {
18 log_error(TOOL_NAME, "Failed to open map '%s' for reading", pMap);
19 return -1;
20 }
21
22 char aSha256Str[SHA256_MAXSTRSIZE];
23 sha256_str(digest: Reader.Sha256(), str: aSha256Str, max_len: sizeof(aSha256Str));
24 log_info(TOOL_NAME, "File size: %d", Reader.Size());
25 log_info(TOOL_NAME, "File SHA256: %s", aSha256Str);
26 log_info(TOOL_NAME, "File CRC32: %08x", Reader.Crc());
27 log_info(TOOL_NAME, "Num items: %d", Reader.NumItems());
28 log_info(TOOL_NAME, "Num data: %d", Reader.NumData());
29
30 for(int Index = 0; Index < Reader.NumItems(); Index++)
31 {
32 log_info(TOOL_NAME, "Item %d:", Index);
33
34 int Type, Id;
35 CUuid Uuid;
36 const void *pItem = Reader.GetItem(Index, pType: &Type, pId: &Id, pUuid: &Uuid);
37
38 log_info(TOOL_NAME, " Type: %d", Type);
39 log_info(TOOL_NAME, " ID: %d", Id);
40 char aUuidStr[UUID_MAXSTRSIZE];
41 FormatUuid(Uuid, pBuffer: aUuidStr, BufferLength: sizeof(aUuidStr));
42 log_info(TOOL_NAME, " UUID: %s", aUuidStr);
43
44 const int Size = Reader.GetItemSize(Index);
45 log_info(TOOL_NAME, " Size: %d bytes", Size);
46
47 if(CalcHashes)
48 {
49 const SHA256_DIGEST ItemDataSha256 = sha256(message: pItem, message_len: Size);
50 sha256_str(digest: ItemDataSha256, str: aSha256Str, max_len: sizeof(aSha256Str));
51 log_info(TOOL_NAME, " Data (SHA256): %s", aSha256Str);
52 }
53 }
54
55 for(int Index = 0; Index < Reader.NumData(); Index++)
56 {
57 log_info(TOOL_NAME, "Data %d:", Index);
58
59 const int Size = Reader.GetDataSize(Index);
60 log_info(TOOL_NAME, " Size: %d bytes", Size);
61
62 const void *pData = Reader.GetData(Index);
63 if(pData == nullptr)
64 {
65 log_info(TOOL_NAME, " Data erroneous");
66 }
67 else if(CalcHashes)
68 {
69 const SHA256_DIGEST ItemDataSha256 = sha256(message: pData, message_len: Size);
70 sha256_str(digest: ItemDataSha256, str: aSha256Str, max_len: sizeof(aSha256Str));
71 log_info(TOOL_NAME, " Data (SHA256): %s", aSha256Str);
72 }
73
74 Reader.UnloadData(Index);
75 }
76
77 Reader.Close();
78 log_info(TOOL_NAME, "Tested map '%s' successfully", pMap);
79 return 0;
80}
81
82int main(int argc, const char **argv)
83{
84 const CCmdlineFix CmdlineFix(&argc, &argv);
85 log_set_global_logger_default();
86
87 const char *pMap;
88 bool CalcHashes;
89 if(argc == 2)
90 {
91 pMap = argv[1];
92 CalcHashes = false;
93 }
94 else if(argc == 3 && str_comp(a: argv[1], b: "--calc-hashes") == 0)
95 {
96 pMap = argv[2];
97 CalcHashes = true;
98 }
99 else
100 {
101 log_error(TOOL_NAME, "Usage: %s [--calc-hashes] <map>", TOOL_NAME);
102 return -1;
103 }
104
105 std::unique_ptr<IStorage> pStorage = std::unique_ptr<IStorage>(CreateStorage(InitializationType: IStorage::EInitializationType::BASIC, NumArgs: argc, ppArguments: argv));
106 if(!pStorage)
107 {
108 log_error(TOOL_NAME, "Error creating basic storage");
109 return -1;
110 }
111
112 return TestMap(pMap, CalcHashes, pStorage: pStorage.get());
113}
114