Skip to content

Commit

Permalink
Many debug info for problem users
Browse files Browse the repository at this point in the history
  • Loading branch information
ClusterM committed Jan 16, 2017
1 parent 2e07a79 commit 2623e97
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 69 deletions.
3 changes: 3 additions & 0 deletions ConfigIni.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -19,6 +20,7 @@ public class ConfigIni

public static void Load()
{
Debug.WriteLine("Loading config");
var fileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), ConfigFile);
if (File.Exists(fileName))
{
Expand Down Expand Up @@ -67,6 +69,7 @@ public static void Load()

public static void Save()
{
Debug.WriteLine("Saving config");
var configLines = new List<string>();
configLines.Add("[Config]");
configLines.Add(string.Format("SelectedGames={0}", SelectedGames));
Expand Down
51 changes: 10 additions & 41 deletions FelLib/Fel.cs
@@ -1,7 +1,7 @@
using MadWizard.WinUSBNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -65,16 +65,12 @@ public static bool DeviceExists(UInt16 vid, UInt16 pid)
try
{
fel.Open(vid, pid);
#if DEBUG
DebugLog("Device detection successful");
#endif
Debug.WriteLine("Device detection successful");
return true;
}
catch (Exception ex)
{
#if DEBUG
DebugLog("Device detection error: " + ex.Message + ex.StackTrace);
#endif
Debug.WriteLine("Device detection error: " + ex.Message + ex.StackTrace);
return false;
}
finally
Expand All @@ -88,36 +84,26 @@ public void Open(UInt16 vid, UInt16 pid)
this.vid = vid;
this.pid = pid;
Close();
#if DEBUG
DebugLog("Trying to open device...");
#endif
Debug.WriteLine("Trying to open device...");
device = USBDevice.GetSingleDevice(vid, pid);
if (device == null) throw new FelException("Device with such VID and PID not found");
#if DEBUG
DebugLog("Checking USB endpoints...");
#endif
Debug.WriteLine("Checking USB endpoints...");
foreach (var pipe in device.Pipes)
{
if (pipe.IsIn)
{
inEndp = pipe.Address;
#if DEBUG
DebugLog("IN endpoint found: " + inEndp);
#endif
Debug.WriteLine("IN endpoint found: " + inEndp);
}
else
{
outEndp = pipe.Address;
#if DEBUG
DebugLog("Out endpoint found: " + outEndp);
#endif
Debug.WriteLine("Out endpoint found: " + outEndp);
}
}
device.Pipes[inEndp].Policy.PipeTransferTimeout = ReadTimeout;
device.Pipes[outEndp].Policy.PipeTransferTimeout = WriteTimeout;
#if DEBUG
DebugLog("Trying to verify device");
#endif
Debug.WriteLine("Trying to verify device");
if (VerifyDevice().Board != 0x00166700) throw new FelException("Invalid board ID: " + VerifyDevice().Board);
}
public void Close()
Expand All @@ -143,19 +129,14 @@ public void Close()

private void WriteToUSB(byte[] buffer)
{
#if DEBUG
DebugLog("-> " + BitConverter.ToString(buffer));
#endif

Debug.WriteLine("-> " + BitConverter.ToString(buffer));
device.Pipes[outEndp].Write(buffer);
}

private int ReadFromUSB(byte[] buffer, int offset, int length)
{
var data = device.Pipes[inEndp].Read(buffer, offset, length);
#if DEBUG
DebugLog("<- " + BitConverter.ToString(buffer));
#endif
Debug.WriteLine("<- " + BitConverter.ToString(buffer));
return data;
}
private byte[] ReadFromUSB(UInt32 length)
Expand Down Expand Up @@ -389,17 +370,5 @@ public void RunUbootCmd(string command, bool noreturn = false, OnFelProgress cal
}
}
}

#if DEBUG
public static void DebugLog(string text)
{
Console.WriteLine(text);
try
{
File.AppendAllText("debug.txt", DateTime.Now + ": " + text + "\r\n");
}
catch { }
}
#endif
}
}
10 changes: 3 additions & 7 deletions FelLib/WinUSBNet/API/DeviceManagement.cs
Expand Up @@ -14,7 +14,7 @@
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using com.clusterrr.FelLib;
using System.Diagnostics;

namespace MadWizard.WinUSBNet.API
{
Expand Down Expand Up @@ -239,18 +239,14 @@ public static DeviceDetails[] FindDevices(UInt16 vid, UInt16 pid)
// Get the String containing the devicePathName.
try
{
#if DEBUG
Fel.DebugLog("Trying to parse device: " + pathName);
#endif
Debug.WriteLine("Trying to parse device: " + pathName);
DeviceDetails details = GetDeviceDetails(pathName, deviceInfoSet, da);
if (details.VID == vid && details.PID == pid)
deviceList.Add(details);
}
catch (APIException ex)
{
#if DEBUG
Fel.DebugLog("Can't parse this device: " + ex.Message + ex.StackTrace);
#endif
Debug.WriteLine("Can't parse this device: " + ex.Message + ex.StackTrace);
continue;
}
}
Expand Down
34 changes: 24 additions & 10 deletions MainForm.cs
Expand Up @@ -55,20 +55,29 @@ public partial class MainForm : Form

