Skip to content

Commit

Permalink
Added a choice between downloading packages only, and fully installin…
Browse files Browse the repository at this point in the history
…g everything.

Added a notification and quit if there's no internet connection.
Improved Nexus file url grabbing.
Fixed filenames of nexus files.
Now starts on the install tab if everything is valid for install.
  • Loading branch information
Petethegoat committed Jul 20, 2018
1 parent 567488a commit 14934ef
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 34 deletions.
40 changes: 26 additions & 14 deletions src/Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

Expand All @@ -12,6 +11,8 @@ static class Installer
{
private const int progressChunksPerPackage = 4;

public static bool doFullInstall = true;

public static event EventHandler StartEvent;
public static event EventHandler<bool> CompleteEvent;
public static event EventHandler<object[]> ProgressEvent;
Expand All @@ -29,23 +30,27 @@ public static void Initialize()
List<Package> paks = (List<Package>)args.Argument;

CleanUpTempFiles(paks[0].list); //TODO
worker.ReportProgress(0, "Downloading package...");

foreach(Package pak in paks)
{
float progressChunk = ((paks.IndexOf(pak) + 1) * 4);
float progressChunk = (paks.IndexOf(pak) + 1) * progressChunksPerPackage;

worker.ReportProgress((int)progressChunk - 4, string.Format("Downloading {0}...", pak.name));
System.Threading.Thread.Sleep(10);
DownloadPackage(pak);
worker.ReportProgress((int)progressChunk - 3, "Unpacking archive...");
System.Threading.Thread.Sleep(10);
Unpack(pak);
worker.ReportProgress((int)progressChunk - 2, "Installing package...");
System.Threading.Thread.Sleep(10);
InstallPackage(pak);
worker.ReportProgress((int)progressChunk - 1, "Cleaning up...");
System.Threading.Thread.Sleep(10);
DeleteArchive(pak);
worker.ReportProgress((int)progressChunk, "Downloading package...");

if(doFullInstall)
{
worker.ReportProgress((int)progressChunk - 3, "Unpacking archive...");
System.Threading.Thread.Sleep(10);
Unpack(pak);
worker.ReportProgress((int)progressChunk - 2, string.Format("Installing {0}...", pak.name));
System.Threading.Thread.Sleep(10);
InstallPackage(pak);
worker.ReportProgress((int)progressChunk - 1, "Cleaning up...");
System.Threading.Thread.Sleep(10);
DeleteArchive(pak);
}
}

worker.ReportProgress(GetProgressBarLength(paks.Count), "Done!");
Expand All @@ -61,6 +66,9 @@ public static void Initialize()
if(args.Error != null)
{
Log.Write(args.Error);
if(args.Error.InnerException != null)
Log.Write(args.Error.InnerException);

CompleteEvent.Invoke(null, false);
return;
}
Expand Down Expand Up @@ -103,7 +111,10 @@ private static void DownloadPackage(Package pak)
string url = pak.fileURL;

if(pak.requiresNexus)
{
url = Nexus.GetNexusDownloadURL(pak.fileURL);
pak.fileName = Nexus.FileNameFromNexusDownloadURL(url);
}

var webClient = new WebClient();
webClient.DownloadFile(url, pak.fileName);
Expand Down Expand Up @@ -178,7 +189,8 @@ private static void InstallPackage(Package pak)

private static void DeleteArchive(Package pak)
{
File.Delete(pak.fileName);
if(File.Exists(pak.fileName))
File.Delete(pak.fileName);
}

private static bool ExcludedByFilter(FileInfo file, Package pak)
Expand Down
29 changes: 26 additions & 3 deletions src/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
mc:Ignorable="d"
Title="MW Installer" Height="450" Width="800" Icon="/MWInstaller;component/res/icon.ico" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
<Grid>
<TabControl x:Name="tabControl" SelectedIndex="1">
<TabItem x:Name="packageTab" Header="Install" IsEnabled="False">
<TabControl x:Name="tabControl" SelectedIndex="1" SelectionChanged="tabControl_SelectionChanged">
<TabItem x:Name="installTab" Header="Install" IsEnabled="False">
<Grid Background="#FFE5E5E5">
<ListView x:Name="packagesView" HorizontalAlignment="Left" Height="291" Margin="10,52,0,0" VerticalAlignment="Top" Width="612" MouseDoubleClick="packagesView_MouseDoubleClick">
<ListView.View>
Expand Down Expand Up @@ -37,7 +37,30 @@
<ProgressBar x:Name="installProgress" HorizontalAlignment="Left" Height="20" Margin="10,360,0,0" VerticalAlignment="Top" Width="612"/>
<Image x:Name="installReadyImage" HorizontalAlignment="Left" Height="147" Margin="627,201,0,0" VerticalAlignment="Top" Width="147" Source="/MWInstaller;component/res/icon.ico"/>
<TextBlock x:Name="packageListUpdated" HorizontalAlignment="Left" Margin="627,52,0,0" TextWrapping="Wrap" Text="Last updated:" VerticalAlignment="Top" Height="64" Width="147" FontSize="14"/>
<Label x:Name="installTask" Content="" HorizontalAlignment="Left" Margin="5,338,0,0" VerticalAlignment="Top" Width="256"/>
<Label x:Name="installTask" Content="" HorizontalAlignment="Left" Margin="5,338,0,0" VerticalAlignment="Top" Width="617"/>
<Border x:Name="installPopup" BorderThickness="1" HorizontalAlignment="Left" Height="124" Margin="344,201,0,0" VerticalAlignment="Top" Width="430" Visibility="Hidden">
<Grid>
<Rectangle Stroke="#FF828790" RadiusY="4" RadiusX="4" />
<Rectangle Width="36" Height="36" Margin="228,102,54,-16" RenderTransformOrigin="0.5,0.5" Fill="#FF828790" HorizontalAlignment="Right" VerticalAlignment="Bottom" >
<Rectangle.RenderTransform>
<TransformGroup>
<RotateTransform Angle="45"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Width="36" Height="36" Margin="228,101,54,-15" RenderTransformOrigin="0.5,0.5" Fill="WhiteSmoke" HorizontalAlignment="Right" VerticalAlignment="Bottom" >
<Rectangle.RenderTransform>
<TransformGroup>
<RotateTransform Angle="45"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Fill="WhiteSmoke" Stroke="#01FFFFFF" RadiusY="4" RadiusX="4"/>
<Button x:Name="buttonFullInstall" Content="Full Install" Height="48" Margin="0,0,10,10" VerticalAlignment="Bottom" FontSize="18" HorizontalAlignment="Right" Width="298" Click="buttonFullInstall_Click"/>
<Button x:Name="buttonDownloadOnly" Content="Download Packages Only" Height="48" Margin="0,0,10,64" VerticalAlignment="Bottom" FontSize="18" HorizontalAlignment="Right" Width="298" Click="buttonDownloadOnly_Click"/>
<Image HorizontalAlignment="Left" Height="120" VerticalAlignment="Top" Width="120" Source="/MWInstaller;component/res/icon.ico" Margin="0,1,0,0"/>
</Grid>
</Border>
</Grid>
</TabItem>
<TabItem Header="Configuration">
Expand Down
25 changes: 22 additions & 3 deletions src/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@ public MainWindow()
Log.Clear();
Log.Write("MW Installer started.\n");


if(!Utils.IsConnectedToInternet())
{
Log.Write("Couldn't connect to internet- quitting instead.\n");
MessageBox.Show("No internet connection, closing MW Installer.");
Application.Current.Shutdown();
}

Installer.Initialize();
Config.LoadConfig();

PrefillConfiguration();

if(CheckInstallability())
installTab.IsSelected = true;

Installer.StartEvent += installerStart;
Installer.CompleteEvent += installerComplete;
Installer.ProgressEvent += updateProgressBar;
Expand Down Expand Up @@ -62,7 +74,7 @@ private void PackageListUpdated(string path)
packagesView.ItemsSource = packages;
packageListSuccessTick.Visibility = Visibility.Visible;
packageListRefresh.Visibility = Visibility.Hidden;
packageTab.IsEnabled = true;
installTab.IsEnabled = true;
CheckInstallability();
packageListInfo.Text = string.Empty;
return;
Expand All @@ -86,12 +98,12 @@ private void PackageListUpdated(string path)
}

