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