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(); public abstract void OnStop();
/// <summary> /// <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> /// </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) foreach (var s in d.Scripts)
{ {
(s as ClientScript).CurrentDirectory=d.Directory; (s as ClientScript).CurrentResource=d;
Main.QueueAction(() => s.OnStart()); Main.QueueAction(() => s.OnStart());
} }
} }

View File

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

View File

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

View File

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

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. /// 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> /// </summary>
public abstract void OnStop(); public abstract void OnStop();
/// <summary> /// <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> /// </summary>
public string CurrentDirectory { get; internal set; } public Core.Scripting.Resource CurrentResource { get;internal set; }
} }
[AttributeUsage(AttributeTargets.Method, Inherited = false)] [AttributeUsage(AttributeTargets.Method, Inherited = false)]