Skip to content

Commit

Permalink
修复
Browse files Browse the repository at this point in the history
  • Loading branch information
Controllerdestiny committed Jun 19, 2024
1 parent 843d0e7 commit 2061862
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions AutoUpdatePlugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -92,7 +95,7 @@ private static List<PluginUpdateInfo> GetUpdate()
if (!response.IsSuccessStatusCode)
throw new Exception("无法连接服务器");
var json = response.Content.ReadAsStringAsync().Result;
var latestPluginList = JsonConvert.DeserializeObject<List<PluginVersionInfo>>(json)!;
var latestPluginList = JsonConvert.DeserializeObject<List<PluginVersionInfo>>(json) ?? new();
List<PluginUpdateInfo> pluginUpdateList = new();
foreach (var latestPluginInfo in latestPluginList)
foreach (var plugin in plugins)
Expand Down Expand Up @@ -122,33 +125,33 @@ private static List<PluginVersionInfo> 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<PluginUpdateInfo> 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
}

0 comments on commit 2061862

Please sign in to comment.