diff --git a/VpSharp/VpSharp.csproj b/VpSharp/VpSharp.csproj index 9f3e932..87427f7 100644 --- a/VpSharp/VpSharp.csproj +++ b/VpSharp/VpSharp.csproj @@ -71,6 +71,7 @@ + diff --git a/VpSharp/src/Entities/VirtualParadiseModelObject.cs b/VpSharp/src/Entities/VirtualParadiseModelObject.cs index abc1937..d45ce26 100644 --- a/VpSharp/src/Entities/VirtualParadiseModelObject.cs +++ b/VpSharp/src/Entities/VirtualParadiseModelObject.cs @@ -96,20 +96,9 @@ public class VirtualParadiseModelObject : VirtualParadiseObject base.ExtractFromBuilder(builder); - if (modelObjectBuilder.Model.HasValue) - { - Model = modelObjectBuilder.Model.Value!; - } - - if (modelObjectBuilder.Description.HasValue) - { - Description = modelObjectBuilder.Description.Value!; - } - - if (modelObjectBuilder.Action.HasValue) - { - Action = modelObjectBuilder.Action.Value!; - } + Model = modelObjectBuilder.Model.ValueOr(Model); + Description = modelObjectBuilder.Description.ValueOr(Description); + Action = modelObjectBuilder.Action.ValueOr(Action); } /// diff --git a/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs b/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs index bdffc40..e5087ec 100644 --- a/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs +++ b/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs @@ -1,4 +1,5 @@ -using VpSharp.Internal; +using Optional; +using VpSharp.Internal; using static VpSharp.Internal.NativeAttributes.StringAttribute; using static VpSharp.Internal.NativeMethods; @@ -22,19 +23,19 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui /// Gets or sets the value of this object's Action field. /// /// The value of this object's Action field, or to leave unchanged. - public Optional Action { get; set; } + public Option Action { get; set; } /// /// Gets or sets the value of this object's Description field. /// /// The value of this object's Description field, or to leave unchanged. - public Optional Description { get; set; } + public Option Description { get; set; } /// /// Gets or sets the value of this object's Model field. /// /// The value of this object's Model field, or to leave unchanged. - public Optional Model { get; set; } + public Option Model { get; set; } internal override void ApplyChanges() { @@ -42,8 +43,8 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui nint handle = Client.NativeInstanceHandle; var targetObject = (VirtualParadiseModelObject)TargetObject; - vp_string_set(handle, ObjectModel, Model.HasValue ? Model.Value! : targetObject.Model); - vp_string_set(handle, ObjectDescription, Description.HasValue ? Description.Value! : targetObject.Description); - vp_string_set(handle, ObjectAction, Action.HasValue ? Action.Value! : targetObject.Action); + vp_string_set(handle, ObjectModel, Model.ValueOr(targetObject.Model)); + vp_string_set(handle, ObjectDescription, Description.ValueOr(targetObject.Description)); + vp_string_set(handle, ObjectAction, Action.ValueOr(targetObject.Action)); } } diff --git a/VpSharp/src/Entities/VirtualParadiseObject.cs b/VpSharp/src/Entities/VirtualParadiseObject.cs index baa0c8b..721d51c 100644 --- a/VpSharp/src/Entities/VirtualParadiseObject.cs +++ b/VpSharp/src/Entities/VirtualParadiseObject.cs @@ -240,35 +240,13 @@ public abstract class VirtualParadiseObject : IEquatable ArgumentNullException.ThrowIfNull(builder); Location location = Location; - Vector3d position = location.Position; - Rotation rotation = location.Rotation; - - if (builder.Position.HasValue) - { - position = builder.Position.Value; - } - - if (builder.Rotation.HasValue) - { - rotation = builder.Rotation.Value; - } + Vector3d position = builder.Position.ValueOr(location.Position); + Rotation rotation = builder.Rotation.ValueOr(location.Rotation); Location = new Location(location.World, position, rotation); - - if (builder.Data.HasValue) - { - Data = builder.Data.Value!.ToArray(); - } - - if (builder.ModificationTimestamp.HasValue) - { - ModificationTimestamp = builder.ModificationTimestamp.Value; - } - - if (builder.Owner.HasValue) - { - Owner = builder.Owner.Value!; - } + Data = builder.Data.ValueOr(Data).ToArray(); + ModificationTimestamp = builder.ModificationTimestamp.ValueOr(ModificationTimestamp); + Owner = builder.Owner.ValueOr(Owner); } /// diff --git a/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs b/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs index 34873de..538906d 100644 --- a/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs +++ b/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs @@ -1,4 +1,5 @@ -using VpSharp.Internal; +using Optional; +using VpSharp.Internal; using static VpSharp.Internal.NativeAttributes.DataAttribute; using static VpSharp.Internal.NativeAttributes.FloatAttribute; using static VpSharp.Internal.NativeAttributes.IntegerAttribute; @@ -32,7 +33,7 @@ public abstract class VirtualParadiseObjectBuilder /// This property may only be set during an object load, and will throw at /// any other point. /// - public Optional ModificationTimestamp { get; set; } + public Option ModificationTimestamp { get; set; } /// /// Gets or sets the owner of this object. @@ -42,21 +43,21 @@ public abstract class VirtualParadiseObjectBuilder /// This property may only be set during an object load, and will throw at /// any other point. /// - public Optional Owner { get; set; } + public Option Owner { get; set; } /// /// Gets or sets the position of the object. /// /// The position of the object, or to leave unchanged. - public Optional Position { get; set; } + public Option Position { get; set; } /// /// Gets or sets the rotation of the object. /// /// The rotation of the object, or to leave unchanged. - public Optional Rotation { get; set; } + public Option Rotation { get; set; } - internal Optional> Data { get; set; } + internal Option> Data { get; set; } private protected VirtualParadiseClient Client { get; } @@ -76,76 +77,49 @@ public abstract class VirtualParadiseObjectBuilder private void ApplyData() { nint handle = Client.NativeInstanceHandle; - if (Data.HasValue) - { - IReadOnlyList data = Data.Value!; - _ = vp_data_set(handle, ObjectData, data.Count, data.ToArray()); - } - else - { - _ = vp_data_set(handle, ObjectData, 0, Array.Empty()); - } + byte[] data = Data.ValueOr(ArraySegment.Empty).ToArray(); + _ = vp_data_set(handle, ObjectData, data.Length, data); } private void ApplyOwner() { nint handle = Client.NativeInstanceHandle; - if (Owner.HasValue) - { - if (Mode != ObjectBuilderMode.Load) - { - throw new InvalidOperationException("Owner can only be assigned during an object load."); - } - _ = vp_int_set(handle, ObjectUserId, Owner.Value!.Id); - } - else + if (Owner.HasValue && Mode != ObjectBuilderMode.Load) { - _ = vp_int_set(handle, ObjectUserId, TargetObject.Owner.Id); + throw new InvalidOperationException("Owner can only be assigned during an object load."); } + + VirtualParadiseUser oldOwner = TargetObject.Owner; + _ = vp_int_set(handle, ObjectUserId, Owner.ValueOr(oldOwner).Id); } private void ApplyModificationTimestamp() { nint handle = Client.NativeInstanceHandle; - if (ModificationTimestamp.HasValue) + if (ModificationTimestamp.HasValue && Mode != ObjectBuilderMode.Load) { - if (Mode != ObjectBuilderMode.Load) - { - throw new InvalidOperationException("Modification timestamp can only be assigned during an object load."); - } + throw new InvalidOperationException("Modification timestamp can only be assigned during an object load."); + } - _ = vp_int_set(handle, ObjectTime, (int)ModificationTimestamp.Value.ToUnixTimeSeconds()); - } - else - { - _ = vp_int_set(handle, ObjectTime, (int)TargetObject.ModificationTimestamp.ToUnixTimeSeconds()); - } + DateTimeOffset oldTimestamp = TargetObject.ModificationTimestamp; + _ = vp_int_set(handle, ObjectTime, (int)ModificationTimestamp.ValueOr(oldTimestamp).ToUnixTimeSeconds()); } private void ApplyPosition() { nint handle = Client.NativeInstanceHandle; - if (Position.HasValue) - { - (double x, double y, double z) = Position.Value; - _ = vp_double_set(handle, ObjectX, x); - _ = vp_double_set(handle, ObjectY, y); - _ = vp_double_set(handle, ObjectZ, z); - } - else if (Mode == ObjectBuilderMode.Create) + if (!Position.HasValue && Mode == ObjectBuilderMode.Create) { throw new ArgumentException("Position must be assigned when creating a new object."); } - else - { - (double x, double y, double z) = TargetObject.Location.Position; - _ = vp_double_set(handle, ObjectX, x); - _ = vp_double_set(handle, ObjectY, y); - _ = vp_double_set(handle, ObjectZ, z); - } + + (double x, double y, double z) = Position.ValueOr(TargetObject.Location.Position); + _ = vp_double_set(handle, ObjectX, x); + _ = vp_double_set(handle, ObjectY, y); + _ = vp_double_set(handle, ObjectZ, z); } private void ApplyRotation() @@ -154,24 +128,13 @@ public abstract class VirtualParadiseObjectBuilder if (!Rotation.HasValue && Mode == ObjectBuilderMode.Create) { - Rotation = VpSharp.Rotation.None; + Rotation = Option.Some(VpSharp.Rotation.None); } - if (Rotation.HasValue) - { - (double x, double y, double z, double angle) = Rotation.Value; - _ = vp_double_set(handle, ObjectRotationX, x); - _ = vp_double_set(handle, ObjectRotationY, y); - _ = vp_double_set(handle, ObjectRotationZ, z); - _ = vp_double_set(handle, ObjectRotationAngle, angle); - } - else - { - (double x, double y, double z, double angle) = TargetObject.Location.Rotation; - _ = vp_double_set(handle, ObjectRotationX, x); - _ = vp_double_set(handle, ObjectRotationY, y); - _ = vp_double_set(handle, ObjectRotationZ, z); - _ = vp_double_set(handle, ObjectRotationAngle, angle); - } + (double x, double y, double z, double angle) = Rotation.ValueOr(TargetObject.Location.Rotation); + _ = vp_double_set(handle, ObjectRotationX, x); + _ = vp_double_set(handle, ObjectRotationY, y); + _ = vp_double_set(handle, ObjectRotationZ, z); + _ = vp_double_set(handle, ObjectRotationAngle, angle); } } diff --git a/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs b/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs index 1d8933c..0b357eb 100644 --- a/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs +++ b/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs @@ -3,6 +3,7 @@ // ReSharper disable MemberCanBePrivate.Global using System.Drawing; +using Optional; using VpSharp.Internal; using VpSharp.Internal.Attributes; using VpSharp.Internal.ValueConverters; @@ -29,7 +30,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The maximum acceleration, or to leave unchanged. [SerializationKey("acceleration_max")] [ValueConverter(typeof(Vector3dConverter))] - public Optional AccelerationMax { get; set; } + public Option AccelerationMax { get; set; } /// /// Gets or sets the minimum acceleration. @@ -37,7 +38,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The minimum acceleration, or to leave unchanged. [SerializationKey("acceleration_min")] [ValueConverter(typeof(Vector3dConverter))] - public Optional AccelerationMin { get; set; } + public Option AccelerationMin { get; set; } /// /// Gets or sets the blend mode. @@ -45,7 +46,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The blend mode, or to leave unchanged. [SerializationKey("blend")] [ValueConverter(typeof(StringToEnumConverter))] - public Optional BlendMode { get; set; } + public Option BlendMode { get; set; } /// /// Gets or sets the maximum color. @@ -53,7 +54,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The maximum color, or to leave unchanged. [SerializationKey("color_max")] [ValueConverter(typeof(HexToColorConverter))] - public Optional ColorMax { get; set; } + public Option ColorMax { get; set; } /// /// Gets or sets the minimum color. @@ -61,7 +62,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The minimum color, or to leave unchanged. [SerializationKey("color_min")] [ValueConverter(typeof(HexToColorConverter))] - public Optional ColorMin { get; set; } + public Option ColorMin { get; set; } /// /// Gets or sets the emitter lifespan. @@ -69,7 +70,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The emitter lifespan, or to leave unchanged. [SerializationKey("emitter_lifespan")] [ValueConverter(typeof(MillisecondToTimeSpanConverter))] - public Optional EmitterLifespan { get; set; } + public Option EmitterLifespan { get; set; } /// /// Gets or sets a value indicating whether this emitter interpolates its values. @@ -80,14 +81,14 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// [SerializationKey("interpolate")] [ValueConverter(typeof(IntToBoolConverter))] - public Optional Interpolate { get; set; } + public Option Interpolate { get; set; } /// /// Gets or sets the opacity. /// /// The opacity, or to leave unchanged. [SerializationKey("opacity")] - public Optional Opacity { get; set; } + public Option Opacity { get; set; } /// /// Gets or sets the particle lifespan. @@ -95,7 +96,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The particle lifespan, or to leave unchanged. [SerializationKey("particle_lifespan")] [ValueConverter(typeof(MillisecondToTimeSpanConverter))] - public Optional ParticleLifespan { get; set; } + public Option ParticleLifespan { get; set; } /// /// Gets or sets the particle type. @@ -103,14 +104,14 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The particle type, or to leave unchanged. [SerializationKey("particle_type")] [ValueConverter(typeof(StringToEnumConverter))] - public Optional ParticleType { get; set; } + public Option ParticleType { get; set; } /// /// Gets or sets the release count. /// /// The release count, or to leave unchanged. [SerializationKey("release_count")] - public Optional ReleaseCount { get; set; } + public Option ReleaseCount { get; set; } /// /// Gets or sets the release time. @@ -118,7 +119,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The release time, or to leave unchanged. [SerializationKey("release_time")] [ValueConverter(typeof(MillisecondToTimeSpanConverter))] - public Optional ReleaseTime { get; set; } + public Option ReleaseTime { get; set; } /// /// Gets or sets the maximum size. @@ -126,7 +127,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The maximum size, or to leave unchanged. [SerializationKey("size_max")] [ValueConverter(typeof(Vector3dConverter))] - public Optional SizeMax { get; set; } + public Option SizeMax { get; set; } /// /// Gets or sets the minimum size. @@ -134,7 +135,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The minimum size, or to leave unchanged. [SerializationKey("size_min")] [ValueConverter(typeof(Vector3dConverter))] - public Optional SizeMin { get; set; } + public Option SizeMin { get; set; } /// /// Gets or sets the maximum speed. @@ -142,7 +143,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The maximum speed, or to leave unchanged. [SerializationKey("speed_max")] [ValueConverter(typeof(Vector3dConverter))] - public Optional SpeedMax { get; set; } + public Option SpeedMax { get; set; } /// /// Gets or sets the minimum speed. @@ -150,7 +151,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The minimum speed, or to leave unchanged. [SerializationKey("speed_min")] [ValueConverter(typeof(Vector3dConverter))] - public Optional SpeedMin { get; set; } + public Option SpeedMin { get; set; } /// /// Gets or sets the maximum spin. @@ -158,7 +159,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The maximum spin, or to leave unchanged. [SerializationKey("spin_max")] [ValueConverter(typeof(Vector3dConverter))] - public Optional SpinMax { get; set; } + public Option SpinMax { get; set; } /// /// Gets or sets the minimum spin. @@ -166,7 +167,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The minimum spin, or to leave unchanged. [SerializationKey("spin_min")] [ValueConverter(typeof(Vector3dConverter))] - public Optional SpinMin { get; set; } + public Option SpinMin { get; set; } /// /// Gets or sets the maximum start angle. @@ -174,7 +175,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The maximum start angle, or to leave unchanged. [SerializationKey("start_angle_max")] [ValueConverter(typeof(Vector3dConverter))] - public Optional StartAngleMax { get; set; } + public Option StartAngleMax { get; set; } /// /// Gets or sets the minimum start angle. @@ -182,7 +183,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The minimum start angle, or to leave unchanged. [SerializationKey("start_angle_min")] [ValueConverter(typeof(Vector3dConverter))] - public Optional StartAngleMin { get; set; } + public Option StartAngleMin { get; set; } /// /// Gets or sets the maximum volume. @@ -190,7 +191,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The maximum volume, or to leave unchanged. [SerializationKey("volume_max")] [ValueConverter(typeof(Vector3dConverter))] - public Optional VolumeMax { get; set; } + public Option VolumeMax { get; set; } /// /// Gets or sets the minimum volume. @@ -198,28 +199,28 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// The minimum volume, or to leave unchanged. [SerializationKey("volume_min")] [ValueConverter(typeof(Vector3dConverter))] - public Optional VolumeMin { get; set; } + public Option VolumeMin { get; set; } /// /// Gets or sets the tag. /// /// The tag, or to leave unchanged. [SerializationKey("tag")] - public Optional Tag { get; set; } + public Option Tag { get; set; } /// /// Gets or sets the texture. /// /// The texture, or to leave unchanged. [SerializationKey("texture")] - public Optional Texture { get; set; } + public Option Texture { get; set; } /// /// Sets the maximum volume. /// /// The maximum volume, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithAccelerationMax(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithAccelerationMax(Option value) { AccelerationMax = value; return this; @@ -230,7 +231,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The minimum acceleration, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithAccelerationMin(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithAccelerationMin(Option value) { AccelerationMin = value; return this; @@ -241,7 +242,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The blend mode, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithBlendMode(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithBlendMode(Option value) { BlendMode = value; return this; @@ -252,7 +253,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The maximum color, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithColorMax(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithColorMax(Option value) { ColorMax = value; return this; @@ -263,7 +264,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The minimum color, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithColorMin(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithColorMin(Option value) { ColorMin = value; return this; @@ -274,7 +275,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The emitter lifespan, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithEmitterLifespan(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithEmitterLifespan(Option value) { EmitterLifespan = value; return this; @@ -288,7 +289,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// to leave unchanged. /// /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithInterpolation(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithInterpolation(Option value) { Interpolate = value; return this; @@ -299,7 +300,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The opacity, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithOpacity(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithOpacity(Option value) { Opacity = value; return this; @@ -310,7 +311,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The particle lifespan, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithParticleLifespan(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithParticleLifespan(Option value) { ParticleLifespan = value; return this; @@ -321,7 +322,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The particle type, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithParticleType(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithParticleType(Option value) { ParticleType = value; return this; @@ -332,7 +333,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The release count, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithReleaseCount(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithReleaseCount(Option value) { ReleaseCount = value; return this; @@ -343,7 +344,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The release time, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithReleaseTime(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithReleaseTime(Option value) { ReleaseTime = value; return this; @@ -354,7 +355,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The maximum size, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithSizeMax(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithSizeMax(Option value) { SizeMax = value; return this; @@ -365,7 +366,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The minimum size, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithSizeMin(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithSizeMin(Option value) { SizeMin = value; return this; @@ -376,7 +377,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The maximum speed, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithSpeedMax(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithSpeedMax(Option value) { SpeedMax = value; return this; @@ -387,7 +388,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The minimum speed, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithSpeedMin(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithSpeedMin(Option value) { SpeedMin = value; return this; @@ -398,7 +399,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The maximum spin, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithSpinMax(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithSpinMax(Option value) { SpinMax = value; return this; @@ -409,7 +410,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The minimum spin, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithSpinMin(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithSpinMin(Option value) { SpinMin = value; return this; @@ -420,7 +421,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The maximum start angle, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithStartAngleMax(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithStartAngleMax(Option value) { StartAngleMax = value; return this; @@ -431,7 +432,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The minimum start angle, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithStartAngleMin(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithStartAngleMin(Option value) { StartAngleMin = value; return this; @@ -442,7 +443,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The maximum volume, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithVolumeMax(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithVolumeMax(Option value) { VolumeMax = value; return this; @@ -453,7 +454,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The minimum volume, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithVolumeMin(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithVolumeMin(Option value) { VolumeMin = value; return this; @@ -464,7 +465,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The tag, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithTag(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithTag(Option value) { Tag = value; return this; @@ -475,7 +476,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The texture, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithTexture(Optional value) + public VirtualParadiseParticleEmitterObjectBuilder WithTexture(Option value) { Texture = value; return this; diff --git a/VpSharp/src/Optional.cs b/VpSharp/src/Optional.cs deleted file mode 100644 index 1a9fe55..0000000 --- a/VpSharp/src/Optional.cs +++ /dev/null @@ -1,108 +0,0 @@ -namespace VpSharp; - -#pragma warning disable CA1716 -#pragma warning disable CA2225 - -/// -/// Represents an optional value. -/// -/// The type of the value. -public readonly struct Optional : IEquatable> -{ - private readonly T? _value; - - /// - /// Initializes a new instance of the struct. - /// - /// - public Optional(T? value) - { - HasValue = true; - _value = value; - } - - /// - /// Gets a value indicating whether this has a value. - /// - /// if a value is defined; otherwise, . - public bool HasValue { get; } - - /// - /// Gets the underlying value of this optional. - /// - /// The value. - public T? Value - { - get - { - if (!HasValue) - { - throw new InvalidOperationException("Cannot access the value of a valueless optional."); - } - - return _value; - } - } - - /// - /// Returns a value indicating whether two instances of are equal. - /// - /// The first . - /// The second . - /// if the two instances are equal; otherwise, . - public static bool operator ==(Optional left, Optional right) - { - return left.Equals(right); - } - - /// - /// Returns a value indicating whether two instances of are not equal. - /// - /// The first . - /// The second . - /// if the two instances are not equal; otherwise, . - public static bool operator !=(Optional left, Optional right) - { - return !left.Equals(right); - } - - /// - /// Implicitly converts a value to a new instance of . - /// - /// A new instance of , wrapping . - public static implicit operator Optional(T? value) - { - return new Optional(value); - } - - /// - /// Implicitly converts a value to a new instance of . - /// - /// A new instance of , wrapping . - public static explicit operator T?(Optional value) - { - return value.Value; - } - - /// - /// Returns a value indicating whether this and another are equal. - /// - /// The other . - /// if the two instances are equal; otherwise, . - public bool Equals(Optional other) - { - return HasValue == other.HasValue && EqualityComparer.Default.Equals(Value, other._value); - } - - /// - public override bool Equals(object? obj) - { - return obj is Optional other && Equals(other); - } - - /// - public override int GetHashCode() - { - return HashCode.Combine(HasValue, Value); - } -} diff --git a/VpSharp/src/WorldSettingsBuilder.cs b/VpSharp/src/WorldSettingsBuilder.cs index da4db84..92e1098 100644 --- a/VpSharp/src/WorldSettingsBuilder.cs +++ b/VpSharp/src/WorldSettingsBuilder.cs @@ -1,5 +1,6 @@ using System.Drawing; using System.Numerics; +using Optional; using VpSharp.Internal; using VpSharp.Internal.Attributes; using VpSharp.Internal.ValueConverters; @@ -25,28 +26,28 @@ public sealed class WorldSettingsBuilder /// /// if the debug menu is enabled; otherwise, . [SerializationKey("allow_debug_menu")] - public bool? AllowDebugMenu { get; set; } + public Option AllowDebugMenu { get; set; } /// /// Gets or sets a value indicating whether flying is enabled. /// /// if flying is enabled; otherwise, . [SerializationKey("allow_flight")] - public bool? AllowFlight { get; set; } + public Option AllowFlight { get; set; } /// /// Gets or sets a value indicating whether pass-through is enabled. /// /// if pass-through is enabled; otherwise, . [SerializationKey("allow_passthrough")] - public bool? AllowPassThrough { get; set; } + public Option AllowPassThrough { get; set; } /// /// Gets or sets the name of the avatar list file in the object path. /// /// The name of the avatar list file in the object path.. [SerializationKey("avatar")] - public string? AvatarsFile { get; set; } + public Option AvatarsFile { get; set; } /// /// Gets or sets the entry position of the world, also known as "landing zone" (LZ) or "ground zero" (GZ). @@ -54,7 +55,7 @@ public sealed class WorldSettingsBuilder /// The entry position of the world. [SerializationKey("entry_point")] [ValueConverter(typeof(Vector4ToVector3Converter))] - public Vector3? EntryPosition { get; set; } + public Option EntryPosition { get; set; } /// /// Gets or sets the entry yaw of the world. @@ -62,28 +63,28 @@ public sealed class WorldSettingsBuilder /// The entry yaw of the world. [SerializationKey("entry_point")] [ValueConverter(typeof(VectorToNthComponentConverter), 4)] - public float? EntryYaw { get; set; } + public Option EntryYaw { get; set; } /// /// Gets or sets the camera's far-plane distance in decameters. /// /// The camera's far-plane distance, in decameters. [SerializationKey("farplane")] - public float? FarPlane { get; set; } + public Option FarPlane { get; set; } /// /// Gets or sets the camera's field of view. /// /// The camera's field of view. [SerializationKey("fov")] - public float? FieldOfView { get; set; } + public Option FieldOfView { get; set; } /// /// Gets or sets the distance at which fog begins. /// /// The distance at which fog begins. [SerializationKey("fog_begin")] - public float? FogBegin { get; set; } + public Option FogBegin { get; set; } /// /// Gets or sets the fog color. @@ -91,21 +92,21 @@ public sealed class WorldSettingsBuilder /// The fog color. [SerializationKey("fog_color")] [ValueConverter(typeof(HexToColorConverter))] - public Color? FogColor { get; set; } + public Option FogColor { get; set; } /// /// Gets or sets the density of the fog. /// /// The density of the fog. [SerializationKey("fog_density")] - public float? FogDensity { get; set; } + public Option FogDensity { get; set; } /// /// Gets or sets the distance at which fog ends. /// /// The distance at which fog ends. [SerializationKey("fog_end")] - public float? FogEnd { get; set; } + public Option FogEnd { get; set; } /// /// Gets or sets the fog mode. @@ -113,63 +114,63 @@ public sealed class WorldSettingsBuilder /// The fog mode. [SerializationKey("fog_mode")] [ValueConverter(typeof(StringToEnumConverter))] - public FogMode? FogMode { get; set; } + public Option FogMode { get; set; } /// /// Gets or sets the ground object. /// /// The ground object. This value will be empty if is . [SerializationKey("ground")] - public string? Ground { get; set; } + public Option Ground { get; set; } /// /// Gets or sets a value indicating whether the ground object specified by should repeat. /// /// if the ground should repeat; otherwise, . [SerializationKey("groundrepeats")] - public bool? GroundRepeats { get; set; } + public Option GroundRepeats { get; set; } /// /// Gets or sets the camera's near-plane distance in decameters. /// /// The camera's near-plane distance, in decameters. [SerializationKey("nearplane")] - public float? NearPlane { get; set; } + public Option NearPlane { get; set; } /// /// Gets or sets the password for extracting password-protected ZIP files from the object path. /// /// The password for extracting password-protected ZIP files from the object path. [SerializationKey("objectpassword")] - public string? ObjectPassword { get; set; } + public Option ObjectPassword { get; set; } /// /// Gets or sets the object path. /// /// The object path. [SerializationKey("objectpath")] - public string? ObjectPath { get; set; } + public Option ObjectPath { get; set; } /// /// Gets or sets a value indicating whether per-pixel lighting is recommended. /// /// if per-pixel lighting is recommended; otherwise, . [SerializationKey("recommended_per_pixel_lighting")] - public bool? PerPixelLightingRecommended { get; set; } + public Option PerPixelLightingRecommended { get; set; } /// /// Gets or sets the speed at which users run. /// /// The speed at which users run. [SerializationKey("run_speed")] - public float? RunSpeed { get; set; } + public Option RunSpeed { get; set; } /// /// Gets or sets a value indicating whether gradient sky is enabled. /// /// if gradient sky is enabled; otherwise, . [SerializationKey("sky")] - public bool? Sky { get; set; } + public Option Sky { get; set; } /// /// Gets or sets the color for the first sky cloud layer. @@ -177,7 +178,7 @@ public sealed class WorldSettingsBuilder /// The color for the first sky cloud layer. [SerializationKey("sky_clouds1_color")] [ValueConverter(typeof(Vector4ToColorConverter))] - public ColorF? SkyClouds1Color { get; set; } + public Option SkyClouds1Color { get; set; } /// /// Gets or sets the color for the second sky cloud layer. @@ -185,7 +186,7 @@ public sealed class WorldSettingsBuilder /// The color for the second sky cloud layer. [SerializationKey("sky_clouds2_color")] [ValueConverter(typeof(Vector4ToColorConverter))] - public ColorF? SkyClouds2Color { get; set; } + public Option SkyClouds2Color { get; set; } /// /// Gets or sets the scale for the first sky cloud layer. @@ -193,7 +194,7 @@ public sealed class WorldSettingsBuilder /// The scale for the first sky cloud layer. [SerializationKey("sky_clouds1_scale")] [ValueConverter(typeof(Vector2Converter))] - public Vector2? SkyClouds1Scale { get; set; } + public Option SkyClouds1Scale { get; set; } /// /// Gets or sets the scale for the second sky cloud layer. @@ -201,21 +202,21 @@ public sealed class WorldSettingsBuilder /// The scale for the second sky cloud layer. [SerializationKey("sky_clouds2_scale")] [ValueConverter(typeof(Vector2Converter))] - public Vector2? SkyClouds2Scale { get; set; } + public Option SkyClouds2Scale { get; set; } /// /// Gets or sets the texture for the first sky cloud layer. /// /// The texture for the first sky cloud layer. [SerializationKey("sky_clouds1")] - public string? SkyClouds1Texture { get; set; } + public Option SkyClouds1Texture { get; set; } /// /// Gets or sets the texture for the second sky cloud layer. /// /// The texture for the second sky cloud layer. [SerializationKey("sky_clouds2")] - public string? SkyClouds2Texture { get; set; } + public Option SkyClouds2Texture { get; set; } /// /// Gets or sets the velocity for the first sky cloud layer. @@ -223,7 +224,7 @@ public sealed class WorldSettingsBuilder /// The velocity for the first sky cloud layer. [SerializationKey("sky_clouds1_velocity")] [ValueConverter(typeof(Vector2Converter))] - public Vector2? SkyClouds1Velocity { get; set; } + public Option SkyClouds1Velocity { get; set; } /// /// Gets or sets the velocity for the second sky cloud layer. @@ -231,7 +232,7 @@ public sealed class WorldSettingsBuilder /// The velocity for the second sky cloud layer. [SerializationKey("sky_clouds2_velocity")] [ValueConverter(typeof(Vector2Converter))] - public Vector2? SkyClouds2Velocity { get; set; } + public Option SkyClouds2Velocity { get; set; } /// /// Gets or sets the first sky gradient color. @@ -239,7 +240,7 @@ public sealed class WorldSettingsBuilder /// The first sky gradient color. [SerializationKey("sky_color1")] [ValueConverter(typeof(Vector4ToColorConverter))] - public ColorF? SkyColor1 { get; set; } + public Option SkyColor1 { get; set; } /// /// Gets or sets the second sky gradient color. @@ -247,7 +248,7 @@ public sealed class WorldSettingsBuilder /// The second sky gradient color. [SerializationKey("sky_color2")] [ValueConverter(typeof(Vector4ToColorConverter))] - public ColorF? SkyColor2 { get; set; } + public Option SkyColor2 { get; set; } /// /// Gets or sets the third sky gradient color. @@ -255,7 +256,7 @@ public sealed class WorldSettingsBuilder /// The third sky gradient color. [SerializationKey("sky_color3")] [ValueConverter(typeof(Vector4ToColorConverter))] - public ColorF? SkyColor3 { get; set; } + public Option SkyColor3 { get; set; } /// /// Gets or sets the fourth sky gradient color. @@ -263,7 +264,7 @@ public sealed class WorldSettingsBuilder /// The fourth sky gradient color. [SerializationKey("sky_color4")] [ValueConverter(typeof(Vector4ToColorConverter))] - public ColorF? SkyColor4 { get; set; } + public Option SkyColor4 { get; set; } /// /// Gets or sets the sky color space. @@ -271,21 +272,21 @@ public sealed class WorldSettingsBuilder /// The sky color space. [SerializationKey("sky_srgb_colors")] [ValueConverter(typeof(IntToEnumConverter))] - public ColorSpace? SkyColorSpace { get; set; } + public Option SkyColorSpace { get; set; } /// /// Gets or sets the skybox texture. /// /// The skybox texture. [SerializationKey("skybox")] - public string? Skybox { get; set; } + public Option Skybox { get; set; } /// /// Gets or sets the file extension for skybox textures. /// /// The file extension for skybox textures. [SerializationKey("skybox_extension")] - public string? SkyboxExtension { get; set; } + public Option SkyboxExtension { get; set; } /// /// Gets or sets a value indicating whether the X axis of skybox textures should be swapped. @@ -295,49 +296,49 @@ public sealed class WorldSettingsBuilder /// /// If , _lf and _rt faces are swapped. [SerializationKey("skybox_extension")] - public bool? SkyboxSwapX { get; set; } + public Option SkyboxSwapX { get; set; } /// /// Gets or sets a value indicating whether terrain is enabled. /// /// if terrain is enabled; otherwise, . [SerializationKey("terrain")] - public bool? Terrain { get; set; } + public Option Terrain { get; set; } /// /// Gets or sets the vertical offset for the terrain. /// /// The vertical offset for the terrain. [SerializationKey("terrainoffset")] - public float? TerrainOffset { get; set; } + public Option TerrainOffset { get; set; } /// /// Gets or sets the scale factor for the terrain grid. /// /// The scale factor for the terrain grid. [SerializationKey("terrainscale")] - public float? TerrainScale { get; set; } + public Option TerrainScale { get; set; } /// /// Gets or sets the speed at which users walk. /// /// The speed at which users walk. [SerializationKey("walk_speed")] - public float? WalkSpeed { get; set; } + public Option WalkSpeed { get; set; } /// /// Gets or sets the URL of the in-world web overlay. /// /// The URL of the in-world web overlay. [SerializationKey("web_overlay")] - public string? WebOverlay { get; set; } + public Option WebOverlay { get; set; } /// /// Gets or sets the welcome message. /// /// The welcome message. [SerializationKey("welcome")] - public string? WelcomeMessage { get; set; } + public Option WelcomeMessage { get; set; } /// /// Gets or sets the ambient light color of the world. @@ -345,7 +346,7 @@ public sealed class WorldSettingsBuilder /// The ambient light color. [SerializationKey("worldlight_ambient")] [ValueConverter(typeof(Vector3ToColorConverter))] - public ColorF? WorldLightAmbient { get; set; } + public Option WorldLightAmbient { get; set; } /// /// Gets or sets the diffuse light color of the world. @@ -353,7 +354,7 @@ public sealed class WorldSettingsBuilder /// The diffuse light color. [SerializationKey("worldlight_diffuse")] [ValueConverter(typeof(Vector3ToColorConverter))] - public ColorF? WorldLightDiffuse { get; set; } + public Option WorldLightDiffuse { get; set; } /// /// Gets or sets the world light position. @@ -361,7 +362,7 @@ public sealed class WorldSettingsBuilder /// The world light position. [SerializationKey("worldlight_position")] [ValueConverter(typeof(Vector3Converter))] - public Vector3? WorldLightPosition { get; set; } + public Option WorldLightPosition { get; set; } /// /// Gets or sets the specular light color of the world. @@ -369,14 +370,14 @@ public sealed class WorldSettingsBuilder /// The specular light color. [SerializationKey("worldlight_specular")] [ValueConverter(typeof(Vector3ToColorConverter))] - public ColorF? WorldLightSpecular { get; set; } + public Option WorldLightSpecular { get; set; } /// /// Gets or sets the name of the world. /// /// The name of the world. [SerializationKey("worldname")] - public string? WorldName { get; set; } + public Option WorldName { get; set; } internal void SendChanges() {