Skip to content

Commit 9336c4e

Browse files
committed
[FlashpointManager] Remove SizeTracker object, disable apply button until changes are made
1 parent d745e09 commit 9336c4e

3 files changed

Lines changed: 36 additions & 53 deletions

File tree

FlashpointManager/src/Common.cs

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,16 @@ public static class FPM
193193
// Controls which component (if any) will be automatically downloaded at launch
194194
public static string AutoDownload { get; set; } = "";
195195

196-
// Object for tracking numerous file size sums
197-
public static class SizeTracker
198-
{
199-
// Tracks total size of components available locally
200-
public static long Downloaded { get; set; }
201-
// Tracks size difference from checking/unchecking components in the manager tab
202-
private static long toChange;
203-
public static long ToChange
204-
{
205-
get => toChange;
206-
set {
207-
toChange = value;
208-
Main.ChangeButton.Text = $"Apply changes ({GetFormattedBytes(toChange - Downloaded)})";
209-
}
210-
}
211-
}
196+
// Total size of every downloaded component; managed by SyncManager() function
197+
public static long DownloadedSize { get; set; } = 0;
212198

213-
// Object for tracking information about certain groups of components
199+
// Object providing easy access to certain groups of components; managed by SyncManager() function
214200
public static class ComponentTracker
215201
{
216-
// Information about components that are available locally
202+
// Returns all downloaded components
217203
public static List<Component> Downloaded { get; set; } = new List<Component>();
218-
// Information about components that are to be updated or added through the updater
219-
public static List<Component> ToUpdate { get; set; } = new List<Component>();
204+
// Returns all outdated components
205+
public static List<Component> Outdated { get; set; } = new List<Component>();
220206
}
221207

222208
// Performs an operation on every node in the specified TreeNodeCollection
@@ -288,7 +274,8 @@ public static TreeNode AddNodeToList(XmlNode child, TreeNodeCollection parent)
288274
public static void SyncManager()
289275
{
290276
ComponentTracker.Downloaded.Clear();
291-
ComponentTracker.ToUpdate.Clear();
277+
ComponentTracker.Outdated.Clear();
278+
Main.UpdateList.Items.Clear();
292279

293280
IterateList(Main.ComponentList.Nodes, node =>
294281
{
@@ -308,8 +295,7 @@ public static void SyncManager()
308295
}
309296
});
310297

311-
Main.UpdateList.Items.Clear();
312-
Main.UpdateButton.Text = "Install updates";
298+
DownloadedSize = ComponentTracker.Downloaded.Sum(c => long.Parse(File.ReadLines(c.InfoFile).First().Split(' ')[1]));
313299

314300
long totalSizeChange = 0;
315301

@@ -326,9 +312,9 @@ void AddToQueue(Component component, long oldSize)
326312
item.SubItems.Add(component.Description);
327313
item.SubItems.Add(component.LastUpdated);
328314
item.SubItems.Add(displayedSize);
329-
Main.UpdateList.Items.Add(item);
330315

331-
ComponentTracker.ToUpdate.Add(component);
316+
Main.UpdateList.Items.Add(item);
317+
ComponentTracker.Outdated.Add(component);
332318
}
333319

334320
IterateXML(XmlTree.GetElementsByTagName("list")[0].ChildNodes, node =>
@@ -367,18 +353,20 @@ void AddToQueue(Component component, long oldSize)
367353
}
368354
});
369355

370-
if (ComponentTracker.ToUpdate.Count > 0)
356+
Main.ChangeButton.Text = $"Apply changes";
357+
Main.ChangeButton.Enabled = false;
358+
359+
Main.UpdateButton.Text = "Install updates";
360+
361+
if (ComponentTracker.Outdated.Count > 0)
371362
{
372-
Main.UpdateButton.Enabled = true;
373363
Main.UpdateButton.Text += $" ({GetFormattedBytes(totalSizeChange)})";
364+
Main.UpdateButton.Enabled = true;
374365
}
375366
else
376367
{
377368
Main.UpdateButton.Enabled = false;
378369
}
379-
380-
SizeTracker.Downloaded = GetTotalSize(Main.ComponentList);
381-
SizeTracker.ToChange = SizeTracker.Downloaded;
382370
}
383371

384372
// Deletes a file as well as any directories made empty by its deletion
@@ -499,26 +487,6 @@ public static bool CheckDependencies(bool alertDepends = true)
499487
return true;
500488
}
501489

502-
// Gets total size in bytes of all checked components in the specified TreeView
503-
public static long GetTotalSize(TreeView sourceTree)
504-
{
505-
long size = 0;
506-
507-
IterateList(sourceTree.Nodes, node =>
508-
{
509-
if (node.Checked && node.Tag.GetType().ToString().EndsWith("Component"))
510-
{
511-
var component = node.Tag as Component;
512-
513-
size += component.Downloaded
514-
? long.Parse(File.ReadLines(component.InfoFile).First().Split(' ')[1])
515-
: component.Size;
516-
}
517-
});
518-
519-
return size;
520-
}
521-
522490
// Formats bytes as a human-readable string
523491
public static string GetFormattedBytes(long bytes)
524492
{

FlashpointManager/src/Forms/Main.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using System.Windows.Forms;
56
using System.Xml;
@@ -65,7 +66,21 @@ public void ComponentList_BeforeCheck(object sender, TreeViewCancelEventArgs e)
6566

6667
public void ComponentList_AfterCheck(object sender, TreeViewEventArgs e)
6768
{
68-
FPM.SizeTracker.ToChange = FPM.GetTotalSize(ComponentList);
69+
long size = 0;
70+
71+
FPM.IterateList(ComponentList.Nodes, node =>
72+
{
73+
if (!node.Checked || !node.Tag.GetType().ToString().EndsWith("Component")) return;
74+
75+
var component = node.Tag as Component;
76+
77+
size += component.Downloaded
78+
? long.Parse(File.ReadLines(component.InfoFile).First().Split(' ')[1])
79+
: component.Size;
80+
});
81+
82+
ChangeButton.Text = $"Apply changes ({FPM.GetFormattedBytes(size - FPM.DownloadedSize)})";
83+
ChangeButton.Enabled = true;
6984
}
7085

7186
private void ComponentList_BeforeSelect(object sender, TreeViewCancelEventArgs e)

FlashpointManager/src/Forms/Operate.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private async void Operation_Load(object sender, EventArgs e)
6262
}
6363
else
6464
{
65-
foreach (var component in FPM.ComponentTracker.ToUpdate)
65+
foreach (var component in FPM.ComponentTracker.Outdated)
6666
{
6767
if (FPM.ComponentTracker.Downloaded.Exists(c => c.ID == component.ID))
6868
{
@@ -256,7 +256,7 @@ private void RemoveComponents()
256256

257257
ProgressLabel.Invoke((MethodInvoker)delegate
258258
{
259-
string text = FPM.ComponentTracker.ToUpdate.Exists(c => c.ID == workingComponent.ID)
259+
string text = FPM.ComponentTracker.Outdated.Exists(c => c.ID == workingComponent.ID)
260260
? $"Removing old version of component \"{workingComponent.Title}\"..."
261261
: $"Removing component \"{workingComponent.Title}\"...";
262262

0 commit comments

Comments
 (0)