Working on improving the serialization system and ObjectProperty/IPropertyObject (TODO: come up with better names). The plan is to:

- Add support for some of the most common types (vectors, colors, rects) so there's no need to parse the values in the setters of the serializable properties (see Holdable.HoldPos for example).
- Make a generic version of the item editing HUD that can be used on any IPropertyObject. Should make it easier to implement things like the character editor, editing structure properties, particle editor, etc.
- Improve the interface of the editing HUD. Instead of having to type in a string value into a textbox, there should be number input fields for numeric properties, sliders for properties that only accept a range of values, a color picker, etc. And tooltips.
This commit is contained in:
Joonas Rikkonen
2017-11-03 19:41:21 +02:00
parent 8b9be55135
commit a0d606ef5f
41 changed files with 345 additions and 263 deletions
@@ -10,7 +10,7 @@ namespace Barotrauma.Items.Components
private Color textColor; private Color textColor;
[HasDefaultValue("", true), Editable(100)] [SerializableProperty("", true), Editable(100)]
public string Text public string Text
{ {
get { return textBlock.Text.Replace("\n", ""); } get { return textBlock.Text.Replace("\n", ""); }
@@ -27,18 +27,17 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue("0.0,0.0,0.0,1.0", true)] [Editable, SerializableProperty("0.0,0.0,0.0,1.0", true)]
public string TextColor public Color TextColor
{ {
get { return XMLExtensions.Vector4ToString(textColor.ToVector4()); } get { return textColor; }
set set
{ {
textColor = new Color(XMLExtensions.ParseToVector4(value)); if (textBlock != null) textBlock.TextColor = value;
if (textBlock != null) textBlock.TextColor = textColor;
} }
} }
[Editable, HasDefaultValue(1.0f, true)] [Editable, SerializableProperty(1.0f, true)]
public float TextScale public float TextScale
{ {
get { return textBlock == null ? 1.0f : textBlock.TextScale; } get { return textBlock == null ? 1.0f : textBlock.TextScale; }
@@ -5,21 +5,21 @@ namespace Barotrauma.Items.Components
{ {
partial class ItemLabel : ItemComponent, IDrawableComponent partial class ItemLabel : ItemComponent, IDrawableComponent
{ {
[HasDefaultValue("", true), Editable(100)] [SerializableProperty("", true), Editable(100)]
public string Text public string Text
{ {
get; get;
set; set;
} }
[Editable, HasDefaultValue("0.0,0.0,0.0,1.0", true)] [Editable, SerializableProperty("0.0,0.0,0.0,1.0", true)]
public string TextColor public Color TextColor
{ {
get; get;
set; set;
} }
[Editable, HasDefaultValue(1.0f, true)] [Editable, SerializableProperty(1.0f, true)]
public float TextScale public float TextScale
{ {
get; get;
@@ -1381,7 +1381,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Source\GameSession\GameSession.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\GameSession\GameSession.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\GameSession\InfoTextManager.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\GameSession\InfoTextManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\GameSettings.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\GameSettings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\IPropertyObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Items\CharacterInventory.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Items\CharacterInventory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Items\Components\DockingPort.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Items\Components\DockingPort.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Items\Components\Door.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Items\Components\Door.cs" />
@@ -1493,10 +1492,12 @@
<Compile Include="$(MSBuildThisFileDirectory)Source\Physics\Physics.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Physics\Physics.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Physics\PhysicsBody.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Physics\PhysicsBody.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\PlayerInput.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\PlayerInput.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Properties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Screens\GameScreen.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Screens\GameScreen.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Screens\NetLobbyScreen.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Screens\NetLobbyScreen.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Screens\Screen.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Screens\Screen.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Serialization\IPropertyObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Serialization\Properties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Serialization\XMLExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Sprite\Sprite.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Sprite\Sprite.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Sprite\SpriteSheet.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Sprite\SpriteSheet.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Timing.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Timing.cs" />
@@ -1507,6 +1508,5 @@
<Compile Include="$(MSBuildThisFileDirectory)Source\Utils\SaveUtil.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Utils\SaveUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Utils\ToolBox.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Utils\ToolBox.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Utils\UpdaterUtil.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Source\Utils\UpdaterUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Utils\XMLExtensions.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -47,21 +47,21 @@ namespace Barotrauma.Items.Components
set { dockingDir = value; } set { dockingDir = value; }
} }
[HasDefaultValue("32.0,32.0", false)] [SerializableProperty("32.0,32.0", false)]
public string DistanceTolerance public Vector2 DistanceTolerance
{ {
get { return XMLExtensions.Vector2ToString(distanceTolerance); } get { return distanceTolerance; }
set { distanceTolerance = XMLExtensions.ParseToVector2(value); } set { distanceTolerance = value; }
} }
[HasDefaultValue(32.0f, false)] [SerializableProperty(32.0f, false)]
public float DockedDistance public float DockedDistance
{ {
get; get;
set; set;
} }
[HasDefaultValue(true, false)] [SerializableProperty(true, false)]
public bool IsHorizontal public bool IsHorizontal
{ {
get; get;
@@ -111,7 +111,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue("0.0,0.0,0.0,0.0", false)] [SerializableProperty("0.0,0.0,0.0,0.0", false)]
public string Window public string Window
{ {
get { return XMLExtensions.Vector4ToString(new Vector4(window.X, window.Y, window.Width, window.Height)); } get { return XMLExtensions.Vector4ToString(new Vector4(window.X, window.Y, window.Width, window.Height)); }
@@ -130,7 +130,7 @@ namespace Barotrauma.Items.Components
get { return window; } get { return window; }
} }
[Editable, HasDefaultValue(false, true)] [Editable, SerializableProperty(false, true)]
public bool IsOpen public bool IsOpen
{ {
get { return isOpen; } get { return isOpen; }
@@ -29,49 +29,49 @@ namespace Barotrauma.Items.Components
//the angle in which the Character holds the item //the angle in which the Character holds the item
protected float holdAngle; protected float holdAngle;
[HasDefaultValue(false, true)] [SerializableProperty(false, true)]
public bool Attached public bool Attached
{ {
get { return attached && item.ParentInventory == null; } get { return attached && item.ParentInventory == null; }
set { attached = value; } set { attached = value; }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool ControlPose public bool ControlPose
{ {
get; get;
set; set;
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool Attachable public bool Attachable
{ {
get { return attachable; } get { return attachable; }
set { attachable = value; } set { attachable = value; }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool AttachedByDefault public bool AttachedByDefault
{ {
get { return attachedByDefault; } get { return attachedByDefault; }
set { attachedByDefault = value; } set { attachedByDefault = value; }
} }
[HasDefaultValue("0.0,0.0", false)] [SerializableProperty("0.0,0.0", false)]
public string HoldPos public Vector2 HoldPos
{ {
get { return XMLExtensions.Vector2ToString(ConvertUnits.ToDisplayUnits(holdPos)); } get { return ConvertUnits.ToDisplayUnits(holdPos); }
set { holdPos = ConvertUnits.ToSimUnits(XMLExtensions.ParseToVector2(value)); } set { holdPos = ConvertUnits.ToSimUnits(value); }
} }
[HasDefaultValue("0.0,0.0", false)] [SerializableProperty("0.0,0.0", false)]
public string AimPos public Vector2 AimPos
{ {
get { return XMLExtensions.Vector2ToString(ConvertUnits.ToDisplayUnits(aimPos)); } get { return ConvertUnits.ToDisplayUnits(aimPos); }
set { aimPos = ConvertUnits.ToSimUnits(XMLExtensions.ParseToVector2(value)); } set { aimPos = ConvertUnits.ToSimUnits(value); }
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float HoldAngle public float HoldAngle
{ {
get { return MathHelper.ToDegrees(holdAngle); } get { return MathHelper.ToDegrees(holdAngle); }
@@ -24,14 +24,14 @@ namespace Barotrauma.Items.Components
private float reloadTimer; private float reloadTimer;
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float Range public float Range
{ {
get { return ConvertUnits.ToDisplayUnits(range); } get { return ConvertUnits.ToDisplayUnits(range); }
set { range = ConvertUnits.ToSimUnits(value); } set { range = ConvertUnits.ToSimUnits(value); }
} }
[HasDefaultValue(0.5f, false)] [SerializableProperty(0.5f, false)]
public float Reload public float Reload
{ {
get { return reload; } get { return reload; }
@@ -22,7 +22,7 @@ namespace Barotrauma.Items.Components
private UsableIn usableIn; private UsableIn usableIn;
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float Force public float Force
{ {
get { return force; } get { return force; }
@@ -30,7 +30,7 @@ namespace Barotrauma.Items.Components
} }
#if CLIENT #if CLIENT
[HasDefaultValue("", false)] [SerializableProperty("", false)]
public string Particles public string Particles
{ {
get { return particles; } get { return particles; }
@@ -13,28 +13,28 @@ namespace Barotrauma.Items.Components
private Vector2 barrelPos; private Vector2 barrelPos;
[HasDefaultValue("0.0,0.0", false)] [SerializableProperty("0.0,0.0", false)]
public string BarrelPos public string BarrelPos
{ {
get { return XMLExtensions.Vector2ToString(ConvertUnits.ToDisplayUnits(barrelPos)); } get { return XMLExtensions.Vector2ToString(ConvertUnits.ToDisplayUnits(barrelPos)); }
set { barrelPos = ConvertUnits.ToSimUnits(XMLExtensions.ParseToVector2(value)); } set { barrelPos = ConvertUnits.ToSimUnits(XMLExtensions.ParseToVector2(value)); }
} }
[HasDefaultValue(1.0f, false)] [SerializableProperty(1.0f, false)]
public float Reload public float Reload
{ {
get { return reload; } get { return reload; }
set { reload = Math.Max(value, 0.0f); } set { reload = Math.Max(value, 0.0f); }
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float Spread public float Spread
{ {
get; get;
set; set;
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float UnskilledSpread public float UnskilledSpread
{ {
get; get;
@@ -21,48 +21,48 @@ namespace Barotrauma.Items.Components
private float activeTimer; private float activeTimer;
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float Range public float Range
{ {
get { return range; } get { return range; }
set { range = value; } set { range = value; }
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float StructureFixAmount public float StructureFixAmount
{ {
get; set; get; set;
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float LimbFixAmount public float LimbFixAmount
{ {
get; set; get; set;
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float ExtinquishAmount public float ExtinquishAmount
{ {
get; set; get; set;
} }
[HasDefaultValue("", false)] [SerializableProperty("", false)]
public string Particles public string Particles
{ {
get { return particles; } get { return particles; }
set { particles = value; } set { particles = value; }
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float ParticleSpeed public float ParticleSpeed
{ {
get; set; get; set;
} }
[HasDefaultValue("0.0,0.0", false)] [SerializableProperty("0.0,0.0", false)]
public string BarrelPos public Vector2 BarrelPos
{ {
get { return XMLExtensions.Vector2ToString(barrelPos); } get { return barrelPos; }
set { barrelPos = XMLExtensions.ParseToVector2(value); } set { barrelPos = value; }
} }
public Vector2 TransformedBarrelPos public Vector2 TransformedBarrelPos
@@ -12,7 +12,7 @@ namespace Barotrauma.Items.Components
bool throwing; bool throwing;
[HasDefaultValue(1.0f, false)] [SerializableProperty(1.0f, false)]
public float ThrowForce public float ThrowForce
{ {
get { return throwForce; } get { return throwForce; }
@@ -47,7 +47,7 @@ namespace Barotrauma.Items.Components
private string msg; private string msg;
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float PickingTime public float PickingTime
{ {
get; get;
@@ -103,21 +103,21 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool CanBePicked public bool CanBePicked
{ {
get { return canBePicked; } get { return canBePicked; }
set { canBePicked = value; } set { canBePicked = value; }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool DrawHudWhenEquipped public bool DrawHudWhenEquipped
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool CanBeSelected public bool CanBeSelected
{ {
get { return canBeSelected; } get { return canBeSelected; }
@@ -136,7 +136,7 @@ namespace Barotrauma.Items.Components
protected set; protected set;
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool DeleteOnUse public bool DeleteOnUse
{ {
get; get;
@@ -153,7 +153,7 @@ namespace Barotrauma.Items.Components
get { return name; } get { return name; }
} }
[HasDefaultValue("", false)] [SerializableProperty("", false)]
public string Msg public string Msg
{ {
get { return msg; } get { return msg; }
@@ -205,7 +205,7 @@ namespace Barotrauma.Items.Components
DebugConsole.ThrowError("Invalid pick key in " + element + "!", e); DebugConsole.ThrowError("Invalid pick key in " + element + "!", e);
} }
properties = ObjectProperty.InitProperties(this, element); properties = ObjectProperty.DeserializeProperties(this, element);
foreach (XElement subElement in element.Elements()) foreach (XElement subElement in element.Elements())
{ {
@@ -586,7 +586,7 @@ namespace Barotrauma.Items.Components
componentElement.Add(newElement); componentElement.Add(newElement);
} }
ObjectProperty.SaveProperties(this, componentElement); ObjectProperty.SerializeProperties(this, componentElement);
parentElement.Add(componentElement); parentElement.Add(componentElement);
return componentElement; return componentElement;
@@ -18,7 +18,7 @@ namespace Barotrauma.Items.Components
private ushort[] itemIds; private ushort[] itemIds;
//how many items can be contained //how many items can be contained
[HasDefaultValue(5, false)] [SerializableProperty(5, false)]
public int Capacity public int Capacity
{ {
get { return capacity; } get { return capacity; }
@@ -26,7 +26,7 @@ namespace Barotrauma.Items.Components
} }
private int capacity; private int capacity;
[HasDefaultValue(true, false)] [SerializableProperty(true, false)]
public bool HideItems public bool HideItems
{ {
get { return hideItems; } get { return hideItems; }
@@ -38,7 +38,7 @@ namespace Barotrauma.Items.Components
} }
private bool hideItems; private bool hideItems;
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool DrawInventory public bool DrawInventory
{ {
get { return drawInventory; } get { return drawInventory; }
@@ -47,24 +47,24 @@ namespace Barotrauma.Items.Components
private bool drawInventory; private bool drawInventory;
//the position of the first item in the container //the position of the first item in the container
[HasDefaultValue("0.0,0.0", false)] [SerializableProperty("0.0,0.0", false)]
public string ItemPos public Vector2 ItemPos
{ {
get { return XMLExtensions.Vector2ToString(itemPos); } get { return itemPos; }
set { itemPos = XMLExtensions.ParseToVector2(value); } set { itemPos = value; }
} }
private Vector2 itemPos; private Vector2 itemPos;
//item[i].Pos = itemPos + itemInterval*i //item[i].Pos = itemPos + itemInterval*i
[HasDefaultValue("0.0,0.0", false)] [SerializableProperty("0.0,0.0", false)]
public string ItemInterval public Vector2 ItemInterval
{ {
get { return XMLExtensions.Vector2ToString(itemInterval); } get { return itemInterval; }
set { itemInterval = XMLExtensions.ParseToVector2(value); } set { itemInterval = value; }
} }
private Vector2 itemInterval; private Vector2 itemInterval;
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float ItemRotation public float ItemRotation
{ {
get { return MathHelper.ToDegrees(itemRotation); } get { return MathHelper.ToDegrees(itemRotation); }
@@ -73,19 +73,18 @@ namespace Barotrauma.Items.Components
private float itemRotation; private float itemRotation;
[HasDefaultValue("0.5,0.9", false)] [SerializableProperty("0.5,0.9", false)]
public string HudPos public Vector2 HudPos
{ {
get { return XMLExtensions.Vector2ToString(hudPos); } get { return hudPos; }
set set
{ {
hudPos = XMLExtensions.ParseToVector2(value); hudPos = value;
//inventory.CenterPos = hudPos;
} }
} }
private Vector2 hudPos; private Vector2 hudPos;
[HasDefaultValue(5, false)] [SerializableProperty(5, false)]
public int SlotsPerRow public int SlotsPerRow
{ {
get { return slotsPerRow; } get { return slotsPerRow; }
@@ -23,7 +23,7 @@ namespace Barotrauma.Items.Components
// } // }
//} //}
[Editable, HasDefaultValue(2000.0f, true)] [Editable, SerializableProperty(2000.0f, true)]
public float MaxForce public float MaxForce
{ {
get { return maxForce; } get { return maxForce; }
@@ -16,21 +16,21 @@ namespace Barotrauma.Items.Components
bool hasPower; bool hasPower;
[Editable, HasDefaultValue(false, true)] [Editable, SerializableProperty(false, true)]
public bool RequireWaterDetectors public bool RequireWaterDetectors
{ {
get; get;
set; set;
} }
[Editable, HasDefaultValue(true, true)] [Editable, SerializableProperty(true, true)]
public bool RequireOxygenDetectors public bool RequireOxygenDetectors
{ {
get; get;
set; set;
} }
[Editable, HasDefaultValue(false, true)] [Editable, SerializableProperty(false, true)]
public bool ShowHullIntegrity public bool ShowHullIntegrity
{ {
get; get;
@@ -29,7 +29,7 @@ namespace Barotrauma.Items.Components
private set; private set;
} }
[Editable, HasDefaultValue(100.0f, true)] [Editable, SerializableProperty(100.0f, true)]
public float GeneratedAmount public float GeneratedAmount
{ {
get { return generatedAmount; } get { return generatedAmount; }
@@ -15,7 +15,7 @@ namespace Barotrauma.Items.Components
public Hull hull1; public Hull hull1;
[HasDefaultValue(0.0f, true)] [SerializableProperty(0.0f, true)]
public float FlowPercentage public float FlowPercentage
{ {
get { return flowPercentage; } get { return flowPercentage; }
@@ -27,7 +27,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(80.0f, false)] [SerializableProperty(80.0f, false)]
public float MaxFlow public float MaxFlow
{ {
get { return maxFlow; } get { return maxFlow; }
@@ -26,14 +26,14 @@ namespace Barotrauma.Items.Components
private float displayBorderSize; private float displayBorderSize;
[HasDefaultValue(10000.0f, false)] [SerializableProperty(10000.0f, false)]
public float Range public float Range
{ {
get { return range; } get { return range; }
set { range = MathHelper.Clamp(value, 0.0f, 100000.0f); } set { range = MathHelper.Clamp(value, 0.0f, 100000.0f); }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool DetectSubmarineWalls public bool DetectSubmarineWalls
{ {
get; get;
@@ -54,7 +54,7 @@ namespace Barotrauma.Items.Components
private float? nextServerLogWriteTime; private float? nextServerLogWriteTime;
private float lastServerLogWriteTime; private float lastServerLogWriteTime;
[Editable, HasDefaultValue(9500.0f, true)] [Editable, SerializableProperty(9500.0f, true)]
public float MeltDownTemp public float MeltDownTemp
{ {
get { return meltDownTemp; } get { return meltDownTemp; }
@@ -64,7 +64,7 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(9000.0f, true)] [Editable, SerializableProperty(9000.0f, true)]
public float FireTemp public float FireTemp
{ {
get { return fireTemp; } get { return fireTemp; }
@@ -74,7 +74,7 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(1.0f, true)] [Editable, SerializableProperty(1.0f, true)]
public float PowerPerTemp public float PowerPerTemp
{ {
get { return powerPerTemp; } get { return powerPerTemp; }
@@ -84,7 +84,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(0.0f, true)] [SerializableProperty(0.0f, true)]
public float FissionRate public float FissionRate
{ {
get { return fissionRate; } get { return fissionRate; }
@@ -95,7 +95,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(0.0f, true)] [SerializableProperty(0.0f, true)]
public float CoolingRate public float CoolingRate
{ {
get { return coolingRate; } get { return coolingRate; }
@@ -106,7 +106,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(0.0f, true)] [SerializableProperty(0.0f, true)]
public float Temperature public float Temperature
{ {
get { return temperature; } get { return temperature; }
@@ -122,7 +122,7 @@ namespace Barotrauma.Items.Components
return (temperature > 0.0f); return (temperature > 0.0f);
} }
[HasDefaultValue(false, true)] [SerializableProperty(false, true)]
public bool AutoTemp public bool AutoTemp
{ {
get { return autoTemp; } get { return autoTemp; }
@@ -139,7 +139,7 @@ namespace Barotrauma.Items.Components
public float AvailableFuel { get; set; } public float AvailableFuel { get; set; }
[HasDefaultValue(500.0f, true)] [SerializableProperty(500.0f, true)]
public float ShutDownTemp public float ShutDownTemp
{ {
get { return shutDownTemp; } get { return shutDownTemp; }
@@ -67,7 +67,7 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(0.5f, true)] [Editable, SerializableProperty(0.5f, true)]
public float NeutralBallastLevel public float NeutralBallastLevel
{ {
get { return neutralBallastLevel; } get { return neutralBallastLevel; }
@@ -32,21 +32,21 @@ namespace Barotrauma.Items.Components
private set; private set;
} }
[Editable, HasDefaultValue(10.0f, true)] [Editable, SerializableProperty(10.0f, true)]
public float MaxOutPut public float MaxOutPut
{ {
set { maxOutput = value; } set { maxOutput = value; }
get { return maxOutput; } get { return maxOutput; }
} }
[HasDefaultValue(10.0f, true), Editable] [SerializableProperty(10.0f, true), Editable]
public float Capacity public float Capacity
{ {
get { return capacity; } get { return capacity; }
set { capacity = Math.Max(value, 1.0f); } set { capacity = Math.Max(value, 1.0f); }
} }
[Editable, HasDefaultValue(0.0f, true)] [Editable, SerializableProperty(0.0f, true)]
public float Charge public float Charge
{ {
get { return charge; } get { return charge; }
@@ -63,7 +63,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(10.0f, true), Editable] [SerializableProperty(10.0f, true), Editable]
public float RechargeSpeed public float RechargeSpeed
{ {
get { return rechargeSpeed; } get { return rechargeSpeed; }
@@ -75,7 +75,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(10.0f, false), Editable] [SerializableProperty(10.0f, false), Editable]
public float MaxRechargeSpeed public float MaxRechargeSpeed
{ {
get { return maxRechargeSpeed; } get { return maxRechargeSpeed; }
@@ -18,14 +18,14 @@ namespace Barotrauma.Items.Components
//the maximum amount of power the item can draw from connected items //the maximum amount of power the item can draw from connected items
protected float powerConsumption; protected float powerConsumption;
[Editable, HasDefaultValue(0.5f, true)] [Editable, SerializableProperty(0.5f, true)]
public float MinVoltage public float MinVoltage
{ {
get { return minVoltage; } get { return minVoltage; }
set { minVoltage = value; } set { minVoltage = value; }
} }
[Editable, HasDefaultValue(0.0f, true)] [Editable, SerializableProperty(0.0f, true)]
public float PowerConsumption public float PowerConsumption
{ {
get { return powerConsumption; } get { return powerConsumption; }
@@ -33,7 +33,7 @@ namespace Barotrauma.Items.Components
} }
[HasDefaultValue(false,true)] [SerializableProperty(false,true)]
public override bool IsActive public override bool IsActive
{ {
get { return base.IsActive; } get { return base.IsActive; }
@@ -44,14 +44,14 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(0.0f, true)] [SerializableProperty(0.0f, true)]
public float CurrPowerConsumption public float CurrPowerConsumption
{ {
get {return currPowerConsumption; } get {return currPowerConsumption; }
set { currPowerConsumption = value; } set { currPowerConsumption = value; }
} }
[HasDefaultValue(0.0f, true)] [SerializableProperty(0.0f, true)]
public float Voltage public float Voltage
{ {
get { return voltage; } get { return voltage; }
@@ -23,35 +23,35 @@ namespace Barotrauma.Items.Components
public Character User; public Character User;
[HasDefaultValue(10.0f, false)] [SerializableProperty(10.0f, false)]
public float LaunchImpulse public float LaunchImpulse
{ {
get { return launchImpulse; } get { return launchImpulse; }
set { launchImpulse = value; } set { launchImpulse = value; }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool CharacterUsable public bool CharacterUsable
{ {
get { return characterUsable; } get { return characterUsable; }
set { characterUsable = value; } set { characterUsable = value; }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool DoesStick public bool DoesStick
{ {
get { return doesStick; } get { return doesStick; }
set { doesStick = value; } set { doesStick = value; }
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool Hitscan public bool Hitscan
{ {
get; get;
set; set;
} }
[HasDefaultValue(false, false)] [SerializableProperty(false, false)]
public bool RemoveOnHit public bool RemoveOnHit
{ {
get; get;
@@ -13,7 +13,7 @@ namespace Barotrauma.Items.Components
//the output is sent if both inputs have received a signal within the timeframe //the output is sent if both inputs have received a signal within the timeframe
protected float timeFrame; protected float timeFrame;
[InGameEditable, HasDefaultValue(0.0f, true)] [InGameEditable, SerializableProperty(0.0f, true)]
public float TimeFrame public float TimeFrame
{ {
get { return timeFrame; } get { return timeFrame; }
@@ -23,14 +23,14 @@ namespace Barotrauma.Items.Components
} }
} }
[InGameEditable, HasDefaultValue("1", true)] [InGameEditable, SerializableProperty("1", true)]
public string Output public string Output
{ {
get { return output; } get { return output; }
set { output = value; } set { output = value; }
} }
[InGameEditable, HasDefaultValue("", true)] [InGameEditable, SerializableProperty("", true)]
public string FalseOutput public string FalseOutput
{ {
get { return falseOutput; } get { return falseOutput; }
@@ -15,7 +15,7 @@ namespace Barotrauma.Items.Components
private Queue<Tuple<string, DateTime>> signalQueue; private Queue<Tuple<string, DateTime>> signalQueue;
[InGameEditable, HasDefaultValue(1.0f, true)] [InGameEditable, SerializableProperty(1.0f, true)]
public float Delay public float Delay
{ {
get { return (float)delay.TotalSeconds; } get { return (float)delay.TotalSeconds; }
@@ -21,7 +21,7 @@ namespace Barotrauma.Items.Components
private bool castShadows; private bool castShadows;
[Editable, HasDefaultValue(100.0f, true)] [Editable, SerializableProperty(100.0f, true)]
public float Range public float Range
{ {
get { return range; } get { return range; }
@@ -31,7 +31,7 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(true, true)] [Editable, SerializableProperty(true, true)]
public bool CastShadows public bool CastShadows
{ {
get { return castShadows; } get { return castShadows; }
@@ -44,7 +44,7 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(false, true)] [Editable, SerializableProperty(false, true)]
public bool IsOn public bool IsOn
{ {
get { return IsActive; } get { return IsActive; }
@@ -57,7 +57,7 @@ namespace Barotrauma.Items.Components
} }
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float Flicker public float Flicker
{ {
get { return flicker; } get { return flicker; }
@@ -67,19 +67,11 @@ namespace Barotrauma.Items.Components
} }
} }
[InGameEditable, HasDefaultValue("1.0,1.0,1.0,1.0", true)] [InGameEditable, SerializableProperty("1.0,1.0,1.0,1.0", true)]
public string LightColor public Color LightColor
{ {
get { return XMLExtensions.Vector4ToString(lightColor.ToVector4(), "0.00"); } get { return lightColor; }
set set { lightColor = value; }
{
Vector4 newColor = XMLExtensions.ParseToVector4(value, false);
newColor.X = MathHelper.Clamp(newColor.X, 0.0f, 1.0f);
newColor.Y = MathHelper.Clamp(newColor.Y, 0.0f, 1.0f);
newColor.Z = MathHelper.Clamp(newColor.Z, 0.0f, 1.0f);
newColor.W = MathHelper.Clamp(newColor.W, 0.0f, 1.0f);
lightColor = new Color(newColor);
}
} }
public override void Move(Vector2 amount) public override void Move(Vector2 amount)
@@ -215,7 +207,7 @@ namespace Barotrauma.Items.Components
IsActive = (signal != "0"); IsActive = (signal != "0");
break; break;
case "set_color": case "set_color":
LightColor = signal; LightColor = XMLExtensions.ParseToColor(signal, false);
break; break;
} }
} }
@@ -17,7 +17,7 @@ namespace Barotrauma.Items.Components
private float updateTimer; private float updateTimer;
[InGameEditable, HasDefaultValue(0.0f, true)] [InGameEditable, SerializableProperty(0.0f, true)]
public float Range public float Range
{ {
get { return range; } get { return range; }
@@ -27,14 +27,14 @@ namespace Barotrauma.Items.Components
} }
} }
[InGameEditable, HasDefaultValue("1", true)] [InGameEditable, SerializableProperty("1", true)]
public string Output public string Output
{ {
get { return output; } get { return output; }
set { output = value; } set { output = value; }
} }
[InGameEditable, HasDefaultValue("", true)] [InGameEditable, SerializableProperty("", true)]
public string FalseOutput public string FalseOutput
{ {
get { return falseOutput; } get { return falseOutput; }
@@ -20,14 +20,14 @@ namespace Barotrauma.Items.Components
private float phase; private float phase;
[InGameEditable, HasDefaultValue(WaveType.Pulse, true)] [InGameEditable, SerializableProperty(WaveType.Pulse, true)]
public WaveType OutputType public WaveType OutputType
{ {
get; get;
set; set;
} }
[InGameEditable, HasDefaultValue(1.0f, true)] [InGameEditable, SerializableProperty(1.0f, true)]
public float Frequency public float Frequency
{ {
get { return frequency; } get { return frequency; }
@@ -16,14 +16,14 @@ namespace Barotrauma.Items.Components
private Regex regex; private Regex regex;
[InGameEditable, HasDefaultValue("1", true)] [InGameEditable, SerializableProperty("1", true)]
public string Output public string Output
{ {
get { return output; } get { return output; }
set { output = value; } set { output = value; }
} }
[InGameEditable, HasDefaultValue("", true)] [InGameEditable, SerializableProperty("", true)]
public string Expression public string Expression
{ {
get { return expression; } get { return expression; }
@@ -11,7 +11,7 @@ namespace Barotrauma.Items.Components
private bool isOn; private bool isOn;
[Editable, HasDefaultValue(1000.0f, true)] [Editable, SerializableProperty(1000.0f, true)]
public float MaxPower public float MaxPower
{ {
get { return maxPower; } get { return maxPower; }
@@ -21,7 +21,7 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(false, true)] [Editable, SerializableProperty(false, true)]
public bool IsOn public bool IsOn
{ {
get get
@@ -8,20 +8,20 @@ namespace Barotrauma.Items.Components
private string targetSignal; private string targetSignal;
[InGameEditable, HasDefaultValue("1", true)] [InGameEditable, SerializableProperty("1", true)]
public string Output public string Output
{ {
get { return output; } get { return output; }
set { output = value; } set { output = value; }
} }
[InGameEditable, HasDefaultValue("0", true)] [InGameEditable, SerializableProperty("0", true)]
public string FalseOutput public string FalseOutput
{ {
get { return falseOutput; } get { return falseOutput; }
set { falseOutput = value; } set { falseOutput = value; }
} }
[InGameEditable, HasDefaultValue("", true)] [InGameEditable, SerializableProperty("", true)]
public string TargetSignal public string TargetSignal
{ {
get { return targetSignal; } get { return targetSignal; }
@@ -14,14 +14,14 @@ namespace Barotrauma.Items.Components
private int channel; private int channel;
[HasDefaultValue(20000.0f, false)] [SerializableProperty(20000.0f, false)]
public float Range public float Range
{ {
get { return range; } get { return range; }
set { range = Math.Max(value, 0.0f); } set { range = Math.Max(value, 0.0f); }
} }
[InGameEditable, HasDefaultValue(1, true)] [InGameEditable, SerializableProperty(1, true)]
public int Channel public int Channel
{ {
get { return channel; } get { return channel; }
@@ -31,7 +31,7 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(false, false)] [Editable, SerializableProperty(false, false)]
public bool LinkToChat public bool LinkToChat
{ {
get; get;
@@ -26,49 +26,44 @@ namespace Barotrauma.Items.Components
Camera cam; Camera cam;
[HasDefaultValue("0,0", false)] [SerializableProperty("0,0", false)]
public string BarrelPos public Vector2 BarrelPos
{ {
get get
{ {
return XMLExtensions.Vector2ToString(barrelPos); return barrelPos;
} }
set set
{ {
barrelPos = XMLExtensions.ParseToVector2(value); barrelPos = value;
} }
} }
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float LaunchImpulse public float LaunchImpulse
{ {
get { return launchImpulse; } get { return launchImpulse; }
set { launchImpulse = value; } set { launchImpulse = value; }
} }
[HasDefaultValue(5.0f, false)] [SerializableProperty(5.0f, false)]
public float Reload public float Reload
{ {
get { return reloadTime; } get { return reloadTime; }
set { reloadTime = value; } set { reloadTime = value; }
} }
[HasDefaultValue("0.0,0.0", true), Editable] [SerializableProperty("0.0,0.0", true), Editable]
public string RotationLimits public Vector2 RotationLimits
{ {
get get
{ {
Vector2 limits = new Vector2(minRotation, maxRotation); return new Vector2(MathHelper.ToDegrees(minRotation), MathHelper.ToDegrees(maxRotation));
limits.X = MathHelper.ToDegrees(limits.X);
limits.Y = MathHelper.ToDegrees(limits.Y);
return XMLExtensions.Vector2ToString(limits);
} }
set set
{ {
Vector2 vector = XMLExtensions.ParseToVector2(value); minRotation = MathHelper.ToRadians(value.X);
minRotation = MathHelper.ToRadians(vector.X); maxRotation = MathHelper.ToRadians(value.Y);
maxRotation = MathHelper.ToRadians(vector.Y);
rotation = (minRotation + maxRotation) / 2; rotation = (minRotation + maxRotation) / 2;
} }
@@ -35,22 +35,21 @@ namespace Barotrauma.Items.Components
private Vector2 armorSector; private Vector2 armorSector;
[HasDefaultValue(0.0f, false)] [SerializableProperty(0.0f, false)]
public float ArmorValue public float ArmorValue
{ {
get { return armorValue; } get { return armorValue; }
set { armorValue = MathHelper.Clamp(value, 0.0f, 100.0f); } set { armorValue = MathHelper.Clamp(value, 0.0f, 100.0f); }
} }
[HasDefaultValue("0.0,360.0", false)] [SerializableProperty("0.0,360.0", false)]
public string ArmorSector public Vector2 ArmorSector
{ {
get { return XMLExtensions.Vector2ToString(armorSector); } get { return armorSector; }
set set
{ {
armorSector = XMLExtensions.ParseToVector2(value); armorSector.X = MathHelper.ToRadians(value.X);
armorSector.X = MathHelper.ToRadians(armorSector.X); armorSector.Y = MathHelper.ToRadians(value.Y);
armorSector.Y = MathHelper.ToRadians(armorSector.Y);
} }
} }
@@ -169,7 +169,7 @@ namespace Barotrauma
} }
protected Color spriteColor; protected Color spriteColor;
[Editable, HasDefaultValue("1.0,1.0,1.0,1.0", true)] [Editable, SerializableProperty("1.0,1.0,1.0,1.0", true)]
public string SpriteColor public string SpriteColor
{ {
get { return XMLExtensions.Vector4ToString(spriteColor.ToVector4()); } get { return XMLExtensions.Vector4ToString(spriteColor.ToVector4()); }
@@ -220,7 +220,7 @@ namespace Barotrauma
get { return condition; } get { return condition; }
} }
[Editable, HasDefaultValue("", true)] [Editable, SerializableProperty("", true)]
public string Tags public string Tags
{ {
get { return string.Join(",",tags); } get { return string.Join(",",tags); }
@@ -359,7 +359,7 @@ namespace Barotrauma
XElement element = prefab.ConfigElement; XElement element = prefab.ConfigElement;
if (element == null) return; if (element == null) return;
properties = ObjectProperty.InitProperties(this, element); properties = ObjectProperty.DeserializeProperties(this, element);
if (submarine == null || !submarine.Loading) FindHull(); if (submarine == null || !submarine.Loading) FindHull();
@@ -1400,7 +1400,7 @@ namespace Barotrauma
ObjectProperty objectProperty = allProperties[propertyIndex]; ObjectProperty objectProperty = allProperties[propertyIndex];
Type type = objectProperty.GetType(); Type type = objectProperty.PropertyType;
if (type == typeof(string)) if (type == typeof(string))
{ {
objectProperty.TrySetValue(msg.ReadString()); objectProperty.TrySetValue(msg.ReadString());
@@ -1643,7 +1643,7 @@ namespace Barotrauma
bool shouldBeLoaded = false; bool shouldBeLoaded = false;
foreach (var propertyAttribute in property.Attributes.OfType<HasDefaultValue>()) foreach (var propertyAttribute in property.Attributes.OfType<SerializableProperty>())
{ {
if (propertyAttribute.isSaveable) if (propertyAttribute.isSaveable)
{ {
@@ -1713,7 +1713,7 @@ namespace Barotrauma
element.Add(new XAttribute("linked", string.Join(",", linkedToIDs))); element.Add(new XAttribute("linked", string.Join(",", linkedToIDs)));
} }
ObjectProperty.SaveProperties(this, element); ObjectProperty.SerializeProperties(this, element);
foreach (ItemComponent ic in components) foreach (ItemComponent ic in components)
{ {
@@ -139,7 +139,7 @@ namespace Barotrauma
} }
} }
[HasDefaultValue(90.0f, true)] [SerializableProperty(90.0f, true)]
public float Oxygen public float Oxygen
{ {
get { return oxygen; } get { return oxygen; }
@@ -102,7 +102,7 @@ namespace Barotrauma
set; set;
} }
[HasDefaultValue(1000, false)] [SerializableProperty(1000, false)]
public int BackgroundSpriteAmount public int BackgroundSpriteAmount
{ {
get; get;
@@ -115,14 +115,14 @@ namespace Barotrauma
set; set;
} }
[HasDefaultValue(100000.0f, false)] [SerializableProperty(100000.0f, false)]
public float Width public float Width
{ {
get { return width; } get { return width; }
set { width = Math.Max(value, 2000.0f); } set { width = Math.Max(value, 2000.0f); }
} }
[HasDefaultValue(50000.0f, false)] [SerializableProperty(50000.0f, false)]
public float Height public float Height
{ {
get { return height; } get { return height; }
@@ -160,7 +160,7 @@ namespace Barotrauma
} }
} }
[HasDefaultValue(5, false)] [SerializableProperty(5, false)]
public int SmallTunnelCount public int SmallTunnelCount
{ {
get { return smallTunnelCount; } get { return smallTunnelCount; }
@@ -177,21 +177,21 @@ namespace Barotrauma
} }
} }
[HasDefaultValue(-300000.0f, false)] [SerializableProperty(-300000.0f, false)]
public float SeaFloorDepth public float SeaFloorDepth
{ {
get { return seaFloorBaseDepth; } get { return seaFloorBaseDepth; }
set { seaFloorBaseDepth = MathHelper.Clamp(value, Level.MaxEntityDepth, 0.0f); } set { seaFloorBaseDepth = MathHelper.Clamp(value, Level.MaxEntityDepth, 0.0f); }
} }
[HasDefaultValue(1000.0f, false)] [SerializableProperty(1000.0f, false)]
public float SeaFloorVariance public float SeaFloorVariance
{ {
get { return seaFloorVariance; } get { return seaFloorVariance; }
set { seaFloorVariance = value; } set { seaFloorVariance = value; }
} }
[HasDefaultValue(0, false)] [SerializableProperty(0, false)]
public int MountainCountMin public int MountainCountMin
{ {
get { return mountainCountMin; } get { return mountainCountMin; }
@@ -201,7 +201,7 @@ namespace Barotrauma
} }
} }
[HasDefaultValue(0, false)] [SerializableProperty(0, false)]
public int MountainCountMax public int MountainCountMax
{ {
get { return mountainCountMax; } get { return mountainCountMax; }
@@ -211,7 +211,7 @@ namespace Barotrauma
} }
} }
[HasDefaultValue(1000.0f, false)] [SerializableProperty(1000.0f, false)]
public float MountainHeightMin public float MountainHeightMin
{ {
get { return mountainHeightMin; } get { return mountainHeightMin; }
@@ -221,7 +221,7 @@ namespace Barotrauma
} }
} }
[HasDefaultValue(5000.0f, false)] [SerializableProperty(5000.0f, false)]
public float MountainHeightMax public float MountainHeightMax
{ {
get { return mountainHeightMax; } get { return mountainHeightMax; }
@@ -231,14 +231,14 @@ namespace Barotrauma
} }
} }
[HasDefaultValue(1, false)] [SerializableProperty(1, false)]
public int RuinCount public int RuinCount
{ {
get { return ruinCount; } get { return ruinCount; }
set { ruinCount = MathHelper.Clamp(value, 0, 10); } set { ruinCount = MathHelper.Clamp(value, 0, 10); }
} }
[HasDefaultValue(0.4f, false)] [SerializableProperty(0.4f, false)]
public float BottomHoleProbability public float BottomHoleProbability
{ {
get { return bottomHoleProbability; } get { return bottomHoleProbability; }
@@ -278,7 +278,7 @@ namespace Barotrauma
private LevelGenerationParams(XElement element) private LevelGenerationParams(XElement element)
{ {
Name = element == null ? "default" : element.Name.ToString(); Name = element == null ? "default" : element.Name.ToString();
ObjectProperties = ObjectProperty.InitProperties(this, element); ObjectProperties = ObjectProperty.DeserializeProperties(this, element);
Vector3 colorVector = element.GetAttributeVector3("BackgroundColor", new Vector3(50, 46, 20)); Vector3 colorVector = element.GetAttributeVector3("BackgroundColor", new Vector3(50, 46, 20));
BackgroundColor = new Color((int)colorVector.X, (int)colorVector.Y, (int)colorVector.Z); BackgroundColor = new Color((int)colorVector.X, (int)colorVector.Y, (int)colorVector.Z);
@@ -86,7 +86,7 @@ namespace Barotrauma.Networking
private List<SavedClientPermission> clientPermissions = new List<SavedClientPermission>(); private List<SavedClientPermission> clientPermissions = new List<SavedClientPermission>();
[HasDefaultValue(true, true)] [SerializableProperty(true, true)]
public bool RandomizeSeed public bool RandomizeSeed
{ {
get; get;
@@ -94,21 +94,21 @@ namespace Barotrauma.Networking
} }
[HasDefaultValue(300.0f, true)] [SerializableProperty(300.0f, true)]
public float RespawnInterval public float RespawnInterval
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(180.0f, true)] [SerializableProperty(180.0f, true)]
public float MaxTransportTime public float MaxTransportTime
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(0.2f, true)] [SerializableProperty(0.2f, true)]
public float MinRespawnRatio public float MinRespawnRatio
{ {
get; get;
@@ -116,42 +116,42 @@ namespace Barotrauma.Networking
} }
[HasDefaultValue(60.0f, true)] [SerializableProperty(60.0f, true)]
public float AutoRestartInterval public float AutoRestartInterval
{ {
get; get;
set; set;
} }
[HasDefaultValue(true, true)] [SerializableProperty(true, true)]
public bool AllowSpectating public bool AllowSpectating
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(true, true)] [SerializableProperty(true, true)]
public bool EndRoundAtLevelEnd public bool EndRoundAtLevelEnd
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(true, true)] [SerializableProperty(true, true)]
public bool SaveServerLogs public bool SaveServerLogs
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(true, true)] [SerializableProperty(true, true)]
public bool AllowFileTransfers public bool AllowFileTransfers
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(800, true)] [SerializableProperty(800, true)]
private int LinesPerLogFile private int LinesPerLogFile
{ {
get get
@@ -175,7 +175,7 @@ namespace Barotrauma.Networking
} }
} }
[HasDefaultValue(true, true)] [SerializableProperty(true, true)]
public bool AllowRespawn public bool AllowRespawn
{ {
get; get;
@@ -203,21 +203,21 @@ namespace Barotrauma.Networking
get { return banList; } get { return banList; }
} }
[HasDefaultValue(true, true)] [SerializableProperty(true, true)]
public bool AllowVoteKick public bool AllowVoteKick
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(0.6f, true)] [SerializableProperty(0.6f, true)]
public float EndVoteRequiredRatio public float EndVoteRequiredRatio
{ {
get; get;
private set; private set;
} }
[HasDefaultValue(0.6f, true)] [SerializableProperty(0.6f, true)]
public float KickVoteRequiredRatio public float KickVoteRequiredRatio
{ {
get; get;
@@ -228,7 +228,7 @@ namespace Barotrauma.Networking
{ {
XDocument doc = new XDocument(new XElement("serversettings")); XDocument doc = new XDocument(new XElement("serversettings"));
ObjectProperty.SaveProperties(this, doc.Root, true); ObjectProperty.SerializeProperties(this, doc.Root, true);
doc.Root.SetAttributeValue("name", name); doc.Root.SetAttributeValue("name", name);
doc.Root.SetAttributeValue("public", isPublic); doc.Root.SetAttributeValue("public", isPublic);
@@ -279,7 +279,7 @@ namespace Barotrauma.Networking
doc = new XDocument(new XElement("serversettings")); doc = new XDocument(new XElement("serversettings"));
} }
ObjectProperties = ObjectProperty.InitProperties(this, doc.Root); ObjectProperties = ObjectProperty.DeserializeProperties(this, doc.Root);
AutoRestart = doc.Root.GetAttributeBool("autorestart", false); AutoRestart = doc.Root.GetAttributeBool("autorestart", false);
#if CLIENT #if CLIENT
@@ -1,4 +1,5 @@
using System; using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
@@ -27,12 +28,12 @@ namespace Barotrauma
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
public class HasDefaultValue : System.Attribute public class SerializableProperty : System.Attribute
{ {
public object defaultValue; public object defaultValue;
public bool isSaveable; public bool isSaveable;
public HasDefaultValue(object defaultValue, bool isSaveable) public SerializableProperty(object defaultValue, bool isSaveable)
{ {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.isSaveable = isSaveable; this.isSaveable = isSaveable;
@@ -41,36 +42,44 @@ namespace Barotrauma
class ObjectProperty class ObjectProperty
{ {
readonly PropertyDescriptor property; private readonly PropertyDescriptor propertyDescriptor;
readonly PropertyInfo propertyInfo; private readonly PropertyInfo propertyInfo;
readonly object obj; private readonly object obj;
public string Name public string Name
{ {
get { return property.Name; } get { return propertyDescriptor.Name; }
} }
public AttributeCollection Attributes public AttributeCollection Attributes
{ {
get { return property.Attributes; } get { return propertyDescriptor.Attributes; }
}
public Type PropertyType
{
get
{
return propertyInfo.PropertyType;
}
} }
public ObjectProperty(PropertyDescriptor property, object obj) public ObjectProperty(PropertyDescriptor property, object obj)
{ {
this.property = property; this.propertyDescriptor = property;
propertyInfo = property.ComponentType.GetProperty(property.Name); propertyInfo = property.ComponentType.GetProperty(property.Name);
this.obj = obj; this.obj = obj;
} }
public bool TrySetValue(string value) public bool TrySetValue(string value)
{ {
if (value == null) return false; if (value == null) return false;
if (property.PropertyType == typeof(string)) if (propertyDescriptor.PropertyType == typeof(string))
{ {
propertyInfo.SetValue(obj, value, null); propertyInfo.SetValue(obj, value, null);
} }
else if (property.PropertyType == typeof(float)) else if (propertyDescriptor.PropertyType == typeof(float))
{ {
float floatVal; float floatVal;
if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal)) if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal))
@@ -78,11 +87,11 @@ namespace Barotrauma
propertyInfo.SetValue(obj, floatVal, null); propertyInfo.SetValue(obj, floatVal, null);
} }
} }
else if (property.PropertyType == typeof(bool)) else if (propertyDescriptor.PropertyType == typeof(bool))
{ {
propertyInfo.SetValue(obj, value.ToLowerInvariant() == "true", null); propertyInfo.SetValue(obj, value.ToLowerInvariant() == "true", null);
} }
else if (property.PropertyType == typeof(int)) else if (propertyDescriptor.PropertyType == typeof(int))
{ {
int intVal; int intVal;
if (int.TryParse(value, out intVal)) if (int.TryParse(value, out intVal))
@@ -90,7 +99,23 @@ namespace Barotrauma
propertyInfo.SetValue(obj, intVal, null); propertyInfo.SetValue(obj, intVal, null);
} }
} }
else if (property.PropertyType.IsEnum) else if (propertyDescriptor.PropertyType == typeof(Vector2))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToVector2(value));
}
else if (propertyDescriptor.PropertyType == typeof(Vector3))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToVector3(value));
}
else if (propertyDescriptor.PropertyType == typeof(Vector4))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToVector4(value));
}
else if (propertyDescriptor.PropertyType == typeof(Color))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToColor(value));
}
else if (propertyDescriptor.PropertyType.IsEnum)
{ {
object enumVal; object enumVal;
try try
@@ -118,18 +143,36 @@ namespace Barotrauma
public bool TrySetValue(object value) public bool TrySetValue(object value)
{ {
if (value == null) return false; if (value == null) return false;
if (obj == null || propertyDescriptor == null) return false;
if (property.PropertyType != value.GetType())
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString());
DebugConsole.ThrowError("(Non-matching type, should be " + property.PropertyType + " instead of " + value.GetType() + ")");
return false;
}
if (obj == null || property == null) return false;
try try
{ {
if (value.GetType() == typeof(string) &&
propertyDescriptor.PropertyType == typeof(Vector2))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToVector2(value.ToString()));
}
else if (value.GetType() == typeof(string) &&
propertyDescriptor.PropertyType == typeof(Vector3))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToVector3(value.ToString()));
}
else if (value.GetType() == typeof(string) &&
propertyDescriptor.PropertyType == typeof(Vector4))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToVector4(value.ToString()));
}
else if (value.GetType() == typeof(string) &&
propertyDescriptor.PropertyType == typeof(Color))
{
propertyInfo.SetValue(obj, XMLExtensions.ParseToColor(value.ToString()));
}
else if (propertyDescriptor.PropertyType != value.GetType())
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString());
DebugConsole.ThrowError("(Non-matching type, should be " + propertyDescriptor.PropertyType + " instead of " + value.GetType() + ")");
return false;
}
propertyInfo.SetValue(obj, value, null); propertyInfo.SetValue(obj, value, null);
} }
catch catch
@@ -143,13 +186,13 @@ namespace Barotrauma
{ {
try try
{ {
propertyInfo.SetValue(obj, value, null); propertyInfo.SetValue(obj, value, null);
} }
catch catch
{ {
return false; return false;
} }
return true; return true;
} }
@@ -181,7 +224,7 @@ namespace Barotrauma
public object GetValue() public object GetValue()
{ {
if (obj == null || property == null) return false; if (obj == null || propertyDescriptor == null) return false;
try try
{ {
@@ -192,12 +235,7 @@ namespace Barotrauma
return false; return false;
} }
} }
public Type GetType()
{
return propertyInfo.PropertyType;
}
public static List<ObjectProperty> GetProperties<T>(IPropertyObject obj) public static List<ObjectProperty> GetProperties<T>(IPropertyObject obj)
{ {
List<ObjectProperty> editableProperties = new List<ObjectProperty>(); List<ObjectProperty> editableProperties = new List<ObjectProperty>();
@@ -210,7 +248,7 @@ namespace Barotrauma
return editableProperties; return editableProperties;
} }
public static Dictionary<string, ObjectProperty> GetProperties(IPropertyObject obj) public static Dictionary<string, ObjectProperty> GetProperties(IPropertyObject obj)
{ {
var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast<PropertyDescriptor>(); var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast<PropertyDescriptor>();
@@ -224,12 +262,17 @@ namespace Barotrauma
return dictionary; return dictionary;
} }
/*/// <summary>
/// Sets all serializable properties to their default value
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Dictionary<string, ObjectProperty> InitProperties(IPropertyObject obj) public static Dictionary<string, ObjectProperty> InitProperties(IPropertyObject obj)
{ {
return InitProperties(obj, null); return DeserializeProperties(obj, null);
} }*/
public static Dictionary<string, ObjectProperty> InitProperties(IPropertyObject obj, XElement element) public static Dictionary<string, ObjectProperty> DeserializeProperties(IPropertyObject obj, XElement element)
{ {
var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast<PropertyDescriptor>(); var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast<PropertyDescriptor>();
@@ -241,14 +284,14 @@ namespace Barotrauma
dictionary.Add(property.Name.ToLowerInvariant(), objProperty); dictionary.Add(property.Name.ToLowerInvariant(), objProperty);
//set the value of the property to the default value if there is one //set the value of the property to the default value if there is one
foreach (var ini in property.Attributes.OfType<HasDefaultValue>()) foreach (var ini in property.Attributes.OfType<SerializableProperty>())
{ {
objProperty.TrySetValue(ini.defaultValue); objProperty.TrySetValue(ini.defaultValue);
break; break;
} }
} }
if (element!=null) if (element != null)
{ {
//go through all the attributes in the xml element //go through all the attributes in the xml element
//and set the value of the matching property if it is initializable //and set the value of the matching property if it is initializable
@@ -256,47 +299,80 @@ namespace Barotrauma
{ {
ObjectProperty property = null; ObjectProperty property = null;
if (!dictionary.TryGetValue(attribute.Name.ToString().ToLowerInvariant(), out property)) continue; if (!dictionary.TryGetValue(attribute.Name.ToString().ToLowerInvariant(), out property)) continue;
if (!property.Attributes.OfType<HasDefaultValue>().Any()) continue; if (!property.Attributes.OfType<SerializableProperty>().Any()) continue;
property.TrySetValue(attribute.Value); property.TrySetValue(attribute.Value);
} }
} }
return dictionary; return dictionary;
} }
public static void SaveProperties(IPropertyObject obj, XElement element, bool saveIfDefault = false) public static void SerializeProperties(IPropertyObject obj, XElement element, bool saveIfDefault = false)
{ {
var saveProperties = GetProperties<HasDefaultValue>(obj); var saveProperties = GetProperties<SerializableProperty>(obj);
foreach (var property in saveProperties) foreach (var property in saveProperties)
{ {
object value = property.GetValue(); object value = property.GetValue();
if (value == null) continue; if (value == null) continue;
if (!saveIfDefault) if (!saveIfDefault)
{ {
//only save //only save
// - if the attribute is saveable and it's different from the default value // - if the attribute is saveable and it's different from the default value
// - or can be changed in-game or in the editor // - or can be changed in-game or in the editor
bool save = false; bool save = false;
foreach (var attribute in property.Attributes.OfType<HasDefaultValue>()) foreach (var attribute in property.Attributes.OfType<SerializableProperty>())
{ {
if ((attribute.isSaveable && !attribute.defaultValue.Equals(value)) || if ((attribute.isSaveable && !attribute.defaultValue.Equals(value)) ||
property.Attributes.OfType<Editable>().Any()) property.Attributes.OfType<Editable>().Any())
{ {
save = true; save = true;
break; break;
} }
} }
if (!save) continue; if (!save) continue;
} }
string stringValue; string stringValue;
if (value is float) if (value.GetType() == typeof(float))
{ {
//do this to make sure the decimal point isn't converted to a comma or anything like that //make sure the decimal point isn't converted to a comma or anything else
stringValue = ((float)value).ToString("G", CultureInfo.InvariantCulture); stringValue = ((float)value).ToString("G", CultureInfo.InvariantCulture);
} }
else if (value.GetType() == typeof(Vector2))
{
Vector2 vector = (Vector2)value;
stringValue =
vector.X.ToString("G", CultureInfo.InvariantCulture) + "," +
vector.Y.ToString("G", CultureInfo.InvariantCulture);
}
else if (value.GetType() == typeof(Vector3))
{
Vector3 vector = (Vector3)value;
stringValue =
vector.X.ToString("G", CultureInfo.InvariantCulture) + "," +
vector.Y.ToString("G", CultureInfo.InvariantCulture) + "," +
vector.Z.ToString("G", CultureInfo.InvariantCulture);
}
else if (value.GetType() == typeof(Vector4))
{
Vector4 vector = (Vector4)value;
stringValue =
vector.X.ToString("G", CultureInfo.InvariantCulture) + "," +
vector.Y.ToString("G", CultureInfo.InvariantCulture) + "," +
vector.Z.ToString("G", CultureInfo.InvariantCulture) + "," +
vector.W.ToString("G", CultureInfo.InvariantCulture);
}
else if (value.GetType() == typeof(Color))
{
Color color = (Color)value;
stringValue =
(color.R / 255.0f).ToString("G", CultureInfo.InvariantCulture) + "," +
(color.G / 255.0f).ToString("G", CultureInfo.InvariantCulture) + "," +
(color.B / 255.0f).ToString("G", CultureInfo.InvariantCulture) + "," +
(color.A / 255.0f).ToString("G", CultureInfo.InvariantCulture);
}
else else
{ {
stringValue = value.ToString(); stringValue = value.ToString();
@@ -289,6 +289,28 @@ namespace Barotrauma
return vector; return vector;
} }
public static Color ParseToColor(string stringColor, bool errorMessages = true)
{
string[] strComponents = stringColor.Split(',');
Color color = Color.White;
if (strComponents.Length < 3)
{
if (errorMessages) DebugConsole.ThrowError("Failed to parse the string \"" + stringColor + "\" to Color");
return Color.White;
}
float[] components = new float[4] { 1.0f, 1.0f, 1.0f, 1.0f };
for (int i = 0; i < 4 && i < strComponents.Length; i++)
{
float.TryParse(strComponents[i], NumberStyles.Float, CultureInfo.InvariantCulture, out components[i]);
}
return new Color(components[0], components[1], components[2], components[3]);
}
public static string Vector4ToString(Vector4 vector, string format = "G") public static string Vector4ToString(Vector4 vector, string format = "G")
{ {
return vector.X.ToString(format, CultureInfo.InvariantCulture) + "," + return vector.X.ToString(format, CultureInfo.InvariantCulture) + "," +