/[pcsx2_0.9.7]/trunk/tools/GSDumpGUI/Library/GSDXWrapper.cs
ViewVC logotype

Contents of /trunk/tools/GSDumpGUI/Library/GSDXWrapper.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 2 months ago) by william
File size: 18454 byte(s)
re-commit (had local access denied errors when committing)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5 using System.IO;
6 using TCPLibrary.MessageBased.Core;
7 using System.Threading;
8
9 namespace GSDumpGUI
10 {
11 public delegate void GSgifTransfer(IntPtr data, int size);
12 public delegate void GSgifTransfer1(IntPtr data, int size);
13 public delegate void GSgifTransfer2(IntPtr data, int size);
14 public delegate void GSgifTransfer3(IntPtr data, int size);
15 public delegate void GSVSync(byte field);
16 public delegate void GSreadFIFO2(IntPtr data, int size);
17 public delegate void GSsetGameCRC(int crc, int options);
18 public delegate int GSfreeze(int mode, IntPtr data);
19 public delegate void GSopen(IntPtr hwnd, String Title, int renderer);
20 public delegate void GSclose();
21 public delegate void GSshutdown();
22 public delegate void GSConfigure();
23 public delegate void GSsetBaseMem(IntPtr data);
24 public delegate IntPtr PSEgetLibName();
25 public delegate void GSinit();
26
27 public class GSDXWrapper
28 {
29 static public bool DumpTooOld = false;
30
31 private GSConfigure gsConfigure;
32 private PSEgetLibName PsegetLibName;
33 private GSgifTransfer GSgifTransfer;
34 private GSgifTransfer1 GSgifTransfer1;
35 private GSgifTransfer2 GSgifTransfer2;
36 private GSgifTransfer3 GSgifTransfer3;
37 private GSVSync GSVSync;
38 private GSreadFIFO2 GSreadFIFO2;
39 private GSsetGameCRC GSsetGameCRC;
40 private GSfreeze GSfreeze;
41 private GSopen GSopen;
42 private GSclose GSclose;
43 private GSshutdown GSshutdown;
44 private GSsetBaseMem GSsetBaseMem;
45 private GSinit GSinit;
46
47 private Boolean Loaded;
48
49 private String DLL;
50 private IntPtr DLLAddr;
51
52 private Boolean Running;
53
54 public Queue<TCPMessage> QueueMessage;
55 public Boolean DebugMode;
56 public GSData CurrentGIFPacket;
57 public bool ThereIsWork;
58 public AutoResetEvent ExternalEvent;
59 public int RunTo;
60
61 static public Boolean IsValidGSDX(String DLL)
62 {
63 NativeMethods.SetErrorMode(0x8007);
64 Boolean Ris = true;
65
66 Directory.SetCurrentDirectory(Path.GetDirectoryName(DLL));
67 IntPtr hmod = NativeMethods.LoadLibrary(DLL);
68 if (hmod.ToInt64() > 0)
69 {
70 IntPtr funcaddrLibName = NativeMethods.GetProcAddress(hmod, "PS2EgetLibName");
71 IntPtr funcaddrConfig = NativeMethods.GetProcAddress(hmod, "GSconfigure");
72
73 IntPtr funcaddrGIF = NativeMethods.GetProcAddress(hmod, "GSgifTransfer");
74 IntPtr funcaddrVSync = NativeMethods.GetProcAddress(hmod, "GSvsync");
75 IntPtr funcaddrSetBaseMem = NativeMethods.GetProcAddress(hmod, "GSsetBaseMem");
76 IntPtr funcaddrOpen = NativeMethods.GetProcAddress(hmod, "GSopen");
77 IntPtr funcaddrSetCRC = NativeMethods.GetProcAddress(hmod, "GSsetGameCRC");
78 IntPtr funcaddrClose = NativeMethods.GetProcAddress(hmod, "GSclose");
79 IntPtr funcaddrShutdown = NativeMethods.GetProcAddress(hmod, "GSshutdown");
80 IntPtr funcaddrFreeze = NativeMethods.GetProcAddress(hmod, "GSfreeze");
81 IntPtr funcaddrGSreadFIFO2 = NativeMethods.GetProcAddress(hmod, "GSreadFIFO2");
82 IntPtr funcaddrinit = NativeMethods.GetProcAddress(hmod, "GSinit");
83
84 NativeMethods.FreeLibrary(hmod);
85 if (!((funcaddrConfig.ToInt64() > 0) && (funcaddrLibName.ToInt64() > 0) && (funcaddrGIF.ToInt64() > 0)))
86 {
87 Int32 id = NativeMethods.GetLastError();
88 System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "log.txt", DLL + " failed to load. Error " + id + Environment.NewLine);
89 Ris = false;
90 }
91 }
92 else
93 {
94 Int32 id = NativeMethods.GetLastError();
95 System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "log.txt", DLL + " failed to load. Error " + id + Environment.NewLine);
96 Ris = false;
97 }
98
99 NativeMethods.SetErrorMode(0x0000);
100 return Ris;
101 }
102
103 public void Load(String DLL)
104 {
105 this.DLL = DLL;
106 NativeMethods.SetErrorMode(0x8007);
107
108 if (!IsValidGSDX(DLL))
109 throw new Exception("Invalid GSDX DLL");
110
111 if (Loaded)
112 Unload();
113
114 Loaded = true;
115 Directory.SetCurrentDirectory(Path.GetDirectoryName(DLL));
116 IntPtr hmod = NativeMethods.LoadLibrary(DLL);
117 if (hmod.ToInt64() > 0)
118 {
119 IntPtr funcaddrLibName = NativeMethods.GetProcAddress(hmod, "PS2EgetLibName");
120 IntPtr funcaddrConfig = NativeMethods.GetProcAddress(hmod, "GSconfigure");
121
122 IntPtr funcaddrGIF = NativeMethods.GetProcAddress(hmod, "GSgifTransfer");
123 IntPtr funcaddrGIF1 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer1");
124 IntPtr funcaddrGIF2 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer2");
125 IntPtr funcaddrGIF3 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer3");
126 IntPtr funcaddrVSync = NativeMethods.GetProcAddress(hmod, "GSvsync");
127 IntPtr funcaddrSetBaseMem = NativeMethods.GetProcAddress(hmod, "GSsetBaseMem");
128 IntPtr funcaddrOpen = NativeMethods.GetProcAddress(hmod, "GSopen");
129 IntPtr funcaddrSetCRC = NativeMethods.GetProcAddress(hmod, "GSsetGameCRC");
130 IntPtr funcaddrClose = NativeMethods.GetProcAddress(hmod, "GSclose");
131 IntPtr funcaddrShutdown = NativeMethods.GetProcAddress(hmod, "GSshutdown");
132 IntPtr funcaddrFreeze = NativeMethods.GetProcAddress(hmod, "GSfreeze");
133 IntPtr funcaddrGSreadFIFO2 = NativeMethods.GetProcAddress(hmod, "GSreadFIFO2");
134 IntPtr funcaddrinit = NativeMethods.GetProcAddress(hmod, "GSinit");
135
136 gsConfigure = (GSConfigure)Marshal.GetDelegateForFunctionPointer(funcaddrConfig, typeof(GSConfigure));
137 PsegetLibName = (PSEgetLibName)Marshal.GetDelegateForFunctionPointer(funcaddrLibName, typeof(PSEgetLibName));
138
139 this.GSgifTransfer = (GSgifTransfer)Marshal.GetDelegateForFunctionPointer(funcaddrGIF, typeof(GSgifTransfer));
140 this.GSgifTransfer1 = (GSgifTransfer1)Marshal.GetDelegateForFunctionPointer(funcaddrGIF1, typeof(GSgifTransfer1));
141 this.GSgifTransfer2 = (GSgifTransfer2)Marshal.GetDelegateForFunctionPointer(funcaddrGIF2, typeof(GSgifTransfer2));
142 this.GSgifTransfer3 = (GSgifTransfer3)Marshal.GetDelegateForFunctionPointer(funcaddrGIF3, typeof(GSgifTransfer3));
143 this.GSVSync = (GSVSync)Marshal.GetDelegateForFunctionPointer(funcaddrVSync, typeof(GSVSync));
144 this.GSsetBaseMem = (GSsetBaseMem)Marshal.GetDelegateForFunctionPointer(funcaddrSetBaseMem, typeof(GSsetBaseMem));
145 this.GSopen = (GSopen)Marshal.GetDelegateForFunctionPointer(funcaddrOpen, typeof(GSopen));
146 this.GSsetGameCRC = (GSsetGameCRC)Marshal.GetDelegateForFunctionPointer(funcaddrSetCRC, typeof(GSsetGameCRC));
147 this.GSclose = (GSclose)Marshal.GetDelegateForFunctionPointer(funcaddrClose, typeof(GSclose));
148 this.GSshutdown = (GSshutdown)Marshal.GetDelegateForFunctionPointer(funcaddrShutdown, typeof(GSshutdown));
149 this.GSfreeze = (GSfreeze)Marshal.GetDelegateForFunctionPointer(funcaddrFreeze, typeof(GSfreeze));
150 this.GSreadFIFO2 = (GSreadFIFO2)Marshal.GetDelegateForFunctionPointer(funcaddrGSreadFIFO2, typeof(GSreadFIFO2));
151 this.GSinit = (GSinit)Marshal.GetDelegateForFunctionPointer(funcaddrinit, typeof(GSinit));
152
153 DLLAddr = hmod;
154 }
155 NativeMethods.SetErrorMode(0x0000);
156 }
157
158 public void Unload()
159 {
160 NativeMethods.FreeLibrary(DLLAddr);
161 Loaded = false;
162 }
163
164 public void GSConfig()
165 {
166 if (!Loaded)
167 throw new Exception("GSDX is not loaded");
168 gsConfigure.Invoke();
169 }
170
171 public String PSEGetLibName()
172 {
173 if (!Loaded)
174 throw new Exception("GSDX is not loaded");
175 return Marshal.PtrToStringAnsi(PsegetLibName.Invoke());
176 }
177
178 public unsafe void Run(GSDump dump, int rendererOverride)
179 {
180 QueueMessage = new Queue<TCPMessage>();
181 Running = true;
182 ExternalEvent = new AutoResetEvent(true);
183
184 int lastVSyncField;
185
186 GSinit();
187 byte[] tempregisters = new byte[8192];
188 Array.Copy(dump.Registers, tempregisters, 8192);
189 fixed (byte* pointer = tempregisters)
190 {
191 GSsetBaseMem(new IntPtr(pointer));
192 Int32 HWND = 0;
193 GSopen(new IntPtr(&HWND), "", rendererOverride);
194 GSsetGameCRC(dump.CRC, 0);
195 fixed (byte* freeze = dump.StateData)
196 {
197 byte[] GSFreez = new byte[8];
198 Array.Copy(BitConverter.GetBytes(dump.StateData.Length), 0, GSFreez, 0, 4);
199 Array.Copy(BitConverter.GetBytes(new IntPtr(freeze).ToInt32()), 0, GSFreez, 4, 4);
200
201 fixed (byte* fr = GSFreez)
202 {
203 int ris = GSfreeze(0, new IntPtr(fr));
204 if (ris == -1)
205 {
206 DumpTooOld = true;
207 return;
208 }
209 GSVSync(1);
210
211 while (Running)
212 {
213 if (!NativeMethods.IsWindowVisible(new IntPtr(HWND)))
214 {
215 Running = false;
216 break;
217 }
218
219 Marshal.Copy(dump.Registers, 0, new IntPtr(pointer), 8192);
220 GSfreeze(0, new IntPtr(fr));
221
222 for (int i = 0; i < dump.Data.Count; i++)
223 {
224 GSData itm = dump.Data[i];
225 CurrentGIFPacket = itm;
226
227 if (DebugMode)
228 {
229 if (RunTo != -1)
230 {
231 if (i == RunTo)
232 {
233 RunTo = -1;
234
235 GSData g = new GSData();
236 g.id = GSType.VSync;
237 g.data = new byte[1];
238 g.data[0] = 0;
239 Step(g, pointer);
240
241 TCPMessage Msg = new TCPMessage();
242 Msg.MessageType = MessageType.RunToCursor;
243 Msg.Parameters.Add(i);
244 Program.Client.Send(Msg);
245
246 ExternalEvent.Set();
247 }
248 else
249 {
250 Step(itm, pointer);
251 }
252 }
253 else
254 {
255 while (!ThereIsWork && Running)
256 {
257 NativeMessage message;
258 while (NativeMethods.PeekMessage(out message, IntPtr.Zero, 0, 0, 1))
259 {
260 if (!NativeMethods.IsWindowVisible(new IntPtr(HWND)))
261 {
262 Running = false;
263 }
264 NativeMethods.TranslateMessage(ref message);
265 NativeMethods.DispatchMessage(ref message);
266 }
267 }
268
269 ThereIsWork = false;
270 if (QueueMessage.Count > 0)
271 {
272 TCPMessage Mess = QueueMessage.Dequeue();
273 switch (Mess.MessageType)
274 {
275 case MessageType.Step:
276 RunTo = i;
277 i = -1;
278 break;
279 case MessageType.RunToCursor:
280 RunTo = (int)Mess.Parameters[0];
281 i = -1;
282 break;
283 case MessageType.RunToNextVSync:
284 RunTo = dump.Data.FindIndex(i, a => a.id == GSType.VSync);
285 i = -1;
286 break;
287 default:
288 break;
289 }
290 Marshal.Copy(dump.Registers, 0, new IntPtr(pointer), 8192);
291 GSfreeze(0, new IntPtr(fr));
292 }
293 }
294 }
295 else
296 {
297 Step(itm, pointer);
298 }
299 }
300 }
301
302 GSclose();
303 GSshutdown();
304 }
305 }
306 }
307 }
308
309 private unsafe void Step(GSData itm, byte* registers)
310 {
311 /*"C:\Users\Alessio\Desktop\Plugins\Dll\gsdx-sse4-r3878.dll" "C:\Users\Alessio\Desktop\Plugins\Dumps\gsdx_20101219182059.gs" "GSReplay" 0*/
312 switch (itm.id)
313 {
314 case GSType.Transfer:
315 switch (((GSTransfer)itm).Path)
316 {
317 case GSTransferPath.Path1Old:
318 byte[] data = new byte[16384];
319 int addr = 16384 - itm.data.Length;
320 Array.Copy(itm.data, 0, data, addr, itm.data.Length);
321 fixed (byte* gifdata = data)
322 {
323 GSgifTransfer1(new IntPtr(gifdata), addr);
324 }
325 break;
326 case GSTransferPath.Path2:
327 fixed (byte* gifdata = itm.data)
328 {
329 GSgifTransfer2(new IntPtr(gifdata), (itm.data.Length) / 16);
330 }
331 break;
332 case GSTransferPath.Path3:
333 fixed (byte* gifdata = itm.data)
334 {
335 GSgifTransfer3(new IntPtr(gifdata), (itm.data.Length) / 16);
336 }
337 break;
338 case GSTransferPath.Path1New:
339 fixed (byte* gifdata = itm.data)
340 {
341 GSgifTransfer(new IntPtr(gifdata), (itm.data.Length) / 16);
342 }
343 break;
344 }
345 break;
346 case GSType.VSync:
347 GSVSync(itm.data[0]);
348 break;
349 case GSType.ReadFIFO2:
350 fixed (byte* FIFO = itm.data)
351 {
352 byte[] arrnew = new byte[*((int*)FIFO)];
353 fixed (byte* arrn = arrnew)
354 {
355 GSreadFIFO2(new IntPtr(arrn), *((int*)FIFO));
356 }
357 }
358 break;
359 case GSType.Registers:
360 Marshal.Copy(itm.data, 0, new IntPtr(registers), 8192);
361 break;
362 default:
363 break;
364 }
365 }
366
367 public void Stop()
368 {
369 Running = false;
370 }
371
372 internal List<Object> GetGifPackets(GSDump dump)
373 {
374 List<Object> Data = new List<Object>();
375 for (int i = 0; i < dump.Data.Count; i++)
376 {
377 String act = i.ToString() + "|";
378 act += dump.Data[i].id.ToString() + "|";
379 if (dump.Data[i].GetType().IsSubclassOf(typeof(GSData)))
380 {
381 act += ((GSTransfer)dump.Data[i]).Path.ToString();
382 }
383 else
384 {
385
386 }
387 Data.Add(act);
388 }
389 return Data;
390 }
391 }
392 }

  ViewVC Help
Powered by ViewVC 1.1.22