Skip to content

Commit

Permalink
Fix league game submission failing with the Windows Store version of …
Browse files Browse the repository at this point in the history
…the game
  • Loading branch information
timoschwarzer committed Sep 2, 2024
1 parent 57670ca commit 1a4b280
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 38 deletions.
4 changes: 3 additions & 1 deletion projects/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ set(
"api/messages/text_style.cpp"
"api/moon_animator_events.cpp"
"api/scenes/create_objects.cpp"
"api/scenes/scene_load.cpp"
"api/scenes/polygon.cpp"
"api/scenes/scene_load.cpp"
"api/system/save_files.cpp"
"api/audio.cpp"
"api/screen_position.cpp"
"api/uber_states/uber_state.cpp"
Expand Down Expand Up @@ -106,6 +107,7 @@ set(
"api/scenes/scene_load.h"
"api/scenes/polygon.h"
"api/system/message_provider.h"
"api/system/save_files.h"
"api/audio.h"
"api/screen_position.h"
"api/uber_states/uber_state.h"
Expand Down
26 changes: 26 additions & 0 deletions projects/Core/api/system/save_files.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <Core/api/game/game.h>
#include <Core/api/system/save_files.h>
#include <Core/utils/byte_stream.h>
#include <Modloader/app/methods/Grdk/Wrapper.h>
#include <Modloader/app/methods/SaveGameController.h>
#include <Modloader/app/methods/System/IO/File.h>

using namespace app::classes;

namespace core::api::save_files {
app::Byte__Array* get_byte_array(int slot_index, int backup_index) {
if (Grdk::Wrapper::get_InitializedOk()) { // Handle GRDK (Xbox Account) saves
return Grdk::Wrapper::Load_1(slot_index, backup_index);
}

auto save_info = SaveGameController::GetSaveFileInfo(game::save_controller(), slot_index, backup_index);
auto path = save_info->fields.m_FullBackupSaveFilePath;
auto path_str = il2cpp::convert_csstring(path);
return System::IO::File::ReadAllBytes(path);
}

std::vector<std::byte> get_bytes(int slot_index, int backup_index) {
return utils::ByteStream(get_byte_array(slot_index, backup_index)).peek_to_end();
}
}

21 changes: 21 additions & 0 deletions projects/Core/api/system/save_files.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <vector>

namespace core::api::save_files {
/**
* Get the on-disk save data for a save file.
* @param slot_index Save slot index
* @param backup_index Backup slot index. Set this to -1 to request the current game save file.
* @return Pointer to an il2cpp array of System.Byte.
*/
CORE_DLLEXPORT app::Byte__Array* get_byte_array(int slot_index, int backup_index = -1);

/**
* Get the on-disk save data for a save file.
* @param slot_index Save slot index
* @param backup_index Backup slot index. Set this to -1 to request the current game save file.
* @return Byte vector containing the on-disk save data
*/
CORE_DLLEXPORT std::vector<std::byte> get_bytes(int slot_index, int backup_index = -1);
}
20 changes: 7 additions & 13 deletions projects/Core/save_meta/save_meta.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include <Core/api/game/game.h>
#include <Core/api/system/save_files.h>
#include <Core/save_meta/save_meta.h>
#include <Core/utils/byte_stream.h>
#include <Modloader/app/methods/DeathUberStateManager.h>
#include <Modloader/app/methods/Grdk/Wrapper.h>
#include <Modloader/app/methods/Moon/UberStateValueStore.h>
#include <Modloader/app/methods/SaveGameController.h>
#include <Modloader/app/methods/SaveSlotsManager.h>
#include <Modloader/app/methods/System/IO/File.h>
#include <Modloader/app/methods/Grdk/Wrapper.h>
#include <Modloader/app/types/Byte.h>
#include <Modloader/interception_macros.h>
#include <Modloader/modloader.h>
Expand Down Expand Up @@ -268,18 +269,11 @@ namespace core::save_meta {
// ...and then load SaveMeta with the ThroughDeathsAndQTMsAndBackups persistence
// level from the backup save file if we're on a new/different save slot
if (current_save_guid != previous_save_guid) {
app::Byte__Array* bytes;

if (Grdk::Wrapper::get_InitializedOk()) { // Handle GRDK (Xbox Account) saves
bytes = Grdk::Wrapper::Load_1(save_slot_index, backup_slot);
} else {
auto save_info = SaveGameController::GetSaveFileInfo(this_ptr, save_slot_index, backup_slot);
auto path = save_info->fields.m_FullBackupSaveFilePath;
auto path_str = il2cpp::convert_csstring(path);
bytes = System::IO::File::ReadAllBytes(path);
}

read_save_meta_from_byte_array(bytes, true, SaveMetaSlotPersistence::ThroughDeathsAndQTMsAndBackups);
read_save_meta_from_byte_array(
api::save_files::get_byte_array(save_slot_index, backup_slot),
true,
SaveMetaSlotPersistence::ThroughDeathsAndQTMsAndBackups
);
}

return return_value;
Expand Down
15 changes: 8 additions & 7 deletions projects/Randomizer/league/league.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
#include <Randomizer/features/credits.h>

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include <Core/api/system/save_files.h>
#include <Core/ipc/ipc.h>
#include <Modloader/app/methods/Grdk/Wrapper.h>
#include <Modloader/app/methods/System/IO/File.h>
#include <httplib.h>
#undef MessageBox

Expand Down Expand Up @@ -173,7 +176,7 @@ namespace randomizer::league {
status_message_mutex.unlock();
}

void start_submission_thread(const std::string& save_file_path) {
void start_submission_thread(const int save_slot_index) {
using namespace std::chrono_literals;

if (!get_multiverse_id().has_value()) {
Expand All @@ -195,11 +198,10 @@ namespace randomizer::league {
const auto multiverse_id = get_multiverse_id().value();
const auto jwt = randomizer::online::get_jwt();

submit_thread = std::make_shared<std::thread>([host, insecure, multiverse_id, jwt, save_file_path]() {
submit_thread = std::make_shared<std::thread>([host, insecure, multiverse_id, jwt, save_slot_index]() {
upload_attempt.store(0);

std::ifstream input(save_file_path, std::ios::binary);
std::vector<char> save_file_data(std::istreambuf_iterator<char>(input), {});
auto save_file_data = core::api::save_files::get_bytes(save_slot_index);

while (submission_status.load() == SubmissionStatus::Uploading) {
const auto attempt = upload_attempt.fetch_add(1) + 1;
Expand All @@ -215,7 +217,7 @@ namespace randomizer::league {
auto result = client.Post(std::format("/api/league/{}/submission", multiverse_id), {
{"User-Agent", std::format("OriAndTheWillOfTheWispsRandomizer/{}", randomizer_version().to_string())},
{"Authorization", std::format("Bearer {}", jwt)}
}, save_file_data.data(), save_file_data.size(), "application/octet-stream");
}, reinterpret_cast<char*>(save_file_data.data()), save_file_data.size(), "application/octet-stream");

if (result.error() != httplib::Error::Success) {
set_status_message_thread_safe("Request failed");
Expand Down Expand Up @@ -319,8 +321,7 @@ namespace randomizer::league {
core::api::audio::play_sound(SoundEventID::LeagueSubmitted);

const auto save_controller = core::api::game::save_controller();
const auto current_save_file_path = il2cpp::convert_csstring(SaveGameController::get_CurrentSaveFileInfo(save_controller)->fields.m_FullSaveFilePath);
start_submission_thread(current_save_file_path);
start_submission_thread(SaveGameController::get_CurrentSlotIndex(save_controller));
});
} else {
const auto resolved_epilogue_timeline = il2cpp::invoke<app::MoonTimeline>(shriek_entity->fields.EpilogueTimeline, "Resolve", 0);
Expand Down
24 changes: 7 additions & 17 deletions projects/Randomizer/ui/main_menu_seed_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@
#include <Core/api/graphics/sprite.h>
#include <Core/api/messages/text_style.h>
#include <Core/api/scenes/scene_load.h>
#include <Core/api/system/save_files.h>
#include <Core/events/action.h>
#include <Core/utils/misc.h>
#include <Modloader/app/methods/CleverMenuItem.h>
#include <Modloader/app/methods/CleverMenuItemSelectionManager.h>
#include <Modloader/app/methods/GameController.h>
#include <Modloader/app/methods/GameStateMachine.h>
#include <Modloader/app/methods/Grdk/Wrapper.h>
#include <Modloader/app/methods/MessageBox.h>
#include <Modloader/app/methods/SaveGameController.h>
#include <Modloader/app/methods/SaveSlotsManager.h>
#include <Modloader/app/methods/SaveSlotsUI.h>
#include <Modloader/app/methods/SetTitleScreenAction.h>
#include <Modloader/app/methods/System/IO/File.h>
#include <Modloader/app/methods/CleverMenuItemSelectionManager.h>
#include <Modloader/app/methods/GameController.h>
#include <Modloader/app/methods/Grdk/Wrapper.h>
#include <Modloader/app/types/CleverMenuItem.h>
#include <Modloader/app/types/GameStateMachine.h>
#include <Modloader/app/types/MessageBox.h>
#include <Modloader/app/types/XboxLiveIdentityUI.h>
#include <Modloader/modloader.h>
#include <Randomizer/game/spawning_and_preloading.h>
#include <Randomizer/randomizer.h>
#include <Randomizer/seed/legacy_parser/parser.h>
#include <magic_enum.hpp>
#include <Core/events/action.h>
#include <Randomizer/game/spawning_and_preloading.h>

using namespace utils;
using namespace app::classes;
Expand Down Expand Up @@ -432,20 +433,9 @@ namespace randomizer::main_menu_seed_info {
if (SaveSlotsManager::SaveFileExists(index)) {
on_seed_loaded_handle = nullptr;

const auto save_controller = core::api::game::save_controller();

app::Byte__Array* data;

if (Grdk::Wrapper::get_InitializedOk()) { // Handle GRDK (Xbox live) saves
data = Grdk::Wrapper::Load_1(index, -1);
} else {
const auto save_info = SaveGameController::GetSaveFileInfo(save_controller, index, -1);
data = System::IO::File::ReadAllBytes(save_info->fields.m_FullSaveFilePath);
}

auto seed_meta_data = std::make_shared<seed::SaveSlotSeedMetaData>();
const auto read_slots = core::save_meta::read_save_meta_slots_from_byte_array(
data,
core::api::save_files::get_byte_array(index, -1),
{
{SaveMetaSlot::SeedMetaData, seed_meta_data}
}
Expand Down

0 comments on commit 1a4b280

Please sign in to comment.