Repository navigation
Expand file tree
/
Copy pathModUpdaterPlugin.cs
More file actions
121 lines (104 loc) · 4.18 KB
/
Copy pathModUpdaterPlugin.cs
File metadata and controls
121 lines (104 loc) · 4.18 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
using System.Collections;
using System.Diagnostics;
using System.Net;
using BepInEx;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace ModUpdater
{
[BepInPlugin("com.onwardmodcommunity.modupdater", "Mod Updater", "0.9.0")]
public class ModUpdaterPlugin : BaseUnityPlugin
{
public static BepInEx.Logging.ManualLogSource Log { get; private set; }
private const string RemoteManifestUrl =
"https://raw.githubusercontent.com/Onward-Mod-Community/ModList/main/onward_forever_mod_list.json";
private ModUpdater updater;
private ModStatusTracker statusTracker;
private bool blockMultiplayer = true;
private Coroutine menuCoroutine;
private WaitForSeconds wait = new WaitForSeconds(1.0f / 60.0f);
private void Awake()
{
Log = Logger;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Logger.LogInfo("ModUpdater plugin loaded.");
DontDestroyOnLoad(gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
statusTracker = new ModStatusTracker();
updater = new ModUpdater(
remoteManifestUrl: RemoteManifestUrl,
pluginsPath: Paths.PluginPath,
onComplete: OnUpdateComplete,
statusTracker: statusTracker,
logger: Logger
);
updater.StartUpdate();
}
private IEnumerator MenuCoroutine()
{
while (true)
{
TextOverlay.DisableMultiButton(blockMultiplayer);
yield return wait;
}
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name.Contains("Menu"))
{
TextOverlay.Init();
if (menuCoroutine == null)
{
Log.LogDebug("starting the multiplayer block coroutine");
menuCoroutine = StartCoroutine(MenuCoroutine());
}
}
else
{
if (menuCoroutine != null)
{
Log.LogDebug("STOPPING the multiplayer block coroutine");
StopCoroutine(menuCoroutine);
menuCoroutine = null;
}
}
}
private void OnUpdateComplete(UpdateResult result)
{
// if a ref "isOn" it means that the reference is not clickable because it assumes it is at that point there
// And by setting it to true, it will block the user from trying to play games with the wrong
// mods
switch (result.Status)
{
case UpdateStatus.AllCurrent:
Logger.LogInfo("All mods are already up to date.");
TextOverlay.SetText("All mods are up to date!");
blockMultiplayer = false;
break;
case UpdateStatus.UpdatedSuccessfully:
var failInfo = result.FailedMods.Count > 0 ? $", {result.FailedMods.Count} failed" : "";
Logger.LogInfo($"All mods updated successfully! ({result.UpdatedCount} updated{failInfo})");
TextOverlay.SetText($"All mods updated! ({result.UpdatedCount} updated{failInfo})"
+ " - <color=#FF4444>Restart Required</color>");
blockMultiplayer = true;
break;
case UpdateStatus.CompletedWithErrors:
Logger.LogWarning(
$"Update finished with {result.FailedMods.Count} error(s). " +
$"{result.UpdatedCount} mod(s) updated. " +
$"Failed: {string.Join(", ", result.FailedMods)}"
);
TextOverlay.SetText($"Update finished with {result.FailedMods.Count} error(s)");
blockMultiplayer = true;
break;
default:
TextOverlay.SetText("Failed when doing OnUpdateComplete");
break;
}
}
}
}