Skip to content

Commit

Permalink
Integrated Working Directory to application
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmightyLks committed Feb 26, 2023
1 parent 7a614a2 commit 922cdc4
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static string GetProductName(string exePath)
try { exePath = SymlinkResolver.GetFinalPathName(exePath); }
catch (Exception e) { Errors.HandleException(e, Resources.ErrorAddApplicationCantReadSymlink.Get()); }

var config = new ApplicationConfig(Path.GetFileName(exePath).ToLower(), GetProductName(exePath), exePath);
var config = new ApplicationConfig(Path.GetFileName(exePath).ToLower(), GetProductName(exePath), exePath, Path.GetDirectoryName(exePath));

// Set AppName if empty & Ensure no duplicate ID.
if (string.IsNullOrEmpty(config.AppName))
Expand Down
1 change: 1 addition & 0 deletions source/Reloaded.Mod.Launcher.Lib/Misc/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class Constants
public const string ParameterKill = "--kill";
public const string ParameterLaunch = "--launch";
public const string ParameterArguments = "--arguments";
public const string WorkingDirectory = "--working-directory";
public const string ParameterDownload = "--download";
public const string ParameterR2Pack = "--r2pack";
public const string ParameterR2PackDownload = "--r2packdl";
Expand Down
10 changes: 6 additions & 4 deletions source/Reloaded.Mod.Launcher.Lib/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,18 @@ private static void LaunchApplicationAndExit(string applicationToLaunch)
if (application != null)
arguments = $"{arguments} {application.Config.AppArguments}";

_commandLineArguments.TryGetValue(Constants.ParameterArguments, out var workingDirectory);

// Show warning for Wine users.
if (Shared.Environment.IsWine)
{
// Set up UI Resources, since they're needed for the dialog.
if (CompatibilityDialogs.WineShowLaunchDialog())
StartGame(applicationToLaunch, arguments);
StartGame(applicationToLaunch, arguments, workingDirectory);
}
else
{
StartGame(applicationToLaunch, arguments);
StartGame(applicationToLaunch, arguments, workingDirectory);
}
}

Expand Down Expand Up @@ -139,10 +141,10 @@ private static void OpenPackAndExit(string r2PackLocation)

private static void InitControllerSupport() => Actions.InitControllerSupport();

private static void StartGame(string applicationToLaunch, string arguments)
private static void StartGame(string applicationToLaunch, string arguments, string? workingDirectory = null)
{
// Launch the application.
var launcher = ApplicationLauncher.FromLocationAndArguments(applicationToLaunch, arguments);
var launcher = ApplicationLauncher.FromLocationAndArguments(applicationToLaunch, arguments, workingDirectory);
launcher.Start();
}

