Skip to content

Commit ff98163

Browse files
committed
Further prepare manager, populate Common namespace
1 parent bcfe798 commit ff98163

9 files changed

Lines changed: 396 additions & 263 deletions

File tree

Common.cs

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
4+
using System.IO;
45
using System.Windows.Forms;
56
using System.Xml;
67

@@ -13,24 +14,66 @@ public static class FPM
1314
public static Main Main { get { return (Main)Application.OpenForms["Main"]; } }
1415
public static XmlDocument XmlTree { get; set; }
1516
public static string ListURL { get; set; }
16-
public static long DownloadSize { get; set; }
17-
public static string Path
17+
public static string DownloadPath
1818
{
19-
get { return Main.FolderTextBox.Text; }
20-
set { Main.FolderTextBox.Text = value; }
19+
get { return Main.DownloadPath.Text; }
20+
set { Main.DownloadPath.Text = value; }
21+
}
22+
public static string FlashpointPath
23+
{
24+
get { return Main.FlashpointPath.Text; }
25+
set { Main.FlashpointPath.Text = value; }
26+
}
27+
28+
private static long downloadSize;
29+
public static long DownloadSize
30+
{
31+
get => downloadSize;
32+
set
33+
{
34+
Main.DownloadSizeDisplay.Text = GetFormattedBytes(value);
35+
downloadSize = value;
36+
}
37+
}
38+
private static long modifiedSize;
39+
public static long ModifiedSize
40+
{
41+
get => modifiedSize;
42+
set
43+
{
44+
modifiedSize = value;
45+
Main.ManagerSizeDisplay.Text = GetFormattedBytes(modifiedSize - InitialSize);
46+
}
47+
}
48+
public static long InitialSize { get; set; }
49+
50+
public static bool UpdateMode { get; set; } = false;
51+
52+
public static List<Dictionary<string, string>> InitialComponentInfo { get; set; } = new List<Dictionary<string, string>>();
53+
public static List<Dictionary<string, string>> AddedComponentInfo { get; set; } = new List<Dictionary<string, string>>();
54+
public static List<Dictionary<string, string>> RemovedComponentInfo { get; set; } = new List<Dictionary<string, string>>();
55+
56+
public static void Iterate(TreeNodeCollection parent, Action<TreeNode> action)
57+
{
58+
foreach (TreeNode childNode in parent)
59+
{
60+
action(childNode);
61+
62+
Iterate(childNode.Nodes, action);
63+
}
2164
}
2265

23-
public static void RecursiveAddToList(XmlNode sourceNode, TreeNodeCollection destNode)
66+
public static void RecursiveAddToList(XmlNode sourceNode, TreeNodeCollection destNode, bool setCheckState)
2467
{
2568
foreach (XmlNode node in sourceNode.ChildNodes)
2669
{
27-
var listNode = FPM.AddNodeToList(node, destNode);
70+
var listNode = AddNodeToList(node, destNode, setCheckState);
2871

29-
RecursiveAddToList(node, listNode.Nodes);
72+
RecursiveAddToList(node, listNode.Nodes, setCheckState);
3073
}
3174
}
3275

33-
public static TreeNode AddNodeToList(XmlNode child, TreeNodeCollection parent)
76+
public static TreeNode AddNodeToList(XmlNode child, TreeNodeCollection parent, bool setCheckState)
3477
{
3578
var listNode = parent.Add(child.Attributes["title"].Value);
3679
listNode.Tag = new Dictionary<string, string>
@@ -48,7 +91,7 @@ public static TreeNode AddNodeToList(XmlNode child, TreeNodeCollection parent)
4891
(listNode.Tag as Dictionary<string, string>).Add("size", child.Attributes["size"].Value);
4992
(listNode.Tag as Dictionary<string, string>).Add("hash", child.Attributes["hash"].Value);
5093

51-
listNode.Checked = bool.Parse(child.Attributes["checked"].Value);
94+
if (setCheckState) listNode.Checked = bool.Parse(child.Attributes["checked"].Value);
5295
}
5396

5497
if ((child.ParentNode.Name == "category" && child.ParentNode.Attributes["required"].Value == "true")
@@ -61,25 +104,80 @@ public static TreeNode AddNodeToList(XmlNode child, TreeNodeCollection parent)
61104
return listNode;
62105
}
63106