// Path didn't exist, or the package list didn't deserialize properly.
packageTab.IsEnabled = false;
installTab.IsEnabled = false;
packageListSuccessTick.Visibility = Visibility.Hidden;
CheckInstallability();
}

private void CheckInstallability(bool forceFail = false)
private bool CheckInstallability(bool forceFail = false)
{
bool readyForInstall = true;

Expand Down Expand Up @@ -128,6 +140,13 @@ private void CheckInstallability(bool forceFail = false)

installButton.IsEnabled = !nexusWarnings && readyForInstall;
installReadyImage.Visibility = !nexusWarnings && readyForInstall ? Visibility.Visible : Visibility.Hidden;
return readyForInstall;
}

private void tabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
installPopup.Visibility = Visibility.Hidden;
CheckInstallability();
}
}
}
32 changes: 30 additions & 2 deletions src/Nexus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ static public string GetNexusDownloadURL(string url)
using(StreamReader reader = new StreamReader(stream))
{
string s = reader.ReadToEnd();
downloadURL = Regex.Match(s, @"""URI"":""(?<url>.+)""").Groups["url"].Value;
var uri = NexusFileURI.Deserialize(s.Trim('[', ']'));
downloadURL = uri.URI;

//downloadURL = Regex.Match(s, @"""URI"":""(?<url>.+)""").Groups["url"].Value;
}

