Skip to content

Commit

Permalink
Implement importing appx (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
MisteFr authored Feb 19, 2021
1 parent 7838f57 commit 818cfcb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vs
x64
packages
packages
Debug
14 changes: 11 additions & 3 deletions MCLauncher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@
</Window.Resources>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox Name="VersionList" d:DataContext="{d:DesignData /SampleData/Versions.xaml}" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch" Grid.Row="0" Margin="0,0,0,10">
<Button Content="Import"
Height="40"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
Margin="0,0,0,10"
VerticalAlignment="Center"
Click="ImportButtonClicked"
Grid.Row="0" />
<ListBox Name="VersionList" d:DataContext="{d:DesignData /SampleData/Versions.xaml}" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch" Grid.Row="1" Margin="0,0,0,10">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl x:Name="control" Content="{Binding}" ContentTemplate="{StaticResource templateVersionNotInstalled}"/>
Expand All @@ -81,14 +90,13 @@
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Row="1" VerticalAlignment="Center" Margin="0,5,0,5">
<Grid Grid.Row="2" VerticalAlignment="Center" Margin="0,5,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Row="0" Grid.Column="0" Name="ShowBetasCheckbox" Content="Show beta versions" Checked="ShowBetaVersionsCheck_Changed" Unchecked="ShowBetaVersionsCheck_Changed" IsChecked="{Binding UserPrefs.ShowBetas }" />
<CheckBox Grid.Row="0" Grid.Column="1" Margin="15,0,0,0" Name="ShowInstalledVersionsOnlyCheckbox" Content="Show installed versions only" Checked="ShowInstalledOnlyCheck_Changed" Unchecked="ShowInstalledOnlyCheck_Changed" IsChecked="{Binding UserPrefs.ShowInstalledOnly }" />
</Grid>

</Grid>
</Window>
51 changes: 48 additions & 3 deletions MCLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;

namespace MCLauncher {
using System.ComponentModel;
Expand All @@ -26,6 +27,7 @@ public partial class MainWindow : Window, ICommonVersionCommands {

private static readonly string MINECRAFT_PACKAGE_FAMILY = "Microsoft.MinecraftUWP_8wekyb3d8bbwe";
private static readonly string PREFS_PATH = @"preferences.json";
private static readonly string IMPORTED_VERSIONS_PATH = @"imported_versions";

private VersionList _versions;
public Preferences UserPrefs { get; }
Expand All @@ -47,7 +49,7 @@ public MainWindow() {
RewritePrefs();
}

_versions = new VersionList("versions.json", this);
_versions = new VersionList("versions.json", IMPORTED_VERSIONS_PATH, this);
VersionList.ItemsSource = _versions;
var view = CollectionViewSource.GetDefaultView(VersionList.ItemsSource) as CollectionView;
view.Filter = VersionListFilter;
Expand All @@ -65,9 +67,35 @@ public MainWindow() {
} catch (Exception e) {
Debug.WriteLine("List download failed:\n" + e.ToString());
}
await _versions.LoadImported();
});
}

private async void ImportButtonClicked(object sender, RoutedEventArgs e) {
Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> result = openFileDlg.ShowDialog();
if (result == true) {
string directory = Path.Combine(IMPORTED_VERSIONS_PATH, openFileDlg.SafeFileName);
if (Directory.Exists(directory)) {
MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("A version with the same name was already imported. Do you want to delete it ?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes) {
Directory.Delete(directory, true);
} else {
return;
}
}

var versionEntry = _versions.AddEntry(openFileDlg.SafeFileName, directory);
await Task.Run(() => {
versionEntry.StateChangeInfo = new VersionStateChangeInfo();
versionEntry.StateChangeInfo.IsExtracting = true;
ZipFile.ExtractToDirectory(openFileDlg.FileName, directory);
versionEntry.StateChangeInfo = null;
});
MessageBox.Show("Successfully imported appx: " + openFileDlg.FileName);
}
}

public ICommand LaunchCommand => new RelayCommand((v) => InvokeLaunch((Version)v));

