Skip to content

Commit

Permalink
feat: DirectX reset event
Browse files Browse the repository at this point in the history
  • Loading branch information
zziger committed Sep 17, 2024
1 parent 772ff8d commit 490173c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/lua/library/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ local disableEvent = __disableEvent --[[@as fun(event: string)]]
---@overload fun(eventName: "soundsLoaded", fn: eventHandler<Event>)
---@overload fun(eventName: "beforeTick", fn: eventHandler<Event>)
---@overload fun(eventName: "afterTick", fn: eventHandler<Event>)
---@overload fun(eventName: "dxInit", fn: eventHandler<Event>)
---@overload fun(eventName: "beforeDxReset", fn: eventHandler<Event>)
---@overload fun(eventName: "afterDxReset", fn: eventHandler<Event>)
---@overload fun(eventName: "uiRender", fn: eventHandler<Event>)
---@overload fun(eventName: "modLoad", fn: eventHandler<ModEvent>)
---@overload fun(eventName: "modUnload", fn: eventHandler<ModEvent>)
Expand Down
4 changes: 2 additions & 2 deletions src/supermod/builtin/ModImplBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <assets/assets.h>
#include <supermod/UpdateManager.hpp>
#include <supermod/events/D3dInitEvent.hpp>
#include <supermod/events/DxInitEvent.hpp>
#include <supermod/game/textures/PngLoader.hpp>
#include <supermod/modloader/mod/Mod.hpp>
#include <supermod/ui/NotificationManager.hpp>
Expand Down Expand Up @@ -44,7 +44,7 @@ std::shared_ptr<modloader::Mod> ModImplBuiltin::CreateMod()
info->version = semver::version::parse(SUPERMOD_VERSION);
info->description = "Встроенные в мод патчи игры";

EventManager::On<D3dInitEvent>([info] {
EventManager::On<DxInitEvent>([info] {
const auto iconData = *utils::read_resource(RES_LOGO);
const std::vector<byte> iconBuf(iconData.begin(), iconData.end());
vector2ui iconSize{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace sm
{
struct D3dInitEvent final : IEvent<"d3dInit", D3dInitEvent>
struct DxInitEvent final : IEvent<"dxInit", DxInitEvent>
{
};

Expand All @@ -17,8 +17,8 @@ HOOK_FN_CONV(inline char, d3d_init, ARGS(HWND a1), __cdecl)
const auto res = d3d_init_orig(a1);
if (res)
{
spdlog::trace("D3dInit event");
EventManager::Emit(D3dInitEvent{});
spdlog::trace("DxInit event");
EventManager::Emit(DxInitEvent{});
}
return res;
}
Expand Down
29 changes: 29 additions & 0 deletions src/supermod/events/DxResetEvent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include <supermod/pch.hpp>

#include <spdlog/spdlog.h>
#include <supermod/Utils.hpp>
#include <supermod/events/EventManager.hpp>
#include <supermod/memory/HookManager.hpp>

namespace sm
{
struct BeforeDxReset final : IEvent<"beforeDxReset", BeforeDxReset>
{
};
struct AfterDxReset final : IEvent<"afterDxReset", AfterDxReset>
{
};

HOOK_FN(inline int, d3d_reset, ARGS())
{
EventManager::Emit(BeforeDxReset());
const auto res = d3d_reset_orig();
EventManager::Emit(AfterDxReset());
return res;
}

inline EventManager::Ready $d3d_reset_event_hook([] {
HookManager::RegisterHook("55 8B EC 83 EC ? E8 ? ? ? ? A1", HOOK_REF(d3d_reset));
});
} // namespace sm
6 changes: 5 additions & 1 deletion src/supermod/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "main.h"

#include "game/DirectX.hpp"
#include "modloader/install/ModInstaller.hpp"
#include "registry/RegistryManager.hpp"
#include "utils/WindowsRegistry.hpp"
Expand All @@ -8,6 +9,8 @@

#include <async/task.h>
#include <cpr/cpr.h>
#include <d3dx9.h>
#include <d3dx9shader.h>
#include <filesystem>
#include <gdiplus.h>
#include <locale>
Expand All @@ -18,7 +21,8 @@
#include <supermod/Utils.hpp>
#include <supermod/builtin/ModImplBuiltin.hpp>
#include <supermod/constants.hpp>
#include <supermod/events/D3dInitEvent.hpp>
#include <supermod/events/DxInitEvent.hpp>
#include <supermod/events/DxResetEvent.hpp>
#include <supermod/events/EventManager.hpp>
#include <supermod/events/GameLoadedEvent.hpp>
#include <supermod/events/ResolveFileEvent.hpp>
Expand Down
4 changes: 2 additions & 2 deletions src/supermod/modloader/ModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <supermod/Config.hpp>
#include <supermod/builtin/ModImplBuiltin.hpp>
#include <supermod/events/D3dInitEvent.hpp>
#include <supermod/events/DxInitEvent.hpp>
#include <supermod/events/TickEvent.hpp>
#include <supermod/exceptions/Error.hpp>
#include <supermod/game/Game.hpp>
Expand Down Expand Up @@ -138,7 +138,7 @@ void ModManager::ScanMods(const bool init)
if (file.path().extension() != ".zip" && file.path().extension() != ".sprm")
continue;

EventManager::On<D3dInitEvent>([=] {
EventManager::On<DxInitEvent>([=] {
auto zip = std::make_shared<io::OwnedZip>(file.path().string(), true);
ModInstaller::AddProvider(
std::make_shared<ModSourceProviderZip>(file.path().filename().string(), std::move(zip)));
Expand Down
4 changes: 2 additions & 2 deletions src/supermod/modloader/install/ModInstaller.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "provider/ModSourceProviderRegistry.hpp"

#include <supermod/constants.hpp>
#include <supermod/events/D3dInitEvent.hpp>
#include <supermod/events/DxInitEvent.hpp>
#include <supermod/game/Game.hpp>
#include <supermod/io/TempManager.hpp>
#include <supermod/io/logs/Console.hpp>
Expand Down Expand Up @@ -99,7 +99,7 @@ void ModInstaller::InvokeURI(const std::string& uriCommand)

void ModInstaller::Init()
{
EventManager::On<D3dInitEvent>([] {
EventManager::On<DxInitEvent>([] {
if (FAILED(OleInitialize(nullptr)))
spdlog::error("Failed to initialize OLE");
if (FAILED(RegisterDragDrop(*game::Game::window, &dropTarget)))
Expand Down
4 changes: 2 additions & 2 deletions src/supermod/patches/AdaptiveResolutionPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <supermod/pch.hpp>

#include <supermod/Utils.hpp>
#include <supermod/events/D3dInitEvent.hpp>
#include <supermod/events/DxInitEvent.hpp>
#include <supermod/events/EventManager.hpp>
#include <supermod/events/ResolutionChangeEvent.hpp>
#include <supermod/events/WindowEvent.hpp>
Expand Down Expand Up @@ -83,7 +83,7 @@ inline EventManager::Ready $adaptive_resolution_patch([] {
}
});

EventManager::On<D3dInitEvent>([] {
EventManager::On<DxInitEvent>([] {
if (!game::Game::IsGameFullscreen())
SetWindowLongA(*game::Game::window, GWL_STYLE,
GetWindowLongA(*game::Game::window, GWL_STYLE) | REQUIRED_STYLES);
Expand Down

0 comments on commit 490173c

Please sign in to comment.