Skip to content

Commit

Permalink
GSDumpGUI: Watch for directory changes.
Browse files Browse the repository at this point in the history
Also try to avoid unclosed file handles.
  • Loading branch information
KrossX authored and lightningterror committed Aug 7, 2019
1 parent b749c8e commit bd6261e
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 56 deletions.
3 changes: 3 additions & 0 deletions tools/GSDumpGUI/Core/Program.cs
Expand Up @@ -48,6 +48,7 @@ static class Program


static private TreeNode CurrentNode; static private TreeNode CurrentNode;
static public IntPtr hMainIcon; static public IntPtr hMainIcon;
static public bool prog_running;


[STAThread] [STAThread]
static void Main(String[] args) static void Main(String[] args)
Expand Down Expand Up @@ -117,12 +118,14 @@ static void Main(String[] args)
Server.OnClientAfterConnect += new TCPLibrary.Core.Server.ConnectedHandler(Server_OnClientAfterConnect); Server.OnClientAfterConnect += new TCPLibrary.Core.Server.ConnectedHandler(Server_OnClientAfterConnect);
Server.OnClientAfterDisconnected += new TCPLibrary.Core.Server.DisconnectedHandler(Server_OnClientAfterDisconnected); Server.OnClientAfterDisconnected += new TCPLibrary.Core.Server.DisconnectedHandler(Server_OnClientAfterDisconnected);
Server.Enabled = true; Server.Enabled = true;
prog_running = true;


Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
frmMain = new GSDumpGUI(); frmMain = new GSDumpGUI();
Application.Run(frmMain); Application.Run(frmMain);


