Skip to content

Commit

Permalink
Added CommandLine parsing to the launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltstro committed Jul 9, 2020
1 parent 171a6b6 commit 0b3512f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
39 changes: 39 additions & 0 deletions src/VoltstroEngineLauncher/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.CommandLine;
using System.CommandLine.Invocation;

namespace VoltstroEngineLauncher
{
/// <summary>
/// Handles setting up command line parameters
/// </summary>
internal static class CommandLine
{
/// <summary>
/// The game name to launch
/// </summary>
public static string GameName;

/// <summary>
/// Parses arguments and sets the supported argument types in their appropriate variable
/// </summary>
/// <param name="args"></param>
public static void ParseArguments(string[] args)
{
RootCommand rootCommand = new RootCommand
{
new Option<string>(
"-game",
getDefaultValue: () => "Sandbox",
description: "The game to launch")
};

rootCommand.Description = "Voltstro Engine Launcher";
rootCommand.Handler = CommandHandler.Create<string>((game) =>
{
GameName = game;
});

rootCommand.InvokeAsync(args);
}
}
}
20 changes: 14 additions & 6 deletions src/VoltstroEngineLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@

namespace VoltstroEngineLauncher
{
/// <summary>
/// Program for VoltstroEngineLauncher
/// </summary>
public class Program
{
private const string DefaultGame = "Sandbox";

/// <summary>
/// The launcher for the VoltstroEngine
/// </summary>
[STAThread]
public static void Main(string[] args)
{
//Do our command line parsing first
CommandLine.ParseArguments(args);

//Create our Eto.Forms app, so we can show message boxes
//We shut this down before we run the engine
Platform.AllowReinitialize = true;
Application app = new Application();

//Now to get the game's entry point
IEntryPoint entryPoint = null;
string dllPath = Path.GetFullPath($"{DefaultGame}/bin/{DefaultGame}.dll");
string dllPath = Path.GetFullPath($"{CommandLine.GameName}/bin/{CommandLine.GameName}.dll");
try
{
//Load the game assembly
AssemblyLoad assemblyLoad = new AssemblyLoad();
Assembly gameDll = assemblyLoad.LoadAssembly(Path.GetFullPath($"{DefaultGame}/bin"), $"{DefaultGame}.dll");
Assembly gameDll = assemblyLoad.LoadAssembly(Path.GetFullPath($"{CommandLine.GameName}/bin"), $"{CommandLine.GameName}.dll");

//Find a class the inherits from IEntryPoint so that we can create the game
foreach (Type type in gameDll.GetTypes().Where(x => x.IsPublic && x.IsClass)) //Needs to be public
Expand All @@ -42,9 +50,9 @@ public static void Main(string[] args)
}
catch (FileNotFoundException ex) //The DLL wasn't found
{
Debug.Assert(false, $"The game DLL for '{DefaultGame}' wasn't found in '{dllPath}'!\n{ex}");
Debug.Assert(false, $"The game DLL for '{CommandLine.GameName}' wasn't found in '{dllPath}'!\n{ex}");
#if !DEBUG
Eto.Forms.MessageBox.Show($"The game DLL for '{DefaultGame}' wasn't found in '{dllPath}'!", "Engine Error",
Eto.Forms.MessageBox.Show($"The game DLL for '{CommandLine.GameName}' wasn't found in '{dllPath}'!", "Engine Error",
Eto.Forms.MessageBoxButtons.OK, Eto.Forms.MessageBoxType.Error);
app.Dispose();
Environment.Exit(0);
Expand Down
4 changes: 4 additions & 0 deletions src/VoltstroEngineLauncher/VoltstroEngineLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<OutputPath>../../game/bin/Release</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20303.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VoltstroEngine\VoltstroEngine.csproj" />
</ItemGroup>
Expand Down

0 comments on commit 0b3512f

Please sign in to comment.