- Added float, double settingentry types. Adjusted display formatting.

- Added menu refresh on Apply button.
- Fixed package name resolution for invalid chars.
- Added helper console command to get xml-safe name for use in localization files.
- Made error messages for bad settings xml more descriptive.
- Modified GUISlider.
- Converted HarmonyEventPatchesService to a System.
- Fixed package name resolution for incompatible names in Xml and files, in many places.
- Fixed base textbox implementation for settings.
This commit is contained in:
MapleWheels
2026-03-05 15:45:00 -05:00
parent f38a7bd574
commit 1fe68aa861
16 changed files with 203 additions and 27 deletions
@@ -66,7 +66,8 @@ public abstract class SettingBase : ISettingBase
{
new GUITextBox(new RectTransform(relativeSize, layoutGroup.RectTransform), font: GUIStyle.SmallFont)
{
OnEnterPressed = (box, txt) =>
Text = GetStringValue(),
OnTextChangedDelegate = (box, txt) =>
{
onSerializedValue?.Invoke(txt);
return true;
@@ -1,7 +1,9 @@
using System;
using System.Globalization;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Microsoft.Toolkit.Diagnostics;
using Microsoft.Xna.Framework;
using OneOf;
namespace Barotrauma.LuaCs.Data;
@@ -44,6 +46,19 @@ public class SettingRangeFloat : SettingRangeBase<float>
}
return base.TrySetValue(value);
}
#if CLIENT
public override void AddDisplayComponent(GUILayoutGroup layoutGroup, Vector2 relativeSize, Action<string> onSerializedValue)
{
GUIUtil.Slider(layoutGroup, new Vector2(MinValue, MaxValue), IncrementalSteps, labelFunc: val =>
{
return val.ToString("G4", CultureInfo.InvariantCulture);
}, Value, setter: val =>
{
onSerializedValue?.Invoke(val.ToString());
}, TextManager.Get(this.GetDisplayInfo().Tooltip), relativeSize);
}
#endif
}
public class SettingRangeInt : SettingRangeBase<int>
@@ -73,4 +88,17 @@ public class SettingRangeInt : SettingRangeBase<int>
}
return base.TrySetValue(value);
}
#if CLIENT
public override void AddDisplayComponent(GUILayoutGroup layoutGroup, Vector2 relativeSize, Action<string> onSerializedValue)
{
GUIUtil.Slider(layoutGroup, new Vector2(MinValue, MaxValue), IncrementalSteps, labelFunc: val =>
{
return ((int)val).ToString();
}, Value, setter: val =>
{
onSerializedValue?.Invoke(((int)val).ToString());
}, TextManager.Get(this.GetDisplayInfo().Tooltip), relativeSize);
}
#endif
}
@@ -34,6 +34,9 @@ public class SettingsEntryRegistrar : ISettingsRegistrationProvider
RegisterSettingEntry<long>(configService, "long", valueChangePredicate);
RegisterSettingEntry<ulong>(configService, "ulong", valueChangePredicate);
RegisterSettingEntry<string>(configService, "string", valueChangePredicate);
RegisterSettingEntry<float>(configService, "float", valueChangePredicate);
RegisterSettingEntry<float>(configService, "single", valueChangePredicate);
RegisterSettingEntry<double>(configService, "double", valueChangePredicate);
// ISettingRangeBase<T>
configService.RegisterSettingTypeInitializer("rangeInt", cfgInfo =>
@@ -9,6 +9,7 @@ using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using AssemblyLoader = Barotrauma.LuaCs.AssemblyLoader;
[assembly: InternalsVisibleTo("ImpromptuInterfaceDynamicAssembly")]
@@ -18,13 +19,14 @@ namespace Barotrauma
internal delegate void LuaCsMessageLogger(string message);
internal delegate void LuaCsErrorHandler(Exception ex, LuaCsMessageOrigin origin);
internal delegate void LuaCsExceptionHandler(Exception ex, LuaCsMessageOrigin origin);
partial class LuaCsSetup : IDisposable, IEventScreenSelected, IEventEnabledPackageListChanged,
IEventReloadAllPackages
{
private static LuaCsSetup _luaCsSetup;
public static LuaCsSetup Instance => _luaCsSetup ??= new LuaCsSetup();
private LuaCsSetup()
{
if (_luaCsSetup != null)
@@ -35,7 +37,6 @@ namespace Barotrauma
// == startup
_servicesProvider = SetupServicesProvider();
_runStateMachine = SetupStateMachine();
_servicesProvider.GetService<HarmonyEventPatchesService>();
SubscribeToLuaCsEvents();
}
@@ -8,6 +8,7 @@ using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Events;
@@ -447,12 +448,20 @@ public sealed partial class ConfigService : IConfigService
using var lck = _operationLock.AcquireReaderLock().ConfigureAwait(false).GetAwaiter().GetResult();
IService.CheckDisposed(this);
if (_storageService.LoadLocalXml(setting.OwnerPackage, SaveDataFileName) is not { } saveFileResult
|| saveFileResult is { IsFailed: true })
if (_storageService.LoadLocalXml(setting.OwnerPackage, SaveDataFileName) is not { } saveFileResult)
{
return FluentResults.Result.Fail(
$"{nameof(LoadSavedValueForConfig)}: Could not open save file for setting [{setting.OwnerPackage.Name}.{setting.InternalName}]");
}
if (saveFileResult is { IsFailed: true })
{
#if DEBUG
_logger.LogResults(saveFileResult.ToResult());
#endif
return FluentResults.Result.Fail(
$"{nameof(LoadSavedValueForConfig)}: Could not open save file for setting [{setting.OwnerPackage.Name}.{setting.InternalName}]");
}
if (saveFileResult.Value.Root is not {} rootElement
|| !string.Equals(rootElement.Name.LocalName, "Configuration", StringComparison.InvariantCultureIgnoreCase))
@@ -460,8 +469,8 @@ public sealed partial class ConfigService : IConfigService
return FluentResults.Result.Fail($"{nameof(LoadSavedValueForConfig)}: Root invalid for setting [{setting.OwnerPackage.Name}.{setting.InternalName}]");
}
if (rootElement.GetChildElement(setting.OwnerPackage.Name, StringComparison.InvariantCulture)
?.GetChildElement(setting.InternalName, StringComparison.InvariantCulture) is not {} cfgValueElement)
if (rootElement.GetChildElement(XmlConvert.EncodeLocalName(setting.OwnerPackage.Name.Trim()), StringComparison.InvariantCultureIgnoreCase)
?.GetChildElement(setting.InternalName, StringComparison.InvariantCultureIgnoreCase) is not {} cfgValueElement)
{
return FluentResults.Result.Fail($"{nameof(LoadSavedValueForConfig)}: Could not find saved value for setting:[{setting.OwnerPackage.Name}.{setting.InternalName}]");
}
@@ -547,7 +556,7 @@ public sealed partial class ConfigService : IConfigService
return FluentResults.Result.Fail($"{nameof(SaveConfigValue)}: Bad save file format for setting: [{setting.OwnerPackage.Name}.{setting.InternalName}]");
}
XElement currentTarget = GetOrAddElement(cpCfgValues.Root, setting.OwnerPackage.Name, name => new XElement(name));
XElement currentTarget = GetOrAddElement(cpCfgValues.Root, XmlConvert.EncodeLocalName(setting.OwnerPackage.Name.Trim()), name => new XElement(name));
currentTarget = GetOrAddElement(currentTarget, setting.InternalName, name => new XElement(name));
var ret = setting.GetSerializableValue().Match(str =>
@@ -13,9 +13,15 @@ using static Barotrauma.ContentPackageManager;
namespace Barotrauma.LuaCs;
[HarmonyPatch]
internal class HarmonyEventPatchesService : IService
internal class HarmonyEventPatchesService : ISystem
{
public bool IsDisposed { get; private set; }
public FluentResults.Result Reset()
{
Unpatch();
Patch();
return FluentResults.Result.Ok();
}
private static IEventService _eventService;
private static ILoggerService _loggerService;
@@ -26,12 +32,23 @@ internal class HarmonyEventPatchesService : IService
_eventService = eventService;
_loggerService = loggerService;
Harmony = new Harmony("LuaCsForBarotrauma.Events");
Harmony.PatchAll(typeof(HarmonyEventPatchesService));
Patch();
}
private void Patch()
{
this.Harmony?.PatchAll(typeof(HarmonyEventPatchesService));
#if SERVER
Harmony.PatchAll(typeof(HarmonyEventPatchesService.Patch_StartGame_End));
this.Harmony?.PatchAll(typeof(HarmonyEventPatchesService.Patch_StartGame_End));
#endif
}
private void Unpatch()
{
this.Harmony?.UnpatchSelf();
}
[HarmonyPatch(typeof(CoroutineManager), nameof(CoroutineManager.Update)), HarmonyPostfix]
public static void CoroutineManager_Update_Post()
{
@@ -308,7 +325,7 @@ internal class HarmonyEventPatchesService : IService
public void Dispose()
{
IsDisposed = true;
Harmony.UnpatchSelf();
this.Harmony?.UnpatchSelf();
}
#if SERVER
@@ -5,6 +5,7 @@ using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Xml;
using Barotrauma.Extensions;
using Barotrauma.LuaCs.Data;
using FluentResults;
@@ -20,6 +21,7 @@ public sealed class PackageManagementService : IPackageManagementService
private IConfigService _configService;
private ILuaScriptManagementService _luaScriptManagementService;
private IPluginManagementService _pluginManagementService;
private IConsoleCommandsService _commandsService;
#if CLIENT
private IUIStylesService _uiStylesService;
#endif
@@ -44,6 +46,7 @@ public sealed class PackageManagementService : IPackageManagementService
ILuaScriptManagementService luaScriptManagementService,
IPluginManagementService pluginManagementService,
IConfigService configService,
IConsoleCommandsService commandsService,
#if CLIENT
IUIStylesService uiStylesService,
#endif
@@ -58,6 +61,31 @@ public sealed class PackageManagementService : IPackageManagementService
#if CLIENT
_uiStylesService = uiStylesService;
#endif
_commandsService = commandsService;
commandsService.RegisterCommand("pms_getxmlname",
"Gets the XML encoded name for the given package, as used in localization.",
onExecute: args =>
{
if (args.Length < 1)
{
_logger.LogError("Please specify the name of the package.");
return;
}
if (ContentPackageManager.AllPackages.FirstOrDefault(p => p.Name == args[0]) is { } pkg)
{
_logger.Log($"Package Xml Name: '{XmlConvert.EncodeLocalName(pkg.Name)}'");
return;
}
_logger.Log($"Could not find package with the name '{args[0]}'");
},
getValidArgs: () =>
{
return new[]
{
this._loadedPackages.Keys.Select(p => p.Name).ToArray()
};
});
}
public void Dispose()
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using FluentResults;
@@ -75,7 +76,7 @@ public sealed class SettingsFileParserService :
continue;
}
var packageIdent = res.path.ContentPackage!.Name;
var packageIdent = XmlConvert.EncodeLocalName(res.path.ContentPackage!.Name);
foreach (var element in settingElements)
{
@@ -108,7 +109,7 @@ public sealed class SettingsFileParserService :
};
if (!IsInfoValid(newSetting))
{
return ReturnFail($"A setting was invalid. ContentPackage: {res.path.ContentPackage}");
return ReturnFail($"A setting was invalid. ContentPackage: {res.path.ContentPackage.Name}. Name: {newSetting?.InternalName}");
}
parsedInfo.Add(newSetting);
}
@@ -128,7 +129,6 @@ public sealed class SettingsFileParserService :
return info.OwnerPackage != null
&& !info.InternalName.IsNullOrWhiteSpace()
&& !info.DataType.IsNullOrWhiteSpace()
&& !info.DataType.IsNullOrWhiteSpace()
&& info.Element != null
#if CLIENT
&& !info.DisplayName.IsNullOrWhiteSpace()
@@ -7,6 +7,7 @@ using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Barotrauma.Networking;
@@ -128,7 +129,7 @@ public class StorageService : IStorageService
try
{
var path = System.IO.Path.GetFullPath(Path.Combine(
ConfigData.LocalPackageDataPath.Replace(ConfigData.LocalDataPathRegex, package.Name).CleanUpPathCrossPlatform(),
ConfigData.LocalPackageDataPath.Replace(ConfigData.LocalDataPathRegex, XmlConvert.EncodeLocalName(package.Name)).CleanUpPathCrossPlatform(),
localFilePath.CleanUpPathCrossPlatform()));
if (!path.StartsWith(Path.GetFullPath(ConfigData.LocalDataSavePath)))
ThrowHelper.ThrowUnauthorizedAccessException($"{nameof(GetAbsolutePathForLocal)}: The local path of '{path}' is not a local path!");