platformVolume.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef TORQUE_DISABLE_VIRTUAL_MOUNT_SYSTEM
  51. // Always mount the data & home dir so scripts work in either configuration. This is used for eg. preferences storage.
  52. Path dataDirectory = Platform::getUserDataDirectory();
  53. Path appDataDirectory = Path::Join(dataDirectory.getFullPath().c_str(), '/', TORQUE_APP_NAME);
  54. Path homeDirectory = Platform::getUserHomeDirectory();
  55. Path appHomeDirectory = Path::Join(homeDirectory.getFullPath().c_str(), '/', TORQUE_APP_NAME);
  56. // Ensure the root of the data directory exists before trying to mount data VFS
  57. if (!Platform::FS::IsDirectory(appDataDirectory) && !Platform::FS::CreateDirectory(appDataDirectory))
  58. {
  59. // NOTE: We can't Con::errorf here because it doesn't actually output by this point in execution
  60. }
  61. // Ensure the root of the home directory exists before trying to mount home VFS
  62. if (!Platform::FS::IsDirectory(appHomeDirectory) && !Platform::FS::CreateDirectory(appHomeDirectory))
  63. {
  64. // NOTE: We can't Con::errorf here because it doesn't actually output by this point in execution
  65. }
  66. // data:/ points to a directory that is usually buried someplace harder to reach on OS
  67. mounted = Mount("data", Platform::FS::createNativeFS(appDataDirectory.getFullPath()));
  68. if (!mounted)
  69. {
  70. // NOTE: We can't Con::errorf here because it doesn't actually output by this point in execution
  71. return false;
  72. }
  73. // home:/ refers to your Documents/<APPNAME> folder which is easier to reach than the data:/ mount
  74. mounted = Mount("home", Platform::FS::createNativeFS(appHomeDirectory.getFullPath()));
  75. if (!mounted)
  76. {
  77. // NOTE: We can't Con::errorf here because it doesn't actually output by this point in execution
  78. return false;
  79. }
  80. // Note that the VirtualMountSystem must be enabled in volume.cpp for zip support to work.
  81. return MountZips("game");
  82. #else
  83. return true;
  84. #endif
  85. }
  86. bool MountZips(const String &root)
  87. {
  88. Path basePath;
  89. basePath.setRoot(root);
  90. Vector<String> outList;
  91. S32 num = FindByPattern(basePath, "*.zip", true, outList);
  92. if(num == 0)
  93. return true; // not an error
  94. S32 mounted = 0;
  95. for(S32 i = 0;i < outList.size();++i)
  96. {
  97. String &zipfile = outList[i];
  98. #ifdef TORQUE_ZIP_DISK_LAYOUT
  99. mounted += (S32)Mount(root, new ZipFileSystem(zipfile, false));
  100. #else
  101. mounted += (S32)Mount(root, new ZipFileSystem(zipfile, true));
  102. #endif
  103. }
  104. return mounted == outList.size();
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool Touch( const Path &path )
  108. {
  109. #if defined(TORQUE_OS_WIN)
  110. return( utime( path.getFullPath(), 0 ) != -1 );
  111. #else
  112. return( utimes( path.getFullPath(), NULL) == 0 ); // utimes returns 0 on success.
  113. #endif
  114. }
  115. } // namespace FS
  116. } // namespace Platform