return Regex.Unescape(downloadURL);
return downloadURL;
}

static public string FileNameFromNexusDownloadURL(string url)
{
return Regex.Match(url, @".*/(.*)\?.*").Groups[1].Value;
}

//from https://www.nexusmods.com/morrowind/mods/45712
Expand Down Expand Up @@ -122,4 +130,24 @@ public static NexusFiles Deserialize(string json)
return files;
}
}

[DataContract]
internal class NexusFileURI
{
[DataMember] public string name { get; set; }
[DataMember] public string short_name { get; set; }
[DataMember] public string URI { get; set; }

public static NexusFileURI Deserialize(string json)
{
var uri = new NexusFileURI();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
var settings = new DataContractJsonSerializerSettings();
var serializer = new DataContractJsonSerializer(typeof(NexusFileURI), settings);
uri = serializer.ReadObject(ms) as NexusFileURI;
ms.Close();

return uri;
}
}
}
10 changes: 10 additions & 0 deletions src/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,15 @@ public static string RelativePath(string absolutePath, string relativeTo)

return relativePath.ToString();
}

[System.Runtime.InteropServices.DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

public static bool IsConnectedToInternet()
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}

}
}
58 changes: 46 additions & 12 deletions src/tabs/InstallTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,49 @@ namespace MWInstaller
public partial class MainWindow : Window
{
private void installButton_Click(object sender, RoutedEventArgs e)
{
if(installPopup.Visibility == Visibility.Visible)
{
installPopup.Visibility = Visibility.Hidden;
installReadyImage.Visibility = Visibility.Visible;
}
else
{
installPopup.Visibility = Visibility.Visible;
installReadyImage.Visibility = Visibility.Hidden;
buttonFullInstall.Focus();
}
}

private void buttonFullInstall_Click(object sender, RoutedEventArgs e)
{
Installer.doFullInstall = true;
installProgress.Maximum = Installer.GetProgressBarLength(packages.Count);
Installer.PerformInstall(packages);
}

private void buttonDownloadOnly_Click(object sender, RoutedEventArgs e)
{
Installer.doFullInstall = false;
installProgress.Maximum = Installer.GetProgressBarLength(packages.Count);
Installer.PerformInstall(packages);
}

private void packagesView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var p = packagesView.SelectedItem as Package;
Log.Write("\n" + p.name + "\n");
if(p.malformed)
{
packages = packageList.GetPackages();
packagesView.ItemsSource = packages;
packagesView.UpdateLayout();
}
}

// Installer functions

private void BeginFullInstall()
{
installProgress.Maximum = Installer.GetProgressBarLength(packages.Count);
Installer.PerformInstall(packages);
Expand All @@ -24,6 +67,9 @@ private void installerStart(object sender, System.EventArgs e)

private void installerComplete(object sender, bool success)
{
mainWindow.Activate();
installPopup.Visibility = Visibility.Hidden;

string message;
if(success)
message = "Installation complete.";
Expand All @@ -36,17 +82,5 @@ private void installerComplete(object sender, bool success)
installReadyImage.Visibility = Visibility.Hidden;
}
}

private void packagesView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var p = packagesView.SelectedItem as Package;
Log.Write("\n" + p.name + "\n");
if(p.malformed)
{
packages = packageList.GetPackages();
packagesView.ItemsSource = packages;
packagesView.UpdateLayout();
}
}
}
}

0 comments on commit 14934ef

Please sign in to comment.