Commit MetaInfo

Revisiond2d1b6db855af428e16af5e4c53806f06932bc23 (tree)
Time2011-01-23 07:34:28
AuthorFace
CommiterFace

Log Message

OrbiterExtensions: added hooking of RecordEvent

Change Summary

Incremental Difference

diff -r 155120f98b02 -r d2d1b6db855a Orbitersdk/samples/AscensionUltra/VirtualDockingTunnel.cpp
--- a/Orbitersdk/samples/AscensionUltra/VirtualDockingTunnel.cpp Sun Jan 09 16:16:55 2011 +0100
+++ b/Orbitersdk/samples/AscensionUltra/VirtualDockingTunnel.cpp Sat Jan 22 23:34:28 2011 +0100
@@ -10,22 +10,43 @@
1010
1111 #define DOCKSTRUCTOFFSET_CURRENTDOCKOBJECT 0x48
1212 #define ADDRESS_GETDOCKSTATUS 0x00476210
13+#define VESSELSTRUCTOFFSET_RECORDING 0x0D32
14+#define ADDRESS_RECORDEVENT 0x00476FA0
15+#define ADDRESS_INTERNALRECORDEVENT 0x00465FB0
1316
1417 // ==============================================================
1518 // Global variables
1619
1720 #include "orbitersdk.h"
1821 #include <map>
22+#include <queue>
1923
2024 namespace OrbiterExtensions
2125
2226 {
2327
28+struct EventEntry
29+{
30+ double MJD;
31+ char *event_type;
32+ char *event;
33+};
34+
35+struct EventQueue
36+{
37+ CRITICAL_SECTION access;
38+ int backlog;
39+ std::queue<EventEntry> queue;
40+};
41+
2442 std::map<VESSEL *, OBJHANDLE> g_DockLink;
43+std::map<VESSEL *, EventQueue> g_Events;
2544 std::map<VESSEL *, int> g_Handles;
2645
2746 DWORD g_Hook;
28-byte g_original[10]={0x8b,0x44,0x24,0x04,0x8b,0x40,0x48,0xc2,0x04,0x00};
47+byte g_GetDockStatusOriginal[9]={0x8b,0x44,0x24,0x04,0x8b,0x40,0x48,0xc2,0x04};
48+byte g_RecordEventOriginal[9]={0x8B,0x09,0x80,0xB9,0x32,0x0D,0x00,0x00,0x00};
49+
2950 //The following array is:
3051 //_asm
3152 //{
@@ -35,7 +56,7 @@
3556 // jmp dword ptr [GetDockStatus]; //Dynamically detected address
3657 // nop;
3758 //}
38-byte g_code[10] = {0x58, 0x51, 0x50, 0xff, 0x25, 0, 0, 0, 0, 0x90};
59+byte g_Code[9] = {0x58, 0x51, 0x50, 0xff, 0x25, 0, 0, 0, 0};
3960
4061 OBJHANDLE _stdcall GetDockStatus(VESSEL *vessel, DOCKHANDLE dock)
4162 {
@@ -46,6 +67,38 @@
4667 return *(OBJHANDLE *)(void *)((char *)dock+DOCKSTRUCTOFFSET_CURRENTDOCKOBJECT);
4768 }
4869
70+void _stdcall RecordEvent(VESSEL *vessel, const char *event_type, const char *event)
71+{
72+ //Do my own RecordEvent
73+ std::map<VESSEL *, EventQueue>::iterator el=g_Events.find(vessel);
74+ if (el!=g_Events.end())
75+ {
76+ EnterCriticalSection(&el->second.access);
77+ if (el->second.queue.size()<el->second.backlog)
78+ {
79+ EventEntry entry;
80+ entry.MJD=oapiGetSimMJD();
81+ entry.event=new char[strlen(event)+1];
82+ strcpy(entry.event, event);
83+ entry.event_type=new char[strlen(event_type)+1];
84+ strcpy(entry.event_type, event_type);
85+ el->second.queue.push(entry);
86+ }
87+ LeaveCriticalSection(&el->second.access);
88+ }
89+ //Original function content
90+ if (*(*(char **)vessel+VESSELSTRUCTOFFSET_RECORDING)==0) return;
91+ _asm
92+ {
93+ push event
94+ push event_type
95+ mov eax, vessel
96+ mov ecx,dword ptr ds:[eax]
97+ mov eax,ADDRESS_INTERNALRECORDEVENT
98+ call eax
99+ }
100+}
101+
49102 int WriteCode(void *address, void *code, DWORD len)
50103 {
51104 //Get process information
@@ -87,25 +140,35 @@
87140 // 1 if already hooked
88141 // -1 if already initialized by handle
89142 // -2 if already hooked by some other system
90-__declspec(dllexport) int __cdecl Init(VESSEL *handle)
143+__declspec(dllexport) int __cdecl Init(VESSEL *handle, int flags=0)
91144 {
92145 if (g_Handles.find(handle)!=g_Handles.end()) return -1;
93146 g_Handles[handle]=1;
147+
148+ if (memcmp((void *)g_GetDockStatusOriginal, (void *)ADDRESS_GETDOCKSTATUS, 9)!=0 ||
149+ memcmp((void *)g_RecordEventOriginal, (void *)ADDRESS_RECORDEVENT, 9)!=0)
150+ {
151+ if (g_Handles.size()==1) return -2;
152+ return 1;
153+ }
154+
94155 union
95156 {
96157 void *pointer;
97158 byte bytes[4];
98159 DWORD value;
99160 } p;
161+
100162 g_Hook=(DWORD)(void *)GetDockStatus;
101163 p.pointer=(void *)&g_Hook;
102- for(int i=0;i<4;i++) g_code[5+i] = p.bytes[i];
103- if (memcmp((void *)g_original, (void *)ADDRESS_GETDOCKSTATUS, 10)!=0)
104- {
105- if (g_Handles.size()==1) return -2;
106- return 1;
107- }
108- WriteCode((void *)ADDRESS_GETDOCKSTATUS, (void *)g_code, 10);
164+ for(int i=0;i<4;i++) g_Code[5+i] = p.bytes[i];
165+ WriteCode((void *)ADDRESS_GETDOCKSTATUS, (void *)g_Code, 9);
166+
167+ g_Hook=(DWORD)(void *)RecordEvent;
168+ p.pointer=(void *)&g_Hook;
169+ for(int i=0;i<4;i++) g_Code[5+i] = p.bytes[i];
170+ WriteCode((void *)ADDRESS_RECORDEVENT, (void *)g_Code, 9);
171+
109172 return 0;
110173 }
111174
@@ -119,8 +182,9 @@
119182 if (g_Handles.find(handle)==g_Handles.end()) return -1;
120183 g_Handles.erase(handle);
121184 if (g_Handles.size()>0) return 1;
122- if (memcmp((void *)g_code, (void *)ADDRESS_GETDOCKSTATUS, 10)!=0) return -2;
123- WriteCode((void *)ADDRESS_GETDOCKSTATUS, (void *)g_original, 10);
185+ if (memcmp((void *)g_Code, (void *)ADDRESS_RECORDEVENT, 10)!=0) return -2;
186+ WriteCode((void *)ADDRESS_GETDOCKSTATUS, (void *)g_GetDockStatusOriginal, 10);
187+ WriteCode((void *)ADDRESS_RECORDEVENT, (void *)g_RecordEventOriginal, 10);
124188 return 0;
125189 }
126190
@@ -140,7 +204,7 @@
140204 return 0;
141205 }
142206
143-__declspec(dllexport) float __cdecl GetVersion(){return (float)0.1;}
207+__declspec(dllexport) float __cdecl GetVersion(){return (float)0.2;}
144208 }
145209
146210 }
\ No newline at end of file
Show on old repository browser