From 4525d7328b6d14fe3d2b0073d1c6b211287227ee Mon Sep 17 00:00:00 2001 From: Sour Date: Fri, 15 Mar 2019 12:24:02 -0400 Subject: [PATCH] Command line: Load rom from command line arguments --- UI/CommandLineHelper.cs | 70 ----------- UI/Forms/frmMain.cs | 10 +- UI/Program.cs | 4 +- UI/UI.csproj | 2 +- UI/Utilities/CommandLineHelper.cs | 191 ++++++++++++++++++++++++++++++ 5 files changed, 203 insertions(+), 74 deletions(-) delete mode 100644 UI/CommandLineHelper.cs create mode 100644 UI/Utilities/CommandLineHelper.cs diff --git a/UI/CommandLineHelper.cs b/UI/CommandLineHelper.cs deleted file mode 100644 index ed294c8..0000000 --- a/UI/CommandLineHelper.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Mesen.GUI -{ - class CommandLineHelper - { - public static List PreprocessCommandLineArguments(string[] args, bool toLower) - { - var switches = new List(); - for(int i = 0; i < args.Length; i++) { - if(args[i] != null) { - string arg = args[i].Trim(); - if(arg.StartsWith("--")) { - arg = "/" + arg.Substring(2); - } else if(arg.StartsWith("-")) { - arg = "/" + arg.Substring(1); - } - - if(toLower) { - arg = arg.ToLowerInvariant(); - } - switches.Add(arg); - } - } - return switches; - } - - public static void GetRomPathFromCommandLine(List switches, out string romPath, out List luaScriptsToLoad) - { - Func getValidPath = (string path, bool forLua) => { - path = path.Trim(); - if(path.ToLower().EndsWith(".lua") == forLua) { - try { - if(!File.Exists(path)) { - //Try loading file as a relative path to the folder Mesen was started from - path = Path.Combine(Program.OriginalFolder, path); - } - if(File.Exists(path)) { - return path; - } - } catch { } - } - return null; - }; - - //Check if any Lua scripts were specified - luaScriptsToLoad = new List(); - foreach(string arg in switches) { - string path = getValidPath(arg, true); - if(path != null) { - luaScriptsToLoad.Add(path); - } - } - - romPath = null; - foreach(string arg in switches) { - string path = getValidPath(arg, false); - if(path != null) { - romPath = path; - break; - } - } - } - } -} diff --git a/UI/Forms/frmMain.cs b/UI/Forms/frmMain.cs index 8d7dc80..0d789ca 100644 --- a/UI/Forms/frmMain.cs +++ b/UI/Forms/frmMain.cs @@ -4,6 +4,7 @@ using Mesen.GUI.Debugger; using Mesen.GUI.Emulation; using Mesen.GUI.Forms.Config; using Mesen.GUI.Updates; +using Mesen.GUI.Utilities; using System; using System.Collections.Generic; using System.ComponentModel; @@ -23,6 +24,7 @@ namespace Mesen.GUI.Forms private NotificationListener _notifListener; private ShortcutHandler _shortcuts; private DisplayManager _displayManager; + private CommandLineHelper _commandLine; public frmMain(string[] args) { @@ -31,6 +33,8 @@ namespace Mesen.GUI.Forms return; } + _commandLine = new CommandLineHelper(args); + ResourceHelper.LoadResources(Language.English); } @@ -69,9 +73,13 @@ namespace Mesen.GUI.Forms BindShortcuts(); ctrlRecentGames.Initialize(); - ctrlRecentGames.Visible = true; ResizeRecentGames(); + _commandLine.LoadGameFromCommandLine(); + if(!EmuRunner.IsRunning()) { + ctrlRecentGames.Visible = true; + } + this.Resize += frmMain_Resize; } diff --git a/UI/Program.cs b/UI/Program.cs index b0720b1..e6727a2 100644 --- a/UI/Program.cs +++ b/UI/Program.cs @@ -12,6 +12,7 @@ using System.Windows.Forms; using System.Xml.Serialization; using Mesen.GUI.Config; using Mesen.GUI.Forms; +using Mesen.GUI.Utilities; namespace Mesen.GUI { @@ -118,8 +119,7 @@ namespace Mesen.GUI singleInstance.ArgumentsReceived += (object sender, ArgumentsReceivedEventArgs e) => { if(frmMain.IsHandleCreated) { frmMain.BeginInvoke((MethodInvoker)(() => { - //frmMain.ProcessCommandLineArguments(CommandLineHelper.PreprocessCommandLineArguments(e.Args, true), false); - //frmMain.LoadGameFromCommandLine(CommandLineHelper.PreprocessCommandLineArguments(e.Args, false)); + new CommandLineHelper(e.Args).LoadGameFromCommandLine(); })); } }; diff --git a/UI/UI.csproj b/UI/UI.csproj index a8835c3..90d8a55 100644 --- a/UI/UI.csproj +++ b/UI/UI.csproj @@ -208,7 +208,6 @@ - @@ -606,6 +605,7 @@ + diff --git a/UI/Utilities/CommandLineHelper.cs b/UI/Utilities/CommandLineHelper.cs new file mode 100644 index 0000000..4ccea03 --- /dev/null +++ b/UI/Utilities/CommandLineHelper.cs @@ -0,0 +1,191 @@ +using Mesen.GUI.Config; +using Mesen.GUI.Emulation; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Mesen.GUI.Utilities +{ + class CommandLineHelper + { + private string[] _args; + private bool _noVideo; + private bool _noAudio; + private bool _noInput; + //private bool _loadLastSessionRequested; + private string _movieToRecord; + private List _luaScriptsToLoad = new List(); + + public CommandLineHelper(string[] args) + { + _args = args; + } + + public void ProcessCommandLineArguments(List switches, bool forStartup) + { + if(forStartup) { + _noVideo = switches.Contains("/novideo"); + _noAudio = switches.Contains("/noaudio"); + _noInput = switches.Contains("/noinput"); + } + + if(switches.Contains("/donotsavesettings")) { + ConfigManager.DoNotSaveSettings = true; + } + + /*if(switches.Contains("/loadlastsession")) { + _loadLastSessionRequested = true; + }*/ + + Regex recordMovieCommand = new Regex("/recordmovie=([^\"]+)"); + foreach(string command in switches) { + Match match = recordMovieCommand.Match(command); + if(match.Success) { + string moviePath = match.Groups[1].Value; + string folder = Path.GetDirectoryName(moviePath); + if(string.IsNullOrWhiteSpace(folder)) { + moviePath = Path.Combine(ConfigManager.MovieFolder, moviePath); + } else if(!Path.IsPathRooted(moviePath)) { + moviePath = Path.Combine(Program.OriginalFolder, moviePath); + } + if(!moviePath.ToLower().EndsWith(".mmo")) { + moviePath += ".mmo"; + } + _movieToRecord = moviePath; + break; + } + } + + ConfigManager.ProcessSwitches(switches); + } + + private void ProcessFullscreenSwitch(List switches) + { + //TODO + /*if(switches.Contains("/fullscreen")) { + double scale = ConfigManager.Config.Video.VideoScale; + if(!ConfigManager.Config.Video.UseExclusiveFullscreen) { + //Go into fullscreen mode right away + SetFullscreenState(true); + } + + _fullscreenRequested = true; + foreach(string option in switches) { + if(option.StartsWith("/videoscale=")) { + _switchOptionScale = scale; + } + } + }*/ + } + + public void LoadGameFromCommandLine() + { + List switches = CommandLineHelper.PreprocessCommandLineArguments(_args, false); + + string romPath; + CommandLineHelper.GetRomPathFromCommandLine(switches, out romPath, out _luaScriptsToLoad); + + if(romPath != null) { + EmuRunner.LoadRom(romPath); + } else { + if(!EmuRunner.IsRunning()) { + //When no ROM is loaded, only process Lua scripts if a ROM was specified as a command line param + _luaScriptsToLoad.Clear(); + _movieToRecord = null; + //_loadLastSessionRequested = false; + } else { + //No game was specified, but a game is running already, load the scripts right away + ProcessPostLoadCommandSwitches(); + } + } + } + + private void ProcessPostLoadCommandSwitches() + { + //TODO + /*if(_luaScriptsToLoad.Count > 0) { + foreach(string luaScript in _luaScriptsToLoad) { + frmScript scriptWindow = DebugWindowManager.OpenScriptWindow(true); + scriptWindow.LoadScriptFile(luaScript); + } + _luaScriptsToLoad.Clear(); + } + + if(_movieToRecord != null) { + if(EmuApi.MovieRecording()) { + EmuApi.MovieStop(); + } + RecordMovieOptions options = new RecordMovieOptions(_movieToRecord, "", "", RecordMovieFrom.StartWithSaveData); + EmuApi.MovieRecord(ref options); + _movieToRecord = null; + } + + if(_loadLastSessionRequested) { + _loadLastSessionRequested = false; + EmuRunner.LoadLastSession(); + }*/ + } + + public static List PreprocessCommandLineArguments(string[] args, bool toLower) + { + var switches = new List(); + for(int i = 0; i < args.Length; i++) { + if(args[i] != null) { + string arg = args[i].Trim(); + if(arg.StartsWith("--")) { + arg = "/" + arg.Substring(2); + } else if(arg.StartsWith("-")) { + arg = "/" + arg.Substring(1); + } + + if(toLower) { + arg = arg.ToLowerInvariant(); + } + switches.Add(arg); + } + } + return switches; + } + + public static void GetRomPathFromCommandLine(List switches, out string romPath, out List luaScriptsToLoad) + { + Func getValidPath = (string path, bool forLua) => { + path = path.Trim(); + if(path.ToLower().EndsWith(".lua") == forLua) { + try { + if(!File.Exists(path)) { + //Try loading file as a relative path to the folder Mesen was started from + path = Path.Combine(Program.OriginalFolder, path); + } + if(File.Exists(path)) { + return path; + } + } catch { } + } + return null; + }; + + //Check if any Lua scripts were specified + luaScriptsToLoad = new List(); + foreach(string arg in switches) { + string path = getValidPath(arg, true); + if(path != null) { + luaScriptsToLoad.Add(path); + } + } + + romPath = null; + foreach(string arg in switches) { + string path = getValidPath(arg, false); + if(path != null) { + romPath = path; + break; + } + } + } + } +}