00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <cstdlib>
00023 #include <cstring>
00024
00025 #ifdef _WIN32
00026 #include <io.h>
00027 #include <direct.h>
00028 #else
00029 #include <unistd.h>
00030 #include <dirent.h>
00031 #endif
00032
00033 #include <physfs.h>
00034
00035 #include "game-server/resourcemanager.hpp"
00036
00037 #include "utils/logger.h"
00038
00039 #define TMWSERV_DATADIR ""
00040
00041 void ResourceManager::initialize()
00042 {
00043 PHYSFS_permitSymbolicLinks(1);
00044
00045 PHYSFS_addToSearchPath("data", 1);
00046 PHYSFS_addToSearchPath(TMWSERV_DATADIR "data", 1);
00047
00048 #ifdef _WIN32
00049
00050 std::string searchString = std::string("data/*.zip");
00051
00052
00053 struct _finddata_t findFileInfo;
00054
00055
00056 long handle =
00057 static_cast<long>(::_findfirst(searchString.c_str(), &findFileInfo));
00058 long file = handle;
00059
00060
00061 while (file >= 0) {
00062
00063 std::string filePath = std::string("data/") +
00064 std::string(findFileInfo.name);
00065
00066 LOG_INFO("Adding to PhysicsFS: " << findFileInfo.name);
00067
00068
00069 PHYSFS_addToSearchPath(filePath.c_str(), 1);
00070
00071
00072 file = ::_findnext(handle, &findFileInfo);
00073 }
00074
00075
00076 ::_findclose(handle);
00077 #else
00078
00079 char programPath[256];
00080 if (!getcwd(programPath, 256))
00081 strcpy(programPath, ".");
00082 strncat(programPath, "/data", 256 - strlen(programPath) - 1);
00083
00084
00085 DIR *dir = opendir(programPath);
00086
00087
00088 if (dir == NULL) {
00089 return;
00090 }
00091
00092 struct dirent *direntry;
00093 while ((direntry = readdir(dir)) != NULL)
00094 {
00095 char *ext = strstr(direntry->d_name, ".zip");
00096
00097 if (ext != NULL && strcmp(ext, ".zip") == 0)
00098 {
00099
00100 std::string filePath = std::string(programPath) +
00101 std::string("/") + std::string(direntry->d_name);
00102
00103 LOG_INFO("Adding to PhysicsFS: " << filePath);
00104
00105
00106 PHYSFS_addToSearchPath(filePath.c_str(), 1);
00107 }
00108 }
00109
00110 closedir(dir);
00111 #endif
00112 }
00113
00114 bool ResourceManager::exists(const std::string &path)
00115 {
00116 return PHYSFS_exists(path.c_str());
00117 }
00118
00119 char *ResourceManager::loadFile(const std::string &fileName, int &fileSize)
00120 {
00121
00122 PHYSFS_file* file = PHYSFS_openRead(fileName.c_str());
00123
00124
00125 if (file == NULL)
00126 {
00127 LOG_WARN("Failed to load '" << fileName << "': "
00128 << PHYSFS_getLastError());
00129 return NULL;
00130 }
00131
00132
00133 fileSize = PHYSFS_fileLength(file);
00134
00135
00136 char *buffer = (char *)malloc(fileSize + 1);
00137 if (PHYSFS_read(file, buffer, 1, fileSize) != fileSize)
00138 {
00139 free(buffer);
00140 LOG_WARN("Failed to load '" << fileName << "': "
00141 << PHYSFS_getLastError());
00142 return NULL;
00143 }
00144
00145
00146 PHYSFS_close(file);
00147
00148
00149 buffer[fileSize] = 0;
00150 return buffer;
00151 }