Skip to content

Commit 15ed79c

Browse files
committed
Add component descriptions, improve stability
1 parent f06325c commit 15ed79c

5 files changed

Lines changed: 283 additions & 198 deletions

File tree

Common.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Windows.Forms;
5+
using System.Xml;
6+
7+
namespace FlashpointInstaller
8+
{
9+
namespace Common
10+
{
11+
public class FPM
12+
{
13+
public static TreeNode AddNodeToList(XmlNode child, TreeNodeCollection parent)
14+
{
15+
var listNode = parent.Add(child.Attributes["title"].Value);
16+
listNode.Tag = new Dictionary<string, string>
17+
{
18+
{ "title", child.Attributes["title"].Value },
19+
{ "url", GetComponentURL(child) },
20+
{ "path", GetComponentPath(child) },
21+
{ "description", child.Attributes["description"].Value },
22+
{ "type", child.Name },
23+
{ "disabled", "false" }
24+
};
25+
26+
if ((listNode.Tag as Dictionary<string, string>)["type"] == "component")
27+
{
28+
(listNode.Tag as Dictionary<string, string>).Add("size", child.Attributes["size"].Value);
29+
(listNode.Tag as Dictionary<string, string>).Add("hash", child.Attributes["hash"].Value);
30+
31+
listNode.Checked = bool.Parse(child.Attributes["checked"].Value);
32+
}
33+
34+
if ((child.ParentNode.Name == "category" && child.ParentNode.Attributes["required"].Value == "true")
35+
|| (child.Name == "category" && child.Attributes["required"].Value == "true"))
36+
{
37+
listNode.ForeColor = Color.FromArgb(255, 96, 96, 96);
38+
(listNode.Tag as Dictionary<string, string>)["disabled"] = "true";
39+
}
40+
41+
return listNode;
42+
}
43+
44+
public static string GetComponentURL(XmlNode node)
45+
{
46+
string path = node.Attributes["url"].Value;
47+
48+
node = node.ParentNode;
49+
50+
while (node != null)
51+
{
52+
if (node.Attributes != null)
53+
{
54+
path = $"{node.Attributes["url"].Value}/{path}";
55+
}
56+
57+
node = node.ParentNode;
58+
}
59+
60+
return path;
61+
}
62+
63+
public static string GetComponentPath(XmlNode node)
64+
{
65+
string path = "";
66+
67+
node = node.ParentNode;
68+
69+
while (node != null && node.Name != "list")
70+
{
71+
if (node.Attributes != null)
72+
{
73+
path = $"{node.Attributes["url"].Value}\\{path}";
74+
}
75+
76+
node = node.ParentNode;
77+
}
78+
79+
return path;
80+
}
81+
82+
public static string GetFormattedBytes(long bytes)
83+
{
84+
if (bytes >= 1000000000000)
85+
{
86+
return (Math.Truncate((double)bytes / 100000000000) / 10).ToString("N1") + "TB";
87+
}
88+
else if (bytes >= 1000000000)
89+
{
90+
return (Math.Truncate((double)bytes / 100000000) / 10).ToString("N1") + "GB";
91+
}
92+
else if (bytes >= 1000000)
93+
{
94+
return (Math.Truncate((double)bytes / 100000) / 10).ToString("N1") + "MB";
95+
}
96+
else if (bytes >= 1000)
97+
{
98+
return (Math.Truncate((double)bytes / 100) / 10).ToString("N1") + "KB";
99+
}
100+
else
101+
{
102+
return $"{bytes}B";
103+
}
104+
}
105+
}
106+
}
107+
}

FlashpointManager.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
</Reference>
265265
</ItemGroup>
266266
<ItemGroup>
267+
<Compile Include="Common.cs" />
267268
<Compile Include="Finish.cs">
268269
<SubType>Form</SubType>
269270
</Compile>

Install.cs

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Windows.Forms;
77

88
using Downloader;
9+
using FlashpointInstaller.Common;
910
using IWshRuntimeLibrary;
1011
using SharpCompress.Archives.Zip;
1112
using SharpCompress.Common;
@@ -61,40 +62,45 @@ void Iterate(TreeNodeCollection parent)
6162
workingComponent = component;
6263
stream = await downloader.DownloadFileTaskAsync(component["url"]);
6364

64-
if (cancelStatus == 2) { return; }
65+
if (cancelStatus != 0) return;
6566

6667
Directory.CreateDirectory(mainForm.FolderTextBox.Text);
6768
await Task.Run(ExtractTask);
6869

6970
byteProgress += int.Parse(component["size"]);
7071
}
7172

72-
if (cancelStatus == 0) { FinishInstallation(); }
73+
if (cancelStatus == 0) FinishInstallation();
7374
}
7475

7576
private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
7677
{
78+
if (cancelStatus != 0)
79+
{
80+
downloader.CancelAsync();
81+
return;
82+
}
83+
7784
double currentProgress = (double)e.ReceivedBytesSize / e.TotalBytesToReceive;
7885
long currentSize = long.Parse(workingComponent["size"]);
86+
double totalProgress = (byteProgress + (currentProgress / 2 * currentSize)) / byteTotal;
7987

8088
Progress.Invoke((MethodInvoker)delegate
8189
{
82-
Progress.Value = (int)((double)
83-
((byteProgress + (currentProgress / 2 * currentSize)) / byteTotal * Progress.Maximum)
84-
);
90+
Progress.Value = (int)((double)totalProgress * Progress.Maximum);
8591
});
8692

8793
Info.Invoke((MethodInvoker)delegate
8894
{
8995
Info.Text =
90-
$"Downloading component \"{workingComponent["title"]}\"... " +
91-
$"{Math.Truncate(currentProgress * 100)}%";
96+
$"[{(int)((double)totalProgress * 100)}%] Downloading component \"{workingComponent["title"]}\"... " +
97+
$"{FPM.GetFormattedBytes(e.ReceivedBytesSize)} of {FPM.GetFormattedBytes(e.TotalBytesToReceive)}";
9298
});
9399
}
94100

