From f7722eb0164156f03e53ff97f6af6229f96c2479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 14 Oct 2023 11:40:18 +0200 Subject: [PATCH] Remove separate `CDataFileWriter::Init/OpenFile` functions Simplify usage of datafile writer by removing duplicate functions for opening file. --- src/engine/shared/datafile.cpp | 44 ++++++++++++++------------------- src/engine/shared/datafile.h | 4 +-- src/game/server/gamecontext.cpp | 3 +-- src/tools/config_store.cpp | 3 +-- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/engine/shared/datafile.cpp b/src/engine/shared/datafile.cpp index f0a972f0f..d0dba6105 100644 --- a/src/engine/shared/datafile.cpp +++ b/src/engine/shared/datafile.cpp @@ -596,9 +596,24 @@ int CDataFileReader::MapSize() const CDataFileWriter::CDataFileWriter() { m_File = 0; + m_pItemTypes = static_cast(calloc(MAX_ITEM_TYPES, sizeof(CItemTypeInfo))); + m_NumItemTypes = 0; + mem_zero(m_pItemTypes, sizeof(CItemTypeInfo) * MAX_ITEM_TYPES); + for(int i = 0; i < MAX_ITEM_TYPES; i++) + { + m_pItemTypes[i].m_First = -1; + m_pItemTypes[i].m_Last = -1; + } + m_pItems = static_cast(calloc(MAX_ITEMS, sizeof(CItemInfo))); + m_NumItems = 0; + m_pDatas = static_cast(calloc(MAX_DATAS, sizeof(CDataInfo))); + m_NumDatas = 0; + + mem_zero(m_aExtendedItemTypes, sizeof(m_aExtendedItemTypes)); + m_NumExtendedItemTypes = 0; } CDataFileWriter::~CDataFileWriter() @@ -634,34 +649,11 @@ CDataFileWriter::~CDataFileWriter() } } -bool CDataFileWriter::OpenFile(class IStorage *pStorage, const char *pFilename, int StorageType) -{ - dbg_assert(!m_File, "a file already exists"); - m_File = pStorage->OpenFile(pFilename, IOFLAG_WRITE, StorageType); - return m_File != 0; -} - -void CDataFileWriter::Init() -{ - dbg_assert(!m_File, "a file already exists"); - m_NumItems = 0; - m_NumDatas = 0; - m_NumItemTypes = 0; - m_NumExtendedItemTypes = 0; - mem_zero(m_pItemTypes, sizeof(CItemTypeInfo) * MAX_ITEM_TYPES); - mem_zero(m_aExtendedItemTypes, sizeof(m_aExtendedItemTypes)); - - for(int i = 0; i < MAX_ITEM_TYPES; i++) - { - m_pItemTypes[i].m_First = -1; - m_pItemTypes[i].m_Last = -1; - } -} - bool CDataFileWriter::Open(class IStorage *pStorage, const char *pFilename, int StorageType) { - Init(); - return OpenFile(pStorage, pFilename, StorageType); + dbg_assert(!m_File, "File already open"); + m_File = pStorage->OpenFile(pFilename, IOFLAG_WRITE, StorageType); + return m_File != 0; } int CDataFileWriter::GetTypeFromIndex(int Index) const diff --git a/src/engine/shared/datafile.h b/src/engine/shared/datafile.h index e5649f17a..8fd95849e 100644 --- a/src/engine/shared/datafile.h +++ b/src/engine/shared/datafile.h @@ -132,13 +132,11 @@ public: } ~CDataFileWriter(); - void Init(); - bool OpenFile(class IStorage *pStorage, const char *pFilename, int StorageType = IStorage::TYPE_SAVE); bool Open(class IStorage *pStorage, const char *pFilename, int StorageType = IStorage::TYPE_SAVE); + int AddItem(int Type, int ID, int Size, const void *pData); int AddData(int Size, const void *pData, int CompressionLevel = Z_DEFAULT_COMPRESSION); int AddDataSwapped(int Size, const void *pData); int AddDataString(const char *pStr); - int AddItem(int Type, int ID, int Size, const void *pData); void Finish(); }; diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 9c102fec3..926318014 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -3866,7 +3866,6 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) Reader.Open(Storage(), pNewMapName, IStorage::TYPE_ALL); CDataFileWriter Writer; - Writer.Init(); int SettingsIndex = Reader.NumData(); bool FoundInfo = false; @@ -3944,7 +3943,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) free(pSettings); Reader.Close(); char aTemp[IO_MAX_PATH_LENGTH]; - Writer.OpenFile(Storage(), IStorage::FormatTmpPath(aTemp, sizeof(aTemp), pNewMapName)); + Writer.Open(Storage(), IStorage::FormatTmpPath(aTemp, sizeof(aTemp), pNewMapName)); Writer.Finish(); str_copy(pNewMapName, aTemp, MapNameSize); diff --git a/src/tools/config_store.cpp b/src/tools/config_store.cpp index 9a6f2eb3c..0c06d0589 100644 --- a/src/tools/config_store.cpp +++ b/src/tools/config_store.cpp @@ -45,7 +45,6 @@ void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName) Reader.Open(pStorage, pMapName, IStorage::TYPE_ABSOLUTE); CDataFileWriter Writer; - Writer.Init(); int SettingsIndex = Reader.NumData(); bool FoundInfo = false; @@ -123,7 +122,7 @@ void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName) free(pSettings); Reader.Close(); - if(!Writer.OpenFile(pStorage, pMapName)) + if(!Writer.Open(pStorage, pMapName)) { dbg_msg("config_store", "couldn't open map file '%s' for writing", pMapName); return;