Skip to content

Commit 2ea4e56

Browse files
committed
- UI tweak: clear up wording on the 'Import Keys..' dialog
- UX tweak: friendlier error to handle key files with malformed lines (this may be common) - improved logging, exception-handling and msgboxes - improved build/staging consistency - remove redundant copies of BMS-Full*.key
1 parent fb1419a commit 2ea4e56

13 files changed

+185
-3015
lines changed

Falcon BMS Alternative Launcher/App.xaml.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public partial class App
1313
public App()
1414
{
1515
Current.DispatcherUnhandledException += App_DispatcherUnhandledException;
16-
Diagnostics.WriteLogFile(false, "Log Start");
17-
Diagnostics.Log("Application Initialization completed successfully.", Diagnostics.LogLevels.Info);
16+
Diagnostics.Log("Application Initialization starting.");
1817
}
1918

2019
private static void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
@@ -25,23 +24,20 @@ private static void App_DispatcherUnhandledException(object sender, DispatcherUn
2524
MessageBoxButton.OK, MessageBoxImage.Information);
2625

2726
e.Handled = true;
27+
return;
2828
}
2929

30-
else
30+
if (Debugger.IsAttached)
3131
{
32-
Debug.WriteLine(e.ToString());
33-
34-
// Skip this step if debugging so the debugger can catch errors.
35-
if (Debugger.IsAttached) return;
36-
37-
MessageBox.Show("An unknown error has occured. Contact support if this problem persists.", "Error",
38-
MessageBoxButton.OK, MessageBoxImage.Error);
32+
Debug.WriteLine(e.Exception.ToString());
33+
Debugger.Break();
34+
}
3935

40-
Diagnostics.Log(e.ToString());
41-
Diagnostics.WriteLogFile();
36+
Diagnostics.Log(e.Exception);
37+
Diagnostics.ShowErrorMsgbox(e.Exception);
4238

43-
e.Handled = true;
44-
}
39+
e.Handled = true;
40+
return;
4541
}
4642
}
4743
}

Falcon BMS Alternative Launcher/BMS - Full-F15ABCD.key

Lines changed: 0 additions & 1443 deletions
This file was deleted.

Falcon BMS Alternative Launcher/BMS - Full.key

Lines changed: 0 additions & 1402 deletions
This file was deleted.
Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Text;
4-
using System.Windows.Forms;
5+
using System.Windows;
56

67
namespace FalconBMS.Launcher
78
{
@@ -15,94 +16,68 @@ public enum LogLevels
1516
Exception,
1617
}
1718

18-
#region Fields
19+
static StreamWriter logWriter;
1920

20-
internal static string logData;
21-
22-
public static readonly string AppDataPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create)}" + "\\Benchmark_Sims";
21+
static Diagnostics()
22+
{
23+
string appDataPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create)}";
24+
string logFileDirectory = Path.Combine(appDataPath, @"Benchmark_Sims");
2325

24-
public static readonly string LogFilePath = $"{AppDataPath}" + "\\Launcher_Log.txt";
26+
if (false == Directory.Exists(logFileDirectory))
27+
Directory.CreateDirectory(logFileDirectory);
2528

26-
#endregion
29+
string logFilePath = Path.Combine(logFileDirectory, @"Launcher_Log.txt");
2730

28-
#region Methods
31+
logWriter = Utils.CreateUtf8TextWihoutBom(logFilePath, append:false);
32+
logWriter.AutoFlush = true;
33+
}
2934

3035
public static void Log(string message)
3136
{
32-
string message2 = $"[{DateTime.Now}] [INFO] :: {message}";
33-
logData = logData + message2 + "\r\n";
34-
//WriteLogFile(logData);
37+
Log(message, LogLevels.Info);
3538
}
3639

3740
public static void Log(string message, LogLevels logLevel)
3841
{
39-
string message2 = $"[{DateTime.Now}] [{GetLogLevelText(logLevel)}] :: {message}";
40-
logData = logData + message2 + "\r\n";
41-
//WriteLogFile(logData);
42+
string level = GetLogLevelText(logLevel);
43+
44+
Debug.WriteLine($"LOG_{level}: " + message);
45+
46+
string message2 = $"[{DateTime.Now}] [{level}] :: {message}";
47+
logWriter.WriteLine(message2);
4248
}
4349

4450
public static void Log(Exception exception)
4551
{
52+
Debug.WriteLine("LOG_EXCEPTION: " + exception.Message);
53+
4654
string message2 = $"[{DateTime.Now}] [EXCEPTION] {exception.Message}:: \r\n \r\n Source: {exception.Source} \r\n Target Site: {exception.TargetSite} \r\n Message: {exception.Message} \r\n Details: {exception.InnerException} \r\n \r\n Exception Data: {exception.Data} \r\n \r\n Stack Trace: {exception.StackTrace} \r\n ============ \r\n";
47-
logData = logData + message2 + "\r\n";
48-
//WriteLogFile(logData);
55+
logWriter.WriteLine(message2);
4956
}
5057

