Image fetcher & 5byte fields (#359)

* Added new image code, quanted existing images, fixed bcsv parser

* small clean and sane(r) filenames

* Made a string slightly nicer

* various revert to fix diff

* full revert of editor

* forced revert of editor

* fix sprite init

* ready for merge
This commit is contained in:
berichan
2020-07-21 01:02:49 +01:00
committed by GitHub
parent c224466bd7
commit ea1d9c7b1b
14 changed files with 779 additions and 335 deletions

View File

@@ -0,0 +1,152 @@
namespace NHSE.WinForms
{
partial class ImageFetcher
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.PBar_MultiUse = new System.Windows.Forms.ProgressBar();
this.B_Download = new System.Windows.Forms.Button();
this.CB_HostSelect = new System.Windows.Forms.ComboBox();
this.L_Warning = new System.Windows.Forms.Label();
this.L_Host = new System.Windows.Forms.Label();
this.L_Status = new System.Windows.Forms.Label();
this.L_ImgStatus = new System.Windows.Forms.Label();
this.L_FileSize = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// PBar_MultiUse
//
this.PBar_MultiUse.Location = new System.Drawing.Point(37, 178);
this.PBar_MultiUse.Name = "PBar_MultiUse";
this.PBar_MultiUse.Size = new System.Drawing.Size(330, 23);
this.PBar_MultiUse.TabIndex = 0;
//
// B_Download
//
this.B_Download.Location = new System.Drawing.Point(125, 149);
this.B_Download.Name = "B_Download";
this.B_Download.Size = new System.Drawing.Size(156, 23);
this.B_Download.TabIndex = 1;
this.B_Download.Text = "Download Images";
this.B_Download.UseVisualStyleBackColor = true;
this.B_Download.Click += new System.EventHandler(this.B_Download_Click);
//
// CB_HostSelect
//
this.CB_HostSelect.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CB_HostSelect.FormattingEnabled = true;
this.CB_HostSelect.Location = new System.Drawing.Point(37, 93);
this.CB_HostSelect.Name = "CB_HostSelect";
this.CB_HostSelect.Size = new System.Drawing.Size(330, 21);
this.CB_HostSelect.TabIndex = 2;
this.CB_HostSelect.SelectedIndexChanged += new System.EventHandler(this.CB_HostSelect_SelectedIndexChanged);
//
// L_Warning
//
this.L_Warning.Location = new System.Drawing.Point(37, 22);
this.L_Warning.Name = "L_Warning";
this.L_Warning.Size = new System.Drawing.Size(330, 44);
this.L_Warning.TabIndex = 3;
this.L_Warning.Text = "The developers of this application are not responsible for curating item images. " +
"Images are hosted by third parties and while every effort is made to ensure comp" +
"leteness, this is not guaranteed.";
this.L_Warning.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// L_Host
//
this.L_Host.AutoSize = true;
this.L_Host.Location = new System.Drawing.Point(162, 77);
this.L_Host.Name = "L_Host";
this.L_Host.Size = new System.Drawing.Size(83, 13);
this.L_Host.TabIndex = 4;
this.L_Host.Text = "Download Host:";
this.L_Host.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// L_Status
//
this.L_Status.Location = new System.Drawing.Point(37, 204);
this.L_Status.Name = "L_Status";
this.L_Status.Size = new System.Drawing.Size(330, 22);
this.L_Status.TabIndex = 5;
this.L_Status.Text = "Downloading...";
this.L_Status.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// L_ImgStatus
//
this.L_ImgStatus.ForeColor = System.Drawing.Color.Red;
this.L_ImgStatus.Location = new System.Drawing.Point(45, 117);
this.L_ImgStatus.Name = "L_ImgStatus";
this.L_ImgStatus.Size = new System.Drawing.Size(317, 29);
this.L_ImgStatus.TabIndex = 6;
this.L_ImgStatus.Text = "Images now exist. Only repair images if absolutely necessary. Images do NOT need " +
"to be redownloaded when NHSE updates.";
this.L_ImgStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// L_FileSize
//
this.L_FileSize.Location = new System.Drawing.Point(287, 72);
this.L_FileSize.Name = "L_FileSize";
this.L_FileSize.Size = new System.Drawing.Size(80, 18);
this.L_FileSize.TabIndex = 7;
this.L_FileSize.Text = "00.0MB";
this.L_FileSize.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// ImageFetcher
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(407, 235);
this.Controls.Add(this.L_FileSize);
this.Controls.Add(this.L_ImgStatus);
this.Controls.Add(this.L_Status);
this.Controls.Add(this.L_Host);
this.Controls.Add(this.L_Warning);
this.Controls.Add(this.CB_HostSelect);
this.Controls.Add(this.B_Download);
this.Controls.Add(this.PBar_MultiUse);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ImageFetcher";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Item Images";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ProgressBar PBar_MultiUse;
private System.Windows.Forms.Button B_Download;
private System.Windows.Forms.ComboBox CB_HostSelect;
private System.Windows.Forms.Label L_Warning;
private System.Windows.Forms.Label L_Host;
private System.Windows.Forms.Label L_Status;
private System.Windows.Forms.Label L_ImgStatus;
private System.Windows.Forms.Label L_FileSize;
}
}

