2018-01-16 22:20:12 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-07 23:24:51 -04:00
|
|
|
#include <boost/container/flat_map.hpp>
|
2018-02-19 00:32:00 -05:00
|
|
|
#include "common/file_util.h"
|
2018-07-17 15:42:15 -04:00
|
|
|
#include "core/file_sys/errors.h"
|
2018-01-16 22:20:12 -05:00
|
|
|
#include "core/file_sys/filesystem.h"
|
2018-07-07 23:24:51 -04:00
|
|
|
#include "core/file_sys/savedata_factory.h"
|
|
|
|
#include "core/file_sys/sdmc_factory.h"
|
2018-01-16 22:20:12 -05:00
|
|
|
#include "core/hle/service/filesystem/filesystem.h"
|
|
|
|
#include "core/hle/service/filesystem/fsp_srv.h"
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
namespace Service::FileSystem {
|
2018-01-16 22:20:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of registered file systems, identified by type. Once an file system is registered here, it
|
|
|
|
* is never removed until UnregisterFileSystems is called.
|
|
|
|
*/
|
2018-07-17 15:42:15 -04:00
|
|
|
static std::unique_ptr<FileSys::RomFSFactory> romfs_factory;
|
|
|
|
static std::unique_ptr<FileSys::SaveDataFactory> save_data_factory;
|
|
|
|
static std::unique_ptr<FileSys::SDMCFactory> sdmc_factory;
|
|
|
|
|
|
|
|
ResultCode RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory) {
|
|
|
|
ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second RomFS");
|
|
|
|
romfs_factory = std::move(factory);
|
|
|
|
LOG_DEBUG(Service_FS, "Registered RomFS");
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
2018-01-16 22:20:12 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
ResultCode RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory) {
|
|
|
|
ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second save data");
|
|
|
|
save_data_factory = std::move(factory);
|
|
|
|
LOG_DEBUG(Service_FS, "Registered save data");
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
2018-01-16 22:20:12 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
ResultCode RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory) {
|
|
|
|
ASSERT_MSG(sdmc_factory == nullptr, "Tried to register a second SDMC");
|
|
|
|
sdmc_factory = std::move(factory);
|
|
|
|
LOG_DEBUG(Service_FS, "Registered SDMC");
|
2018-01-16 22:20:12 -05:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenRomFS(u64 title_id) {
|
|
|
|
LOG_TRACE(Service_FS, "Opening RomFS for title_id={:016X}", title_id);
|
2018-01-16 22:20:12 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
if (romfs_factory == nullptr) {
|
2018-01-16 22:20:12 -05:00
|
|
|
// TODO(bunnei): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
return romfs_factory->Open(title_id);
|
2018-01-16 22:20:12 -05:00
|
|
|
}
|
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenSaveData(
|
|
|
|
FileSys::SaveDataSpaceId space, FileSys::SaveDataDescriptor save_struct) {
|
|
|
|
LOG_TRACE(Service_FS, "Opening Save Data for space_id={:01X}, save_struct={}",
|
|
|
|
static_cast<u8>(space), SaveStructDebugInfo(save_struct));
|
2018-03-04 13:03:58 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
if (save_data_factory == nullptr) {
|
|
|
|
return ResultCode(ErrorModule::FS, FileSys::ErrCodes::SaveDataNotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
return save_data_factory->Open(space, save_struct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenSDMC() {
|
|
|
|
LOG_TRACE(Service_FS, "Opening SDMC");
|
|
|
|
|
|
|
|
if (sdmc_factory == nullptr) {
|
|
|
|
return ResultCode(ErrorModule::FS, FileSys::ErrCodes::SdCardNotFound);
|
2018-03-04 13:03:58 -05:00
|
|
|
}
|
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
return sdmc_factory->Open();
|
2018-03-04 13:03:58 -05:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:32:00 -05:00
|
|
|
void RegisterFileSystems() {
|
2018-07-17 15:42:15 -04:00
|
|
|
romfs_factory = nullptr;
|
|
|
|
save_data_factory = nullptr;
|
|
|
|
sdmc_factory = nullptr;
|
2018-02-19 00:32:00 -05:00
|
|
|
|
2018-07-07 23:24:51 -04:00
|
|
|
std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
|
2018-03-19 22:17:15 -04:00
|
|
|
std::string sd_directory = FileUtil::GetUserPath(D_SDMC_IDX);
|
2018-02-19 00:32:00 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
auto savedata = std::make_unique<FileSys::SaveDataFactory>(std::move(nand_directory));
|
|
|
|
save_data_factory = std::move(savedata);
|
2018-07-07 23:24:51 -04:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
auto sdcard = std::make_unique<FileSys::SDMCFactory>(std::move(sd_directory));
|
|
|
|
sdmc_factory = std::move(sdcard);
|
2018-01-16 22:20:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
2018-02-19 00:32:00 -05:00
|
|
|
RegisterFileSystems();
|
2018-01-16 22:20:12 -05:00
|
|
|
std::make_shared<FSP_SRV>()->InstallAsService(service_manager);
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::FileSystem
|