51-
public static void Log(Exception exception, string message)
58+
public static void ShowErrorMsgbox(Exception ex)
5259
{
53-
string message2 = $"[{DateTime.Now}] [EXCEPTION] {exception.Message} \r\n {message} \r\n Source: {exception.Source} \r\n Target Site: {exception.TargetSite} \r\n Message: {exception.Message} \r\n Details: {exception.InnerException} \r\n \r\n Exception Data: {exception.Data} \r\n \r\n Stack Trace: {exception.StackTrace} \r\n ============ \r\n";
54-
logData = logData + message2 + "\r\n";
55-
//WriteLogFile(logData);
60+
ShowErrorMsgbox(
61+
"An unknown problem occurred, check error log for details:\r\n\r\n"+
62+
"%LocalAppData%\\Benchmark_Sims\\Launcher_Log.txt", ex);
5663
}
5764

58-
public static void WriteLogFile()
65+
public static void ShowErrorMsgbox(string message, Exception ex = null)
5966
{
60-
WriteLogFile(false, logData);
61-
}
67+
if (ex != null)
68+
message += "\r\n\r\n" + ex.GetType() + "\r\n" + ex.Message;
6269

63-
public static void WriteLogFile(Exception e)
64-
{
65-
WriteLogFile(false, logData + $"{e}");
66-
MessageBox.Show("Error Log Saved to " + LogFilePath);
70+
MessageBox.Show(message, "Error",
71+
MessageBoxButton.OK, MessageBoxImage.Error);
6772
}
6873

69-
public static void WriteLogFile(bool append, Exception e)
74+
public static void FinalizeLogfile()
7075
{
71-
WriteLogFile(append, $"{e}");
72-
MessageBox.Show("Error Log Saved to " + LogFilePath);
73-
}
74-
75-
public static void WriteLogFile(bool append, string args)
76-
{
77-
try
78-
{
79-
if (!Directory.Exists(AppDataPath))
80-
{
81-
Directory.CreateDirectory(AppDataPath);
82-
}
83-
84-
DirectoryInfo dirInfo = new DirectoryInfo(AppDataPath);
85-
dirInfo.Attributes = dirInfo.Attributes & ~FileAttributes.ReadOnly;
86-
87-
if (!File.Exists(LogFilePath))
88-
{
89-
FileStream fs = File.Create(LogFilePath);
90-
fs.Close();
91-
File.SetCreationTimeUtc(LogFilePath, DateTime.UtcNow);
92-
}
93-
94-
StreamWriter file = new StreamWriter(LogFilePath, append, Encoding.Default);
95-
96-
file.Write(args);
97-
file.Close();
98-
}
99-
catch
100-
{
101-
MessageBox.Show(args);
102-
}
76+
logWriter.Close();
77+
logWriter = null;
10378
}
10479

105-
internal static string GetLogLevelText(LogLevels logLevel)
80+
private static string GetLogLevelText(LogLevels logLevel)
10681
{
10782
switch (logLevel)
10883
{
@@ -115,10 +90,9 @@ internal static string GetLogLevelText(LogLevels logLevel)
11590
case LogLevels.Exception:
11691
return "EXCEPTION";
11792
default:
118-
throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null);
93+
throw new InvalidProgramException("GetLogLevelText");
11994
}
12095
}
12196

122-
#endregion
12397
}
12498
}

Falcon BMS Alternative Launcher/Falcon BMS Alternative Launcher.csproj

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
</PropertyGroup>
4545
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
4646
<PlatformTarget>x86</PlatformTarget>
47-
<DebugType>none</DebugType>
47+
<DebugSymbols>true</DebugSymbols>
48+
<DebugType>pdbonly</DebugType>
4849
<Optimize>true</Optimize>
49-
<OutputPath>bin\Release\</OutputPath>
50+
<OutputPath>..\bin\Release\</OutputPath>
5051
<DefineConstants>TRACE</DefineConstants>
5152
<ErrorReport>prompt</ErrorReport>
5253
<WarningLevel>4</WarningLevel>
@@ -212,12 +213,6 @@
212213
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
213214
</EmbeddedResource>
214215
<None Include="app.manifest" />
215-
<Content Include="BMS - Full-F15ABCD.key">
216-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
217-
</Content>
218-
<Content Include="BMS - Full.key">
219-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
220-
</Content>
221216
<None Include="packages.config">
222217
<SubType>Designer</SubType>
223218
</None>

