[Emulator] Improvements to launch_data

Added support for autoload titles on boot with:
- Specific launch data
- Specific xex
- Restarting into packages: XamContentLaunchImageFromFileInternal
This commit is contained in:
Gliniak
2024-02-10 22:38:21 +01:00
committed by Radosław Gliński
parent 7e7784347b
commit 6f287a9597
10 changed files with 275 additions and 116 deletions

View File

@@ -14,10 +14,13 @@
#include "xenia/kernel/user_module.h"
#include "xenia/kernel/util/shim_utils.h"
#include "xenia/kernel/xam/xam_content_device.h"
#include "xenia/kernel/xam/xam_module.h"
#include "xenia/kernel/xam/xam_private.h"
#include "xenia/kernel/xboxkrnl/xboxkrnl_module.h"
#include "xenia/kernel/xboxkrnl/xboxkrnl_threading.h"
#include "xenia/kernel/xenumerator.h"
#include "xenia/ui/imgui_dialog.h"
#include "xenia/ui/imgui_drawer.h"
#include "xenia/xbox.h"
DEFINE_int32(
@@ -498,6 +501,50 @@ dword_result_t XamLoaderGetMediaInfoEx_entry(dword_t unk1, dword_t unk2,
DECLARE_XAM_EXPORT1(XamLoaderGetMediaInfoEx, kContent, kStub);
dword_result_t XamContentLaunchImageFromFileInternal_entry(
lpstring_t image_location, lpstring_t xex_name, dword_t unk) {
const std::string image_path = image_location;
const std::string xex_name_ = xex_name;
vfs::Entry* entry = kernel_state()->file_system()->ResolvePath(image_path);
if (!entry) {
return 0;
}
const std::filesystem::path host_path =
kernel_state()->emulator()->content_root() / entry->name();
if (!std::filesystem::exists(host_path)) {
vfs::VirtualFileSystem::ExtractContentFile(
entry, kernel_state()->emulator()->content_root(), true);
}
auto xam = kernel_state()->GetKernelModule<XamModule>("xam.xex");
auto& loader_data = xam->loader_data();
loader_data.host_path = xe::path_to_utf8(host_path);
loader_data.launch_path = xex_name_;
xam->SaveLoaderData();
auto display_window = kernel_state()->emulator()->display_window();
auto imgui_drawer = kernel_state()->emulator()->imgui_drawer();
if (display_window && imgui_drawer) {
display_window->app_context().CallInUIThreadSynchronous([imgui_drawer]() {
xe::ui::ImGuiDialog::ShowMessageBox(
imgui_drawer, "Launching new title!",
"Launching new title. \nPlease close Xenia and launch it again. Game "
"should load automatically.");
});
}
kernel_state()->TerminateTitle();
return 0;
}
DECLARE_XAM_EXPORT1(XamContentLaunchImageFromFileInternal, kContent, kStub);
} // namespace xam
} // namespace kernel
} // namespace xe

View File

@@ -291,24 +291,6 @@ dword_result_t XamLoaderSetLaunchData_entry(lpvoid_t data, dword_t size) {
loader_data.launch_data_present = size ? true : false;
loader_data.launch_data.resize(size);
std::memcpy(loader_data.launch_data.data(), data, size);
// Because we have no way to restart game while it is working. Remove as soon
// as possible.
const std::filesystem::path launch_data_dir = "launch_data";
std::filesystem::path file_path =
launch_data_dir /
fmt::format("{:08X}_launch_data.bin", kernel_state()->title_id());
if (!std::filesystem::exists(launch_data_dir)) {
std::filesystem::create_directories(launch_data_dir);
}
auto file = xe::filesystem::OpenFile(file_path, "wb+");
if (file) {
fwrite(loader_data.launch_data.data(), size, 1, file);
fclose(file);
}
return 0;
}
DECLARE_XAM_EXPORT1(XamLoaderSetLaunchData, kNone, kSketchy);
@@ -357,15 +339,12 @@ void XamLoaderLaunchTitle_entry(lpstring_t raw_name_ptr, dword_t flags) {
if (path.empty()) {
loader_data.launch_path = "game:\\default.xex";
} else {
if (xe::utf8::find_name_from_guest_path(path) == path) {
path = xe::utf8::join_guest_paths(
xe::utf8::find_base_guest_path(
kernel_state()->GetExecutableModule()->path()),
path);
}
loader_data.launch_path = path;
loader_data.launch_path = xe::path_to_utf8(path);
loader_data.launch_data_present = true;
}
xam->SaveLoaderData();
if (loader_data.launch_data_present) {
auto display_window = kernel_state()->emulator()->display_window();
auto imgui_drawer = kernel_state()->emulator()->imgui_drawer();
@@ -375,8 +354,8 @@ void XamLoaderLaunchTitle_entry(lpstring_t raw_name_ptr, dword_t flags) {
[imgui_drawer]() {
xe::ui::ImGuiDialog::ShowMessageBox(
imgui_drawer, "Title was restarted",
"Title closed with new launch data. \nPlease close Xenia and "
"start title again.");
"Title closed with new launch data. \nPlease restart Xenia. "
"Game will be loaded automatically.");
});
}
}

