Resource interoperability

This commit is contained in:
Sardelka 2022-07-29 17:44:02 +08:00
parent 163f342d7a
commit 62e5bad6ac
4 changed files with 66 additions and 8 deletions

View File

@ -375,6 +375,43 @@ namespace RageCoop.Server.Scripting
{
RegisterCustomEventHandler(CustomEvents.Hash(name), handler);
}
/// <summary>
/// Find a script matching the specified type
/// </summary>
/// <typeparam name="T">Type of the script to search for</typeparam>
/// <param name="resourceName">Which resource to search for this script. Will search in all loaded resources if unspecified </param>
/// <returns></returns>
public dynamic FindScript<T>(string resourceName=null) where T : ServerScript
{
if (resourceName==null)
{
foreach(var res in LoadedResources.Values)
{
if (res.Scripts.TryGetValue(typeof(T).FullName, out var script))
{
return script;
}
}
}
else if (LoadedResources.TryGetValue(resourceName, out var res))
{
Logger?.Debug("Found matching resource:"+resourceName);
Logger.Debug(typeof(T).FullName);
if(res.Scripts.TryGetValue(typeof(T).FullName, out var script))
{
return script;
}
}
return null;
}
#endregion
#region PROPERTIES
/// <summary>
/// Get a <see cref="Core.Logger"/> that the server is currently using, you should use <see cref="ServerResource.Logger"/> to display resource-specific information.
/// </summary>
@ -396,6 +433,25 @@ namespace RageCoop.Server.Scripting
}
}
}
/// <summary>
/// Get all currently loaded <see cref="ServerResource"/> as a dictionary indexed by their names
/// </summary>
/// <remarks>Accessing this property from script constructor is stronly discouraged since other scripts and resources might have yet been loaded.
/// Accessing from <see cref="ServerScript.OnStart"/> is not recommended either. Although all script assemblies will have been loaded to memory and instantiated, <see cref="ServerScript.OnStart"/> invocation of other scripts are not guaranteed.
/// </remarks>
public Dictionary<string,ServerResource> LoadedResources
{
get
{
if (!Server.Resources.IsLoaded)
{
Logger?.Warning("Attempting to get resources before all scripts are loaded");
Logger.Trace(new System.Diagnostics.StackTrace().ToString());
}
return Server.Resources.LoadedResources;
}
}
#endregion
}
}

View File

@ -13,9 +13,10 @@ namespace RageCoop.Server.Scripting
{
internal class Resources
{
private Dictionary<string,ServerResource> LoadedResources=new();
public Dictionary<string,ServerResource> LoadedResources=new();
private readonly Server Server;
private readonly Logger Logger;
public bool IsLoaded { get; private set; } = false;
public Resources(Server server)
{
Server = server;
@ -110,7 +111,7 @@ namespace RageCoop.Server.Scripting
Logger?.Warning($"Resource \"{name}\" has already been loaded, ignoring...");
continue;
}
Logger?.Info($"Loading resource: name");
Logger?.Info($"Loading resource: "+name);
var r = ServerResource.LoadFromZip(resource, Path.Combine("Resources", "Temp", "Server"), dataFolder, Logger);
LoadedResources.Add(r.Name, r);
}
@ -126,7 +127,7 @@ namespace RageCoop.Server.Scripting
{
foreach (var r in LoadedResources.Values)
{
foreach (ServerScript s in r.Scripts)
foreach (ServerScript s in r.Scripts.Values)
{
s.API=Server.API;
try
@ -138,6 +139,7 @@ namespace RageCoop.Server.Scripting
}
}
}
IsLoaded=true;
}
}
@ -147,7 +149,7 @@ namespace RageCoop.Server.Scripting
{
foreach (var d in LoadedResources.Values)
{
foreach (var s in d.Scripts)
foreach (var s in d.Scripts.Values)
{
try
{

View File

@ -41,7 +41,7 @@ namespace RageCoop.Server.Scripting
r.Dispose();
throw new FileNotFoundException($"Main assembly for resource \"{r.Name}\" cannot be found.");
}
r.Scripts = new List<ServerScript>();
r.Scripts = new();
r.DataFolder=Path.Combine(dataFolder, r.Name);
r.Reloaded+=(s, e) => { r.Logger?.Info($"Resource: {r.Name} has been reloaded"); };
@ -105,7 +105,7 @@ namespace RageCoop.Server.Scripting
/// <summary>
/// Get all <see cref="ServerScript"/> instance in this resource
/// </summary>
public List<ServerScript> Scripts { get; internal set; } = new List<ServerScript>();
public Dictionary<string,ServerScript> Scripts { get; internal set; } = new();
/// <summary>
/// Get all <see cref="ResourceFile"/> that can be used to acces files in this resource
/// </summary>
@ -132,7 +132,7 @@ namespace RageCoop.Server.Scripting
var script = constructor.Invoke(null) as ServerScript;
script.CurrentResource = this;
script.CurrentFile=rfile;
Scripts.Add(script);
Scripts.Add(script.GetType().FullName,script);
count++;
}
catch (Exception ex)

View File

@ -48,7 +48,7 @@ namespace RageCoop.Server
internal Client _hostClient;
private Dictionary<int,FileTransfer> InProgressFileTransfers=new();
private Resources Resources;
internal Resources Resources;
internal Logger Logger;
private Security Security;
private System.Timers.Timer _sendInfoTimer = new System.Timers.Timer(5000);