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 707877f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ Default=VPinballX72.exe
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
```

With this information, VPinballX.starter can be used as a replacement for VPinballX.exe.
Expand Down Expand Up @@ -91,4 +92,5 @@ it will take care of starting the right version independent if you are using Exp
The two settings PREcmd and POSTcmd can be used to run windows batch cmd files before and after a certain table is opened.
PREcmd It can be used to setup the stage for a certain table, loading pictures or whatever comes to mind. And POSTcmd is
then used to cleanup afterwards.
As the default settings tells, it will search for a file called <tablename>.pre.cmd an call it if found.
As the default settings tells, it will search for a file called <tablename>.pre.cmd and call it if found.
It will also search for VPinballX.starter.pre/post.cmd and call it for every table started.
84 changes: 41 additions & 43 deletions VPinballX.starter/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ public static extern int MessageBoxW(
}
private void Application_Startup(object sender, StartupEventArgs eventArgs)
{
string strSettingsIniFilePath = Path.Combine(strExeFilePath, strIniConfigFilename);
if (eventArgs.Args.Length > 0)
{
mArgs = eventArgs.Args;
}
try
{
string strSettingsIniFilePath = Path.Combine(strExeFilePath, $"{strIniConfigFilename}");

if (!FileOrDirectoryExists(strSettingsIniFilePath))
{
Expand Down Expand Up @@ -262,31 +262,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
PREcmdExtension=.pre.cmd
POSTcmdExtension=.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 +301,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 +339,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 All @@ -364,24 +361,26 @@ VPinballX.starter can therefore be used as a VPinballX.exe replacement.
if (configFileFromPath["VPinballX"][strFileVersion] == null)
strFileVersion = "Default";

// Check the TableNameExceptions either for a Table Name within the list or a specific alien VPX version used (e.g x64, x32 or GL)
if (configFileFromPath["TableNameExceptions"] != null)
if (!tableFilename.Equals(""))
{
foreach (var key in configFileFromPath["TableNameExceptions"].Keys)
// Check the TableNameExceptions either for a Table Name within the list or a specific alien VPX version used (e.g x64, x32 or GL)
if (configFileFromPath["TableNameExceptions"] != null)
{
if (tableFilename.Contains(key.Name))
foreach (var key in configFileFromPath["TableNameExceptions"].Keys)
{
if (logVersions) LogToFile($"Found {key.Name} in {tableFilename}");

if (configFileFromPath["VPinballX"][$"{strFileVersion}{key.ValueRaw}"] != null)
if (tableFilename.Contains(key.Name))
{
strFileVersion = $"{strFileVersion}{key.ValueRaw}";
break;
if (logVersions) LogToFile($"Found {key.Name} in {tableFilename}");

if (configFileFromPath["VPinballX"][$"{strFileVersion}{key.ValueRaw}"] != null)
{
strFileVersion = $"{strFileVersion}{key.ValueRaw}";
break;
}
}
}
}
}

string vpxCommand = configFileFromPath["VPinballX"][strFileVersion] ?? configFileFromPath["VPinballX"]["Default"];

if (object.Equals(vpxCommand, null))
Expand All @@ -398,31 +397,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 && (!tableFilename.Equals("")))
{
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(configFileFromPath["VPinballX"]["PREcmdExtension"] ?? ".pre.cmd", strSettingsIniFilePath);
StartPrePostCommands(configFileFromPath["VPinballX"]["PREcmdExtension"] ?? ".pre.cmd", 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 && (!tableFilename.Equals("")))
{
StartPrePostCommands(configFileFromPath["VPinballX"]["POSTcmdExtension"] ?? ".post.cmd", tableFilename);
StartPrePostCommands(configFileFromPath["VPinballX"]["POSTcmdExtension"] ?? ".post.cmd", strSettingsIniFilePath);
}
Environment.Exit(0);

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

}
void StartPrePostCommands(string prepostExtension, string scriptBasedFilename)
{
if (!prepostExtension.Equals(""))
{
string prepostCommand = Path.ChangeExtension(scriptBasedFilename, prepostExtension);

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

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

0 comments on commit 707877f

Please sign in to comment.