108 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RageCoop.Core.Scripting;
using System.IO;
using Ionic.Zip;
using System.Reflection;
namespace RageCoop.Server.Scripting
{
internal class Resources : ResourceLoader
{
2022-06-12 17:11:14 +08:00
public Resources() : base("RageCoop.Server.Scripting.ServerScript", Program.Logger) { }
public static bool HasClientResources = false;
public void LoadAll()
{
#region CLIENT
var path = Path.Combine("Resources", "Client");
Directory.CreateDirectory(path);
var clientResources = Directory.GetDirectories(path);
if (clientResources.Length!=0)
{
// Pack client side resources as a zip file
Logger.Info("Packing client-side resources");
try
{
var zippath = Path.Combine(path, "Resources.zip");
if (File.Exists(zippath))
{
File.Delete(zippath);
}
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(path);
zip.Save(zippath);
}
HasClientResources=true;
}
catch (Exception ex)
{
Logger.Error("Failed to pack client resources");
Logger.Error(ex);
}
}
#endregion
#region SERVER
path = Path.Combine("Resources", "Server");
Directory.CreateDirectory(path);
foreach (var resource in Directory.GetDirectories(path))
{
Logger.Info($"Loading resource: {Path.GetFileName(resource)}");
LoadResource(resource);
}
// Start scripts
lock (LoadedResources)
{
foreach (var d in LoadedResources)
{
foreach (var s in d.Scripts)
{
(s as ServerScript).CurrentResource = d;
try
{
s.OnStart();
}
catch(Exception ex) {Logger.Error($"Failed to start resource: {d.Name}"); Logger.Error(ex); }
}
}
}
#endregion
}
public void StopAll()
{
lock (LoadedResources)
{
foreach (var d in LoadedResources)
{
foreach (var s in d.Scripts)
{
s.OnStop();
}
}
}
}
public void SendTo(Client client)
{
string path;
if (HasClientResources && File.Exists(path = Path.Combine("Resources", "Client", "Resources.zip")))
{
Task.Run(() =>
{
2022-06-21 18:13:30 +08:00
Logger.Info($"Sending resources to client:{client.Username}");
Server.SendFile(path, "Resources.zip", client);
2022-06-21 18:13:30 +08:00
Logger.Info($"Resources sent to:{client.Username}");
});
}
}
}
}