Otclient  14/8/2020
resourcemanager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef RESOURCES_H
24 #define RESOURCES_H
25 
26 #include "declarations.h"
27 
28 #include <boost/filesystem.hpp>
29 
30 namespace fs = boost::filesystem;
31 
32 // @bindsingleton g_resources
34 {
35 public:
36  // @dontbind
37  void init(const char *argv0);
38  // @dontbind
39  void terminate();
40 
41  bool discoverWorkDir(const std::string& existentFile);
42  bool setupUserWriteDir(const std::string& appWriteDirName);
43  bool setWriteDir(const std::string& writeDir, bool create = false);
44 
45  bool addSearchPath(const std::string& path, bool pushFront = false);
46  bool removeSearchPath(const std::string& path);
47  void searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt);
48 
49  bool fileExists(const std::string& fileName);
50  bool directoryExists(const std::string& directoryName);
51 
52  // @dontbind
53  void readFileStream(const std::string& fileName, std::iostream& out);
54  std::string readFileContents(const std::string& fileName);
55  // @dontbind
56  bool writeFileBuffer(const std::string& fileName, const uchar* data, uint size);
57  bool writeFileContents(const std::string& fileName, const std::string& data);
58  // @dontbind
59  bool writeFileStream(const std::string& fileName, std::iostream& in);
60 
61  FileStreamPtr openFile(const std::string& fileName);
62  FileStreamPtr appendFile(const std::string& fileName);
63  FileStreamPtr createFile(const std::string& fileName);
64  bool deleteFile(const std::string& fileName);
65 
66  bool makeDir(const std::string directory);
67  std::list<std::string> listDirectoryFiles(const std::string& directoryPath = "");
68  std::vector<std::string> getDirectoryFiles(const std::string& path, bool filenameOnly, bool recursive);
69 
70  std::string resolvePath(const std::string& path);
71  std::string getRealDir(const std::string& path);
72  std::string getRealPath(const std::string& path);
73  std::string getBaseDir();
74  std::string getUserDir();
75  std::string getWriteDir() { return m_writeDir; }
76  std::string getWorkDir() { return m_workDir; }
77  std::deque<std::string> getSearchPaths() { return m_searchPaths; }
78 
79  std::string guessFilePath(const std::string& filename, const std::string& type);
80  bool isFileType(const std::string& filename, const std::string& type);
81  ticks_t getFileTime(const std::string& filename);
82 
83 protected:
84  std::vector<std::string> discoverPath(const fs::path& path, bool filenameOnly, bool recursive);
85 
86 private:
87  std::string m_workDir;
88  std::string m_writeDir;
89  std::deque<std::string> m_searchPaths;
90 };
91 
93 
94 #endif
ResourceManager::isFileType
bool isFileType(const std::string &filename, const std::string &type)
Definition: resourcemanager.cpp:359
ResourceManager::listDirectoryFiles
std::list< std::string > listDirectoryFiles(const std::string &directoryPath="")
Definition: resourcemanager.cpp:268
ResourceManager::writeFileStream
bool writeFileStream(const std::string &fileName, std::iostream &in)
Definition: resourcemanager.cpp:214
ResourceManager::removeSearchPath
bool removeSearchPath(const std::string &path)
Definition: resourcemanager.cpp:134
ResourceManager::getRealPath
std::string getRealPath(const std::string &path)
Definition: resourcemanager.cpp:337
ResourceManager::getRealDir
std::string getRealDir(const std::string &path)
Definition: resourcemanager.cpp:328
ResourceManager::directoryExists
bool directoryExists(const std::string &directoryName)
Definition: resourcemanager.cpp:163
ResourceManager::appendFile
FileStreamPtr appendFile(const std::string &fileName)
Definition: resourcemanager.cpp:242
ResourceManager::resolvePath
std::string resolvePath(const std::string &path)
Definition: resourcemanager.cpp:311
ticks_t
int64 ticks_t
Definition: types.h:43
ResourceManager::getDirectoryFiles
std::vector< std::string > getDirectoryFiles(const std::string &path, bool filenameOnly, bool recursive)
Definition: resourcemanager.cpp:280
ResourceManager
Definition: resourcemanager.h:33
ResourceManager::init
void init(const char *argv0)
Definition: resourcemanager.cpp:34
ResourceManager::setupUserWriteDir
bool setupUserWriteDir(const std::string &appWriteDirName)
Definition: resourcemanager.cpp:70
ResourceManager::getFileTime
ticks_t getFileTime(const std::string &filename)
Definition: resourcemanager.cpp:366
ResourceManager::fileExists
bool fileExists(const std::string &fileName)
Definition: resourcemanager.cpp:157
declarations.h
uint
unsigned int uint
Definition: types.h:31
ResourceManager::searchAndAddPackages
void searchAndAddPackages(const std::string &packagesDir, const std::string &packageExt)
Definition: resourcemanager.cpp:144
ResourceManager::terminate
void terminate()
Definition: resourcemanager.cpp:40
ResourceManager::makeDir
bool makeDir(const std::string directory)
Definition: resourcemanager.cpp:263
ResourceManager::getWorkDir
std::string getWorkDir()
Definition: resourcemanager.h:76
ResourceManager::openFile
FileStreamPtr openFile(const std::string &fileName)
Definition: resourcemanager.cpp:232
ResourceManager::readFileContents
std::string readFileContents(const std::string &fileName)
Definition: resourcemanager.cpp:185
ResourceManager::addSearchPath
bool addSearchPath(const std::string &path, bool pushFront=false)
Definition: resourcemanager.cpp:108
ResourceManager::deleteFile
bool deleteFile(const std::string &fileName)
Definition: resourcemanager.cpp:258
uchar
unsigned char uchar
Definition: types.h:29
ResourceManager::getSearchPaths
std::deque< std::string > getSearchPaths()
Definition: resourcemanager.h:77
ResourceManager::readFileStream
void readFileStream(const std::string &fileName, std::iostream &out)
Definition: resourcemanager.cpp:173
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
stdext::shared_object_ptr< FileStream >
ResourceManager::getWriteDir
std::string getWriteDir()
Definition: resourcemanager.h:75
ResourceManager::discoverPath
std::vector< std::string > discoverPath(const fs::path &path, bool filenameOnly, bool recursive)
Definition: resourcemanager.cpp:289
ResourceManager::writeFileContents
bool writeFileContents(const std::string &fileName, const std::string &data)
Definition: resourcemanager.cpp:227
ResourceManager::setWriteDir
bool setWriteDir(const std::string &writeDir, bool create=false)
Definition: resourcemanager.cpp:90
ResourceManager::writeFileBuffer
bool writeFileBuffer(const std::string &fileName, const uchar *data, uint size)
Definition: resourcemanager.cpp:201
ResourceManager::createFile
FileStreamPtr createFile(const std::string &fileName)
Definition: resourcemanager.cpp:250
ResourceManager::discoverWorkDir
bool discoverWorkDir(const std::string &existentFile)
Definition: resourcemanager.cpp:45
ResourceManager::getBaseDir
std::string getBaseDir()
Definition: resourcemanager.cpp:342
ResourceManager::guessFilePath
std::string guessFilePath(const std::string &filename, const std::string &type)
Definition: resourcemanager.cpp:352
ResourceManager::getUserDir
std::string getUserDir()
Definition: resourcemanager.cpp:347