95101
private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
96102
{
97-
if (e.Cancelled) { cancelStatus = 2; }
103+
if (e.Cancelled) cancelStatus = 2;
98104
}
99105

100106
private void ExtractTask()
@@ -116,9 +122,9 @@ private void ExtractTask()
116122
writer.WriteLine($"{workingComponent["hash"]} {workingComponent["url"]}");
117123
}
118124

119-
while (!reader.Cancelled && reader.MoveToNextEntry())
125+
while (cancelStatus == 0 && reader.MoveToNextEntry())
120126
{
121-
if (reader.Entry.IsDirectory) { continue; }
127+
if (reader.Entry.IsDirectory) continue;
122128

123129
reader.WriteEntryToDirectory(
124130
mainForm.FolderTextBox.Text, new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
@@ -133,53 +139,54 @@ private void ExtractTask()
133139

134140
double currentProgress = (double)extractedSize / totalSize;
135141
long currentSize = long.Parse(workingComponent["size"]);
136-
142+
double totalProgress = (byteProgress + (currentSize / 2) + (currentProgress / 2 * currentSize)) / byteTotal;
143+
137144
Progress.Invoke((MethodInvoker)delegate
138145
{
139-
Progress.Value = (int)(
140-
(byteProgress + (currentSize / 2) + (currentProgress / 2 * currentSize)) / byteTotal * Progress.Maximum
141-
);
146+
Progress.Value = (int)((double)totalProgress * Progress.Maximum);
142147
});
143148

144149
Info.Invoke((MethodInvoker)delegate
145150
{
146151
Info.Text =
147-
$"Extracting component \"{workingComponent["title"]}\"... " +
148-
$"{Math.Truncate(currentProgress * 100)}%";
152+
$"[{(int)((double)totalProgress * 100)}%] Extracting component \"{workingComponent["title"]}\"... " +
153+
$"{FPM.GetFormattedBytes(extractedSize)} of {FPM.GetFormattedBytes(totalSize)}";
149154
});
150155
}
151156

152-
if (reader.Cancelled)
157+
if (cancelStatus != 0)
153158
{
154-
cancelStatus = 1;
155-
Directory.Delete(mainForm.FolderTextBox.Text, true);
159+
reader.Cancel();
156160
cancelStatus = 2;
157161
}
158162
}
159163
}
160164
}
161165

162-
private void FinishInstallation()
166+
private async void FinishInstallation()
163167
{
164-
var shortcutPaths = new List<string>();
165-
166-
if (mainForm.ShortcutDesktop.Checked)
168+
await Task.Run(() =>
167169
{
168-
shortcutPaths.Add(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
169-
}
170-
if (mainForm.ShortcutStartMenu.Checked)
171-
{
172-
shortcutPaths.Add(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
173-
}
170+
var shortcutPaths = new List<string>();
174171

175-
foreach(string path in shortcutPaths)
176-
{
177-
IWshShortcut shortcut = new WshShell().CreateShortcut(Path.Combine(path, "Flashpoint.lnk"));
178-
shortcut.TargetPath = Path.Combine(mainForm.FolderTextBox.Text, @"Launcher\Flashpoint.exe");
179-
shortcut.WorkingDirectory = Path.Combine(mainForm.FolderTextBox.Text, @"Launcher");
180-
shortcut.Description = "Shortcut to Flashpoint";
181-
shortcut.Save();
182-
}
172+
if (mainForm.ShortcutDesktop.Checked)
173+
{
174+
shortcutPaths.Add(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
175+
}
176+
if (mainForm.ShortcutStartMenu.Checked)
177+
{
178+
shortcutPaths.Add(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
179+
}
180+
181+
foreach (string path in shortcutPaths)
182+
{
183+
IWshShortcut shortcut = new WshShell().CreateShortcut(Path.Combine(path, "Flashpoint.lnk"));
184+
shortcut.TargetPath = Path.Combine(mainForm.FolderTextBox.Text, @"Launcher\Flashpoint.exe");
185+
shortcut.WorkingDirectory = Path.Combine(mainForm.FolderTextBox.Text, @"Launcher");
186+
shortcut.Description = "Shortcut to Flashpoint";
187+
shortcut.Save();
188+
}
189+
});
183190

184191
Hide();
185192
mainForm.Hide();
@@ -191,12 +198,18 @@ private void FinishInstallation()
191198
private async void Cancel_Click(object sender, EventArgs e)
192199
{
193200
Cancel.Enabled = false;
201+
cancelStatus = 1;
194202

195-
if (downloader.IsBusy) { downloader.CancelAsync(); }
196-
if (reader != null) { reader.Cancel(); }
197-
198-
await Task.Run(() => { while (cancelStatus != 2) { } });
203+
await Task.Run(() =>
204+
{
205+
while (cancelStatus != 2) { }
199206

207+
if (Directory.Exists(mainForm.FolderTextBox.Text))
208+
{
209+
Directory.Delete(mainForm.FolderTextBox.Text, true);
210+
}
211+
});
212+
200213
Close();
201214
}
202215
}

0 commit comments

Comments
 (0)