Added /mm sendinventory
This commit is contained in:
parent
ab425a04cc
commit
e9a113abdd
8 changed files with 443 additions and 2 deletions
198
MosswartMassacre/UpdateManager.cs
Normal file
198
MosswartMassacre/UpdateManager.cs
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MosswartMassacre
|
||||
{
|
||||
public static class UpdateManager
|
||||
{
|
||||
private const string UPDATE_URL = "https://git.snakedesert.se/SawatoMosswartsEnjoyersClub/MosswartMassacre/raw/branch/spawn-detection/MosswartMassacre/bin/Release/MosswartMassacre.dll";
|
||||
|
||||
private static bool updateAvailable = false;
|
||||
private static long remoteFileSize = 0;
|
||||
private static long localFileSize = 0;
|
||||
private static DateTime lastCheckTime = DateTime.MinValue;
|
||||
|
||||
public static bool IsUpdateAvailable => updateAvailable;
|
||||
public static DateTime LastCheckTime => lastCheckTime;
|
||||
|
||||
public static async Task<bool> CheckForUpdateAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] Checking for updates...");
|
||||
|
||||
// Get local file size
|
||||
string localPath = GetLocalDllPath();
|
||||
if (!File.Exists(localPath))
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] Error: Could not find local DLL file");
|
||||
return false;
|
||||
}
|
||||
|
||||
localFileSize = new FileInfo(localPath).Length;
|
||||
|
||||
// Check remote file size
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
client.Timeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, UPDATE_URL));
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
remoteFileSize = response.Content.Headers.ContentLength ?? 0;
|
||||
|
||||
if (remoteFileSize == 0)
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] Error: Could not determine remote file size");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Compare sizes
|
||||
updateAvailable = (remoteFileSize != localFileSize);
|
||||
lastCheckTime = DateTime.Now;
|
||||
|
||||
if (updateAvailable)
|
||||
{
|
||||
PluginCore.WriteToChat($"[Update] Update available! Local: {localFileSize} bytes, Remote: {remoteFileSize} bytes");
|
||||
}
|
||||
else
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] Up to date");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
PluginCore.WriteToChat($"[Update] Network error: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] Request timed out");
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PluginCore.WriteToChat($"[Update] Check failed: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<bool> DownloadAndInstallUpdateAsync()
|
||||
{
|
||||
if (!updateAvailable)
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] No update available. Run /mm checkforupdate first.");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] Downloading update...");
|
||||
|
||||
string localPath = GetLocalDllPath();
|
||||
string tempPath = localPath + ".tmp";
|
||||
string backupPath = localPath + ".bak";
|
||||
|
||||
// Download to temp file
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
client.Timeout = TimeSpan.FromSeconds(30);
|
||||
|
||||
var response = await client.GetAsync(UPDATE_URL);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
using (var fileStream = File.Create(tempPath))
|
||||
{
|
||||
await response.Content.CopyToAsync(fileStream);
|
||||
}
|
||||
}
|
||||
|
||||
// Validate downloaded file
|
||||
var downloadedSize = new FileInfo(tempPath).Length;
|
||||
if (downloadedSize != remoteFileSize)
|
||||
{
|
||||
File.Delete(tempPath);
|
||||
PluginCore.WriteToChat($"[Update] Download validation failed. Expected {remoteFileSize} bytes, got {downloadedSize} bytes");
|
||||
return false;
|
||||
}
|
||||
|
||||
PluginCore.WriteToChat("[Update] Download complete, installing...");
|
||||
|
||||
// Atomically replace current file with new version (creates backup automatically)
|
||||
File.Replace(tempPath, localPath, backupPath);
|
||||
|
||||
// Clear update flag
|
||||
updateAvailable = false;
|
||||
|
||||
PluginCore.WriteToChat("[Update] Update installed successfully!");
|
||||
PluginCore.WriteToChat("[Update] Previous version backed up as MosswartMassacre.dll.bak");
|
||||
|
||||
// Wait a moment for file system to settle, then trigger hot reload
|
||||
await System.Threading.Tasks.Task.Delay(1000);
|
||||
|
||||
try
|
||||
{
|
||||
// Touch the file to ensure FileSystemWatcher detects the change
|
||||
File.SetLastWriteTime(localPath, DateTime.Now);
|
||||
PluginCore.WriteToChat("[Update] Triggering hot reload...");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PluginCore.WriteToChat($"[Update] Could not trigger hot reload: {ex.Message}");
|
||||
PluginCore.WriteToChat("[Update] Please use /mm gui to reload manually");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
PluginCore.WriteToChat($"[Update] Download error: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] Download timed out");
|
||||
return false;
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
PluginCore.WriteToChat("[Update] File access denied. Make sure the plugin directory is writable.");
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PluginCore.WriteToChat($"[Update] Install failed: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetLocalDllPath()
|
||||
{
|
||||
// Get the path to the current DLL
|
||||
string assemblyPath = typeof(PluginCore).Assembly.Location;
|
||||
|
||||
// If empty (hot reload scenario), use AssemblyDirectory + filename
|
||||
if (string.IsNullOrEmpty(assemblyPath))
|
||||
{
|
||||
return Path.Combine(PluginCore.AssemblyDirectory, "MosswartMassacre.dll");
|
||||
}
|
||||
|
||||
return assemblyPath;
|
||||
}
|
||||
|
||||
public static string GetUpdateStatus()
|
||||
{
|
||||
if (lastCheckTime == DateTime.MinValue)
|
||||
{
|
||||
return "Update Status: Not checked";
|
||||
}
|
||||
|
||||
return updateAvailable ? "Update Status: Update available" : "Update Status: Up to date";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue