Skip to content

Commit

Permalink
Move download processing to DownloadModFile
Browse files Browse the repository at this point in the history
Another small step to making Mod Browser smaller
  • Loading branch information
Jofairden committed Jul 10, 2019
1 parent dc19a12 commit a8e984a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ internal class DownloadModFile : DownloadFile
public UIModDownloadItem ModBrowserItem;

public DownloadModFile(string url, string filePath, string displayText) : base(url, filePath, displayText) {
OnComplete += ProcessDownloadedMod;
}

private void ProcessDownloadedMod() {
var mod = ModLoader.GetMod(ModBrowserItem.ModName);
if (mod != null) {
Interface.modBrowser.anEnabledModDownloaded = true;
}

if (!ModBrowserItem.HasUpdate) Interface.modBrowser.aNewModDownloaded = true;
else Interface.modBrowser.aModUpdated = true;

if (ModLoader.autoReloadAndEnableModsLeavingModBrowser) ModLoader.EnableMod(ModBrowserItem.ModName);
Interface.modBrowser.RemoveItem(ModBrowserItem);
Interface.modBrowser.UpdateNeeded = true;
}

internal override void PreCopy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void HandleDownloads(params DownloadFile[] downloads) {
_downloads.Add(download);
}
}
ActivateUI();
Show();
}

public new void ActivateUI() {
public new void Show() {
Main.menuMode = Interface.downloadProgressID;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public override void OnActivate() {
}
}

private bool RemoveItem(UIModDownloadItem item) => _items.Remove(item);
internal bool RemoveItem(UIModDownloadItem item) => _items.Remove(item);

internal void ClearItems() => _items.Clear();

Expand Down Expand Up @@ -342,14 +342,11 @@ internal void DownloadMods(IEnumerable<string> modNames) {

foreach (string desiredMod in modNames) {
var mod = _items.FirstOrDefault(x => x.ModName == desiredMod);
if (mod == null) {
// Not found on the browser
if (mod == null) { // Not found on the browser
_missingMods.Add(desiredMod);
}
else if (mod.Installed == null || mod.HasUpdate) {
// Found, add to downloads
else if (mod.Installed == null || mod.HasUpdate) { // Found, add to downloads
var modDownload = mod.GetModDownload();
modDownload.OnComplete += () => { ProcessDownloadedMod(modDownload); };
downloads.Add(modDownload);
}
}
Expand Down Expand Up @@ -378,20 +375,6 @@ internal UIModDownloadItem FindModDownloadItem(string modName)
// }
// }

internal void ProcessDownloadedMod(DownloadModFile modDownload) {
var mod = ModLoader.GetMod(modDownload.ModBrowserItem.ModName);
if (mod != null) {
Interface.modBrowser.anEnabledModDownloaded = true;
}

if (!modDownload.ModBrowserItem.HasUpdate) Interface.modBrowser.aNewModDownloaded = true;
else Interface.modBrowser.aModUpdated = true;

if (ModLoader.autoReloadAndEnableModsLeavingModBrowser) ModLoader.EnableMod(modDownload.ModBrowserItem.ModName);
Interface.modBrowser.RemoveItem(modDownload.ModBrowserItem);
UpdateNeeded = true;
}

private void SetHeading(string heading) {
HeaderTextPanel.SetText(heading, 0.8f, true);
HeaderTextPanel.Recalculate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json.Linq;
Expand All @@ -20,7 +14,6 @@
using Terraria.ModLoader.Core;
using Terraria.ModLoader.UI.DownloadManager;
using Terraria.UI;
using Terraria.UI.Chat;

namespace Terraria.ModLoader.UI.ModBrowser
{
Expand Down Expand Up @@ -368,9 +361,6 @@ public override void MouseOut(UIMouseEvent evt) {
private void DownloadMod(UIMouseEvent evt, UIElement listeningElement) {
Main.PlaySound(SoundID.MenuTick);
var modDownload = GetModDownload();
modDownload.OnComplete += () => {
Interface.modBrowser.ProcessDownloadedMod(modDownload);
};
Interface.downloadProgress.gotoMenu = Interface.modBrowserID;
Interface.downloadProgress.HandleDownloads(modDownload);
}
Expand All @@ -384,12 +374,6 @@ private void DownloadWithDeps(UIMouseEvent evt, UIElement listeningElement) {
.Select(x => x.GetModDownload())
.ToList();
mods.Add(modDownload);
foreach (var mod in mods) {
mod.OnComplete += () => {
Interface.modBrowser.ProcessDownloadedMod(mod);
};
}

Interface.downloadProgress.gotoMenu = Interface.modBrowserID;
Interface.downloadProgress.HandleDownloads(mods.ToArray());
}
Expand All @@ -399,7 +383,6 @@ private void RequestMoreInfo(UIMouseEvent evt, UIElement listeningElement) {
Interface.modInfo.Show(ModName, DisplayName, Interface.modBrowserID, Installed, loadFromWeb: true);
}


public DownloadModFile GetModDownload() {
var modDownload = new DownloadModFile(DownloadUrl, $"{ModLoader.ModPath}{Path.DirectorySeparatorChar}{ModName}.tmod", DisplayName) {
ModBrowserItem = this
Expand Down

0 comments on commit a8e984a

Please sign in to comment.