2015-08-21 21:02:50 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
#include <base/tl/array.h>
|
|
|
|
#include <engine/shared/datafile.h>
|
|
|
|
#include <engine/shared/linereader.h>
|
|
|
|
#include <engine/storage.h>
|
|
|
|
#include <game/mapitems.h>
|
|
|
|
|
|
|
|
void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName)
|
|
|
|
{
|
2017-07-12 18:15:00 +00:00
|
|
|
IOHANDLE File = pStorage->OpenFile(pConfigName, IOFLAG_READ, IStorage::TYPE_ABSOLUTE);
|
2015-08-21 21:02:50 +00:00
|
|
|
array<char *> aLines;
|
|
|
|
char *pSettings = NULL;
|
|
|
|
if(!File)
|
|
|
|
{
|
|
|
|
dbg_msg("config_store", "config '%s' not found", pConfigName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLineReader LineReader;
|
|
|
|
LineReader.Init(File);
|
|
|
|
|
|
|
|
char *pLine;
|
|
|
|
int TotalLength = 0;
|
|
|
|
while((pLine = LineReader.Get()))
|
|
|
|
{
|
|
|
|
int Length = str_length(pLine) + 1;
|
2018-04-09 09:56:39 +00:00
|
|
|
char *pCopy = (char *)malloc(Length);
|
2015-08-21 21:02:50 +00:00
|
|
|
mem_copy(pCopy, pLine, Length);
|
|
|
|
aLines.add(pCopy);
|
|
|
|
TotalLength += Length;
|
|
|
|
}
|
2017-07-08 11:38:27 +00:00
|
|
|
io_close(File);
|
2015-08-21 21:02:50 +00:00
|
|
|
|
2018-04-09 09:56:39 +00:00
|
|
|
pSettings = (char *)malloc(TotalLength);
|
2015-08-21 21:02:50 +00:00
|
|
|
int Offset = 0;
|
|
|
|
for(int i = 0; i < aLines.size(); i++)
|
|
|
|
{
|
|
|
|
int Length = str_length(aLines[i]) + 1;
|
|
|
|
mem_copy(pSettings + Offset, aLines[i], Length);
|
|
|
|
Offset += Length;
|
2018-04-09 09:56:39 +00:00
|
|
|
free(aLines[i]);
|
2015-08-21 21:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CDataFileReader Reader;
|
2017-07-12 18:15:00 +00:00
|
|
|
Reader.Open(pStorage, pMapName, IStorage::TYPE_ABSOLUTE);
|
2015-08-21 21:02:50 +00:00
|
|
|
|
|
|
|
CDataFileWriter Writer;
|
|
|
|
Writer.Init();
|
|
|
|
|
|
|
|
int SettingsIndex = Reader.NumData();
|
2015-08-27 13:46:51 +00:00
|
|
|
bool FoundInfo = false;
|
2015-08-21 21:02:50 +00:00
|
|
|
for(int i = 0; i < Reader.NumItems(); i++)
|
|
|
|
{
|
|
|
|
int TypeID;
|
|
|
|
int ItemID;
|
|
|
|
int *pData = (int *)Reader.GetItem(i, &TypeID, &ItemID);
|
2017-08-28 16:06:19 +00:00
|
|
|
int Size = Reader.GetItemSize(i);
|
2015-08-21 21:02:50 +00:00
|
|
|
CMapItemInfoSettings MapInfo;
|
|
|
|
if(TypeID == MAPITEMTYPE_INFO && ItemID == 0)
|
|
|
|
{
|
2015-08-27 13:46:51 +00:00
|
|
|
FoundInfo = true;
|
2015-08-21 21:02:50 +00:00
|
|
|
CMapItemInfoSettings *pInfo = (CMapItemInfoSettings *)pData;
|
|
|
|
if(Size >= (int)sizeof(CMapItemInfoSettings))
|
|
|
|
{
|
|
|
|
MapInfo = *pInfo;
|
|
|
|
pData = (int *)&MapInfo;
|
|
|
|
Size = sizeof(MapInfo);
|
|
|
|
if(pInfo->m_Settings > -1)
|
|
|
|
{
|
|
|
|
SettingsIndex = pInfo->m_Settings;
|
|
|
|
char *pMapSettings = (char *)Reader.GetData(SettingsIndex);
|
2017-08-30 06:36:17 +00:00
|
|
|
int DataSize = Reader.GetDataSize(SettingsIndex);
|
2015-08-27 14:20:56 +00:00
|
|
|
if(DataSize == TotalLength && mem_comp(pSettings, pMapSettings, DataSize) == 0)
|
2015-08-21 21:02:50 +00:00
|
|
|
{
|
|
|
|
dbg_msg("config_store", "configs coincide, not updating map");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Reader.UnloadData(pInfo->m_Settings);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MapInfo = *pInfo;
|
|
|
|
MapInfo.m_Settings = SettingsIndex;
|
|
|
|
pData = (int *)&MapInfo;
|
|
|
|
Size = sizeof(MapInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*(CMapItemInfo *)&MapInfo = *(CMapItemInfo *)pInfo;
|
|
|
|
MapInfo.m_Settings = SettingsIndex;
|
|
|
|
pData = (int *)&MapInfo;
|
|
|
|
Size = sizeof(MapInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Writer.AddItem(TypeID, ItemID, Size, pData);
|
|
|
|
}
|
|
|
|
|
2015-08-27 13:46:51 +00:00
|
|
|
if(!FoundInfo)
|
|
|
|
{
|
|
|
|
CMapItemInfoSettings Info;
|
|
|
|
Info.m_Version = 1;
|
|
|
|
Info.m_Author = -1;
|
|
|
|
Info.m_MapVersion = -1;
|
|
|
|
Info.m_Credits = -1;
|
|
|
|
Info.m_License = -1;
|
|
|
|
Info.m_Settings = SettingsIndex;
|
|
|
|
Writer.AddItem(MAPITEMTYPE_INFO, 0, sizeof(Info), &Info);
|
|
|
|
}
|
|
|
|
|
2015-08-21 21:02:50 +00:00
|
|
|
for(int i = 0; i < Reader.NumData() || i == SettingsIndex; i++)
|
|
|
|
{
|
|
|
|
if(i == SettingsIndex)
|
|
|
|
{
|
|
|
|
Writer.AddData(TotalLength, pSettings);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unsigned char *pData = (unsigned char *)Reader.GetData(i);
|
2017-08-30 06:36:17 +00:00
|
|
|
int Size = Reader.GetDataSize(i);
|
2015-08-21 21:02:50 +00:00
|
|
|
Writer.AddData(Size, pData);
|
|
|
|
Reader.UnloadData(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader.Close();
|
2015-08-26 22:36:10 +00:00
|
|
|
if(!Writer.OpenFile(pStorage, pMapName))
|
2015-08-21 21:02:50 +00:00
|
|
|
{
|
|
|
|
dbg_msg("config_store", "couldn't open map file '%s' for writing", pMapName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Writer.Finish();
|
|
|
|
dbg_msg("config_store", "imported settings");
|
|
|
|
}
|
|
|
|
#include "config_common.h"
|