Expand Down
14 changes: 8 additions & 6 deletions source/Reloaded.Mod.Launcher.Lib/Utility/ApplicationLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ namespace Reloaded.Mod.Launcher.Lib.Utility;
public class ApplicationLauncher
{
private string _location = "";
private string? _arguments = "";
private string _arguments = "";
private string _workingDirectory = "";

/// <summary/>
private ApplicationLauncher() { }

/// <summary>
/// Creates an <see cref="ApplicationLauncher"/> from a whole commandline, i.e. path and arguments.
/// </summary>
public static ApplicationLauncher FromLocationAndArguments(string location, string? arguments)
public static ApplicationLauncher FromLocationAndArguments(string location, string? workingDirectory, string? arguments)
{
var launcher = new ApplicationLauncher
{
_arguments = arguments,
_location = location
_arguments = arguments ?? "",
_location = location,
_workingDirectory = workingDirectory ?? Path.GetDirectoryName(location)!
};

if (!File.Exists(launcher._location))
Expand All @@ -33,7 +35,7 @@ public static ApplicationLauncher FromLocationAndArguments(string location, stri
/// </summary>
public static ApplicationLauncher FromApplicationConfig(PathTuple<ApplicationConfig> config)
{
return FromLocationAndArguments($"{ApplicationConfig.GetAbsoluteAppLocation(config)}", $"{config.Config.AppArguments}");
return FromLocationAndArguments($"{ApplicationConfig.GetAbsoluteAppLocation(config)}", config.Config.WorkingDirectory, $"{config.Config.AppArguments}");
}

/// <summary>
Expand All @@ -52,7 +54,7 @@ public void Start()

bool success = Native.CreateProcessW(null, $"\"{_location}\" {_arguments}", ref lpProcessAttributes,
ref lpThreadAttributes, false, Native.ProcessCreationFlags.CREATE_SUSPENDED,
IntPtr.Zero, Path.GetDirectoryName(_location)!, ref startupInfo, ref processInformation);
IntPtr.Zero, _workingDirectory, ref startupInfo, ref processInformation);

if (!success)
{
Expand Down
1 change: 1 addition & 0 deletions source/Reloaded.Mod.Launcher/Assets/Languages/en-GB.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<sys:String x:Key="AddAppName">Application Name</sys:String>
<sys:String x:Key="AddAppLocation">Executable Location</sys:String>
<sys:String x:Key="AddAppArguments">Commandline Arguments</sys:String>
<sys:String x:Key="AddAppWorkingDirectory">Working Directory</sys:String>

<sys:String x:Key="AddAppAdvancedOptions">Advanced Tools &amp; Options</sys:String>
<sys:String x:Key="AddAppCreateShortcut">Create Shortcut</sys:String>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Button = Sewer56.UI.Controller.Core.Enums.Button;

namespace Reloaded.Mod.Launcher.Pages.BaseSubpages.ApplicationSubPages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@
MinWidth="{StaticResource DetailsColumnWidth}"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"/>

<TextBox
x:Name="WorkingDirectory"
Style="{DynamicResource TextboxWithPlaceholder}"
Text="{Binding Application.Config.WorkingDirectory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Tag="{DynamicResource AddAppWorkingDirectory}"
ToolTip="{DynamicResource AddAppWorkingDirectory}"
ToolTipService.InitialShowDelay="0"
Margin="{DynamicResource CommonItemVerticalMargin}"
MinWidth="{StaticResource DetailsColumnWidth}"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"/>

<!-- Package Resolver Options. -->
<controls:PopupLabel ButtonText="{DynamicResource AddAppModSources}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
<OutputFile>$(OutDir)\X86\Bootstrapper\$(TargetName)$(TargetExt)</OutputFile>
<ProgramDatabaseFile>$(OutDir)\X86\Bootstrapper\$(TargetName).pdb</ProgramDatabaseFile>
<DelayLoadDLLs>%(DelayLoadDLLs)</DelayLoadDLLs>
<AdditionalOptions></AdditionalOptions>
<AdditionalOptions>
</AdditionalOptions>
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
Expand Down Expand Up @@ -141,7 +142,8 @@
<OutputFile>$(OutDir)\X64\Bootstrapper\$(TargetName)$(TargetExt)</OutputFile>
<ProgramDatabaseFile>$(OutDir)\X64\Bootstrapper\$(TargetName).pdb</ProgramDatabaseFile>
<DelayLoadDLLs>%(DelayLoadDLLs)</DelayLoadDLLs>
<AdditionalOptions></AdditionalOptions>
<AdditionalOptions>
</AdditionalOptions>
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
Expand Down Expand Up @@ -195,7 +197,8 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<DisableSpecificWarnings>6262;26451;4099</DisableSpecificWarnings>
<StringPooling>true</StringPooling>
<AdditionalOptions></AdditionalOptions>
<AdditionalOptions>
</AdditionalOptions>
<BufferSecurityCheck>false</BufferSecurityCheck>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
</ClCompile>
Expand Down
10 changes: 7 additions & 3 deletions source/Reloaded.Mod.Loader.IO/Config/ApplicationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ApplicationConfig : ObservableObject, IApplicationConfig, IConfig<A
public string AppIcon { get; set; } = DefaultIcon;
public bool AutoInject { get; set; } = false;
public string[] EnabledMods { get; set; }
public string WorkingDirectory { get; set; }

// V2

Expand All @@ -36,15 +37,16 @@ public class ApplicationConfig : ObservableObject, IApplicationConfig, IConfig<A

public ApplicationConfig() { }

public ApplicationConfig(string appId, string appName, string appLocation)
public ApplicationConfig(string appId, string appName, string appLocation, string workingDirectory = null)
{
AppId = appId;
AppName = appName;
AppLocation = appLocation;
EnabledMods = EmptyArray<string>.Instance;
EnabledMods = EmptyArray<string>.Instance;
WorkingDirectory = workingDirectory;
}

public ApplicationConfig(string appId, string appName, string appLocation, string[] enabledMods) : this(appId, appName, appLocation)
public ApplicationConfig(string appId, string appName, string appLocation, string[] enabledMods, string workingDirectory = null) : this(appId, appName, appLocation, workingDirectory)
{
EnabledMods = enabledMods;
}
Expand Down Expand Up @@ -184,6 +186,8 @@ public static string GetAbsoluteAppLocation(PathTuple<ApplicationConfig> config)
return Path.GetFullPath(finalPath);
}



// Reflection-less JSON
public static JsonTypeInfo<ApplicationConfig> GetJsonTypeInfo(out bool supportsSerialize)
{
Expand Down

0 comments on commit 922cdc4

Please sign in to comment.