FileMonitor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <cstring>
  7. #include <string>
  8. #include <unordered_set>
  9. #include <vector>
  10. #include "Common/CommonTypes.h"
  11. #include "Common/StringUtil.h"
  12. #include "Common/Logging/LogManager.h"
  13. #include "Core/ConfigManager.h"
  14. #include "Core/Core.h"
  15. #include "Core/Boot/Boot.h"
  16. #include "DiscIO/FileMonitor.h"
  17. #include "DiscIO/Filesystem.h"
  18. #include "DiscIO/Volume.h"
  19. #include "DiscIO/VolumeCreator.h"
  20. namespace FileMon
  21. {
  22. static DiscIO::IVolume *OpenISO = nullptr;
  23. static DiscIO::IFileSystem *pFileSystem = nullptr;
  24. static std::string ISOFile = "", CurrentFile = "";
  25. static bool FileAccess = true;
  26. // Filtered files
  27. bool IsSoundFile(const std::string& filename)
  28. {
  29. std::string extension;
  30. SplitPath(filename, nullptr, nullptr, &extension);
  31. std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  32. static std::unordered_set<std::string> extensions = {
  33. ".adp", // 1080 Avalanche, Crash Bandicoot, etc.
  34. ".adx", // Sonic Adventure 2 Battle, etc.
  35. ".afc", // Zelda WW
  36. ".ast", // Zelda TP, Mario Kart
  37. ".brstm", // Wii Sports, Wario Land, etc.
  38. ".dsp", // Metroid Prime
  39. ".hps", // SSB Melee
  40. ".ogg", // Tony Hawk's Underground 2
  41. ".sad", // Disaster
  42. ".snd", // Tales of Symphonia
  43. ".song", // Tales of Symphonia
  44. ".ssm", // Custom Robo, Kirby Air Ride, etc.
  45. ".str", // Harry Potter & the Sorcerer's Stone
  46. };
  47. return extensions.find(extension) != extensions.end();
  48. }
  49. // Read the file system
  50. void ReadFileSystem(const std::string& filename)
  51. {
  52. // Should have an actual Shutdown procedure or something
  53. if (OpenISO != nullptr)
  54. {
  55. delete OpenISO;
  56. OpenISO = nullptr;
  57. }
  58. if (pFileSystem != nullptr)
  59. {
  60. delete pFileSystem;
  61. pFileSystem = nullptr;
  62. }
  63. OpenISO = DiscIO::CreateVolumeFromFilename(filename);
  64. if (!OpenISO)
  65. return;
  66. if (OpenISO->GetVolumeType() != DiscIO::IVolume::WII_WAD)
  67. {
  68. pFileSystem = DiscIO::CreateFileSystem(OpenISO);
  69. if (!pFileSystem)
  70. return;
  71. }
  72. FileAccess = true;
  73. }
  74. // Logs a file if it passes a few checks
  75. void CheckFile(const std::string& file, u64 size)
  76. {
  77. // Don't do anything if the log is unselected
  78. if (!LogManager::GetInstance()->IsEnabled(LogTypes::FILEMON, LogTypes::LWARNING))
  79. return;
  80. // Do nothing if we found the same file again
  81. if (CurrentFile == file)
  82. return;
  83. if (size > 0)
  84. size = (size / 1000);
  85. std::string str = StringFromFormat("%s kB %s", ThousandSeparate(size, 7).c_str(), file.c_str());
  86. if (IsSoundFile(file))
  87. {
  88. INFO_LOG(FILEMON, "%s", str.c_str());
  89. }
  90. else
  91. {
  92. WARN_LOG(FILEMON, "%s", str.c_str());
  93. }
  94. // Update the current file
  95. CurrentFile = file;
  96. }
  97. // Find the filename
  98. void FindFilename(u64 offset)
  99. {
  100. // Don't do anything if a game is not running
  101. if (Core::GetState() != Core::CORE_RUN)
  102. return;
  103. // Or if the log is unselected
  104. if (!LogManager::GetInstance()->IsEnabled(LogTypes::FILEMON, LogTypes::LWARNING))
  105. return;
  106. // Or if we don't have file access
  107. if (!FileAccess)
  108. return;
  109. if (!pFileSystem || ISOFile != SConfig::GetInstance().m_LastFilename)
  110. {
  111. FileAccess = false;
  112. ReadFileSystem(SConfig::GetInstance().m_LastFilename);
  113. ISOFile = SConfig::GetInstance().m_LastFilename;
  114. INFO_LOG(FILEMON, "Opening '%s'", ISOFile.c_str());
  115. return;
  116. }
  117. const std::string filename = pFileSystem->GetFileName(offset);
  118. if (filename.empty())
  119. return;
  120. CheckFile(filename, pFileSystem->GetFileSize(filename));
  121. }
  122. void Close()
  123. {
  124. if (OpenISO != nullptr)
  125. {
  126. delete OpenISO;
  127. OpenISO = nullptr;
  128. }
  129. if (pFileSystem != nullptr)
  130. {
  131. delete pFileSystem;
  132. pFileSystem = nullptr;
  133. }
  134. ISOFile = "";
  135. CurrentFile = "";
  136. FileAccess = true;
  137. }
  138. } // FileMon