Unstable 1.8.4.0

This commit is contained in:
Markus Isberg
2025-03-12 12:56:27 +00:00
parent a4c3e868e4
commit a4a3427e4e
627 changed files with 29860 additions and 10018 deletions
@@ -24,7 +24,14 @@ sealed class ConditionallyEditable : Editable
IsSwappableItem,
AllowRotating,
Attachable,
/// <summary>
/// Does the entity currently have a physics body?
/// </summary>
HasBody,
/// <summary>
/// Does the entity normally have a physics body? Can be used if a property should be enabled on a wall whose collisions have been disabled.
/// </summary>
HasBodyByDefault,
Pickable,
OnlyByStatusEffectsAndNetwork,
HasIntegratedButtons,
@@ -44,12 +51,14 @@ sealed class ConditionallyEditable : Editable
ConditionType.IsSwappableItem
=> entity is Item item && item.Prefab.SwappableItem != null,
ConditionType.AllowRotating
=> (entity is Item { body: null } item && item.Prefab.AllowRotatingInEditor)
=> (entity is Item item && (item.body == null || item.body.BodyType == FarseerPhysics.BodyType.Static) && item.Prefab.AllowRotatingInEditor)
|| (entity is Structure structure && structure.Prefab.AllowRotatingInEditor),
ConditionType.Attachable
=> GetComponent<Holdable>(entity) is Holdable { Attachable: true },
ConditionType.HasBody
=> entity is Structure { HasBody: true } or Item { body: not null },
ConditionType.HasBodyByDefault
=> entity is Structure { Prefab.Body: true } or Item { body: not null },
ConditionType.Pickable
=> entity is Item item && item.GetComponent<Pickable>() != null,
ConditionType.OnlyByStatusEffectsAndNetwork
@@ -1,11 +1,14 @@
using System;
using System;
namespace Barotrauma;
[AttributeUsage(AttributeTargets.Property)]
class Editable : Attribute
{
public int MaxLength;
/// <summary>
/// Maximum length of the value if the value is a string. Only has an effect is larger than 0.
/// </summary>
public int MaxLength = -1;
public int DecimalCount = 1;
public int MinValueInt = int.MinValue, MaxValueInt = int.MaxValue;
@@ -34,9 +37,8 @@ class Editable : Attribute
/// </summary>
public bool ReadOnly;
public Editable(int maxLength = 20)
public Editable()
{
MaxLength = maxLength;
}
public Editable(int minValue, int maxValue)
@@ -214,10 +214,10 @@ namespace Barotrauma
PropertyInfo.SetValue(parentObject, new RawLString(value));
break;
case "stringarray":
PropertyInfo.SetValue(parentObject, XMLExtensions.ParseStringArray(value));
PropertyInfo.SetValue(parentObject, ParseStringArray(value));
break;
case "identifierarray":
PropertyInfo.SetValue(parentObject, XMLExtensions.ParseIdentifierArray(value));
PropertyInfo.SetValue(parentObject, ParseIdentifierArray(value));
break;
}
}
@@ -229,6 +229,17 @@ namespace Barotrauma
return true;
}
private static string[] ParseStringArray(string stringArrayValues)
{
return string.IsNullOrEmpty(stringArrayValues) ? Array.Empty<string>() : stringArrayValues.Split(';');
}
private static Identifier[] ParseIdentifierArray(string stringArrayValues)
{
return ParseStringArray(stringArrayValues).ToIdentifiers();
}
public bool TrySetValue(object parentObject, object value)
{
if (value == null || parentObject == null || PropertyInfo == null) return false;
@@ -298,10 +309,10 @@ namespace Barotrauma
PropertyInfo.SetValue(parentObject, new RawLString((string)value));
return true;
case "stringarray":
PropertyInfo.SetValue(parentObject, XMLExtensions.ParseStringArray((string)value));
PropertyInfo.SetValue(parentObject, ParseStringArray((string)value));
return true;
case "identifierarray":
PropertyInfo.SetValue(parentObject, XMLExtensions.ParseIdentifierArray((string)value));
PropertyInfo.SetValue(parentObject, ParseIdentifierArray((string)value));
return true;
default:
DebugConsole.ThrowError($"Failed to set the value of the property \"{Name}\" of \"{parentObject}\" to {value}");
@@ -562,6 +573,11 @@ namespace Barotrauma
if (parentObject is PowerContainer powerContainer) { value = powerContainer.Charge; return true; }
}
break;
case nameof(Repairable.StressDeteriorationMultiplier):
{
if (parentObject is Repairable repairable) { value = repairable.StressDeteriorationMultiplier; return true; }
}
break;
case nameof(PowerContainer.ChargePercentage):
{
if (parentObject is PowerContainer powerContainer) { value = powerContainer.ChargePercentage; return true; }
@@ -572,6 +588,11 @@ namespace Barotrauma
if (parentObject is PowerContainer powerContainer) { value = powerContainer.RechargeRatio; return true; }
}
break;
case nameof(ItemContainer.ContainedNonBrokenItemCount):
{
if (parentObject is ItemContainer itemContainer) { value = itemContainer.ContainedNonBrokenItemCount; return true; }
}
break;
case nameof(Reactor.AvailableFuel):
{ if (parentObject is Reactor reactor) { value = reactor.AvailableFuel; return true; } }
break;
@@ -611,6 +632,15 @@ namespace Barotrauma
case nameof(Item.Condition):
{ if (parentObject is Item item) { value = item.Condition; return true; } }
break;
case nameof(Item.ConditionPercentage):
{ if (parentObject is Item item) { value = item.ConditionPercentage; return true; } }
break;
case nameof(Item.SightRange):
{ if (parentObject is Item item) { value = item.SightRange; return true; } }
break;
case nameof(Item.SoundRange):
{ if (parentObject is Item item) { value = item.SoundRange; return true; } }
break;
case nameof(Character.SpeedMultiplier):
{ if (parentObject is Character character) { value = character.SpeedMultiplier; return true; } }
break;
@@ -620,6 +650,9 @@ namespace Barotrauma
case nameof(Character.LowPassMultiplier):
{ if (parentObject is Character character) { value = character.LowPassMultiplier; return true; } }
break;
case nameof(Character.ObstructVisionAmount):
{ if (parentObject is Character character) { value = character.ObstructVisionAmount; return true; } }
break;
case nameof(Character.HullOxygenPercentage):
{
if (parentObject is Character character)
@@ -655,12 +688,21 @@ namespace Barotrauma
case nameof(PowerTransfer.Overload):
if (parentObject is PowerTransfer powerTransfer) { value = powerTransfer.Overload; return true; }
break;
case nameof(PowerContainer.OutputDisabled):
if (parentObject is PowerContainer powerContainer) { value = powerContainer.OutputDisabled; return true; }
break;
case nameof(MotionSensor.MotionDetected):
if (parentObject is MotionSensor motionSensor) { value = motionSensor.MotionDetected; return true; }
break;
case nameof(Character.IsDead):
{ if (parentObject is Character character) { value = character.IsDead; return true; } }
break;
case nameof(Character.NeedsAir):
{ if (parentObject is Character character) { value = character.NeedsAir; return true; } }
break;
case nameof(Character.NeedsOxygen):
{ if (parentObject is Character character) { value = character.NeedsOxygen; return true; } }
break;
case nameof(Character.IsHuman):
{ if (parentObject is Character character) { value = character.IsHuman; return true; } }
break;
@@ -684,6 +726,9 @@ namespace Barotrauma
case nameof(Controller.State):
if (parentObject is Controller controller) { value = controller.State; return true; }
break;
case nameof(Holdable.Attached):
if (parentObject is Holdable holdable) { value = holdable.Attached; return true; }
break;
case nameof(Character.InWater):
{
if (parentObject is Character character)
@@ -768,6 +813,12 @@ namespace Barotrauma
case nameof(Item.Scale):
{ if (parentObject is Item item) { item.Scale = value; return true; } }
break;
case nameof(Item.SightRange):
{ if (parentObject is Item item) { item.SightRange = value; return true; } }
break;
case nameof(Item.SoundRange):
{ if (parentObject is Item item) { item.SoundRange = value; return true; } }
break;
}
return false;
}
@@ -1082,7 +1133,7 @@ namespace Barotrauma
{
var componentElement = subElement.FirstElement();
if (componentElement == null) { continue; }
ItemComponent itemComponent = item2.Components.First(c => c.Name == componentElement.Name.ToString());
ItemComponent itemComponent = item2.Components.FirstOrDefault(c => c.Name == componentElement.Name.ToString());
if (itemComponent == null) { continue; }
foreach (XAttribute attribute in componentElement.Attributes())
{
@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
@@ -94,7 +95,7 @@ namespace Barotrauma
try
{
ToolBox.IsProperFilenameCase(filePath);
using FileStream stream = File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
using FileStream stream = File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, catchUnauthorizedAccessExceptions: false);
using XmlReader reader = CreateReader(stream, Path.GetFullPath(filePath));
doc = XDocument.Load(reader, LoadOptions.SetBaseUri);
}
@@ -511,23 +512,54 @@ namespace Barotrauma
return ushortValue;
}
public static T ParseEnumValue<T>(string value, T defaultValue, XAttribute attribute) where T : struct, Enum
{
if (Enum.TryParse(value, true, out T result))
{
return result;
}
else if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out int resultInt))
{
return Unsafe.As<int, T>(ref resultInt);
}
else
{
DebugConsole.ThrowError($"Error in {attribute}! \"{value}\" is not a valid {typeof(T).Name} value");
return defaultValue;
}
}
public static T GetAttributeEnum<T>(this XElement element, string name, T defaultValue) where T : struct, Enum
{
var attr = element?.GetAttribute(name);
if (attr == null) { return defaultValue; }
return ParseEnumValue<T>(attr.Value, defaultValue, attr);
}
if (Enum.TryParse(attr.Value, true, out T result))
[return: NotNullIfNotNull("defaultValue")]
public static T[] GetAttributeEnumArray<T>(this XElement element, string name, T[] defaultValue) where T : struct, Enum
{
string[] stringArray = element.GetAttributeStringArray(name, null);
if (stringArray == null) { return defaultValue; }
else if (stringArray.Length == 0) { return new T[0]; }
T[] enumArray = new T[stringArray.Length];
var attribute = element.GetAttribute(name);
for (int i = 0; i < stringArray.Length; i++)
{
return result;
try
{
enumArray[i] = ParseEnumValue<T>(stringArray[i].Trim(), default(T), attribute);
}
catch (Exception e)
{
LogAttributeError(attribute, element, e);
}
}
else if (int.TryParse(attr.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out int resultInt))
{
return Unsafe.As<int, T>(ref resultInt);
}
DebugConsole.ThrowError($"Error in {attr}! \"{attr}\" is not a valid {typeof(T).Name} value");
return default;
return enumArray;
}
public static bool GetAttributeBool(this XElement element, string name, bool defaultValue)
@@ -704,9 +736,9 @@ namespace Barotrauma
public static string ElementInnerText(this XElement el)
{
StringBuilder str = new StringBuilder();
foreach (XNode element in el.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Text))
foreach (XText textNode in el.DescendantNodes().OfType<XText>())
{
str.Append(element.ToString());
str.Append(textNode.Value);
}
return str.ToString();
}
@@ -1029,16 +1061,6 @@ namespace Barotrauma
public static Identifier VariantOf(this XElement element) =>
element.GetAttributeIdentifier("inherit", element.GetAttributeIdentifier("variantof", ""));
public static string[] ParseStringArray(string stringArrayValues)
{
return string.IsNullOrEmpty(stringArrayValues) ? Array.Empty<string>() : stringArrayValues.Split(';');
}
public static Identifier[] ParseIdentifierArray(string stringArrayValues)
{
return ParseStringArray(stringArrayValues).ToIdentifiers().ToArray();
}
public static bool IsOverride(this XElement element) => element.NameAsIdentifier() == "override";
/// <summary>
@@ -1051,7 +1073,21 @@ namespace Barotrauma
public static XAttribute GetAttribute(this XElement element, string name, StringComparison comparisonMethod = StringComparison.OrdinalIgnoreCase) => element.GetAttribute(a => a.Name.ToString().Equals(name, comparisonMethod));
public static void SetAttributeValue(this XElement element, string name, object value, StringComparison comparisonMethod = StringComparison.OrdinalIgnoreCase) => GetAttribute(element, name, comparisonMethod)?.SetValue(value);
public static bool TrySetAttributeValue(this XElement element, string name, object value, StringComparison comparisonMethod = StringComparison.OrdinalIgnoreCase)
{
var attribute = GetAttribute(element, name, comparisonMethod);
if (attribute == null) { return false; }
attribute.SetValue(value);
return true;
}
public static void SetAttribute(this XElement element, string name, object value)
{
if (!TrySetAttributeValue(element, name, value))
{
element.SetAttributeValue(name, value);
}
}
public static XAttribute GetAttribute(this XElement element, Identifier name) => element.GetAttribute(name.Value, StringComparison.OrdinalIgnoreCase);