- Deleted unused service IPackageListRetrievalService.cs
- Added caching function to LuaScriptLoader.cs - Added sample async code to LuaScriptManagementService.cs - Removed most of the State functions in LuaCsSetup.cs (requires rewrite). - Fixed CsEnabled check. - Moved IsRunningWorkshop check to client-only project.
This commit is contained in:
+24
-4
@@ -23,6 +23,7 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using static Barotrauma.GameSettings;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services;
|
||||
@@ -34,6 +35,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
|
||||
[MemberNotNullWhen(true, nameof(_script))]
|
||||
public bool IsRunning => _isRunning;
|
||||
private List<ILuaScriptResourceInfo> _resourcesInfo = new List<ILuaScriptResourceInfo>();
|
||||
|
||||
private readonly AsyncReaderWriterLock _operationsLock = new ();
|
||||
|
||||
private readonly ILuaScriptLoader _luaScriptLoader;
|
||||
private readonly ILuaScriptServicesConfig _luaScriptServicesConfig;
|
||||
@@ -67,13 +70,30 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
|
||||
|
||||
public bool IsDisposed { get; private set; }
|
||||
|
||||
public Task<FluentResults.Result> LoadScriptResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourcesInfo)
|
||||
public async Task<FluentResults.Result> LoadScriptResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourcesInfo)
|
||||
{
|
||||
// Do any exception checks you can before acquiring a lock to avoid needlessly holding up resources.
|
||||
if (resourcesInfo.IsDefaultOrEmpty)
|
||||
ThrowHelper.ThrowArgumentNullException($"{nameof(LoadScriptResourcesAsync)}: The parameter is empty!");
|
||||
|
||||
// Acquire a lock:
|
||||
// Reader = Allow parallel operations (try to avoid nesting acquiring the lock when possible)
|
||||
// Writer = Exclusive use (ie. executing scripts or Dispose())
|
||||
using var lck = await _operationsLock.AcquireWriterLock(); // IDisposable using with generate a try-finally and release for you.
|
||||
IService.CheckDisposed(this); // Check disposed after you have the lock
|
||||
|
||||
// If you use a ConcurrentDictionary instead of a List, it will handle threading issues for you.
|
||||
_resourcesInfo.AddRange(resourcesInfo.OrderBy(static r => r.LoadPriority));
|
||||
|
||||
// TODO disk caching
|
||||
|
||||
return Task.FromResult(FluentResults.Result.Ok());
|
||||
// Use the StorageService's caching function by just loading the file with caching turned on.
|
||||
// Right now the LuaScriptLoader has this on by default.
|
||||
var cacheRes = await _luaScriptLoader.CacheResourcesAsync(resourcesInfo);
|
||||
|
||||
// Aggregate and return results to the caller to deal with. Optionally, log here if you want.
|
||||
// Automatically converted to a Task<T> when 'async' is in the method declaration.
|
||||
if (cacheRes.IsFailed)
|
||||
return cacheRes.ToResult();
|
||||
return new FluentResults.Result().WithReasons(cacheRes.Value.SelectMany(cr => cr.Item2.Reasons));
|
||||
}
|
||||
|
||||
private void SetupEnvironment()
|
||||
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services;
|
||||
|
||||
public sealed class PackageListRetrievalService : IPackageListRetrievalService
|
||||
{
|
||||
public void Dispose()
|
||||
{
|
||||
// stateless service
|
||||
return;
|
||||
}
|
||||
|
||||
public void CheckDisposed()
|
||||
{
|
||||
// stateless service
|
||||
return;
|
||||
}
|
||||
|
||||
public bool IsDisposed => false;
|
||||
|
||||
public IEnumerable<ContentPackage> GetEnabledContentPackages()
|
||||
{
|
||||
return ContentPackageManager.EnabledPackages.All;
|
||||
}
|
||||
|
||||
public IEnumerable<ContentPackage> GetAllContentPackages()
|
||||
{
|
||||
return ContentPackageManager.AllPackages;
|
||||
}
|
||||
}
|
||||
+30
-4
@@ -141,16 +141,31 @@ public sealed class PackageManagementService : IPackageManagementService
|
||||
|
||||
private FluentResults.Result UnsafeAddPackageInternal(ContentPackage package, IModConfigInfo config)
|
||||
{
|
||||
if (_loadedPackages.TryGetValue(package, out var result))
|
||||
if (_loadedPackages.TryGetValue(package, out _))
|
||||
{
|
||||
_logger.LogWarning($"Tried to load already-loaded package {package.Name}.");
|
||||
return FluentResults.Result.Ok();
|
||||
}
|
||||
|
||||
_loadedPackages[package] = config;
|
||||
var res = new FluentResults.Result();
|
||||
res.WithReasons(_luaScriptManagementService.LoadScriptResourcesAsync(config.LuaScripts).ConfigureAwait(false).GetAwaiter().GetResult().Reasons);
|
||||
return res;
|
||||
try
|
||||
{
|
||||
var res = new FluentResults.Result();
|
||||
var r = Task.WhenAll(
|
||||
new Task<Task<FluentResults.Result>>(async Task<FluentResults.Result> () => new FluentResults.Result()
|
||||
.WithReasons((await _configService.LoadConfigsAsync(config.Configs)).Reasons)
|
||||
.WithReasons((await _configService.LoadConfigsProfilesAsync(config.Configs)).Reasons)),
|
||||
new Task<Task<FluentResults.Result>>(async () => await _luaScriptManagementService.LoadScriptResourcesAsync(config.LuaScripts))
|
||||
).ConfigureAwait(false).GetAwaiter().GetResult();
|
||||
|
||||
foreach (var task in r)
|
||||
res.WithReasons(task.ConfigureAwait(false).GetAwaiter().GetResult().Reasons);
|
||||
return res;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return FluentResults.Result.Fail(new ExceptionalError(e));
|
||||
}
|
||||
}
|
||||
|
||||
public FluentResults.Result ExecuteLoadedPackages(ImmutableArray<ContentPackage> executionOrder)
|
||||
@@ -292,4 +307,15 @@ public sealed class PackageManagementService : IPackageManagementService
|
||||
IService.CheckDisposed(this);
|
||||
return _runningPackages.ContainsKey(package);
|
||||
}
|
||||
|
||||
public ImmutableArray<ContentPackage> GetLoadedAssemblyPackages()
|
||||
{
|
||||
using var lck = _operationsLock.AcquireReaderLock().ConfigureAwait(false).GetAwaiter().GetResult();
|
||||
IService.CheckDisposed(this);
|
||||
if (_loadedPackages.IsEmpty)
|
||||
return ImmutableArray<ContentPackage>.Empty;
|
||||
return [.._loadedPackages.Values
|
||||
.Where(cfg => !cfg.Assemblies.IsDefaultOrEmpty)
|
||||
.Select(cfg => cfg.Package)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
using MoonSharp.Interpreter.Loaders;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
using Barotrauma.LuaCs.Data;
|
||||
using FluentResults;
|
||||
using MoonSharp.Interpreter.Loaders;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services.Safe;
|
||||
|
||||
public interface ILuaScriptLoader : IService, IScriptLoader
|
||||
{
|
||||
void ClearCaches();
|
||||
Task<Result<ImmutableArray<(ContentPath Path, Result<string>)>>> CacheResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourceInfos);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using MoonSharp.Interpreter;
|
||||
using MoonSharp.Interpreter.Loaders;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Barotrauma.LuaCs.Data;
|
||||
using Barotrauma.LuaCs.Services.Safe;
|
||||
using FluentResults;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services.Safe
|
||||
{
|
||||
@@ -59,6 +62,15 @@ namespace Barotrauma.LuaCs.Services.Safe
|
||||
_storageService?.PurgeCache();
|
||||
}
|
||||
|
||||
public async Task<Result<ImmutableArray<(ContentPath Path, Result<string>)>>> CacheResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourceInfos)
|
||||
{
|
||||
// TODO: Needs an async lock?
|
||||
IService.CheckDisposed(this);
|
||||
if (!_storageService.UseCaching)
|
||||
return FluentResults.Result.Fail($"Caching is not enabled.");
|
||||
return await this._storageService.LoadPackageTextFilesAsync([..resourceInfos.SelectMany(ri => ri.FilePaths)]);
|
||||
}
|
||||
|
||||
public override bool ScriptFileExists(string file)
|
||||
{
|
||||
((IService)this).CheckDisposed();
|
||||
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services;
|
||||
|
||||
public interface IPackageListRetrievalService : IService
|
||||
{
|
||||
IEnumerable<ContentPackage> GetEnabledContentPackages();
|
||||
IEnumerable<ContentPackage> GetAllContentPackages();
|
||||
}
|
||||
+2
@@ -6,6 +6,7 @@ using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using Barotrauma.Extensions;
|
||||
using Barotrauma.LuaCs.Data;
|
||||
using FluentResults;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services;
|
||||
|
||||
@@ -20,4 +21,5 @@ public interface IPackageManagementService : IReusableService
|
||||
public FluentResults.Result UnloadAllPackages();
|
||||
public ImmutableArray<ContentPackage> GetAllLoadedPackages();
|
||||
public bool IsPackageRunning(ContentPackage package);
|
||||
public ImmutableArray<ContentPackage> GetLoadedAssemblyPackages();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user