Skip to content

Commit

Permalink
Add a way to change game directory
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed May 2, 2021
1 parent 5e55ff4 commit a808558
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 13 deletions.
2 changes: 2 additions & 0 deletions GenshinLauncher.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Genshin/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
3 changes: 0 additions & 3 deletions GenshinLauncher/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="MinWidth" Value="200" />
</Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="MinWidth" Value="125" />
</Style>
</Style.Resources>
</Style>
</ResourceDictionary>
Expand Down
3 changes: 2 additions & 1 deletion GenshinLauncher/GenshinLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<RepositoryUrl>https://github.com/sabihoshi/GenshinLauncher</RepositoryUrl>
<Description>A Genshin Impact launcher with more options in Modern Fluent UI.</Description>
<ApplicationIcon>GenshinImpactIcon.ico</ApplicationIcon>
<Version>1.1.0.1</Version>
<Version>1.2.0</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
92 changes: 87 additions & 5 deletions GenshinLauncher/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System.Configuration;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CliWrap;
using Microsoft.Win32;
using ModernWpf.Controls;
using Stylet;

namespace GenshinLauncher.ViewModels
{
public class MainWindowViewModel : Screen
{
public const string Title = "Genshin Impact Launcher";

public MainWindowViewModel()
{
Quality = new();
Expand All @@ -20,16 +25,93 @@ public MainWindowViewModel()

public Resolution Resolution { get; set; }

[UserScopedSetting]
public string GenshinLocation { get; set; } =
@"C:\Program Files\Genshin Impact\Genshin Impact Game\GenshinImpact.exe";
public string GenshinLocation { get; set; }

public string Title { get; } = "Genshin Impact Launcher";
private static string? InstallLocation => Registry.LocalMachine
.OpenSubKey(@"SOFTWARE\launcher", false)
?.GetValue("InstPath") as string;

public async Task<CommandResult> LaunchSelector() =>
await Client.WithArguments("-show-screen-selector")
.ExecuteAsync();

protected override async void OnInitialActivate()
{
if (!TryGetLocation())
await LocationMissing();
}

private async Task LocationMissing()
{
var dialog = new ContentDialog
{
Title = "Error",
Content = "Could not find Game's Location",

PrimaryButtonText = "Find Manually...",
SecondaryButtonText = "Ignore",
CloseButtonText = "Exit"
};

var result = await dialog.ShowAsync();

switch (result)
{
case ContentDialogResult.None:
RequestClose();
break;
case ContentDialogResult.Primary:
await SetLocation();
break;
case ContentDialogResult.Secondary:
break;
}
}

public async Task SetLocation()
{
var openFileDialog = new OpenFileDialog
{
Filter = "Executable|*.exe|All files (*.*)|*.*",
InitialDirectory = InstallLocation is null
? string.Empty
: Path.Combine(InstallLocation, "Genshin Impact Game")
};

var success = openFileDialog.ShowDialog() == true;
var set = TrySetLocation(openFileDialog.FileName);

if (!(success && set)) await LocationMissing();
}

private bool TryGetLocation()
{
var location = @"C:\Program Files\Genshin Impact\Genshin Impact Game\GenshinImpact.exe";

if (TrySetLocation(location))
return true;

location = InstallLocation + @"\Genshin Impact Game\GenshinImpact.exe";

if (TrySetLocation(location))
return true;

location = AppContext.BaseDirectory + "GenshinImpact.exe";

return TrySetLocation(location);
}

private bool TrySetLocation(string location)
{
if (File.Exists(location))
{
GenshinLocation = location;
return true;
}

return false;
}

public void LaunchGame()
{
var client = Client
Expand Down
22 changes: 18 additions & 4 deletions GenshinLauncher/Views/MainWindowView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
xmlns:genshinLauncher="clr-namespace:GenshinLauncher"
d:DataContext="{d:DesignInstance Type=viewModels:MainWindowViewModel}"

Title="{Binding Title}"
Width="500" Height="450"
MinWidth="500" MinHeight="450"
Title="{x:Static viewModels:MainWindowViewModel.Title}"
Width="500" Height="600"
MinWidth="500" MinHeight="600"
ui:WindowHelper.UseModernWindowStyle="True"
ui:TitleBar.ExtendViewIntoTitleBar="True">

Expand All @@ -30,7 +30,7 @@
<ui:SimpleStackPanel>
<TextBlock
Style="{StaticResource TitleTextBlockStyle}"
Text="{Binding Title}" />
Text="{x:Static viewModels:MainWindowViewModel.Title}" />

<ui:SimpleStackPanel Orientation="Horizontal">
<ui:ToggleSwitch
Expand Down Expand Up @@ -62,6 +62,20 @@
SelectedItem="{Binding Resolution}" />
</GroupBox>
</ui:SimpleStackPanel>
<GroupBox Header="Path">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBox Grid.Column="0" Text="{Binding GenshinLocation}" TextWrapping="WrapWithOverflow" />
<Button Grid.Column="1" Margin="5,0,0,0"
Command="{s:Action SetLocation}">
<ui:FontIcon Glyph="&#xE8E5;" />
</Button>
</Grid>
</GroupBox>

<ui:SimpleStackPanel
Orientation="Horizontal"
Expand Down

0 comments on commit a808558

Please sign in to comment.