Skip to content

Commit

Permalink
v0.9.8a release
Browse files Browse the repository at this point in the history
  • Loading branch information
sfryers authored Mar 16, 2024
1 parent 66f37df commit 359624d
Show file tree
Hide file tree
Showing 43 changed files with 7,516 additions and 7,046 deletions.
233 changes: 132 additions & 101 deletions src/MT32Editor/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,38 @@
internal static class ConfigFile
{
// MT32Edit: ConfigFile class (static)
// S.Fryers Feb 2024

private const string MIDI_IN = "Midi In";
private const string MIDI_OUT = "Midi Out";
private const string SHOW_CONSOLE = "Show Console";
private const string VERBOSE_MESSAGES = "Verbose messages";
private const string HARDWARE_CONNECTED = "Hardware MT-32 connected";
private const string ALLOW_RESET = "Allow MT-32 reset";
private const string SEND_MESSAGES = "Send messages to MT-32";
private const string IGNORE_SYSTEM_ON_LOAD = "Ignore system area on SysEx load";
private const string EXCLUDE_SYSTEM_ON_SAVE = "Exclude system area on SysEx save";
private const string AUTOSAVE = "Autosave every 5 mins";
// S.Fryers Mar 2024

private const string COMMENT_CHARACTER = "#";
private const string TEXT_MIDI_IN = "Midi In";
private const string TEXT_MIDI_OUT = "Midi Out";
private const string TEXT_UNIT_NO = "Unit No. (should normally be set to 17)";
private const string TEXT_AUTOSAVE = "Autosave every 5 mins";
private const string TEXT_DARK_MODE = "Dark Mode";
private const string TEXT_SHOW_CONSOLE = "Show Console";
private const string TEXT_VERBOSE_MESSAGES = "Verbose messages";
private const string TEXT_HARDWARE_CONNECTED = "Hardware MT-32 connected";
private const string TEXT_ALLOW_RESET = "Allow MT-32 reset";
private const string TEXT_SEND_MESSAGES = "Send messages to MT-32";
private const string TEXT_IGNORE_SYSTEM_ON_LOAD = "Ignore system area on SysEx load";
private const string TEXT_EXCLUDE_SYSTEM_ON_SAVE = "Exclude system area on SysEx save";

private static readonly string iniFileName = "MT32Edit.ini";
private static readonly string iniFileLocation = Path.Combine($"{FileTools.applicationPath}", iniFileName);

/// <summary>
/// Opens and parses MT32Edit.ini to determine saved user settings from previous session.
/// </summary>
/// <returns>Array of two strings containing names of MIDI In and MIDI Out devices.</returns>
public static string[] Load()
{
string[] midiDeviceNames = { "", "" };
string[] parameterNames = {
TEXT_MIDI_IN, TEXT_MIDI_OUT, TEXT_UNIT_NO, TEXT_AUTOSAVE, TEXT_DARK_MODE,
TEXT_SHOW_CONSOLE, TEXT_VERBOSE_MESSAGES, TEXT_HARDWARE_CONNECTED, TEXT_ALLOW_RESET,
TEXT_SEND_MESSAGES, TEXT_IGNORE_SYSTEM_ON_LOAD, TEXT_EXCLUDE_SYSTEM_ON_SAVE
};

if (!File.Exists(iniFileLocation))
{
//return blank device names if no config file found
Expand All @@ -33,75 +46,80 @@ public static string[] Load()
}

StreamReader fs = new StreamReader(iniFileLocation);
ConsoleMessage.SendLine($"Loading settings from {iniFileLocation}");
ConsoleMessage.SendVerboseLine($"Loading settings from {iniFileLocation}");
while (!fs.EndOfStream)
{
//parse config file one line at a time until end of file is reached
string? fileLine = fs.ReadLine();
if (fileLine is null)
if (fileLine is null || fileLine.Length < 10 || fileLine.StartsWith(COMMENT_CHARACTER))
{
continue;
}

fileLine = ParseTools.RemoveLeadingSpaces(fileLine);
if (ParseTools.LeftMost(fileLine, 1) == "#")
{
continue;
}

else if (ParseTools.LeftMost(fileLine, MIDI_IN.Length).ToLower() == MIDI_IN.ToLower())
{
midiDeviceNames[0] = GetMidiDeviceName(fileLine);
}

else if (ParseTools.LeftMost(fileLine, MIDI_OUT.Length).ToLower() == MIDI_OUT.ToLower())
{
midiDeviceNames[1] = GetMidiDeviceName(fileLine);
}

else if (ParseTools.LeftMost(fileLine, SHOW_CONSOLE.Length).ToLower() == SHOW_CONSOLE.ToLower())
{
CheckConsoleSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, VERBOSE_MESSAGES.Length).ToLower() == VERBOSE_MESSAGES.ToLower())
{
CheckVerboseMessagesSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, HARDWARE_CONNECTED.Length).ToLower() == HARDWARE_CONNECTED.ToLower())
{
CheckHardwareConnectedSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, ALLOW_RESET.Length).ToLower() == ALLOW_RESET.ToLower())
{
CheckAllowResetSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, SEND_MESSAGES.Length).ToLower() == SEND_MESSAGES.ToLower())
{
CheckSendMessagesSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, IGNORE_SYSTEM_ON_LOAD.Length).ToLower() == IGNORE_SYSTEM_ON_LOAD.ToLower())
{
CheckIgnoreOnLoadSetting(fileLine);
}
string parameter = FindParameterName(fileLine);
CheckParameter(fileLine, parameter);
}
fs.Close();
return midiDeviceNames;