View File

@@ -63,6 +63,82 @@ void XamModule::RegisterExportTable(xe::cpu::ExportResolver* export_resolver) {
XamModule::~XamModule() {}
void XamModule::LoadLoaderData() {
FILE* file = xe::filesystem::OpenFile(kXamModuleLoaderDataFileName, "rb");
if (!file) {
loader_data_.launch_data_present = false;
return;
}
loader_data_.launch_data_present = true;
auto string_read = [file]() {
uint16_t string_size = 0;
fread(&string_size, sizeof(string_size), 1, file);
std::string result_string;
result_string.resize(string_size);
fread(result_string.data(), string_size, 1, file);
return result_string;
};
loader_data_.host_path = string_read();
loader_data_.launch_path = string_read();
fread(&loader_data_.launch_flags, sizeof(loader_data_.launch_flags), 1, file);
uint16_t launch_data_size = 0;
fread(&launch_data_size, sizeof(launch_data_size), 1, file);
if (launch_data_size > 0) {
loader_data_.launch_data.resize(launch_data_size);
fread(loader_data_.launch_data.data(), launch_data_size, 1, file);
}
fclose(file);
// We read launch data. Let's remove it till next request.
std::filesystem::remove(kXamModuleLoaderDataFileName);
}
void XamModule::SaveLoaderData() {
FILE* file = xe::filesystem::OpenFile(kXamModuleLoaderDataFileName, "wb");
if (!file) {
return;
}
std::filesystem::path host_path = loader_data_.host_path;
if (host_path.extension() == ".xex") {
host_path.remove_filename();
host_path = host_path / loader_data_.launch_path;
loader_data_.launch_path = "";
}
const std::string host_path_as_string = xe::path_to_utf8(host_path);
const uint16_t host_path_length =
static_cast<uint16_t>(host_path_as_string.size());
fwrite(&host_path_length, sizeof(host_path_length), 1, file);
fwrite(host_path_as_string.c_str(), host_path_length, 1, file);
const uint16_t launch_path_length =
static_cast<uint16_t>(loader_data_.launch_path.size());
fwrite(&launch_path_length, sizeof(launch_path_length), 1, file);
fwrite(loader_data_.launch_path.c_str(), launch_path_length, 1, file);
fwrite(&loader_data_.launch_flags, sizeof(loader_data_.launch_flags), 1,
file);
const uint16_t launch_data_size =
static_cast<uint16_t>(loader_data_.launch_data.size());
fwrite(&launch_data_size, sizeof(launch_data_size), 1, file);
fwrite(loader_data_.launch_data.data(), launch_data_size, 1, file);
fclose(file);
}
} // namespace xam
} // namespace kernel
} // namespace xe

View File

@@ -23,6 +23,8 @@ namespace xam {
bool xeXamIsUIActive();
static const std::string kXamModuleLoaderDataFileName = "launch_data.bin";
class XamModule : public KernelModule {
public:
XamModule(Emulator* emulator, KernelState* kernel_state);
@@ -32,11 +34,17 @@ class XamModule : public KernelModule {
struct LoaderData {
bool launch_data_present = false;
std::vector<uint8_t> launch_data;
std::string host_path; // Full host path to next title to load
std::string
launch_path; // Full guest path to next xex. might be default.xex
uint32_t launch_flags = 0;
std::string launch_path; // Full path to next xex
std::vector<uint8_t> launch_data;
};
void LoadLoaderData();
void SaveLoaderData();
const LoaderData& loader_data() const { return loader_data_; }
LoaderData& loader_data() { return loader_data_; }