64-
public static long GetEstimatedSize(TreeNodeCollection sourceNodes)
107+
public static bool SetDownloadPath(string path, bool updateText)
65108
{
66-
long size = 0;
109+
string errorPath;
67110

68-
void Iterate(TreeNodeCollection parent)
111+
if (path.StartsWith(Environment.ExpandEnvironmentVariables("%ProgramW6432%"))
112+
|| path.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)))
69113
{
70-
foreach (TreeNode childNode in parent)
71-
{
72-
var attributes = childNode.Tag as Dictionary<string, string>;
114+
errorPath = "Program Files";
115+
}
116+
else if (path.StartsWith(Path.GetTempPath().Remove(Path.GetTempPath().Length - 1)))
117+
{
118+
errorPath = "Temporary Files";
119+
}
120+
else if (path.StartsWith(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "OneDrive")))
121+
{
122+
errorPath = "OneDrive";
123+
}
124+
else
125+
{
126+
if (updateText) DownloadPath = Path.Combine(path, "Flashpoint");
127+
128+
return true;
129+
}
130+
131+
MessageBox.Show(
132+
$"Flashpoint cannot be installed to the {errorPath} directory! Choose a different folder.",
133+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error
134+
);
135+
136+
return false;
137+
}
73138

74-
if (childNode.Checked && attributes["type"] == "component")
75-
{
76-
size += int.Parse(attributes["size"]);
77-
}
139+
public static bool SetFlashpointPath(string path, bool updateText)
140+
{
141+
bool isFlashpoint = false;
142+
143+
Iterate(Main.ComponentList2.Nodes, node =>
144+
{
145+
var attributes = node.Tag as Dictionary<string, string>;
146+
string infoPath = Path.Combine(path, "Components", attributes["path"], $"{attributes["title"]}.txt");
78147

79-
Iterate(childNode.Nodes);
148+
if (File.Exists(infoPath))
149+
{
150+
isFlashpoint = true;
151+
152+
return;
80153
}
154+
});
155+
156+
if (isFlashpoint)
157+
{
158+
if (updateText) FlashpointPath = path;
159+
160+
return true;
81161
}
82-
Iterate(sourceNodes);
162+
163+
MessageBox.Show($"Flashpoint was not found in this directory!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
164+
165+
return false;
166+
}
167+
168+
public static long GetEstimatedSize(TreeNodeCollection sourceNodes)
169+
{
170+
long size = 0;
171+
172+
Iterate(sourceNodes, node =>
173+
{
174+
var attributes = node.Tag as Dictionary<string, string>;
175+
176+
if (node.Checked && attributes["type"] == "component")
177+
{
178+
size += int.Parse(attributes["size"]);
179+
}
180+
});
83181

84182
return size;
85183
}
@@ -132,17 +230,9 @@ public static string GetFormattedBytes(long bytes)
132230
{
133231
return (Math.Truncate((double)bytes / 100000000) / 10).ToString("N1") + "GB";
134232
}
135-
else if (bytes >= 1000000)
136-
{
137-
return (Math.Truncate((double)bytes / 100000) / 10).ToString("N1") + "MB";
138-
}
139-
else if (bytes >= 1000)
140-
{
141-
return (Math.Truncate((double)bytes / 100) / 10).ToString("N1") + "KB";
142-
}
143233
else
144234
{
145-
return $"{bytes}B";
235+
return (Math.Truncate((double)bytes / 100000) / 10).ToString("N1") + "MB";
146236
}
147237
}
148238
}
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Install.cs renamed to Download.cs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace FlashpointInstaller
1818
{
19-
public partial class Install : Form
19+
public partial class Download : Form
2020
{
2121
List<Dictionary<string, string>> componentInfo = new List<Dictionary<string, string>>();
2222
Dictionary<string, string> workingComponent;
@@ -32,28 +32,22 @@ public partial class Install : Form
3232

3333
int cancelStatus = 0;
3434

35-
public Install() => InitializeComponent();
35+
public Download() => InitializeComponent();
3636

37-
private async void Install_Load(object sender, EventArgs e)
37+
private async void Download_Load(object sender, EventArgs e)
3838
{
3939
downloader.DownloadProgressChanged += OnDownloadProgressChanged;
4040
downloader.DownloadFileCompleted += OnDownloadFileCompleted;
41-
42-
void Iterate(TreeNodeCollection parent)
43-
{
44-
foreach (TreeNode childNode in parent)
45-
{
46-
var attributes = childNode.Tag as Dictionary<string, string>;
4741

48-
if (attributes["type"] == "component" && childNode.Checked)
49-
{
50-
componentInfo.Add(childNode.Tag as Dictionary<string, string>);
51-
}
42+
FPM.Iterate(FPM.Main.ComponentList.Nodes, node =>
43+
{
44+
var attributes = node.Tag as Dictionary<string, string>;
5245

53-
Iterate(childNode.Nodes);
46+
if (attributes["type"] == "component" && node.Checked)
47+
{
48+
componentInfo.Add(attributes);
5449
}
55-
}
56-
Iterate(FPM.Main.ComponentList.Nodes);
50+
});
5751

5852
foreach (var component in componentInfo)
5953
{
@@ -62,13 +56,13 @@ void Iterate(TreeNodeCollection parent)
6256

6357
if (cancelStatus != 0) return;
6458

65-
Directory.CreateDirectory(FPM.Path);
59+
Directory.CreateDirectory(FPM.DownloadPath);
6660
await Task.Run(ExtractTask);
6761

6862
byteProgress += int.Parse(component["size"]);
6963
}
7064

71-
if (cancelStatus == 0) FinishInstallation();
65+
if (cancelStatus == 0) FinishDownload();
7266
}
7367

7468
private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
@@ -110,23 +104,23 @@ private void ExtractTask()
110104
long extractedSize = 0;
111105
long totalSize = archive.TotalUncompressSize;
112106

113-
string infoPath = Path.Combine(FPM.Path, "Components", workingComponent["path"]);
107+
string infoPath = Path.Combine(FPM.DownloadPath, "Components", workingComponent["path"]);
114108
string infoFile = $"{workingComponent["title"]}.txt";
115109

116110
Directory.CreateDirectory(infoPath);
117111

118112
using (TextWriter writer = File.CreateText(infoPath + infoFile))
119113
{
120-
writer.WriteLine($"{workingComponent["hash"]} {workingComponent["url"]}");
114+
writer.WriteLine($"{workingComponent["hash"]} {workingComponent["size"]} {workingComponent["url"]}");
121115
}
122116

123117
while (cancelStatus == 0 && reader.MoveToNextEntry())
124118
{
125119
if (reader.Entry.IsDirectory) continue;
126120

127-
reader.WriteEntryToDirectory(
128-
FPM.Path, new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
129-
);
121+
reader.WriteEntryToDirectory(FPM.DownloadPath, new ExtractionOptions {
122+
ExtractFullPath = true, Overwrite = true, PreserveFileTime = true
123+
});
130124

131125
using (TextWriter writer = File.AppendText(infoPath + infoFile))
132126
{
@@ -161,7 +155,7 @@ private void ExtractTask()
161155
}
162156
}
163157

164-
private async void FinishInstallation()
158+
private async void FinishDownload()
165159
{
166160
await Task.Run(() =>
167161
{
@@ -179,8 +173,8 @@ await Task.Run(() =>
179173
foreach (string path in shortcutPaths)
180174
{
181175
IWshShortcut shortcut = new WshShell().CreateShortcut(Path.Combine(path, "Flashpoint.lnk"));
182-
shortcut.TargetPath = Path.Combine(FPM.Path, @"Launcher\Flashpoint.exe");
183-
shortcut.WorkingDirectory = Path.Combine(FPM.Path, @"Launcher");
176+
shortcut.TargetPath = Path.Combine(FPM.DownloadPath, @"Launcher\Flashpoint.exe");
177+
shortcut.WorkingDirectory = Path.Combine(FPM.DownloadPath, @"Launcher");
184178
shortcut.Description = "Shortcut to Flashpoint";
185179
shortcut.Save();
186180
}
@@ -189,8 +183,8 @@ await Task.Run(() =>
189183
Hide();
190184
FPM.Main.Hide();
191185

192-
var FinishWindow = new Finish();
193-
FinishWindow.ShowDialog();
186+
var finishWindow = new Finish();
187+
finishWindow.ShowDialog();
194188
}
195189

196190
private async void Cancel_Click(object sender, EventArgs e)
@@ -202,10 +196,7 @@ await Task.Run(() =>
202196
{
203197
while (cancelStatus != 2) { }
204198

205-
if (Directory.Exists(FPM.Path))
206-
{
207-
Directory.Delete(FPM.Path, true);
208-
}
199+
if (Directory.Exists(FPM.DownloadPath)) Directory.Delete(FPM.DownloadPath, true);
209200
});
210201

211202
Close();
File renamed without changes.

Finish.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private void Exit(object sender, EventArgs e)
1818
var flashpointProcess = new Process();
1919
flashpointProcess.StartInfo.UseShellExecute = true;
2020
flashpointProcess.StartInfo.FileName = "Flashpoint.exe";
21-
flashpointProcess.StartInfo.WorkingDirectory = Path.Combine(FPM.Path, @"Launcher");
21+
flashpointProcess.StartInfo.WorkingDirectory = Path.Combine(FPM.DownloadPath, @"Launcher");
2222
flashpointProcess.Start();
2323
}
2424

0 commit comments

Comments
 (0)