else if (ParseTools.LeftMost(fileLine, EXCLUDE_SYSTEM_ON_SAVE.Length).ToLower() == EXCLUDE_SYSTEM_ON_SAVE.ToLower())
string FindParameterName(string inputText)
{
for (int i = 0; i < parameterNames.Length; i++)
{
CheckExcludeOnSaveSetting(fileLine);
if (inputText.StartsWith(parameterNames[i]))
{
return parameterNames[i];
}
}
return string.Empty;
}

else if (ParseTools.LeftMost(fileLine, AUTOSAVE.Length).ToLower() == AUTOSAVE.ToLower())
{
CheckAutoSaveSetting(fileLine);
void CheckParameter(string inputText, string parameter)
{
bool? status = ParseTools.StringToBool(inputText);
switch (parameter)
{
case TEXT_MIDI_IN:
midiDeviceNames[0] = GetMidiDeviceName(inputText);
break;
case TEXT_MIDI_OUT:
midiDeviceNames[1] = GetMidiDeviceName(inputText);
break;
case TEXT_UNIT_NO:
CheckUnitNoSetting(inputText);
break;
case TEXT_AUTOSAVE:
CheckAutoSaveSetting(status);
break;
case TEXT_DARK_MODE:
CheckDarkModeSetting(status);
break;
case TEXT_SHOW_CONSOLE:
CheckConsoleSetting(status);
break;
case TEXT_VERBOSE_MESSAGES:
CheckVerboseMessagesSetting(status);
break;
case TEXT_HARDWARE_CONNECTED:
CheckHardwareConnectedSetting(status);
break;
case TEXT_ALLOW_RESET:
CheckAllowResetSetting(status);
break;
case TEXT_SEND_MESSAGES:
CheckSendMessagesSetting(status);
break;
case TEXT_IGNORE_SYSTEM_ON_LOAD:
CheckIgnoreOnLoadSetting(status);
break;
case TEXT_EXCLUDE_SYSTEM_ON_SAVE:
CheckExcludeOnSaveSetting(status);
break;
default:
break;
}
}

fs.Close();
return midiDeviceNames;

string GetMidiDeviceName(string inputString)
{
string deviceName = inputString;
Expand All @@ -110,80 +128,91 @@ string GetMidiDeviceName(string inputString)
return deviceName;
}

void CheckIgnoreOnLoadSetting(string fileLine)
void CheckUnitNoSetting(string inputString)
{
int.TryParse(ParseTools.RightOfChar(inputString, '='), out int unitNo);
if (unitNo > 0 && unitNo < 33)
{
MT32SysEx.DeviceID = (byte)(unitNo - 1);
}
}

void CheckAutoSaveSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
LoadSysExFile.ignoreSystemArea = (bool)status;
SaveSysExFile.autoSave = (bool)status;
}
}

void CheckExcludeOnSaveSetting(string fileLine)
void CheckDarkModeSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
SaveSysExFile.excludeSystemArea = (bool)status;
UITools.DarkMode = (bool)status;
}
}

void CheckConsoleSetting(string fileLine)
void CheckIgnoreOnLoadSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
ConsoleMessage.SetVisibility((bool)status);
LoadSysExFile.ignoreSystemArea = (bool)status;
}
}

void CheckVerboseMessagesSetting(string fileLine)
void CheckExcludeOnSaveSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
ConsoleMessage.SetVerbose((bool)status);
SaveSysExFile.excludeSystemArea = (bool)status;
}
}

void CheckHardwareConnectedSetting(string fileLine)
void CheckConsoleSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
Midi.hardwareMT32Connected = (bool)status;
ConsoleMessage.SetVisibility((bool)status);
}
}

void CheckAllowResetSetting(string fileLine)
void CheckVerboseMessagesSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
MT32SysEx.allowReset = (bool)status;
ConsoleMessage.SetVerbose((bool)status);
}
}

void CheckSendMessagesSetting(string fileLine)
void CheckHardwareConnectedSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
MT32SysEx.sendTextToMT32 = (bool)status;
MT32SysEx.hardwareMT32Connected = (bool)status;
}
}

void CheckAllowResetSetting(bool? status)
{
if (status.HasValue)
{
MT32SysEx.allowReset = (bool)status;
}
}

void CheckAutoSaveSetting(string fileLine)
void CheckSendMessagesSetting(bool? status)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
SaveSysExFile.autoSave = (bool)status;
MT32SysEx.sendTextToMT32 = (bool)status;
}
}
}

