Files
RAGECOOP-V/Client/Scripting/ClientScript.cs

63 lines
2.2 KiB
C#
Raw Normal View History

2023-02-12 22:06:57 +08:00
using GTA;
2023-02-27 11:54:02 +08:00
using RageCoop.Core;
2023-02-13 20:44:50 +08:00
using RageCoop.Core.Scripting;
2023-02-27 11:54:02 +08:00
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Runtime.InteropServices;
2023-02-12 22:06:57 +08:00
namespace RageCoop.Client.Scripting
{
2023-02-27 11:54:02 +08:00
[JsonDontSerialize]
2023-02-12 22:06:57 +08:00
[ScriptAttributes(NoDefaultInstance = true)]
2023-02-13 20:44:50 +08:00
public abstract class ClientScript : Script
2023-02-12 22:06:57 +08:00
{
2023-03-07 20:17:53 +08:00
readonly ConcurrentQueue<Func<bool>> _jobQueue = new();
readonly Queue<Func<bool>> _reAdd = new();
2023-02-27 11:54:02 +08:00
public ClientScript()
{
2023-03-07 20:17:53 +08:00
var dir = SHVDN.Core.CurrentDirectory;
CurrentResource = APIBridge.GetResourceFromPath(dir);
2023-02-27 11:54:02 +08:00
if (CurrentResource == null)
throw new Exception("No resource associated with this script is found");
2023-03-07 20:17:53 +08:00
CurrentFile = CurrentResource.Files.Values.FirstOrDefault(x => x?.FullPath?.ToLower() == FilePath?.ToLower());
2023-02-27 11:54:02 +08:00
if (CurrentFile == null)
{
Logger.Warning("No file associated with curent script was found");
}
}
protected void QueueAction(Func<bool> action) => _jobQueue.Enqueue(action);
protected void QueueAction(Action action) => QueueAction(() => { action(); return true; });
2023-03-06 21:54:41 +08:00
protected override void OnTick()
{
base.OnTick();
DoQueuedJobs();
}
2023-02-27 11:54:02 +08:00
private void DoQueuedJobs()
{
while (_reAdd.TryDequeue(out var toAdd))
_jobQueue.Enqueue(toAdd);
while (_jobQueue.TryDequeue(out var job))
{
if (!job())
_reAdd.Enqueue(job);
}
}
/// <summary>
/// Get the <see cref="ClientFile" /> instance where this script is loaded from.
/// </summary>
public ClientFile CurrentFile { get; }
2023-02-13 20:44:50 +08:00
/// <summary>
/// Get the <see cref="ClientResource" /> that this script belongs to.
/// </summary>
2023-02-27 11:54:02 +08:00
public ClientResource CurrentResource { get; }
2023-02-13 20:44:50 +08:00
/// <summary>
2023-02-27 11:54:02 +08:00
/// Eqivalent of <see cref="ClientResource.Logger" /> in <see cref="CurrentResource" />
2023-02-13 20:44:50 +08:00
/// </summary>
2023-02-27 11:54:02 +08:00
public ResourceLogger Logger => CurrentResource.Logger;
2023-02-12 22:06:57 +08:00
}
}