public MainForm()
{
InitializeComponent();
ConfigIni.Load();
BaseDir = Path.GetDirectoryName(Application.ExecutablePath);
GamesDir = Path.Combine(BaseDir, "games");
KernelDump = Path.Combine(Path.Combine(BaseDir, "dump"), "kernel.img");
useExtendedFontToolStripMenuItem.Checked = ConfigIni.UseFont;
LoadGames();
LoadHidden();
LoadPresets();
new Thread(NesGame.LoadCache).Start();
try
{
InitializeComponent();
ConfigIni.Load();
BaseDir = Path.GetDirectoryName(Application.ExecutablePath);
GamesDir = Path.Combine(BaseDir, "games");
KernelDump = Path.Combine(Path.Combine(BaseDir, "dump"), "kernel.img");
useExtendedFontToolStripMenuItem.Checked = ConfigIni.UseFont;
LoadGames();
LoadHidden();
LoadPresets();
new Thread(NesGame.LoadCache).Start();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
MessageBox.Show(this, "Critical error: "+ex.Message+ex.StackTrace, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

public void LoadGames()
{
Debug.WriteLine("Loading games");
var selected = ConfigIni.SelectedGames.Split(';');
Directory.CreateDirectory(GamesDir);
var gameDirs = Directory.GetDirectories(GamesDir);
Expand All @@ -82,6 +91,7 @@ public void LoadGames()
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
MessageBox.Show(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
continue;
}
Expand Down Expand Up @@ -332,13 +342,15 @@ private void SaveConfig()
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Debug.WriteLine("Closing main form");
SaveConfig();
}

Expand Down Expand Up @@ -386,6 +398,7 @@ void AddGames(string[] files)
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
MessageBox.Show(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
continue;
}
Expand Down Expand Up @@ -711,6 +724,7 @@ private void deleteGame(int pos)
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
MessageBox.Show(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Expand Down
9 changes: 8 additions & 1 deletion NesGame.cs
Expand Up @@ -3,6 +3,7 @@
using nQuant;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
Expand Down Expand Up @@ -172,6 +173,7 @@ public NesGame(string gamesDirectory, string nesFileName, bool ignoreMapper = fa

public void Save()
{
Debug.WriteLine("Saving game " + Code);
File.WriteAllText(ConfigPath, string.Format(
"[Desktop Entry]\n" +
"Type=Application\n" +
Expand Down Expand Up @@ -311,6 +313,7 @@ public static void LoadCache()
try
{
var xmlDataBasePath = Path.Combine(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "data"), "nescarts.xml");
Debug.WriteLine("Loading " + xmlDataBasePath);

if (File.Exists(xmlDataBasePath))
{
Expand Down Expand Up @@ -340,8 +343,12 @@ public static void LoadCache()
};
}
}
Debug.WriteLine(string.Format("XML loading done, {0} roms total", gameInfoCache.Count));
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
}
catch { }
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions Program.cs
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
Expand All @@ -11,21 +12,33 @@ static class Program
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
#if DEBUG
AllocConsole();
Stream logFile = File.Create("debuglog.txt");
Debug.Listeners.Add(new TextWriterTraceListener(logFile));
Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));
Debug.AutoFlush = true;
#endif
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "hakchi2", out createdNew))
{
if (createdNew)
{
Debug.WriteLine("Starting...");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
Debug.WriteLine("Done.");
}
else
{
Expand Down
24 changes: 14 additions & 10 deletions WorkerForm.cs
Expand Up @@ -172,17 +172,13 @@ void ShowError(Exception ex)
Invoke(new Action<Exception>(ShowError), new object[] { ex });
return;
}
#if DEBUG
var stackTrace = ex.StackTrace;
#else
var stackTrace = "";
#endif
Debug.WriteLine(ex.Message + ex.StackTrace);
if (ex is GameGenieFormatException || ex is GameGenieNotFoundException)
MessageBox.Show(this, ex.Message + stackTrace, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
else if (ex is MadWizard.WinUSBNet.USBException)
MessageBox.Show(this, ex.Message + stackTrace + "\r\n" + Resources.PleaseTryAgain + "\r\n" + Resources.PleaseTryAgainUSB, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(this, ex.Message + "\r\n" + Resources.PleaseTryAgain + "\r\n" + Resources.PleaseTryAgainUSB, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show(this, ex.Message + stackTrace + "\r\n" + Resources.PleaseTryAgain, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(this, ex.Message + "\r\n" + Resources.PleaseTryAgain, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
thread = null;
Close();
}
Expand Down Expand Up @@ -468,7 +464,9 @@ private byte[] CreatePatchedKernel()
throw new Exception("Can't rebuild kernel");

var result = File.ReadAllBytes(kernelPatched);
#if !DEBUG
Directory.Delete(tempDirectory, true);
#endif
if (result.Length > Fel.kernel_max_size) throw new Exception("Kernel is too big");
return result;
}
Expand All @@ -492,15 +490,21 @@ private bool ExecuteTool(string tool, string args, out byte[] output, string dir
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(1251);
process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(866);
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
Debug.WriteLine("Executing: " + fileName);
Debug.WriteLine("Arguments: " + args);
Debug.WriteLine("Directory: " + directory);
process.Start();
string outputStr = process.StandardOutput.ReadToEnd();
string errorStr = process.StandardError.ReadToEnd();
process.WaitForExit();
output = Encoding.GetEncoding(1251).GetBytes(outputStr);
output = Encoding.GetEncoding(866).GetBytes(outputStr);
Debug.WriteLineIf(outputStr.Length > 0 && outputStr.Length < 300, "Output:\r\n" + outputStr);
Debug.WriteLineIf(errorStr.Length > 0, "Errors:\r\n" + errorStr);
Debug.WriteLine("Exit code: " + process.ExitCode);
return process.ExitCode == 0;
}

Expand Down

0 comments on commit 2623e97

Please sign in to comment.