Skip to content

Commit

Permalink
Add possibility to add pre and post command #13, and VPinballX.starte…
Browse files Browse the repository at this point in the history
…r.pre.cmd VPinballX.starter.post.cmd
  • Loading branch information
JockeJarre committed Apr 14, 2024
1 parent 25bb284 commit 01d5b0c
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions VPinballX.starter/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
using System.Runtime.InteropServices;
using Salaros.Configuration;
using System.ComponentModel;
using System;


namespace VPinballX.starter
Expand Down Expand Up @@ -233,7 +234,7 @@ private void Application_Startup(object sender, StartupEventArgs eventArgs)
}
try
{
string strSettingsIniFilePath = Path.Combine(strExeFilePath, $"{strIniConfigFilename}");
string strSettingsIniFilePath = Path.Combine(strExeFilePath, strIniConfigFilename);

if (!FileOrDirectoryExists(strSettingsIniFilePath))
{
Expand Down Expand Up @@ -262,31 +263,27 @@ private void Application_Startup(object sender, StartupEventArgs eventArgs)
10.80=VPinballX85.exe
10.80x32=VPinballX85x32.exe
10.80GL=VPinballX85_GL.exe
;cmd files to run before and after a table has been started, uncomment to activate.
;PREcmd=${tablename}.pre.cmd
;POSTcmd=${tablename}.post.cmd";
;cmd files to run before and after a table has been started. Activate here
PREPOSTactive=false
PREcmd=${tablename}.pre.cmd
POSTcmd=${tablename}.post.cmd";

string strWelcomeString =
$@"Welcome new VPinballX.starter user!
We could not find a ""{strIniConfigFilename}"" next to ""{strExeFileName}"".
The file should look like this:
X-----X-----X-----X----X-----X-----X-----X-----X
{strDefaultIniConfig}
X-----X-----X-----X----X-----X-----X-----X-----X
VPinballX.starter can therefore be used as a VPinballX.exe replacement.
It works like this:
VPinballX.starter is started with exactly the same parameters as VPinballX.exe. First it loads the table file and finds out what version it was saved with. Then it takes that information and looks in the [VPinballX] table above to find out which version of VPinballX.xxx.exe you want to start with. It will then run the VPinballX.xxx.exe that you have configured using exactly the same parameters.
If it cannot find a version in the table the default entry under [VPinballX] will be used. If you simply double-click the VPinballX.starter without any table, the Default under [VPinballx.starter] is used.
This way the correct table version or the version you have chosen will always be used. Each time you start VPinballX, a log entry will be added to VPinballX.starter.log, telling which version was used. This can be turned of in the ini file.
Do you want to create this file now?";
Expand All @@ -305,9 +302,10 @@ VPinballX.starter can therefore be used as a VPinballX.exe replacement.
throw new FileNotFoundException($"Configuration \"{strSettingsIniFilePath}\" cannot be found!\n\nExiting");
}
}
string[] AllTrue = new string[] { "true", "1", "yes" };

var configFileFromPath = new ConfigParser(strSettingsIniFilePath);
bool logVersions = (configFileFromPath["VPinballX.starter"]["LogVersions"] ?? "0").Equals("1");
bool logVersions = AllTrue.Any((configFileFromPath["VPinballX.starter"]["LogVersions"] ?? "false").Trim().ToLower().Contains);

string tableFilename = "";

Expand Down Expand Up @@ -342,7 +340,7 @@ VPinballX.starter can therefore be used as a VPinballX.exe replacement.

if (!FileOrDirectoryExists(tableFilename))
{
if (logVersions) LogToFile($"Table file \"{tableFilename}\" cannot be found! Please check your frontend software!");
LogToFile($"Table file \"{tableFilename}\" cannot be found! Please check your frontend software!");
throw new FileNotFoundException($"Table file\n\n{tableFilename}\n\n cannot be found!\nPlease check your frontend software!");
}

Expand Down Expand Up @@ -398,31 +396,17 @@ VPinballX.starter can therefore be used as a VPinballX.exe replacement.
LogToFile($"Using default version {strFileVersion} mapped to \"{vpxCommand}\"");
}

string tableName = Path.GetFileNameWithoutExtension(Path.GetFileName(tableFilename));
string preCommand = configFileFromPath["VPinballX"]["PREcmd"] ?? "";
if (!preCommand.Equals(""))
bool PREPOSTactive = AllTrue.Any((configFileFromPath["VPinballX"]["PREPOSTactive"] ?? "false").Trim().ToLower().Contains);
if(PREPOSTactive)
{
LogToFile($"Calling found PREcmd: {preCommand}");
preCommand = preCommand.Replace("${tablename}", tableName);
preCommand = $"{Directory.GetCurrentDirectory()}\\{preCommand}";
if (File.Exists(preCommand))
{
LogToFile($"Calling found PREcmd: {preCommand}");
StartAnotherProgram(preCommand, mArgs, false);
}
StartPrePostCommands("${tablename}.pre.cmd", strSettingsIniFilePath);
StartPrePostCommands(configFileFromPath["VPinballX"]["PREcmd"] ?? "", tableFilename);
}
StartAnotherProgram(vpxCommand, mArgs);
string postCommand = configFileFromPath["VPinballX"]["POSTcmd"] ?? "";
if (!postCommand.Equals(""))
{
postCommand = postCommand.Replace("${tablename}", tableName);
postCommand = $"{Directory.GetCurrentDirectory()}\\{postCommand}";

if (File.Exists(postCommand))
{
LogToFile($"Calling found POSTcmd: {postCommand}");
StartAnotherProgram(postCommand, mArgs, false);
}
if (PREPOSTactive)
{
StartPrePostCommands(configFileFromPath["VPinballX"]["POSTcmd"] ?? "", tableFilename);
StartPrePostCommands("${tablename}.post.cmd", strSettingsIniFilePath);
}
Environment.Exit(0);

Expand All @@ -442,7 +426,20 @@ VPinballX.starter can therefore be used as a VPinballX.exe replacement.
Environment.Exit(1);

}
void StartPrePostCommands(string prepostTemplate, string scriptBasedFilename)
{
if (!prepostTemplate.Equals(""))
{
string prepostCommand = prepostTemplate.Replace("${tablename}", Path.GetFileNameWithoutExtension(scriptBasedFilename));

if (File.Exists(prepostCommand))
{
LogToFile($"Calling found POSTcmd: {prepostCommand}");
StartAnotherProgram(prepostCommand, mArgs, false);
}
}

}
void LogToFile(string logText)
{
using (var sw = File.AppendText(strLogFilename))
Expand Down

0 comments on commit 01d5b0c

Please sign in to comment.