From 771e73a7980f6f291636b31046808a2ebec8378f Mon Sep 17 00:00:00 2001 From: MapleWheels Date: Fri, 6 Feb 2026 14:29:25 -0500 Subject: [PATCH] - Basic Config & Settings working (read-only, changes must be made via debug halt). --- .../LuaCs/Data/ISettingTypeDef.cs | 1 + .../SharedSource/LuaCs/Data/SettingEntry.cs | 9 +- .../SettingsFactoryRegistrationProvider.cs | 55 ++++++--- .../SharedSource/LuaCs/LuaCsSetup.cs | 112 ++++++++++-------- 4 files changed, 102 insertions(+), 75 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs index 89e5b9662..766a4e02f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/ISettingTypeDef.cs @@ -86,6 +86,7 @@ public interface ISettingRangeBase : ISettingBase where T : IEquatable, /// The value type. See public interface ISettingList : ISettingBase where T : IEquatable, IConvertible { + bool TrySetValueByIndex(int index); IReadOnlyList Options { get; } IReadOnlyList StringOptions { get; } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs index 55788c8f3..24aec5f62 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingEntry.cs @@ -36,18 +36,11 @@ public class SettingEntry : SettingBase, ISettingBase, INetworkSyncEntity try { Value = (T)Convert.ChangeType(ConfigInfo.Element.GetAttributeString("Value", null), typeof(T)); + DefaultValue = Value; } catch (Exception e) when (e is InvalidCastException or ArgumentNullException) { Value = default(T); - } - - try - { - DefaultValue = (T)Convert.ChangeType(ConfigInfo.Element.GetAttributeString("Value", null), typeof(T)); - } - catch (Exception e) when (e is InvalidCastException or ArgumentNullException) - { DefaultValue = default(T); } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingsFactoryRegistrationProvider.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingsFactoryRegistrationProvider.cs index 35bf2b73b..3ab15e4d6 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingsFactoryRegistrationProvider.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingsFactoryRegistrationProvider.cs @@ -2,6 +2,7 @@ using System.Xml.Linq; using Barotrauma.LuaCs.Data; using Barotrauma.LuaCs; +using OneOf; namespace Barotrauma.LuaCs.Data; @@ -21,33 +22,53 @@ public class SettingsEntryRegistrar : ISettingsRegistrationProvider public void RegisterTypeProviders(IConfigService configService, Func, bool> valueChangePredicate) { + // TODO: Replace this with a proper IFactory lookup pattern. // ISettingBase - RegisterSettingEntry(configService, "bool"); - RegisterSettingEntry(configService, "byte"); - RegisterSettingEntry(configService, "sbyte"); - RegisterSettingEntry(configService, "short"); - RegisterSettingEntry(configService, "ushort"); - RegisterSettingEntry(configService, "int"); - RegisterSettingEntry(configService, "uint"); - RegisterSettingEntry(configService, "long"); - RegisterSettingEntry(configService, "ulong"); - RegisterSettingEntry(configService, "string"); + RegisterSettingEntry(configService, "bool", valueChangePredicate); + RegisterSettingEntry(configService, "byte", valueChangePredicate); + RegisterSettingEntry(configService, "sbyte", valueChangePredicate); + RegisterSettingEntry(configService, "short", valueChangePredicate); + RegisterSettingEntry(configService, "ushort", valueChangePredicate); + RegisterSettingEntry(configService, "int", valueChangePredicate); + RegisterSettingEntry(configService, "uint", valueChangePredicate); + RegisterSettingEntry(configService, "long", valueChangePredicate); + RegisterSettingEntry(configService, "ulong", valueChangePredicate); + RegisterSettingEntry(configService, "string", valueChangePredicate); + // ISettingRangeBase - // ISettingList + configService.RegisterSettingTypeInitializer("rangeInt", cfgInfo => + { + return new SettingRangeInt.RangeFactory().CreateInstance(cfgInfo.Info, (val) => + IsValueChangeAllowed(cfgInfo.Info, val, valueChangePredicate)); + }); + + configService.RegisterSettingTypeInitializer("rangeFloat", cfgInfo => + { + return new SettingRangeFloat.RangeFactory().CreateInstance(cfgInfo.Info, (val) => + IsValueChangeAllowed(cfgInfo.Info, val, valueChangePredicate)); + }); + + // ISettingList : Not Implemented yet } - private void RegisterSettingEntry(IConfigService configService, string typeName) where T : IEquatable, IConvertible + private void RegisterSettingEntry(IConfigService configService, string typeName, Func, bool> valueChangePredicate) where T : IEquatable, IConvertible { configService.RegisterSettingTypeInitializer(typeName, cfgInfo => { - return new SettingEntry.Factory().CreateInstance(cfgInfo.Info, (val) => - { - return !cfgInfo.Info.Element.GetAttributeBool("ReadOnly", false) - && cfgInfo.Info.EditableStates.HasFlag(_infoProvider?.CurrentRunState ?? RunState.Running); - }); + return new SettingEntry.Factory().CreateInstance(cfgInfo.Info, (val) => + IsValueChangeAllowed(cfgInfo.Info, val, valueChangePredicate)); }); } + private bool IsValueChangeAllowed(IConfigInfo info, OneOf newValue, + Func, bool> valueChangePredicate) + { + return !info.Element.GetAttributeBool("ReadOnly", false) + || !info.EditableStates.HasFlag(_infoProvider?.CurrentRunState ?? RunState.Running) + || valueChangePredicate is null + || valueChangePredicate.Invoke(newValue); + } + public void Dispose() { if (!ModUtils.Threading.CheckIfClearAndSetBool(ref _isDisposed)) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index 19ea7031e..b4379a95b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -33,28 +33,10 @@ namespace Barotrauma { // == startup _servicesProvider = SetupServicesProvider(); - if (!ValidateLuaCsContent()) - { - Logger.LogError($"{nameof(LuaCsSetup)}: ModConfig.xml missing. Unable to continue."); - throw new ApplicationException($"{nameof(LuaCsSetup)}: Lua's ModConfig.xml is missing. Unable to continue."); - } _runStateMachine = SetupStateMachine(); SubscribeToLuaCsEvents(); } - private bool ValidateLuaCsContent() - { -#if DEBUG - // TODO: we just wanna boot for now - return true; -#endif - // check if /Content/ModConfig.xml exists - // if not, try to copy missing files from the Local Mods folder - // if not, try to copy missing files from the Workshop Mods folder - // if that fails, throw an error and exit. - throw new NotImplementedException(); - } - private void SubscribeToLuaCsEvents() { EventService.Subscribe(this); // game state hook in @@ -102,7 +84,7 @@ namespace Barotrauma internal set => _isCsEnabled?.TrySetValue(value); } - private SettingEntry _isCsEnabled; + private ISettingBase _isCsEnabled; /// /// Whether the popup error GUI should be hidden/suppressed. @@ -112,7 +94,7 @@ namespace Barotrauma get => _disableErrorGUIOverlay?.Value ?? false; internal set => _disableErrorGUIOverlay?.TrySetValue(value); } - private SettingEntry _disableErrorGUIOverlay; + private ISettingBase _disableErrorGUIOverlay; /// /// Whether usernames are anonymized or show in logs. @@ -122,7 +104,7 @@ namespace Barotrauma get => _hideUserNamesInLogs?.Value ?? false; internal set => _hideUserNamesInLogs?.TrySetValue(value); } - private SettingEntry _hideUserNamesInLogs; + private ISettingBase _hideUserNamesInLogs; /// /// The SteamId of the Workshop LuaCs CPackage in use, if available. @@ -132,7 +114,7 @@ namespace Barotrauma get => _luaForBarotraumaSteamId?.Value ?? 0; internal set => _luaForBarotraumaSteamId?.TrySetValue(value); } - private SettingEntry _luaForBarotraumaSteamId; + private ISettingBase _luaForBarotraumaSteamId; /// /// TODO: @evilfactory@users.noreply.github.com @@ -142,7 +124,7 @@ namespace Barotrauma get => _restrictMessageSize?.Value ?? false; internal set => _restrictMessageSize?.TrySetValue(value); } - private SettingEntry _restrictMessageSize; + private ISettingBase _restrictMessageSize; /// /// The local save path for all local data storage for mods. @@ -152,28 +134,45 @@ namespace Barotrauma get => _localDataSavePath?.Value ?? Path.Combine(Directory.GetCurrentDirectory(), "/Data/Mods"); internal set => _localDataSavePath?.TrySetValue(value); } - private SettingEntry _localDataSavePath; + private ISettingBase _localDataSavePath; void LoadLuaCsConfig() { - _isCsEnabled = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "IsCsEnabled", out var val1) ? val1 - : throw new NullReferenceException($"{nameof(IsCsEnabled)} cannot be loaded."); - _disableErrorGUIOverlay = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "DisableErrorGUIOverlay", out var val3) ? val3 - : throw new NullReferenceException($"{nameof(DisableErrorGUIOverlay)} cannot be loaded."); - _hideUserNamesInLogs = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "HideUserNamesInLogs", out var val4) ? val4 - : throw new NullReferenceException($"{nameof(HideUserNamesInLogs)} cannot be loaded."); - _luaForBarotraumaSteamId = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "LuaForBarotraumaSteamId", out var val5) ? val5 - : throw new NullReferenceException($"{nameof(LuaForBarotraumaSteamId)} cannot be loaded."); - _restrictMessageSize = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "RestrictMessageSize", out var val7) ? val7 - : throw new NullReferenceException($"{nameof(RestrictMessageSize)} cannot be loaded."); - _localDataSavePath = ConfigService.TryGetConfig>(ContentPackageManager.VanillaCorePackage, "LocalDataSavePath", out var val8) ? val8 - : throw new NullReferenceException($"{nameof(LocalDataSavePath)} cannot be loaded."); + var luaCsPackage = ContentPackageManager.EnabledPackages.Regular.FirstOrDefault(cp => cp.NameMatches("LuaCsForBarotrauma"), null) + ?? ContentPackageManager.LocalPackages.FirstOrDefault(cp => cp.NameMatches("LuaCsForBarotrauma")) + ?? ContentPackageManager.WorkshopPackages.FirstOrDefault(cp => cp.NameMatches("LuaCsForBarotrauma")); + + _isCsEnabled = + ConfigService.TryGetConfig>(luaCsPackage, "IsCsEnabled", out var val1) + ? val1 + : null; + _disableErrorGUIOverlay = + ConfigService.TryGetConfig>(luaCsPackage, "DisableErrorGUIOverlay", out var val3) + ? val3 + : null; + _hideUserNamesInLogs = + ConfigService.TryGetConfig>(luaCsPackage, "HideUserNamesInLogs", out var val4) + ? val4 + : null; + _luaForBarotraumaSteamId = + ConfigService.TryGetConfig>(luaCsPackage, "LuaForBarotraumaSteamId", out var val5) + ? val5 + : null; + _restrictMessageSize = + ConfigService.TryGetConfig>(luaCsPackage, "RestrictMessageSize", out var val7) + ? val7 + : null; + _localDataSavePath = + ConfigService.TryGetConfig>(luaCsPackage, "LocalDataSavePath", out var val8) + ? val8 + : null; } private IServicesProvider SetupServicesProvider() { var servicesProvider = new ServicesProvider(); + // Base Service servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Transient); @@ -182,9 +181,22 @@ namespace Barotrauma servicesProvider.RegisterServiceResolver(factory => factory.GetInstance() as ILuaCsHook); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); - servicesProvider.RegisterServiceType(ServiceLifetime.Transient); servicesProvider.RegisterServiceResolver(factory => factory.GetInstance()); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); + servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); + // TODO: INetworkingService + + // Extension/Sub Services + servicesProvider.RegisterServiceType(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType, ModConfigFileParserService>(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType, ModConfigFileParserService>(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType, ModConfigFileParserService>(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType, SettingsFileParserService>(ServiceLifetime.Transient); + servicesProvider.RegisterServiceType, SettingsFileParserService>(ServiceLifetime.Transient); + + // All Lua Extras servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); @@ -193,20 +205,13 @@ namespace Barotrauma servicesProvider.RegisterServiceType(ServiceLifetime.Transient); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); - servicesProvider.RegisterServiceType(ServiceLifetime.Transient); - // TODO: INetworkingService - servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); - servicesProvider.RegisterServiceType(ServiceLifetime.Transient); - servicesProvider.RegisterServiceType, ModConfigFileParserService>(ServiceLifetime.Transient); - servicesProvider.RegisterServiceType, ModConfigFileParserService>(ServiceLifetime.Transient); - servicesProvider.RegisterServiceType, ModConfigFileParserService>(ServiceLifetime.Transient); - servicesProvider.RegisterServiceType, SettingsFileParserService>(ServiceLifetime.Transient); - servicesProvider.RegisterServiceType, SettingsFileParserService>(ServiceLifetime.Transient); + // service config data servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); servicesProvider.RegisterServiceType(ServiceLifetime.Singleton); + // gen IL servicesProvider.Compile(); return servicesProvider; @@ -291,6 +296,7 @@ namespace Barotrauma Logger.LogResults(PackageManagementService.UnloadAllPackages()); } + ConfigService.Reset(); LuaScriptManagementService.Reset(); PackageManagementService.Reset(); EventService.Reset(); @@ -309,9 +315,12 @@ namespace Barotrauma if (!PackageManagementService.IsAnyPackageLoaded()) { + foreach (var registrationProvider in _servicesProvider.GetAllServices()) + { + registrationProvider.RegisterTypeProviders(ConfigService, null); + } Logger.LogResults(PackageManagementService.LoadPackagesInfo(ContentPackageManager.EnabledPackages.All.ToImmutableArray())); - // TODO: Implement full xml content necessary for this to work. - //LoadLuaCsConfig(); + LoadLuaCsConfig(); } CurrentRunState = RunState.LoadedNoExec; @@ -321,9 +330,12 @@ namespace Barotrauma { if (!PackageManagementService.IsAnyPackageLoaded()) { + foreach (var registrationProvider in _servicesProvider.GetAllServices()) + { + registrationProvider.RegisterTypeProviders(ConfigService, null); + } Logger.LogResults(PackageManagementService.LoadPackagesInfo(ContentPackageManager.EnabledPackages.All.ToImmutableArray())); - // TODO: Enable later - //LoadLuaCsConfig(); + LoadLuaCsConfig(); } if (!PackageManagementService.IsAnyPackageRunning())