Skip to content

Commit

Permalink
feat(saving): add save dialog
Browse files Browse the repository at this point in the history
- changed ui naming a bit
  • Loading branch information
szaaamerik authored Sep 18, 2024
1 parent 20b230d commit 3d67174
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 81 deletions.
2 changes: 2 additions & 0 deletions forza_saveswapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ add_executable(forza_saveswapper
encryption/keys/Fh4Keys.cpp
encryption/keys/Fh4Keys.hpp
encryption/keys/Fh5Keys.hpp
fileUtilities/TFileSaveResult.hpp
fileUtilities/TFileSaveResult.hpp
)

target_link_libraries(forza_saveswapper PRIVATE
Expand Down
24 changes: 12 additions & 12 deletions forza_saveswapper/application/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

namespace forza_saveswapper {

class Window;
class Window;

class Application {
public:
explicit Application(const std::string& windowName);
~Application();
class Application {
public:
explicit Application(const std::string& windowName);
~Application();

void Run() const;
void Run() const;

private:
void Loop() const;
private:
void Loop() const;

private:
std::string m_WindowName;
std::unique_ptr<Window> m_Window;
};
private:
std::string m_WindowName;
std::unique_ptr<Window> m_Window;
};

} // forza_saveswapper

Expand Down
45 changes: 25 additions & 20 deletions forza_saveswapper/application/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
//

#include "Window.hpp"

#include "../fileUtilities/FileDialogs.hpp"
#include "../swapper./CBinaryStream.hpp"
#include "../encryption/streams/CFh3Stream.hpp"
#include "../swapper/CBinaryStream.hpp"
#include "../swapper/CCompileTimeHash.hpp"
#include "../swapper/CRuntimeHash.hpp"

#include <imgui_internal.h>
#include <iostream>
#include <thread>
#include <utility>
#include <filesystem>
#include <fstream>
#include <winuser.h>

#include <imgui_internal.h>
#include <backends/imgui_impl_dx11.h>
#include <backends/imgui_impl_win32.h>
#include <winuser.h>

#include "../swapper/CCompileTimeHash.hpp"
#include "../swapper/CRuntimeHash.hpp"

extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(
HWND window,
Expand Down Expand Up @@ -115,10 +114,12 @@ namespace forza_saveswapper {
m_Application(application),
m_Name (std::move(name)),
m_IsRunning(true) {
m_FileOpenReuslt.SelectedFileName = "No save file picked!";
m_OpenSaveFilename = "No save file picked!";
}

bool Window::Initialize() {
constexpr auto className = "class001";

m_WindowClass.cbSize = sizeof(WNDCLASSEX);
m_WindowClass.style = CS_CLASSDC;
m_WindowClass.lpfnWndProc = WindowProcess;
Expand All @@ -129,13 +130,13 @@ namespace forza_saveswapper {
m_WindowClass.hCursor = nullptr;
m_WindowClass.hbrBackground = nullptr;
m_WindowClass.lpszMenuName = nullptr;
m_WindowClass.lpszClassName = "class001";
m_WindowClass.lpszClassName = className;
m_WindowClass.hIconSm = nullptr;

RegisterClassEx(&m_WindowClass);
m_Window = CreateWindowEx(
0,
"class001",
className,
m_Name.c_str(),
WS_POPUP,
100,
Expand Down Expand Up @@ -258,19 +259,23 @@ namespace forza_saveswapper {
}

void Window::OpenSave() {
m_FileOpenReuslt = FileDialogs::OpenFile();
if (!m_FileOpenReuslt.Succeeded) {
const auto openResult = FileDialogs::OpenFile();
if (!openResult.Succeeded) {
MessageBoxA(nullptr, "The save file opening failed!", "Error", MB_ICONERROR);
return;
}

m_OpenSavePath = openResult.Path;
m_OpenSaveFilename = openResult.SelectedFileName;
}

void Window::SwapSave(const uint64_t newXuid) const {
if (!m_FileOpenReuslt.Succeeded) {
if (m_OpenSavePath.empty()) {
MessageBoxA(nullptr, "The file open must succeed first!", "Error", MB_ICONERROR);
return;
}

const std::filesystem::path inputPath(m_FileOpenReuslt.Path);
const std::filesystem::path inputPath(m_OpenSavePath);
if (!exists(inputPath)) {
MessageBoxA(nullptr, "The input save doesn't exist anymore!", "Error", MB_ICONERROR);
return;
Expand Down Expand Up @@ -323,15 +328,15 @@ namespace forza_saveswapper {
const auto es = std::make_unique<CFh3EncryptionStream>(*output2, input_size, std::array<uint8_t, 16>{ }, s_Contexts[0]);
es->WriteData(*output);

std::ofstream file(m_FileOpenReuslt.SelectedFileName, std::ios::binary);
const auto fileSaveRes = FileDialogs::SaveFile();
std::ofstream file(fileSaveRes.Path, std::ios::binary);
if (!file) {
MessageBoxA(nullptr, "Failed to open output file!", "Error", MB_ICONERROR);
return;
}

std::string contents = output2->str();
file.write(contents.c_str(), contents.size());

file.write(contents.c_str(), static_cast<std::streamsize>(contents.size()));
if (file.fail()) {
MessageBoxA(nullptr, "Failed to write to output file!", "Error", MB_ICONERROR);
return;
Expand Down Expand Up @@ -403,13 +408,13 @@ namespace forza_saveswapper {
const auto buttonWidth = ImVec2(ImGui::GetContentRegionAvail().x, 0);

ImGui::Text("Open save: ");
if (ImGui::Button(m_FileOpenReuslt.SelectedFileName.c_str(), buttonWidth)) {
if (ImGui::Button(m_OpenSaveFilename.c_str(), buttonWidth)) {
OpenSave();
}

static uint64_t xuid = 0;

ImGui::Text("Enter XUID");
ImGui::Text("Enter XUID (In hex)");
ImGui::InputScalar("##xuid", ImGuiDataType_U64, &xuid, nullptr, nullptr, "%016llX");
ImGui::SameLine();
if (ImGui::Button("Grab XUID", ImVec2(ImGui::GetContentRegionAvail().x, 0))) {
Expand All @@ -423,7 +428,7 @@ namespace forza_saveswapper {
);
}

if (ImGui::Button("Swap!", buttonWidth)) {
if (ImGui::Button("Export", buttonWidth)) {
SwapSave(xuid);
}
ImGui::End();
Expand Down
94 changes: 47 additions & 47 deletions forza_saveswapper/application/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,74 @@
#define GUI_HPP

#include "Application.hpp"
#include "../fileUtilities/TFileOpenResult.hpp"

#include <memory>
#include <d3d11.h>

namespace forza_saveswapper {

class Window
{
public:
static constexpr int WIDTH = 320;
static constexpr int HEIGHT = 170;
class Window
{
public:
static constexpr int WIDTH = 320;
static constexpr int HEIGHT = 170;

explicit Window(Application& application, std::string name);
explicit Window(Application& application, std::string name);

bool Initialize();
void Cleanup();
bool Initialize();
void Cleanup();

void BeginRender() noexcept;
void EndRender() const noexcept;
void Render() noexcept;
void ResetDevice() const noexcept;
void BeginRender() noexcept;
void EndRender() const noexcept;
void Render() noexcept;
void ResetDevice() const noexcept;

[[nodiscard]] IDXGISwapChain* GetSwapchain() const {
return m_SwapChain;
}
[[nodiscard]] IDXGISwapChain* GetSwapchain() const {
return m_SwapChain;
}

[[nodiscard]] POINTS GetPosition() const {
return m_Position;
}
[[nodiscard]] POINTS GetPosition() const {
return m_Position;
}

void SetPosition(const POINTS newValue) {
m_Position = newValue;
}
void SetPosition(const POINTS newValue) {
m_Position = newValue;
}

[[nodiscard]] HWND GetWindow() const {
return m_Window;
}
[[nodiscard]] HWND GetWindow() const {
return m_Window;
}

[[nodiscard]] bool GetIsRunning() const {
return m_IsRunning;
}
[[nodiscard]] bool GetIsRunning() const {
return m_IsRunning;
}

private:
void CreateDevice() noexcept;
void DestroyDevice() const noexcept;
void CreateImGui() const noexcept;
private:
void CreateDevice() noexcept;
void DestroyDevice() const noexcept;
void CreateImGui() const noexcept;

static void DestroyImGui() noexcept;
static void DestroyImGui() noexcept;

void OpenSave();
void SwapSave(uint64_t newXuid) const;
void OpenSave();
void SwapSave(uint64_t newXuid) const;

private:
HWND m_Window;
WNDCLASSEX m_WindowClass{};
POINTS m_Position{};
private:
HWND m_Window;
WNDCLASSEX m_WindowClass{};
POINTS m_Position{};

ID3D11Device* m_Device;
ID3D11DeviceContext* m_DeviceContext;
IDXGISwapChain* m_SwapChain;
ID3D11Device* m_Device;
ID3D11DeviceContext* m_DeviceContext;
IDXGISwapChain* m_SwapChain;

Application& m_Application;
std::string m_Name;
bool m_IsRunning;
Application& m_Application;
std::string m_Name;
bool m_IsRunning;

TFileOpenResult m_FileOpenReuslt;
};
std::string m_OpenSavePath;
std::string m_OpenSaveFilename;
};

} // forza_saveswapper

Expand Down
49 changes: 49 additions & 0 deletions forza_saveswapper/fileUtilities/FileDialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,53 @@ namespace forza_saveswapper {
return result;
}

TFileSaveResult FileDialogs::SaveFile() {
TFileSaveResult result{};
HRESULT f_SysHr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(f_SysHr)) {
return result;
}

IFileSaveDialog* f_FileSystem;
f_SysHr = CoCreateInstance(CLSID_FileSaveDialog, nullptr, CLSCTX_ALL, IID_IFileSaveDialog, reinterpret_cast<void**>(&f_FileSystem));
if (FAILED(f_SysHr)) {
CoUninitialize();
return result;
}

f_SysHr = f_FileSystem->Show(nullptr);
if (FAILED(f_SysHr)) {
f_FileSystem->Release();
CoUninitialize();
return result;
}

IShellItem* f_Files;
f_SysHr = f_FileSystem->GetResult(&f_Files);
if (FAILED(f_SysHr)) {
f_FileSystem->Release();
CoUninitialize();
return result;
}

PWSTR f_Path;
f_SysHr = f_Files->GetDisplayName(SIGDN_FILESYSPATH, &f_Path);
if (FAILED(f_SysHr)) {
f_Files->Release();
f_FileSystem->Release();
CoUninitialize();
return result;
}

std::wstring wPath(f_Path);
const std::string path(wPath.begin(), wPath.end());
result.Path = path;

CoTaskMemFree(f_Path);
f_Files->Release();
f_FileSystem->Release();
CoUninitialize();
result.Succeeded = true;
return result;
}
} // forza_saveswapper
3 changes: 3 additions & 0 deletions forza_saveswapper/fileUtilities/FileDialogs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

#ifndef FILEDIALOGS_HPP
#define FILEDIALOGS_HPP

#include "TFileOpenResult.hpp"
#include "TFileSaveResult.hpp"

namespace forza_saveswapper::FileDialogs {

TFileOpenResult OpenFile();
TFileSaveResult SaveFile();

} // forza_saveswapper

Expand Down
19 changes: 19 additions & 0 deletions forza_saveswapper/fileUtilities/TFileSaveResult.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by merika on 9/18/2024.
//

#ifndef TFILESAVERESULT_HPP
#define TFILESAVERESULT_HPP

#include <string>

namespace forza_saveswapper {

struct TFileSaveResult {
bool Succeeded = false;
std::string Path;
};

} // forza_saveswapper

#endif //TFILESAVERESULT_HPP
5 changes: 3 additions & 2 deletions forza_saveswapper/swapper/CBinaryStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ namespace forza_saveswapper {

class CBinaryStream {
public:
explicit CBinaryStream(const std::shared_ptr<std::iostream>& stream)
: m_Stream(stream) {}
explicit CBinaryStream(const std::shared_ptr<std::iostream>& stream) : m_Stream(stream) {

}

template <typename T>
T Read() {
Expand Down

0 comments on commit 3d67174

Please sign in to comment.