public ICommand RemoveCommand => new RelayCommand((v) => InvokeRemove((Version)v));
Expand Down Expand Up @@ -292,7 +320,13 @@ private void InvokeRemove(Version v) {
await UnregisterPackage(Path.GetFullPath(v.GameDirectory));
Directory.Delete(v.GameDirectory, true);
v.StateChangeInfo = null;
v.UpdateInstallStatus();
if (v.UUID == Version.UNKNOWN_UUID) {
Dispatcher.Invoke(() => _versions.Remove(v));
Debug.WriteLine("Removed imported version " + v.DisplayName);
} else {
v.UpdateInstallStatus();
Debug.WriteLine("Removed release version " + v.DisplayName);
}
});
}

Expand Down Expand Up @@ -345,6 +379,7 @@ public class Versions : List<Object> {
}

public class Version : NotifyPropertyChangedBase {
public static readonly string UNKNOWN_UUID = "UNKNOWN";

public Version() { }
public Version(string uuid, string name, bool isBeta, ICommonVersionCommands commands) {
Expand All @@ -354,13 +389,23 @@ public Version(string uuid, string name, bool isBeta, ICommonVersionCommands com
this.DownloadCommand = commands.DownloadCommand;
this.LaunchCommand = commands.LaunchCommand;
this.RemoveCommand = commands.RemoveCommand;
this.GameDirectory = "Minecraft-" + Name;
}
public Version(string uuid, string name, bool isBeta, string directory, ICommonVersionCommands commands) {
this.UUID = uuid;
this.Name = name;
this.IsBeta = isBeta;
this.DownloadCommand = commands.DownloadCommand;
this.LaunchCommand = commands.LaunchCommand;
this.RemoveCommand = commands.RemoveCommand;
this.GameDirectory = directory;
}

public string UUID { get; set; }
public string Name { get; set; }
public bool IsBeta { get; set; }

public string GameDirectory => "Minecraft-" + Name;
public string GameDirectory { get; set; }

public bool IsInstalled => Directory.Exists(GameDirectory);

Expand Down
19 changes: 18 additions & 1 deletion MCLauncher/VersionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,33 @@ namespace MCLauncher {
class VersionList : ObservableCollection<WPFDataTypes.Version> {

private readonly string _cacheFile;
private readonly string _importedDirectory;
private readonly WPFDataTypes.ICommonVersionCommands _commands;
private readonly HttpClient _client = new HttpClient();
HashSet<string> dbVersions = new HashSet<string>();

public VersionList(string cacheFile, WPFDataTypes.ICommonVersionCommands commands) {
public VersionList(string cacheFile, string importedDirectory, WPFDataTypes.ICommonVersionCommands commands) {
_cacheFile = cacheFile;
_importedDirectory = importedDirectory;
_commands = commands;
}

private void ParseList(JArray data) {
Clear();
// ([name, uuid, isBeta])[]
foreach (JArray o in data.AsEnumerable().Reverse()) {
dbVersions.Add(o[0].Value<string>());
Add(new WPFDataTypes.Version(o[1].Value<string>(), o[0].Value<string>(), o[2].Value<int>() == 1, _commands));
}
}

public async Task LoadImported() {
string[] subdirectoryEntries = await Task.Run(() => Directory.GetDirectories(_importedDirectory));
foreach (string subdirectory in subdirectoryEntries) {
AddEntry(Path.GetFileName(subdirectory), subdirectory);
}
}

public async Task LoadFromCache() {
try {
using (var reader = File.OpenText(_cacheFile)) {
Expand All @@ -47,5 +58,11 @@ public async Task DownloadList() {
ParseList(JArray.Parse(data));
}

public WPFDataTypes.Version AddEntry(string name, string path) {
var result = new WPFDataTypes.Version(WPFDataTypes.Version.UNKNOWN_UUID, name.Replace(".appx", ""), false, path, _commands);
Add(result);
return result;
}

}
}

0 comments on commit 818cfcb

Please sign in to comment.