diff options
| author | oy <Tom_Adams@web.de> | 2010-10-06 23:07:35 +0200 |
|---|---|---|
| committer | oy <Tom_Adams@web.de> | 2010-10-06 23:07:35 +0200 |
| commit | 3a98f7a048c044ce0d870320fc0b1def0db4f7fb (patch) | |
| tree | 7dfa1826e537355b37d00e77558ca623a915bac2 /src/engine | |
| parent | a62a7413d1ff25c8e07657b96a485f001004619a (diff) | |
| download | zcatch-3a98f7a048c044ce0d870320fc0b1def0db4f7fb.tar.gz zcatch-3a98f7a048c044ce0d870320fc0b1def0db4f7fb.zip | |
added mod system. Closes #34
Diffstat (limited to 'src/engine')
| -rw-r--r-- | src/engine/client.h | 5 | ||||
| -rw-r--r-- | src/engine/client/client.cpp | 17 | ||||
| -rw-r--r-- | src/engine/client/client.h | 4 | ||||
| -rw-r--r-- | src/engine/client/graphics.cpp | 12 | ||||
| -rw-r--r-- | src/engine/client/graphics.h | 4 | ||||
| -rw-r--r-- | src/engine/client/sound.cpp | 2 | ||||
| -rw-r--r-- | src/engine/client/text.cpp | 2 | ||||
| -rw-r--r-- | src/engine/graphics.h | 4 | ||||
| -rw-r--r-- | src/engine/server/server.cpp | 2 | ||||
| -rw-r--r-- | src/engine/shared/config.cpp | 2 | ||||
| -rw-r--r-- | src/engine/shared/console.cpp | 2 | ||||
| -rw-r--r-- | src/engine/shared/datafile.cpp | 6 | ||||
| -rw-r--r-- | src/engine/shared/datafile.h | 2 | ||||
| -rw-r--r-- | src/engine/shared/demo.cpp | 14 | ||||
| -rw-r--r-- | src/engine/shared/demo.h | 2 | ||||
| -rw-r--r-- | src/engine/shared/map.cpp | 2 | ||||
| -rw-r--r-- | src/engine/shared/masterserver.cpp | 4 | ||||
| -rw-r--r-- | src/engine/shared/storage.cpp | 290 | ||||
| -rw-r--r-- | src/engine/storage.h | 13 |
19 files changed, 229 insertions, 160 deletions
diff --git a/src/engine/client.h b/src/engine/client.h index fe8e91f2..11ba7385 100644 --- a/src/engine/client.h +++ b/src/engine/client.h @@ -73,7 +73,7 @@ public: virtual void Connect(const char *pAddress) = 0; virtual void Disconnect() = 0; virtual void Quit() = 0; - virtual const char *DemoPlayer_Play(const char *pFilename) = 0; + virtual const char *DemoPlayer_Play(const char *pFilename, int StorageType) = 0; virtual void DemoRecorder_Start(const char *pFilename) = 0; // networking @@ -121,9 +121,6 @@ public: return SendMsg(&Packer, Flags); } - // - virtual const char *UserDirectory() = 0; - // virtual const char *ErrorString() = 0; virtual const char *LatestVersion() = 0; diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 4952446f..7c0c632e 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -585,7 +585,7 @@ void CClient::ServerInfoRequest() int CClient::LoadData() { - m_DebugFont = Graphics()->LoadTexture("debug_font.png", CImageInfo::FORMAT_AUTO, IGraphics::TEXLOAD_NORESAMPLE); + m_DebugFont = Graphics()->LoadTexture("debug_font.png", IStorage::TYPE_ALL, CImageInfo::FORMAT_AUTO, IGraphics::TEXLOAD_NORESAMPLE); return 1; } @@ -990,7 +990,7 @@ void CClient::ProcessPacket(CNetChunk *pPacket) m_MapdownloadChunk = 0; str_copy(m_aMapdownloadName, pMap, sizeof(m_aMapdownloadName)); - m_MapdownloadFile = Storage()->OpenFile(m_aMapdownloadFilename, IOFLAG_WRITE); + m_MapdownloadFile = Storage()->OpenFile(m_aMapdownloadFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); m_MapdownloadCrc = MapCrc; m_MapdownloadTotalsize = -1; m_MapdownloadAmount = 0; @@ -1537,13 +1537,6 @@ void CClient::Update() m_ServerBrowser.Update(); } -const char *CClient::UserDirectory() -{ - static char saPath[1024] = {0}; - fs_storage_path("Teeworlds", saPath, sizeof(saPath)); - return saPath; -} - void CClient::VersionUpdate() { if(m_VersionInfo.m_State == 0) @@ -1871,7 +1864,7 @@ void CClient::Con_AddFavorite(IConsole::IResult *pResult, void *pUserData) pSelf->m_ServerBrowser.AddFavorite(Addr); } -const char *CClient::DemoPlayer_Play(const char *pFilename) +const char *CClient::DemoPlayer_Play(const char *pFilename, int StorageType) { int Crc; const char *pError; @@ -1881,7 +1874,7 @@ const char *CClient::DemoPlayer_Play(const char *pFilename) // try to start playback m_DemoPlayer.SetListner(this); - if(m_DemoPlayer.Load(Storage(), m_pConsole, pFilename)) + if(m_DemoPlayer.Load(Storage(), m_pConsole, pFilename, StorageType)) return "error loading demo"; // load map @@ -1926,7 +1919,7 @@ const char *CClient::DemoPlayer_Play(const char *pFilename) void CClient::Con_Play(IConsole::IResult *pResult, void *pUserData) { CClient *pSelf = (CClient *)pUserData; - pSelf->DemoPlayer_Play(pResult->GetString(0)); + pSelf->DemoPlayer_Play(pResult->GetString(0), IStorage::TYPE_ALL); } void CClient::DemoRecorder_Start(const char *pFilename) diff --git a/src/engine/client/client.h b/src/engine/client/client.h index aa381467..d9199b94 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -259,8 +259,6 @@ public: void Update(); - virtual const char *UserDirectory(); - void InitEngine(const char *pAppname); void RegisterInterfaces(); void InitInterfaces(); @@ -283,7 +281,7 @@ public: void RegisterCommands(); - const char *DemoPlayer_Play(const char *pFilename); + const char *DemoPlayer_Play(const char *pFilename, int StorageType); void DemoRecorder_Start(const char *pFilename); virtual class CEngine *Engine() { return &m_Engine; } diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index 14a88394..665f9ce7 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -354,7 +354,7 @@ int CGraphics_OpenGL::LoadTextureRaw(int Width, int Height, int Format, const vo } // simple uncompressed RGBA loaders -int CGraphics_OpenGL::LoadTexture(const char *pFilename, int StoreFormat, int Flags) +int CGraphics_OpenGL::LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags) { int l = str_length(pFilename); int Id; @@ -362,7 +362,7 @@ int CGraphics_OpenGL::LoadTexture(const char *pFilename, int StoreFormat, int Fl if(l < 3) return -1; - if(LoadPNG(&Img, pFilename)) + if(LoadPNG(&Img, pFilename, StorageType)) { if (StoreFormat == CImageInfo::FORMAT_AUTO) StoreFormat = Img.m_Format; @@ -375,7 +375,7 @@ int CGraphics_OpenGL::LoadTexture(const char *pFilename, int StoreFormat, int Fl return m_InvalidTexture; } -int CGraphics_OpenGL::LoadPNG(CImageInfo *pImg, const char *pFilename) +int CGraphics_OpenGL::LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType) { char aCompleteFilename[512]; unsigned char *pBuffer; @@ -384,7 +384,7 @@ int CGraphics_OpenGL::LoadPNG(CImageInfo *pImg, const char *pFilename) // open file for reading png_init(0,0); // ignore_convention - IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, aCompleteFilename, sizeof(aCompleteFilename)); + IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType, aCompleteFilename, sizeof(aCompleteFilename)); if(File) io_close(File); else @@ -450,7 +450,7 @@ void CGraphics_OpenGL::ScreenshotDirect(const char *pFilename) char aWholePath[1024]; png_t Png; // ignore_convention - IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_WRITE, aWholePath, sizeof(aWholePath)); + IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE, aWholePath, sizeof(aWholePath)); if(File) io_close(File); @@ -898,7 +898,7 @@ void CGraphics_SDL::Swap() { IOHANDLE io; str_format(aFilename, sizeof(aFilename), "screenshots/screenshot%s-%05d.png", aDate, Index); - io = m_pStorage->OpenFile(aFilename, IOFLAG_READ); + io = m_pStorage->OpenFile(aFilename, IOFLAG_READ, IStorage::TYPE_SAVE); if(io) io_close(io); else diff --git a/src/engine/client/graphics.h b/src/engine/client/graphics.h index fa360a22..ef6360e8 100644 --- a/src/engine/client/graphics.h +++ b/src/engine/client/graphics.h @@ -87,8 +87,8 @@ public: virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags); // simple uncompressed RGBA loaders - virtual int LoadTexture(const char *pFilename, int StoreFormat, int Flags); - virtual int LoadPNG(CImageInfo *pImg, const char *pFilename); + virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags); + virtual int LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType); void ScreenshotDirect(const char *pFilename); diff --git a/src/engine/client/sound.cpp b/src/engine/client/sound.cpp index b829686c..69fd7462 100644 --- a/src/engine/client/sound.cpp +++ b/src/engine/client/sound.cpp @@ -325,7 +325,7 @@ int CSound::LoadWV(const char *pFilename) if(!m_pStorage) return -1; - ms_File = m_pStorage->OpenFile(pFilename, IOFLAG_READ); // TODO: use system.h stuff for this + ms_File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, IStorage::TYPE_ALL); if(!ms_File) { dbg_msg("sound/wv", "failed to open %s", pFilename); diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index bdf71dbb..0fb6b043 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -70,7 +70,7 @@ struct CFontSizeData class CFont { public: - char m_aFilename[128]; + char m_aFilename[512]; FT_Face m_FtFace; CFontSizeData m_aSizes[NUM_FONT_SIZES]; }; diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 4d3c0bc0..cbd7bbff 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -74,10 +74,10 @@ public: virtual void BlendAdditive() = 0; virtual int MemoryUsage() const = 0; - virtual int LoadPNG(CImageInfo *pImg, const char *pFilename) =0; + virtual int LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType) =0; virtual int UnloadTexture(int Index) = 0; virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags) = 0; - virtual int LoadTexture(const char *pFilename, int StoreFormat, int Flags) = 0; + virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags) = 0; virtual void TextureSet(int TextureID) = 0; struct CLineItem diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 37d5770a..c7b728d4 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -1043,7 +1043,7 @@ int CServer::LoadMap(const char *pMapName) // load compelate map into memory for download { - IOHANDLE File = Storage()->OpenFile(aBuf, IOFLAG_READ); + IOHANDLE File = Storage()->OpenFile(aBuf, IOFLAG_READ, IStorage::TYPE_ALL); m_CurrentMapSize = (int)io_length(File); if(m_pCurrentMapData) mem_free(m_pCurrentMapData); diff --git a/src/engine/shared/config.cpp b/src/engine/shared/config.cpp index 4ff02689..85c6b5ec 100644 --- a/src/engine/shared/config.cpp +++ b/src/engine/shared/config.cpp @@ -74,7 +74,7 @@ public: { if(!m_pStorage) return; - m_ConfigFile = m_pStorage->OpenFile("settings.cfg", IOFLAG_WRITE); + m_ConfigFile = m_pStorage->OpenFile("settings.cfg", IOFLAG_WRITE, IStorage::TYPE_SAVE); if(!m_ConfigFile) return; diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp index 808b64ed..e32a0f81 100644 --- a/src/engine/shared/console.cpp +++ b/src/engine/shared/console.cpp @@ -300,7 +300,7 @@ void CConsole::ExecuteFile(const char *pFilename) m_pFirstExec = &ThisFile; // exec the file - IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ); + IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, IStorage::TYPE_ALL); char aBuf[256]; if(File) diff --git a/src/engine/shared/datafile.cpp b/src/engine/shared/datafile.cpp index 4ef278cd..81d0e2cf 100644 --- a/src/engine/shared/datafile.cpp +++ b/src/engine/shared/datafile.cpp @@ -65,11 +65,11 @@ struct CDatafile char *m_pData; }; -bool CDataFileReader::Open(class IStorage *pStorage, const char *pFilename) +bool CDataFileReader::Open(class IStorage *pStorage, const char *pFilename, int StorageType) { dbg_msg("datafile", "loading. filename='%s'", pFilename); - IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ); + IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType); if(!File) { dbg_msg("datafile", "could not open '%s'", pFilename); @@ -394,7 +394,7 @@ unsigned CDataFileReader::Crc() bool CDataFileWriter::Open(class IStorage *pStorage, const char *pFilename) { dbg_assert(!m_File, "a file already exists"); - m_File = pStorage->OpenFile(pFilename, IOFLAG_WRITE); + m_File = pStorage->OpenFile(pFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); if(!m_File) return false; diff --git a/src/engine/shared/datafile.h b/src/engine/shared/datafile.h index e0f08b7c..0392e493 100644 --- a/src/engine/shared/datafile.h +++ b/src/engine/shared/datafile.h @@ -12,7 +12,7 @@ public: bool IsOpen() const { return m_pDataFile != 0; } - bool Open(class IStorage *pStorage, const char *pFilename); + bool Open(class IStorage *pStorage, const char *pFilename, int StorageType); bool Close(); void *GetData(int Index); diff --git a/src/engine/shared/demo.cpp b/src/engine/shared/demo.cpp index 5ae7a340..4ae4a4c5 100644 --- a/src/engine/shared/demo.cpp +++ b/src/engine/shared/demo.cpp @@ -40,12 +40,12 @@ int CDemoRecorder::Start(class IStorage *pStorage, class IConsole *pConsole, con char aMapFilename[128]; // try the normal maps folder str_format(aMapFilename, sizeof(aMapFilename), "maps/%s.map", pMap); - IOHANDLE MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ); + IOHANDLE MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ, IStorage::TYPE_ALL); if(!MapFile) { // try the downloaded maps str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%08x.map", pMap, Crc); - MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ); + MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ, IStorage::TYPE_ALL); } if(!MapFile) { @@ -55,7 +55,7 @@ int CDemoRecorder::Start(class IStorage *pStorage, class IConsole *pConsole, con return -1; } - m_File = pStorage->OpenFile(pFilename, IOFLAG_WRITE); + m_File = pStorage->OpenFile(pFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); if(!m_File) { io_close(MapFile); @@ -513,10 +513,10 @@ void CDemoPlayer::Unpause() } } -int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename) +int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType) { m_pConsole = pConsole; - m_File = pStorage->OpenFile(pFilename, IOFLAG_READ); + m_File = pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType); if(!m_File) { char aBuf[256]; @@ -566,7 +566,7 @@ int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const int Crc = (m_Info.m_Header.m_aCrc[0]<<24) | (m_Info.m_Header.m_aCrc[1]<<16) | (m_Info.m_Header.m_aCrc[2]<<8) | (m_Info.m_Header.m_aCrc[3]); char aMapFilename[128]; str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%08x.map", m_Info.m_Header.m_aMap, Crc); - IOHANDLE MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ); + IOHANDLE MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ, IStorage::TYPE_ALL); if(MapFile) { @@ -580,7 +580,7 @@ int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const io_read(m_File, pMapData, MapSize); // save map - MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_WRITE); + MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); io_write(MapFile, pMapData, MapSize); io_close(MapFile); diff --git a/src/engine/shared/demo.h b/src/engine/shared/demo.h index eeaa1f99..9d510542 100644 --- a/src/engine/shared/demo.h +++ b/src/engine/shared/demo.h @@ -103,7 +103,7 @@ public: void SetListner(IListner *pListner); - int Load(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename); + int Load(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType); int Play(); void Pause(); void Unpause(); diff --git a/src/engine/shared/map.cpp b/src/engine/shared/map.cpp index 505d18e9..827930aa 100644 --- a/src/engine/shared/map.cpp +++ b/src/engine/shared/map.cpp @@ -28,7 +28,7 @@ public: IStorage *pStorage = Kernel()->RequestInterface<IStorage>(); if(!pStorage) return false; - return m_DataFile.Open(pStorage, pMapName); + return m_DataFile.Open(pStorage, pMapName, IStorage::TYPE_ALL); } virtual bool IsLoaded() diff --git a/src/engine/shared/masterserver.cpp b/src/engine/shared/masterserver.cpp index f10469bd..9ecf3f71 100644 --- a/src/engine/shared/masterserver.cpp +++ b/src/engine/shared/masterserver.cpp @@ -119,7 +119,7 @@ public: return -1; // try to open file - File = pStorage->OpenFile("masters.cfg", IOFLAG_READ); + File = pStorage->OpenFile("masters.cfg", IOFLAG_READ, IStorage::TYPE_SAVE); if(!File) return -1; @@ -165,7 +165,7 @@ public: return -1; // try to open file - File = pStorage->OpenFile("masters.cfg", IOFLAG_WRITE); + File = pStorage->OpenFile("masters.cfg", IOFLAG_WRITE, IStorage::TYPE_SAVE); if(!File) return -1; diff --git a/src/engine/shared/storage.cpp b/src/engine/shared/storage.cpp index 8785a5bf..cfe64e4b 100644 --- a/src/engine/shared/storage.cpp +++ b/src/engine/shared/storage.cpp @@ -3,6 +3,7 @@ #include <base/system.h> #include <engine/storage.h> #include "engine.h" +#include "linereader.h" // compiled-in data-dir path #define DATA_DIR "data" @@ -10,36 +11,29 @@ class CStorage : public IStorage { public: - char m_aApplicationSavePath[512]; - char m_aDatadir[512]; + enum + { + MAX_PATHS = 16, + MAX_PATH_LENGTH = 512 + }; + + char m_aaStoragePaths[MAX_PATHS][MAX_PATH_LENGTH]; + int m_NumPaths; + char m_aDatadir[MAX_PATH_LENGTH]; + char m_aUserdir[MAX_PATH_LENGTH]; CStorage() { - m_aApplicationSavePath[0] = 0; + mem_zero(m_aaStoragePaths, sizeof(m_aaStoragePaths)); + m_NumPaths = 0; m_aDatadir[0] = 0; + m_aUserdir[0] = 0; } int Init(const char *pApplicationName, int NumArgs, const char **ppArguments) { - char aPath[1024] = {0}; - fs_storage_path(pApplicationName, m_aApplicationSavePath, sizeof(m_aApplicationSavePath)); - if(fs_makedir(m_aApplicationSavePath) == 0) - { - str_format(aPath, sizeof(aPath), "%s/screenshots", m_aApplicationSavePath); - fs_makedir(aPath); - - str_format(aPath, sizeof(aPath), "%s/maps", m_aApplicationSavePath); - fs_makedir(aPath); - - str_format(aPath, sizeof(aPath), "%s/dumps", m_aApplicationSavePath); - fs_makedir(aPath); - - str_format(aPath, sizeof(aPath), "%s/downloadedmaps", m_aApplicationSavePath); - fs_makedir(aPath); - - str_format(aPath, sizeof(aPath), "%s/demos", m_aApplicationSavePath); - fs_makedir(aPath); - } + // get userdir + fs_storage_path(pApplicationName, m_aUserdir, sizeof(m_aUserdir)); // check for datadir override for(int i = 1; i < NumArgs; i++) @@ -50,54 +44,160 @@ public: break; } } - - return FindDatadir(ppArguments[0]); + // get datadir + FindDatadir(ppArguments[0]); + + // load paths from storage.cfg + LoadPaths(ppArguments[0]); + + if(!m_NumPaths) + { + dbg_msg("storage", "using standard paths"); + AddDefaultPaths(); + } + + // add save directories + if(m_NumPaths && (!m_aaStoragePaths[TYPE_SAVE][0] || !fs_makedir(m_aaStoragePaths[TYPE_SAVE]))) + { + char aPath[MAX_PATH_LENGTH]; + fs_makedir(GetPath(TYPE_SAVE, "screenshots", aPath, sizeof(aPath))); + fs_makedir(GetPath(TYPE_SAVE, "maps", aPath, sizeof(aPath))); + fs_makedir(GetPath(TYPE_SAVE, "dumps", aPath, sizeof(aPath))); + fs_makedir(GetPath(TYPE_SAVE, "downloadedmaps", aPath, sizeof(aPath))); + fs_makedir(GetPath(TYPE_SAVE, "demos", aPath, sizeof(aPath))); + } + + return m_NumPaths ? 0 : 1; + } + + void LoadPaths(const char *pArgv0) + { + // check current directory + IOHANDLE File = io_open("storage.cfg", IOFLAG_READ); + if(!File) + { + // check usable path in argv[0] + unsigned int Pos = ~0U; + for(unsigned i = 0; pArgv0[i]; i++) + if(pArgv0[i] == '/' || pArgv0[i] == '\\') + Pos = i; + if(Pos < MAX_PATH_LENGTH) + { + char aBuffer[MAX_PATH_LENGTH]; + str_copy(aBuffer, pArgv0, Pos+1); + str_append(aBuffer, "/storage.cfg", sizeof(aBuffer)); + File = io_open(aBuffer, IOFLAG_READ); + } + + if(Pos >= MAX_PATH_LENGTH || !File) + { + dbg_msg("storage", "couldn't open storage.cfg"); + return; + } + } + + char *pLine; + CLineReader LineReader; + LineReader.Init(File); + + while((pLine = LineReader.Get())) + { + if(str_length(pLine) > 9 && !str_comp_num(pLine, "add_path ", 9)) + AddPath(pLine+9); + } + + io_close(File); + + if(!m_NumPaths) + dbg_msg("storage", "no paths found in storage.cfg"); + } + + void AddDefaultPaths() + { + AddPath("$USERDIR"); + AddPath("$DATADIR"); + AddPath("$CURRENTDIR"); + } + + void AddPath(const char *pPath) + { + if(m_NumPaths >= MAX_PATHS || !pPath[0]) + return; + + int OldNum = m_NumPaths; + + if(!str_comp(pPath, "$USERDIR")) + { + if(m_aUserdir[0]) + str_copy(m_aaStoragePaths[m_NumPaths++], m_aUserdir, MAX_PATH_LENGTH); + } + else if(!str_comp(pPath, "$DATADIR")) + { + if(m_aDatadir[0]) + str_copy(m_aaStoragePaths[m_NumPaths++], m_aDatadir, MAX_PATH_LENGTH); + } + else if(!str_comp(pPath, "$CURRENTDIR")) + { + m_aaStoragePaths[m_NumPaths++][0] = 0; + } + else + { + if(fs_is_dir(pPath)) + str_copy(m_aaStoragePaths[m_NumPaths++], pPath, MAX_PATH_LENGTH); + } + + if(OldNum != m_NumPaths) + dbg_msg("storage", "added path '%s'", pPath); } - int FindDatadir(const char *pArgv0) + void FindDatadir(const char *pArgv0) { // 1) use provided data-dir override if(m_aDatadir[0]) { - if(fs_is_dir(m_aDatadir)) - return 0; - else + char aBuffer[MAX_PATH_LENGTH]; + str_format(aBuffer, sizeof(aBuffer), "%s/mapres", m_aDatadir); + if(!fs_is_dir(aBuffer)) { - dbg_msg("engine/datadir", "specified data-dir '%s' does not exist", m_aDatadir); - return -1; + dbg_msg("storage", "specified data directory '%s' does not exist", m_aDatadir); + m_aDatadir[0] = 0; } + else + return; } // 2) use data-dir in PWD if present if(fs_is_dir("data/mapres")) { str_copy(m_aDatadir, "data", sizeof(m_aDatadir)); - return 0; + return; } // 3) use compiled-in data-dir if present - if (fs_is_dir(DATA_DIR "/mapres")) + if(fs_is_dir(DATA_DIR "/mapres")) { str_copy(m_aDatadir, DATA_DIR, sizeof(m_aDatadir)); - return 0; + return; } // 4) check for usable path in argv[0] { unsigned int Pos = ~0U; for(unsigned i = 0; pArgv0[i]; i++) - if(pArgv0[i] == '/') + if(pArgv0[i] == '/' || pArgv0[i] == '\\') Pos = i; - if (Pos < sizeof(m_aDatadir)) + if(Pos < MAX_PATH_LENGTH) { - char aBaseDir[sizeof(m_aDatadir)]; - str_copy(aBaseDir, pArgv0, Pos); - aBaseDir[Pos] = '\0'; + char aBaseDir[MAX_PATH_LENGTH]; + str_copy(aBaseDir, pArgv0, Pos+1); str_format(m_aDatadir, sizeof(m_aDatadir), "%s/data", aBaseDir); + str_append(aBaseDir, "/data/mapres", sizeof(aBaseDir)); - if (fs_is_dir(m_aDatadir)) - return 0; + if(fs_is_dir(aBaseDir)) + return; + else + m_aDatadir[0] = 0; } } @@ -105,11 +205,11 @@ public: // 5) check for all default locations { const char *aDirs[] = { - "/usr/share/teeworlds/data", - "/usr/share/games/teeworlds/data", - "/usr/local/share/teeworlds/data", - "/usr/local/share/games/teeworlds/data", - "/opt/teeworlds/data" + "/usr/share/teeworlds/data/mapres", + "/usr/share/games/teeworlds/data/mapres", + "/usr/local/share/teeworlds/data/mapres", + "/usr/local/share/games/teeworlds/data/mapres", + "/opt/teeworlds/data/mapres" }; const int DirsCount = sizeof(aDirs) / sizeof(aDirs[0]); @@ -119,55 +219,41 @@ public: if (fs_is_dir(aDirs[i])) { str_copy(m_aDatadir, aDirs[i], sizeof(m_aDatadir)); - return 0; + return; } } } #endif // no data-dir found - dbg_msg("engine/datadir", "warning no data directory found"); - return -1; + dbg_msg("storage", "warning no data directory found"); } - virtual void ListDirectory(int Types, const char *pPath, FS_LISTDIR_CALLBACK pfnCallback, void *pUser) + virtual void ListDirectory(int Type, const char *pPath, FS_LISTDIR_CALLBACK pfnCallback, void *pUser) { - char aBuffer[1024]; - - // list current directory - if(Types&TYPE_CURRENT) + char aBuffer[MAX_PATH_LENGTH]; + if(Type == TYPE_ALL) { - fs_listdir(pPath, pfnCallback, TYPE_CURRENT, pUser); + // list all available directories + for(int i = 0; i < m_NumPaths; ++i) + fs_listdir(GetPath(i, pPath, aBuffer, sizeof(aBuffer)), pfnCallback, i, pUser); } - - // list users directory - if(Types&TYPE_SAVE) + else if(Type >= 0 && Type < m_NumPaths) { - str_format(aBuffer, sizeof(aBuffer), "%s/%s", m_aApplicationSavePath, pPath); - fs_listdir(aBuffer, pfnCallback, TYPE_SAVE, pUser); + // list wanted directory + fs_listdir(GetPath(Type, pPath, aBuffer, sizeof(aBuffer)), pfnCallback, Type, pUser); } - - // list datadir directory - if(Types&TYPE_DATA) - { - str_format(aBuffer, sizeof(aBuffer), "%s/%s", m_aDatadir, pPath); - fs_listdir(aBuffer, pfnCallback, TYPE_DATA, pUser); - } } - virtual const char *GetDirectory(int Type) const + const char *GetPath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize) { - switch(Type) - { - case TYPE_SAVE: return m_aApplicationSavePath; - case TYPE_DATA: return m_aDatadir; - default: return ""; - } + str_format(pBuffer, BufferSize, "%s%s%s", m_aaStoragePaths[Type], !m_aaStoragePaths[Type][0] ? "" : "/", pDir); + return pBuffer; } - virtual IOHANDLE OpenFile(const char *pFilename, int Flags, char *pBuffer = 0, int BufferSize = 0) + virtual IOHANDLE OpenFile(const char *pFilename, int Flags, int Type, char *pBuffer = 0, int BufferSize = 0) { - char aBuffer[1024]; + char aBuffer[MAX_PATH_LENGTH]; if(!pBuffer) { pBuffer = aBuffer; @@ -176,45 +262,42 @@ public: if(Flags&IOFLAG_WRITE) { - str_format(pBuffer, BufferSize, "%s/%s", m_aApplicationSavePath, pFilename); - return io_open(pBuffer, Flags); + return io_open(GetPath(TYPE_SAVE, pFilename, pBuffer, BufferSize), Flags); } else { IOHANDLE Handle = 0; - - // check current directory - Handle = io_open(pFilename, Flags); - if(Handle) - return Handle; - - // check user directory - str_format(pBuffer, BufferSize, "%s/%s", m_aApplicationSavePath, pFilename); - Handle = io_open(pBuffer, Flags); - if(Handle) - return Handle; - - // check normal data directory - str_format(pBuffer, BufferSize, "%s/%s", m_aDatadir, pFilename); - Handle = io_open(pBuffer, Flags); - if(Handle) - return Handle; + + if(Type == TYPE_ALL) + { + // check all available directories + for(int i = 0; i < m_NumPaths; ++i) + { + Handle = io_open(GetPath(i, pFilename, pBuffer, BufferSize), Flags); + if(Handle) + return Handle; + } + } + else if(Type >= 0 && Type < m_NumPaths) + { + // check wanted directory + Handle = io_open(GetPath(Type, pFilename, pBuffer, BufferSize), Flags); + if(Handle) + return Handle; + } } pBuffer[0] = 0; return 0; } - virtual bool RemoveFile(const char *pFilename) + virtual bool RemoveFile(const char *pFilename, int Type) { - char aBuffer[1024]; - str_format(aBuffer, sizeof(aBuffer), "%s/%s", m_aApplicationSavePath, pFilename); - bool Fail = remove(aBuffer); - - if(Fail) - Fail = remove(pFilename); - - return !Fail; + if(Type < 0 || Type >= m_NumPaths) + return false; + + char aBuffer[MAX_PATH_LENGTH]; + return remove(GetPath(Type, pFilename, aBuffer, sizeof(aBuffer))); } static IStorage *Create(const char *pApplicationName, int NumArgs, const char **ppArguments) @@ -222,6 +305,7 @@ public: CStorage *p = new CStorage(); if(p && p->Init(pApplicationName, NumArgs, ppArguments)) { + dbg_msg("storage", "initialisation failed"); delete p; p = 0; } diff --git a/src/engine/storage.h b/src/engine/storage.h index b9b02239..c483c52d 100644 --- a/src/engine/storage.h +++ b/src/engine/storage.h @@ -9,16 +9,13 @@ class IStorage : public IInterface public: enum { - TYPE_SAVE = 1, - TYPE_CURRENT = 2, - TYPE_DATA = 4, - TYPE_ALL = ~0 + TYPE_SAVE = 0, + TYPE_ALL = -1 }; - virtual void ListDirectory(int Types, const char *pPath, FS_LISTDIR_CALLBACK pfnCallback, void *pUser) = 0; - virtual const char *GetDirectory(int Type) const = 0; - virtual IOHANDLE OpenFile(const char *pFilename, int Flags, char *pBuffer = 0, int BufferSize = 0) = 0; - virtual bool RemoveFile(const char *pFilename) = 0; + virtual void ListDirectory(int Type, const char *pPath, FS_LISTDIR_CALLBACK pfnCallback, void *pUser) = 0; + virtual IOHANDLE OpenFile(const char *pFilename, int Flags, int Type, char *pBuffer = 0, int BufferSize = 0) = 0; + virtual bool RemoveFile(const char *pFilename, int Type) = 0; }; extern IStorage *CreateStorage(const char *pApplicationName, int NumArgs, const char **ppArguments); |