#include"MW4AppHeaders.hpp"
#include<Windows.h>

//
//Do not use ....\EulaTest\\1.0 as the key for your game; use your game name and version instead...

// PLEASE NOTE: do not place backslashes on beginning or end of regkey.
//
#define GAME_REG_KEY	 	"Software\\Microsoft\\Microsoft Games\\MechWarrior Vengeance"

typedef DWORD (*EBUPROC) (LPCTSTR lpRegKeyLocation, LPCTSTR lpEULAFileName, LPCSTR lpWarrantyFileName, BOOL fCheckForFirstRun);

//
//The game application's .RC file should define strings for the EULA and WARRANTY pathnames...

//

bool FirstRunEula(const char *eula_filename,const char *warranty_filename)
{
#if 1 // jcem
	return true;
#else // jcem
	EBUPROC	pfnEBUEula;
	
	HINSTANCE hMod;
	hMod = LoadLibrary("Assets\\binaries\\EBUEula.dll");
	
	if (NULL == hMod)  		
		hMod = LoadLibrary("EBUEula.dll");
	
	if (NULL == hMod)  		// can't attach to DLL
	{
		STOP(("Cannot Load EBUEula.dll"));
	}
	pfnEBUEula = (EBUPROC) GetProcAddress(hMod, "EBUEula");
	
	if (NULL == pfnEBUEula) 	// can't find entry point
	{
		FreeLibrary(hMod);
		STOP(("Cannot Find Entry Point to EBUEula.dll"));
	}
	
	
	//
	//This call enables both EULA and warranty accepting/viewing/printing.  If your
	//game doesn't ship with a WARRANTY file, specifiy NULL instead of szWarranty...

	//The code below, for instance, works with both OEM and retail builds...

	//
	const char *pszWarrantyParam = 0xFFFFFFFF != GetFileAttributes(warranty_filename) ? warranty_filename : NULL;
	BOOL fAllowGameToRun = pfnEBUEula(GAME_REG_KEY, eula_filename, pszWarrantyParam, TRUE);
	
	FreeLibrary(hMod);
	return fAllowGameToRun?true:false;
#endif // jcem
}

bool EulaDeclineMsg(const char *str,const char *caption)
{
	return IDOK==MessageBox(NULL,str,caption,MB_OKCANCEL);
}

bool ExecuteAndWait(const char *exename)
{
	STARTUPINFO sinfo;
	PROCESS_INFORMATION pinfo;
	ZeroMemory(&sinfo,sizeof(sinfo));
	sinfo.cb=sizeof(sinfo);
	if(CreateProcess(exename,NULL,NULL,NULL,FALSE,NULL,NULL,NULL,&sinfo,&pinfo)==NULL) return false;
	
	DWORD eflag;
	do
	{
		
		if(GetExitCodeProcess(pinfo.hProcess,&eflag)==0) return false;
	}
	while(eflag==STILL_ACTIVE);
	
	return true;
}

extern HWND hWindow;
extern void __stdcall EnterWindowMode();
extern void __stdcall EnterFullScreenMode();

bool WinMessageBox(const char *str,const char *msg,int mode)
{
	bool res=false;
	bool needflip=Environment.fullScreen;
	if(needflip) gos_EnableSetting(gos_Set_MinMaxApp,0);
	switch(mode)
	{
	case 0:
		res=(IDRETRY==MessageBox(hWindow,msg,str,MB_RETRYCANCEL));
		break;
	case 1:
		res=(IDOK==MessageBox(hWindow,msg,str,MB_OK));
		break;
		
	}
	if(needflip) gos_EnableSetting(gos_Set_MinMaxApp,1);
	return res;
}