Skip to content

Commit

Permalink
Merge pull request ddnet#7008 from Robyt3/Datafile-String-Util
Browse files Browse the repository at this point in the history
Add functions for reading/writing strings from/to datafile, minor refactoring of datafile/map reader
  • Loading branch information
def- authored Oct 3, 2023
2 parents fb3c32a + 7acf2c1 commit 2e5ee82
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 101 deletions.
6 changes: 4 additions & 2 deletions src/engine/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class IMap : public IInterface
{
MACRO_INTERFACE("map", 0)
public:
virtual void *GetData(int Index) = 0;
virtual int GetDataSize(int Index) const = 0;
virtual void *GetData(int Index) = 0;
virtual void *GetDataSwapped(int Index) = 0;
virtual const char *GetDataString(int Index) = 0;
virtual void UnloadData(int Index) = 0;
virtual int NumData() const = 0;

virtual void *GetItem(int Index, int *pType = nullptr, int *pID = nullptr) = 0;
virtual int GetItemSize(int Index) = 0;
virtual void *GetItem(int Index, int *pType = nullptr, int *pID = nullptr) = 0;
virtual void GetType(int Type, int *pStart, int *pNum) = 0;
virtual int FindItemIndex(int Type, int ID) = 0;
virtual void *FindItem(int Type, int ID) = 0;
virtual int NumItems() const = 0;
};
Expand Down
28 changes: 25 additions & 3 deletions src/engine/shared/datafile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ int CDataFileReader::GetDataSize(int Index) const
return Size;
}