View File

@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NHSE.Core;
using NHSE.Sprites;
namespace NHSE.WinForms
{
public sealed partial class ImageFetcher : Form
{
private const string Filename = "image.zip";
private static string ZipFilePath { get => Path.Combine(ItemSprite.PlatformAppDataPath, Filename); }
private readonly List<string> AllHosts;
public ImageFetcher()
{
InitializeComponent();
L_Status.Text = L_FileSize.Text = string.Empty;
AllHosts = new List<string>(LoadHosts());
CB_HostSelect.SelectedIndex = 0; // set outside of initialise to update filesize via HEAD response
CheckFileStatusLabel();
}
private string[] LoadHosts()
{
CB_HostSelect.Items.Clear();
var hosts = Properties.Resources.hosts_images;
var splitHosts = hosts.Split(new string[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var host in splitHosts)
CB_HostSelect.Items.Add(CleanUrl(host));
return splitHosts;
}
private void B_Download_Click(object sender, EventArgs e)
{
var path = ItemSprite.PlatformAppDataPath;
var hostSelected = AllHosts[CB_HostSelect.SelectedIndex];
SetUIDownloadState(false);
L_Status.Text = "Downloading...";
try
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (var webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(hostSelected), ZipFilePath);
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
WinFormsUtil.Error(ex.Message, ex.InnerException == null ? string.Empty : ex.InnerException.Message);
SetUIDownloadState(true);
}
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) => PBar_MultiUse.Value = e.ProgressPercentage;
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
WinFormsUtil.Error(e.Error.Message, e.Error.InnerException == null ? string.Empty : e.Error.InnerException.Message);
SetUIDownloadState(true);
return;
}
PBar_MultiUse.Value = 100;
L_Status.Text = "Unzipping...";
UnzipFile();
}
private async void UnzipFile()
{
try
{
var outputFolderPath = ItemSprite.PlatformAppDataImagePath;
if (Directory.Exists(outputFolderPath)) // overwrite existing
Directory.Delete(outputFolderPath, true);
Directory.CreateDirectory(outputFolderPath);
await Task.Run(() => ZipFile.ExtractToDirectory(ZipFilePath, outputFolderPath));
SetUIDownloadState(true, true);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
WinFormsUtil.Error(ex.Message, ex.InnerException == null ? string.Empty : ex.InnerException.Message);
SetUIDownloadState(true);
}
}
private void SetUIDownloadState(bool val, bool success = false)
{
ControlBox = val;
B_Download.Enabled = val;
PBar_MultiUse.Value = 0;
L_Status.Text = success ? "Images installed successfully." : string.Empty;
CheckFileStatusLabel();
if (success)
ItemSprite.Initialize(Core.GameInfo.GetStrings("en").itemlist);
if (File.Exists(ZipFilePath))
File.Delete(ZipFilePath);
}
private void CB_HostSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if (AllHosts == null)
return;
CheckNetworkFileSizeAsync();
}
private async void CheckNetworkFileSizeAsync()
{
L_FileSize.Text = string.Empty;
try
{
var webClient = new WebClient();
await webClient.OpenReadTaskAsync(new Uri(AllHosts[CB_HostSelect.SelectedIndex], UriKind.Absolute));
var totalSizeBytes = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
var totalSizeMb = totalSizeBytes / 1e+6;
L_FileSize.Text = totalSizeMb.ToString("0.##") + "MB";
}
catch (Exception ex)
{
L_FileSize.Text = ex.Message;
}
}
private string CleanUrl(string url)
{
var uri = new Uri(url);
if (uri.Segments.Length < 2)
return url;
return $"{uri.Host}/{uri.Segments[1]}";
}
private bool CheckFileStatusLabel() => L_ImgStatus.Visible = ItemSprite.SingleSpriteExists;
}
}

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>