From 20618621e39ad7ed996d937938a9f51459e72cf0 Mon Sep 17 00:00:00 2001 From: Controllerdestiny <523321293@qq.com> Date: Thu, 20 Jun 2024 02:26:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AutoUpdatePlugin/Plugin.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/AutoUpdatePlugin/Plugin.cs b/AutoUpdatePlugin/Plugin.cs index c514829e2..ea819cd56 100644 --- a/AutoUpdatePlugin/Plugin.cs +++ b/AutoUpdatePlugin/Plugin.cs @@ -25,6 +25,9 @@ public class Plugin : TerrariaPlugin private static readonly HttpClient _httpClient = new(); + private const string TempSaveDir = "TempFile"; + + private const string TempZipName = "Plugins.zip"; public Plugin(Main game) : base(game) { @@ -53,7 +56,7 @@ private void UpdateCmd(CommandArgs args) ExtractDirectoryZip(); args.Player.SendInfoMessage("正在升级插件..."); UpdatePlugin(updates); - args.Player.SendSuccessMessage("[更新完成]\n" + string.Join("\n", updates.Select(i => $"{i.Name} v{i.OldVersion} => {i.Name} v{i.NewVersion}"))); + args.Player.SendSuccessMessage("[更新完成]\n" + string.Join("\n", updates.Select(i => $"[{i.Name}] V{i.OldVersion} >>> V{i.NewVersion}"))); args.Player.SendSuccessMessage("重启服务器后插件生效!"); } catch (Exception ex) @@ -73,7 +76,7 @@ private void CheckCmd(CommandArgs args) args.Player.SendSuccessMessage("你的插件全是最新版本,无需更新哦~"); return; } - args.Player.SendInfoMessage("[插件更新列表]\n" + string.Join("\n", updates.Select(i => $"{i.Name} v{i.OldVersion} => {i.Name} v{i.NewVersion}"))); + args.Player.SendInfoMessage("[以下插件有新的版本更新]\n" + string.Join("\n", updates.Select(i => $"[{i.Name}] V{i.OldVersion} >>> V{i.NewVersion}"))); } catch (Exception ex) { @@ -92,7 +95,7 @@ private static List GetUpdate() if (!response.IsSuccessStatusCode) throw new Exception("无法连接服务器"); var json = response.Content.ReadAsStringAsync().Result; - var latestPluginList = JsonConvert.DeserializeObject>(json)!; + var latestPluginList = JsonConvert.DeserializeObject>(json) ?? new(); List pluginUpdateList = new(); foreach (var latestPluginInfo in latestPluginList) foreach (var plugin in plugins) @@ -122,33 +125,33 @@ private static List GetPlugins() private static void DownLoadPlugin() { - DirectoryInfo directoryInfo = new("TempFile"); + DirectoryInfo directoryInfo = new(TempSaveDir); if (!directoryInfo.Exists) directoryInfo.Create(); HttpClient httpClient = new(); var zipBytes = httpClient.GetByteArrayAsync(PUrl + ReleaseUrl).Result; - File.WriteAllBytes(Path.Combine(directoryInfo.FullName, "Plugins.zip"), zipBytes); + File.WriteAllBytes(Path.Combine(directoryInfo.FullName, TempZipName), zipBytes); } private static void ExtractDirectoryZip() { - DirectoryInfo directoryInfo = new("TempFile"); - ZipFile.ExtractToDirectory(Path.Combine(directoryInfo.FullName, "Plugins.zip"), Path.Combine(directoryInfo.FullName, "Plugins"), true); + DirectoryInfo directoryInfo = new(TempSaveDir); + ZipFile.ExtractToDirectory(Path.Combine(directoryInfo.FullName, TempZipName), Path.Combine(directoryInfo.FullName, "Plugins"), true); } private static void UpdatePlugin(List pluginUpdateInfos) { foreach (var pluginUpdateInfo in pluginUpdateInfos) { - string sourcePath = Path.Combine("TempFile", "Plugins", pluginUpdateInfo.RemotePath); + string sourcePath = Path.Combine(TempSaveDir, "Plugins", pluginUpdateInfo.RemotePath); string destinationPath = Path.Combine(ServerApi.ServerPluginsDirectoryPath, pluginUpdateInfo.LocalPath); // 确保目标目录存在 string destinationDirectory = Path.GetDirectoryName(destinationPath)!; // 复制并覆盖文件 File.Copy(sourcePath, destinationPath, true); } - if (Directory.Exists("TempFile")) - Directory.Delete("TempFile"); + if (Directory.Exists(TempSaveDir)) + Directory.Delete(TempSaveDir, true); } #endregion }