Browse Source

basic resource manager implementation

load/save configurations files in user home
Eduardo Bart 11 years ago
parent
commit
b6f1a18beb
7 changed files with 160 additions and 20 deletions
  1. 10
    11
      src/configmanager.cpp
  2. 17
    4
      src/main.cpp
  3. 3
    0
      src/platform.h
  4. 82
    0
      src/resourcemanager.cpp
  5. 30
    2
      src/resourcemanager.h
  6. 1
    0
      src/util.h
  7. 17
    3
      src/x11platform.cpp

+ 10
- 11
src/configmanager.cpp View File

@@ -28,6 +28,7 @@
28 28
 
29 29
 #include <fstream>
30 30
 #include <yaml-cpp/yaml.h>
31
+#include "resourcemanager.h"
31 32
 
32 33
 ConfigManager g_config;
33 34
 
@@ -43,11 +44,16 @@ ConfigManager::~ConfigManager()
43 44
 
44 45
 bool ConfigManager::load(const std::string& fileName)
45 46
 {
46
-    std::ifstream fin(fileName.c_str());
47
-    if(!fin.good())
47
+    m_fileName = fileName;
48
+
49
+    if(!g_resources.fileExists(fileName))
48 50
         return false;
49 51
 
50
-    m_fileName = fileName;
52
+    std::string fileContents = g_resources.loadTextFile(fileName);
53
+    if(fileContents.size() == 0)
54
+        return false;
55
+
56
+    std::istringstream fin(fileContents);
51 57
 
52 58
     try {
53 59
         YAML::Parser parser(fin);
@@ -71,16 +77,9 @@ bool ConfigManager::load(const std::string& fileName)
71 77
 
72 78
 void ConfigManager::save()
73 79
 {
74
-    std::ofstream fout(m_fileName.c_str());
75
-    if(!fout.good()) {
76
-        error("Failed to save configuration file %s", m_fileName.c_str());
77
-        return;
78
-    }
79
-
80 80
     YAML::Emitter out;
81 81
     out << m_confsMap;
82
-
83
-    fout << out.c_str();
82
+    g_resources.saveFile(m_fileName, (const unsigned char*)out.c_str(), out.size());
84 83
 }
85 84
 
86 85
 void ConfigManager::setValue(const std::string &key, const std::string &value)

+ 17
- 4
src/main.cpp View File

@@ -25,12 +25,13 @@
25 25
 #include "engine.h"
26 26
 #include "const.h"
27 27
 #include "logger.h"
28
+#include "configmanager.h"
29
+#include "resourcemanager.h"
30
+#include "platform.h"
28 31
 
29 32
 #include <csignal>
30
-#include "configmanager.h"
31
-#include "util.h"
32 33
 
33
-// catches terminate signals to exit nicely
34
+/// Catches signals so we can exit nicely
34 35
 void signal_handler(int sig)
35 36
 {
36 37
     switch(sig) {
@@ -48,7 +49,7 @@ void signal_handler(int sig)
48 49
     }
49 50
 }
50 51
 
51
-/// Default otclient configurations
52
+/// Default configurations
52 53
 void setDefaultConfigs()
53 54
 {
54 55
     g_config.setValue("width", 640);
@@ -62,7 +63,15 @@ int main(int argc, const char *argv[])
62 63
     signal(SIGINT, signal_handler);
63 64
     signal(SIGQUIT, signal_handler);
64 65
 
66
+    // setup resources
67
+    g_resources.init(argv[0]);
68
+    if(g_resources.setWriteDir(Platform::getAppUserDir()))
69
+        g_resources.addToSearchPath(Platform::getAppUserDir());
70
+
71
+    // before loading configurations set the default ones
65 72
     setDefaultConfigs();
73
+
74
+    // load configurations
66 75
     if(!g_config.load("config.yml"))
67 76
         notice("Could not read configuration file, default configurations will be used.");
68 77
 
@@ -73,6 +82,10 @@ int main(int argc, const char *argv[])
73 82
     g_engine.run();
74 83
     g_engine.terminate();
75 84
 
85
+    // save configurations before exiting
76 86
     g_config.save();
87
+
88
+    // unload resources
89
+    g_resources.terminate();
77 90
     return 0;
78 91
 }

+ 3
- 0
src/platform.h View File

@@ -63,6 +63,9 @@ namespace Platform
63 63
     void setVsync(bool enable = true);
64 64
     /// Swap GL buffers
65 65
     void swapBuffers();
66
+
67
+    /// Get the app user directory, the place to save files configurations files
68
+    const char *getAppUserDir();
66 69
 }
67 70
 
68 71
 #endif // PLATFORM_H

+ 82
- 0
src/resourcemanager.cpp View File

@@ -23,6 +23,9 @@
23 23
 
24 24
 
25 25
 #include "resourcemanager.h"
26
+#include "logger.h"
27
+
28
+#include <physfs.h>
26 29
 
27 30
 ResourceManager g_resources;
28 31
 
@@ -35,3 +38,82 @@ ResourceManager::~ResourceManager()
35 38
 {
36 39
 
37 40
 }
41
+
42
+void ResourceManager::init(const char *argv0)
43
+{
44
+    PHYSFS_init(argv0);
45
+}
46
+
47
+void ResourceManager::terminate()
48
+{
49
+    PHYSFS_deinit();
50
+}
51
+
52
+bool ResourceManager::setWriteDir(const std::string& path)
53
+{
54
+    bool ret = (bool)PHYSFS_setWriteDir(path.c_str());
55
+
56
+    if(!ret)
57
+        error("Could not set the path %s as write directory, file write will not work.");
58
+    return ret;
59
+}
60
+
61
+bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront)
62
+{
63
+    if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1)) {
64
+        error("Error while adding %s to resources search path: %s", PHYSFS_getLastError());
65
+        return false;
66
+    }
67
+    return true;
68
+}
69
+
70
+bool ResourceManager::fileExists(const std::string& filePath)
71
+{
72
+    return PHYSFS_exists(filePath.c_str());
73
+}
74
+
75
+unsigned char *ResourceManager::loadFile(const std::string& fileName, unsigned int *fileSize)
76
+{
77
+    PHYSFS_file *file = PHYSFS_openRead(fileName.c_str());
78
+    if(!file) {
79
+        error("Failed to load file %s: %s", fileName.c_str(), PHYSFS_getLastError());
80
+        *fileSize = 0;
81
+        return NULL;
82
+    }
83
+
84
+    *fileSize = PHYSFS_fileLength(file);
85
+    unsigned char *buffer = new unsigned char[*fileSize];
86
+    PHYSFS_read(file, (void*)buffer, 1, *fileSize);
87
+    PHYSFS_close(file);
88
+    return buffer;
89
+}
90
+
91
+std::string ResourceManager::loadTextFile(const std::string& fileName)
92
+{
93
+    std::string text;
94
+    unsigned int fileSize;
95
+    char *buffer = (char *)loadFile(fileName, &fileSize);
96
+    if(buffer) {
97
+        text.assign(buffer);
98
+        delete[] buffer;
99
+    }
100
+    return text;
101
+}
102
+
103
+bool ResourceManager::saveFile(const std::string &fileName, const unsigned char *data, unsigned int size)
104
+{
105
+    PHYSFS_file *file = PHYSFS_openWrite(fileName.c_str());
106
+    if(!file) {
107
+        error("Failed to save file %s: %s", fileName.c_str(), PHYSFS_getLastError());
108
+        return false;
109
+    }
110
+
111
+    PHYSFS_write(file, (void*)data, size, 1);
112
+    PHYSFS_close(file);
113
+    return true;
114
+}
115
+
116
+bool ResourceManager::saveTextFile(const std::string &fileName, std::string text)
117
+{
118
+    return saveFile(fileName, (const unsigned char*)text.c_str(), text.size());
119
+}

