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/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";
if (File::Exists(filePath))
file_path = file_path + ".diff";
if (File::Exists(file_path))
{
return filePath;
return file_path;
}
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];
return (u32)data.size();
data = file_cache[file_name];
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);
if (gameFilePath.empty())
std::string game_file_path = getFilePath(file_name);
if (game_file_path.empty())
{
fileCache[fileName] = "";
file_cache[file_name] = "";
data = "";
return 0;
}
std::string fileContents;
// 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);
}
std::string file_contents;
// If the file was a diff file and the game is running, load the main file from ISO and apply
// patch
if (gameFilePath.substr(gameFilePath.length() - 5) == ".diff" &&
if (game_file_path.substr(game_file_path.length() - 5) == ".diff" &&
Core::GetState() == Core::State::Running)
{
std::vector<u8> buf;
INFO_LOG_FMT(SLIPPI, "Will process diff");
Core::System::GetInstance().GetDVDThread().ReadFile(fileName, buf);
std::string diffContents = fileContents;
decoder.Decode((char*)buf.data(), buf.size(), diffContents, &fileContents);
Core::System::GetInstance().GetDVDThread().ReadFile(file_name, buf);
std::string diff_contents = file_contents;
decoder.Decode((char*)buf.data(), buf.size(), diff_contents, &file_contents);
}
fileCache[fileName] = fileContents;
data = fileCache[fileName];
INFO_LOG_FMT(SLIPPI, "File size: {}", (u32)data.size());
return (u32)data.size();
file_cache[file_name] = file_contents;
data = file_cache[file_name];
INFO_LOG_FMT(SLIPPI, "File size: {}", static_cast<u32>(data.size()));
return static_cast<u32>(data.size());
}

View file

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