prog_running = false;
Server.Enabled = false; Server.Enabled = false;
} }
} }
Expand Down
2 changes: 1 addition & 1 deletion tools/GSDumpGUI/Forms/Helper/GsDumpFinder.cs
Expand Up @@ -53,7 +53,7 @@ public IEnumerable<GsDumpFile> GetValidGsdxDumps(DirectoryInfo directory)
foreach (var dump in dumps) foreach (var dump in dumps)
{ {
int crc; int crc;
using (var fileStream = File.Open(dump.FullName, FileMode.Open)) using (var fileStream = File.OpenRead(dump.FullName))
{ {
using (var br = new BinaryReader(fileStream)) using (var br = new BinaryReader(fileStream))
{ {
Expand Down
98 changes: 98 additions & 0 deletions tools/GSDumpGUI/Forms/frmMain.cs
Expand Up @@ -24,9 +24,11 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Concurrent;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using System.Threading;
using System.IO; using System.IO;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
Expand Down Expand Up @@ -92,6 +94,11 @@ public Int32 SelectedRad
private readonly GsDumps _availableGsDumps; private readonly GsDumps _availableGsDumps;
private readonly GsDlls _availableGsDlls; private readonly GsDlls _availableGsDlls;


private List<FileSystemWatcher> dll_watcher;
private List<FileSystemWatcher> dump_watcher;

private ConcurrentQueue<int> watcher_events;

public GSDumpGUI() public GSDumpGUI()
{ {
PortableXmlSettingsProvider.ApplyProvider(Settings); PortableXmlSettingsProvider.ApplyProvider(Settings);
Expand All @@ -116,6 +123,47 @@ public GSDumpGUI()
Processes = new List<Process>(); Processes = new List<Process>();


NoImage = CreateDefaultImage(); NoImage = CreateDefaultImage();

dll_watcher = new List<FileSystemWatcher>();
dump_watcher = new List<FileSystemWatcher>();
watcher_events = new ConcurrentQueue<int>();

Thread wt = new Thread(changes_watchdog_thread);
wt.Start();
}

private void changes_watchdog_thread()
{
while (Program.prog_running)
{
bool dll_reload = false;
bool dump_reload = false;

int evt;
while (watcher_events.TryDequeue(out evt))
{
if (evt == 1) dll_reload = true;
else if (evt == 2) dump_reload = true;
}

if (dll_reload) this.Invoke((MethodInvoker)delegate { ReloadGsdxDlls(); });
if (dump_reload) this.Invoke((MethodInvoker)delegate { ReloadGsdxDumps(); });

Thread.Sleep(1000);
}
}


private void OnDllDirChange(object source, FileSystemEventArgs e)
{
watcher_events.Enqueue(1);
//ReloadGsdxDlls();
}

private void OnDumpDirChange(object source, FileSystemEventArgs e)
{
watcher_events.Enqueue(2);
//ReloadGsdxDumps();
} }


private static void BindListControl<TModel, TElement>(ListControl lb, TModel model, Func<TModel, BindingList<TElement>> collectionAccessor, Expression<Func<TElement, string>> displayTextAccessor, Expression<Func<TModel, int>> selectedIndexAccessor) private static void BindListControl<TModel, TElement>(ListControl lb, TModel model, Func<TModel, BindingList<TElement>> collectionAccessor, Expression<Func<TElement, string>> displayTextAccessor, Expression<Func<TModel, int>> selectedIndexAccessor)
Expand Down Expand Up @@ -151,6 +199,31 @@ private void ReloadGsdxDlls()


Settings.GSDXDir = gsdxFolder.FullName; Settings.GSDXDir = gsdxFolder.FullName;
_internalLogger.Information("Completed GSdx Loading Procedures"); _internalLogger.Information("Completed GSdx Loading Procedures");

string[] paths = { "", "\\plugins", "\\dll", "\\dlls" };

foreach (FileSystemWatcher w in dll_watcher)
{
w.EnableRaisingEvents = false;
w.Dispose();
}

dll_watcher.Clear();

for (int i = 0; i < 4; i++)
{
try
{
FileSystemWatcher w = new FileSystemWatcher(Settings.GSDXDir + paths[i], "*.dll");
//w.Changed += OnDllDirChange;
w.Created += OnDllDirChange;
w.Deleted += OnDllDirChange;
w.Renamed += OnDllDirChange;
w.EnableRaisingEvents = true;
dll_watcher.Add(w);
}
catch { }
}
} }


private void ReloadGsdxDumps() private void ReloadGsdxDumps()
Expand All @@ -165,6 +238,31 @@ private void ReloadGsdxDumps()


Settings.DumpDir = dumpFolder.FullName; Settings.DumpDir = dumpFolder.FullName;
_internalLogger.Information("...Completed GSdx Dump Loading Procedures"); _internalLogger.Information("...Completed GSdx Dump Loading Procedures");

string[] paths = { "", "\\dumps", "\\gsdumps" };

foreach (FileSystemWatcher w in dump_watcher)
{
w.EnableRaisingEvents = false;
w.Dispose();
}

dump_watcher.Clear();

for (int i = 0; i < 3; i++)
{
try
{
FileSystemWatcher w = new FileSystemWatcher(Settings.DumpDir + paths[i], "*.gs");
//w.Changed += OnDumpDirChange;
w.Created += OnDumpDirChange;
w.Deleted += OnDumpDirChange;
w.Renamed += OnDumpDirChange;
w.EnableRaisingEvents = true;
dump_watcher.Add(w);
}
catch { }
}
} }


private void GSDumpGUI_Load(object sender, EventArgs e) private void GSDumpGUI_Load(object sender, EventArgs e)
Expand Down
107 changes: 52 additions & 55 deletions tools/GSDumpGUI/Library/GSDXWrapper.cs
Expand Up @@ -101,62 +101,58 @@ public void Load(String DLL)
Unload(); Unload();


string dir = DLL; string dir = DLL;
while (true) dir = Path.GetDirectoryName(dir);
{ if (dir == null) return;
dir = Path.GetDirectoryName(dir);
if (dir == null)
break;
Directory.SetCurrentDirectory(dir);
IntPtr hmod = NativeMethods.LoadLibrary(DLL);
if (hmod.ToInt64() > 0)
{
DLLAddr = hmod;

IntPtr funcaddrLibName = NativeMethods.GetProcAddress(hmod, "PS2EgetLibName");
IntPtr funcaddrConfig = NativeMethods.GetProcAddress(hmod, "GSconfigure");

IntPtr funcaddrGIF = NativeMethods.GetProcAddress(hmod, "GSgifTransfer");
IntPtr funcaddrGIF1 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer1");
IntPtr funcaddrGIF2 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer2");
IntPtr funcaddrGIF3 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer3");
IntPtr funcaddrVSync = NativeMethods.GetProcAddress(hmod, "GSvsync");
IntPtr funcaddrSetBaseMem = NativeMethods.GetProcAddress(hmod, "GSsetBaseMem");
IntPtr funcaddrGSReset = NativeMethods.GetProcAddress(hmod, "GSreset");
IntPtr funcaddrOpen = NativeMethods.GetProcAddress(hmod, "GSopen");
IntPtr funcaddrSetCRC = NativeMethods.GetProcAddress(hmod, "GSsetGameCRC");
IntPtr funcaddrClose = NativeMethods.GetProcAddress(hmod, "GSclose");
IntPtr funcaddrShutdown = NativeMethods.GetProcAddress(hmod, "GSshutdown");
IntPtr funcaddrFreeze = NativeMethods.GetProcAddress(hmod, "GSfreeze");
IntPtr funcaddrGSreadFIFO2 = NativeMethods.GetProcAddress(hmod, "GSreadFIFO2");
IntPtr funcaddrinit = NativeMethods.GetProcAddress(hmod, "GSinit");
IntPtr funcmakeSnapshot = NativeMethods.GetProcAddress(hmod, "GSmakeSnapshot");

if (!((funcaddrConfig.ToInt64() > 0) && (funcaddrLibName.ToInt64() > 0) && (funcaddrGIF.ToInt64() > 0)))
{
break;
}


gsConfigure = (GSConfigure) Marshal.GetDelegateForFunctionPointer(funcaddrConfig, typeof(GSConfigure)); Directory.SetCurrentDirectory(dir);
PsegetLibName = (PSEgetLibName) Marshal.GetDelegateForFunctionPointer(funcaddrLibName, typeof(PSEgetLibName)); System.Diagnostics.Trace.WriteLine("LoadLibrary: " + DLL);

IntPtr hmod = NativeMethods.LoadLibrary(DLL);
this.GSgifTransfer = (GSgifTransfer) Marshal.GetDelegateForFunctionPointer(funcaddrGIF, typeof(GSgifTransfer)); if (hmod != IntPtr.Zero)
this.GSgifTransfer1 = (GSgifTransfer1) Marshal.GetDelegateForFunctionPointer(funcaddrGIF1, typeof(GSgifTransfer1)); {
this.GSgifTransfer2 = (GSgifTransfer2) Marshal.GetDelegateForFunctionPointer(funcaddrGIF2, typeof(GSgifTransfer2)); DLLAddr = hmod;
this.GSgifTransfer3 = (GSgifTransfer3) Marshal.GetDelegateForFunctionPointer(funcaddrGIF3, typeof(GSgifTransfer3));
this.GSVSync = (GSVSync) Marshal.GetDelegateForFunctionPointer(funcaddrVSync, typeof(GSVSync)); IntPtr funcaddrLibName = NativeMethods.GetProcAddress(hmod, "PS2EgetLibName");
this.GSsetBaseMem = (GSsetBaseMem) Marshal.GetDelegateForFunctionPointer(funcaddrSetBaseMem, typeof(GSsetBaseMem)); IntPtr funcaddrConfig = NativeMethods.GetProcAddress(hmod, "GSconfigure");
this.GSopen = (GSopen) Marshal.GetDelegateForFunctionPointer(funcaddrOpen, typeof(GSopen));
this.GSsetGameCRC = (GSsetGameCRC) Marshal.GetDelegateForFunctionPointer(funcaddrSetCRC, typeof(GSsetGameCRC)); IntPtr funcaddrGIF = NativeMethods.GetProcAddress(hmod, "GSgifTransfer");
this.GSclose = (GSclose) Marshal.GetDelegateForFunctionPointer(funcaddrClose, typeof(GSclose)); IntPtr funcaddrGIF1 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer1");
this.GSshutdown = (GSshutdown) Marshal.GetDelegateForFunctionPointer(funcaddrShutdown, typeof(GSshutdown)); IntPtr funcaddrGIF2 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer2");
this.GSfreeze = (GSfreeze) Marshal.GetDelegateForFunctionPointer(funcaddrFreeze, typeof(GSfreeze)); IntPtr funcaddrGIF3 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer3");
this.GSreset = (GSreset) Marshal.GetDelegateForFunctionPointer(funcaddrGSReset, typeof(GSreset)); IntPtr funcaddrVSync = NativeMethods.GetProcAddress(hmod, "GSvsync");
this.GSreadFIFO2 = (GSreadFIFO2) Marshal.GetDelegateForFunctionPointer(funcaddrGSreadFIFO2, typeof(GSreadFIFO2)); IntPtr funcaddrSetBaseMem = NativeMethods.GetProcAddress(hmod, "GSsetBaseMem");
this.GSinit = (GSinit) Marshal.GetDelegateForFunctionPointer(funcaddrinit, typeof(GSinit)); IntPtr funcaddrGSReset = NativeMethods.GetProcAddress(hmod, "GSreset");
this.GSmakeSnapshot = (GSmakeSnapshot)Marshal.GetDelegateForFunctionPointer(funcmakeSnapshot, typeof(GSmakeSnapshot)); IntPtr funcaddrOpen = NativeMethods.GetProcAddress(hmod, "GSopen");

IntPtr funcaddrSetCRC = NativeMethods.GetProcAddress(hmod, "GSsetGameCRC");
Loaded = true; IntPtr funcaddrClose = NativeMethods.GetProcAddress(hmod, "GSclose");
} IntPtr funcaddrShutdown = NativeMethods.GetProcAddress(hmod, "GSshutdown");
IntPtr funcaddrFreeze = NativeMethods.GetProcAddress(hmod, "GSfreeze");
IntPtr funcaddrGSreadFIFO2 = NativeMethods.GetProcAddress(hmod, "GSreadFIFO2");
IntPtr funcaddrinit = NativeMethods.GetProcAddress(hmod, "GSinit");
IntPtr funcmakeSnapshot = NativeMethods.GetProcAddress(hmod, "GSmakeSnapshot");

if (!((funcaddrConfig.ToInt64() > 0) && (funcaddrLibName.ToInt64() > 0) && (funcaddrGIF.ToInt64() > 0)))
throw new InvalidGSPlugin("");

gsConfigure = (GSConfigure) Marshal.GetDelegateForFunctionPointer(funcaddrConfig, typeof(GSConfigure));
PsegetLibName = (PSEgetLibName) Marshal.GetDelegateForFunctionPointer(funcaddrLibName, typeof(PSEgetLibName));

this.GSgifTransfer = (GSgifTransfer) Marshal.GetDelegateForFunctionPointer(funcaddrGIF, typeof(GSgifTransfer));
this.GSgifTransfer1 = (GSgifTransfer1) Marshal.GetDelegateForFunctionPointer(funcaddrGIF1, typeof(GSgifTransfer1));
this.GSgifTransfer2 = (GSgifTransfer2) Marshal.GetDelegateForFunctionPointer(funcaddrGIF2, typeof(GSgifTransfer2));
this.GSgifTransfer3 = (GSgifTransfer3) Marshal.GetDelegateForFunctionPointer(funcaddrGIF3, typeof(GSgifTransfer3));
this.GSVSync = (GSVSync) Marshal.GetDelegateForFunctionPointer(funcaddrVSync, typeof(GSVSync));
this.GSsetBaseMem = (GSsetBaseMem) Marshal.GetDelegateForFunctionPointer(funcaddrSetBaseMem, typeof(GSsetBaseMem));
this.GSopen = (GSopen) Marshal.GetDelegateForFunctionPointer(funcaddrOpen, typeof(GSopen));
this.GSsetGameCRC = (GSsetGameCRC) Marshal.GetDelegateForFunctionPointer(funcaddrSetCRC, typeof(GSsetGameCRC));
this.GSclose = (GSclose) Marshal.GetDelegateForFunctionPointer(funcaddrClose, typeof(GSclose));
this.GSshutdown = (GSshutdown) Marshal.GetDelegateForFunctionPointer(funcaddrShutdown, typeof(GSshutdown));
this.GSfreeze = (GSfreeze) Marshal.GetDelegateForFunctionPointer(funcaddrFreeze, typeof(GSfreeze));
this.GSreset = (GSreset) Marshal.GetDelegateForFunctionPointer(funcaddrGSReset, typeof(GSreset));
this.GSreadFIFO2 = (GSreadFIFO2) Marshal.GetDelegateForFunctionPointer(funcaddrGSreadFIFO2, typeof(GSreadFIFO2));
this.GSinit = (GSinit) Marshal.GetDelegateForFunctionPointer(funcaddrinit, typeof(GSinit));
this.GSmakeSnapshot = (GSmakeSnapshot)Marshal.GetDelegateForFunctionPointer(funcmakeSnapshot, typeof(GSmakeSnapshot));

Loaded = true;
} }


if (!Loaded) if (!Loaded)
Expand All @@ -178,6 +174,7 @@ public void Load(String DLL)


public void Unload() public void Unload()
{ {
System.Diagnostics.Trace.WriteLine("FreeLibrary: " + DLL);
NativeMethods.FreeLibrary(DLLAddr); NativeMethods.FreeLibrary(DLLAddr);
Loaded = false; Loaded = false;
} }
Expand Down

0 comments on commit bd6261e

Please sign in to comment.