+ 30
- 2
src/resourcemanager.h View File

@@ -26,7 +26,6 @@
26 26
 #define RESOURCEMANAGER_H
27 27
 
28 28
 #include <string>
29
-#include <list>
30 29
 
31 30
 class ResourceManager
32 31
 {
@@ -34,7 +33,36 @@ public:
34 33
     ResourceManager();
35 34
     ~ResourceManager();
36 35
 
37
-private:
36
+    void init(const char *argv0);
37
+    void terminate();
38
+
39
+    /// Sets the write directory
40
+    bool setWriteDir(const std::string &path);
41
+
42
+    /// Adds a directory or zip archive to the search path
43
+    bool addToSearchPath(const std::string& path, bool insertInFront = true);
44
+
45
+    /// Checks whether the given file exists in the search path
46
+    bool fileExists(const std::string& filePath);
47
+
48
+    /// Searches for zip files and adds them to the search path
49
+    void searchAndAddArchives(const std::string &path,
50
+                              const std::string &ext,
51
+                              const bool append);
52
+
53
+    /** Load a file by allocating a buffer and filling it with the file contents
54
+     * where fileSize will be set to the file size.
55
+     * The returned buffer must be freed with delete[]. */
56
+    unsigned char *loadFile(const std::string &fileName, unsigned int *fileSize);
57
+
58
+    /// Loads a text file into a std::string
59
+    std::string loadTextFile(const std::string &fileName);
60
+
61
+    /// Save a file into write directory
62
+    bool saveFile(const std::string &fileName, const unsigned char *data, unsigned int size);
63
+
64
+    /// Save a text file into write directory
65
+    bool saveTextFile(const std::string &fileName, std::string text);
38 66
 };
39 67
 
40 68
 extern ResourceManager g_resources;

+ 1
- 0
src/util.h View File

@@ -43,6 +43,7 @@ inline std::string castToString(const T& x) {
43 43
     return ss.str();
44 44
 }
45 45
 
46
+/// Convert std:;string to int/float like types
46 47
 template<typename T>
47 48
 inline T castFromString(const std::string& s) {
48 49
     std::istringstream ss(s);

+ 17
- 3
src/x11platform.cpp View File

@@ -26,18 +26,23 @@
26 26
 #include "engine.h"
27 27
 #include "input.h"
28 28
 #include "logger.h"
29
+#include "const.h"
29 30
 
30
-#include <cstring>
31
+#include <time.h>
32
+#include <sys/time.h>
33
+#include <sys/stat.h>
34
+#include <errno.h>
31 35
 
36
+#include <cstring>
32 37
 #include <string>
38
+#include <sstream>
33 39
 #include <algorithm>
34 40
 #include <map>
35 41
 
36
-#include <time.h>
37
-#include <sys/time.h>
38 42
 #include <X11/Xlib.h>
39 43
 #include <X11/Xatom.h>
40 44
 #include <GL/glx.h>
45
+#include <physfs.h>
41 46
 
42 47
 struct X11PlatformPrivate {
43 48
     Display *display;
@@ -719,3 +724,12 @@ int Platform::getWindowHeight()
719 724
 {
720 725
     return x11.height;
721 726
 }
727
+
728
+const char *Platform::getAppUserDir()
729
+{
730
+    std::stringstream sdir;
731
+    sdir << PHYSFS_getUserDir() << "/." << APP_NAME << "/";
732
+    if((mkdir(sdir.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) && (errno != EEXIST))
733
+        error("Couldn't create directory for saving configuration file. (%s)", sdir.str().c_str());
734
+    return sdir.str().c_str();
735
+}

Loading…
Cancel
Save