Add ServerScript/ClientScript.CurrentResource

This commit is contained in:
Sardelka
2022-06-22 09:45:17 +08:00
parent 6e2c0a3037
commit 8a42f7c626
6 changed files with 22 additions and 14 deletions

View File

@ -16,8 +16,8 @@
public abstract void OnStop();
/// <summary>
/// Get the resource directory this script belongs to, beware that this directory should not be used to store any client-specific information since it'll get deleted every time the resource is loaded.
/// Get the <see cref="Core.Scripting.Resource"/> object this script belongs to, this property will be initiated before <see cref="OnStart"/> (will be null if you access it in the constructor).
/// </summary>
public string CurrentDirectory { get;internal set; }
public Core.Scripting.Resource CurrentResource { get; internal set; }
}
}

View File

@ -15,7 +15,7 @@ namespace RageCoop.Client.Scripting
{
foreach (var s in d.Scripts)
{
(s as ClientScript).CurrentDirectory=d.Directory;
(s as ClientScript).CurrentResource=d;
Main.QueueAction(() => s.OnStart());
}
}

View File

@ -32,7 +32,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Logging\" />
<Folder Include="Scripting\Events\" />
</ItemGroup>

View File

@ -10,11 +10,17 @@ using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("RageCoop.Client")]
namespace RageCoop.Core.Scripting
{
internal class Resource
public class Resource
{
public string Name { get; set; }
public string Directory { get; set; }
public List<IScriptable> Scripts { get; set; }=new List<IScriptable>();
/// <summary>
/// Name of the resource
/// </summary>
public string Name { get;internal set; }
/// <summary>
/// The directory of current resource
/// </summary>
public string Directory { get;internal set; }
public List<IScriptable> Scripts { get; internal set; }=new List<IScriptable>();
}
internal class ResourceLoader
{
@ -45,7 +51,7 @@ namespace RageCoop.Core.Scripting
Name=Path.GetDirectoryName(path),
Directory=path,
};
foreach (var f in Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories))
foreach (var f in Directory.GetFiles(path, "*.dll"))
{
LoadScriptsFromAssembly(f, r);
}

View File

@ -63,8 +63,12 @@ namespace RageCoop.Server.Scripting
{
foreach (var s in d.Scripts)
{
(s as ServerScript).CurrentDirectory = d.Directory;
s.OnStart();
(s as ServerScript).CurrentResource = d;
try
{
s.OnStart();
}
catch(Exception ex) {Logger.Error($"Failed to start resource: {d.Name}"); Logger.Error(ex); }
}
}
}

View File

@ -16,11 +16,10 @@ namespace RageCoop.Server.Scripting
/// This method would be called from main thread when the server is shutting down, you MUST terminate all background jobs/threads in this method.
/// </summary>
public abstract void OnStop();
/// <summary>
/// Get the resource directory this script belongs to.
/// Get the <see cref="Core.Scripting.Resource"/> object this script belongs to, this property will be initiated before <see cref="OnStart"/> (will be null if you access it in the constructor).
/// </summary>
public string CurrentDirectory { get; internal set; }
public Core.Scripting.Resource CurrentResource { get;internal set; }
}
[AttributeUsage(AttributeTargets.Method, Inherited = false)]