From 36ab037ad2284342ee73ea9b840dfb80b20840dc Mon Sep 17 00:00:00 2001 From: "code@koerner-de.net" Date: Sun, 16 Oct 2011 18:09:51 +0000 Subject: [PATCH] Add comments in the source code of the dll. Changed c-runtime linkage of the GUI to static. The use of Mutex is enabled, for a secure execution of the server thread in the dll. BugFix: - GUI: If texture are removed (which were previously loaded) they are now marked as not loaded. - DLL: Added AddRef/Release counter to ensure a clean up before the device is destroyed. - DLL: The reference to the FileToMod array is now update to the new value (after merging an updated). - DLL: SingleTexture is now unswitched, if it was switched with a texture, which shall be replaced with a texture due to a new update. --- OTM_DX9/OTM_ArrayHandler.cpp | 27 +- OTM_DX9/OTM_ArrayHandler.h | 2 - OTM_DX9/OTM_DX9_dll.cpp | 102 ++--- OTM_DX9/OTM_Defines.h | 6 + OTM_DX9/OTM_IDirect3D9.cpp | 66 +-- OTM_DX9/OTM_IDirect3D9.h | 29 +- OTM_DX9/OTM_IDirect3DDevice9.cpp | 382 +++++++++--------- OTM_DX9/OTM_IDirect3DDevice9.h | 13 +- OTM_DX9/OTM_IDirect3DTexture9.cpp | 31 +- OTM_DX9/OTM_IDirect3DTexture9.h | 12 +- OTM_DX9/OTM_Main.h | 8 +- OTM_DX9/OTM_TextureClient.cpp | 153 ++++--- OTM_DX9/OTM_TextureClient.h | 42 +- OTM_DX9/OTM_TextureServer.cpp | 87 ++-- OTM_DX9/OTM_TextureServer.h | 59 +-- OTM_GUI/OTM_Language.cpp | 4 +- OTM_GUI/OTM_Sender.cpp | 12 +- .../Example_OTM_LanguagePack_English.txt | 1 + .../languages/OTM_LanguagePackU_Deutsch.txt | Bin 4954 -> 5048 bytes .../languages/OTM_LanguagePackU_Russian.txt | Bin 4498 -> 5368 bytes OTM_GUI/config.gcc | 2 +- OTM_GUI/config.vc | 2 +- 22 files changed, 511 insertions(+), 529 deletions(-) diff --git a/OTM_DX9/OTM_ArrayHandler.cpp b/OTM_DX9/OTM_ArrayHandler.cpp index a09192e..0a46a85 100644 --- a/OTM_DX9/OTM_ArrayHandler.cpp +++ b/OTM_DX9/OTM_ArrayHandler.cpp @@ -50,7 +50,7 @@ int OTM_FileHandler::Add(TextureFileStruct* file) if (file->Reference>=0) return (RETURN_UPDATE_ALLREADY_ADDED); - if (Number/FieldLength==FieldCounter) + if (Number/FieldLength==FieldCounter) // get more memory { TextureFileStruct*** temp = NULL; try {temp = new TextureFileStruct**[FieldCounter+10];} @@ -60,9 +60,9 @@ int OTM_FileHandler::Add(TextureFileStruct* file) return (RETURN_NO_MEMORY); } - for (int i=0; iReference = Number++; + file->Reference = Number++; //set the reference for a fast deleting return (RETURN_OK); } @@ -93,12 +93,13 @@ int OTM_FileHandler::Remove(TextureFileStruct* file) Message("OTM_FileHandler::Remove(%lu): %lu\n", file, this); if (gl_ErrorState & OTM_ERROR_FATAL) return (RETURN_FATAL_ERROR); int ref = file->Reference; + if (ref<0) return (RETURN_OK); // returning if no Reference is set - file->Reference = -1; - if (ref<(--Number)) + file->Reference = -1; //set reference outside of bound + if (ref<(--Number)) //if reference is unequal to Number-1 we copy the last entry to the index "ref" { Files[ref/FieldLength][ref%FieldLength] = Files[Number/FieldLength][Number%FieldLength]; - Files[ref/FieldLength][ref%FieldLength]->Reference = ref; + Files[ref/FieldLength][ref%FieldLength]->Reference = ref; //set the new reference entry } return (RETURN_OK); } @@ -214,13 +215,3 @@ int OTM_TextureHandler::Remove(OTM_IDirect3DTexture9* pTexture) //will be called } return (RETURN_OK); } - -/* -int OTM_TextureHandler::RemoveAll(void) //only for deleting all fake textures -{ - Message("OTM_TextureHandler::RemoveAll() %d: %lu\n", Number, this); - for (int i=0; iRelease(); - Number=0; - return (RETURN_OK); -} -*/ diff --git a/OTM_DX9/OTM_ArrayHandler.h b/OTM_DX9/OTM_ArrayHandler.h index 160d156..c88b77a 100644 --- a/OTM_DX9/OTM_ArrayHandler.h +++ b/OTM_DX9/OTM_ArrayHandler.h @@ -26,7 +26,6 @@ along with OpenTexMod. If not, see . typedef struct { - bool Checked; // to check if texture was already update; used by TextureClient::MergeUpdate() bool ForceReload; // to force a reload of the texture (if it is already modded) char* pData; // store texture file as file in memory unsigned int Size; // size of file @@ -65,7 +64,6 @@ public: int Add( OTM_IDirect3DTexture9* texture); int Remove( OTM_IDirect3DTexture9* texture); - //int RemoveAll(void); //only for deleting all fake textures int GetNumber(void) {return (Number);} OTM_IDirect3DTexture9 *operator [] (int i) {if (i<0||i>=Number) return (NULL); else return (Textures[i/FieldLength][i%FieldLength]);} diff --git a/OTM_DX9/OTM_DX9_dll.cpp b/OTM_DX9/OTM_DX9_dll.cpp index b295617..b67153c 100644 --- a/OTM_DX9/OTM_DX9_dll.cpp +++ b/OTM_DX9/OTM_DX9_dll.cpp @@ -26,28 +26,26 @@ along with OpenTexMod. If not, see . #include "OTM_Main.h" -//#include -//#pragma comment(lib, "Shlwapi.lib") - -OTM_IDirect3DDevice9* gl_pIDirect3DDevice9 = NULL; -OTM_IDirect3D9* gl_pIDirect3D9 = NULL; +// global variable which is not linked extern HINSTANCE gl_hOriginalDll = NULL; HINSTANCE gl_hThisInstance = NULL; -unsigned int gl_ErrorState = 0u; OTM_TextureServer* gl_TextureServer = NULL; -FILE* gl_File =NULL; HANDLE gl_ServerThread = NULL; + +// global variable which is linked extern +unsigned int gl_ErrorState = 0u; + +#ifdef LOG_MESSAGE +FILE* gl_File = NULL; +#endif + BOOL WINAPI DllMain( HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { UNREFERENCED_PARAMETER(lpReserved); - /* - Beep(300, 50); - Beep(600, 50); - Beep(900, 50); - Beep(1200, 50); -*/ + + //dll entry routine, here we initialize or clean up switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: @@ -60,8 +58,7 @@ BOOL WINAPI DllMain( HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpRese ExitInstance(); break; } - case DLL_THREAD_ATTACH: break; - case DLL_THREAD_DETACH: break; + default: break; } return (true); @@ -70,17 +67,19 @@ BOOL WINAPI DllMain( HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpRese #ifdef NO_INJECTION + + IDirect3D9* WINAPI Direct3DCreate9(UINT SDKVersion) { Message("WINAPI Direct3DCreate9\n"); if (!gl_hOriginalDll) LoadOriginalDll(); // looking for the "right d3d9.dll" - // Hooking IDirect3D Object from Original Library + // find original function in original d3d9.dll typedef IDirect3D9 *(WINAPI* D3D9_Type)(UINT SDKVersion); D3D9_Type D3DCreate9_fn = (D3D9_Type) GetProcAddress( gl_hOriginalDll, "Direct3DCreate9"); - // Debug + if (!D3DCreate9_fn) { Message("Direct3DCreate9: original function not found in dll\n"); @@ -88,20 +87,20 @@ IDirect3D9* WINAPI Direct3DCreate9(UINT SDKVersion) } - if (gl_pIDirect3D9!=NULL) gl_ErrorState |= OTM_ERROR_MULTIPLE_IDirect3D9; - - // Request pointer from Original Dll. + //Create originale IDirect3D9 object IDirect3D9 *pIDirect3D9_orig = D3DCreate9_fn(SDKVersion); - gl_pIDirect3D9 = new OTM_IDirect3D9( pIDirect3D9_orig, gl_TextureServer); - // Return pointer to hooking Object instead of "real one" - return (gl_pIDirect3D9); + //create our OTM_IDirect3D9 object + OTM_IDirect3D9 *pIDirect3D9 = new OTM_IDirect3D9( pIDirect3D9_orig, gl_TextureServer); + + // Return pointer to our object instead of "real one" + return (pIDirect3D9); } -bool HookThisProgram( wchar_t *ret) +bool HookThisProgram( wchar_t *ret) //this function always return true, it needed for the name and path of te executable { wchar_t Executable[MAX_PATH]; - GetModuleFileNameW( GetModuleHandle( NULL ), Executable, MAX_PATH ); + GetModuleFileNameW( GetModuleHandle( NULL ), Executable, MAX_PATH ); //ask for name and path of this executable int len = 0; while (Executable[len]) {ret[len] = Executable[len]; len++;} @@ -117,10 +116,10 @@ bool HookThisProgram( wchar_t *ret) typedef IDirect3D9 *(APIENTRY *Direct3DCreate9_type)(UINT); -Direct3DCreate9_type Direct3DCreate9_fn; +Direct3DCreate9_type Direct3DCreate9_fn; // we need to store the pointer to the original Direct3DCreate9 function after we have done a detour -bool RetourFunc(BYTE *src, BYTE *restore, const int len); void *DetourFunc(BYTE *src, const BYTE *dst, const int len); +bool RetourFunc(BYTE *src, BYTE *restore, const int len); IDirect3D9 *APIENTRY MyDirect3DCreate9(UINT SDKVersion) { @@ -140,44 +139,45 @@ IDirect3D9 *APIENTRY MyDirect3DCreate9(UINT SDKVersion) IDirect3D9 *pIDirect3D9_orig = NULL; if (Direct3DCreate9_fn) { - pIDirect3D9_orig = Direct3DCreate9_fn(SDKVersion); + pIDirect3D9_orig = Direct3DCreate9_fn(SDKVersion); //creating the original IDirect3D9 object } else return (NULL); + OTM_IDirect3D9 *pIDirect3D9; if (pIDirect3D9_orig) { - gl_pIDirect3D9 = new OTM_IDirect3D9( pIDirect3D9_orig, gl_TextureServer); + pIDirect3D9 = new OTM_IDirect3D9( pIDirect3D9_orig, gl_TextureServer); //creating our OTM_IDirect3D9 object } - return (gl_pIDirect3D9); + return (pIDirect3D9); //return our object instead of the "real one" } bool HookThisProgram( wchar_t *ret) { FILE* file; - wchar_t *app_path = _wgetenv( L"APPDATA"); + wchar_t *app_path = _wgetenv( L"APPDATA"); //asc for the user application directory wchar_t file_name[MAX_PATH]; swprintf_s( file_name, MAX_PATH, L"%ls\\%ls\\%ls", app_path, OTM_APP_DIR, OTM_APP_DX9); - if (_wfopen_s( &file, file_name, L"rt,ccs=UTF-16LE")) return (false); + if (_wfopen_s( &file, file_name, L"rt,ccs=UTF-16LE")) return (false); // open the file in utf-16 LE mode wchar_t Executable[MAX_PATH]; wchar_t Game[MAX_PATH]; - GetModuleFileNameW( GetModuleHandle( NULL ), Executable, MAX_PATH ); + GetModuleFileNameW( GetModuleHandle( NULL ), Executable, MAX_PATH ); //ask for name and path of this executable //MessageBoxW( NULL, Executable, L"test", 0); while (!feof(file)) { - if ( fgetws( Game, MAX_PATH, file) != NULL ) + if ( fgetws( Game, MAX_PATH, file) != NULL ) //get each line of the file { //MessageBoxW( NULL, Game, L"test", 0); int len = 0; while (Game[len]) { - if (Game[len]==L'\r' || Game[len]==L'\n') {Game[len]=0; break;} + if (Game[len]==L'\r' || Game[len]==L'\n') {Game[len]=0; break;} //removing the new line symbols len++; } - if ( _wcsicmp( Executable, Game ) == 0 ) + if ( _wcsicmp( Executable, Game ) == 0 ) //compare both strings { for (int i=0; iMainLoop(); + if (gl_TextureServer!=NULL) gl_TextureServer->MainLoop(); //This is and endless mainloop, it sleep till something is written into the pipe return (0); } void InitInstance(HINSTANCE hModule) { - DisableThreadLibraryCalls( hModule ); + DisableThreadLibraryCalls( hModule ); //reduce overhead gl_hThisInstance = (HINSTANCE) hModule; wchar_t game[MAX_PATH]; - if (HookThisProgram( game)) + if (HookThisProgram( game)) //ask if we need to hook this program { OpenMessage(); Message("InitInstance: %lu\n", hModule); - gl_TextureServer = new OTM_TextureServer(game); + gl_TextureServer = new OTM_TextureServer(game); //create the server which listen on the pipe and prepare the update for the texture clients if (gl_TextureServer!=NULL) { - if (gl_TextureServer->OpenPipe(game)) + if (gl_TextureServer->OpenPipe(game)) //open the pipe and send the name+path of this executable { Message("InitInstance: Pipe not opened\n"); return; } - gl_ServerThread = CreateThread( NULL, 0, ServerThread, NULL, 0, NULL); - if (gl_ServerThread==NULL) Message("InitInstance: Serverthread not started\n"); + gl_ServerThread = CreateThread( NULL, 0, ServerThread, NULL, 0, NULL); //creating a thread for the mainloop + if (gl_ServerThread==NULL) {Message("InitInstance: Serverthread not started\n");} + - //this is for testing purpose, these functions should be called from the server thread, provoked by the OTM_GUI - //gl_TextureServer->SaveAllTextures(true); - //gl_TextureServer->SetSaveDirectory("tex\\"); /* + // + //this is for testing purpose, these functions should be called from the server thread, provoked by the OTM_GUI + // + + gl_TextureServer->SaveAllTextures(true); + gl_TextureServer->SetSaveDirectory("tex\\"); + gl_TextureServer->AddFile("BF_0xbc2a9196.dds", 0xbc2a9196ul); gl_TextureServer->AddFile("0X1FD33669.dds", 0X1FD33669ul); gl_TextureServer->AddFile("0X26D19B9A.dds", 0X26D19B9Aul); @@ -256,7 +261,6 @@ void LoadOriginalDll(void) // try to load the system's d3d9.dll, if pointer empty if (!gl_hOriginalDll) gl_hOriginalDll = LoadLibrary(buffer); - // Debug if (!gl_hOriginalDll) { ExitProcess(0); // exit the hard way @@ -324,7 +328,7 @@ bool RetourFunc(BYTE *src, BYTE *restore, const int len) HHOOK gl_hHook = NULL; LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) { - return (CallNextHookEx( gl_hHook, nCode, wParam, lParam)); + return (CallNextHookEx( gl_hHook, nCode, wParam, lParam)); //we do not change something, we need this hook only to get our dll loaded into a starting program } void InstallHook() diff --git a/OTM_DX9/OTM_Defines.h b/OTM_DX9/OTM_Defines.h index c1cd1cd..e81e4d7 100644 --- a/OTM_DX9/OTM_Defines.h +++ b/OTM_DX9/OTM_Defines.h @@ -23,9 +23,15 @@ along with OpenTexMod. If not, see . #ifdef ALPHA + +#define LOG_MESSAGE +extern FILE *gl_File; + #define Message(...) {if (gl_File!=NULL) {fprintf( gl_File, __VA_ARGS__); fflush(gl_File);}} #define OpenMessage(...) {if (fopen_s( &gl_File, "OTM_log.txt", "wt")) gl_File=NULL;} #define CloseMessage(...) {if (gl_File!=NULL) fclose(gl_File);} + + #else #define OpenMessage(...) #define Message(...) diff --git a/OTM_DX9/OTM_IDirect3D9.cpp b/OTM_DX9/OTM_IDirect3D9.cpp index 719654c..931e914 100644 --- a/OTM_DX9/OTM_IDirect3D9.cpp +++ b/OTM_DX9/OTM_IDirect3D9.cpp @@ -25,7 +25,6 @@ along with OpenTexMod. If not, see . OTM_IDirect3D9::OTM_IDirect3D9(IDirect3D9 *pOriginal, OTM_TextureServer* server) { Message("OTM_IDirect3D9( %lu, %lu): %lu\n", pOriginal, server, this); - //MessageBox( NULL, TEXT( "Init OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); m_pIDirect3D9 = pOriginal; OTM_Server = server; } @@ -36,8 +35,7 @@ OTM_IDirect3D9::~OTM_IDirect3D9(void) HRESULT __stdcall OTM_IDirect3D9::QueryInterface(REFIID riid, void** ppvObj) { - //MessageBox( NULL, TEXT( "QueryInterface OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - *ppvObj = NULL; + *ppvObj = NULL; // call this to increase AddRef at original object // and to check if such an interface is there @@ -54,24 +52,17 @@ HRESULT __stdcall OTM_IDirect3D9::QueryInterface(REFIID riid, void** ppvObj) ULONG __stdcall OTM_IDirect3D9::AddRef(void) { - //MessageBox( NULL, TEXT( "AddRef OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->AddRef()); + return(m_pIDirect3D9->AddRef()); } ULONG __stdcall OTM_IDirect3D9::Release(void) { - //MessageBox( NULL, TEXT( "Release OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - // call original routine ULONG count = m_pIDirect3D9->Release(); - // in case no further Ref is there, the Original Object has deleted itself - // so do we here + // in case no further Ref is there, the Original Object has deleted itself if (count == 0) { - //MessageBox( NULL, TEXT( "Release everything" ), TEXT( "Timer" ), MB_OK ); - //extern OTM_IDirect3D9* gl_pOTM_IDirect3D9; - gl_pIDirect3D9 = NULL; delete(this); } @@ -80,97 +71,78 @@ ULONG __stdcall OTM_IDirect3D9::Release(void) HRESULT __stdcall OTM_IDirect3D9::RegisterSoftwareDevice(void* pInitializeFunction) { - //MessageBox( NULL, TEXT( "RegisterSoftwareDevice OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->RegisterSoftwareDevice(pInitializeFunction)); + return(m_pIDirect3D9->RegisterSoftwareDevice(pInitializeFunction)); } UINT __stdcall OTM_IDirect3D9::GetAdapterCount(void) { - //MessageBox( NULL, TEXT( "GetAdapterCount OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->GetAdapterCount()); + return(m_pIDirect3D9->GetAdapterCount()); } HRESULT __stdcall OTM_IDirect3D9::GetAdapterIdentifier(UINT Adapter,DWORD Flags,D3DADAPTER_IDENTIFIER9* pIdentifier) { - //MessageBox( NULL, TEXT( "GetAdapterIdentifier OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->GetAdapterIdentifier(Adapter,Flags,pIdentifier)); + return(m_pIDirect3D9->GetAdapterIdentifier(Adapter,Flags,pIdentifier)); } UINT __stdcall OTM_IDirect3D9::GetAdapterModeCount(UINT Adapter, D3DFORMAT Format) { - //MessageBox( NULL, TEXT( "GetAdapterModeCount OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->GetAdapterModeCount(Adapter, Format)); + return(m_pIDirect3D9->GetAdapterModeCount(Adapter, Format)); } HRESULT __stdcall OTM_IDirect3D9::EnumAdapterModes(UINT Adapter,D3DFORMAT Format,UINT Mode,D3DDISPLAYMODE* pMode) { - //MessageBox( NULL, TEXT( "QueryInterface OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->EnumAdapterModes(Adapter,Format,Mode,pMode)); + return(m_pIDirect3D9->EnumAdapterModes(Adapter,Format,Mode,pMode)); } HRESULT __stdcall OTM_IDirect3D9::GetAdapterDisplayMode( UINT Adapter,D3DDISPLAYMODE* pMode) { - //MessageBox( NULL, TEXT( "EnumAdapterModes OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->GetAdapterDisplayMode(Adapter,pMode)); + return(m_pIDirect3D9->GetAdapterDisplayMode(Adapter,pMode)); } HRESULT __stdcall OTM_IDirect3D9::CheckDeviceType(UINT iAdapter,D3DDEVTYPE DevType,D3DFORMAT DisplayFormat,D3DFORMAT BackBufferFormat,BOOL bWindowed) { - //MessageBox( NULL, TEXT( "CheckDeviceType OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->CheckDeviceType(iAdapter,DevType,DisplayFormat,BackBufferFormat,bWindowed)); + return(m_pIDirect3D9->CheckDeviceType(iAdapter,DevType,DisplayFormat,BackBufferFormat,bWindowed)); } HRESULT __stdcall OTM_IDirect3D9::CheckDeviceFormat(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat) { - //MessageBox( NULL, TEXT( "CheckDeviceFormat OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->CheckDeviceFormat(Adapter,DeviceType,AdapterFormat,Usage,RType,CheckFormat)); + return(m_pIDirect3D9->CheckDeviceFormat(Adapter,DeviceType,AdapterFormat,Usage,RType,CheckFormat)); } HRESULT __stdcall OTM_IDirect3D9::CheckDeviceMultiSampleType(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType,DWORD* pQualityLevels) { - //MessageBox( NULL, TEXT( "CheckDeviceMultiSampleType OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->CheckDeviceMultiSampleType(Adapter,DeviceType,SurfaceFormat,Windowed,MultiSampleType,pQualityLevels)); + return(m_pIDirect3D9->CheckDeviceMultiSampleType(Adapter,DeviceType,SurfaceFormat,Windowed,MultiSampleType,pQualityLevels)); } HRESULT __stdcall OTM_IDirect3D9::CheckDepthStencilMatch(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat) { - //MessageBox( NULL, TEXT( "CheckDepthStencilMatch OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->CheckDepthStencilMatch(Adapter,DeviceType,AdapterFormat,RenderTargetFormat,DepthStencilFormat)); + return(m_pIDirect3D9->CheckDepthStencilMatch(Adapter,DeviceType,AdapterFormat,RenderTargetFormat,DepthStencilFormat)); } HRESULT __stdcall OTM_IDirect3D9::CheckDeviceFormatConversion(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SourceFormat,D3DFORMAT TargetFormat) { - //MessageBox( NULL, TEXT( "CheckDeviceFormatConversion OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->CheckDeviceFormatConversion(Adapter,DeviceType,SourceFormat,TargetFormat)); + return(m_pIDirect3D9->CheckDeviceFormatConversion(Adapter,DeviceType,SourceFormat,TargetFormat)); } HRESULT __stdcall OTM_IDirect3D9::GetDeviceCaps(UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS9* pCaps) { - //MessageBox( NULL, TEXT( "GetDeviceCaps OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->GetDeviceCaps(Adapter,DeviceType,pCaps)); + return(m_pIDirect3D9->GetDeviceCaps(Adapter,DeviceType,pCaps)); } HMONITOR __stdcall OTM_IDirect3D9::GetAdapterMonitor(UINT Adapter) { - //MessageBox( NULL, TEXT( "GetAdapterMonitor OTM_IDirect3D9" ), TEXT( "Timer" ), MB_OK ); - return(m_pIDirect3D9->GetAdapterMonitor(Adapter)); + return(m_pIDirect3D9->GetAdapterMonitor(Adapter)); } HRESULT __stdcall OTM_IDirect3D9::CreateDevice(UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface) { - // we intercept this call and provide our own "fake" Device Object HRESULT hres = m_pIDirect3D9->CreateDevice( Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface); - - // Create our own Device object and store it in global pointer - // note: the object will delete itself once Ref count is zero (similar to COM objects) - if (gl_pIDirect3DDevice9!=NULL) gl_ErrorState |= OTM_ERROR_MULTIPLE_IDirect3DDevice9; - gl_pIDirect3DDevice9 = new OTM_IDirect3DDevice9(*ppReturnedDeviceInterface, OTM_Server); + OTM_IDirect3DDevice9 *pIDirect3DDevice9 = new OTM_IDirect3DDevice9(*ppReturnedDeviceInterface, OTM_Server); - - // store our pointer (the fake one) for returning it to the calling progam - *ppReturnedDeviceInterface = gl_pIDirect3DDevice9; + // store our pointer (the fake one) for returning it to the calling program + *ppReturnedDeviceInterface = pIDirect3DDevice9; return(hres); } diff --git a/OTM_DX9/OTM_IDirect3D9.h b/OTM_DX9/OTM_IDirect3D9.h index 390020c..0b5036a 100644 --- a/OTM_DX9/OTM_IDirect3D9.h +++ b/OTM_DX9/OTM_IDirect3D9.h @@ -39,27 +39,26 @@ public: OTM_IDirect3D9(IDirect3D9 *pOriginal, OTM_TextureServer *server); virtual ~OTM_IDirect3D9(void); - // The original DX9 function definitions - HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObj); - ULONG __stdcall AddRef(void); + // The original DX9 function definitions + HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObj); + ULONG __stdcall AddRef(void); ULONG __stdcall Release(void); - HRESULT __stdcall RegisterSoftwareDevice(void* pInitializeFunction); + HRESULT __stdcall RegisterSoftwareDevice(void* pInitializeFunction); UINT __stdcall GetAdapterCount(void); HRESULT __stdcall GetAdapterIdentifier(UINT Adapter,DWORD Flags,D3DADAPTER_IDENTIFIER9* pIdentifier) ; - UINT __stdcall GetAdapterModeCount(UINT Adapter, D3DFORMAT Format); - HRESULT __stdcall EnumAdapterModes(UINT Adapter,D3DFORMAT Format,UINT Mode,D3DDISPLAYMODE* pMode) ; - HRESULT __stdcall GetAdapterDisplayMode( UINT Adapter,D3DDISPLAYMODE* pMode) ; - HRESULT __stdcall CheckDeviceType(UINT iAdapter,D3DDEVTYPE DevType,D3DFORMAT DisplayFormat,D3DFORMAT BackBufferFormat,BOOL bWindowed) ; - HRESULT __stdcall CheckDeviceFormat(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat) ; - HRESULT __stdcall CheckDeviceMultiSampleType(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType,DWORD* pQualityLevels) ; - HRESULT __stdcall CheckDepthStencilMatch(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat) ; - HRESULT __stdcall CheckDeviceFormatConversion(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SourceFormat,D3DFORMAT TargetFormat); - HRESULT __stdcall GetDeviceCaps(UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS9* pCaps) ; + UINT __stdcall GetAdapterModeCount(UINT Adapter, D3DFORMAT Format); + HRESULT __stdcall EnumAdapterModes(UINT Adapter,D3DFORMAT Format,UINT Mode,D3DDISPLAYMODE* pMode) ; + HRESULT __stdcall GetAdapterDisplayMode( UINT Adapter,D3DDISPLAYMODE* pMode) ; + HRESULT __stdcall CheckDeviceType(UINT iAdapter,D3DDEVTYPE DevType,D3DFORMAT DisplayFormat,D3DFORMAT BackBufferFormat,BOOL bWindowed) ; + HRESULT __stdcall CheckDeviceFormat(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat) ; + HRESULT __stdcall CheckDeviceMultiSampleType(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType,DWORD* pQualityLevels) ; + HRESULT __stdcall CheckDepthStencilMatch(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat) ; + HRESULT __stdcall CheckDeviceFormatConversion(UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SourceFormat,D3DFORMAT TargetFormat); + HRESULT __stdcall GetDeviceCaps(UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS9* pCaps) ; HMONITOR __stdcall GetAdapterMonitor(UINT Adapter) ; HRESULT __stdcall CreateDevice(UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface) ; - // The original DX9 function definitions -//private: +private: IDirect3D9 *m_pIDirect3D9; OTM_TextureServer* OTM_Server; }; diff --git a/OTM_DX9/OTM_IDirect3DDevice9.cpp b/OTM_DX9/OTM_IDirect3DDevice9.cpp index 517e7f0..7082603 100644 --- a/OTM_DX9/OTM_IDirect3DDevice9.cpp +++ b/OTM_DX9/OTM_IDirect3DDevice9.cpp @@ -24,14 +24,13 @@ along with OpenTexMod. If not, see . int OTM_IDirect3DDevice9::CreateSingleTexture(void) { - if (SingleTexture==NULL) //Create green texture + if (SingleTexture==NULL) //create texture { if( D3D_OK != CreateTexture(8, 8, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, (IDirect3DTexture9**) &SingleTexture, NULL)) return (RETURN_TEXTURE_NOT_LOADED); - LastCreatedTexture = NULL; - SingleTexture->FAKE = true; + LastCreatedTexture = NULL; // set LastCreatedTexture to NULL, cause LastCreatedTexture is equal SingleTexture + SingleTexture->FAKE = true; //this is no texture created from by game SingleTexture->Reference = -2; - OTM_Client->TextureColour; TextureColour = OTM_Client->TextureColour; D3DLOCKED_RECT d3dlr; IDirect3DTexture9 *pD3Dtex = SingleTexture->m_D3Dtex; @@ -44,7 +43,7 @@ int OTM_IDirect3DDevice9::CreateSingleTexture(void) } DWORD *pDst = (DWORD*)d3dlr.pBits; - for (int xy=0; xy < 8*8; xy++) *(pDst++) = TextureColour; + for (int i=0; i<8*8; i++) pDst[i] = TextureColour; pD3Dtex->UnlockRect(0); } return (RETURN_OK); @@ -53,8 +52,10 @@ int OTM_IDirect3DDevice9::CreateSingleTexture(void) OTM_IDirect3DDevice9::OTM_IDirect3DDevice9(IDirect3DDevice9* pOriginal, OTM_TextureServer* server) { Message("OTM_IDirect3DDevice9( %lu, %lu): %lu\n", pOriginal, server, this); + OTM_Server = server; - OTM_Client = new OTM_TextureClient( OTM_Server, this); + OTM_Client = new OTM_TextureClient( OTM_Server, this); //get a new texture client for this device + LastCreatedTexture = NULL; m_pIDirect3DDevice9 = pOriginal; // store the pointer to original object TextureColour = D3DCOLOR_ARGB(255,0,255,0); @@ -62,15 +63,14 @@ OTM_IDirect3DDevice9::OTM_IDirect3DDevice9(IDirect3DDevice9* pOriginal, OTM_Text CounterSaveSingleTexture = -1; SingleTexture = NULL; OSD_Font = NULL; - FontColour = D3DCOLOR_ARGB(255,255,0,0); - //Message("end OTM_IDirect3DDevice9( %lu, %lu): %lu\n", pOriginal, server, this); + OTM_Reference = 1; } OTM_IDirect3DDevice9::~OTM_IDirect3DDevice9(void) { } -HRESULT OTM_IDirect3DDevice9::QueryInterface (REFIID riid, void** ppvObj) +HRESULT OTM_IDirect3DDevice9::QueryInterface(REFIID riid, void** ppvObj) { // check if original dll can provide interface. then send *our* address *ppvObj = NULL; @@ -87,107 +87,101 @@ HRESULT OTM_IDirect3DDevice9::QueryInterface (REFIID riid, void** ppvObj) ULONG OTM_IDirect3DDevice9::AddRef(void) { - return (m_pIDirect3DDevice9->AddRef()); + OTM_Reference++; //increasing our counter + return (m_pIDirect3DDevice9->AddRef()); } ULONG OTM_IDirect3DDevice9::Release(void) { - // ATTENTION: This is a booby-trap ! Watch out ! - // If we create our own sprites, surfaces, etc. (thus increasing the ref counter - // by external action), we need to delete that objects before calling the original - // Release function - + if (--OTM_Reference==0) //if our counter drops to zero, the real device will be deleted, so we clean up before + { + // we must not release the fake textures, cause they are released if the target textures are released + // and the target textures are released by the game. + + if (SingleTexture!=NULL) SingleTexture->Release(); //this is the only texture we must release by ourself + if (OSD_Font!=NULL) OSD_Font->Release(); + + if (OTM_Client!=NULL) delete OTM_Client; //must be deleted at the end, because other releases might call a function of this object + + OTM_Client = NULL; + SingleTexture = NULL; + OSD_Font = NULL; + } - // release/delete own objects - // ..... - - // Calling original function now ULONG count = m_pIDirect3DDevice9->Release(); Message("%lu = IDirect3DDevice9::Release(): %lu\n", count, this); - - if (count==0u) - { - // now, the Original Object has deleted itself, so do we here - if (OTM_Client!=NULL) - { - //OTM_Client->ReleaseAllFakeTexture(); - if (SingleTexture!=NULL) SingleTexture->Release(); - if (OSD_Font!=NULL) OSD_Font->Release(); - delete OTM_Client; - OTM_Client = NULL; - } - - //if (gl_pTextureHandler!=NULL) gl_pTextureHandler->ReleaseAllFakeTexture(); // release all fake textures - //gl_pIDirect3DDevice9 = NULL; - delete(this); // destructor will be called automatically - } + if (OTM_Reference!=count) //bug + { + Message("Error in IDirect3DDevice9::Release(): %lu!=%lu\n", OTM_Reference, count); + } + if (count==0u) delete(this); return (count); } HRESULT OTM_IDirect3DDevice9::TestCooperativeLevel(void) { - return(m_pIDirect3DDevice9->TestCooperativeLevel()); + return(m_pIDirect3DDevice9->TestCooperativeLevel()); } UINT OTM_IDirect3DDevice9::GetAvailableTextureMem(void) { - return(m_pIDirect3DDevice9->GetAvailableTextureMem()); + return(m_pIDirect3DDevice9->GetAvailableTextureMem()); } HRESULT OTM_IDirect3DDevice9::EvictManagedResources(void) { - return(m_pIDirect3DDevice9->EvictManagedResources()); + return(m_pIDirect3DDevice9->EvictManagedResources()); } HRESULT OTM_IDirect3DDevice9::GetDirect3D(IDirect3D9** ppD3D9) { - return(m_pIDirect3DDevice9->GetDirect3D(ppD3D9)); + return(m_pIDirect3DDevice9->GetDirect3D(ppD3D9)); } HRESULT OTM_IDirect3DDevice9::GetDeviceCaps(D3DCAPS9* pCaps) { - return(m_pIDirect3DDevice9->GetDeviceCaps(pCaps)); + return(m_pIDirect3DDevice9->GetDeviceCaps(pCaps)); } HRESULT OTM_IDirect3DDevice9::GetDisplayMode(UINT iSwapChain,D3DDISPLAYMODE* pMode) { - return(m_pIDirect3DDevice9->GetDisplayMode(iSwapChain, pMode)); + return(m_pIDirect3DDevice9->GetDisplayMode(iSwapChain, pMode)); } HRESULT OTM_IDirect3DDevice9::GetCreationParameters(D3DDEVICE_CREATION_PARAMETERS *pParameters) { - return(m_pIDirect3DDevice9->GetCreationParameters(pParameters)); + return(m_pIDirect3DDevice9->GetCreationParameters(pParameters)); } HRESULT OTM_IDirect3DDevice9::SetCursorProperties(UINT XHotSpot,UINT YHotSpot,IDirect3DSurface9* pCursorBitmap) { - return(m_pIDirect3DDevice9->SetCursorProperties(XHotSpot,YHotSpot,pCursorBitmap)); + return(m_pIDirect3DDevice9->SetCursorProperties(XHotSpot,YHotSpot,pCursorBitmap)); } -void OTM_IDirect3DDevice9::SetCursorPosition(int X,int Y,DWORD Flags) +void OTM_IDirect3DDevice9::SetCursorPosition(int X,int Y,DWORD Flags) { m_pIDirect3DDevice9->SetCursorPosition(X,Y,Flags); } -BOOL OTM_IDirect3DDevice9::ShowCursor(BOOL bShow) +BOOL OTM_IDirect3DDevice9::ShowCursor(BOOL bShow) { - return(m_pIDirect3DDevice9->ShowCursor(bShow)); + return(m_pIDirect3DDevice9->ShowCursor(bShow)); } HRESULT OTM_IDirect3DDevice9::CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DSwapChain9** pSwapChain) { - return(m_pIDirect3DDevice9->CreateAdditionalSwapChain(pPresentationParameters,pSwapChain)); + return(m_pIDirect3DDevice9->CreateAdditionalSwapChain(pPresentationParameters,pSwapChain)); } HRESULT OTM_IDirect3DDevice9::GetSwapChain(UINT iSwapChain,IDirect3DSwapChain9** pSwapChain) { - return(m_pIDirect3DDevice9->GetSwapChain(iSwapChain,pSwapChain)); + return(m_pIDirect3DDevice9->GetSwapChain(iSwapChain,pSwapChain)); } -UINT OTM_IDirect3DDevice9::GetNumberOfSwapChains(void) +UINT OTM_IDirect3DDevice9::GetNumberOfSwapChains(void) { - return(m_pIDirect3DDevice9->GetNumberOfSwapChains()); + return(m_pIDirect3DDevice9->GetNumberOfSwapChains()); } HRESULT OTM_IDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS* pPresentationParameters) @@ -198,163 +192,155 @@ HRESULT OTM_IDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS* pPresentationParamete HRESULT OTM_IDirect3DDevice9::Present(CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) { - // we may want to draw own things here before flipping surfaces - // ... draw own stuff ... - //this->ShowWeAreHere(); - - // call original routine - HRESULT hres = m_pIDirect3DDevice9->Present( pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion); - - return (hres); + return (m_pIDirect3DDevice9->Present( pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion)); } HRESULT OTM_IDirect3DDevice9::GetBackBuffer(UINT iSwapChain,UINT iBackBuffer,D3DBACKBUFFER_TYPE Type,IDirect3DSurface9** ppBackBuffer) { - return(m_pIDirect3DDevice9->GetBackBuffer(iSwapChain,iBackBuffer,Type,ppBackBuffer)); + return(m_pIDirect3DDevice9->GetBackBuffer(iSwapChain,iBackBuffer,Type,ppBackBuffer)); } HRESULT OTM_IDirect3DDevice9::GetRasterStatus(UINT iSwapChain,D3DRASTER_STATUS* pRasterStatus) { - return(m_pIDirect3DDevice9->GetRasterStatus(iSwapChain,pRasterStatus)); + return(m_pIDirect3DDevice9->GetRasterStatus(iSwapChain,pRasterStatus)); } HRESULT OTM_IDirect3DDevice9::SetDialogBoxMode(BOOL bEnableDialogs) { - return(m_pIDirect3DDevice9->SetDialogBoxMode(bEnableDialogs)); + return(m_pIDirect3DDevice9->SetDialogBoxMode(bEnableDialogs)); } -void OTM_IDirect3DDevice9::SetGammaRamp(UINT iSwapChain,DWORD Flags,CONST D3DGAMMARAMP* pRamp) +void OTM_IDirect3DDevice9::SetGammaRamp(UINT iSwapChain,DWORD Flags,CONST D3DGAMMARAMP* pRamp) { - (m_pIDirect3DDevice9->SetGammaRamp(iSwapChain,Flags,pRamp)); + (m_pIDirect3DDevice9->SetGammaRamp(iSwapChain,Flags,pRamp)); } -void OTM_IDirect3DDevice9::GetGammaRamp(UINT iSwapChain,D3DGAMMARAMP* pRamp) +void OTM_IDirect3DDevice9::GetGammaRamp(UINT iSwapChain,D3DGAMMARAMP* pRamp) { (m_pIDirect3DDevice9->GetGammaRamp(iSwapChain,pRamp)); } HRESULT OTM_IDirect3DDevice9::CreateTexture(UINT Width,UINT Height,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DTexture9** ppTexture,HANDLE* pSharedHandle) { - //Message("CreateTexture( ): %lu\n", this); + //create real texture HRESULT ret = (m_pIDirect3DDevice9->CreateTexture(Width,Height,Levels,Usage,Format,Pool,ppTexture,pSharedHandle)); if(ret != D3D_OK) return (ret); + //create fake texture OTM_IDirect3DTexture9 *texture = new OTM_IDirect3DTexture9(ppTexture, this, Width, Height, Format); if (texture) *ppTexture = texture; - - if (LastCreatedTexture!=NULL) + if (LastCreatedTexture!=NULL) //if a texture was loaded before, hopefully this texture contains now the data, so we can add it { - if ( OTM_Client!=NULL) OTM_Client->AddTexture( LastCreatedTexture); + if ( OTM_Client!=NULL) OTM_Client->AddTexture( LastCreatedTexture); } - LastCreatedTexture = texture; - - //Message("end CreateTexture( ): %lu\n", this); return (ret); } HRESULT OTM_IDirect3DDevice9::CreateVolumeTexture(UINT Width,UINT Height,UINT Depth,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DVolumeTexture9** ppVolumeTexture,HANDLE* pSharedHandle) { - return (m_pIDirect3DDevice9->CreateVolumeTexture(Width,Height,Depth,Levels,Usage,Format,Pool,ppVolumeTexture,pSharedHandle)); + return (m_pIDirect3DDevice9->CreateVolumeTexture(Width,Height,Depth,Levels,Usage,Format,Pool,ppVolumeTexture,pSharedHandle)); } HRESULT OTM_IDirect3DDevice9::CreateCubeTexture(UINT EdgeLength,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DCubeTexture9** ppCubeTexture,HANDLE* pSharedHandle) { - return(m_pIDirect3DDevice9->CreateCubeTexture(EdgeLength,Levels,Usage,Format,Pool,ppCubeTexture,pSharedHandle)); + return(m_pIDirect3DDevice9->CreateCubeTexture(EdgeLength,Levels,Usage,Format,Pool,ppCubeTexture,pSharedHandle)); } HRESULT OTM_IDirect3DDevice9::CreateVertexBuffer(UINT Length,DWORD Usage,DWORD FVF,D3DPOOL Pool,IDirect3DVertexBuffer9** ppVertexBuffer,HANDLE* pSharedHandle) { - return(m_pIDirect3DDevice9->CreateVertexBuffer(Length,Usage,FVF,Pool,ppVertexBuffer,pSharedHandle)); + return(m_pIDirect3DDevice9->CreateVertexBuffer(Length,Usage,FVF,Pool,ppVertexBuffer,pSharedHandle)); } HRESULT OTM_IDirect3DDevice9::CreateIndexBuffer(UINT Length,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DIndexBuffer9** ppIndexBuffer,HANDLE* pSharedHandle) { - return(m_pIDirect3DDevice9->CreateIndexBuffer(Length,Usage,Format,Pool,ppIndexBuffer,pSharedHandle)); + return(m_pIDirect3DDevice9->CreateIndexBuffer(Length,Usage,Format,Pool,ppIndexBuffer,pSharedHandle)); } HRESULT OTM_IDirect3DDevice9::CreateRenderTarget(UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Lockable,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle) { - return(m_pIDirect3DDevice9->CreateRenderTarget(Width,Height,Format,MultiSample,MultisampleQuality,Lockable,ppSurface,pSharedHandle)); + return(m_pIDirect3DDevice9->CreateRenderTarget(Width,Height,Format,MultiSample,MultisampleQuality,Lockable,ppSurface,pSharedHandle)); } HRESULT OTM_IDirect3DDevice9::CreateDepthStencilSurface(UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Discard,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle) { - return(m_pIDirect3DDevice9->CreateDepthStencilSurface(Width,Height,Format,MultiSample,MultisampleQuality,Discard,ppSurface,pSharedHandle)); + return(m_pIDirect3DDevice9->CreateDepthStencilSurface(Width,Height,Format,MultiSample,MultisampleQuality,Discard,ppSurface,pSharedHandle)); } HRESULT OTM_IDirect3DDevice9::UpdateSurface(IDirect3DSurface9* pSourceSurface,CONST RECT* pSourceRect,IDirect3DSurface9* pDestinationSurface,CONST POINT* pDestPoint) { - return(m_pIDirect3DDevice9->UpdateSurface(pSourceSurface,pSourceRect,pDestinationSurface,pDestPoint)); + return(m_pIDirect3DDevice9->UpdateSurface(pSourceSurface,pSourceRect,pDestinationSurface,pDestPoint)); } HRESULT OTM_IDirect3DDevice9::UpdateTexture(IDirect3DBaseTexture9* pSourceTexture,IDirect3DBaseTexture9* pDestinationTexture) { + // we must pass the real texture objects + // if (dev != this) this texture was not initialized through our device and is thus no fake texture object + IDirect3DDevice9 *dev = NULL; if (pSourceTexture != NULL && ((OTM_IDirect3DTexture9*)(pSourceTexture))->GetDevice(&dev) == D3D_OK) { - if(dev == this) pSourceTexture = ((OTM_IDirect3DTexture9*)(pSourceTexture))->m_D3Dtex; + if (dev == this) pSourceTexture = ((OTM_IDirect3DTexture9*)(pSourceTexture))->m_D3Dtex; } if (pDestinationTexture != NULL && ((OTM_IDirect3DTexture9*)(pDestinationTexture))->GetDevice(&dev) == D3D_OK) { - if(dev == this) pDestinationTexture = ((OTM_IDirect3DTexture9*)(pDestinationTexture))->m_D3Dtex; + if (dev == this) pDestinationTexture = ((OTM_IDirect3DTexture9*)(pDestinationTexture))->m_D3Dtex; } return(m_pIDirect3DDevice9->UpdateTexture(pSourceTexture,pDestinationTexture)); } HRESULT OTM_IDirect3DDevice9::GetRenderTargetData(IDirect3DSurface9* pRenderTarget,IDirect3DSurface9* pDestSurface) { - return(m_pIDirect3DDevice9->GetRenderTargetData(pRenderTarget,pDestSurface)); + return(m_pIDirect3DDevice9->GetRenderTargetData(pRenderTarget,pDestSurface)); } HRESULT OTM_IDirect3DDevice9::GetFrontBufferData(UINT iSwapChain,IDirect3DSurface9* pDestSurface) { - return(m_pIDirect3DDevice9->GetFrontBufferData(iSwapChain,pDestSurface)); + return(m_pIDirect3DDevice9->GetFrontBufferData(iSwapChain,pDestSurface)); } HRESULT OTM_IDirect3DDevice9::StretchRect(IDirect3DSurface9* pSourceSurface,CONST RECT* pSourceRect,IDirect3DSurface9* pDestSurface,CONST RECT* pDestRect,D3DTEXTUREFILTERTYPE Filter) { - return(m_pIDirect3DDevice9->StretchRect(pSourceSurface,pSourceRect,pDestSurface,pDestRect,Filter)); + return(m_pIDirect3DDevice9->StretchRect(pSourceSurface,pSourceRect,pDestSurface,pDestRect,Filter)); } HRESULT OTM_IDirect3DDevice9::ColorFill(IDirect3DSurface9* pSurface,CONST RECT* pRect,D3DCOLOR color) { - return(m_pIDirect3DDevice9->ColorFill(pSurface,pRect,color)); + return(m_pIDirect3DDevice9->ColorFill(pSurface,pRect,color)); } HRESULT OTM_IDirect3DDevice9::CreateOffscreenPlainSurface(UINT Width,UINT Height,D3DFORMAT Format,D3DPOOL Pool,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle) { - return(m_pIDirect3DDevice9->CreateOffscreenPlainSurface(Width,Height,Format,Pool,ppSurface,pSharedHandle)); + return(m_pIDirect3DDevice9->CreateOffscreenPlainSurface(Width,Height,Format,Pool,ppSurface,pSharedHandle)); } HRESULT OTM_IDirect3DDevice9::SetRenderTarget(DWORD RenderTargetIndex,IDirect3DSurface9* pRenderTarget) { - return(m_pIDirect3DDevice9->SetRenderTarget(RenderTargetIndex,pRenderTarget)); + return(m_pIDirect3DDevice9->SetRenderTarget(RenderTargetIndex,pRenderTarget)); } HRESULT OTM_IDirect3DDevice9::GetRenderTarget(DWORD RenderTargetIndex,IDirect3DSurface9** ppRenderTarget) { - return(m_pIDirect3DDevice9->GetRenderTarget(RenderTargetIndex,ppRenderTarget)); + return(m_pIDirect3DDevice9->GetRenderTarget(RenderTargetIndex,ppRenderTarget)); } HRESULT OTM_IDirect3DDevice9::SetDepthStencilSurface(IDirect3DSurface9* pNewZStencil) { - return(m_pIDirect3DDevice9->SetDepthStencilSurface(pNewZStencil)); + return(m_pIDirect3DDevice9->SetDepthStencilSurface(pNewZStencil)); } HRESULT OTM_IDirect3DDevice9::GetDepthStencilSurface(IDirect3DSurface9** ppZStencilSurface) { - return(m_pIDirect3DDevice9->GetDepthStencilSurface(ppZStencilSurface)); + return(m_pIDirect3DDevice9->GetDepthStencilSurface(ppZStencilSurface)); } HRESULT OTM_IDirect3DDevice9::BeginScene(void) { - //Message("BeginScene(): %lu\n", this); - if ( OTM_Client!=NULL) + //if ( OTM_Client!=NULL) // this if condition is senseless, since an exception is raised if the client could not be created { - OTM_Client->MergeUpdate(); - if (LastCreatedTexture!=NULL) + OTM_Client->MergeUpdate(); // merge an update, if present + if (LastCreatedTexture!=NULL) // add the last created texture { OTM_Client->AddTexture( LastCreatedTexture); LastCreatedTexture = NULL; @@ -365,7 +351,7 @@ HRESULT OTM_IDirect3DDevice9::BeginScene(void) if (SingleTexture!=NULL) { - if (TextureColour!=OTM_Client->TextureColour) + if (TextureColour!=OTM_Client->TextureColour) //if TextureColour has changed, dye the texture in the new colour { D3DLOCKED_RECT d3dlr; IDirect3DTexture9 *pD3Dtex; @@ -380,42 +366,45 @@ HRESULT OTM_IDirect3DDevice9::BeginScene(void) pD3Dtex->UnlockRect(0); } } - if (OTM_Client->KeyBack>0 && (GetAsyncKeyState( OTM_Client->KeyBack ) &1) ) + if (OTM_Client->KeyBack>0 && (GetAsyncKeyState( OTM_Client->KeyBack ) &1) ) //ask for the status of the back key { - UnswitchTextures( SingleTexture); - if (CounterSaveSingleTexture<0) CounterSaveSingleTexture = 0; //first initialization + UnswitchTextures( SingleTexture); // can be called, even if texture is not switched + if (CounterSaveSingleTexture<0) CounterSaveSingleTexture = 0; //first initialization of the counter else if (--CounterSaveSingleTexture<0) CounterSaveSingleTexture = OTM_Client->OriginalTextures.GetNumber() - 1; - if (CounterSaveSingleTexture >= 0) SwitchTextures( SingleTexture, OTM_Client->OriginalTextures[CounterSaveSingleTexture]); + if (CounterSaveSingleTexture >= 0) + { + SwitchTextures( SingleTexture, OTM_Client->OriginalTextures[CounterSaveSingleTexture]); + SingleTexture->Hash = OTM_Client->OriginalTextures[CounterSaveSingleTexture]->Hash; //set the hash for the display + } } - if (OTM_Client->KeySave>0 && (GetAsyncKeyState( OTM_Client->KeySave ) &1) ) + if (OTM_Client->KeySave>0 && (GetAsyncKeyState( OTM_Client->KeySave ) &1) ) //ask for the status of the save key { - SingleTexture->Hash = SingleTexture->CrossRef_D3Dtex->Hash; - OTM_Client->SaveTexture( SingleTexture); - SingleTexture->Hash = 0u; + OTM_Client->SaveTexture( SingleTexture); //after switching the SingleTexture holds the pointer to the original texture object } - if (OTM_Client->KeyNext>0 && (GetAsyncKeyState( OTM_Client->KeyNext ) &1) ) + if (OTM_Client->KeyNext>0 && (GetAsyncKeyState( OTM_Client->KeyNext ) &1) ) //ask for the status of the next key { - UnswitchTextures( SingleTexture); - if (CounterSaveSingleTexture<0) CounterSaveSingleTexture = 0; //first initialization + UnswitchTextures( SingleTexture); // can be called, even if texture is not switched + if (CounterSaveSingleTexture<0) CounterSaveSingleTexture = 0; //first initialization of the counter else if (++CounterSaveSingleTexture>=OTM_Client->OriginalTextures.GetNumber()) CounterSaveSingleTexture = 0; - if (CounterSaveSingleTexture < OTM_Client->OriginalTextures.GetNumber()) SwitchTextures( SingleTexture, OTM_Client->OriginalTextures[CounterSaveSingleTexture]); + if (CounterSaveSingleTexture < OTM_Client->OriginalTextures.GetNumber()) + { + SwitchTextures( SingleTexture, OTM_Client->OriginalTextures[CounterSaveSingleTexture]); + SingleTexture->Hash = OTM_Client->OriginalTextures[CounterSaveSingleTexture]->Hash; //set the hash for the display + } } } } } - Return_BeginScene = m_pIDirect3DDevice9->BeginScene(); - return (Return_BeginScene); - //return (m_pIDirect3DDevice9->BeginScene()); + + return (m_pIDirect3DDevice9->BeginScene()); } HRESULT OTM_IDirect3DDevice9::EndScene(void) { - if ( Return_BeginScene==D3D_OK && OTM_Client!=NULL && OTM_Client->BoolSaveSingleTexture && SingleTexture!=NULL && SingleTexture->CrossRef_D3Dtex!=NULL) + if ( /*OTM_Client!=NULL &&*/ OTM_Client->BoolSaveSingleTexture && SingleTexture!=NULL && SingleTexture->CrossRef_D3Dtex!=NULL) { - if (OSD_Font==NULL || FontColour != OTM_Client->FontColour) + if (OSD_Font==NULL) // create the font { - FontColour = OTM_Client->FontColour; - if (OSD_Font!=NULL) OSD_Font->Release(); if (D3D_OK!=D3DXCreateFontA( m_pIDirect3DDevice9, 20, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &OSD_Font)) { OSD_Font=NULL; @@ -424,399 +413,400 @@ HRESULT OTM_IDirect3DDevice9::EndScene(void) } char buffer[100]; - sprintf_s( buffer, 100, "Actual texture: %4d (1..%d): %#lX", CounterSaveSingleTexture+1, OTM_Client->OriginalTextures.GetNumber(), SingleTexture->CrossRef_D3Dtex->Hash); + sprintf_s( buffer, 100, "Actual texture: %4d (1..%d): %#lX", CounterSaveSingleTexture+1, OTM_Client->OriginalTextures.GetNumber(), SingleTexture->Hash); D3DVIEWPORT9 viewport; GetViewport( &viewport); RECT rct; rct.left=viewport.X + 10; - rct.right=0; + rct.right=0; //size of box is calculated automatically (DT_NOCLIP) rct.top=viewport.Y + 10; - rct.bottom=0; - OSD_Font->DrawTextA(NULL, buffer, -1, &rct, DT_NOCLIP, FontColour); - + rct.bottom=0; //size of box is calculated automatically (DT_NOCLIP) + OSD_Font->DrawTextA(NULL, buffer, -1, &rct, DT_NOCLIP, OTM_Client->FontColour); } - //Message("EndScene(): %lu\n", this); return(m_pIDirect3DDevice9->EndScene()); } HRESULT OTM_IDirect3DDevice9::Clear(DWORD Count,CONST D3DRECT* pRects,DWORD Flags,D3DCOLOR Color,float Z,DWORD Stencil) { - return(m_pIDirect3DDevice9->Clear(Count,pRects,Flags,Color,Z,Stencil)); + return(m_pIDirect3DDevice9->Clear(Count,pRects,Flags,Color,Z,Stencil)); } HRESULT OTM_IDirect3DDevice9::SetTransform(D3DTRANSFORMSTATETYPE State,CONST D3DMATRIX* pMatrix) { - return(m_pIDirect3DDevice9->SetTransform(State,pMatrix)); + return(m_pIDirect3DDevice9->SetTransform(State,pMatrix)); } HRESULT OTM_IDirect3DDevice9::GetTransform(D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) { - return(m_pIDirect3DDevice9->GetTransform(State,pMatrix)); + return(m_pIDirect3DDevice9->GetTransform(State,pMatrix)); } HRESULT OTM_IDirect3DDevice9::MultiplyTransform(D3DTRANSFORMSTATETYPE State,CONST D3DMATRIX* pMatrix) { - return(m_pIDirect3DDevice9->MultiplyTransform(State,pMatrix)); + return(m_pIDirect3DDevice9->MultiplyTransform(State,pMatrix)); } HRESULT OTM_IDirect3DDevice9::SetViewport(CONST D3DVIEWPORT9* pViewport) { - return(m_pIDirect3DDevice9->SetViewport(pViewport)); + return(m_pIDirect3DDevice9->SetViewport(pViewport)); } HRESULT OTM_IDirect3DDevice9::GetViewport(D3DVIEWPORT9* pViewport) { - return(m_pIDirect3DDevice9->GetViewport(pViewport)); + return(m_pIDirect3DDevice9->GetViewport(pViewport)); } HRESULT OTM_IDirect3DDevice9::SetMaterial(CONST D3DMATERIAL9* pMaterial) { - return(m_pIDirect3DDevice9->SetMaterial(pMaterial)); + return(m_pIDirect3DDevice9->SetMaterial(pMaterial)); } HRESULT OTM_IDirect3DDevice9::GetMaterial(D3DMATERIAL9* pMaterial) { - return(m_pIDirect3DDevice9->GetMaterial(pMaterial)); + return(m_pIDirect3DDevice9->GetMaterial(pMaterial)); } HRESULT OTM_IDirect3DDevice9::SetLight(DWORD Index,CONST D3DLIGHT9* pLight) { - return(m_pIDirect3DDevice9->SetLight(Index,pLight)); + return(m_pIDirect3DDevice9->SetLight(Index,pLight)); } HRESULT OTM_IDirect3DDevice9::GetLight(DWORD Index,D3DLIGHT9* pLight) { - return(m_pIDirect3DDevice9->GetLight(Index,pLight)); + return(m_pIDirect3DDevice9->GetLight(Index,pLight)); } HRESULT OTM_IDirect3DDevice9::LightEnable(DWORD Index,BOOL Enable) { - return(m_pIDirect3DDevice9->LightEnable(Index,Enable)); + return(m_pIDirect3DDevice9->LightEnable(Index,Enable)); } HRESULT OTM_IDirect3DDevice9::GetLightEnable(DWORD Index,BOOL* pEnable) { - return(m_pIDirect3DDevice9->GetLightEnable(Index, pEnable)); + return(m_pIDirect3DDevice9->GetLightEnable(Index, pEnable)); } HRESULT OTM_IDirect3DDevice9::SetClipPlane(DWORD Index,CONST float* pPlane) { - return(m_pIDirect3DDevice9->SetClipPlane(Index, pPlane)); + return(m_pIDirect3DDevice9->SetClipPlane(Index, pPlane)); } HRESULT OTM_IDirect3DDevice9::GetClipPlane(DWORD Index,float* pPlane) { - return(m_pIDirect3DDevice9->GetClipPlane(Index,pPlane)); + return(m_pIDirect3DDevice9->GetClipPlane(Index,pPlane)); } HRESULT OTM_IDirect3DDevice9::SetRenderState(D3DRENDERSTATETYPE State,DWORD Value) { - return(m_pIDirect3DDevice9->SetRenderState(State, Value)); + return(m_pIDirect3DDevice9->SetRenderState(State, Value)); } HRESULT OTM_IDirect3DDevice9::GetRenderState(D3DRENDERSTATETYPE State,DWORD* pValue) { - return(m_pIDirect3DDevice9->GetRenderState(State, pValue)); + return(m_pIDirect3DDevice9->GetRenderState(State, pValue)); } HRESULT OTM_IDirect3DDevice9::CreateStateBlock(D3DSTATEBLOCKTYPE Type,IDirect3DStateBlock9** ppSB) { - return(m_pIDirect3DDevice9->CreateStateBlock(Type,ppSB)); + return(m_pIDirect3DDevice9->CreateStateBlock(Type,ppSB)); } HRESULT OTM_IDirect3DDevice9::BeginStateBlock(void) { - return(m_pIDirect3DDevice9->BeginStateBlock()); + return(m_pIDirect3DDevice9->BeginStateBlock()); } HRESULT OTM_IDirect3DDevice9::EndStateBlock(IDirect3DStateBlock9** ppSB) { - return(m_pIDirect3DDevice9->EndStateBlock(ppSB)); + return(m_pIDirect3DDevice9->EndStateBlock(ppSB)); } HRESULT OTM_IDirect3DDevice9::SetClipStatus(CONST D3DCLIPSTATUS9* pClipStatus) { - return(m_pIDirect3DDevice9->SetClipStatus(pClipStatus)); + return(m_pIDirect3DDevice9->SetClipStatus(pClipStatus)); } HRESULT OTM_IDirect3DDevice9::GetClipStatus(D3DCLIPSTATUS9* pClipStatus) { - return(m_pIDirect3DDevice9->GetClipStatus( pClipStatus)); + return(m_pIDirect3DDevice9->GetClipStatus( pClipStatus)); } HRESULT OTM_IDirect3DDevice9::GetTexture(DWORD Stage,IDirect3DBaseTexture9** ppTexture) { - return(m_pIDirect3DDevice9->GetTexture(Stage,ppTexture)); + return(m_pIDirect3DDevice9->GetTexture(Stage,ppTexture)); } HRESULT OTM_IDirect3DDevice9::SetTexture(DWORD Stage, IDirect3DBaseTexture9* pTexture) { + // we must pass the real texture objects + // if (dev != this) this texture was not initialized through our device and is thus no fake texture object + IDirect3DDevice9 *dev = NULL; if (pTexture != NULL && ((OTM_IDirect3DTexture9*)(pTexture))->GetDevice(&dev) == D3D_OK) { - if(dev == this) return (m_pIDirect3DDevice9->SetTexture(Stage, ((OTM_IDirect3DTexture9*)(pTexture))->m_D3Dtex)); + if(dev == this) pTexture = ((OTM_IDirect3DTexture9*)(pTexture))->m_D3Dtex; } return (m_pIDirect3DDevice9->SetTexture(Stage, pTexture)); } HRESULT OTM_IDirect3DDevice9::GetTextureStageState(DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) { - return(m_pIDirect3DDevice9->GetTextureStageState(Stage,Type, pValue)); + return(m_pIDirect3DDevice9->GetTextureStageState(Stage,Type, pValue)); } HRESULT OTM_IDirect3DDevice9::SetTextureStageState(DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD Value) { - return(m_pIDirect3DDevice9->SetTextureStageState(Stage,Type,Value)); + return(m_pIDirect3DDevice9->SetTextureStageState(Stage,Type,Value)); } HRESULT OTM_IDirect3DDevice9::GetSamplerState(DWORD Sampler,D3DSAMPLERSTATETYPE Type,DWORD* pValue) { - return(m_pIDirect3DDevice9->GetSamplerState(Sampler,Type, pValue)); + return(m_pIDirect3DDevice9->GetSamplerState(Sampler,Type, pValue)); } HRESULT OTM_IDirect3DDevice9::SetSamplerState(DWORD Sampler,D3DSAMPLERSTATETYPE Type,DWORD Value) { - return(m_pIDirect3DDevice9->SetSamplerState(Sampler,Type,Value)); + return(m_pIDirect3DDevice9->SetSamplerState(Sampler,Type,Value)); } HRESULT OTM_IDirect3DDevice9::ValidateDevice(DWORD* pNumPasses) { - return(m_pIDirect3DDevice9->ValidateDevice( pNumPasses)); + return(m_pIDirect3DDevice9->ValidateDevice( pNumPasses)); } HRESULT OTM_IDirect3DDevice9::SetPaletteEntries(UINT PaletteNumber,CONST PALETTEENTRY* pEntries) { - return(m_pIDirect3DDevice9->SetPaletteEntries(PaletteNumber, pEntries)); + return(m_pIDirect3DDevice9->SetPaletteEntries(PaletteNumber, pEntries)); } HRESULT OTM_IDirect3DDevice9::GetPaletteEntries(UINT PaletteNumber,PALETTEENTRY* pEntries) { - return(m_pIDirect3DDevice9->GetPaletteEntries(PaletteNumber, pEntries)); + return(m_pIDirect3DDevice9->GetPaletteEntries(PaletteNumber, pEntries)); } HRESULT OTM_IDirect3DDevice9::SetCurrentTexturePalette(UINT PaletteNumber) { - return(m_pIDirect3DDevice9->SetCurrentTexturePalette(PaletteNumber)); + return(m_pIDirect3DDevice9->SetCurrentTexturePalette(PaletteNumber)); } HRESULT OTM_IDirect3DDevice9::GetCurrentTexturePalette(UINT *PaletteNumber) { - return(m_pIDirect3DDevice9->GetCurrentTexturePalette(PaletteNumber)); + return(m_pIDirect3DDevice9->GetCurrentTexturePalette(PaletteNumber)); } HRESULT OTM_IDirect3DDevice9::SetScissorRect(CONST RECT* pRect) { - return(m_pIDirect3DDevice9->SetScissorRect( pRect)); + return(m_pIDirect3DDevice9->SetScissorRect( pRect)); } HRESULT OTM_IDirect3DDevice9::GetScissorRect( RECT* pRect) { - return(m_pIDirect3DDevice9->GetScissorRect( pRect)); + return(m_pIDirect3DDevice9->GetScissorRect( pRect)); } HRESULT OTM_IDirect3DDevice9::SetSoftwareVertexProcessing(BOOL bSoftware) { - return(m_pIDirect3DDevice9->SetSoftwareVertexProcessing(bSoftware)); + return(m_pIDirect3DDevice9->SetSoftwareVertexProcessing(bSoftware)); } -BOOL OTM_IDirect3DDevice9::GetSoftwareVertexProcessing(void) +BOOL OTM_IDirect3DDevice9::GetSoftwareVertexProcessing(void) { - return(m_pIDirect3DDevice9->GetSoftwareVertexProcessing()); + return(m_pIDirect3DDevice9->GetSoftwareVertexProcessing()); } HRESULT OTM_IDirect3DDevice9::SetNPatchMode(float nSegments) { - return(m_pIDirect3DDevice9->SetNPatchMode(nSegments)); + return(m_pIDirect3DDevice9->SetNPatchMode(nSegments)); } float OTM_IDirect3DDevice9::GetNPatchMode(void) { - return(m_pIDirect3DDevice9->GetNPatchMode()); + return(m_pIDirect3DDevice9->GetNPatchMode()); } HRESULT OTM_IDirect3DDevice9::DrawPrimitive(D3DPRIMITIVETYPE PrimitiveType,UINT StartVertex,UINT PrimitiveCount) { - return(m_pIDirect3DDevice9->DrawPrimitive(PrimitiveType,StartVertex,PrimitiveCount)); + return(m_pIDirect3DDevice9->DrawPrimitive(PrimitiveType,StartVertex,PrimitiveCount)); } HRESULT OTM_IDirect3DDevice9::DrawIndexedPrimitive(D3DPRIMITIVETYPE PrimitiveType,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) { - return(m_pIDirect3DDevice9->DrawIndexedPrimitive(PrimitiveType,BaseVertexIndex,MinVertexIndex,NumVertices,startIndex,primCount)); + return(m_pIDirect3DDevice9->DrawIndexedPrimitive(PrimitiveType,BaseVertexIndex,MinVertexIndex,NumVertices,startIndex,primCount)); } HRESULT OTM_IDirect3DDevice9::DrawPrimitiveUP(D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) { - return(m_pIDirect3DDevice9->DrawPrimitiveUP(PrimitiveType,PrimitiveCount,pVertexStreamZeroData,VertexStreamZeroStride)); + return(m_pIDirect3DDevice9->DrawPrimitiveUP(PrimitiveType,PrimitiveCount,pVertexStreamZeroData,VertexStreamZeroStride)); } HRESULT OTM_IDirect3DDevice9::DrawIndexedPrimitiveUP(D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,UINT NumVertices,UINT PrimitiveCount,CONST void* pIndexData,D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) { - return(m_pIDirect3DDevice9->DrawIndexedPrimitiveUP(PrimitiveType,MinVertexIndex,NumVertices,PrimitiveCount, pIndexData, IndexDataFormat, pVertexStreamZeroData,VertexStreamZeroStride)); + return(m_pIDirect3DDevice9->DrawIndexedPrimitiveUP(PrimitiveType,MinVertexIndex,NumVertices,PrimitiveCount, pIndexData, IndexDataFormat, pVertexStreamZeroData,VertexStreamZeroStride)); } HRESULT OTM_IDirect3DDevice9::ProcessVertices(UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer9* pDestBuffer,IDirect3DVertexDeclaration9* pVertexDecl,DWORD Flags) { - return(m_pIDirect3DDevice9->ProcessVertices( SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags)); + return(m_pIDirect3DDevice9->ProcessVertices( SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags)); } HRESULT OTM_IDirect3DDevice9::CreateVertexDeclaration(CONST D3DVERTEXELEMENT9* pVertexElements,IDirect3DVertexDeclaration9** ppDecl) { - return(m_pIDirect3DDevice9->CreateVertexDeclaration( pVertexElements,ppDecl)); + return(m_pIDirect3DDevice9->CreateVertexDeclaration( pVertexElements,ppDecl)); } HRESULT OTM_IDirect3DDevice9::SetVertexDeclaration(IDirect3DVertexDeclaration9* pDecl) { - return(m_pIDirect3DDevice9->SetVertexDeclaration(pDecl)); + return(m_pIDirect3DDevice9->SetVertexDeclaration(pDecl)); } HRESULT OTM_IDirect3DDevice9::GetVertexDeclaration(IDirect3DVertexDeclaration9** ppDecl) { - return(m_pIDirect3DDevice9->GetVertexDeclaration(ppDecl)); + return(m_pIDirect3DDevice9->GetVertexDeclaration(ppDecl)); } HRESULT OTM_IDirect3DDevice9::SetFVF(DWORD FVF) { - return(m_pIDirect3DDevice9->SetFVF(FVF)); + return(m_pIDirect3DDevice9->SetFVF(FVF)); } HRESULT OTM_IDirect3DDevice9::GetFVF(DWORD* pFVF) { - return(m_pIDirect3DDevice9->GetFVF(pFVF)); + return(m_pIDirect3DDevice9->GetFVF(pFVF)); } HRESULT OTM_IDirect3DDevice9::CreateVertexShader(CONST DWORD* pFunction,IDirect3DVertexShader9** ppShader) { - return(m_pIDirect3DDevice9->CreateVertexShader(pFunction,ppShader)); + return(m_pIDirect3DDevice9->CreateVertexShader(pFunction,ppShader)); } HRESULT OTM_IDirect3DDevice9::SetVertexShader(IDirect3DVertexShader9* pShader) { - return(m_pIDirect3DDevice9->SetVertexShader(pShader)); + return(m_pIDirect3DDevice9->SetVertexShader(pShader)); } HRESULT OTM_IDirect3DDevice9::GetVertexShader(IDirect3DVertexShader9** ppShader) { - return(m_pIDirect3DDevice9->GetVertexShader(ppShader)); + return(m_pIDirect3DDevice9->GetVertexShader(ppShader)); } HRESULT OTM_IDirect3DDevice9::SetVertexShaderConstantF(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount) { - return(m_pIDirect3DDevice9->SetVertexShaderConstantF(StartRegister,pConstantData, Vector4fCount)); + return(m_pIDirect3DDevice9->SetVertexShaderConstantF(StartRegister,pConstantData, Vector4fCount)); } HRESULT OTM_IDirect3DDevice9::GetVertexShaderConstantF(UINT StartRegister,float* pConstantData,UINT Vector4fCount) { - return(m_pIDirect3DDevice9->GetVertexShaderConstantF(StartRegister,pConstantData,Vector4fCount)); + return(m_pIDirect3DDevice9->GetVertexShaderConstantF(StartRegister,pConstantData,Vector4fCount)); } HRESULT OTM_IDirect3DDevice9::SetVertexShaderConstantI(UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount) { - return(m_pIDirect3DDevice9->SetVertexShaderConstantI(StartRegister,pConstantData,Vector4iCount)); + return(m_pIDirect3DDevice9->SetVertexShaderConstantI(StartRegister,pConstantData,Vector4iCount)); } HRESULT OTM_IDirect3DDevice9::GetVertexShaderConstantI(UINT StartRegister,int* pConstantData,UINT Vector4iCount) { - return(m_pIDirect3DDevice9->GetVertexShaderConstantI(StartRegister,pConstantData,Vector4iCount)); + return(m_pIDirect3DDevice9->GetVertexShaderConstantI(StartRegister,pConstantData,Vector4iCount)); } HRESULT OTM_IDirect3DDevice9::SetVertexShaderConstantB(UINT StartRegister,CONST BOOL* pConstantData,UINT BoolCount) { - return(m_pIDirect3DDevice9->SetVertexShaderConstantB(StartRegister,pConstantData,BoolCount)); + return(m_pIDirect3DDevice9->SetVertexShaderConstantB(StartRegister,pConstantData,BoolCount)); } HRESULT OTM_IDirect3DDevice9::GetVertexShaderConstantB(UINT StartRegister,BOOL* pConstantData,UINT BoolCount) { - return(m_pIDirect3DDevice9->GetVertexShaderConstantB(StartRegister,pConstantData,BoolCount)); + return(m_pIDirect3DDevice9->GetVertexShaderConstantB(StartRegister,pConstantData,BoolCount)); } HRESULT OTM_IDirect3DDevice9::SetStreamSource(UINT StreamNumber,IDirect3DVertexBuffer9* pStreamData,UINT OffsetInBytes,UINT Stride) { - return(m_pIDirect3DDevice9->SetStreamSource(StreamNumber,pStreamData,OffsetInBytes,Stride)); + return(m_pIDirect3DDevice9->SetStreamSource(StreamNumber,pStreamData,OffsetInBytes,Stride)); } HRESULT OTM_IDirect3DDevice9::GetStreamSource(UINT StreamNumber,IDirect3DVertexBuffer9** ppStreamData,UINT* OffsetInBytes,UINT* pStride) { - return(m_pIDirect3DDevice9->GetStreamSource(StreamNumber,ppStreamData,OffsetInBytes,pStride)); + return(m_pIDirect3DDevice9->GetStreamSource(StreamNumber,ppStreamData,OffsetInBytes,pStride)); } HRESULT OTM_IDirect3DDevice9::SetStreamSourceFreq(UINT StreamNumber,UINT Divider) { - return(m_pIDirect3DDevice9->SetStreamSourceFreq(StreamNumber,Divider)); + return(m_pIDirect3DDevice9->SetStreamSourceFreq(StreamNumber,Divider)); } HRESULT OTM_IDirect3DDevice9::GetStreamSourceFreq(UINT StreamNumber,UINT* Divider) { - return(m_pIDirect3DDevice9->GetStreamSourceFreq(StreamNumber,Divider)); + return(m_pIDirect3DDevice9->GetStreamSourceFreq(StreamNumber,Divider)); } HRESULT OTM_IDirect3DDevice9::SetIndices(IDirect3DIndexBuffer9* pIndexData) { - return(m_pIDirect3DDevice9->SetIndices(pIndexData)); + return(m_pIDirect3DDevice9->SetIndices(pIndexData)); } HRESULT OTM_IDirect3DDevice9::GetIndices(IDirect3DIndexBuffer9** ppIndexData) { - return(m_pIDirect3DDevice9->GetIndices(ppIndexData)); + return(m_pIDirect3DDevice9->GetIndices(ppIndexData)); } HRESULT OTM_IDirect3DDevice9::CreatePixelShader(CONST DWORD* pFunction,IDirect3DPixelShader9** ppShader) { - return(m_pIDirect3DDevice9->CreatePixelShader(pFunction,ppShader)); + return(m_pIDirect3DDevice9->CreatePixelShader(pFunction,ppShader)); } HRESULT OTM_IDirect3DDevice9::SetPixelShader(IDirect3DPixelShader9* pShader) { - return(m_pIDirect3DDevice9->SetPixelShader(pShader)); + return(m_pIDirect3DDevice9->SetPixelShader(pShader)); } HRESULT OTM_IDirect3DDevice9::GetPixelShader(IDirect3DPixelShader9** ppShader) { - return(m_pIDirect3DDevice9->GetPixelShader(ppShader)); + return(m_pIDirect3DDevice9->GetPixelShader(ppShader)); } HRESULT OTM_IDirect3DDevice9::SetPixelShaderConstantF(UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount) { - return(m_pIDirect3DDevice9->SetPixelShaderConstantF(StartRegister,pConstantData,Vector4fCount)); + return(m_pIDirect3DDevice9->SetPixelShaderConstantF(StartRegister,pConstantData,Vector4fCount)); } HRESULT OTM_IDirect3DDevice9::GetPixelShaderConstantF(UINT StartRegister,float* pConstantData,UINT Vector4fCount) { - return(m_pIDirect3DDevice9->GetPixelShaderConstantF(StartRegister,pConstantData,Vector4fCount)); + return(m_pIDirect3DDevice9->GetPixelShaderConstantF(StartRegister,pConstantData,Vector4fCount)); } HRESULT OTM_IDirect3DDevice9::SetPixelShaderConstantI(UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount) { - return(m_pIDirect3DDevice9->SetPixelShaderConstantI(StartRegister,pConstantData,Vector4iCount)); + return(m_pIDirect3DDevice9->SetPixelShaderConstantI(StartRegister,pConstantData,Vector4iCount)); } HRESULT OTM_IDirect3DDevice9::GetPixelShaderConstantI(UINT StartRegister,int* pConstantData,UINT Vector4iCount) { - return(m_pIDirect3DDevice9->GetPixelShaderConstantI(StartRegister,pConstantData,Vector4iCount)); + return(m_pIDirect3DDevice9->GetPixelShaderConstantI(StartRegister,pConstantData,Vector4iCount)); } HRESULT OTM_IDirect3DDevice9::SetPixelShaderConstantB(UINT StartRegister,CONST BOOL* pConstantData,UINT BoolCount) { - return(m_pIDirect3DDevice9->SetPixelShaderConstantB(StartRegister,pConstantData,BoolCount)); + return(m_pIDirect3DDevice9->SetPixelShaderConstantB(StartRegister,pConstantData,BoolCount)); } HRESULT OTM_IDirect3DDevice9::GetPixelShaderConstantB(UINT StartRegister,BOOL* pConstantData,UINT BoolCount) { - return(m_pIDirect3DDevice9->GetPixelShaderConstantB(StartRegister,pConstantData,BoolCount)); + return(m_pIDirect3DDevice9->GetPixelShaderConstantB(StartRegister,pConstantData,BoolCount)); } HRESULT OTM_IDirect3DDevice9::DrawRectPatch(UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) { - return(m_pIDirect3DDevice9->DrawRectPatch(Handle,pNumSegs, pRectPatchInfo)); + return(m_pIDirect3DDevice9->DrawRectPatch(Handle,pNumSegs, pRectPatchInfo)); } HRESULT OTM_IDirect3DDevice9::DrawTriPatch(UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) { - return(m_pIDirect3DDevice9->DrawTriPatch(Handle, pNumSegs, pTriPatchInfo)); + return(m_pIDirect3DDevice9->DrawTriPatch(Handle, pNumSegs, pTriPatchInfo)); } HRESULT OTM_IDirect3DDevice9::DeletePatch(UINT Handle) { - return(m_pIDirect3DDevice9->DeletePatch(Handle)); + return(m_pIDirect3DDevice9->DeletePatch(Handle)); } HRESULT OTM_IDirect3DDevice9::CreateQuery(D3DQUERYTYPE Type,IDirect3DQuery9** ppQuery) diff --git a/OTM_DX9/OTM_IDirect3DDevice9.h b/OTM_DX9/OTM_IDirect3DDevice9.h index 2c664ad..92b8d9b 100644 --- a/OTM_DX9/OTM_IDirect3DDevice9.h +++ b/OTM_DX9/OTM_IDirect3DDevice9.h @@ -160,25 +160,22 @@ public: HRESULT __stdcall CreateQuery(D3DQUERYTYPE Type,IDirect3DQuery9** ppQuery); // END: The original DX9 function definitions + + OTM_TextureClient* GetOTM_Client(void) {return (OTM_Client);} OTM_IDirect3DTexture9* GetLastCreatedTexture(void) {return (LastCreatedTexture);} int SetLastCreatedTexture(OTM_IDirect3DTexture9* pTexture) {LastCreatedTexture=pTexture; return (RETURN_OK);} OTM_IDirect3DTexture9* GetSingleTexture(void) {return (SingleTexture);} -/* - int SetKeyBack( int key) {if (key>0) KeyBack=key; return (0);} - int SetKeySave( int key) {if (key>0) KeySave=key; return (0);} - int SetKeyNext( int key) {if (key>0) KeyNext=key; return (0);} -*/ + private: int CreateSingleTexture(void); int CounterSaveSingleTexture; OTM_IDirect3DTexture9* SingleTexture; D3DCOLOR TextureColour; ID3DXFont *OSD_Font; - D3DCOLOR FontColour; + //D3DCOLOR FontColour; - - HRESULT Return_BeginScene; + int OTM_Reference; IDirect3DDevice9* m_pIDirect3DDevice9; OTM_IDirect3DTexture9* LastCreatedTexture; OTM_TextureServer* OTM_Server; diff --git a/OTM_DX9/OTM_IDirect3DTexture9.cpp b/OTM_DX9/OTM_IDirect3DTexture9.cpp index ac4ce9b..bd7ba01 100644 --- a/OTM_DX9/OTM_IDirect3DTexture9.cpp +++ b/OTM_DX9/OTM_IDirect3DTexture9.cpp @@ -16,7 +16,10 @@ You should have received a copy of the GNU General Public License along with OpenTexMod. If not, see . */ - +/* + * some function (e.g. AddReff()) are presumed to work on the texture object which belong to them + * if this texture was switched, we must redirect this calls to the CrossRef_D3Dtex texture object + */ #include "OTM_Main.h" @@ -26,9 +29,10 @@ HRESULT APIENTRY OTM_IDirect3DTexture9::QueryInterface(REFIID riid, void** ppvOb return (m_D3Dtex->QueryInterface(riid, ppvObj)); } +//this function yields for the non switched texture object ULONG APIENTRY OTM_IDirect3DTexture9::AddRef() { - if (FAKE) return (1); //this case should never happen + if (FAKE) return (1); //bug, this case should never happen if (CrossRef_D3Dtex!=NULL) { return (CrossRef_D3Dtex->m_D3Dtex->AddRef()); @@ -36,6 +40,7 @@ ULONG APIENTRY OTM_IDirect3DTexture9::AddRef() else return (m_D3Dtex->AddRef()); } +//this function yields for the non switched texture object ULONG APIENTRY OTM_IDirect3DTexture9::Release() { ULONG count; @@ -46,14 +51,14 @@ ULONG APIENTRY OTM_IDirect3DTexture9::Release() } else { - if (CrossRef_D3Dtex!=NULL) + if (CrossRef_D3Dtex!=NULL) //if this texture is switched with a fake texture { OTM_IDirect3DTexture9 *fake_texture = CrossRef_D3Dtex; count = fake_texture->m_D3Dtex->Release(); //release the original texture - if (count==0) + if (count==0) //if texture is released we switch the textures back { UnswitchTextures(this); - if (((OTM_IDirect3DDevice9*)m_D3Ddev)->GetSingleTexture()!=fake_texture) fake_texture->Release(); + if (((OTM_IDirect3DDevice9*)m_D3Ddev)->GetSingleTexture()!=fake_texture) fake_texture->Release(); // we release the fake texture } } else @@ -62,9 +67,13 @@ ULONG APIENTRY OTM_IDirect3DTexture9::Release() } } - if (count==0) + if (count==0) //if this texture is released, we clean up { - ((OTM_IDirect3DDevice9*) m_D3Ddev)-> GetOTM_Client()->RemoveTexture(this); //this is also valid for textures, which are released before we have add them + // remove this texture from the texture client + ((OTM_IDirect3DDevice9*) m_D3Ddev)->GetOTM_Client()->RemoveTexture(this); //this is also valid for textures, which are released before we have add them + + // if this texture is the LastCreatedTexture, the next time LastCreatedTexture would be added, + // the hash of a non existing texture would be calculated if (((OTM_IDirect3DDevice9*)m_D3Ddev)->GetLastCreatedTexture()==this) ((OTM_IDirect3DDevice9*)m_D3Ddev)->SetLastCreatedTexture( NULL); delete(this); } @@ -77,18 +86,21 @@ HRESULT APIENTRY OTM_IDirect3DTexture9::GetDevice(IDirect3DDevice9** ppDevice) return D3D_OK; } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::SetPrivateData(REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->SetPrivateData(refguid, pData, SizeOfData, Flags)); return (m_D3Dtex->SetPrivateData(refguid, pData, SizeOfData, Flags)); } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::GetPrivateData(REFGUID refguid,void* pData,DWORD* pSizeOfData) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->GetPrivateData(refguid, pData, pSizeOfData)); return (m_D3Dtex->GetPrivateData(refguid, pData, pSizeOfData)); } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::FreePrivateData(REFGUID refguid) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->FreePrivateData(refguid)); @@ -145,30 +157,35 @@ void APIENTRY OTM_IDirect3DTexture9::GenerateMipSubLevels() m_D3Dtex->GenerateMipSubLevels(); } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::GetLevelDesc(UINT Level,D3DSURFACE_DESC *pDesc) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->GetLevelDesc(Level, pDesc)); return (m_D3Dtex->GetLevelDesc(Level, pDesc)); } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::GetSurfaceLevel(UINT Level,IDirect3DSurface9** ppSurfaceLevel) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->GetSurfaceLevel(Level, ppSurfaceLevel)); return (m_D3Dtex->GetSurfaceLevel(Level, ppSurfaceLevel)); } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::LockRect(UINT Level,D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->LockRect(Level, pLockedRect, pRect, Flags)); return (m_D3Dtex->LockRect(Level, pLockedRect, pRect, Flags)); } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::UnlockRect(UINT Level) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->UnlockRect(Level)); return (m_D3Dtex->UnlockRect(Level)); } +//this function yields for the non switched texture object HRESULT APIENTRY OTM_IDirect3DTexture9::AddDirtyRect(CONST RECT* pDirtyRect) { if (CrossRef_D3Dtex!=NULL) return (CrossRef_D3Dtex->m_D3Dtex->AddDirtyRect(pDirtyRect)); diff --git a/OTM_DX9/OTM_IDirect3DTexture9.h b/OTM_DX9/OTM_IDirect3DTexture9.h index 7492942..215fadb 100644 --- a/OTM_DX9/OTM_IDirect3DTexture9.h +++ b/OTM_DX9/OTM_IDirect3DTexture9.h @@ -39,12 +39,12 @@ interface OTM_IDirect3DTexture9 : public IDirect3DTexture9 { OTM_IDirect3DTexture9(IDirect3DTexture9 **ppTex, IDirect3DDevice9 *pIDirect3DDevice9, UINT Width, UINT Height, D3DFORMAT Format) { - //Message("OTM_IDirect3DTexture9( %lu, %lu): %lu\n", ppTex, pIDirect3DDevice9, this); m_D3Dtex = *ppTex; //Texture which will be displayed and will be passed to the game m_D3Ddev = pIDirect3DDevice9; //device pointer CrossRef_D3Dtex = NULL; //cross reference - // fake texture: store the pointer to the original OTM_IDirect3DTexture9 object, needed if a fake texture is deselected - // original texture: stores the pointer to the fake texture object, is needed if original texture is deleted (that fake texture can be replaced, if original texture is loaded again) + // fake texture: store the pointer to the original OTM_IDirect3DTexture9 object, needed if a fake texture is unselected + // original texture: stores the pointer to the fake texture object, is needed if original texture is deleted, + // thus the fake texture can also be deleted Reference = -1; //need for fast deleting Hash = 0u; FAKE = false; @@ -90,7 +90,7 @@ inline void UnswitchTextures(OTM_IDirect3DTexture9 *pTexture) OTM_IDirect3DTexture9* CrossRef = pTexture->CrossRef_D3Dtex; if (CrossRef!=NULL) { - //switch textures back + // switch textures back IDirect3DTexture9* cpy = pTexture->m_D3Dtex; pTexture->m_D3Dtex = CrossRef->m_D3Dtex; CrossRef->m_D3Dtex = cpy; @@ -105,11 +105,11 @@ inline int SwitchTextures( OTM_IDirect3DTexture9 *pTexture1, OTM_IDirect3DTextur { if (pTexture1->m_D3Ddev == pTexture2->m_D3Ddev && pTexture1->CrossRef_D3Dtex == NULL && pTexture2->CrossRef_D3Dtex == NULL) { - //make cross reference + // make cross reference pTexture1->CrossRef_D3Dtex = pTexture2; pTexture2->CrossRef_D3Dtex = pTexture1; - //switch textures + // switch textures IDirect3DTexture9* cpy = pTexture2->m_D3Dtex; pTexture2->m_D3Dtex = pTexture1->m_D3Dtex; pTexture1->m_D3Dtex = cpy; diff --git a/OTM_DX9/OTM_Main.h b/OTM_DX9/OTM_Main.h index 63ceab5..cf3a75f 100644 --- a/OTM_DX9/OTM_Main.h +++ b/OTM_DX9/OTM_Main.h @@ -51,12 +51,6 @@ along with OpenTexMod. If not, see . #include "OTM_TextureClient.h" - -extern OTM_IDirect3DDevice9 *gl_pIDirect3DDevice9; -extern OTM_IDirect3D9 *gl_pIDirect3D9; -extern HINSTANCE gl_hOriginalDll; -extern HINSTANCE gl_hThisInstance; -extern unsigned int gl_ErrorState; -extern FILE *gl_File; +extern unsigned int gl_ErrorState; #endif diff --git a/OTM_DX9/OTM_TextureClient.cpp b/OTM_DX9/OTM_TextureClient.cpp index db50ab2..e7ae10e 100644 --- a/OTM_DX9/OTM_TextureClient.cpp +++ b/OTM_DX9/OTM_TextureClient.cpp @@ -20,10 +20,6 @@ along with OpenTexMod. If not, see . #include "OTM_Main.h" -FILE *file; - - - OTM_TextureClient::OTM_TextureClient(OTM_TextureServer* server, IDirect3DDevice9* device) { @@ -46,13 +42,12 @@ OTM_TextureClient::OTM_TextureClient(OTM_TextureServer* server, IDirect3DDevice9 FileToMod = NULL; } } - Mutex = NULL;//CreateMutex(NULL, false, NULL); + Mutex = CreateMutex(NULL, false, NULL); Update = NULL; NumberOfUpdate = -1; FontColour = D3DCOLOR_ARGB(255,255,0,0); TextureColour = D3DCOLOR_ARGB(255,0,255,0); - //Message("end OTM_TextureClient(void): %lu\n", this); } OTM_TextureClient::~OTM_TextureClient(void) @@ -60,39 +55,34 @@ OTM_TextureClient::~OTM_TextureClient(void) Message("~OTM_TextureClient(void): %lu\n", this); if (Server!=NULL) Server->RemoveClient(this); - //Message("after Server ~OTM_TextureClient(void): %lu\n", this); - if (Mutex!=NULL) CloseHandle(Mutex); - //Message("after CloseHandle ~OTM_TextureClient(void): %lu\n", this); if (Update!=NULL) delete [] Update; - //Message("after Update ~OTM_TextureClient(void): %lu\n", this); - if (FileToMod!=NULL) delete [] FileToMod; - //Message("end ~OTM_TextureClient(void): %lu\n", this); } int OTM_TextureClient::AddTexture( OTM_IDirect3DTexture9* pTexture) { - ((OTM_IDirect3DDevice9*)D3D9Device)->SetLastCreatedTexture(NULL); //a loop would arise if a texture would be switched - if (pTexture->FAKE) return (RETURN_OK); // this is a fake texture + ((OTM_IDirect3DDevice9*)D3D9Device)->SetLastCreatedTexture(NULL); //this texture must no be added twice + if (pTexture->FAKE) return (RETURN_OK); // this is a fake texture + Message("OTM_TextureClient::AddTexture( %lu): %lu (thread: %lu)\n", pTexture, this, GetCurrentThread()); D3DLOCKED_RECT d3dlr; - if (pTexture->LockRect( 0, &d3dlr, NULL, 0)!=D3D_OK) + if (pTexture->LockRect( 0, &d3dlr, NULL, 0)!=D3D_OK) //get the raw data of the texture { return (RETURN_LockRect_FAILED); } D3DSURFACE_DESC desc; - if (pTexture->GetLevelDesc(0, &desc)!=D3D_OK) + if (pTexture->GetLevelDesc(0, &desc)!=D3D_OK) //get the format and the size of the texture { pTexture->UnlockRect(0); return (RETURN_GetLevelDesc_FAILED); } int size=0; - switch(desc.Format) + switch(desc.Format) //switch trough the formats to calculate the size of the raw data { case D3DFMT_A1: // 1-bit monochrome. { @@ -170,20 +160,21 @@ int OTM_TextureClient::AddTexture( OTM_IDirect3DTexture9* pTexture) break; } } - //MyTypeHash hash = GetHash( (unsigned char*) d3dlr.pBits, size); - MyTypeHash hash = GetCRC32( (char*) d3dlr.pBits, size); - if (pTexture->UnlockRect(0)!=D3D_OK) + //MyTypeHash hash = GetHash( (unsigned char*) d3dlr.pBits, size); + MyTypeHash hash = GetCRC32( (char*) d3dlr.pBits, size); //calaculate the crc32 of the texture + + if (pTexture->UnlockRect(0)!=D3D_OK) //unlock the raw data { return (RETURN_UnlockRect_FAILED); } - pTexture->Hash = hash; // note: this will only be done for original textures + pTexture->Hash = hash; if (BoolSaveAllTextures) SaveTexture(pTexture); if (gl_ErrorState & OTM_ERROR_FATAL) return (RETURN_FATAL_ERROR); - OriginalTextures.Add( pTexture); + OriginalTextures.Add( pTexture); // add the texture to the list of original texture return (LookUpToMod(pTexture)); // check if this texture should be modded } @@ -197,11 +188,14 @@ int OTM_TextureClient::RemoveTexture( OTM_IDirect3DTexture9* pTexture) // is cal IDirect3DDevice9 *dev = NULL; if (pTexture != NULL && pTexture->GetDevice(&dev) == D3D_OK) { + // this condition is senseless, since this function is only called if a OTM_IDirect3DTexture9 object is released + // and OTM_IDirect3DTexture9 object are created only from a OTM_IDirect3DDevice9 object, hence the pointers must be equal if (dev == D3D9Device) { if (gl_ErrorState & OTM_ERROR_FATAL) return (RETURN_FATAL_ERROR); if (pTexture->FAKE) { + // we need to set the corresponding FileToMod[X].pTexture to NULL, to avoid a link to a non existing texture object int ref = pTexture->Reference; if (ref>=0 && refRelease(); //this will call this->RemoveTexture again and the fake_texture will also be deleted from the FileToMod. } */ - return (OriginalTextures.Remove( pTexture)); + return (OriginalTextures.Remove( pTexture)); //remove this texture form the list } } } return (RETURN_OK); } -/* -int OTM_TextureClient::ReleaseAllFakeTexture(void) -{ - Message("ReleaseAllFakeTexture(): %lu\n", this); - return (FakeTextures.RemoveAll()); -} -*/ int OTM_TextureClient::SaveAllTextures(bool val) { @@ -240,13 +227,10 @@ int OTM_TextureClient::SaveAllTextures(bool val) int OTM_TextureClient::SaveSingleTexture(bool val) { Message("OTM_TextureClient::SaveSingleTexture( %d): %lu\n", val, this); - if (BoolSaveSingleTexture && !val) + if (BoolSaveSingleTexture && !val) //if BoolSaveSingleTexture is set to false and was previously true we switch the SingleTexture back { - if (D3D9Device!=NULL) - { - OTM_IDirect3DTexture9* pTexture = ((OTM_IDirect3DDevice9*)D3D9Device)->GetSingleTexture(); - if (pTexture!=NULL) UnswitchTextures(pTexture); - } + OTM_IDirect3DTexture9* pTexture = ((OTM_IDirect3DDevice9*)D3D9Device)->GetSingleTexture(); + if (pTexture!=NULL) UnswitchTextures(pTexture); } BoolSaveSingleTexture = val; return (RETURN_OK); @@ -263,7 +247,7 @@ int OTM_TextureClient::SetSaveDirectory( wchar_t *dir) SavePath[0]=0; return (RETURN_BAD_ARGUMENT); } - SavePath[i]=0; + else SavePath[i]=0; return (RETURN_OK); } @@ -277,7 +261,7 @@ int OTM_TextureClient::SetGameName( wchar_t *name) GameName[0]=0; return (RETURN_BAD_ARGUMENT); } - GameName[i]=0; + else GameName[i]=0; return (RETURN_OK); } @@ -285,25 +269,21 @@ int OTM_TextureClient::SaveTexture(OTM_IDirect3DTexture9* pTexture) { if (pTexture==NULL) return (RETURN_BAD_ARGUMENT); if (SavePath[0]==0) {Message("OTM_TextureClient::SaveTexture( %#lX, %lu): %lu, SavePath not set\n", pTexture->Hash, pTexture->m_D3Dtex, this); return (RETURN_TEXTURE_NOT_SAVED);} - Message("OTM_TextureClient::SaveTexture( %#lX, %lu): %lu\n", pTexture->Hash, pTexture->m_D3Dtex, this); wchar_t file[MAX_PATH]; if (GameName[0]) swprintf_s( file, MAX_PATH, L"%ls\\%ls_%#lX.dds", SavePath, GameName, pTexture->Hash); else swprintf_s( file, MAX_PATH, L"%ls\\%#lX.dds", SavePath, pTexture->Hash); - Message("OTM_TextureClient::SaveTexture( %ls ) \n", file); + Message("OTM_TextureClient::SaveTexture( %ls): %lu\n", file, this); if (D3D_OK!=D3DXSaveTextureToFileW( file, D3DXIFF_DDS, pTexture->m_D3Dtex, NULL)) return (RETURN_TEXTURE_NOT_SAVED); - Message("OTM_TextureClient::SaveTexture( ) Done!\n", file); - return (RETURN_OK); } -int OTM_TextureClient::AddUpdate(TextureFileStruct* update, int number) //client must delete the temp array +int OTM_TextureClient::AddUpdate(TextureFileStruct* update, int number) //client must delete the update array { Message("AddUpdate( %lu, %d): %lu\n", update, number, this); - //if (update==NULL || number<=0) return (RETURN_OK); if (int ret = LockMutex()) {gl_ErrorState |= OTM_ERROR_TEXTURE; return (ret);} if (Update!=NULL) delete [] Update; Update = update; @@ -315,65 +295,74 @@ int OTM_TextureClient::AddUpdate(TextureFileStruct* update, int number) //clien int OTM_TextureClient::MergeUpdate(void) { - if (NumberOfUpdate<0) {Update=NULL; return (RETURN_OK);} + if (NumberOfUpdate<0) {return (RETURN_OK);} if (int ret = LockMutex()) {gl_ErrorState |= OTM_ERROR_TEXTURE ; return (ret);} Message("MergeUpdate(): %lu\n", this); - for (int i=0; iCrossRef_D3Dtex; - Update[u].pTexture->Release(); - Update[u].pTexture = NULL; - if (pTexture!=NULL) //should always be the case + if (Update[u].ForceReload) // if force is enable, we reload the texture { - OTM_IDirect3DTexture9 *fake_Texture; - if (int ret = LoadTexture( & (Update[u]), &fake_Texture)) return (ret); - if (SwitchTextures( fake_Texture, pTexture)) + OTM_IDirect3DTexture9 *pTexture = Update[u].pTexture->CrossRef_D3Dtex; + Update[u].pTexture->Release(); //release the old fake texture + Update[u].pTexture = NULL; + if (pTexture!=NULL) //should always be the case { - Message("MergeUpdate(): textures not switched %#lX\n", hash); - fake_Texture->Release(); - } - else - { - //FakeTextures.Add(fake_Texture); - Update[u].pTexture = fake_Texture; - fake_Texture->Reference = u; + OTM_IDirect3DTexture9 *fake_Texture; + if (int ret = LoadTexture( & (Update[u]), &fake_Texture)) return (ret); + if (SwitchTextures( fake_Texture, pTexture)) + { + Message("MergeUpdate(): textures not switched %#lX\n", hash); + fake_Texture->Release(); + } + else + { + Update[u].pTexture = fake_Texture; + fake_Texture->Reference = u; + } } } + else Update[u].pTexture->Reference = u; //set the new reference, needed for a fast delete } break; } } if (!found) { - if (FileToMod[i].pTexture!=NULL) FileToMod[i].pTexture->Release(); + if (FileToMod[i].pTexture!=NULL) FileToMod[i].pTexture->Release(); //release the fake texture, if it is not included in the update } } - // looking through the update and switch the textures if they are currently used in the game + // looking through the non switched texture in the update and search for a target hash in the list of original game textures int num = OriginalTextures.GetNumber(); - for (int u=0; uHash && OriginalTextures[i]->CrossRef_D3Dtex==NULL) + for (int i=0; iHash) { - Update[u].Checked = true; + if (OriginalTextures[i]->CrossRef_D3Dtex!=NULL) + { + //if the texture is already switched, it might be the SingleTexture (for saving a single texture) + if (OriginalTextures[i]->CrossRef_D3Dtex==((OTM_IDirect3DDevice9*)D3D9Device)->GetSingleTexture()) UnswitchTextures(OriginalTextures[i]); + else continue; //bug, this might happen if a hash is twice in the update list or due to an other bug^^ + } OTM_IDirect3DTexture9 *pTexture = OriginalTextures[i]; OTM_IDirect3DTexture9 *fake_Texture; if (RETURN_OK == LoadTexture( & (Update[u]), &fake_Texture)) @@ -387,9 +376,10 @@ int OTM_TextureClient::MergeUpdate(void) { Update[u].pTexture = fake_Texture; fake_Texture->Reference = u; - //FakeTextures.Add(fake_Texture); } } + + break; } } @@ -406,14 +396,14 @@ int OTM_TextureClient::MergeUpdate(void) int OTM_TextureClient::LockMutex(void) { - //if (( gl_ErrorState & (OTM_ERROR_FATAL | OTM_ERROR_MUTEX) )) return (RETURN_NO_MUTEX); - //if (WAIT_OBJECT_0!=WaitForSingleObject( Mutex, 100)) return (RETURN_MUTEX_LOCK); //waiting 100ms, to wait infinite pass INFINITE + if (( gl_ErrorState & (OTM_ERROR_FATAL | OTM_ERROR_MUTEX) )) return (RETURN_NO_MUTEX); + if (WAIT_OBJECT_0!=WaitForSingleObject( Mutex, 100)) return (RETURN_MUTEX_LOCK); //waiting 100ms, to wait infinite pass INFINITE return (RETURN_OK); } int OTM_TextureClient::UnlockMutex(void) { - //if (ReleaseMutex(Mutex)==0) return (RETURN_MUTEX_UNLOCK); + if (ReleaseMutex(Mutex)==0) return (RETURN_MUTEX_UNLOCK); return (RETURN_OK); } @@ -424,12 +414,13 @@ int OTM_TextureClient::UnlockMutex(void) int OTM_TextureClient::LookUpToMod( OTM_IDirect3DTexture9* pTexture) // should only be called for original textures { Message("OTM_TextureClient::LookUpToMod( %lu): hash: %#lX, %lu\n", pTexture, pTexture->Hash, this); - if (pTexture->CrossRef_D3Dtex!=NULL) return (RETURN_OK); // to avoid broken links + if (pTexture->CrossRef_D3Dtex!=NULL) return (RETURN_OK); // bug, this texture is already switched + MyTypeHash hash = pTexture->Hash; for (int i=0; iReference = i; } @@ -449,28 +439,29 @@ int OTM_TextureClient::LookUpToMod( OTM_IDirect3DTexture9* pTexture) // should o } -int OTM_TextureClient::LoadTexture( TextureFileStruct* file_in_memory, OTM_IDirect3DTexture9 **ppTexture) // to load fake texture from file in memory +int OTM_TextureClient::LoadTexture( TextureFileStruct* file_in_memory, OTM_IDirect3DTexture9 **ppTexture) // to load fake texture from a file in memory { Message("LoadTexture( %lu, %lu): %lu\n", file_in_memory, ppTexture, this); - if (D3D_OK != D3DXCreateTextureFromFileInMemory( D3D9Device, file_in_memory->pData, file_in_memory->Size, (IDirect3DTexture9 **) ppTexture)) { *ppTexture=NULL; return (RETURN_TEXTURE_NOT_LOADED); } (*ppTexture)->FAKE = true; + ((OTM_IDirect3DDevice9*)D3D9Device)->SetLastCreatedTexture(NULL); //this texture is a fake texture and must not be added Message("LoadTexture(): DONE\n", file_in_memory, ppTexture, this); return (RETURN_OK); } +/* MyTypeHash OTM_TextureClient::GetHash(unsigned char *str, int len) // estimate the hash { MyTypeHash hash = 0; for (int i=0; i. class OTM_TextureServer; - +/* + * An object of this class is owned by each d3d9 device. + * functions called by the Server are called from the server thread instance. + * All other functions are called from the render thread instance of the game itself. + */ class OTM_TextureClient { @@ -36,34 +40,31 @@ public: OTM_TextureClient(OTM_TextureServer* server, IDirect3DDevice9* device); ~OTM_TextureClient(void); - int AddTexture( OTM_IDirect3DTexture9* tex); //called from OTM_IDirect3DDevice9::CreateTexture(...) + int AddTexture( OTM_IDirect3DTexture9* tex); //called from OTM_IDirect3DDevice9::CreateTexture(...) or OTM_IDirect3DDevice9::BeginScene() int RemoveTexture( OTM_IDirect3DTexture9* tex); //called from OTM_IDirect3DTexture9::Release() - //int ReleaseAllFakeTexture(void); // called when the Game is closed - int SaveAllTextures(bool val); //called from the Server int SaveSingleTexture(bool val); //called from the Server int SetSaveDirectory( wchar_t *dir); //called from the Server int SetGameName( wchar_t *dir); //called from the Server - int SaveTexture(OTM_IDirect3DTexture9* pTexture); + int SaveTexture(OTM_IDirect3DTexture9* pTexture); //called from OTM_IDirect3DDevice9::BeginScene() (save button) or from AddTexture(...) (SaveAllTextures) - int SetKeyBack( int key) {if (key>0) KeyBack = key; return (RETURN_OK);} - int SetKeySave( int key) {if (key>0) KeySave = key; return (RETURN_OK);} - int SetKeyNext( int key) {if (key>0) KeyNext = key; return (RETURN_OK);} + int SetKeyBack( int key) {if (key>0) KeyBack = key; return (RETURN_OK);} //called from the Server + int SetKeySave( int key) {if (key>0) KeySave = key; return (RETURN_OK);} //called from the Server + int SetKeyNext( int key) {if (key>0) KeyNext = key; return (RETURN_OK);} //called from the Server - int SetFontColour( DWORD r, DWORD g, DWORD b) {FontColour = D3DCOLOR_ARGB(255, r,g,b); return (RETURN_OK);} - int SetTextureColour( DWORD r, DWORD g, DWORD b) {TextureColour = D3DCOLOR_ARGB(255, r,g,b); return (RETURN_OK);} + int SetFontColour( DWORD r, DWORD g, DWORD b) {FontColour = D3DCOLOR_ARGB(255, r,g,b); return (RETURN_OK);} //called from the Server + int SetTextureColour( DWORD r, DWORD g, DWORD b) {TextureColour = D3DCOLOR_ARGB(255, r,g,b); return (RETURN_OK);} //called from the Server - int AddUpdate(TextureFileStruct* update, int number); //client must delete the temp array - int MergeUpdate(void); + int AddUpdate(TextureFileStruct* update, int number); //called from the Server, client object must delete update array + int MergeUpdate(void); //called from OTM_IDirect3DDevice9::BeginScene() - //OTM_TextureHandler FakeTextures; - OTM_TextureHandler OriginalTextures; + OTM_TextureHandler OriginalTextures; // stores the pointer to the OTM_IDirect3DTexture9 objects created by the game bool BoolSaveAllTextures; bool BoolSaveSingleTexture; int KeyBack; @@ -86,15 +87,16 @@ private: int UnlockMutex(); HANDLE Mutex; - int NumberToMod; - TextureFileStruct* FileToMod; + int NumberToMod; // number of texture to be modded + TextureFileStruct* FileToMod; // array which stores the file in memory and the hash of each texture to be modded - int LookUpToMod( OTM_IDirect3DTexture9* pTexture); - int LoadTexture( TextureFileStruct* file_in_memory, OTM_IDirect3DTexture9 **ppTexture); - MyTypeHash GetHash(unsigned char *str, int len); + int LookUpToMod( OTM_IDirect3DTexture9* pTexture); // called at the end AddTexture(...) + int LoadTexture( TextureFileStruct* file_in_memory, OTM_IDirect3DTexture9 **ppTexture); // called if a target texture is found + // and the corresponding fake texture should be loaded + + //MyTypeHash GetHash(unsigned char *str, int len); unsigned int GetCRC32(char *pcDatabuf, unsigned int ulDatalen); - }; diff --git a/OTM_DX9/OTM_TextureServer.cpp b/OTM_DX9/OTM_TextureServer.cpp index baced4f..1a62edf 100644 --- a/OTM_DX9/OTM_TextureServer.cpp +++ b/OTM_DX9/OTM_TextureServer.cpp @@ -25,6 +25,7 @@ OTM_TextureServer::OTM_TextureServer(wchar_t *game) Message("OTM_TextureServer(void): %lu\n", this); Message("sizeof(unsigned long)=%lu\n", sizeof(unsigned long)); Message("sizeof(DWORDLONG)=%lu\n", sizeof(DWORDLONG)); + Mutex = CreateMutex(NULL, false, NULL); Clients = NULL; @@ -69,10 +70,10 @@ OTM_TextureServer::~OTM_TextureServer(void) //delete the files in memory int num = CurrentMod.GetNumber(); - for (int i = 0; i < num; i++) delete[] CurrentMod[i]->pData; + for (int i = 0; i < num; i++) delete[] CurrentMod[i]->pData; //delete the file content of the texture num = OldMod.GetNumber(); - for (int i = 0; i < num; i++) delete[] OldMod[i]->pData; + for (int i = 0; i < num; i++) delete[] OldMod[i]->pData; //delete the file content of the texture if (Pipe.In != INVALID_HANDLE_VALUE ) CloseHandle(Pipe.In); Pipe.In = INVALID_HANDLE_VALUE; @@ -91,7 +92,7 @@ int OTM_TextureServer::AddClient(OTM_TextureClient *client, TextureFileStruct** // the following functions must not change the original OTM_IDirect3DDevice9 object // somehow on game start some OTM_IDirect3DDevice9 object are created, which must rest unchanged!! - // these objects are released and are not uses for rendering + // these objects are released and are not used for rendering client->SetGameName(GameName); client->SaveAllTextures(BoolSaveAllTextures); client->SaveSingleTexture(BoolSaveSingleTexture); @@ -116,12 +117,12 @@ int OTM_TextureServer::AddClient(OTM_TextureClient *client, TextureFileStruct** } - if (int ret = PrepareUpdate( update, number)) return (ret); + if (int ret = PrepareUpdate( update, number)) return (ret); // get a copy of all texture to be modded - OTM_TextureClient** temp = NULL; - if (NumberOfClients == LenghtOfClients) + if (NumberOfClients == LenghtOfClients) //allocate more memory { + OTM_TextureClient** temp = NULL; try {temp = new OTM_TextureClient*[LenghtOfClients + 10];} catch (...) { @@ -157,19 +158,19 @@ int OTM_TextureServer::RemoveClient(OTM_TextureClient *client) // called from a return (UnlockMutex()); } -int OTM_TextureServer::AddFile( char* buffer, unsigned int size, MyTypeHash hash, bool force) // called from the server +int OTM_TextureServer::AddFile( char* buffer, unsigned int size, MyTypeHash hash, bool force) // called from Mainloop() { Message("OTM_TextureServer::AddFile( %lu %lu, %#lX, %d): %lu\n", buffer, size, hash, force, this); TextureFileStruct* temp = NULL; int num = CurrentMod.GetNumber(); - for (int i = 0; i < num; i++) if (CurrentMod[i]->Hash == hash) + for (int i = 0; iHash == hash) //look through all current textures { - if (force) {temp = CurrentMod[i]; break;} - else return (RETURN_OK); + if (force) {temp = CurrentMod[i]; break;} // we need to reload it + else return (RETURN_OK); // we still have added this texture } - if (temp==NULL) + if (temp==NULL) // if not found, look through all old textures { num = OldMod.GetNumber(); for (int i = 0; i < num; i++) if (OldMod[i]->Hash == hash) @@ -177,19 +178,19 @@ int OTM_TextureServer::AddFile( char* buffer, unsigned int size, MyTypeHash has temp = OldMod[i]; OldMod.Remove(temp); CurrentMod.Add(temp); - if (force) break; - else return (RETURN_OK); + if (force) break; // we must reload it + else return (RETURN_OK); // we should not reload it } } bool new_file = true; - if (temp!=NULL) + if (temp!=NULL) //if it was found, we delete the old file content { new_file = false; if (temp->pData!=NULL) delete [] temp->pData; temp->pData = NULL; } - else + else //if it was not found, we need to create a new object { new_file = true; temp = new TextureFileStruct; @@ -202,7 +203,7 @@ int OTM_TextureServer::AddFile( char* buffer, unsigned int size, MyTypeHash has } catch (...) { - if (!new_file) CurrentMod.Remove( temp); + if (!new_file) CurrentMod.Remove( temp); // if this is a not a new file it is in the list of the CurrentMod delete temp; gl_ErrorState |= OTM_ERROR_MEMORY | OTM_ERROR_SERVER; return (RETURN_NO_MEMORY); @@ -210,20 +211,20 @@ int OTM_TextureServer::AddFile( char* buffer, unsigned int size, MyTypeHash has for (unsigned int i=0; ipData[i] = buffer[i]; - temp->Checked = false; temp->Size = size; temp->pTexture = NULL; temp->Hash = hash; - if (new_file) temp->ForceReload = false; + if (new_file) temp->ForceReload = false; // no need to force a load of the texture else temp->ForceReload = force; Message("End AddFile(%#lX)\n", hash); - if (new_file) return (CurrentMod.Add(temp)); + if (new_file) return (CurrentMod.Add(temp)); // new files must be added to the list of the CurrentMod else return (RETURN_OK); } -int OTM_TextureServer::AddFile(wchar_t* file_name, MyTypeHash hash, bool force) // called from the server +int OTM_TextureServer::AddFile(wchar_t* file_name, MyTypeHash hash, bool force) // called from Mainloop +// this functions does the same, but loads the file content from disk { Message("OTM_TextureServer::AddFile( %ls, %#lX, %d): %lu\n", file_name, hash, force, this); @@ -284,19 +285,16 @@ int OTM_TextureServer::AddFile(wchar_t* file_name, MyTypeHash hash, bool force) gl_ErrorState |= OTM_ERROR_MEMORY | OTM_ERROR_SERVER; return (RETURN_NO_MEMORY); } - //Message("Read AddFile( %lu)\n", hash, this); int result = fread(temp->pData, 1, size, file); fclose(file); if (result != size) { - //Message("Read Failed AddFile( %lu): %lu\n", hash, this); delete[] temp->pData; if (!new_file) CurrentMod.Remove( temp); delete temp; return (RETURN_FILE_NOT_LOADED); } - temp->Checked = false; temp->Size = size; temp->pTexture = NULL; temp->Hash = hash; @@ -309,7 +307,7 @@ int OTM_TextureServer::AddFile(wchar_t* file_name, MyTypeHash hash, bool force) else return (RETURN_OK); } -int OTM_TextureServer::RemoveFile(MyTypeHash hash) // called from the server +int OTM_TextureServer::RemoveFile(MyTypeHash hash) // called from Mainloop() { Message("RemoveFile( %lu): %lu\n", hash, this); @@ -323,7 +321,7 @@ int OTM_TextureServer::RemoveFile(MyTypeHash hash) // called from the server return (RETURN_OK); } -int OTM_TextureServer::SaveAllTextures(bool val) // called from the server +int OTM_TextureServer::SaveAllTextures(bool val) // called from Mainloop() { if (BoolSaveAllTextures == val) return (RETURN_OK); BoolSaveAllTextures = val; @@ -340,7 +338,7 @@ int OTM_TextureServer::SaveAllTextures(bool val) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::SaveSingleTexture(bool val) // called from the server +int OTM_TextureServer::SaveSingleTexture(bool val) // called from Mainloop() { if (BoolSaveSingleTexture == val) return (RETURN_OK); BoolSaveSingleTexture = val; @@ -357,7 +355,7 @@ int OTM_TextureServer::SaveSingleTexture(bool val) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::SetSaveDirectory(wchar_t *dir) // called from the server +int OTM_TextureServer::SetSaveDirectory(wchar_t *dir) // called from Mainloop() { Message("OTM_TextureServer::SetSaveDirectory( %ls): %lu\n", dir, this); int i = 0; @@ -367,7 +365,7 @@ int OTM_TextureServer::SetSaveDirectory(wchar_t *dir) // called from the server SavePath[0] = 0; return (RETURN_BAD_ARGUMENT); } - SavePath[i] = 0; + else SavePath[i] = 0; if (int ret = LockMutex()) { @@ -381,7 +379,7 @@ int OTM_TextureServer::SetSaveDirectory(wchar_t *dir) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::SetKeyBack(int key) // called from the server +int OTM_TextureServer::SetKeyBack(int key) // called from Mainloop() { if (KeyBack == key || KeySave == key || KeyNext == key) return (RETURN_OK); if (int ret = LockMutex()) @@ -397,7 +395,7 @@ int OTM_TextureServer::SetKeyBack(int key) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::SetKeySave(int key) // called from the server +int OTM_TextureServer::SetKeySave(int key) // called from Mainloop() { if (KeyBack == key || KeySave == key || KeyNext == key) return (RETURN_OK); if (int ret = LockMutex()) @@ -413,7 +411,7 @@ int OTM_TextureServer::SetKeySave(int key) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::SetKeyNext(int key) // called from the server +int OTM_TextureServer::SetKeyNext(int key) // called from Mainloop() { if (KeyBack == key || KeySave == key || KeyNext == key) return (RETURN_OK); if (int ret = LockMutex()) @@ -429,7 +427,7 @@ int OTM_TextureServer::SetKeyNext(int key) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::SetFontColour(DWORD colour) // called from the server +int OTM_TextureServer::SetFontColour(DWORD colour) // called from Mainloop() { if (colour==0u) return (RETURN_OK); if (int ret = LockMutex()) @@ -449,7 +447,7 @@ int OTM_TextureServer::SetFontColour(DWORD colour) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::SetTextureColour(DWORD colour) // called from the server +int OTM_TextureServer::SetTextureColour(DWORD colour) // called from Mainloop() { if (colour==0u) return (RETURN_OK); if (int ret = LockMutex()) @@ -469,7 +467,7 @@ int OTM_TextureServer::SetTextureColour(DWORD colour) // called from the server return (UnlockMutex()); } -int OTM_TextureServer::PropagateUpdate(OTM_TextureClient* client) // called from the server, send the update to all clients +int OTM_TextureServer::PropagateUpdate(OTM_TextureClient* client) // called from Mainloop(), send the update to all clients { Message("PropagateUpdate(%lu): %lu\n", client, this); if (int ret = LockMutex()) @@ -498,7 +496,7 @@ int OTM_TextureServer::PropagateUpdate(OTM_TextureClient* client) // called from } #define cpy_file_struct( a, b) \ -{ a.Checked = b.Checked; \ +{ \ a.ForceReload = b.ForceReload; \ a.pData = b.pData; \ a.Size = b.Size; \ @@ -506,7 +504,8 @@ int OTM_TextureServer::PropagateUpdate(OTM_TextureClient* client) // called from a.pTexture = b.pTexture; \ a.Hash = b.Hash; } -int OTM_TextureServer::PrepareUpdate(TextureFileStruct** update, int* number) // called from the server, prepare an update for one client. the allocated memory must deleted by the client +int OTM_TextureServer::PrepareUpdate(TextureFileStruct** update, int* number) // called from the PropagateUpdate() and AddClient. +// Prepare an update for one client. The allocated memory must deleted by the client. { Message("PrepareUpdate(%lu, %d): %lu\n", update, number, this); TextureFileStruct* temp = NULL; @@ -532,18 +531,18 @@ int OTM_TextureServer::PrepareUpdate(TextureFileStruct** update, int* number) // int OTM_TextureServer::LockMutex(void) { - //if (( gl_ErrorState & (OTM_ERROR_FATAL | OTM_ERROR_MUTEX) )) return (RETURN_NO_MUTEX); - //if (WAIT_OBJECT_0!=WaitForSingleObject( Mutex, 100)) return (RETURN_MUTEX_LOCK); //waiting 100ms, to wait infinite pass INFINITE + if (( gl_ErrorState & (OTM_ERROR_FATAL | OTM_ERROR_MUTEX) )) return (RETURN_NO_MUTEX); + if (WAIT_OBJECT_0!=WaitForSingleObject( Mutex, 100)) return (RETURN_MUTEX_LOCK); //waiting 100ms, to wait infinite pass INFINITE return (RETURN_OK); } int OTM_TextureServer::UnlockMutex(void) { - //if (ReleaseMutex(Mutex)==0) return (RETURN_MUTEX_UNLOCK); + if (ReleaseMutex(Mutex)==0) return (RETURN_MUTEX_UNLOCK); return (RETURN_OK); } -int OTM_TextureServer::MainLoop(void) // run as a separated thread !! +int OTM_TextureServer::MainLoop(void) // run as a separated thread { Message("MainLoop: begin\n"); if (Pipe.In == INVALID_HANDLE_VALUE) return (RETURN_PIPE_NOT_OPENED); @@ -564,12 +563,10 @@ int OTM_TextureServer::MainLoop(void) // run as a separated thread !! NULL); // not overlapped Message("MainLoop: read something (%lu)\n", num); - //for (int i=0; i. #include "OTM_ArrayHandler.h" +/* + * An object of this class is created only once. + * The Mainloop functions is executed by a server thread, + * which listen on a pipe. + * + * Functions called by the Client are called from the a thread instance of the game itself. + * Nearly all other functions are called from the server thread instance. + */ class OTM_TextureClient; @@ -35,32 +43,33 @@ public: OTM_TextureServer(wchar_t *name); ~OTM_TextureServer(void); - int AddClient(OTM_TextureClient *client, TextureFileStruct** update, int* number); - int RemoveClient(OTM_TextureClient *client); + int AddClient(OTM_TextureClient *client, TextureFileStruct** update, int* number); // called from a Client + int RemoveClient(OTM_TextureClient *client); // called from a Client - int OpenPipe(wchar_t *name); - int ClosePipe(void); - int MainLoop(void); + int OpenPipe(wchar_t *name); // called on initialization of our d3d9 fake dll + int ClosePipe(void); // called on exit of our d3d9 fake dll + int MainLoop(void); // is executed in a server thread - //following functions are public for testing purpose !! - int AddFile( char* buffer, unsigned int size, MyTypeHash hash, bool force); - int AddFile( wchar_t* file_name, MyTypeHash hash, bool force); - int RemoveFile( MyTypeHash hash); + // following functions are only public for testing purpose !! + // they should be private and only be called from the Mainloop - int SaveAllTextures(bool val); - int SaveSingleTexture(bool val); + int AddFile( char* buffer, unsigned int size, MyTypeHash hash, bool force); // called from Mainloop(), if the content of the texture is sent + int AddFile( wchar_t* file_name, MyTypeHash hash, bool force); // called from Mainloop(), if the name and the path to the file is sent + int RemoveFile( MyTypeHash hash); // called from Mainloop() - int SetSaveDirectory( wchar_t *dir); - int SaveTexture(OTM_IDirect3DTexture9* pTexture); + int SaveAllTextures(bool val); // called from Mainloop() + int SaveSingleTexture(bool val); // called from Mainloop() + + int SetSaveDirectory( wchar_t *dir); // called from Mainloop() - int SetKeyBack( int key); - int SetKeySave( int key); - int SetKeyNext( int key); + int SetKeyBack( int key); // called from Mainloop() + int SetKeySave( int key); // called from Mainloop() + int SetKeyNext( int key); // called from Mainloop() - int SetFontColour(DWORD colour); - int SetTextureColour(DWORD colour); + int SetFontColour(DWORD colour); // called from Mainloop() + int SetTextureColour(DWORD colour); // called from Mainloop() private: bool BoolSaveAllTextures; @@ -68,11 +77,15 @@ private: wchar_t SavePath[MAX_PATH]; wchar_t GameName[MAX_PATH]; - int PropagateUpdate(OTM_TextureClient* client=NULL); - int PrepareUpdate(TextureFileStruct** update, int* number); + int PropagateUpdate(OTM_TextureClient* client=NULL); // called from Mainloop() if texture are loaded or removed + int PrepareUpdate(TextureFileStruct** update, int* number); // called from PropagateUpdate() and AddClient() + // generate a copy of the current texture to be modded + // the file content of the textures are not copied, the clients get the pointer to the file content + // but the arrays allocate by this function, must be deleted by the client int LockMutex(); int UnlockMutex(); + HANDLE Mutex; int KeyBack; @@ -82,7 +95,6 @@ private: DWORD FontColour; DWORD TextureColour; - HANDLE Mutex; PipeStruct Pipe; @@ -90,8 +102,9 @@ private: int NumberOfClients; int LenghtOfClients; - OTM_FileHandler CurrentMod; - OTM_FileHandler OldMod; + OTM_FileHandler CurrentMod; // hold the file content of texture + OTM_FileHandler OldMod; // hold the file content of texture which were added previously but are not needed any more + // this is needed, because a texture clients might not have merged the last update and thus hold pointers to the file content of old textures }; diff --git a/OTM_GUI/OTM_Language.cpp b/OTM_GUI/OTM_Language.cpp index d96fd0a..c96108a 100644 --- a/OTM_GUI/OTM_Language.cpp +++ b/OTM_GUI/OTM_Language.cpp @@ -284,12 +284,12 @@ int OTM_Language::LoadDefault(void) { CurrentLanguage="English"; - MenuLanguage = "Change Language"; + MenuLanguage = "Change language"; MenuHelp = "Help"; MenuAbout = "About"; MenuAcknowledgement = "Acknowledgement"; MenuAddGame = "Add game"; - MenuDeleteGame = "Delete Game"; + MenuDeleteGame = "Delete game"; MenuLoadTemplate = "Load template"; MenuSaveTemplate = "Save template"; MenuSaveTemplateAs = "Save template as ..."; diff --git a/OTM_GUI/OTM_Sender.cpp b/OTM_GUI/OTM_Sender.cpp index bd48788..721d241 100644 --- a/OTM_GUI/OTM_Sender.cpp +++ b/OTM_GUI/OTM_Sender.cpp @@ -275,11 +275,15 @@ int OTM_Sender::SendTextures(unsigned int num, AddTextureClass *tex) // tex[i].Add!=true we can always remove, cause removing does take time in the render thread // if tex[i].Add==true and WasAdded[j]!=true this texture was not loaded but should be loaded, so maybe we can load it now { - bool hit = false; + bool hit = false; //we send only if this has was not send before unsigned long temp_hash = tex[i].Hash[j]; for (unsigned int ii=0u; iiBIG_BUFSIZE) { @@ -294,8 +298,8 @@ int OTM_Sender::SendTextures(unsigned int num, AddTextureClass *tex) if (tex[i].Add) { - msg->Control = CONTROL_FORCE_RELOAD_TEXTURE_DATA; //we always force because whether force==true - //or Add==true && WasAdded[j]!=true which means it is loaded the first time, or in previous loads it could not be loaded + msg->Control = CONTROL_FORCE_RELOAD_TEXTURE_DATA; //we always force because whether force is true or not + //if (Add==true && WasAdded[j]!=true) the texture is loaded the first time, or in previous loads it could not be loaded //because an other texture was send with the same hash, in all cases forcing is the best choice (atm) char* temp = tex[i].Textures[j]; if (temp!=NULL) diff --git a/OTM_GUI/bin/languages/Example_OTM_LanguagePack_English.txt b/OTM_GUI/bin/languages/Example_OTM_LanguagePack_English.txt index 36513f5..3322dce 100644 --- a/OTM_GUI/bin/languages/Example_OTM_LanguagePack_English.txt +++ b/OTM_GUI/bin/languages/Example_OTM_LanguagePack_English.txt @@ -50,6 +50,7 @@ MenuLoadTemplate:Load template| MenuSaveTemplate:Save template| MenuSaveTemplateAs:Save template as ...| MenuSetDefaultTemplate:Set template as default| +MenuLanguage:Change language| MenuExit:Exit| MainMenuMain:Main| MainMenuHelp: diff --git a/OTM_GUI/bin/languages/OTM_LanguagePackU_Deutsch.txt b/OTM_GUI/bin/languages/OTM_LanguagePackU_Deutsch.txt index fa8db47f9e10fb33e7e31239d8f14b0f60a40818..d9561c16b275f0c43e4cf589ea3a4e1f946b4f07 100644 GIT binary patch delta 66 zcmcbmwnKeG57XpTta66I3gfm3juCTs

