2022-06-12 15:39:32 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using RageCoop.Core.Scripting;
|
2022-06-24 16:49:59 +08:00
|
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
2022-06-30 09:28:13 +08:00
|
|
|
|
using System;
|
2022-06-12 15:39:32 +08:00
|
|
|
|
|
|
|
|
|
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) { }
|
2022-06-12 15:39:32 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
2022-06-12 15:39:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
2022-06-12 15:39:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Load all resources from the server
|
|
|
|
|
/// </summary>
|
2022-06-24 16:49:59 +08:00
|
|
|
|
/// <param name="path">The path to the directory containing all resources to load.</param>
|
2022-06-12 15:39:32 +08:00
|
|
|
|
public void Load(string path)
|
|
|
|
|
{
|
|
|
|
|
Unload();
|
|
|
|
|
foreach (var d in Directory.GetDirectories(path))
|
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
if(Path.GetFileName(d).ToLower() != "data")
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(d, true);
|
|
|
|
|
}
|
2022-06-12 15:39:32 +08:00
|
|
|
|
}
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
foreach (var resource in Directory.GetDirectories(path))
|
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
if (Path.GetFileName(resource).ToLower()!="data") { continue; }
|
2022-06-12 17:11:14 +08:00
|
|
|
|
Logger?.Info($"Loading resource: {Path.GetFileName(resource)}");
|
2022-06-24 16:49:59 +08:00
|
|
|
|
LoadResource(resource,Path.Combine(path,"data"));
|
2022-06-12 15:39:32 +08:00
|
|
|
|
}
|
|
|
|
|
StartAll();
|
|
|
|
|
}
|
|
|
|
|
public void Unload()
|
|
|
|
|
{
|
|
|
|
|
StopAll();
|
|
|
|
|
LoadedResources.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|