84 lines
1.8 KiB
C#
Raw Normal View History

using System.IO;
using RageCoop.Core.Scripting;
using ICSharpCode.SharpZipLib.Zip;
2022-06-30 09:28:13 +08:00
using System;
namespace RageCoop.Client.Scripting
{
internal class Resources:ResourceLoader
{
2022-06-12 17:11:14 +08:00
public Resources() : base("RageCoop.Client.Scripting.ClientScript", Main.Logger) { }
private void StartAll()
{
lock (LoadedResources)
{
foreach (var d in LoadedResources)
{
foreach (var s in d.Scripts)
{
2022-06-30 09:28:13 +08:00
try
{
s.OnStart();
}
catch(Exception ex)
{
Logger.Error("Error occurred when starting script:"+s.GetType().FullName);
Logger?.Error(ex);
}
}
}
}
}
private void StopAll()
{
lock (LoadedResources)
{
foreach (var d in LoadedResources)
{
foreach (var s in d.Scripts)
{
2022-06-30 09:28:13 +08:00
try
{
s.OnStop();
}
catch (Exception ex)
{
Logger.Error("Error occurred when stopping script:"+s.GetType().FullName);
Logger?.Error(ex);
}
}
}
}
}
/// <summary>
/// Load all resources from the server
/// </summary>
/// <param name="path">The path to the directory containing all resources to load.</param>
public void Load(string path)
{
Unload();
foreach (var d in Directory.GetDirectories(path))
{
if(Path.GetFileName(d).ToLower() != "data")
{
Directory.Delete(d, true);
}
}
Directory.CreateDirectory(path);
foreach (var resource in Directory.GetDirectories(path))
{
if (Path.GetFileName(resource).ToLower()!="data") { continue; }
2022-06-12 17:11:14 +08:00
Logger?.Info($"Loading resource: {Path.GetFileName(resource)}");
LoadResource(resource,Path.Combine(path,"data"));
}
StartAll();
}
public void Unload()
{
StopAll();
LoadedResources.Clear();
}
}
}