No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resourcemanager.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "resourcemanager.h"
  24. #include <physfs.h>
  25. ResourceManager g_resources;
  26. ResourceManager::ResourceManager()
  27. {
  28. }
  29. ResourceManager::~ResourceManager()
  30. {
  31. }
  32. void ResourceManager::init(const char *argv0)
  33. {
  34. PHYSFS_init(argv0);
  35. }
  36. void ResourceManager::terminate()
  37. {
  38. PHYSFS_deinit();
  39. }
  40. bool ResourceManager::setWriteDir(const std::string& path)
  41. {
  42. bool ret = (bool)PHYSFS_setWriteDir(path.c_str());
  43. if(!ret)
  44. error("Could not set the path %s as write directory, file write will not work.");
  45. return ret;
  46. }
  47. bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront)
  48. {
  49. if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1)) {
  50. error("Error while adding %s to resources search path: %s", PHYSFS_getLastError());
  51. return false;
  52. }
  53. return true;
  54. }
  55. bool ResourceManager::fileExists(const std::string& filePath)
  56. {
  57. return PHYSFS_exists(filePath.c_str());
  58. }
  59. unsigned char *ResourceManager::loadFile(const std::string& fileName, unsigned int *fileSize)
  60. {
  61. PHYSFS_file *file = PHYSFS_openRead(fileName.c_str());
  62. if(!file) {
  63. error("Failed to load file %s: %s", fileName.c_str(), PHYSFS_getLastError());
  64. *fileSize = 0;
  65. return NULL;
  66. }
  67. *fileSize = PHYSFS_fileLength(file);
  68. unsigned char *buffer = new unsigned char[*fileSize + 1];
  69. PHYSFS_read(file, (void*)buffer, 1, *fileSize);
  70. buffer[*fileSize] = 0;
  71. PHYSFS_close(file);
  72. return buffer;
  73. }
  74. std::string ResourceManager::loadTextFile(const std::string& fileName)
  75. {
  76. std::string text;
  77. unsigned int fileSize;
  78. char *buffer = (char *)loadFile(fileName, &fileSize);
  79. if(buffer) {
  80. text.assign(buffer);
  81. delete[] buffer;
  82. }
  83. return text;
  84. }
  85. bool ResourceManager::saveFile(const std::string &fileName, const unsigned char *data, unsigned int size)
  86. {
  87. PHYSFS_file *file = PHYSFS_openWrite(fileName.c_str());
  88. if(!file) {
  89. error("Failed to save file %s: %s", fileName.c_str(), PHYSFS_getLastError());
  90. return false;
  91. }
  92. PHYSFS_write(file, (void*)data, size, 1);
  93. PHYSFS_close(file);
  94. return true;
  95. }
  96. bool ResourceManager::saveTextFile(const std::string &fileName, std::string text)
  97. {
  98. return saveFile(fileName, (const unsigned char*)text.c_str(), text.size());
  99. }