|AMlWQTIcLu7z1H4q?fHE5$MVOe(F3I#XsE7wto!VGJX1xg*rtWn zu$r~4Yk?lwZR=RXuBt>Y#__2F^=ERI^+9Je@9znIOM7|=!;#(*pX!-sIUITV47vrY z+p68N*w$H_5XaVnx@HB~1gHx{kMvzK5B18EXTIReL`P(H#s%L4=O(BzSbt(X1>r3@ zyE;7ulGjV;!~aQ|$^oRMH+iE}9!+NkiXeAdt@vw}K2@e|a-UPkkQTCkv{ONd(2G*& zKaC&bO`t7WyP<@AZ1wAQ7fNT>!_Vg~C|;Wxl`5r$oQI5z^R#IRyJC=j4M%=Zx>n!j`yJQ| zft;r&uq52aaQy9YmH4rz`Il1{F13g{Cd*C4dny}J-@tdZuPA>#Umhj4K^=iyC0o>S zpSOlp7~6{|vEh6E{k535Lh!bemC=t_1G%|ukngOGL2^1f3!1#S4p-4&6MZL!Q_W59|fI&>PvtyAE-ANsG@ALKBJ_dEsFt*Ba0JIl^u&E&;*OgifqE0 J@3Bhp0{|M~87u$* diff --git a/OTM_GUI/config.gcc b/OTM_GUI/config.gcc index 2c05b6d..ac29dd2 100644 --- a/OTM_GUI/config.gcc +++ b/OTM_GUI/config.gcc @@ -140,7 +140,7 @@ CPPUNIT_LIBS ?= # Version of C runtime library to use. You can change this to # static if SHARED=0, but it is highly recommended to not do # it if SHARED=1 unless you know what you are doing. [dynamic,static] -RUNTIME_LIBS ?= dynamic +RUNTIME_LIBS ?= static # Set the version of your Mingw installation here. # "3" ...... this is for Mingw 2.0 or newer (comes with gcc3) diff --git a/OTM_GUI/config.vc b/OTM_GUI/config.vc index 8655990..aa404c3 100644 --- a/OTM_GUI/config.vc +++ b/OTM_GUI/config.vc @@ -145,5 +145,5 @@ CPPUNIT_LIBS = # Version of C runtime library to use. You can change this to # static if SHARED=0, but it is highly recommended to not do # it if SHARED=1 unless you know what you are doing. [dynamic,static] -RUNTIME_LIBS = dynamic +RUNTIME_LIBS = static