roketto-launch/LoadingForm.cs
Ray 81c97c1b93 Changed to .NET 10
Changed to remove TileContainer
Added loading progress dialog
2026-05-08 23:16:26 +01:00

321 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using RokettoLaunch.Models.SaveFile;
using RyzStudio.Windows.Forms;
namespace RokettoLaunch
{
public class LoadingForm : Form
{
private TProgressBar progressBar1;
private RyzStudio.Windows.ThemedForms.ThUserControl userControl1;
private RyzStudio.Windows.ThemedForms.ThUserControl userControl2;
private TProgressBar progressBar2;
private App4Options result = null;
private Label label1;
private Label label2;
private string loadFilename = "";
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 = System.Drawing.Color.FromArgb(158, 225, 249);
progressBar1.Dock = DockStyle.Fill;
progressBar1.EnableMovable = false;
progressBar1.Location = new System.Drawing.Point(3, 3);
progressBar1.Maximum = 100;
progressBar1.Minimum = 0;
progressBar1.Name = "progressBar1";
progressBar1.OnProgressChanged = null;
progressBar1.ShowText = true;
progressBar1.Size = new System.Drawing.Size(418, 18);
progressBar1.TabIndex = 156;
progressBar1.Value = 25;
//
// userControl1
//
userControl1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
userControl1.BackColor = System.Drawing.Color.Transparent;
userControl1.Controls.Add(progressBar1);
userControl1.EnableMovable = false;
userControl1.Location = new System.Drawing.Point(10, 64);
userControl1.Name = "userControl1";
userControl1.Size = new System.Drawing.Size(424, 24);
userControl1.TabIndex = 157;
//
// userControl2
//
userControl2.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
userControl2.BackColor = System.Drawing.Color.Transparent;
userControl2.Controls.Add(progressBar2);
userControl2.EnableMovable = false;
userControl2.Location = new System.Drawing.Point(10, 98);
userControl2.Name = "userControl2";
userControl2.Size = new System.Drawing.Size(424, 24);
userControl2.TabIndex = 158;
//
// progressBar2
//
progressBar2.BarColour = System.Drawing.Color.FromArgb(158, 225, 249);
progressBar2.Dock = DockStyle.Fill;
progressBar2.EnableMovable = false;
progressBar2.Location = new System.Drawing.Point(3, 3);
progressBar2.Maximum = 100;
progressBar2.Minimum = 0;
progressBar2.Name = "progressBar2";
progressBar2.OnProgressChanged = null;
progressBar2.ShowText = true;
progressBar2.Size = new System.Drawing.Size(418, 18);
progressBar2.TabIndex = 156;
progressBar2.Value = 25;
//
// label1
//
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(10, 20);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(59, 15);
label1.TabIndex = 159;
label1.Text = "Loading...";
//
// label2
//
label2.AutoSize = true;
label2.Location = new System.Drawing.Point(10, 40);
label2.Name = "label2";
label2.Size = new System.Drawing.Size(59, 15);
label2.TabIndex = 160;
label2.Text = "Loading...";
//
// LoadingForm
//
BackColor = System.Drawing.Color.White;
ClientSize = new System.Drawing.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);
PerformLayout();
}
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);
// Update progress bars
progressBar1.Maximum = newSession.Groups?.Count ?? 0;
progressBar2.Maximum = 0;
UIControl.SetText(label1, "Counting...");
UIControl.SetText(label2, "");
foreach (var group in newSession?.Groups ?? new List<App4Options.Group>())
{
UIControl.SetText(label2, "... " + group.Title);
foreach (var item in group?.Items ?? new List<App4Options.Item>())
{
if (item.IsGroup)
{
progressBar2.Maximum += item.Items?.Count ?? 0;
}
else
{
progressBar2.Maximum++;
}
}
}
// Resolve paths
progressBar2.Value = 0;
UIControl.SetText(label1, "Seeking...");
UIControl.SetText(label2, "");
// Resolve paths
foreach (var group in newSession?.Groups ?? new List<App4Options.Group>())
{
foreach (var item in group?.Items ?? new List<App4Options.Item>())
{
progressBar1.Value++;
UIControl.SetText(label2, "... " + group.Title);
if (item.IsGroup)
{
foreach (var subItem in item.Items ?? new List<App4Options.Item>())
{
progressBar2.Value++;
UIControl.SetText(label2, "... " + subItem.Title);
subItem.ResolvePaths();
}
}
else
{
progressBar2.Value++;
UIControl.SetText(label2, "... " + item.Title);
item.ResolvePaths();
}
}
}
result = newSession;
UIControl.SetText(label1, "Done");
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 App4Options Result
{
get => result ?? new App4Options();
}
public async Task<DialogResult> ShowDialog(string filename)
{
loadFilename = filename;
return this.ShowDialog();
}
public void Clear()
{
UIControl.SetText(label1, "Ready");
UIControl.SetText(label2, "");
progressBar1.Value = progressBar1.Maximum = 0;
progressBar2.Value = progressBar2.Maximum = 0;
}
private async Task<App4Options> LoadSaveSessionWithVersion(string filename)
{
var result = new App4Options();
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.
App3Options loadSession = null;
switch (fileExtension)
{
case ".json":
case ".jsonfig":
loadSession = await RyzStudio.Text.Json.JsonSerialiser.DeserialiseFile<App3Options>(filename);
break;
case ".jsnx":
loadSession = await RyzStudio.IO.Compression.ZFile.ReadFile<App3Options>(filename, "Document.json");
break;
default:
break;
}
if (loadSession == null)
{
return result;
}
// File likely not correct schema.
if (loadSession.FileVersion <= 0)
{
return result;
}
// Load session, if version 4.
if (loadSession.FileVersion == 4)
{
switch (fileExtension)
{
case ".json":
case ".jsonfig":
result = await RyzStudio.Text.Json.JsonSerialiser.DeserialiseFile<App4Options>(filename);
break;
case ".jsnx":
result = await RyzStudio.IO.Compression.ZFile.ReadFile<App4Options>(filename, "Document.json");
break;
default:
break;
}
}
if (result == null)
{
return new App4Options();
}
// Convert version 3 to 4.
if (loadSession.FileVersion == 3)
{
result.Load(loadSession);
}
return result;
}
}
}