360 lines
11 KiB
C#
360 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using BukkuBuddy.DTOs.SaveFile;
|
|
using RyzStudio.Windows.Forms;
|
|
using static RyzStudio.Windows.Forms.BookmarkTreeView;
|
|
|
|
namespace BukkuBuddy.Forms
|
|
{
|
|
public class LoadingForm : Form
|
|
{
|
|
private TProgressBar progressBar1;
|
|
private RyzStudio.Windows.ThemedForms.ThUserControl userControl1;
|
|
private RyzStudio.Windows.ThemedForms.ThUserControl userControl2;
|
|
private TProgressBar progressBar2;
|
|
private Label label1;
|
|
private Label label2;
|
|
|
|
private App6Options result = null;
|
|
|
|
private string loadFilename = "";
|
|
private BookmarkTreeView treeView = null;
|
|
|
|
|
|
public LoadingForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
UISetup.Dialog(this, true);
|
|
|
|
this.Text = "Loading";
|
|
}
|
|
|
|
|
|
private void InitializeComponent()
|
|
{
|
|
progressBar1 = new TProgressBar();
|
|
userControl1 = new RyzStudio.Windows.ThemedForms.ThUserControl();
|
|
userControl2 = new RyzStudio.Windows.ThemedForms.ThUserControl();
|
|
progressBar2 = new TProgressBar();
|
|
label1 = new Label();
|
|
label2 = new Label();
|
|
userControl1.SuspendLayout();
|
|
userControl2.SuspendLayout();
|
|
SuspendLayout();
|
|
//
|
|
// progressBar1
|
|
//
|
|
progressBar1.BarColour = Color.FromArgb(191, 102, 243);
|
|
progressBar1.Dock = DockStyle.Fill;
|
|
progressBar1.EnableMovable = false;
|
|
progressBar1.Location = new Point(3, 3);
|
|
progressBar1.Maximum = 100;
|
|
progressBar1.Minimum = 0;
|
|
progressBar1.Name = "progressBar1";
|
|
progressBar1.OnProgressChanged = null;
|
|
progressBar1.ShowText = true;
|
|
progressBar1.Size = new Size(418, 18);
|
|
progressBar1.TabIndex = 156;
|
|
progressBar1.Value = 25;
|
|
//
|
|
// userControl1
|
|
//
|
|
userControl1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
|
userControl1.BackColor = Color.Transparent;
|
|
userControl1.Controls.Add(progressBar1);
|
|
userControl1.EnableMovable = false;
|
|
userControl1.Location = new Point(10, 64);
|
|
userControl1.Name = "userControl1";
|
|
userControl1.Size = new Size(424, 24);
|
|
userControl1.TabIndex = 157;
|
|
//
|
|
// userControl2
|
|
//
|
|
userControl2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
|
userControl2.BackColor = Color.Transparent;
|
|
userControl2.Controls.Add(progressBar2);
|
|
userControl2.EnableMovable = false;
|
|
userControl2.Location = new Point(10, 98);
|
|
userControl2.Name = "userControl2";
|
|
userControl2.Size = new Size(424, 24);
|
|
userControl2.TabIndex = 158;
|
|
//
|
|
// progressBar2
|
|
//
|
|
progressBar2.BarColour = Color.FromArgb(25, 104, 218);
|
|
progressBar2.Dock = DockStyle.Fill;
|
|
progressBar2.EnableMovable = false;
|
|
progressBar2.Location = new Point(3, 3);
|
|
progressBar2.Maximum = 100;
|
|
progressBar2.Minimum = 0;
|
|
progressBar2.Name = "progressBar2";
|
|
progressBar2.OnProgressChanged = null;
|
|
progressBar2.ShowText = true;
|
|
progressBar2.Size = new Size(418, 18);
|
|
progressBar2.TabIndex = 156;
|
|
progressBar2.Value = 25;
|
|
//
|
|
// label1
|
|
//
|
|
label1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
|
label1.AutoEllipsis = true;
|
|
label1.Location = new Point(10, 20);
|
|
label1.Name = "label1";
|
|
label1.Size = new Size(424, 15);
|
|
label1.TabIndex = 159;
|
|
//
|
|
// label2
|
|
//
|
|
label2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
|
label2.AutoEllipsis = true;
|
|
label2.Location = new Point(10, 40);
|
|
label2.Name = "label2";
|
|
label2.Size = new Size(424, 15);
|
|
label2.TabIndex = 160;
|
|
//
|
|
// LoadingForm
|
|
//
|
|
BackColor = Color.White;
|
|
ClientSize = new Size(444, 141);
|
|
Controls.Add(label2);
|
|
Controls.Add(label1);
|
|
Controls.Add(userControl2);
|
|
Controls.Add(userControl1);
|
|
Name = "LoadingForm";
|
|
Text = "Edit Section";
|
|
userControl1.ResumeLayout(false);
|
|
userControl2.ResumeLayout(false);
|
|
ResumeLayout(false);
|
|
}
|
|
|
|
protected async override void OnShown(EventArgs e)
|
|
{
|
|
base.OnShown(e);
|
|
|
|
await Task.Run(async () =>
|
|
{
|
|
this.Clear();
|
|
|
|
UIControl.SetText(label1, "Loading...");
|
|
UIControl.SetText(label2, "... " + Path.GetFileName(loadFilename));
|
|
|
|
var newSession = await LoadSaveSessionWithVersion(loadFilename);
|
|
var isPack = Path.GetExtension(loadFilename).Equals(".jsnx", StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
// Update progress bars
|
|
progressBar1.Maximum = newSession.Directories?.Count ?? 0;
|
|
progressBar2.Maximum = newSession.Items?.Count ?? 0;
|
|
progressBar1.Value = 0;
|
|
progressBar2.Value = 0;
|
|
|
|
UIControl.SetText(label1, "Loading...");
|
|
UIControl.SetText(label2, "");
|
|
|
|
foreach (var item in newSession?.Directories ?? new List<string>())
|
|
{
|
|
if (string.IsNullOrWhiteSpace(item))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
UIControl.SetText(label2, "... " + Path.GetFileNameWithoutExtension(item));
|
|
|
|
progressBar1.Value++;
|
|
|
|
treeView.CreateNodePath(item, (int)NodeIcon.Folder1, (int)NodeIcon.Folder2);
|
|
}
|
|
|
|
foreach (var item in newSession?.Items ?? new List<App6Options.Item>())
|
|
{
|
|
UIControl.SetText(label2, "... " + item.Title);
|
|
|
|
progressBar2.Value++;
|
|
|
|
// Load extra data from archive file
|
|
if (isPack)
|
|
{
|
|
item.Icon = await LoadIconFromFile(loadFilename, item);
|
|
}
|
|
|
|
treeView.AddNode(item);
|
|
}
|
|
|
|
if (treeView.Nodes.Count >= 0)
|
|
{
|
|
UIControl.Invoke(treeView, (x) =>
|
|
{
|
|
treeView.Nodes[0].Expand();
|
|
|
|
treeView.SelectedNode = treeView.Nodes[0];
|
|
});
|
|
}
|
|
|
|
result = newSession;
|
|
|
|
UIControl.SetText(label1, "");
|
|
UIControl.SetText(label2, "Done");
|
|
});
|
|
|
|
this.DialogResult = DialogResult.OK;
|
|
}
|
|
|
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
|
{
|
|
base.OnFormClosing(e);
|
|
|
|
if (this.DialogResult != DialogResult.OK)
|
|
{
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
|
|
public App6Options Result
|
|
{
|
|
get => result ?? new App6Options();
|
|
}
|
|
|
|
|
|
public async Task<DialogResult> ShowDialog(string filename, BookmarkTreeView control)
|
|
{
|
|
loadFilename = filename;
|
|
treeView = control;
|
|
|
|
return this.ShowDialog();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
UIControl.SetText(label1, "Ready");
|
|
UIControl.SetText(label2, "");
|
|
|
|
progressBar1.Value = progressBar1.Maximum = 0;
|
|
progressBar2.Value = progressBar2.Maximum = 0;
|
|
|
|
treeView.Clear(true, true, "New Session");
|
|
}
|
|
|
|
private async Task<App6Options> LoadSaveSessionWithVersion(string filename)
|
|
{
|
|
var result = new App6Options();
|
|
|
|
if (string.IsNullOrWhiteSpace(filename))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var fileExtension = Path.GetExtension(filename?.ToLower()?.Trim() ?? string.Empty);
|
|
if (string.IsNullOrWhiteSpace(fileExtension))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
// Load session, assume version 3.
|
|
App6Options loadSession = null;
|
|
switch (fileExtension)
|
|
{
|
|
case ".json":
|
|
case ".jsonfig":
|
|
loadSession = await RyzStudio.Text.Json.JsonSerialiser.DeserialiseFile<App6Options>(filename);
|
|
if (loadSession == null)
|
|
{
|
|
loadSession = await LoadFileR4(filename);
|
|
}
|
|
|
|
break;
|
|
case ".jsnx":
|
|
loadSession = await RyzStudio.IO.Compression.ZFile.ReadFile<App6Options>(filename, "Document.json");
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (loadSession == null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
// File likely not correct schema.
|
|
if (loadSession.FileVersion <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
result = loadSession;
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task<App6Options> LoadFileR4(string filename)
|
|
{
|
|
var session = await RyzStudio.Text.Json.JsonSerialiser.DeserialiseFile<List<App4Options.Item>>(filename);
|
|
if (session == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var result = new App6Options();
|
|
result.Items = new List<App6Options.Item>();
|
|
|
|
foreach (var item in session)
|
|
{
|
|
result.Items.Add(new App6Options.Item()
|
|
{
|
|
Title = item.SiteName,
|
|
Address = item.SiteAddress,
|
|
Description = item.SiteDescription,
|
|
Notes = item.Notes,
|
|
Path = item.Path
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task<Image> LoadIconFromFile(string filename, App6Options.Item item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (item.Id == Guid.Empty)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
using (var archive = ZipFile.Open(filename, ZipArchiveMode.Read))
|
|
{
|
|
var key = "icon\\" + item.Id.ToString() + ".png";
|
|
|
|
var zipEntry = archive.GetEntry(key);
|
|
if (zipEntry == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using (Stream entryStream = zipEntry.Open())
|
|
{
|
|
return Image.FromStream(entryStream);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|
|
} |