-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModStatusTracker.cs
More file actions
165 lines (148 loc) · 5.16 KB
/
Copy pathModStatusTracker.cs
File metadata and controls
165 lines (148 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Collections.Generic;
using System.Text;
namespace ModUpdater
{
public class ModStatusTracker
{
private readonly object lockObj = new object();
private readonly List<ModStatusEntry> entries = new List<ModStatusEntry>();
private readonly StringBuilder sb = new StringBuilder();
private string cachedText = "No mods found.";
private bool dirty = true;
public bool HasUpdate { get { lock (lockObj) return dirty; } }
public void Clear()
{
lock (lockObj)
{
entries.Clear();
dirty = true;
}
}
public void SetMod(string name, string version, ModStatus status)
{
lock (lockObj)
{
for (int i = 0; i < entries.Count; i++)
{
if (entries[i].Name == name)
{
var entry = entries[i];
entry.Version = version;
entry.Status = status;
entries[i] = entry;
dirty = true;
return;
}
}
entries.Add(new ModStatusEntry { Name = name, Version = version, Status = status });
dirty = true;
}
}
public string BuildDisplayText()
{
lock (lockObj)
{
if (!dirty)
return cachedText;
if (entries.Count == 0)
{
cachedText = "No mods found.";
dirty = false;
return cachedText;
}
sb.Clear();
foreach (var entry in entries)
{
if (entry.Status == ModStatus.Installed)
sb.Append("<b><color=green>OK</color> ");
else if (entry.Status == ModStatus.Skipped)
sb.Append("<b><color=yellow>-</color> ");
else
sb.Append("<b><color=red>X</color> ");
sb.Append(entry.Name);
sb.Append("</b> ... ");
sb.Append(entry.Version);
if (entry.Status != ModStatus.Installed)
{
sb.Append(" .... ");
sb.Append(StatusLabel(entry.Status));
}
sb.AppendLine();
}
cachedText = sb.ToString().TrimEnd();
dirty = false;
return cachedText;
}
}
public string StatusSummary
{
get
{
lock (lockObj)
{
int downloading = 0;
bool anyDownloaded = false;
foreach (var entry in entries)
{
if (entry.Status == ModStatus.Downloading || entry.Status == ModStatus.Queued)
downloading++;
else if (entry.Status == ModStatus.Downloaded)
anyDownloaded = true;
}
if (downloading > 0)
return $"Downloading {downloading}";
if (anyDownloaded)
return "NEED RESTART";
return "Up to date";
}
}
}
public void GetDownloadProgress(out int completed, out int total, out bool isDownloading)
{
lock (lockObj)
{
completed = 0;
total = 0;
isDownloading = false;
foreach (var entry in entries)
{
switch (entry.Status)
{
case ModStatus.Downloaded:
completed++;
total++;
break;
case ModStatus.Downloading:
total++;
isDownloading = true;
break;
case ModStatus.Queued:
case ModStatus.Failed:
total++;
break;
case ModStatus.Skipped:
break;
}
}
}
}
private static string StatusLabel(ModStatus status)
{
switch (status)
{
case ModStatus.Downloading: return "Downloading";
case ModStatus.Queued: return "Queued";
case ModStatus.Downloaded: return "Updated";
case ModStatus.Skipped: return "Skipped";
case ModStatus.Failed: return "Failed";
default: return "";
}
}
private struct ModStatusEntry
{
public string Name;
public string Version;
public ModStatus Status;
}
}
}