- Added setting to disable lua scripts caching in the storage service for scenarios that use extern editors.

This commit is contained in:
MapleWheels
2026-04-02 23:29:21 -04:00
parent 4f2da55a8e
commit 413cc3ed4c
9 changed files with 70 additions and 8 deletions

View File

@@ -3,5 +3,6 @@
<Settings>
<Setting Name="HideUserNamesInLogs" Type="bool" Value="true"/>
<Setting Name="IsCsEnabled" Type="bool" Value="false" AllowChangesWhileExecuting="false"/>
<Setting Name="UseCaching" Type="bool" Value="true" AllowChangesWhileExecuting="false"/>
</Settings>
</Configuration>

View File

@@ -3,14 +3,16 @@
<LuaCsForBarotrauma.SettingsMenu.ModControlsButton>Mod Controls Settings</LuaCsForBarotrauma.SettingsMenu.ModControlsButton>
<LuaCsForBarotrauma.SettingsMenu.ModGameplayButton>Mod Gameplay Settings</LuaCsForBarotrauma.SettingsMenu.ModGameplayButton>
<!-- Settings -->
<LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayName>Suppress GUI Popup on Error</LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayName>
<LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayCategory>General</LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayCategory>
<!-- Is Cs Enabled-->
<LuaCsForBarotrauma.IsCsEnabled.DisplayName>Are C# Mods Allowed</LuaCsForBarotrauma.IsCsEnabled.DisplayName>
<LuaCsForBarotrauma.IsCsEnabled.Tooltip>Should unsandboxed scripts and dlls be allowed to run.</LuaCsForBarotrauma.IsCsEnabled.Tooltip>
<LuaCsForBarotrauma.IsCsEnabled.DisplayCategory>General</LuaCsForBarotrauma.IsCsEnabled.DisplayCategory>
<!-- Use Caching -->
<LuaCsForBarotrauma.UseCaching.DisplayName>Use Pre-Caching</LuaCsForBarotrauma.UseCaching.DisplayName>
<LuaCsForBarotrauma.UseCaching.Tooltip>Should mod files be preloaded to speed up loading. Should only be turned off if you have mods that have issues with this.</LuaCsForBarotrauma.UseCaching.Tooltip>
<LuaCsForBarotrauma.UseCaching.DisplayCategory>General</LuaCsForBarotrauma.UseCaching.DisplayCategory>
<!-- Hide Usernames In Logs-->
<LuaCsForBarotrauma.HideUserNamesInLogs.DisplayName>Hide Local OS Account Name In Logs</LuaCsForBarotrauma.HideUserNamesInLogs.DisplayName>
<LuaCsForBarotrauma.HideUserNamesInLogs.Tooltip>If true, will replace your OS account name with 'USERNAME' in log files' paths.</LuaCsForBarotrauma.HideUserNamesInLogs.Tooltip>
<LuaCsForBarotrauma.HideUserNamesInLogs.DisplayCategory>General</LuaCsForBarotrauma.HideUserNamesInLogs.DisplayCategory>
<LuaCsForBarotrauma.RestrictMessageSize.DisplayName>Limit Network Message Size</LuaCsForBarotrauma.RestrictMessageSize.DisplayName>
<LuaCsForBarotrauma.RestrictMessageSize.Tooltip>Limits the maximum network message size to avoid buffer size issues.</LuaCsForBarotrauma.RestrictMessageSize.Tooltip>
<LuaCsForBarotrauma.RestrictMessageSize.DisplayCategory>Networking</LuaCsForBarotrauma.RestrictMessageSize.DisplayCategory>
</infotexts>

View File

@@ -106,6 +106,12 @@ namespace Barotrauma
}
private ISettingBase<bool> _hideUserNamesInLogs;
public bool UseCaching
{
get => _useCaching?.Value ?? true;
}
private ISettingBase<bool> _useCaching;
public static ContentPackage GetLuaCsPackage()
{
return ContentPackageManager.EnabledPackages.Regular.FirstOrDefault(cp => cp.NameMatches(PackageId), null)
@@ -125,6 +131,10 @@ namespace Barotrauma
ConfigService.TryGetConfig<ISettingBase<bool>>(luaCsPackage, "HideUserNamesInLogs", out var val4)
? val4
: null;
_useCaching =
ConfigService.TryGetConfig<ISettingBase<bool>>(luaCsPackage, "UseCaching", out var val5)
? val5
: null;
}
private IServicesProvider SetupServicesProvider()
@@ -329,7 +339,6 @@ namespace Barotrauma
Logger.LogResults(PackageManagementService.LoadPackagesInfo(GetEnabledPackagesList()));
Logger.LogResults(ConfigService.LoadSavedConfigsValues());
LoadLuaCsConfig();
}
CurrentRunState = RunState.LoadedNoExec;

