-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
169 lines (147 loc) · 6.61 KB
/
Main.cs
File metadata and controls
169 lines (147 loc) · 6.61 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
166
167
168
169
using LabApi.Events.CustomHandlers;
using LabApi.Features;
using LabApi.Features.Console;
using LabApi.Features.Wrappers;
using LabApi.Loader;
using LabApi.Loader.Features.Plugins;
using MEC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DeltaPatch;
public class Main : Plugin<Config>
{
public bool awaitingReboot = false;
public List<(string pluginName, string pluginStatus)> pluginUpdates = [];
public RoundEvents RoundEvents { get; } = new();
public static Main Instance { get; private set; }
public override string Name => "DeltaPatch";
public override string Description => "Made by Kenley M. with ❤";
public override string Author => "Kenley M.";
public override Version Version => Version.Parse("2.0.0");
public override Version RequiredApiVersion { get; } = new(LabApiProperties.CompiledVersion);
public override bool IsTransparent => true;
public string githubRepo = "KenleyundLeon/DeltaPatch";
public override void Enable()
{
Instance = this;
try
{
CustomHandlersManager.RegisterEventsHandler(RoundEvents);
if (!Config.IsEnabled) return;
Logger.Info(startmsg);
Timing.RunCoroutine(CheckUpdatesCoroutine(), Segment.RealtimeUpdate);
}
catch (Exception ex)
{
Logger.Error(ex.Message);
}
}
public override void Disable()
{
try {
Instance = null;
CustomHandlersManager.UnregisterEventsHandler(RoundEvents);
Logger.Info("DeltaPatch has been Disabled.");
}
catch (Exception ex)
{
Logger.Error(ex.Message);
}
}
private IEnumerator<float> CheckUpdatesCoroutine()
{
while (true)
{
if (awaitingReboot) Utils.LogMsg("warn", "Awaiting reboot to apply updates...");
Update(this.GetConfigDirectory());
yield return Timing.WaitForSeconds(Config.UpdateTimer);
}
}
private void Update(DirectoryInfo confDir)
{
Utils.LogMsg("info", "Checking for updates . . .");
if (Server.Host?.IsDestroyed ?? true) return;
if (Utils.Timeout > DateTime.Now)
{
Utils.LogMsg("warn", "[SKIPPING] Rate limit in effect.");
return;
}
var pluginInfos = Utils.GetPluginInfos(confDir);
if (pluginInfos.Count == 0)
{
Logger.Warn("No compatible plugins found.");
return;
}
Task.Run(async () =>
{
try
{
foreach (var item in pluginInfos)
{
var results = await Utils.GetLatestReleaseDateAsync(item.GithubRepo);
string pluginVersionStr = Regex.Replace(item.pluginVersion?.ToString() ?? "", @"\D", "");
string versionTagStr = Regex.Replace(results.tag ?? "", @"\D", "");
// ACHTUNG TEST HIER!!
int pluginVersion = int.TryParse(pluginVersionStr, out var pv) ? pv : 0;
int versionTag = int.TryParse(versionTagStr, out var vt) ? vt : 0;
if (results.publishedAt > File.GetLastWriteTime(item.FilePath) || versionTag > pluginVersion)
{
var path = item.FilePath.Contains("global")
? "global"
: (item.FilePath.Contains(Server.Port.ToString())
? Server.Port.ToString()
: "unknown");
Utils.LogMsg("warn", $"[UPDATE AVAILABLE] In {path} there is a new version ({results.tag}) available for {item.GithubRepo}.");
bool updated = await Utils.UpdatePlugin(item.FilePath, item.GithubRepo);
if (updated)
{
if (!Config.RebootOnUpdate)
{
var existing = pluginUpdates.FirstOrDefault(x => x.pluginName == item.PluginName);
if (existing.pluginStatus != "Update Available")
{
pluginUpdates.RemoveAll(x => x.pluginName == item.PluginName);
pluginUpdates.Add((item.PluginName, "Update Available"));
awaitingReboot = true;
await Utils.SendDiscordWebhookMessage(item.PluginName, item.pluginVersion.ToString(), results.tag);
}
continue;
}
if (!Round.IsRoundStarted)
{
Utils.LogMsg("info", $"[RESTARTING] Restarting server to apply updates...");
Timing.CallDelayed(1, Server.Restart);
}
}
else
{
Logger.Error($"[ERROR] Couldn't update: {item.GithubRepo}");
pluginUpdates.RemoveAll(x => x.pluginName == item.PluginName);
pluginUpdates.Add((item.PluginName, "Error while Updating"));
}
}
else
{
Utils.LogMsg("info", $"[UP-TO-DATE] The plugin {item.GithubRepo} is up-to-date.");
if (!pluginUpdates.Any(x => x.pluginName == item.PluginName && x.pluginStatus == "Up-to-date"))
{
pluginUpdates.RemoveAll(x => x.pluginName == item.PluginName);
pluginUpdates.Add((item.PluginName, "Up-to-date"));
}
}
}
if (awaitingReboot && !Round.IsRoundStarted && Config.RebootOnUpdate)
Timing.CallDelayed(1, Server.Restart);
}
catch (Exception ex)
{
Logger.Error($"[Update] Unhandled exception during update check: {ex.Message}");
}
});
}
private const string startmsg = "\n ____ _____ _ _____ _ ____ _ _____ ____ _ _ \r\n| _ \\| ____| | |_ _|/ \\ | _ \\ / \\|_ _/ ___| | | |\r\n| | | | _| | | | | / _ \\ | |_) / _ \\ | || | | |_| |\r\n| |_| | |___| |___| |/ ___ \\| __/ ___ \\| || |___| _ |\r\n|____/|_____|_____|_/_/ \\_\\_| /_/ \\_\\_| \\____|_| |_| \nMade by Kenley M.";
}