/// <summary> /// Creates a .bat script the user can use to start the game easily, using the options set in /// <paramref name="settings"/>. /// </summary> /// <param name="settings">Contains the parameters to use.</param> /// <param name="exportPath">The path of the .bat file to create.</param> /// <exception cref="System.ArgumentNullException"><paramref name="settings"/> or /// <paramref name="exportPath"/> is null, or username and password are not set.</exception> /// <exception cref="System.IO.IOException">The file could not be opened or there was an error while /// writing to it.</exception> public static void ExportToBat(StartupSettings settings, string exportPath) { settings.ThrowIfNull("settings"); exportPath.ThrowIfNull("exportPath"); settings.Username.ThrowIfNull("settings.Username"); settings.Password.ThrowIfNull("settings.Password"); StringBuilder batContent = new StringBuilder(); batContent.AppendLine("@echo off"); batContent.AppendLine("REM Make sure you understand the risks associated with storing your password in a file on your computer."); batContent.AppendLine(string.Format("REM Generated by {0} {1}", VersionInfo.AssemblyTitle, VersionInfo.AssemblyVersion)); batContent.AppendLine(); string exeName = Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location); string exeFullPath = System.Reflection.Assembly.GetExecutingAssembly().Location; string args = GetArgs(settings); // Try DFOCP.exe in the current directory first. If it doesn't exist (the user didn't save the .bat // to the DFOCP directory [reasonable considering applications are not really supposed to save stuff // to their program directory]), use the full path to DFOCP.exe. // // This allows the user to move the DFOCP directory if they saved the .bat in it. // If they did not save the .bat to the DFOCP directory and they moved the DFOCP directory, a // reasonably intelligent user would realize that there's no way the script could know about the moving. batContent.AppendLine(string.Format("IF NOT EXIST \"{0}\" (goto :fullpath)", BatEscapeInQuotes(exeName))); batContent.AppendLine(string.Format("\"{0}\" {1}", BatEscapeInQuotes(exeName), args)); batContent.AppendLine("IF ERRORLEVEL 1 (goto :error) ELSE (goto :success)"); batContent.AppendLine(":fullpath"); batContent.AppendLine(string.Format("\"{0}\" {1}", BatEscapeInQuotes(exeFullPath), args)); batContent.AppendLine("IF ERRORLEVEL 1 (goto :error) ELSE (goto :success)"); batContent.AppendLine(":error"); batContent.AppendLine("pause"); batContent.AppendLine(":success"); try { File.WriteAllText(exportPath, batContent.ToString()); } catch (Exception ex) { if (ex is ArgumentException || ex is IOException || ex is UnauthorizedAccessException || ex is NotSupportedException || ex is System.Security.SecurityException) { throw new IOException(string.Format("Could not save to {0}. {1}", exportPath, ex.Message), ex); } else { throw; } } }
public static void Save(StartupSettings settings) { Logging.Log.InfoFormat("Saving settings to {0}.", Paths.SettingsPath); XElement root = new XElement(s_rootName); string settingsVersion = string.Format("{0}.{1}", s_majorVersion, s_minorVersion); root.SetAttributeValue(s_settingsVersion, settingsVersion); if (settings.RememberUsername == true) { AddElement(root, s_rememberMe, GetBoolString(settings.RememberUsername)); AddElement(root, s_username, settings.Username); } AddElement(root, s_closePopup, GetBoolString(settings.ClosePopup)); AddElement(root, s_launchWindowed, GetBoolString(settings.LaunchWindowed)); if (settings.GameWindowHeight.HasValue || settings.GameWindowWidth.HasValue) { XElement sizeElement = new XElement(s_gameWindowSize); AddElement(sizeElement, s_gameWindowSize_Width, GetIntString(settings.GameWindowWidth)); AddElement(sizeElement, s_gameWindowSize_Height, GetIntString(settings.GameWindowHeight)); root.Add(sizeElement); } foreach (string switchableName in settings.SwitchFile.Keys) { XElement switchableElement = new XElement(s_switchableElement); AddElement(switchableElement, s_switchableSwitch, GetBoolString(settings.SwitchFile[switchableName].Value)); switchableElement.SetAttributeValue(s_switchableNameAttr, switchableName); root.Add(switchableElement); } try { root.Save(Paths.SettingsPath); Logging.Log.InfoFormat("Settings saved."); } catch (Exception ex) { // I'm kinda guessing with these exceptions...XElement.Save() does not document what exceptions it can throw -_- if (ex is IOException || ex is UnauthorizedAccessException || ex is NotSupportedException || ex is System.Security.SecurityException) { Logging.Log.ErrorFormat("Could not save settings file {0}: {1}", Paths.SettingsPath, ex.Message); } else { throw; } } }
/// <summary> /// Creates a .bat script the user can use to start the game easily, using the options set in /// <paramref name="settings"/>. /// </summary> /// <param name="settings">Contains the parameters to use.</param> /// <param name="exportPath">The path of the .bat file to create.</param> /// <exception cref="System.ArgumentNullException"><paramref name="settings"/> or /// <paramref name="exportPath"/> is null, or username and password are not set.</exception> /// <exception cref="System.IO.IOException">The file could not be opened or there was an error while /// writing to it.</exception> public static void ExportToBat( StartupSettings settings, string exportPath ) { settings.ThrowIfNull( "settings" ); exportPath.ThrowIfNull( "exportPath" ); settings.Username.ThrowIfNull( "settings.Username" ); settings.Password.ThrowIfNull( "settings.Password" ); StringBuilder batContent = new StringBuilder(); batContent.AppendLine( "@echo off" ); batContent.AppendLine( "REM Make sure you understand the risks associated with storing your password in a file on your computer." ); batContent.AppendLine( string.Format( "REM Generated by {0} {1}", VersionInfo.AssemblyTitle, VersionInfo.AssemblyVersion ) ); batContent.AppendLine(); string exeName = Path.GetFileName( System.Reflection.Assembly.GetExecutingAssembly().Location ); string exeFullPath = System.Reflection.Assembly.GetExecutingAssembly().Location; string args = GetArgs( settings ); // Try DFOCP.exe in the current directory first. If it doesn't exist (the user didn't save the .bat // to the DFOCP directory [reasonable considering applications are not really supposed to save stuff // to their program directory]), use the full path to DFOCP.exe. // // This allows the user to move the DFOCP directory if they saved the .bat in it. // If they did not save the .bat to the DFOCP directory and they moved the DFOCP directory, a // reasonably intelligent user would realize that there's no way the script could know about the moving. batContent.AppendLine( string.Format( "IF NOT EXIST \"{0}\" (goto :fullpath)", BatEscapeInQuotes( exeName ) ) ); batContent.AppendLine( string.Format( "\"{0}\" {1}", BatEscapeInQuotes( exeName ), args ) ); batContent.AppendLine( "IF ERRORLEVEL 1 (goto :error) ELSE (goto :success)" ); batContent.AppendLine( ":fullpath" ); batContent.AppendLine( string.Format( "\"{0}\" {1}", BatEscapeInQuotes( exeFullPath ), args ) ); batContent.AppendLine( "IF ERRORLEVEL 1 (goto :error) ELSE (goto :success)" ); batContent.AppendLine( ":error" ); batContent.AppendLine( "pause" ); batContent.AppendLine( ":success" ); try { File.WriteAllText( exportPath, batContent.ToString() ); } catch ( Exception ex ) { if ( ex is ArgumentException || ex is IOException || ex is UnauthorizedAccessException || ex is NotSupportedException || ex is System.Security.SecurityException ) { throw new IOException( string.Format( "Could not save to {0}. {1}", exportPath, ex.Message ), ex ); } else { throw; } } }
private void ctlMainForm_Load(object sender, EventArgs e) { Logging.Log.Debug("Loading main window."); Rectangle primaryScreenSize = Screen.PrimaryScreen.Bounds; Logging.Log.DebugFormat("Primary screen size is {0}x{1}", primaryScreenSize.Width, primaryScreenSize.Height); int maxWidth = Math.Min(primaryScreenSize.Width, DfoLauncher.GetWidthFromHeight(primaryScreenSize.Height)); ctlWindowSizeSlider.Minimum = DfoLauncher.DefaultGameWindowWidth; ctlWindowSizeSlider.Maximum = Math.Max(maxWidth, DfoLauncher.DefaultGameWindowWidth); Logging.Log.DebugFormat("Allowable game window width: {0}-{1}", ctlWindowSizeSlider.Minimum, ctlWindowSizeSlider.Maximum); // Initialize window size UI ctlWindowSizeSlider_ValueChanged(ctlWindowSizeSlider, EventArgs.Empty); // Bind switchable files to checkboxes IDictionary <string, CheckBox> switchableFileCheckboxes = GetSwitchableCheckboxes(); foreach (ISwitchableFile switchableFile in m_parsedArgs.Settings.SwitchableFiles.Values) { if (switchableFileCheckboxes.ContainsKey(switchableFile.Name)) { SwitchableFiles.Add(switchableFile.Name, new CheckboxSwitchableFile(switchableFileCheckboxes[switchableFile.Name], switchableFile)); } else { // Hmmm...No UI binding for a switchable file. Add it as a switchable file and let // setting/command-line behavior take effect. SwitchableFiles.Add(switchableFile.Name, new PlainUiSwitchableFile(switchableFile)); } } m_savedSettings = SettingsLoader.Load(); ApplySettingsAndArguments(); FixSwitchableFilesIfNeeded(); ctlUsername.Select(); Logging.Log.Debug("Main window loaded."); }
private StartupSettings GetCurrentSettings() { StartupSettings settings = new StartupSettings(); if (m_parsedArgs.Settings.DfoDir != null) { settings.DfoDir = DfoDir; } foreach (IUiSwitchableFile uiBoundFile in SwitchableFiles.Values) { SwitchableFile switchableFile = new SwitchableFile(uiBoundFile); if (m_parsedArgs.Settings.SwitchableFiles[switchableFile.Name].CustomFile == null) { // Custom file was not specified on commmand-line and there is no UI, so the user wants // the default. switchableFile.CustomFile = null; } if (m_parsedArgs.Settings.SwitchableFiles[switchableFile.Name].TempFile == null) { // ditto switchableFile.TempFile = null; } settings.SwitchableFiles[switchableFile.Name] = switchableFile; settings.SwitchFile[switchableFile.Name] = switchableFile.Switch; } settings.ClosePopup = ClosePopup; settings.LaunchWindowed = LaunchWindowed; settings.Password = Password; settings.RememberUsername = RememberMe; settings.Username = Username; settings.GameWindowWidth = GameWindowStartingWidth; settings.GameWindowHeight = GameWindowStartingHeight; return(settings); }
private StartupSettings GetCurrentSettings() { StartupSettings settings = new StartupSettings(); if ( m_parsedArgs.Settings.DfoDir != null ) { settings.DfoDir = DfoDir; } foreach ( IUiSwitchableFile uiBoundFile in SwitchableFiles.Values ) { SwitchableFile switchableFile = new SwitchableFile( uiBoundFile ); if ( m_parsedArgs.Settings.SwitchableFiles[ switchableFile.Name ].CustomFile == null ) { // Custom file was not specified on commmand-line and there is no UI, so the user wants // the default. switchableFile.CustomFile = null; } if ( m_parsedArgs.Settings.SwitchableFiles[ switchableFile.Name ].TempFile == null ) { // ditto switchableFile.TempFile = null; } settings.SwitchableFiles[ switchableFile.Name ] = switchableFile; settings.SwitchFile[ switchableFile.Name ] = switchableFile.Switch; } settings.ClosePopup = ClosePopup; settings.LaunchWindowed = LaunchWindowed; settings.Password = Password; settings.RememberUsername = RememberMe; settings.Username = Username; settings.GameWindowWidth = GameWindowStartingWidth; settings.GameWindowHeight = GameWindowStartingHeight; return settings; }
private void ctlMainForm_Load( object sender, EventArgs e ) { Logging.Log.Debug( "Loading main window." ); Rectangle primaryScreenSize = Screen.PrimaryScreen.Bounds; Logging.Log.DebugFormat( "Primary screen size is {0}x{1}", primaryScreenSize.Width, primaryScreenSize.Height ); int maxWidth = Math.Min( primaryScreenSize.Width, DfoLauncher.GetWidthFromHeight( primaryScreenSize.Height ) ); ctlWindowSizeSlider.Minimum = DfoLauncher.DefaultGameWindowWidth; ctlWindowSizeSlider.Maximum = Math.Max( maxWidth, DfoLauncher.DefaultGameWindowWidth ); Logging.Log.DebugFormat( "Allowable game window width: {0}-{1}", ctlWindowSizeSlider.Minimum, ctlWindowSizeSlider.Maximum ); // Initialize window size UI ctlWindowSizeSlider_ValueChanged( ctlWindowSizeSlider, EventArgs.Empty ); // Bind switchable files to checkboxes IDictionary<string, CheckBox> switchableFileCheckboxes = GetSwitchableCheckboxes(); foreach ( ISwitchableFile switchableFile in m_parsedArgs.Settings.SwitchableFiles.Values ) { if ( switchableFileCheckboxes.ContainsKey( switchableFile.Name ) ) { SwitchableFiles.Add( switchableFile.Name, new CheckboxSwitchableFile( switchableFileCheckboxes[ switchableFile.Name ], switchableFile ) ); } else { // Hmmm...No UI binding for a switchable file. Add it as a switchable file and let // setting/command-line behavior take effect. SwitchableFiles.Add( switchableFile.Name, new PlainUiSwitchableFile( switchableFile ) ); } } m_savedSettings = SettingsLoader.Load(); ApplySettingsAndArguments(); FixSwitchableFilesIfNeeded(); ctlUsername.Select(); Logging.Log.Debug( "Main window loaded." ); }
private static string GetArgs(StartupSettings settings) { StringBuilder args = new StringBuilder(); args.Append(string.Format("-cli \"--username={0}\" \"--password={1}\"", BatEscapeInQuotes(settings.Username), BatEscapeInQuotes(settings.Password))); if (settings.ClosePopup.HasValue) { if (settings.ClosePopup.Value) { args.Append(" -closepopup"); } else { args.Append(" -noclosepopup"); } } if (settings.LaunchWindowed.HasValue) { if (settings.LaunchWindowed.Value) { args.Append(" -windowed"); if (settings.GameWindowWidth.HasValue) { args.AppendFormat(" --width={0}", settings.GameWindowWidth.Value.ToString()); } if (settings.GameWindowHeight.HasValue) { args.AppendFormat(" --height={0}", settings.GameWindowHeight.Value.ToString()); } } else { args.Append(" -full"); } } if (settings.DfoDir != null) { args.Append(string.Format(" \"--dfodir={0}\"", BatEscapeInQuotes(settings.DfoDir))); } foreach (ISwitchableFile switchableFile in settings.SwitchableFiles.Values) { if (settings.SwitchFile[switchableFile.Name].HasValue) { if (settings.SwitchFile[switchableFile.Name].Value) { args.Append(string.Format(" -{0}", switchableFile.WhetherToSwitchArg)); } else { args.Append(string.Format(" -no{0}", switchableFile.WhetherToSwitchArg)); } } if (switchableFile.CustomFile != null) { args.Append(string.Format(" \"--{0}={1}\"", switchableFile.CustomFileArg, BatEscapeInQuotes(switchableFile.CustomFile))); } if (switchableFile.TempFile != null) { args.Append(string.Format(" \"--{0}={1}\"", switchableFile.TempFileArg, BatEscapeInQuotes(switchableFile.TempFile))); } } return(args.ToString()); }
// bool = 0 or 1, anything else is an error public static StartupSettings Load() { StartupSettings settings = new StartupSettings(); XElement root; try { Logging.Log.InfoFormat("Loading settings from {0}.", Paths.SettingsPath); root = XElement.Load(Paths.SettingsPath); } catch (Exception ex) { // I'm kinda guessing with these exceptions...XElement.Load() does not document what exceptions it can throw -_- if (ex is IOException || ex is UnauthorizedAccessException || ex is NotSupportedException || ex is System.Security.SecurityException || ex is XmlException) { Logging.Log.WarnFormat("Could not load settings file {0}: {1}", Paths.SettingsPath, ex.Message); return(new StartupSettings()); } else { throw; } } XElement settingsRoot = null; var settingsRootCollection = root.DescendantsAndSelf(s_rootName); foreach (var element in settingsRootCollection) { settingsRoot = element; break; } if (settingsRoot == null) { Logging.Log.ErrorFormat("{0} element not found.", s_rootName); return(new StartupSettings()); } string settingsVersion = GetAttribute(settingsRoot, s_settingsVersion); string majorVersion = s_majorVersion; string minorVersion = s_minorVersion; if (settingsVersion == null) { Logging.Log.ErrorFormat("Settings version not found. Assuming {0}.{1}", majorVersion, minorVersion); } else { string[] versionSplit = settingsVersion.Split('.'); if (versionSplit.Length < 2) { Logging.Log.ErrorFormat("Settings version '{0}' is badly formatted. Assuming {1}.{2}.", settingsVersion, majorVersion, minorVersion); } else { majorVersion = versionSplit[0]; minorVersion = versionSplit[1]; if (majorVersion != s_majorVersion && majorVersion != "2") { Logging.Log.InfoFormat( "Major version of settings file format is {0}, which is different from {1}. Ignoring file and using default settings.", majorVersion, s_majorVersion); return(new StartupSettings()); } } } settings.Username = GetString(settingsRoot, s_username); settings.RememberUsername = GetBool(settingsRoot, s_rememberMe); settings.ClosePopup = GetBool(settingsRoot, s_closePopup); settings.LaunchWindowed = GetBool(settingsRoot, s_launchWindowed); if (majorVersion == "2") { settings.SwitchFile[SwitchableFile.Soundpacks] = GetBool(settingsRoot, s_switchSoundpacks); } else { var switchableCollection = settingsRoot.Elements(s_switchableElement); foreach (XElement switchable in switchableCollection) { string switchableName = GetAttribute(switchable, s_switchableNameAttr); if (switchableName == null) { continue; } bool?whetherToSwitch = GetBool(switchable, s_switchableSwitch); settings.SwitchFile[switchableName] = whetherToSwitch; } var windowSizeCollection = settingsRoot.Elements(s_gameWindowSize); foreach (XElement sizeElement in windowSizeCollection) { int?windowWidth = GetInt(sizeElement, s_gameWindowSize_Width); if (windowWidth != null) { settings.GameWindowWidth = windowWidth; } int?windowHeight = GetInt(sizeElement, s_gameWindowSize_Height); if (windowHeight != null) { settings.GameWindowHeight = windowHeight; } } } Logging.Log.Info("Settings loaded."); return(settings); }
private static string GetArgs( StartupSettings settings ) { StringBuilder args = new StringBuilder(); args.Append( string.Format( "-cli \"--username={0}\" \"--password={1}\"", BatEscapeInQuotes( settings.Username ), BatEscapeInQuotes( settings.Password ) ) ); if ( settings.ClosePopup.HasValue ) { if ( settings.ClosePopup.Value ) { args.Append( " -closepopup" ); } else { args.Append( " -noclosepopup" ); } } if ( settings.LaunchWindowed.HasValue ) { if ( settings.LaunchWindowed.Value ) { args.Append( " -windowed" ); if ( settings.GameWindowWidth.HasValue ) { args.AppendFormat( " --width={0}", settings.GameWindowWidth.Value.ToString() ); } if ( settings.GameWindowHeight.HasValue ) { args.AppendFormat( " --height={0}", settings.GameWindowHeight.Value.ToString() ); } } else { args.Append( " -full" ); } } if ( settings.DfoDir != null ) { args.Append( string.Format( " \"--dfodir={0}\"", BatEscapeInQuotes( settings.DfoDir ) ) ); } foreach ( ISwitchableFile switchableFile in settings.SwitchableFiles.Values ) { if ( settings.SwitchFile[ switchableFile.Name ].HasValue ) { if ( settings.SwitchFile[ switchableFile.Name ].Value ) { args.Append( string.Format( " -{0}", switchableFile.WhetherToSwitchArg ) ); } else { args.Append( string.Format( " -no{0}", switchableFile.WhetherToSwitchArg ) ); } } if ( switchableFile.CustomFile != null ) { args.Append( string.Format( " \"--{0}={1}\"", switchableFile.CustomFileArg, BatEscapeInQuotes( switchableFile.CustomFile ) ) ); } if ( switchableFile.TempFile != null ) { args.Append( string.Format( " \"--{0}={1}\"", switchableFile.TempFileArg, BatEscapeInQuotes( switchableFile.TempFile ) ) ); } } return args.ToString(); }