/// <summary>
/// Opens MT32Edit.ini to save current user settings.
/// </summary>
public static void Save()
{
try
Expand All @@ -196,16 +225,18 @@ public static void Save()
ConsoleMessage.SendVerboseLine($"Saving settings to {iniFileLocation}");
StreamWriter fs = new StreamWriter(iniFileLocation, false);
fs.WriteLine("##### MT-32 Editor configuration settings #####");
fs.WriteLine($"{MIDI_IN} = [{Midi.GetInputDeviceName(Midi.InDeviceIndex)}]");
fs.WriteLine($"{MIDI_OUT} = [{Midi.GetOutputDeviceName(Midi.OutDeviceIndex)}]");
fs.WriteLine($"{SHOW_CONSOLE} = {ConsoleMessage.Visible()}");
fs.WriteLine($"{VERBOSE_MESSAGES} = {ConsoleMessage.Verbose()}");
fs.WriteLine($"{HARDWARE_CONNECTED} = {Midi.hardwareMT32Connected}");
fs.WriteLine($"{ALLOW_RESET} = {MT32SysEx.allowReset}");
fs.WriteLine($"{SEND_MESSAGES} = {MT32SysEx.sendTextToMT32}");
fs.WriteLine($"{IGNORE_SYSTEM_ON_LOAD} = {LoadSysExFile.ignoreSystemArea}");
fs.WriteLine($"{EXCLUDE_SYSTEM_ON_SAVE} = {SaveSysExFile.excludeSystemArea}");
fs.WriteLine($"{AUTOSAVE} = {SaveSysExFile.autoSave}");
fs.WriteLine($"{TEXT_MIDI_IN} = [{Midi.GetCurrentInputDeviceName()}]");
fs.WriteLine($"{TEXT_MIDI_OUT} = [{Midi.GetCurrentOutputDeviceName()}]");
fs.WriteLine($"{TEXT_UNIT_NO} = {MT32SysEx.DeviceID + 1}");
fs.WriteLine($"{TEXT_AUTOSAVE} = {SaveSysExFile.autoSave}");
fs.WriteLine($"{TEXT_DARK_MODE} = {UITools.DarkMode}");
fs.WriteLine($"{TEXT_SHOW_CONSOLE} = {ConsoleMessage.Visible()}");
fs.WriteLine($"{TEXT_VERBOSE_MESSAGES} = {ConsoleMessage.Verbose()}");
fs.WriteLine($"{TEXT_HARDWARE_CONNECTED} = {MT32SysEx.hardwareMT32Connected}");
fs.WriteLine($"{TEXT_ALLOW_RESET} = {MT32SysEx.allowReset}");
fs.WriteLine($"{TEXT_SEND_MESSAGES} = {MT32SysEx.sendTextToMT32}");
fs.WriteLine($"{TEXT_IGNORE_SYSTEM_ON_LOAD} = {LoadSysExFile.ignoreSystemArea}");
fs.WriteLine($"{TEXT_EXCLUDE_SYSTEM_ON_SAVE} = {SaveSysExFile.excludeSystemArea}");
fs.Close();
}
catch
Expand Down
21 changes: 19 additions & 2 deletions src/MT32Editor/ConsoleMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
internal static class ConsoleMessage
{
// MT32Edit: ConsoleMessage class (static)
// S.Fryers Jan 2024
// S.Fryers Mar 2024
private static bool verboseEnabled = false; //Determines whether messages are sent to console.
private static bool consoleVisible = false; //Determines whether entire console is visible or not.

Expand Down Expand Up @@ -50,28 +50,45 @@ public static void SetVisibility(bool state)
consoleVisible = state;
}

/// <summary>
/// Sends text in the specified colour to the console, including new line character.
/// </summary>
/// <param name="message"></param>
/// <param name="color"></param>
public static void SendString(string message, ConsoleColor color = ConsoleColor.Gray)
{
Console.ForegroundColor = color;
Console.Write(message);
Console.ForegroundColor = ConsoleColor.Gray;
}

/// <summary>
/// If verboseEnabled is true, sends text in the specified colour to the console, including new line character.
/// </summary>
/// <param name="message"></param>
/// <param name="color"></param>
public static void SendVerboseString(string message, ConsoleColor color = ConsoleColor.Gray)
{
if (verboseEnabled)
{
SendString(message, color);
}
}

/// <summary>
/// Sends text in the specified colour to the console.
/// </summary>
/// <param name="message"></param>
/// <param name="color"></param>
public static void SendLine(string message, ConsoleColor color = ConsoleColor.Gray)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;
}

/// <summary>
/// If verboseEnabled is true, sends text in the specified colour to the console.
/// </summary>
public static void SendVerboseLine(string message, ConsoleColor color = ConsoleColor.Gray)
{
if (verboseEnabled)
Expand Down
Loading

0 comments on commit 359624d

Please sign in to comment.