Skip to content

Commit 4af4a5a

Browse files
committed
[FlashpointManager] Fix duplicate items in update tab, reduce File.Exists() use
1 parent 9336c4e commit 4af4a5a

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

FlashpointManager/src/Common.cs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -297,41 +297,17 @@ public static void SyncManager()
297297

298298
DownloadedSize = ComponentTracker.Downloaded.Sum(c => long.Parse(File.ReadLines(c.InfoFile).First().Split(' ')[1]));
299299

300-
long totalSizeChange = 0;
301-
302-
void AddToQueue(Component component, long oldSize)
303-
{
304-
long sizeChange = component.Size - oldSize;
305-
totalSizeChange += sizeChange;
306-
307-
string displayedSize = GetFormattedBytes(sizeChange);
308-
if (displayedSize[0] != '-') displayedSize = "+" + displayedSize;
309-
310-
var item = new ListViewItem();
311-
item.Text = component.Title;
312-
item.SubItems.Add(component.Description);
313-
item.SubItems.Add(component.LastUpdated);
314-
item.SubItems.Add(displayedSize);
315-
316-
Main.UpdateList.Items.Add(item);
317-
ComponentTracker.Outdated.Add(component);
318-
}
319-
320300
IterateXML(XmlTree.GetElementsByTagName("list")[0].ChildNodes, node =>
321301
{
322302
if (node.Name != "component") return;
323303

324304
var component = new Component(node);
325-
326305
bool update = false;
327-
long oldSize = 0;
328306

329-
if (ComponentTracker.Downloaded.Any(item => item.ID == component.ID))
307+
if (ComponentTracker.Downloaded.Exists(c => c.ID == component.ID))
330308
{
331-
string[] componentData = File.ReadLines(component.InfoFile).First().Split(' ');
332-
333-
update = componentData[0] != component.Hash;
334-
oldSize = long.Parse(componentData[1]);
309+
string localHash = File.ReadLines(component.InfoFile).First().Split(' ')[0];
310+
update = localHash != component.Hash;
335311
}
336312
else if (component.Required)
337313
{
@@ -340,19 +316,44 @@ void AddToQueue(Component component, long oldSize)
340316

341317
if (update)
342318
{
343-
AddToQueue(component, oldSize);
319+
ComponentTracker.Outdated.Add(component);
344320

345321
foreach (string dependID in component.Depends)
346322
{
347-
if (!ComponentTracker.Downloaded.Any(item => item.ID == dependID))
323+
if (!ComponentTracker.Downloaded.Exists(c => c.ID == dependID))
348324
{
349325
var query = Main.ComponentList.Nodes.Find(dependID, true);
350-
if (query.Length > 0) AddToQueue(query[0].Tag as Component, 0);
326+
if (query.Length > 0) ComponentTracker.Outdated.Add(query[0].Tag as Component);
351327
}
352328
}
353329
}
354330
});
355331

332+
ComponentTracker.Outdated = ComponentTracker.Outdated.Distinct().ToList();
333+
334+
long totalSizeChange = 0;
335+
336+
foreach (var component in ComponentTracker.Outdated)
337+
{
338+
long oldSize = ComponentTracker.Downloaded.Exists(c => c.ID == component.ID)
339+
? long.Parse(File.ReadLines(component.InfoFile).First().Split(' ')[1])
340+
: 0;
341+
342+
long sizeChange = component.Size - oldSize;
343+
totalSizeChange += sizeChange;
344+
345+
string displayedSize = GetFormattedBytes(sizeChange);
346+
if (displayedSize[0] != '-') displayedSize = "+" + displayedSize;
347+
348+
var item = new ListViewItem();
349+
item.Text = component.Title;
350+
item.SubItems.Add(component.Description);
351+
item.SubItems.Add(component.LastUpdated);
352+
item.SubItems.Add(displayedSize);
353+
354+
Main.UpdateList.Items.Add(item);
355+
}
356+
356357
Main.ChangeButton.Text = $"Apply changes";
357358
Main.ChangeButton.Enabled = false;
358359

@@ -428,7 +429,7 @@ public static bool CheckDependencies(bool alertDepends = true)
428429
{
429430
var component = node.Tag as Component;
430431

431-
if (component.Downloaded)
432+
if (ComponentTracker.Downloaded.Exists(c => c.ID == component.ID))
432433
{
433434
requiredDepends.AddRange(File.ReadLines(component.InfoFile).First().Split(' ').Skip(2).ToArray());
434435
}
@@ -450,7 +451,7 @@ public static bool CheckDependencies(bool alertDepends = true)
450451
{
451452
node.Checked = true;
452453

453-
if (ComponentTracker.Downloaded.Any(depend => depend.ID == component.ID))
454+
if (ComponentTracker.Downloaded.Exists(depend => depend.ID == component.ID))
454455
{
455456
persistDepends.Add(component.Title);
456457
}

FlashpointManager/src/Forms/Main.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void ComponentList_AfterCheck(object sender, TreeViewEventArgs e)
7474

7575
var component = node.Tag as Component;
7676

77-
size += component.Downloaded
77+
size += FPM.ComponentTracker.Downloaded.Exists(c => c.ID == component.ID)
7878
? long.Parse(File.ReadLines(component.InfoFile).First().Split(' ')[1])
7979
: component.Size;
8080
});
@@ -122,8 +122,6 @@ private void ChangeButton_Click(object sender, EventArgs e)
122122

123123
private void UpdateButton_Click(object sender, EventArgs e)
124124
{
125-
FPM.CheckDependencies();
126-
127125
FPM.UpdateMode = true;
128126

129127
var downloadWindow = new Operate();

FlashpointManager/src/Forms/Operate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private async void Operation_Load(object sender, EventArgs e)
7373
}
7474
}
7575

76-
byteTotal = removedComponents.Concat(addedComponents).Sum(item => item.Size);
76+
byteTotal = removedComponents.Concat(addedComponents).Sum(c => c.Size);
7777

7878
foreach (var component in addedComponents)
7979
{

0 commit comments

Comments
 (0)