Falcon BMS Alternative Launcher/Input/DeviceControl.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public void SaveXml()
191191
}
192192
catch (Exception ex)
193193
{
194-
Diagnostics.WriteLogFile(ex);
194+
Diagnostics.Log(ex);
195+
Diagnostics.ShowErrorMsgbox(ex);
195196
}
196197
finally
197198
{

Falcon BMS Alternative Launcher/Input/JoyAssgn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public int JoyAxisState(int joyAxisNumber)
196196
}
197197
catch
198198
{
199-
System.Diagnostics.Debug.WriteLine("(Catching exception from hwDevice.CurrentJoystickState.)");
199+
Diagnostics.Log("JoyAssgn: catching exception from hwDevice.CurrentJoystickState.");
200200
return 0;
201201
}
202202
}

Falcon BMS Alternative Launcher/Input/KeyFile.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,41 @@ public KeyFile(string filename)
6060
return;
6161
}
6262

63+
public static bool ValidateKeyfileLines(string filename)
64+
{
65+
if (false == File.Exists(filename)) return false;
66+
67+
List<string> errors = new List<string>(50);
68+
69+
using (StreamReader reader = File.OpenText(filename))
70+
{
71+
int lineNum = 0;
72+
while (true)
73+
{
74+
++lineNum;
75+
76+
string line = reader.ReadLine();
77+
if (line == null) break;
78+
79+
if (string.IsNullOrWhiteSpace(line))
80+
continue;
81+
if (RegexFactory.LineComment.IsMatch(line))
82+
continue;
83+
if (RegexFactory.ButtonOrHatBindingLine.IsMatch(line))
84+
continue;
85+
if (RegexFactory.KeyBindingLine.IsMatch(line))
86+
continue;
87+
88+
// Unrecognized line.
89+
string err = $"Unrecognized line #{lineNum}: " + line;
90+
Diagnostics.Log(err, Diagnostics.LogLevels.Warning);
91+
92+
errors.Add(err);
93+
}
94+
}
95+
return (errors.Count == 0);
96+
}
97+
6398
private static string ParseCategoryHeaderLabel(string line)
6499
{
65100
Match m = RegexFactory.CategoryHeaderLine.Match(line);

Falcon BMS Alternative Launcher/Program.cs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,28 @@ public class Program
1818
[STAThread]
1919
public static void Main()
2020
{
21-
// Set cwd to the EXE location.
22-
string thisExe = Assembly.GetExecutingAssembly().Location;
23-
string thisExeDir = Path.GetDirectoryName(thisExe);
24-
Environment.CurrentDirectory = thisExeDir;
25-
26-
// Launch the WPF app.
27-
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
28-
App.Main();
29-
}
30-
31-
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
32-
{
33-
if (e.ExceptionObject is NotImplementedException)
21+
try
3422
{
35-
Exception ex = e.ExceptionObject as Exception;
36-
37-
MessageBox.Show("This feature has not yet been implemented.", "Feature not Implemented",
38-
MessageBoxButton.OK, MessageBoxImage.Information);
39-
40-
41-
//ex.Handled = true;
23+
// Set cwd to the EXE location.
24+
string thisExe = Assembly.GetExecutingAssembly().Location;
25+
string thisExeDir = Path.GetDirectoryName(thisExe);
26+
Environment.CurrentDirectory = thisExeDir;
27+
28+
// Launch the WPF app.
29+
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
30+
App.Main();
4231
}
43-
44-
else
32+
catch (Exception ex)
4533
{
46-
// Skip this step if debugging so the debugger can catch errors.
47-
if (Debugger.IsAttached) return;
48-
49-
MessageBox.Show("An unknown error has occured. Contact support if this problem persists.", "Error",
50-
MessageBoxButton.OK, MessageBoxImage.Error);
51-
Diagnostics.Log(e.ExceptionObject.ToString());
52-
Diagnostics.WriteLogFile();
53-
54-
//e.Handled = true;
34+
Diagnostics.Log(ex);
35+
Diagnostics.ShowErrorMsgbox(ex);
5536
}
56-
}
57-
58-
private static void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
59-
{
60-
37+
finally
38+
{
39+
Diagnostics.Log("Process exiting - closing logfile.");
40+
Diagnostics.FinalizeLogfile();
41+
}
42+
return;
6143
}
6244

6345
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)

Falcon BMS Alternative Launcher/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@
5050
// 既定値にすることができます:
5151
//[assembly: AssemblyVersion("2023.10.19.3")]
5252
//[assembly: AssemblyFileVersion("2023.10.19.3")]
53-
[assembly: AssemblyVersion("2.4.0.5")]
54-
[assembly: AssemblyFileVersion("2.4.0.5")]
53+
[assembly: AssemblyVersion("2.4.0.6")]
54+
[assembly: AssemblyFileVersion("2.4.0.6")]

Falcon BMS Alternative Launcher/Windows/KeyMappingWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void KeyMappingtimerCode(object sender, EventArgs e)
125125
}
126126
catch (Exception ex)
127127
{
128-
Diagnostics.WriteLogFile(ex);
128+
Diagnostics.Log(ex);
129129
}
130130
}
131131

0 commit comments

Comments
 (0)