finished read through of SlippiGameFileLoader.cpp

This commit is contained in:
Nikhil Narayana 2023-08-20 17:02:32 -07:00
commit e063e722c1
No known key found for this signature in database
GPG key ID: 1B34839FA8D6245E
2 changed files with 27 additions and 34 deletions

View file

@ -9,67 +9,60 @@
#include "Core/HW/DVD/DVDThread.h" #include "Core/HW/DVD/DVDThread.h"
#include "Core/System.h" #include "Core/System.h"
std::string getFilePath(std::string fileName) std::string getFilePath(std::string file_name)
{ {
std::string dirPath = File::GetSysDirectory(); std::string dir_path = File::GetSysDirectory();
std::string filePath = dirPath + "GameFiles/GALE01/" + fileName; // TODO: Handle other games? std::string file_path = dir_path + "GameFiles/GALE01/" + file_name; // TODO: Handle other games?
if (File::Exists(filePath)) if (File::Exists(file_path))
{ {
return filePath; return file_path;
} }
filePath = filePath + ".diff"; file_path = file_path + ".diff";
if (File::Exists(filePath)) if (File::Exists(file_path))
{ {
return filePath; return file_path;
} }
return ""; return "";
} }
u32 SlippiGameFileLoader::LoadFile(std::string fileName, std::string& data) u32 SlippiGameFileLoader::LoadFile(std::string file_name, std::string& data)
{ {
if (fileCache.count(fileName)) if (file_cache.count(file_name))
{ {
data = fileCache[fileName]; data = file_cache[file_name];
return (u32)data.size(); return static_cast<u32>(data.size());
} }
INFO_LOG_FMT(SLIPPI, "Loading file: {}", fileName.c_str()); INFO_LOG_FMT(SLIPPI, "Loading file: {}", file_name.c_str());
std::string gameFilePath = getFilePath(fileName); std::string game_file_path = getFilePath(file_name);
if (gameFilePath.empty()) if (game_file_path.empty())
{ {
fileCache[fileName] = ""; file_cache[file_name] = "";
data = ""; data = "";
return 0; return 0;
} }
std::string fileContents; std::string file_contents;
// Don't read MxDt.dat because our Launcher may not have successfully deleted it and
// loading the old one from the file system would break m-ex based ISOs
if (fileName != "MxDt.dat")
{
File::ReadFileToString(gameFilePath, fileContents);
}
// If the file was a diff file and the game is running, load the main file from ISO and apply // If the file was a diff file and the game is running, load the main file from ISO and apply
// patch // patch
if (gameFilePath.substr(gameFilePath.length() - 5) == ".diff" && if (game_file_path.substr(game_file_path.length() - 5) == ".diff" &&
Core::GetState() == Core::State::Running) Core::GetState() == Core::State::Running)
{ {
std::vector<u8> buf; std::vector<u8> buf;
INFO_LOG_FMT(SLIPPI, "Will process diff"); INFO_LOG_FMT(SLIPPI, "Will process diff");
Core::System::GetInstance().GetDVDThread().ReadFile(fileName, buf); Core::System::GetInstance().GetDVDThread().ReadFile(file_name, buf);
std::string diffContents = fileContents; std::string diff_contents = file_contents;
decoder.Decode((char*)buf.data(), buf.size(), diffContents, &fileContents); decoder.Decode((char*)buf.data(), buf.size(), diff_contents, &file_contents);
} }
fileCache[fileName] = fileContents; file_cache[file_name] = file_contents;
data = fileCache[fileName]; data = file_cache[file_name];
INFO_LOG_FMT(SLIPPI, "File size: {}", (u32)data.size()); INFO_LOG_FMT(SLIPPI, "File size: {}", static_cast<u32>(data.size()));
return (u32)data.size(); return static_cast<u32>(data.size());
} }

View file

@ -9,9 +9,9 @@
class SlippiGameFileLoader class SlippiGameFileLoader
{ {
public: public:
u32 LoadFile(std::string fileName, std::string& contents); u32 LoadFile(std::string file_name, std::string& contents);
protected: protected:
std::unordered_map<std::string, std::string> fileCache; std::unordered_map<std::string, std::string> file_cache;
open_vcdiff::VCDiffDecoder decoder; open_vcdiff::VCDiffDecoder decoder;
}; };