Skip to content

Commit

Permalink
thcrap_tsa: attempt to fix devicelost crash
Browse files Browse the repository at this point in the history
  • Loading branch information
DankRank committed Dec 15, 2016
1 parent 66e6c82 commit 42e7b15
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions thcrap_tsa/src/devicelost.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Touhou Community Reliant Automatic Patcher
* Team Shanghai Alice support plugin
*
* ----
*
* Device Lost error fixes
*/

#include <thcrap.h>
#include "thcrap_tsa.h"

#define FACILITY_D3D 0x88760000
#define D3D_OK 0
#define D3DERR_DRIVERINTERNALERROR (FACILITY_D3D|2087)
#define D3DERR_DEVICELOST (FACILITY_D3D|2152)
#define D3DERR_DEVICENOTRESET (FACILITY_D3D|2153)

int BP_devicelost(x86_reg_t *regs, json_t *bp_info) {
void ***d3dd9 = ((void****)regs->esp)[1];
void *presParams = ((void**)regs->esp)[2];

// function no. 16 - Reset
int(__stdcall*d3dd9_Reset)(void***,void*) = (*d3dd9)[16];
// function no. 3 - TestCooperativeLevel
int(__stdcall*d3dd9_TestCooperativeLevel)(void***) = (*d3dd9)[3];

d3dd9_Reset(d3dd9, presParams); // not sure if needed
for(;;){
switch (d3dd9_TestCooperativeLevel(d3dd9)) {
case D3DERR_DEVICELOST:
// wait for a little
MsgWaitForMultipleObjects(0, NULL, FALSE, 10, QS_ALLINPUT);
break;
case D3DERR_DEVICENOTRESET:
d3dd9_Reset(d3dd9, presParams);
break;
default:
case D3DERR_DRIVERINTERNALERROR:
MessageBox(0, "Unable to recover from Device Lost error.", "Error", MB_ICONERROR);
// panic and return (will probably result in crash)
case D3D_OK:
return 0;
}
}
}
2 changes: 2 additions & 0 deletions thcrap_tsa/thcrap_tsa.def
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ EXPORTS
BP_th06_file_load
BP_th06_file_loaded

BP_devicelost

; Bounds
; ------
fn_for_bounds
Expand Down
1 change: 1 addition & 0 deletions thcrap_tsa/thcrap_tsa.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<ItemGroup>
<ClCompile Include="src\anm.c" />
<ClCompile Include="src\anm_bounds.c" />
<ClCompile Include="src\devicelost.c" />
<ClCompile Include="src\encraption.c" />
<ClCompile Include="src\gentext.c" />
<ClCompile Include="src\layout.c" />
Expand Down

0 comments on commit 42e7b15

Please sign in to comment.