View File

@@ -14,6 +14,7 @@ public sealed class LuaCsInfoProvider : ILuaCsInfoProvider
public bool IsDisposed => false;
public bool IsCsEnabled => LuaCsSetup.Instance.IsCsEnabled;
public bool HideUserNamesInLogs => LuaCsSetup.Instance.HideUserNamesInLogs;
public bool UseCaching => LuaCsSetup.Instance.UseCaching;
public RunState CurrentRunState => LuaCsSetup.Instance.CurrentRunState;
public ContentPackage LuaCsForBarotraumaPackage
{

View File

@@ -52,6 +52,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
private readonly IPluginManagementService _pluginManagementService;
private readonly INetworkingService _networkingService;
private readonly IConsoleCommandsService _commandsService;
private readonly ILuaCsInfoProvider _luaCsInfoProvider;
//private readonly ILuaCsUtility _luaCsUtility;
public LuaScriptManagementService(
@@ -67,8 +68,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
IEventService eventService,
//ILuaCsUtility luaCsUtility,
ILuaCsTimer luaCsTimer,
IConsoleCommandsService commandsService
)
IConsoleCommandsService commandsService,
ILuaCsInfoProvider luaCsInfoProvider)
{
_luaScriptLoader = loader;
_userDataService = userDataService;
@@ -82,6 +83,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
_luaGame = luaGame;
_eventService = eventService;
_commandsService = commandsService;
_luaCsInfoProvider = luaCsInfoProvider;
_luaCsTimer = luaCsTimer;
RegisterLuaEvents();
@@ -164,11 +166,23 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
public bool IsDisposed { get; private set; }
public void SetCachingPolicy(bool useCaching)
{
_luaScriptLoader?.SetCachingPolicy(useCaching);
}
public async Task<FluentResults.Result> LoadScriptResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourcesInfo)
{
if (!_luaCsInfoProvider.UseCaching)
{
return FluentResults.Result.Ok();
}
// 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)
@@ -186,7 +200,9 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
// 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));
}
@@ -325,6 +341,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
{
_loggerService.LogMessage($"[Lua] {msg}");
};
SetCachingPolicy(_luaCsInfoProvider.UseCaching);
_script.Options.ScriptLoader = _luaScriptLoader;
_script.Options.CheckThreadAccess = false;
@@ -486,6 +504,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
}
_resourcesInfo.Clear();
_luaScriptLoader.ClearCaches();
return FluentResults.Result.Ok();
}

View File

@@ -15,6 +15,11 @@ public interface ILuaCsInfoProvider : IService
/// </summary>
public bool HideUserNamesInLogs { get; }
/// <summary>
/// Whether file system caching is enabled.
/// </summary>
public bool UseCaching { get; }
/// <summary>
/// The current state of the Execution State Machine.
/// </summary>

View File

@@ -21,6 +21,12 @@ public interface ILuaScriptManagementService : IReusableService
FluentResults.Result<DynValue> DoString(string code);
DynValue? CallFunctionSafe(object luaFunction, params object[] args);
/// <summary>
/// Whether to enable/disable the file system caching for lua.
/// </summary>
/// <param name="useCaching"></param>
void SetCachingPolicy(bool useCaching);
/// <summary>
/// Parses and loads script sources (code) into a memory cache without executing it.
/// </summary>

View File

@@ -9,5 +9,10 @@ namespace Barotrauma.LuaCs;
public interface ILuaScriptLoader : IService, IScriptLoader, ISafeStorageValidation
{
void ClearCaches();
/// <summary>
/// Whether caching is enabled/disabled.
/// </summary>
/// <param name="useCaching"></param>
void SetCachingPolicy(bool useCaching);
Task<Result<ImmutableArray<(ContentPath Path, Result<string>)>>> CacheResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourceInfos);
}

View File

@@ -56,6 +56,20 @@ namespace Barotrauma.LuaCs
_storageService?.PurgeCache();
}
public void SetCachingPolicy(bool useCaching)
{
if (_storageService is null)
{
return;
}
if (!useCaching)
{
_storageService.PurgeCache();
}
_storageService.UseCaching = useCaching;
}
public async Task<Result<ImmutableArray<(ContentPath Path, Result<string>)>>> CacheResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourceInfos)
{
IService.CheckDisposed(this);