platformVolume.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/console.h"
  23. #include "platform/platform.h"
  24. #if defined(TORQUE_OS_WIN)
  25. #include <sys/utime.h>
  26. #else
  27. #include <sys/time.h>
  28. #endif
  29. #include "platform/platformVolume.h"
  30. #include "core/util/zip/zipVolume.h"
  31. using namespace Torque;
  32. using namespace Torque::FS;
  33. namespace Platform
  34. {
  35. namespace FS
  36. {
  37. bool MountDefaults()
  38. {
  39. String path = getAssetDir();
  40. bool mounted = Mount( "game", createNativeFS( path ));
  41. if ( !mounted )
  42. return false;
  43. #ifdef TORQUE_SECURE_VFS
  44. mounted = Mount("/", createNativeFS(path));
  45. if (!mounted)
  46. {
  47. return false;
  48. }
  49. #endif
  50. // Always mount the data dir so scripts work in either configuration. This is used for eg. preferences storage.
  51. Path dataDirectory = Platform::getUserDataDirectory();
  52. Path appDataDirectory = Path::Join(dataDirectory.getFullPath().c_str(), '/', TORQUE_APP_NAME);
  53. // Ensure the root of the data directory exists before trying to mount data VFS
  54. if (!Platform::FS::IsDirectory(appDataDirectory) && !Platform::FS::CreateDirectory(appDataDirectory))
  55. {
  56. // NOTE: We can't Con::errorf here because it doesn't actually output by this point in execution
  57. }
  58. mounted = Mount("data", Platform::FS::createNativeFS(appDataDirectory.getFullPath()));
  59. if (!mounted)
  60. {
  61. // NOTE: We can't Con::errorf here because it doesn't actually output by this point in execution
  62. return false;
  63. }
  64. #ifndef TORQUE_DISABLE_VIRTUAL_MOUNT_SYSTEM
  65. // Note that the VirtualMountSystem must be enabled in volume.cpp for zip support to work.
  66. return MountZips("game");
  67. #else
  68. return true;
  69. #endif
  70. }
  71. bool MountZips(const String &root)
  72. {
  73. Path basePath;
  74. basePath.setRoot(root);
  75. Vector<String> outList;
  76. S32 num = FindByPattern(basePath, "*.zip", true, outList);
  77. if(num == 0)
  78. return true; // not an error
  79. S32 mounted = 0;
  80. for(S32 i = 0;i < outList.size();++i)
  81. {
  82. String &zipfile = outList[i];
  83. #ifdef TORQUE_ZIP_DISK_LAYOUT
  84. mounted += (S32)Mount(root, new ZipFileSystem(zipfile, false));
  85. #else
  86. mounted += (S32)Mount(root, new ZipFileSystem(zipfile, true));
  87. #endif
  88. }
  89. return mounted == outList.size();
  90. }
  91. //-----------------------------------------------------------------------------
  92. bool Touch( const Path &path )
  93. {
  94. #if defined(TORQUE_OS_WIN)
  95. return( utime( path.getFullPath(), 0 ) != -1 );
  96. #else
  97. return( utimes( path.getFullPath(), NULL) == 0 ); // utimes returns 0 on success.
  98. #endif
  99. }
  100. } // namespace FS
  101. } // namespace Platform