Skip to content

Commit a18d693

Browse files
author
StArray
committed
fix(d3d11): improve Il2Cpp compatibility and hook stability- Add LoadLibrary fallback for d3d11.dll when module handle not found- Handle swap chain resize in Il2Cpp by recreating MainRTV- Add IsIl2Cpp() helper to detect GameAssembly.dll- Remove redundant GetGameRenderer() call and manual DisplaySize set- Add build_and_run.sh for one-click build and deploy
1 parent 18d7b82 commit a18d693

7 files changed

Lines changed: 125 additions & 17 deletions

File tree

‎Corehold/log.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ModManager/Managed-Benchmark][Info]: Path resolve: 0.006s
2+
[ModManager/Runtime][Info]: Detected backend: Il2Cpp
3+
[ModManager/ModLoader][Info]: 共发现 0 个 Mod
4+
[ModManager/ImGuiDXRenderer][Info]: NativeInit=0
5+
[ModManager/ImGuiDXRenderer][Info]: Ready (multi-backend)
6+
[ModManager/Managed-Benchmark][Info]: === Startup total: 0.130s ===
7+
[ModManager/ImGuiDXRenderer][Info]: Init

‎Scripts/build_and_run.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set -e
2+
cd $(dirname $0)/..
3+
dotnet build StArray.ModManager.Windows/StArray.ModManager.Windows.csproj
4+
cp StArray.ModManager.Windows/bin/Debug/net10.0/StArray.ModManager*.dll /c/Users/StArray/Desktop/3.1.1_Il2Cpp/Corehold/managed
5+
/c/Users/StArray/Desktop/3.1.1_Il2Cpp/ADOFAI.exe

‎StArray.ModManager.Windows/Managed.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public static int Entry(int argc, IntPtr argv)
8888
Logger.Info("Runtime", $"Detected backend: {RuntimeManager.Backend}");
8989

9090
// 检测图形设备类型
91-
var renderer = GetGameRenderer();
92-
Logger.Info("Backend", $"Detected renderer: {renderer}");
91+
//var renderer = GetGameRenderer();
92+
//Logger.Info("Backend", $"Detected renderer: {renderer}");
9393

9494
ModManagerUI modManagerUI = new(new ModLoader(modsPath), managedDir);
9595

‎StArray.ModManager.Windows/Native/dx11hook.cpp‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "cimgui.h"
44
#include "kiero.hpp"
55
#include "kiero_d3d11.hpp"
6+
#include <d3dcompiler.h>
67

78
typedef HRESULT(APIENTRY* PresentFn)(IDXGISwapChain*, UINT, UINT);
89