void *CDataFileReader::GetDataImpl(int Index, int Swap)
void *CDataFileReader::GetDataImpl(int Index, bool Swap)
{
if(!m_pDataFile)
{
Expand Down Expand Up @@ -394,16 +394,31 @@ void *CDataFileReader::GetDataImpl(int Index, int Swap)

void *CDataFileReader::GetData(int Index)
{
return GetDataImpl(Index, 0);
return GetDataImpl(Index, false);
}

void *CDataFileReader::GetDataSwapped(int Index)
{
return GetDataImpl(Index, 1);
return GetDataImpl(Index, true);
}

const char *CDataFileReader::GetDataString(int Index)
{
if(Index == -1)
return "";
const int DataSize = GetDataSize(Index);
if(!DataSize)
return nullptr;
const char *pData = static_cast<char *>(GetData(Index));
if(pData == nullptr || mem_has_null(pData, DataSize - 1) || pData[DataSize - 1] != '\0' || !str_utf8_check(pData))
return nullptr;
return pData;
}

void CDataFileReader::ReplaceData(int Index, char *pData, size_t Size)
{
dbg_assert(Index >= 0 && Index < m_pDataFile->m_Header.m_NumRawData, "Index invalid");

free(m_pDataFile->m_ppDataPtrs[Index]);
m_pDataFile->m_ppDataPtrs[Index] = pData;
m_pDataFile->m_pDataSizes[Index] = Size;
Expand Down Expand Up @@ -745,6 +760,13 @@ int CDataFileWriter::AddDataSwapped(int Size, const void *pData)
#endif
}

int CDataFileWriter::AddDataString(const char *pStr)
{
if(pStr[0] == '\0')
return -1;
return AddData(str_length(pStr) + 1, pStr);
}

void CDataFileWriter::Finish()
{
dbg_assert((bool)m_File, "file not open");
Expand Down
8 changes: 5 additions & 3 deletions src/engine/shared/datafile.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum
class CDataFileReader
{
struct CDatafile *m_pDataFile;
void *GetDataImpl(int Index, int Swap);
void *GetDataImpl(int Index, bool Swap);
int GetFileDataSize(int Index) const;

int GetExternalItemType(int InternalType);
Expand All @@ -42,15 +42,16 @@ class CDataFileReader
bool IsOpen() const { return m_pDataFile != nullptr; }
IOHANDLE File() const;

int GetDataSize(int Index) const;
void *GetData(int Index);
void *GetDataSwapped(int Index); // makes sure that the data is 32bit LE ints when saved
int GetDataSize(int Index) const;
const char *GetDataString(int Index);
void ReplaceData(int Index, char *pData, size_t Size); // memory for data must have been allocated with malloc
void UnloadData(int Index);
int NumData() const;

void *GetItem(int Index, int *pType = nullptr, int *pID = nullptr);
int GetItemSize(int Index) const;
void *GetItem(int Index, int *pType = nullptr, int *pID = nullptr);
void GetType(int Type, int *pStart, int *pNum);
int FindItemIndex(int Type, int ID);
void *FindItem(int Type, int ID);
Expand Down Expand Up @@ -136,6 +137,7 @@ class CDataFileWriter
bool Open(class IStorage *pStorage, const char *pFilename, int StorageType = IStorage::TYPE_SAVE);
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();
};
Expand Down
26 changes: 18 additions & 8 deletions src/engine/shared/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@

CMap::CMap() = default;

void *CMap::GetData(int Index)
int CMap::GetDataSize(int Index) const
{
return m_DataFile.GetData(Index);
return m_DataFile.GetDataSize(Index);
}

int CMap::GetDataSize(int Index) const
void *CMap::GetData(int Index)
{
return m_DataFile.GetDataSize(Index);
return m_DataFile.GetData(Index);
}

void *CMap::GetDataSwapped(int Index)
{
return m_DataFile.GetDataSwapped(Index);
}

const char *CMap::GetDataString(int Index)
{
return m_DataFile.GetDataString(Index);
}

void CMap::UnloadData(int Index)
{
m_DataFile.UnloadData(Index);
Expand All @@ -35,21 +40,26 @@ int CMap::NumData() const
return m_DataFile.NumData();
}

void *CMap::GetItem(int Index, int *pType, int *pID)
int CMap::GetItemSize(int Index)
{
return m_DataFile.GetItem(Index, pType, pID);
return m_DataFile.GetItemSize(Index);
}

int CMap::GetItemSize(int Index)
void *CMap::GetItem(int Index, int *pType, int *pID)
{
return m_DataFile.GetItemSize(Index);
return m_DataFile.GetItem(Index, pType, pID);
}

void CMap::GetType(int Type, int *pStart, int *pNum)
{
m_DataFile.GetType(Type, pStart, pNum);
}

int CMap::FindItemIndex(int Type, int ID)
{
return m_DataFile.FindItemIndex(Type, ID);
}

void *CMap::FindItem(int Type, int ID)
{
return m_DataFile.FindItem(Type, ID);
Expand Down
6 changes: 4 additions & 2 deletions src/engine/shared/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ class CMap : public IEngineMap

CDataFileReader *GetReader() { return &m_DataFile; }

void *GetData(int Index) override;
int GetDataSize(int Index) const override;
void *GetData(int Index) override;
void *GetDataSwapped(int Index) override;
const char *GetDataString(int Index) override;
void UnloadData(int Index) override;
int NumData() const override;

void *GetItem(int Index, int *pType = nullptr, int *pID = nullptr) override;
int GetItemSize(int Index) override;
void *GetItem(int Index, int *pType = nullptr, int *pID = nullptr) override;
void GetType(int Type, int *pStart, int *pNum) override;
int FindItemIndex(int Type, int ID) override;
void *FindItem(int Type, int ID) override;
int NumItems() const override;

Expand Down
37 changes: 19 additions & 18 deletions src/game/client/components/mapimages.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <base/log.h>

#include <engine/graphics.h>
#include <engine/map.h>
#include <engine/storage.h>
#include <engine/textrender.h>
#include <game/generated/client_data.h>
#include <game/mapitems.h>

#include <game/client/gameclient.h>
#include <game/generated/client_data.h>
#include <game/layers.h>
#include <game/mapitems.h>

#include "mapimages.h"

#include <game/client/gameclient.h>

const char *const gs_apModEntitiesNames[] = {
"ddnet",
"ddrace",
Expand Down Expand Up @@ -99,37 +100,37 @@ void CMapImages::OnMapLoadImpl(class CLayers *pLayers, IMap *pMap)
}
}

int TextureLoadFlag = Graphics()->HasTextureArrays() ? IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE : IGraphics::TEXLOAD_TO_3D_TEXTURE;
const int TextureLoadFlag = Graphics()->HasTextureArrays() ? IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE : IGraphics::TEXLOAD_TO_3D_TEXTURE;

// load new textures
for(int i = 0; i < m_Count; i++)
{
int LoadFlag = (((m_aTextureUsedByTileOrQuadLayerFlag[i] & 1) != 0) ? TextureLoadFlag : 0) | (((m_aTextureUsedByTileOrQuadLayerFlag[i] & 2) != 0) ? 0 : (Graphics()->IsTileBufferingEnabled() ? IGraphics::TEXLOAD_NO_2D_TEXTURE : 0));
const int LoadFlag = (((m_aTextureUsedByTileOrQuadLayerFlag[i] & 1) != 0) ? TextureLoadFlag : 0) | (((m_aTextureUsedByTileOrQuadLayerFlag[i] & 2) != 0) ? 0 : (Graphics()->IsTileBufferingEnabled() ? IGraphics::TEXLOAD_NO_2D_TEXTURE : 0));
const CMapItemImage_v2 *pImg = (CMapItemImage_v2 *)pMap->GetItem(Start + i);
const CImageInfo::EImageFormat Format = pImg->m_Version < CMapItemImage_v2::CURRENT_VERSION ? CImageInfo::FORMAT_RGBA : CImageInfo::ImageFormatFromInt(pImg->m_Format);

const char *pName = pMap->GetDataString(pImg->m_ImageName);
if(pName == nullptr || pName[0] == '\0')
{
log_error("mapimages", "Failed to load map image %d: failed to load name.", i);
continue;
}

if(pImg->m_External)
{
char aPath[IO_MAX_PATH_LENGTH];
char *pName = (char *)pMap->GetData(pImg->m_ImageName);
str_format(aPath, sizeof(aPath), "mapres/%s.png", pName);
m_aTextures[i] = Graphics()->LoadTexture(aPath, IStorage::TYPE_ALL, LoadFlag);
pMap->UnloadData(pImg->m_ImageName);
}
else if(Format != CImageInfo::FORMAT_RGBA)
{
m_aTextures[i] = Graphics()->InvalidTexture();
pMap->UnloadData(pImg->m_ImageName);
}
else
else if(Format == CImageInfo::FORMAT_RGBA)
{
void *pData = pMap->GetData(pImg->m_ImageData);
char *pName = (char *)pMap->GetData(pImg->m_ImageName);
char aTexName[128];
str_format(aTexName, sizeof(aTexName), "%s %s", "embedded:", pName);
char aTexName[IO_MAX_PATH_LENGTH];
str_format(aTexName, sizeof(aTexName), "embedded: %s", pName);
m_aTextures[i] = Graphics()->LoadTextureRaw(pImg->m_Width, pImg->m_Height, Format, pData, LoadFlag, aTexName);
pMap->UnloadData(pImg->m_ImageName);
pMap->UnloadData(pImg->m_ImageData);
}
pMap->UnloadData(pImg->m_ImageName);
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/game/client/components/mapsounds.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <base/log.h>

#include <engine/demo.h>
#include <engine/sound.h>

#include <game/client/components/camera.h>
#include <game/client/components/maplayers.h> // envelope
#include <game/client/components/sounds.h>

#include <game/client/gameclient.h>

#include <game/layers.h>
#include <game/mapitems.h>

Expand All @@ -30,15 +29,20 @@ void CMapSounds::OnMapLoad()
// load new samples
for(int i = 0; i < m_Count; i++)
{
m_aSounds[i] = 0;

CMapItemSound *pSound = (CMapItemSound *)pMap->GetItem(Start + i);
if(pSound->m_External)
{
const char *pName = pMap->GetDataString(pSound->m_SoundName);
if(pName == nullptr || pName[0] == '\0')
{
log_error("mapsounds", "Failed to load map sound %d: failed to load name.", i);
continue;
}

char aBuf[IO_MAX_PATH_LENGTH];
char *pName = (char *)pMap->GetData(pSound->m_SoundName);
str_format(aBuf, sizeof(aBuf), "mapres/%s.opus", pName);
m_aSounds[i] = Sound()->LoadOpus(aBuf);
pMap->UnloadData(pSound->m_SoundName);
}
else
{
Expand Down
Loading

0 comments on commit 2e5ee82

Please sign in to comment.