Merge pull request #918 from Ashafix/CLI

added command line arguments parser
This commit is contained in:
zeromus 2017-07-14 12:21:40 -05:00 committed by GitHub
commit 11fc26ec3b
3 changed files with 143 additions and 109 deletions

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace BizHawk.Client.EmuHawk
{
class ArgParser
//parses command line arguments and adds the values to a class attribute
//default values are null for strings and false for boolean
//the last value will overwrite previously set values
//unrecognized parameters are simply ignored or in the worst case assumed to be a ROM name [cmdRom]
{
public string cmdRom = null;
public string cmdLoadSlot = null;
public string cmdLoadState = null;
public string cmdMovie = null;
public string cmdDumpType = null;
public string cmdDumpName = null;
public HashSet<int> _currAviWriterFrameList;
public int _autoDumpLength;
public bool _autoCloseOnDump = false;
public bool _chromeless = false;
public bool startFullscreen = false;
public string luaScript = null;
public bool luaConsole = false;
public int socket_port = 9999;
public string socket_ip = null;
public void parseArguments(string[] args)
{
for (int i = 0; i<args.Length; i++)
{
// For some reason sometimes visual studio will pass this to us on the commandline. it makes no sense.
if (args[i] == ">")
{
i++;
var stdout = args[i];
Console.SetOut(new StreamWriter(stdout));
continue;
}
var arg = args[i].ToLower();
if (arg.StartsWith("--load-slot="))
{
cmdLoadSlot = arg.Substring(arg.IndexOf('=') + 1);
}
if (arg.StartsWith("--load-state="))
{
cmdLoadState = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--movie="))
{
cmdMovie = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--dump-type="))
{
cmdDumpType = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--dump-frames="))
{
var list = arg.Substring(arg.IndexOf('=') + 1);
var items = list.Split(',');
_currAviWriterFrameList = new HashSet<int>();
foreach (string item in items)
{
_currAviWriterFrameList.Add(int.Parse(item));
}
// automatically set dump length to maximum frame
_autoDumpLength = _currAviWriterFrameList.OrderBy(x => x).Last();
}
else if (arg.StartsWith("--dump-name="))
{
cmdDumpName = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--dump-length="))
{
int.TryParse(arg.Substring(arg.IndexOf('=') + 1), out _autoDumpLength);
}
else if (arg.StartsWith("--dump-close"))
{
_autoCloseOnDump = true;
}
else if (arg.StartsWith("--chromeless"))
{
_chromeless = true;
}
else if (arg.StartsWith("--fullscreen"))
{
startFullscreen = true;
}
else if (arg.StartsWith("--lua="))
{
luaScript = arg.Substring(arg.IndexOf('=') + 1);
luaConsole = true;
}
else if (arg.StartsWith("--luaconsole"))
{
luaConsole = true;
}
else if (arg.StartsWith("--socket_port="))
{
int.TryParse(arg.Substring(arg.IndexOf('=') + 1), out socket_port);
}
else if (arg.StartsWith("--socket_ip="))
{
socket_ip = arg.Substring(arg.IndexOf('=') + 1);
}
else
{
cmdRom = arg;
}
}
}
}
}

View File

@ -126,6 +126,7 @@
<Compile Include="ArchiveChooser.Designer.cs">
<DependentUpon>ArchiveChooser.cs</DependentUpon>
</Compile>
<Compile Include="ArgParser.cs" />
<Compile Include="AVOut\AviWriter.cs" />
<Compile Include="AVOut\AVSync.cs" />
<Compile Include="AVOut\BmpVideoProvder.cs" />

View File

@ -153,93 +153,8 @@ namespace BizHawk.Client.EmuHawk
}
};
// TODO - replace this with some kind of standard dictionary-yielding parser in a separate component
string cmdRom = null;
string cmdLoadState = null;
string cmdLoadSlot = null;
string cmdMovie = null;
string cmdDumpType = null;
string cmdDumpName = null;
bool startFullscreen = false;
string luaScript = null;
bool luaConsole = false;
for (int i = 0; i < args.Length; i++)
{
// For some reason sometimes visual studio will pass this to us on the commandline. it makes no sense.
if (args[i] == ">")
{
i++;
var stdout = args[i];
Console.SetOut(new StreamWriter(stdout));
continue;
}
var arg = args[i].ToLower();
if (arg.StartsWith("--load-slot="))
{
cmdLoadSlot = arg.Substring(arg.IndexOf('=') + 1);
}
if (arg.StartsWith("--load-state="))
{
cmdLoadState = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--movie="))
{
cmdMovie = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--dump-type="))
{
cmdDumpType = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--dump-frames="))
{
var list = arg.Substring(arg.IndexOf('=') + 1);
var items = list.Split(',');
_currAviWriterFrameList = new HashSet<int>();
foreach (string item in items)
{
_currAviWriterFrameList.Add(int.Parse(item));
}
// automatically set dump length to maximum frame
_autoDumpLength = _currAviWriterFrameList.OrderBy(x => x).Last();
}
else if (arg.StartsWith("--dump-name="))
{
cmdDumpName = arg.Substring(arg.IndexOf('=') + 1);
}
else if (arg.StartsWith("--dump-length="))
{
int.TryParse(arg.Substring(arg.IndexOf('=') + 1), out _autoDumpLength);
}
else if (arg.StartsWith("--dump-close"))
{
_autoCloseOnDump = true;
}
else if (arg.StartsWith("--chromeless"))
{
_chromeless = true;
}
else if (arg.StartsWith("--fullscreen"))
{
startFullscreen = true;
}
else if (arg.StartsWith("--lua="))
{
luaScript = arg.Substring(arg.IndexOf('=') + 1);
luaConsole = true;
}
else if (arg.StartsWith("--luaconsole"))
{
luaConsole = true;
}
else
{
cmdRom = arg;
}
}
ArgParser argParse = new ArgParser();
argParse.parseArguments(args);
Database.LoadDatabase(Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb.txt"));
@ -355,13 +270,13 @@ namespace BizHawk.Client.EmuHawk
Location = new Point(Global.Config.MainWndx, Global.Config.MainWndy);
}
if (cmdRom != null)
if (argParse.cmdRom != null)
{
// Commandline should always override auto-load
LoadRom(cmdRom, new LoadRomArgs { OpenAdvanced = new OpenAdvanced_OpenRom() });
LoadRom(argParse.cmdRom, new LoadRomArgs { OpenAdvanced = new OpenAdvanced_OpenRom() });
if (Global.Game == null)
{
MessageBox.Show("Failed to load " + cmdRom + " specified on commandline");
MessageBox.Show("Failed to load " + argParse.cmdRom + " specified on commandline");
}
}
else if (Global.Config.RecentRoms.AutoLoad && !Global.Config.RecentRoms.Empty)
@ -369,7 +284,7 @@ namespace BizHawk.Client.EmuHawk
LoadRomFromRecent(Global.Config.RecentRoms.MostRecent);
}
if (cmdMovie != null)
if (argParse.cmdMovie != null)
{
_supressSyncSettingsWarning = true; // We dont' want to be nagged if we are attempting to automate
if (Global.Game == null)
@ -380,7 +295,7 @@ namespace BizHawk.Client.EmuHawk
// If user picked a game, then do the commandline logic
if (!Global.Game.IsNullInstance)
{
var movie = MovieService.Get(cmdMovie);
var movie = MovieService.Get(argParse.cmdMovie);
Global.MovieSession.ReadOnly = true;
// if user is dumping and didnt supply dump length, make it as long as the loaded movie
@ -390,11 +305,11 @@ namespace BizHawk.Client.EmuHawk
}
// Copy pasta from drag & drop
if (MovieImport.IsValidMovieExtension(Path.GetExtension(cmdMovie)))
if (MovieImport.IsValidMovieExtension(Path.GetExtension(argParse.cmdMovie)))
{
string errorMsg;
string warningMsg;
var imported = MovieImport.ImportFile(cmdMovie, out errorMsg, out warningMsg);
var imported = MovieImport.ImportFile(argParse.cmdMovie, out errorMsg, out warningMsg);
if (!string.IsNullOrEmpty(errorMsg))
{
MessageBox.Show(errorMsg, "Conversion error", MessageBoxButtons.OK, MessageBoxIcon.Error);
@ -412,7 +327,7 @@ namespace BizHawk.Client.EmuHawk
else
{
StartNewMovie(movie, false);
Global.Config.RecentMovies.Add(cmdMovie);
Global.Config.RecentMovies.Add(argParse.cmdMovie);
}
_supressSyncSettingsWarning = false;
@ -439,20 +354,20 @@ namespace BizHawk.Client.EmuHawk
}
}
if (startFullscreen || Global.Config.StartFullscreen)
if (argParse.startFullscreen || Global.Config.StartFullscreen)
{
_needsFullscreenOnLoad = true;
}
if (!Global.Game.IsNullInstance)
{
if (cmdLoadState != null)
if (argParse.cmdLoadState != null)
{
LoadState(cmdLoadState, Path.GetFileName(cmdLoadState));
LoadState(argParse.cmdLoadState, Path.GetFileName(argParse.cmdLoadState));
}
else if (cmdLoadSlot != null)
else if (argParse.cmdLoadSlot != null)
{
LoadQuickSave("QuickSave" + cmdLoadSlot);
LoadQuickSave("QuickSave" + argParse.cmdLoadSlot);
}
else if (Global.Config.AutoLoadLastSaveSlot)
{
@ -461,14 +376,14 @@ namespace BizHawk.Client.EmuHawk
}
//start Lua Console if requested in the command line arguments
if (luaConsole)
if (argParse.luaConsole)
{
GlobalWin.Tools.Load<LuaConsole>();
}
//load Lua Script if requested in the command line arguments
if (luaScript != null)
if (argParse.luaScript != null)
{
GlobalWin.Tools.LuaConsole.LoadLuaFile(luaScript);
GlobalWin.Tools.LuaConsole.LoadLuaFile(argParse.luaScript);
}
GlobalWin.Tools.AutoLoad();
@ -491,9 +406,9 @@ namespace BizHawk.Client.EmuHawk
}
// start dumping, if appropriate
if (cmdDumpType != null && cmdDumpName != null)
if (argParse.cmdDumpType != null && argParse.cmdDumpName != null)
{
RecordAv(cmdDumpType, cmdDumpName);
RecordAv(argParse.cmdDumpType, argParse.cmdDumpName);
}
SetMainformMovieInfo();
@ -839,7 +754,7 @@ namespace BizHawk.Client.EmuHawk
// zero 09-sep-2012 - all input is eligible for controller input. not sure why the above was done.
// maybe because it doesnt make sense to me to bind hotkeys and controller inputs to the same keystrokes
// adelikat 02-dec-2012 - implemented options for how to handle controller vs hotkey conflicts. This is primarily motivated by computer emulation and thus controller being nearly the entire keyboard
// adelikat 02-dec-2012 - implemented options for how to handle controller vs hotkey conflicts. This is primarily motivated by computer emulation and thus controller being nearly the entire keyboard
bool handled;
switch (Global.Config.Input_Hotkey_OverrideOptions)
{
@ -1669,7 +1584,7 @@ namespace BizHawk.Client.EmuHawk
var oldram = Emulator.AsSaveRam().CloneSaveRam();
if (oldram == null)
{
// we're eating this one now. the possible negative consequence is that a user could lose
// we're eating this one now. The possible negative consequence is that a user could lose
// their saveram and not know why
// MessageBox.Show("Error: tried to load saveram, but core would not accept it?");
return;
@ -1784,7 +1699,6 @@ namespace BizHawk.Client.EmuHawk
{
GenesisSubMenu.Visible = true;
}
break;
case "TI83":
TI83SubMenu.Visible = true;
@ -3584,7 +3498,7 @@ namespace BizHawk.Client.EmuHawk
loader.OnLoadSettings += CoreSettings;
loader.OnLoadSyncSettings += CoreSyncSettings;
// this also happens in CloseGame(). but it needs to happen here since if we're restarting with the same core,
// this also happens in CloseGame(). But it needs to happen here since if we're restarting with the same core,
// any settings changes that we made need to make it back to config before we try to instantiate that core with
// the new settings objects
CommitCoreSettingsToConfig(); // adelikat: I Think by reordering things, this isn't necessary anymore