Resource interoperability
This commit is contained in:
parent
163f342d7a
commit
62e5bad6ac
@ -375,6 +375,43 @@ namespace RageCoop.Server.Scripting
|
|||||||
{
|
{
|
||||||
RegisterCustomEventHandler(CustomEvents.Hash(name), handler);
|
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>
|
/// <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.
|
/// 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>
|
/// </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
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,10 @@ namespace RageCoop.Server.Scripting
|
|||||||
{
|
{
|
||||||
internal class Resources
|
internal class Resources
|
||||||
{
|
{
|
||||||
private Dictionary<string,ServerResource> LoadedResources=new();
|
public Dictionary<string,ServerResource> LoadedResources=new();
|
||||||
private readonly Server Server;
|
private readonly Server Server;
|
||||||
private readonly Logger Logger;
|
private readonly Logger Logger;
|
||||||
|
public bool IsLoaded { get; private set; } = false;
|
||||||
public Resources(Server server)
|
public Resources(Server server)
|
||||||
{
|
{
|
||||||
Server = server;
|
Server = server;
|
||||||
@ -110,7 +111,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
Logger?.Warning($"Resource \"{name}\" has already been loaded, ignoring...");
|
Logger?.Warning($"Resource \"{name}\" has already been loaded, ignoring...");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Logger?.Info($"Loading resource: name");
|
Logger?.Info($"Loading resource: "+name);
|
||||||
var r = ServerResource.LoadFromZip(resource, Path.Combine("Resources", "Temp", "Server"), dataFolder, Logger);
|
var r = ServerResource.LoadFromZip(resource, Path.Combine("Resources", "Temp", "Server"), dataFolder, Logger);
|
||||||
LoadedResources.Add(r.Name, r);
|
LoadedResources.Add(r.Name, r);
|
||||||
}
|
}
|
||||||
@ -126,7 +127,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
{
|
{
|
||||||
foreach (var r in LoadedResources.Values)
|
foreach (var r in LoadedResources.Values)
|
||||||
{
|
{
|
||||||
foreach (ServerScript s in r.Scripts)
|
foreach (ServerScript s in r.Scripts.Values)
|
||||||
{
|
{
|
||||||
s.API=Server.API;
|
s.API=Server.API;
|
||||||
try
|
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 d in LoadedResources.Values)
|
||||||
{
|
{
|
||||||
foreach (var s in d.Scripts)
|
foreach (var s in d.Scripts.Values)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -41,7 +41,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
r.Dispose();
|
r.Dispose();
|
||||||
throw new FileNotFoundException($"Main assembly for resource \"{r.Name}\" cannot be found.");
|
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.DataFolder=Path.Combine(dataFolder, r.Name);
|
||||||
r.Reloaded+=(s, e) => { r.Logger?.Info($"Resource: {r.Name} has been reloaded"); };
|
r.Reloaded+=(s, e) => { r.Logger?.Info($"Resource: {r.Name} has been reloaded"); };
|
||||||
|
|
||||||
@ -105,7 +105,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all <see cref="ServerScript"/> instance in this resource
|
/// Get all <see cref="ServerScript"/> instance in this resource
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<ServerScript> Scripts { get; internal set; } = new List<ServerScript>();
|
public Dictionary<string,ServerScript> Scripts { get; internal set; } = new();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all <see cref="ResourceFile"/> that can be used to acces files in this resource
|
/// Get all <see cref="ResourceFile"/> that can be used to acces files in this resource
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -132,7 +132,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
var script = constructor.Invoke(null) as ServerScript;
|
var script = constructor.Invoke(null) as ServerScript;
|
||||||
script.CurrentResource = this;
|
script.CurrentResource = this;
|
||||||
script.CurrentFile=rfile;
|
script.CurrentFile=rfile;
|
||||||
Scripts.Add(script);
|
Scripts.Add(script.GetType().FullName,script);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -48,7 +48,7 @@ namespace RageCoop.Server
|
|||||||
internal Client _hostClient;
|
internal Client _hostClient;
|
||||||
|
|
||||||
private Dictionary<int,FileTransfer> InProgressFileTransfers=new();
|
private Dictionary<int,FileTransfer> InProgressFileTransfers=new();
|
||||||
private Resources Resources;
|
internal Resources Resources;
|
||||||
internal Logger Logger;
|
internal Logger Logger;
|
||||||
private Security Security;
|
private Security Security;
|
||||||
private System.Timers.Timer _sendInfoTimer = new System.Timers.Timer(5000);
|
private System.Timers.Timer _sendInfoTimer = new System.Timers.Timer(5000);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user