C++17ification.
C++17ification! - Filesystem interaction now uses std::filesystem::path. - Usage of const char*, std::string have been changed to std::string_view where appropriate. - Usage of printf-style functions changed to use fmt.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2015 Ben Vanik. All rights reserved. *
|
||||
* Copyright 2020 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "third_party/fmt/include/fmt/format.h"
|
||||
#include "xenia/base/filesystem.h"
|
||||
#include "xenia/base/string.h"
|
||||
#include "xenia/kernel/kernel_state.h"
|
||||
@@ -21,18 +22,18 @@ namespace xe {
|
||||
namespace kernel {
|
||||
namespace xam {
|
||||
|
||||
static const wchar_t* kThumbnailFileName = L"__thumbnail.png";
|
||||
static const char* kThumbnailFileName = "__thumbnail.png";
|
||||
|
||||
static const wchar_t* kGameUserContentDirName = L"profile";
|
||||
static const char* kGameUserContentDirName = "profile";
|
||||
|
||||
static int content_device_id_ = 0;
|
||||
|
||||
ContentPackage::ContentPackage(KernelState* kernel_state, std::string root_name,
|
||||
ContentPackage::ContentPackage(KernelState* kernel_state,
|
||||
const std::string_view root_name,
|
||||
const XCONTENT_DATA& data,
|
||||
std::wstring package_path)
|
||||
: kernel_state_(kernel_state), root_name_(std::move(root_name)) {
|
||||
device_path_ = std::string("\\Device\\Content\\") +
|
||||
std::to_string(++content_device_id_) + "\\";
|
||||
const std::filesystem::path& package_path)
|
||||
: kernel_state_(kernel_state), root_name_(root_name) {
|
||||
device_path_ = fmt::format("\\Device\\Content\\{0}\\", ++content_device_id_);
|
||||
|
||||
auto fs = kernel_state_->file_system();
|
||||
auto device =
|
||||
@@ -49,53 +50,49 @@ ContentPackage::~ContentPackage() {
|
||||
}
|
||||
|
||||
ContentManager::ContentManager(KernelState* kernel_state,
|
||||
std::wstring root_path)
|
||||
: kernel_state_(kernel_state), root_path_(std::move(root_path)) {}
|
||||
const std::filesystem::path& root_path)
|
||||
: kernel_state_(kernel_state), root_path_(root_path) {}
|
||||
|
||||
ContentManager::~ContentManager() = default;
|
||||
|
||||
std::wstring ContentManager::ResolvePackageRoot(uint32_t content_type) {
|
||||
wchar_t title_id[9] = L"00000000";
|
||||
std::swprintf(title_id, 9, L"%.8X", kernel_state_->title_id());
|
||||
std::filesystem::path ContentManager::ResolvePackageRoot(
|
||||
uint32_t content_type) {
|
||||
auto title_id = fmt::format("{:8X}", kernel_state_->title_id());
|
||||
|
||||
std::wstring type_name;
|
||||
std::string type_name;
|
||||
switch (content_type) {
|
||||
case 1:
|
||||
// Save games.
|
||||
type_name = L"00000001";
|
||||
type_name = "00000001";
|
||||
break;
|
||||
case 2:
|
||||
// DLC from the marketplace.
|
||||
type_name = L"00000002";
|
||||
type_name = "00000002";
|
||||
break;
|
||||
case 3:
|
||||
// Publisher content?
|
||||
type_name = L"00000003";
|
||||
type_name = "00000003";
|
||||
break;
|
||||
case 0x000D0000:
|
||||
// ???
|
||||
type_name = L"000D0000";
|
||||
type_name = "000D0000";
|
||||
break;
|
||||
default:
|
||||
assert_unhandled_case(data.content_type);
|
||||
return nullptr;
|
||||
return std::filesystem::path();
|
||||
}
|
||||
|
||||
// Package root path:
|
||||
// content_root/title_id/type_name/
|
||||
auto package_root =
|
||||
xe::join_paths(root_path_, xe::join_paths(title_id, type_name));
|
||||
return package_root + xe::kWPathSeparator;
|
||||
return root_path_ / title_id / type_name;
|
||||
}
|
||||
|
||||
std::wstring ContentManager::ResolvePackagePath(const XCONTENT_DATA& data) {
|
||||
std::filesystem::path ContentManager::ResolvePackagePath(
|
||||
const XCONTENT_DATA& data) {
|
||||
// Content path:
|
||||
// content_root/title_id/type_name/data_file_name/
|
||||
auto package_root = ResolvePackageRoot(data.content_type);
|
||||
auto package_path =
|
||||
xe::join_paths(package_root, xe::to_wstring(data.file_name));
|
||||
package_path += xe::kPathSeparator;
|
||||
return package_path;
|
||||
return package_root / xe::to_path(data.file_name);
|
||||
}
|
||||
|
||||
std::vector<XCONTENT_DATA> ContentManager::ListContent(uint32_t device_id,
|
||||
@@ -114,8 +111,8 @@ std::vector<XCONTENT_DATA> ContentManager::ListContent(uint32_t device_id,
|
||||
XCONTENT_DATA content_data;
|
||||
content_data.device_id = device_id;
|
||||
content_data.content_type = content_type;
|
||||
content_data.display_name = file_info.name;
|
||||
content_data.file_name = xe::to_string(file_info.name);
|
||||
content_data.display_name = xe::path_to_utf16(file_info.name);
|
||||
content_data.file_name = xe::path_to_utf8(file_info.name);
|
||||
result.emplace_back(std::move(content_data));
|
||||
}
|
||||
|
||||
@@ -123,7 +120,7 @@ std::vector<XCONTENT_DATA> ContentManager::ListContent(uint32_t device_id,
|
||||
}
|
||||
|
||||
std::unique_ptr<ContentPackage> ContentManager::ResolvePackage(
|
||||
std::string root_name, const XCONTENT_DATA& data) {
|
||||
const std::string_view root_name, const XCONTENT_DATA& data) {
|
||||
auto package_path = ResolvePackagePath(data);
|
||||
if (!xe::filesystem::PathExists(package_path)) {
|
||||
return nullptr;
|
||||
@@ -141,11 +138,11 @@ bool ContentManager::ContentExists(const XCONTENT_DATA& data) {
|
||||
return xe::filesystem::PathExists(path);
|
||||
}
|
||||
|
||||
X_RESULT ContentManager::CreateContent(std::string root_name,
|
||||
X_RESULT ContentManager::CreateContent(const std::string_view root_name,
|
||||
const XCONTENT_DATA& data) {
|
||||
auto global_lock = global_critical_region_.Acquire();
|
||||
|
||||
if (open_packages_.count(root_name)) {
|
||||
if (open_packages_.count(string_key(root_name))) {
|
||||
// Already content open with this root name.
|
||||
return X_ERROR_ALREADY_EXISTS;
|
||||
}
|
||||
@@ -163,16 +160,16 @@ X_RESULT ContentManager::CreateContent(std::string root_name,
|
||||
auto package = ResolvePackage(root_name, data);
|
||||
assert_not_null(package);
|
||||
|
||||
open_packages_.insert({root_name, package.release()});
|
||||
open_packages_.insert({string_key::create(root_name), package.release()});
|
||||
|
||||
return X_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
X_RESULT ContentManager::OpenContent(std::string root_name,
|
||||
X_RESULT ContentManager::OpenContent(const std::string_view root_name,
|
||||
const XCONTENT_DATA& data) {
|
||||
auto global_lock = global_critical_region_.Acquire();
|
||||
|
||||
if (open_packages_.count(root_name)) {
|
||||
if (open_packages_.count(string_key(root_name))) {
|
||||
// Already content open with this root name.
|
||||
return X_ERROR_ALREADY_EXISTS;
|
||||
}
|
||||
@@ -187,15 +184,15 @@ X_RESULT ContentManager::OpenContent(std::string root_name,
|
||||
auto package = ResolvePackage(root_name, data);
|
||||
assert_not_null(package);
|
||||
|
||||
open_packages_.insert({root_name, package.release()});
|
||||
open_packages_.insert({string_key::create(root_name), package.release()});
|
||||
|
||||
return X_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
X_RESULT ContentManager::CloseContent(std::string root_name) {
|
||||
X_RESULT ContentManager::CloseContent(const std::string_view root_name) {
|
||||
auto global_lock = global_critical_region_.Acquire();
|
||||
|
||||
auto it = open_packages_.find(root_name);
|
||||
auto it = open_packages_.find(string_key(root_name));
|
||||
if (it == open_packages_.end()) {
|
||||
return X_ERROR_FILE_NOT_FOUND;
|
||||
}
|
||||
@@ -211,7 +208,7 @@ X_RESULT ContentManager::GetContentThumbnail(const XCONTENT_DATA& data,
|
||||
std::vector<uint8_t>* buffer) {
|
||||
auto global_lock = global_critical_region_.Acquire();
|
||||
auto package_path = ResolvePackagePath(data);
|
||||
auto thumb_path = xe::join_paths(package_path, kThumbnailFileName);
|
||||
auto thumb_path = package_path / kThumbnailFileName;
|
||||
if (xe::filesystem::PathExists(thumb_path)) {
|
||||
auto file = xe::filesystem::OpenFile(thumb_path, "rb");
|
||||
fseek(file, 0, SEEK_END);
|
||||
@@ -232,7 +229,7 @@ X_RESULT ContentManager::SetContentThumbnail(const XCONTENT_DATA& data,
|
||||
auto package_path = ResolvePackagePath(data);
|
||||
xe::filesystem::CreateFolder(package_path);
|
||||
if (xe::filesystem::PathExists(package_path)) {
|
||||
auto thumb_path = xe::join_paths(package_path, kThumbnailFileName);
|
||||
auto thumb_path = package_path / kThumbnailFileName;
|
||||
auto file = xe::filesystem::OpenFile(thumb_path, "wb");
|
||||
fwrite(buffer.data(), 1, buffer.size(), file);
|
||||
fclose(file);
|
||||
@@ -254,18 +251,13 @@ X_RESULT ContentManager::DeleteContent(const XCONTENT_DATA& data) {
|
||||
}
|
||||
}
|
||||
|
||||
std::wstring ContentManager::ResolveGameUserContentPath() {
|
||||
wchar_t title_id[9] = L"00000000";
|
||||
std::swprintf(title_id, 9, L"%.8X", kernel_state_->title_id());
|
||||
auto user_name = xe::to_wstring(kernel_state_->user_profile()->name());
|
||||
std::filesystem::path ContentManager::ResolveGameUserContentPath() {
|
||||
auto title_id = fmt::format("{:8X}", kernel_state_->title_id());
|
||||
auto user_name = xe::to_path(kernel_state_->user_profile()->name());
|
||||
|
||||
// Per-game per-profile data location:
|
||||
// content_root/title_id/profile/user_name
|
||||
auto package_root = xe::join_paths(
|
||||
root_path_,
|
||||
xe::join_paths(title_id,
|
||||
xe::join_paths(kGameUserContentDirName, user_name)));
|
||||
return package_root + xe::kWPathSeparator;
|
||||
return root_path_ / title_id / kGameUserContentDirName / user_name;
|
||||
}
|
||||
|
||||
} // namespace xam
|
||||
|
||||
Reference in New Issue
Block a user