@@ -12,6 +13,7 @@ namespace DX11Hook {
1213

1314
static PresentFn OriginalPresent = nullptr;
1415
static bool Initialised = false;
16+
static bool isIl2Cpp;
1517

1618
LRESULT APIENTRY HookWndProc(HWND h, UINT m, WPARAM w, LPARAM l) {
1719
if (ImGui_ImplWin32_WndProcHandler(h, m, w, l))
@@ -42,7 +44,10 @@ namespace DX11Hook {
4244
}
4345

4446
ID3D11RenderTargetView *MainRTV = nullptr;
47+
ID3D11Device* CachedDevice = nullptr;
4548

49+
50+
ID3D11RenderTargetView* gameRTV;
4651
HRESULT HookPresent(IDXGISwapChain* swapChain, UINT syncInterval, UINT flags) {
4752
if (!Initialised) {
4853
DXGI_SWAP_CHAIN_DESC desc;
@@ -52,38 +57,38 @@ namespace DX11Hook {
5257
ID3D11Device* device = nullptr;
5358
swapChain->GetDevice(__uuidof(ID3D11Device), (void**)&device);
5459
device->GetImmediateContext(&DeviceContext);
60+
CachedDevice = device;
5561
if (imgui_callbacks.init_callback) imgui_callbacks.init_callback();
5662

5763
if (ImGui_ImplWin32_Init(g_GameWindow) && ImGui_ImplDX11_Init(device, DeviceContext)) {
5864
SwapChain = swapChain;
5965
g_OriginalWndProc = (WNDPROC)SetWindowLongPtrW(g_GameWindow,
6066
GWLP_WNDPROC, (LONG_PTR)HookWndProc);
67+
isIl2Cpp = IsIl2Cpp();
6168
Initialised = true;
6269
ImGui_Initialised = true;
6370
DEBUG_LOG("DX11Hook: ImGui initialised, hwnd=%p", g_GameWindow);
6471
}
6572
}
6673

6774
if (Initialised) {
68-
/*if (MainRTV) { MainRTV->Release(); MainRTV = nullptr; }
69-
ID3D11Texture2D* bb = nullptr;
70-
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&bb);
71-
ID3D11Device* device = nullptr;
72-
swapChain->GetDevice(__uuidof(ID3D11Device), (void**)&device);
73-
if (bb) { device->CreateRenderTargetView(bb, nullptr, &MainRTV); bb->Release(); }
74-
*/
75-
75+
// Rebuild RTV each frame
76+
if (isIl2Cpp) {
77+
//TODO: impl resize
78+
if (MainRTV) { MainRTV->Release(); MainRTV = nullptr; }
79+
ID3D11Texture2D* bb = nullptr;
80+
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&bb);
81+
if (bb) {
82+
CachedDevice->CreateRenderTargetView(bb,nullptr, &MainRTV);
83+
bb->Release();
84+
}
85+
DeviceContext->OMSetRenderTargets(1, &MainRTV, nullptr);
86+
}
7687
ImGui_ImplDX11_NewFrame();
7788
ImGui_ImplWin32_NewFrame();
78-
auto io = igGetIO();
79-
DXGI_SWAP_CHAIN_DESC desc;
80-
swapChain->GetDesc(&desc);
81-
io->DisplaySize.x = desc.BufferDesc.Width;
82-
io->DisplaySize.y = desc.BufferDesc.Height;
89+
8390
if (imgui_callbacks.render_callback) imgui_callbacks.render_callback();
8491
ImGui_ImplDX11_RenderDrawData(igGetDrawData());
85-
86-
//if (MainRTV) DeviceContext->OMSetRenderTargets(1, &MainRTV, nullptr);
8792
}
8893

8994
return OriginalPresent(swapChain, syncInterval, flags);
@@ -92,6 +97,10 @@ namespace DX11Hook {
9297
bool InstallHook() {
9398
DEBUG_LOG("DX11Hook::InstallHook: locating D3D11 methods via kiero...");
9499

100+
if (GetModuleHandle("d3d11.dll") == nullptr) {
101+
DEBUG_LOG("DX11Hook::InstallHook: GetModuleHandle failed");
102+
LoadLibrary("d3d11.dll");
103+
}
95104
kiero::D3D11Output output;
96105
auto err = kiero::locate<kiero::Implementation_D3D11>(nullptr, &output);
97106
if (err != kiero::Error_Nil) {

‎StArray.ModManager.Windows/Native/main.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ inline char* GetDirectoryFile(char* filename) {
2929
return path;
3030
}
3131

32+
inline bool IsIl2Cpp() {
33+
return GetModuleHandle("GameAssembly.dll") != nullptr;
34+
}
35+
3236
inline void Log(const char* fmt, ...) {
3337
if (!fmt) return;
3438
char text[4096];

‎imgui.ini‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Window][Debug##Default]
2+
Pos=60,60
3+
Size=400,400
4+
Collapsed=0
5+
6+
[Window][StArray ModManager]
7+
Pos=313,137
8+
Size=738,610
9+
Collapsed=0
10+
11+
[Table][0x16406B02,4]
12+
RefScale=16
13+
Column 0 Width=50
14+
Column 1 Weight=1.0000
15+
Column 2 Width=80
16+
Column 3 Width=50
17+
18+
[Docking][Data]
19+

‎log.txt‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,67 @@
22
[DEBUG] DX11Hook::InstallHook: Present hook OK
33
[DEBUG] DX11Hook::InstallHook: creating dummy device...
44
[DEBUG] DX11Hook::InstallHook: Present hook OK
5+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
6+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
7+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
8+
[DEBUG] DX11Hook: ImGui initialised, hwnd=00000000000608D8
9+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
10+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
11+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
12+
[DEBUG] DX11Hook: ImGui initialised, hwnd=000000000008069C
13+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
14+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
15+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
16+
[DEBUG] DX11Hook: ImGui initialised, hwnd=00000000003006E0
17+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
18+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
19+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
20+
[DEBUG] DX11Hook: ImGui initialised, hwnd=000000000073067E
21+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
22+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
23+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
24+
[DEBUG] DX11Hook: ImGui initialised, hwnd=0000000000140A08
25+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
26+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
27+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
28+
[DEBUG] DX11Hook: ImGui initialised, hwnd=000000000031084C
29+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
30+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
31+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
32+
[DEBUG] DX11Hook: ImGui initialised, hwnd=0000000000120A06
33+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
34+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
35+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
36+
[DEBUG] DX11Hook: ImGui initialised, hwnd=00000000000E0826
37+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
38+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
39+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
40+
[DEBUG] DX11Hook: ImGui initialised, hwnd=00000000000F0826
41+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
42+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
43+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
44+
[DEBUG] DX11Hook: ImGui initialised, hwnd=0000000000100794
45+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
46+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
47+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
48+
[DEBUG] DX11Hook: ImGui initialised, hwnd=0000000000050776
49+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
50+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
51+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
52+
[DEBUG] DX11Hook: ImGui initialised, hwnd=000000000011066A
53+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
54+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
55+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
56+
[DEBUG] DX11Hook: ImGui initialised, hwnd=00000000001006A2
57+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
58+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
59+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
60+
[DEBUG] DX11Hook: ImGui initialised, hwnd=000000000026043C
61+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
62+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFC600618C0
63+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
64+
[DEBUG] DX11Hook: ImGui initialised, hwnd=00000000005E0356
65+
[DEBUG] DX11Hook::InstallHook: locating D3D11 methods via kiero...
66+
[DEBUG] DX11Hook::InstallHook: Present found at 00007FFA862E18C0
67+
[DEBUG] DX11Hook::InstallHook: hook installed successfully
68+
[DEBUG] DX11Hook: ImGui initialised, hwnd=00000000000E0C06

0 commit comments

Comments
 (0)