- 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
@@ -3,5 +3,6 @@
<Settings> <Settings>
<Setting Name="HideUserNamesInLogs" Type="bool" Value="true"/> <Setting Name="HideUserNamesInLogs" Type="bool" Value="true"/>
<Setting Name="IsCsEnabled" Type="bool" Value="false" AllowChangesWhileExecuting="false"/> <Setting Name="IsCsEnabled" Type="bool" Value="false" AllowChangesWhileExecuting="false"/>
<Setting Name="UseCaching" Type="bool" Value="true" AllowChangesWhileExecuting="false"/>
</Settings> </Settings>
</Configuration> </Configuration>
@@ -3,14 +3,16 @@
<LuaCsForBarotrauma.SettingsMenu.ModControlsButton>Mod Controls Settings</LuaCsForBarotrauma.SettingsMenu.ModControlsButton> <LuaCsForBarotrauma.SettingsMenu.ModControlsButton>Mod Controls Settings</LuaCsForBarotrauma.SettingsMenu.ModControlsButton>
<LuaCsForBarotrauma.SettingsMenu.ModGameplayButton>Mod Gameplay Settings</LuaCsForBarotrauma.SettingsMenu.ModGameplayButton> <LuaCsForBarotrauma.SettingsMenu.ModGameplayButton>Mod Gameplay Settings</LuaCsForBarotrauma.SettingsMenu.ModGameplayButton>
<!-- Settings --> <!-- Settings -->
<LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayName>Suppress GUI Popup on Error</LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayName> <!-- Is Cs Enabled-->
<LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayCategory>General</LuaCsForBarotrauma.DisableErrorGUIOverlay.DisplayCategory>
<LuaCsForBarotrauma.IsCsEnabled.DisplayName>Are C# Mods Allowed</LuaCsForBarotrauma.IsCsEnabled.DisplayName> <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.Tooltip>Should unsandboxed scripts and dlls be allowed to run.</LuaCsForBarotrauma.IsCsEnabled.Tooltip>
<LuaCsForBarotrauma.IsCsEnabled.DisplayCategory>General</LuaCsForBarotrauma.IsCsEnabled.DisplayCategory> <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.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.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> </infotexts>
@@ -106,6 +106,12 @@ namespace Barotrauma
} }
private ISettingBase<bool> _hideUserNamesInLogs; private ISettingBase<bool> _hideUserNamesInLogs;
public bool UseCaching
{
get => _useCaching?.Value ?? true;
}
private ISettingBase<bool> _useCaching;
public static ContentPackage GetLuaCsPackage() public static ContentPackage GetLuaCsPackage()
{ {
return ContentPackageManager.EnabledPackages.Regular.FirstOrDefault(cp => cp.NameMatches(PackageId), null) 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) ConfigService.TryGetConfig<ISettingBase<bool>>(luaCsPackage, "HideUserNamesInLogs", out var val4)
? val4 ? val4
: null; : null;
_useCaching =
ConfigService.TryGetConfig<ISettingBase<bool>>(luaCsPackage, "UseCaching", out var val5)
? val5
: null;
} }
private IServicesProvider SetupServicesProvider() private IServicesProvider SetupServicesProvider()
@@ -329,7 +339,6 @@ namespace Barotrauma
Logger.LogResults(PackageManagementService.LoadPackagesInfo(GetEnabledPackagesList())); Logger.LogResults(PackageManagementService.LoadPackagesInfo(GetEnabledPackagesList()));
Logger.LogResults(ConfigService.LoadSavedConfigsValues()); Logger.LogResults(ConfigService.LoadSavedConfigsValues());
LoadLuaCsConfig(); LoadLuaCsConfig();
} }
CurrentRunState = RunState.LoadedNoExec; CurrentRunState = RunState.LoadedNoExec;
@@ -14,6 +14,7 @@ public sealed class LuaCsInfoProvider : ILuaCsInfoProvider
public bool IsDisposed => false; public bool IsDisposed => false;
public bool IsCsEnabled => LuaCsSetup.Instance.IsCsEnabled; public bool IsCsEnabled => LuaCsSetup.Instance.IsCsEnabled;
public bool HideUserNamesInLogs => LuaCsSetup.Instance.HideUserNamesInLogs; public bool HideUserNamesInLogs => LuaCsSetup.Instance.HideUserNamesInLogs;
public bool UseCaching => LuaCsSetup.Instance.UseCaching;
public RunState CurrentRunState => LuaCsSetup.Instance.CurrentRunState; public RunState CurrentRunState => LuaCsSetup.Instance.CurrentRunState;
public ContentPackage LuaCsForBarotraumaPackage public ContentPackage LuaCsForBarotraumaPackage
{ {
@@ -52,6 +52,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
private readonly IPluginManagementService _pluginManagementService; private readonly IPluginManagementService _pluginManagementService;
private readonly INetworkingService _networkingService; private readonly INetworkingService _networkingService;
private readonly IConsoleCommandsService _commandsService; private readonly IConsoleCommandsService _commandsService;
private readonly ILuaCsInfoProvider _luaCsInfoProvider;
//private readonly ILuaCsUtility _luaCsUtility; //private readonly ILuaCsUtility _luaCsUtility;
public LuaScriptManagementService( public LuaScriptManagementService(
@@ -67,8 +68,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
IEventService eventService, IEventService eventService,
//ILuaCsUtility luaCsUtility, //ILuaCsUtility luaCsUtility,
ILuaCsTimer luaCsTimer, ILuaCsTimer luaCsTimer,
IConsoleCommandsService commandsService IConsoleCommandsService commandsService,
) ILuaCsInfoProvider luaCsInfoProvider)
{ {
_luaScriptLoader = loader; _luaScriptLoader = loader;
_userDataService = userDataService; _userDataService = userDataService;
@@ -82,6 +83,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
_luaGame = luaGame; _luaGame = luaGame;
_eventService = eventService; _eventService = eventService;
_commandsService = commandsService; _commandsService = commandsService;
_luaCsInfoProvider = luaCsInfoProvider;
_luaCsTimer = luaCsTimer; _luaCsTimer = luaCsTimer;
RegisterLuaEvents(); RegisterLuaEvents();
@@ -164,11 +166,23 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
public bool IsDisposed { get; private set; } public bool IsDisposed { get; private set; }
public void SetCachingPolicy(bool useCaching)
{
_luaScriptLoader?.SetCachingPolicy(useCaching);
}
public async Task<FluentResults.Result> LoadScriptResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourcesInfo) 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. // Do any exception checks you can before acquiring a lock to avoid needlessly holding up resources.
if (resourcesInfo.IsDefaultOrEmpty) if (resourcesInfo.IsDefaultOrEmpty)
{
ThrowHelper.ThrowArgumentNullException($"{nameof(LoadScriptResourcesAsync)}: The parameter is empty!"); ThrowHelper.ThrowArgumentNullException($"{nameof(LoadScriptResourcesAsync)}: The parameter is empty!");
}
// Acquire a lock: // Acquire a lock:
// Reader = Allow parallel operations (try to avoid nesting acquiring the lock when possible) // 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. // 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. // Automatically converted to a Task<T> when 'async' is in the method declaration.
if (cacheRes.IsFailed) if (cacheRes.IsFailed)
{
return cacheRes.ToResult(); return cacheRes.ToResult();
}
return new FluentResults.Result().WithReasons(cacheRes.Value.SelectMany(cr => cr.Item2.Reasons)); return new FluentResults.Result().WithReasons(cacheRes.Value.SelectMany(cr => cr.Item2.Reasons));
} }
@@ -325,6 +341,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
{ {
_loggerService.LogMessage($"[Lua] {msg}"); _loggerService.LogMessage($"[Lua] {msg}");
}; };
SetCachingPolicy(_luaCsInfoProvider.UseCaching);
_script.Options.ScriptLoader = _luaScriptLoader; _script.Options.ScriptLoader = _luaScriptLoader;
_script.Options.CheckThreadAccess = false; _script.Options.CheckThreadAccess = false;
@@ -486,6 +504,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService,
} }
_resourcesInfo.Clear(); _resourcesInfo.Clear();
_luaScriptLoader.ClearCaches();
return FluentResults.Result.Ok(); return FluentResults.Result.Ok();
} }
@@ -15,6 +15,11 @@ public interface ILuaCsInfoProvider : IService
/// </summary> /// </summary>
public bool HideUserNamesInLogs { get; } public bool HideUserNamesInLogs { get; }
/// <summary>
/// Whether file system caching is enabled.
/// </summary>
public bool UseCaching { get; }
/// <summary> /// <summary>
/// The current state of the Execution State Machine. /// The current state of the Execution State Machine.
/// </summary> /// </summary>
@@ -21,6 +21,12 @@ public interface ILuaScriptManagementService : IReusableService
FluentResults.Result<DynValue> DoString(string code); FluentResults.Result<DynValue> DoString(string code);
DynValue? CallFunctionSafe(object luaFunction, params object[] args); 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> /// <summary>
/// Parses and loads script sources (code) into a memory cache without executing it. /// Parses and loads script sources (code) into a memory cache without executing it.
/// </summary> /// </summary>
@@ -9,5 +9,10 @@ namespace Barotrauma.LuaCs;
public interface ILuaScriptLoader : IService, IScriptLoader, ISafeStorageValidation public interface ILuaScriptLoader : IService, IScriptLoader, ISafeStorageValidation
{ {
void ClearCaches(); 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); Task<Result<ImmutableArray<(ContentPath Path, Result<string>)>>> CacheResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourceInfos);
} }
@@ -56,6 +56,20 @@ namespace Barotrauma.LuaCs
_storageService?.PurgeCache(); _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) public async Task<Result<ImmutableArray<(ContentPath Path, Result<string>)>>> CacheResourcesAsync(ImmutableArray<ILuaScriptResourceInfo> resourceInfos)
{ {
IService.CheckDisposed(this); IService.CheckDisposed(this);