<commit_before>/**
  * 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];

	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)
			// [[fallthrough]]
		case D3D_OK:
			regs->esp += 8; // simulate RETN 8
			return 0;
		}
	}
}<commit_msg>devicelost: use bp_info instead of stack arguments<commit_after>/**
  * 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) {
	// Parameters
	// ----------
	void ***d3dd9 = *(void ****)json_object_get_register(bp_info, regs, "d3dd9");
	void *presParams = (void *)json_object_get_immediate(bp_info, regs, "pres_params");
	// ----------

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

	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)
			// [[fallthrough]]
		case D3D_OK:
			return 0;
		}
	}
}
{'fraction_non_alphanumeric': 0.1386942087790483, 'fraction_numerical': 0.05016599040944301, 'mean_word_length': 4.370297029702971, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3716074', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1086, 'n_words': 224}