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#include <game/mapitems.h>
9
10#include <zlib.h>
11
12void CreateEmptyMap(IStorage *pStorage)
13{
14 const char *pMapName = "maps/dummy3.map";
15
16 CDataFileWriter Writer;
17 if(!Writer.Open(pStorage, pFilename: pMapName))
18 {
19 dbg_msg(sys: "dummy_map", fmt: "couldn't open map file '%s' for writing", pMapName);
20 return;
21 }
22 CMapItemGroup_v1 Group;
23 Group.m_Version = 1;
24 Group.m_OffsetX = 0;
25 Group.m_OffsetY = 0;
26 Group.m_ParallaxX = 0;
27 Group.m_ParallaxY = 0;
28 Group.m_StartLayer = 0;
29 Group.m_NumLayers = 2;
30 Writer.AddItem(Type: MAPITEMTYPE_GROUP, Id: 0, Size: sizeof(Group), pData: &Group);
31
32 constexpr int LayerWidth = 2;
33 constexpr int LayerHeight = 2;
34 CTile aTiles[LayerWidth * LayerHeight];
35 for(auto &Tile : aTiles)
36 {
37 Tile.m_Index = 1;
38 Tile.m_Flags = 0;
39 Tile.m_Skip = 0;
40 Tile.m_Reserved = 0;
41 }
42 const int TilesData = Writer.AddData(Size: sizeof(aTiles), pData: &aTiles);
43
44 CMapItemLayerTilemap GameLayer;
45 GameLayer.m_Layer.m_Version = 0; // Not set by the official client.
46 GameLayer.m_Layer.m_Type = LAYERTYPE_TILES;
47 GameLayer.m_Layer.m_Flags = 0;
48 GameLayer.m_Version = 2;
49 GameLayer.m_Width = LayerWidth;
50 GameLayer.m_Height = LayerHeight;
51 GameLayer.m_Flags = TILESLAYERFLAG_GAME;
52 GameLayer.m_Color.r = 0;
53 GameLayer.m_Color.g = 0;
54 GameLayer.m_Color.b = 0;
55 GameLayer.m_Color.a = 0;
56 GameLayer.m_ColorEnv = -1;
57 GameLayer.m_ColorEnvOffset = 0;
58 GameLayer.m_Image = -1;
59 GameLayer.m_Data = TilesData;
60 Writer.AddItem(Type: MAPITEMTYPE_LAYER, Id: 0, Size: sizeof(GameLayer) - sizeof(GameLayer.m_aName) - sizeof(GameLayer.m_Tele) - sizeof(GameLayer.m_Speedup) - sizeof(GameLayer.m_Front) - sizeof(GameLayer.m_Switch) - sizeof(GameLayer.m_Tune), pData: &GameLayer);
61
62 CMapItemLayerTilemap Layer;
63 Layer.m_Layer.m_Version = 0;
64 Layer.m_Layer.m_Type = LAYERTYPE_TILES;
65 Layer.m_Layer.m_Flags = 0;
66 Layer.m_Version = 2;
67 Layer.m_Width = LayerWidth;
68 Layer.m_Height = LayerHeight;
69 Layer.m_Flags = 0;
70 Layer.m_Color.r = 0;
71 Layer.m_Color.g = 0;
72 Layer.m_Color.b = 0;
73 Layer.m_Color.a = 255;
74 Layer.m_ColorEnv = -1;
75 Layer.m_ColorEnvOffset = 0;
76 Layer.m_Image = -1;
77 Layer.m_Data = TilesData;
78 Writer.AddItem(Type: MAPITEMTYPE_LAYER, Id: 1, Size: sizeof(Layer) - sizeof(Layer.m_aName) - sizeof(Layer.m_Tele) - sizeof(Layer.m_Speedup) - sizeof(Layer.m_Front) - sizeof(Layer.m_Switch) - sizeof(Layer.m_Tune), pData: &Layer);
79
80 Writer.Finish();
81
82 dbg_msg(sys: "dummy_map", fmt: "dummy map written to '%s'", pMapName);
83
84 CDataFileReader Reader;
85 void *pData;
86 unsigned DataSize;
87 if(!pStorage->ReadFile(pFilename: pMapName, Type: IStorage::TYPE_ALL, ppResult: &pData, pResultLen: &DataSize))
88 {
89 dbg_msg(sys: "dummy_map", fmt: "couldn't open map file '%s' for reading", pMapName);
90 return;
91 }
92 unsigned char *pDataChar = static_cast<unsigned char *>(pData);
93
94 unsigned Crc = crc32(crc: 0, buf: pDataChar, len: DataSize);
95 SHA256_DIGEST Sha256 = sha256(message: pDataChar, message_len: DataSize);
96
97 char aMapSha[SHA256_MAXSTRSIZE];
98 sha256_str(digest: Sha256, str: aMapSha, max_len: sizeof(aMapSha));
99 dbg_msg(sys: "dummy_map", fmt: "crc32 %08X, sha256 %s", Crc, aMapSha);
100
101 const unsigned HexSize = 6 * DataSize + 1;
102 char *pHex = static_cast<char *>(malloc(size: HexSize));
103 str_hex_cstyle(dst: pHex, dst_size: HexSize, data: pDataChar, data_size: DataSize);
104 dbg_msg(sys: "dummy_map", fmt: "data %s", pHex);
105 free(ptr: pHex);
106
107 free(ptr: pDataChar);
108}
109
110int main(int argc, const char **argv)
111{
112 CCmdlineFix CmdlineFix(&argc, &argv);
113 log_set_global_logger_default();
114 IStorage *pStorage = CreateStorage(StorageType: IStorage::STORAGETYPE_SERVER, NumArgs: argc, ppArguments: argv);
115 if(!pStorage)
116 return -1;
117 CreateEmptyMap(pStorage);
118 return 0;
119}
120