1#include <base/system.h>
2#include <engine/shared/datafile.h>
3#include <engine/storage.h>
4#include <game/mapitems.h>
5
6void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName)
7{
8 CDataFileReader Reader;
9 if(!Reader.Open(pStorage, pFilename: pMapName, StorageType: IStorage::TYPE_ABSOLUTE))
10 {
11 dbg_msg(sys: "config_retrieve", fmt: "error opening map '%s'", pMapName);
12 return;
13 }
14 bool ConfigFound = false;
15 int Start, Num;
16 Reader.GetType(Type: MAPITEMTYPE_INFO, pStart: &Start, pNum: &Num);
17 for(int i = Start; i < Start + Num; i++)
18 {
19 int Id;
20 CMapItemInfoSettings *pItem = (CMapItemInfoSettings *)Reader.GetItem(Index: i, pType: nullptr, pId: &Id);
21 int ItemSize = Reader.GetItemSize(Index: i);
22 if(!pItem || Id != 0)
23 continue;
24
25 if(ItemSize < (int)sizeof(CMapItemInfoSettings))
26 break;
27 if(!(pItem->m_Settings > -1))
28 break;
29
30 ConfigFound = true;
31 IOHANDLE Config = pStorage->OpenFile(pFilename: pConfigName, Flags: IOFLAG_WRITE, Type: IStorage::TYPE_ABSOLUTE);
32 if(!Config)
33 {
34 dbg_msg(sys: "config_retrieve", fmt: "error opening config for writing '%s'", pConfigName);
35 Reader.Close();
36 return;
37 }
38
39 int Size = Reader.GetDataSize(Index: pItem->m_Settings);
40 char *pSettings = (char *)Reader.GetData(Index: pItem->m_Settings);
41 char *pNext = pSettings;
42 while(pNext < pSettings + Size)
43 {
44 int StrSize = str_length(str: pNext) + 1;
45 io_write(io: Config, buffer: pNext, size: StrSize - 1);
46 io_write_newline(io: Config);
47 pNext += StrSize;
48 }
49 Reader.UnloadData(Index: pItem->m_Settings);
50 io_close(io: Config);
51 break;
52 }
53 Reader.Close();
54 if(!ConfigFound)
55 {
56 fs_remove(filename: pConfigName);
57 }
58}
59#include "config_common.h"
60