Skip to content

Commit

Permalink
tools: Add table script utility similar to vptscript in vpx-js
Browse files Browse the repository at this point in the history
  • Loading branch information
jsm174 committed Jun 5, 2021
1 parent cc711a7 commit 32e95f4
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Visual Pinball.
## Tools

- [Material Patcher](VisualPinball.MaterialPatcher) - Quickly update common materials to a bunch of tables
- [Table Script](VisualPinball.TableScript) - Quickly extract and save table scripts

## License

[GPL-3.0](LICENSE)
[GPL-3.0](LICENSE)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="VisualPinball.Engine" Version="0.0.1-preview.38" />
<PackageReference Include="VisualPinball.Engine" Version="0.0.1-preview.46" />
</ItemGroup>

</Project>
75 changes: 75 additions & 0 deletions VisualPinball.TableScript/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.IO;
using VisualPinball.Engine.VPT.Table;

namespace VisualPinball.TableScript
{
class Program
{
static void Main(string[] args)
{
// validate inputs
try {
if (args.Length < 1) {
throw new ArgumentException("USAGE: VisualPinball.TableScript <input file or folder> [<output folder>]");
}

// input file(s)
if (!FileOrDirectoryExists(args[0])) {
throw new ArgumentException($"Non-existent input path: \"{args[0]}\".");
}
var inputAttr = File.GetAttributes(args[0]);
var inputFiles = (inputAttr & FileAttributes.Directory) == FileAttributes.Directory
? Directory.GetFiles(args[0], "*.vpx")
: new[] {args[0]};
if (inputFiles.Length == 0) {
throw new ArgumentException($"No .vpx files found at \"{args[0]}\"");
}

// output folder
string outputDir = null;

if (args.Length > 1) {
outputDir = args[1];
if (!Directory.Exists(outputDir)) {
throw new ArgumentException($"Invalid output folder \"{outputDir}\".");
}
}

foreach (var inputFile in inputFiles) {
Console.WriteLine($"Processing \"{inputFile}\"...");

var inputTable = TableLoader.Load(inputFile);

var code = inputTable.Data.Code;

if (Environment.NewLine == "\n") {
code = code.Replace("\r\r\n", "\n");
code = code.Replace("\r\n", "\n");
code = code.Replace("\r", "\n");
}

if (outputDir != null) {
var outputFilePath = Path.Join(outputDir, Path.GetFileNameWithoutExtension(inputFile) + ".vbs");

File.WriteAllText(outputFilePath, code);
}
else {
Console.WriteLine(code);
}
}

} catch (ArgumentException e) {
Console.WriteLine(e.Message);

} catch (Exception e) {
Console.WriteLine(e);
}
}

private static bool FileOrDirectoryExists(string name)
{
return Directory.Exists(name) || File.Exists(name);
}
}
}
41 changes: 41 additions & 0 deletions VisualPinball.TableScript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Table Script

This is a command line tool that simply:

- Reads all `.vpx` files in a folder (or a single file)
- Optionally takes in an output folder

It then:

- Extracts all input table scripts
- Converts CR/LF to NL on Unix systems
- Saves the table scripts to the output folder if specified, otherwise outputs to the console.

## Usage

Usage is pretty simple:

```bash
VisualPinball.TableScript <.vpx or input folder> [<output folder>]
```

## Compilation

To get a single binary on Windows:

```bash
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
```

To get a single binary on MacOS:

```bash
dotnet publish -r osx-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
```


To get a single binary on Linux:

```bash
dotnet publish -r linux-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
```
17 changes: 17 additions & 0 deletions VisualPinball.TableScript/VisualPinball.TableScript.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<Company>Visual Pinball Engine</Company>
<Product>Table Script</Product>
<AssemblyVersion>0.0.3.0</AssemblyVersion>
<FileVersion>0.0.3.0-SNAPSHOT</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="VisualPinball.Engine" Version="0.0.1-preview.46" />
</ItemGroup>

</Project>
21 changes: 14 additions & 7 deletions VisualPinball.Tools.sln
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualPinball.MaterialPatcher", "VisualPinball.MaterialPatcher\VisualPinball.MaterialPatcher.csproj", "{FEE9DBFB-FBF8-4CEA-B221-119FABA62581}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution", "Solution", "{AB27DDB7-69F3-4D10-B659-76EB301A9827}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
.gitignore = .gitignore
EndProjectSection
ProjectSection(SolutionItems) = preProject
README.md = README.md
.gitignore = .gitignore
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{6DDE16F0-311E-403D-9D40-374990513C61}"
ProjectSection(SolutionItems) = preProject
.github\workflows\MaterialPatcher.yml = .github\workflows\MaterialPatcher.yml
EndProjectSection
ProjectSection(SolutionItems) = preProject
.github\workflows\MaterialPatcher.yml = .github\workflows\MaterialPatcher.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualPinball.TableScript", "VisualPinball.TableScript\VisualPinball.TableScript.csproj", "{A012BD98-AE59-468A-A23D-80C04A52171E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,6 +26,10 @@ Global
{FEE9DBFB-FBF8-4CEA-B221-119FABA62581}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEE9DBFB-FBF8-4CEA-B221-119FABA62581}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEE9DBFB-FBF8-4CEA-B221-119FABA62581}.Release|Any CPU.Build.0 = Release|Any CPU
{A012BD98-AE59-468A-A23D-80C04A52171E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A012BD98-AE59-468A-A23D-80C04A52171E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A012BD98-AE59-468A-A23D-80C04A52171E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A012BD98-AE59-468A-A23D-80C04A52171E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6DDE16F0-311E-403D-9D40-374990513C61} = {AB27DDB7-69F3-4D10-B659-76EB301A9827}
Expand Down

0 comments on commit 32e95f4

Please sign in to comment.