diff options
Diffstat (limited to 'contrib/npclient/npclient.c')
-rw-r--r-- | contrib/npclient/npclient.c | 299 |
1 files changed, 131 insertions, 168 deletions
diff --git a/contrib/npclient/npclient.c b/contrib/npclient/npclient.c index ac8757f4..03fa589a 100644 --- a/contrib/npclient/npclient.c +++ b/contrib/npclient/npclient.c @@ -3,9 +3,9 @@ #include <stdarg.h> #include <stdlib.h> #include <stdbool.h> -#include <stdint.h> #include <math.h> #include <string.h> +#include <stdio.h> #include <windows.h> @@ -14,8 +14,6 @@ #define UNUSED(var) (void)var -//#define DEBUG - typedef struct TFreeTrackData { int DataID; @@ -41,91 +39,44 @@ typedef struct TFreeTrackData float Y3; float X4; float Y4; -} volatile TFreeTrackData; +} TFreeTrackData; typedef struct FTMemMap { TFreeTrackData data; - uint32_t GameId; + __int32 GameId; unsigned char table[8]; - uint32_t GameId2; -} volatile FTMemMap; + __int32 GameId2; +} FTMemMap; + +static bool bEncryptionChecked = false; +static double r = 0, p = 0, y = 0, tx = 0, ty = 0, tz = 0; #define NP_DECLSPEC __declspec(dllexport) #define NP_EXPORT(t) t NP_DECLSPEC __stdcall #define NP_AXIS_MAX 16383 -static uint32_t volatile game_id_local; -static HANDLE hFTMemMap = 0; -static FTMemMap* pMemData = 0; - -#if defined _MSC_VER -# define force_inline __forceinline -#else -# define force_inline __attribute__((always_inline, gnu_inline)) inline -#endif - -#ifdef DEBUG -# include <stdio.h> -# define dbg_report(...) \ - do \ - { \ - if (debug_stream) \ - { \ - fprintf(debug_stream, __VA_ARGS__); \ - fprintf(debug_stream, "\n"); \ - fflush(debug_stream); \ - } \ - } while (0) +static bool FTCreateMapping(void); +static void FTDestroyMapping(void); +static __inline double clamp(double x, double xmin, double xmax); +static __inline double clamp_(double x); +#if DEBUG static FILE *debug_stream; +#define dbg_report(...) do { if (debug_stream) { fprintf(debug_stream, __VA_ARGS__); fflush(debug_stream); } } while(0); #else -# define dbg_report(...) do { (void)0; } while (0) +#define dbg_report(...) #endif -static bool FTCreateMapping(void) -{ - if (pMemData) - return true; - - dbg_report("FTCreateMapping request (pMemData == NULL)"); - - HANDLE hFTMutex = CreateMutexA(NULL, FALSE, FREETRACK_MUTEX); - CloseHandle(hFTMutex); - - hFTMemMap = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, sizeof(FTMemMap), FT_MM_DATA); - pMemData = (FTMemMap *) MapViewOfFile(hFTMemMap, FILE_MAP_WRITE, 0, 0, sizeof(FTMemMap)); - return pMemData != NULL; -} - -static void FTDestroyMapping(void) -{ - if (pMemData != NULL) - { - InterlockedExchange((LONG volatile*) &pMemData->data.DataID, -1); - UnmapViewOfFile((void*)pMemData); - } - - CloseHandle(hFTMemMap); - pMemData = 0; - hFTMemMap = 0; -} - -static force_inline double clamp(double x, double xmin, double xmax) -{ - if (x > xmax) - return xmax; - - if (x < xmin) - return xmin; - - return x; -} +typedef enum npclient_status_ { + NPCLIENT_STATUS_OK, + NPCLIENT_STATUS_DISABLED +} npclient_status; -static force_inline double clamp_(double x) -{ - return clamp(x, -NP_AXIS_MAX, NP_AXIS_MAX); -} +static HANDLE hFTMemMap = 0; +static FTMemMap volatile * pMemData = 0; +static bool bEncryption = false; +static unsigned char table[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; typedef struct tir_data { @@ -135,13 +86,13 @@ typedef struct tir_data float roll, pitch, yaw; float tx, ty, tz; float padding[9]; -} tir_data; +} tir_data_t; typedef struct tir_signature { char DllSignature[200]; char AppSignature[200]; -} tir_signature; +} tir_signature_t; BOOL DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { @@ -150,17 +101,16 @@ BOOL DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) switch (fdwReason) { case DLL_PROCESS_ATTACH: -#ifdef DEBUG +#if DEBUG debug_stream = fopen("c:\\NPClient.log", "a"); #endif - #ifdef _WIN64 - dbg_report("\n= WIN64 ========================================================================================="); + dbg_report("\n= WIN64 =========================================================================================\n"); #else - dbg_report("\n= WIN32 ========================================================================================="); + dbg_report("\n= WIN32 =========================================================================================\n"); #endif - dbg_report("DllMain: (%p, %ld, %p)", (void*) hinstDLL, (long) fdwReason, lpvReserved); - dbg_report("DllMain: Attach request"); + dbg_report("DllMain: (%p, %ld, %p)\n", (void*) hinstDLL, (long) fdwReason, lpvReserved); + dbg_report("DllMain: Attach request\n"); DisableThreadLibraryCalls(hinstDLL); #if 0 timeBeginPeriod(1); @@ -168,13 +118,12 @@ BOOL DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) break; case DLL_PROCESS_DETACH: - dbg_report("DllMain: Detach"); + dbg_report("DllMain: Detach\n"); dbg_report("DllMain: (%p, %ld, %p)\n", (void*) hinstDLL, (long) fdwReason, lpvReserved); - dbg_report("=========================================================================================="); + dbg_report("==========================================================================================\n"); #if 0 timeEndPeriod(1); #endif - FTDestroyMapping(); break; } @@ -186,7 +135,7 @@ BOOL DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) NP_EXPORT(int) NPPriv_ClientNotify(void) { - dbg_report("stub"); + dbg_report("stub\n"); /* @stub in .spec */ return 0; } @@ -196,7 +145,7 @@ NP_EXPORT(int) NPPriv_ClientNotify(void) NP_EXPORT(int) NPPriv_GetLastError(void) { - dbg_report("stub"); + dbg_report("stub\n"); /* @stub in .spec */ return 0; } @@ -206,7 +155,7 @@ NP_EXPORT(int) NPPriv_GetLastError(void) NP_EXPORT(int) NPPriv_SetData(void) { - dbg_report("stub"); + dbg_report("stub\n"); /* @stub in .spec */ return 0; } @@ -216,7 +165,7 @@ NP_EXPORT(int) NPPriv_SetData(void) NP_EXPORT(int) NPPriv_SetLastError(void) { - dbg_report("stub"); + dbg_report("stub\n"); /* @stub in .spec */ return 0; } @@ -226,7 +175,7 @@ NP_EXPORT(int) NPPriv_SetLastError(void) NP_EXPORT(int) NPPriv_SetParameter(void) { - dbg_report("stub"); + dbg_report("stub\n"); /* @stub in .spec */ return 0; } @@ -236,7 +185,7 @@ NP_EXPORT(int) NPPriv_SetParameter(void) NP_EXPORT(int) NPPriv_SetSignature(void) { - dbg_report("stub"); + dbg_report("stub\n"); /* @stub in .spec */ return 0; } @@ -246,7 +195,7 @@ NP_EXPORT(int) NPPriv_SetSignature(void) NP_EXPORT(int) NPPriv_SetVersion(void) { - dbg_report("stub"); + dbg_report("stub\n"); /* @stub in .spec */ return 0; } @@ -313,7 +262,7 @@ static unsigned cksum(unsigned char buf[], unsigned size) return (unsigned)c; } -static inline void enhance(unsigned char buf[], unsigned size, unsigned char table[], unsigned table_size) +static __inline void enhance(unsigned char buf[], unsigned size, unsigned char table[], unsigned table_size) { unsigned table_ptr = 0; unsigned char var = 0x88; @@ -339,30 +288,24 @@ static inline void enhance(unsigned char buf[], unsigned size, unsigned char tab * NP_GetData (NPCLIENT.8) */ -typedef enum npclient_status_ { - NPCLIENT_STATUS_OK, - NPCLIENT_STATUS_DISABLED -} npclient_status; - -NP_EXPORT(int) NP_GetData(tir_data* data) +NP_EXPORT(int) NP_GetData(tir_data_t * data) { - static double r = 0, p = 0, y = 0, tx = 0, ty = 0, tz = 0; - static unsigned frameno = 0; - static unsigned char table[8] = {0}; - static bool bEncryption = false; - static bool bEncryptionChecked = false; - int i, data_id = -1; - + static int frameno = 0; + int i; +#if DEBUG + int recv = 0; +#endif if (!FTCreateMapping()) { - dbg_report("Can't open mapping"); - return NPCLIENT_STATUS_DISABLED; + dbg_report("Can't open mapping\n"); + return 0; } - if (game_id_local > 0 && - pMemData->GameId == game_id_local && - pMemData->GameId == pMemData->GameId2) + if (pMemData) { +#if DEBUG + recv = 1; +#endif y = pMemData->data.Yaw * NP_AXIS_MAX / M_PI; p = pMemData->data.Pitch * NP_AXIS_MAX / M_PI; r = pMemData->data.Roll * NP_AXIS_MAX / M_PI; @@ -371,9 +314,9 @@ NP_EXPORT(int) NP_GetData(tir_data* data) ty = pMemData->data.Y * NP_AXIS_MAX / 500.; tz = pMemData->data.Z * NP_AXIS_MAX / 500.; - if (!bEncryptionChecked) + if (pMemData->GameId == pMemData->GameId2 && !bEncryptionChecked) { - dbg_report("NP_GetData: game = %d", pMemData->GameId); + dbg_report("NP_GetData: game = %d\n", pMemData->GameId); bEncryptionChecked = true; memcpy(table, (void*)pMemData->table, 8); for (i = 0; i < 8; i++) @@ -382,29 +325,17 @@ NP_EXPORT(int) NP_GetData(tir_data* data) bEncryption = true; break; } - dbg_report("NP_GetData: Table = %02d %02d %02d %02d %02d %02d %02d %02d", table[0],table[1],table[2],table[3],table[4],table[5], table[6], table[7]); + dbg_report("NP_GetData: Table = %02d %02d %02d %02d %02d %02d %02d %02d\n", table[0],table[1],table[2],table[3],table[4],table[5], table[6], table[7]); } - - data_id = InterlockedCompareExchange((LONG volatile*) &pMemData->data.DataID, -1, -1); } - data->frame = ++frameno; + frameno++; + data->frame = frameno; - bool running = false; + bool running = y != 0 || p != 0 || r != 0 || + tx != 0 || ty != 0 || tz != 0; - if (data_id == 0) - { - running = true; - y = 0, r = 0, p = 0, tx = 0, ty = 0, tz = 0; - (void)InterlockedCompareExchange((LONG volatile*) &pMemData->data.DataID, -1, 0); - } - else if (data_id > 0) - { - running = true; - (void)InterlockedCompareExchange((LONG volatile*) &pMemData->data.DataID, data_id - 1, data_id); - } - - data->status = NPCLIENT_STATUS_OK; + data->status = running ? NPCLIENT_STATUS_OK : NPCLIENT_STATUS_DISABLED; data->cksum = 0; data->roll = clamp_(r); @@ -415,18 +346,17 @@ NP_EXPORT(int) NP_GetData(tir_data* data) data->ty = clamp_(ty); data->tz = clamp_(tz); - for (i = 0; i < 9; ++i) - data->padding[i] = 0; + for(i = 0; i < 9; ++i) + data->padding[i] = 0.0; -#ifdef DEBUG - dbg_report("GetData: rotation: %f %f %f", data->yaw, data->pitch, data->roll); - dbg_report("GetData: status:%d dataid:%d enc:%d id1:%d id2:%d\n", (int) running, data_id, (int)bEncryption, (int)pMemData->GameId, (int)pMemData->GameId2); +#if DEBUG + dbg_report("GetData: rotation: %d %f %f %f\n", recv, data->yaw, data->pitch, data->roll); #endif - data->cksum = cksum((unsigned char*)data, sizeof(*data)); + data->cksum = cksum((unsigned char*)data, sizeof(tir_data_t)); if (bEncryption) - enhance((unsigned char*)data, sizeof(*data), table, sizeof(table)); + enhance((unsigned char*)data, sizeof(tir_data_t), table, sizeof(table)); return running ? NPCLIENT_STATUS_OK : NPCLIENT_STATUS_DISABLED; } @@ -437,8 +367,8 @@ NP_EXPORT(int) NP_GetData(tir_data* data) NP_EXPORT(int) NP_GetParameter(int arg0, int arg1) { UNUSED(arg0); UNUSED(arg1); - dbg_report("GetParameter request: %d %d", arg0, arg1); - return 0; + dbg_report("GetParameter request: %d %d\n", arg0, arg1); + return (int) 0; } /****************************************************************** @@ -447,7 +377,7 @@ NP_EXPORT(int) NP_GetParameter(int arg0, int arg1) * */ -static unsigned char volatile const part2_2[] = { +static volatile unsigned char part2_2[200] = { 0xe3, 0xe5, 0x8e, 0xe8, 0x06, 0xd4, 0xab, 0xcf, 0xfa, 0x51, 0xa6, 0x84, 0x69, 0x52, 0x21, 0xde, 0x6b, 0x71, 0xe6, 0xac, 0xaa, @@ -462,10 +392,11 @@ static unsigned char volatile const part2_2[] = { 0xe4, 0xc0, 0xf1, 0x7f, 0x87, 0xd0, 0x70, 0xa4, 0x04, 0x07, 0x05, 0x69, 0x2a, 0x16, 0x15, 0x55, 0x85, 0xa6, 0x30, 0xc8, 0xb6, + 0x00 }; -static unsigned char volatile const part1_2[] = { +static volatile unsigned char part1_2[200] = { 0x6d, 0x0b, 0xab, 0x56, 0x74, 0xe6, 0x1c, 0xff, 0x24, 0xe8, 0x34, 0x8f, 0x00, 0x63, 0xed, 0x47, 0x5d, 0x9b, 0xe1, 0xe0, 0x1d, @@ -481,10 +412,10 @@ static unsigned char volatile const part1_2[] = { 0x5d, 0x1a, 0xb4, 0x84, 0x9c, 0x29, 0xf0, 0xe6, 0x69, 0x73, 0x66, 0x0e, 0x4b, 0x3c, 0x7d, 0x99, 0x8b, 0x4e, 0x7d, 0xaf, 0x86, - 0x92 + 0x92, 0xff }; -static unsigned char volatile const part2_1[] = { +static volatile unsigned char part2_1[200] = { 0x8b, 0x84, 0xfc, 0x8c, 0x71, 0xb5, 0xd9, 0xaa, 0xda, 0x32, 0xc7, 0xe9, 0x0c, 0x20, 0x40, 0xd4, 0x4b, 0x02, 0x89, 0xca, 0xde, @@ -499,9 +430,10 @@ static unsigned char volatile const part2_1[] = { 0x81, 0x83, 0x9e, 0x11, 0xf3, 0xa2, 0x1f, 0xc8, 0x24, 0x53, 0x60, 0x0a, 0x42, 0x78, 0x7a, 0x39, 0xea, 0xc1, 0x59, 0xad, 0xc5, + 0x00 }; -static unsigned char volatile const part1_1[] = { +static volatile unsigned char part1_1[200] = { 0x1d, 0x79, 0xce, 0x35, 0x1d, 0x95, 0x79, 0xdf, 0x4c, 0x8d, 0x55, 0xeb, 0x20, 0x17, 0x9f, 0x26, 0x3e, 0xf0, 0x88, 0x8e, 0x7a, @@ -517,30 +449,24 @@ static unsigned char volatile const part1_1[] = { 0x24, 0x7f, 0xf7, 0xeb, 0xf2, 0x5d, 0x82, 0x89, 0x05, 0x53, 0x32, 0x6b, 0x28, 0x54, 0x13, 0xf6, 0xe7, 0x21, 0x1a, 0xc6, 0xe3, - 0xe1 + 0xe1, 0xff }; -NP_EXPORT(int) NP_GetSignature(tir_signature* sig) +NP_EXPORT(int) NP_GetSignature(tir_signature_t * sig) { - unsigned i; - dbg_report("GetSignature request"); + int i; + dbg_report("GetSignature request\n"); - for (i = 0; i < sizeof(part1_1); i++) + for (i = 0; i < 200; i++) sig->DllSignature[i] = part1_2[i] ^ part1_1[i]; - for (; i < 200; i++) - sig->DllSignature[i] = '\0'; - - for (i = 0; i < sizeof(part2_1); i++) + for (i = 0; i < 200; i++) sig->AppSignature[i] = part2_1[i] ^ part2_2[i]; - for (; i < 200; i++) - sig->AppSignature[i] = '\0'; - return 0; } -NP_EXPORT(int) NP_QueryVersion(unsigned short* version) +NP_EXPORT(int) NP_QueryVersion(unsigned short * version) { - dbg_report("QueryVersion request"); + dbg_report("QueryVersion request\n"); *version=0x0500; return 0; } @@ -561,10 +487,7 @@ NP_EXPORT(int) NP_ReCenter(void) NP_EXPORT(int) NP_RegisterProgramProfileID(unsigned short id) { if (FTCreateMapping()) - { pMemData->GameId = id; - game_id_local = id; - } dbg_report("RegisterProgramProfileID request: %d\n", id); return 0; } @@ -576,7 +499,7 @@ NP_EXPORT(int) NP_RegisterWindowHandle(HWND hwnd) { UNUSED(hwnd); dbg_report("RegisterWindowHandle request: %p\n", (void*) hwnd); - return 0; + return (int) 0; } /****************************************************************** * NP_RequestData (NPCLIENT.15) @@ -586,7 +509,7 @@ NP_EXPORT(int) NP_RequestData(unsigned short req) { UNUSED(req); dbg_report("RequestData request: %d\n", req); - return 0; + return (int) 0; } /****************************************************************** * NP_SetParameter (NPCLIENT.16) @@ -596,7 +519,7 @@ NP_EXPORT(int) NP_SetParameter(int arg0, int arg1) { UNUSED(arg0); UNUSED(arg1); dbg_report("SetParameter request: %d %d\n", arg0, arg1); - return 0; + return (int) 0; } /****************************************************************** * NP_StartCursor (NPCLIENT.17) @@ -605,7 +528,7 @@ NP_EXPORT(int) NP_SetParameter(int arg0, int arg1) NP_EXPORT(int) NP_StartCursor(void) { dbg_report("StartCursor request\n"); - return 0; + return (int) 0; } /****************************************************************** * NP_StartDataTransmission (NPCLIENT.18) @@ -615,7 +538,7 @@ NP_EXPORT(int) NP_StartDataTransmission(void) { dbg_report("StartDataTransmission request.\n"); - return 0; + return (int) 0; } /****************************************************************** * NP_StopCursor (NPCLIENT.19) @@ -624,7 +547,7 @@ NP_EXPORT(int) NP_StartDataTransmission(void) NP_EXPORT(int) NP_StopCursor(void) { dbg_report("StopCursor request\n"); - return 0; + return (int) 0; } /****************************************************************** * NP_StopDataTransmission (NPCLIENT.20) @@ -632,7 +555,7 @@ NP_EXPORT(int) NP_StopCursor(void) NP_EXPORT(int) NP_StopDataTransmission(void) { - return 0; + return (int) 0; } /****************************************************************** * NP_UnregisterWindowHandle (NPCLIENT.21) @@ -641,6 +564,46 @@ NP_EXPORT(int) NP_StopDataTransmission(void) NP_EXPORT(int) NP_UnregisterWindowHandle(void) { dbg_report("UnregisterWindowHandle request\n"); - return 0; + return (int) 0; +} + +static bool FTCreateMapping(void) +{ + if (pMemData) + return true; + + dbg_report("FTCreateMapping request (pMemData == NULL).\n"); + + HANDLE hFTMutex = CreateMutexA(NULL, FALSE, FREETRACK_MUTEX); + CloseHandle(hFTMutex); + + hFTMemMap = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, sizeof(FTMemMap), (LPCSTR) FT_MM_DATA); + pMemData = (FTMemMap *) MapViewOfFile(hFTMemMap, FILE_MAP_WRITE, 0, 0, sizeof(FTMemMap)); + return pMemData != NULL; } +static void FTDestroyMapping(void) +{ + if (pMemData != NULL) + UnmapViewOfFile((void*)pMemData); + + CloseHandle(hFTMemMap); + pMemData = 0; + hFTMemMap = 0; +} + +static __inline double clamp(double x, double xmin, double xmax) +{ + if (x > xmax) + return xmax; + + if (x < xmin) + return xmin; + + return x; +} + +static __inline double clamp_(double x) +{ + return clamp(x, -NP_AXIS_MAX, NP_AXIS_MAX); +} |