From 18d2b7939863ea50a10913c8d2703a95e22a41ec Mon Sep 17 00:00:00 2001 From: Daniel Willett Date: Wed, 2 Oct 2024 21:09:05 -0400 Subject: [PATCH] added some extra methods to IBuildable --- UncreatedWarfare/Buildables/IBuildable.cs | 168 ++++++++++++++++-- UncreatedWarfare/FOBs/FOB.cs | 3 - .../Phases/Flags/FlagActionPhaseLayout.cs | 4 +- UncreatedWarfare/Players/WarfarePlayer.cs | 6 - .../Plugins/WarfarePluginLoader.cs | 4 +- UncreatedWarfare/UncreatedWarfare.csproj | 11 +- .../Util/Transform/ITransformObject.cs | 10 -- UncreatedWarfare/Vehicles/VehicleService.cs | 2 +- UncreatedWarfare/Zones/ActiveZoneCluster.cs | 7 +- 9 files changed, 163 insertions(+), 52 deletions(-) diff --git a/UncreatedWarfare/Buildables/IBuildable.cs b/UncreatedWarfare/Buildables/IBuildable.cs index 434f2ae5..aad96057 100644 --- a/UncreatedWarfare/Buildables/IBuildable.cs +++ b/UncreatedWarfare/Buildables/IBuildable.cs @@ -1,22 +1,97 @@ using System; +using System.Runtime.CompilerServices; +using DanielWillett.ReflectionTools; +using Uncreated.Warfare.Util; namespace Uncreated.Warfare.Buildables; +/// +/// Any structure or barricade. Can be or . +/// [CannotApplyEqualityOperator] -public interface IBuildable : IEquatable +public interface IBuildable : IEquatable, ITransformObject { + /// + /// The instance ID of this buildable specific to it's buildable type. + /// uint InstanceId { get; } + + /// + /// If the buildable is a structure instead of a barricade. + /// bool IsStructure { get; } + + /// + /// If the buildable has been destroyed. + /// bool IsDead { get; } + + /// + /// The asset for the buildable. + /// ItemPlaceableAsset Asset { get; } + + /// + /// The transform of the buildable. + /// Transform Model { get; } - ulong Owner { get; } - ulong Group { get; } + + /// + /// The player that placed the buildable. + /// + CSteamID Owner { get; } + + /// + /// The group of the player that placed the buildable. + /// + CSteamID Group { get; } + + /// + /// The or of the buildable. + /// object Drop { get; } + + /// + /// The or of the buildable. + /// object Data { get; } + + /// + /// The or of the buildable. + /// + object Item { get; } + + /// + /// The ID used to refer to networked objects. + /// NetId NetId { get; } - Vector3 Position { get; } - Quaternion Rotation { get; } + + /// + /// Get as either a or . + /// + /// + TDrop GetDrop() where TDrop : class + { + return Drop as TDrop ?? throw new InvalidCastException($"This buildable's drop is not a {Accessor.ExceptionFormatter.Format()}."); + } + + /// + /// Get as either a or . + /// + /// + TData GetData() where TData : class + { + return Data as TData ?? throw new InvalidCastException($"This buildable's data is not a {Accessor.ExceptionFormatter.Format()}."); + } + + /// + /// Get as either a or . + /// + /// + TData GetItem() where TData : class + { + return Item as TData ?? throw new InvalidCastException($"This buildable's item is not a {Accessor.ExceptionFormatter.Format()}."); + } } [CannotApplyEqualityOperator] @@ -27,11 +102,32 @@ public class BuildableBarricade : IBuildable, IEquatable, IE public bool IsDead => Data.barricade.isDead; public ItemPlaceableAsset Asset => Drop.asset; public Transform Model => Drop.model == null || Data.barricade.isDead ? null! : Drop.model; // so you can use ? on it - public ulong Owner => Data.owner; - public ulong Group => Data.group; + public CSteamID Owner => Unsafe.As(ref Data.owner); + public CSteamID Group => Unsafe.As(ref Data.group); public NetId NetId => Drop.GetNetId(); - public Vector3 Position => Data.point; - public Quaternion Rotation => Data.rotation; + public Vector3 Position + { + get => Data.point; + set + { + GameThread.AssertCurrent(); + + BarricadeManager.ServerSetBarricadeTransform(Drop.model, value, Data.rotation); + } + } + + public Quaternion Rotation + { + get => Data.rotation; + set + { + GameThread.AssertCurrent(); + + BarricadeManager.ServerSetBarricadeTransform(Drop.model, Data.point, value); + } + } + + public BarricadeDrop Drop { get; } public BarricadeData Data { get; } public BuildableBarricade(BarricadeDrop drop) @@ -40,8 +136,21 @@ public BuildableBarricade(BarricadeDrop drop) Data = drop.GetServersideData(); } + public void SetPositionAndRotation(Vector3 position, Quaternion rotation) + { + GameThread.AssertCurrent(); + + BarricadeManager.ServerSetBarricadeTransform(Drop.model, position, rotation); + } + object IBuildable.Drop => Drop; object IBuildable.Data => Data; + object IBuildable.Item => Data.barricade; + Vector3 ITransformObject.Scale + { + get => Vector3.one; + set => throw new NotSupportedException(); + } public bool Equals(BarricadeDrop? other) => other is not null && other.instanceID == Drop.instanceID; public bool Equals(BuildableBarricade? other) => other is not null && other.Drop.instanceID == Drop.instanceID; @@ -58,21 +167,54 @@ public class BuildableStructure : IBuildable, IEquatable, IE public bool IsDead => Data.structure.isDead; public ItemPlaceableAsset Asset => Drop.asset; public Transform Model => Drop.model == null || Data.structure.isDead ? null! : Drop.model; // so you can use ? on it - public ulong Owner => Data.owner; - public ulong Group => Data.group; + public CSteamID Owner => Unsafe.As(ref Data.owner); + public CSteamID Group => Unsafe.As(ref Data.group); public NetId NetId => Drop.GetNetId(); - public Vector3 Position => Data.point; - public Quaternion Rotation => Data.rotation; + public Vector3 Position + { + get => Data.point; + set + { + GameThread.AssertCurrent(); + + StructureManager.ServerSetStructureTransform(Drop.model, value, Data.rotation); + } + } + + public Quaternion Rotation + { + get => Data.rotation; + set + { + GameThread.AssertCurrent(); + + StructureManager.ServerSetStructureTransform(Drop.model, Data.point, value); + } + } public StructureDrop Drop { get; } public StructureData Data { get; } + public Structure Structure => Data.structure; public BuildableStructure(StructureDrop drop) { Drop = drop; Data = drop.GetServersideData(); } + public void SetPositionAndRotation(Vector3 position, Quaternion rotation) + { + GameThread.AssertCurrent(); + + StructureManager.ServerSetStructureTransform(Drop.model, position, rotation); + } + object IBuildable.Drop => Drop; object IBuildable.Data => Data; + object IBuildable.Item => Data.structure; + Vector3 ITransformObject.Scale + { + get => Vector3.one; + set => throw new NotSupportedException(); + } public bool Equals(StructureDrop? other) => other is not null && other.instanceID == Drop.instanceID; public bool Equals(BuildableStructure? other) => other is not null && other.Drop.instanceID == Drop.instanceID; diff --git a/UncreatedWarfare/FOBs/FOB.cs b/UncreatedWarfare/FOBs/FOB.cs index 5ce9b30c..2f026b61 100644 --- a/UncreatedWarfare/FOBs/FOB.cs +++ b/UncreatedWarfare/FOBs/FOB.cs @@ -255,8 +255,6 @@ object ICloneable.Clone() IReadOnlyList ITrackingProximity.ActiveObjects => Vehicles; - Matrix4x4 ITransformObject.WorldToLocal => transform.worldToLocalMatrix; - public Matrix4x4 LocalToWorld => throw new NotImplementedException(); public void SetPositionAndRotation(Vector3 position, Quaternion rotation) @@ -266,7 +264,6 @@ public void SetPositionAndRotation(Vector3 position, Quaternion rotation) public Matrix4x4 WorldToLocal => throw new NotImplementedException(); - Matrix4x4 ITransformObject.LocalToWorld => transform.localToWorldMatrix; BoundingSphere ISphereProximity.Sphere { get diff --git a/UncreatedWarfare/Layouts/Phases/Flags/FlagActionPhaseLayout.cs b/UncreatedWarfare/Layouts/Phases/Flags/FlagActionPhaseLayout.cs index c0a4c47b..4c30dceb 100644 --- a/UncreatedWarfare/Layouts/Phases/Flags/FlagActionPhaseLayout.cs +++ b/UncreatedWarfare/Layouts/Phases/Flags/FlagActionPhaseLayout.cs @@ -15,7 +15,6 @@ public class FlagActionPhaseLayout : IFlagRotationPhase { private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; - private readonly IPlayerService _playerService; private IList? _pathingResult; private ZoneStore _zoneStore; @@ -39,11 +38,10 @@ public class FlagActionPhaseLayout : IFlagRotationPhase /// public ActiveZoneCluster EndingTeam { get; private set; } - public FlagActionPhaseLayout(ILogger logger, IServiceProvider serviceProvider, IPlayerService playerService, IConfiguration config) + public FlagActionPhaseLayout(ILogger logger, IServiceProvider serviceProvider, IConfiguration config) { _logger = logger; _serviceProvider = serviceProvider; - _playerService = playerService; Configuration = config; } diff --git a/UncreatedWarfare/Players/WarfarePlayer.cs b/UncreatedWarfare/Players/WarfarePlayer.cs index c84a01e1..eef08d77 100644 --- a/UncreatedWarfare/Players/WarfarePlayer.cs +++ b/UncreatedWarfare/Players/WarfarePlayer.cs @@ -59,12 +59,6 @@ public Vector3 Position } } - /// - public Matrix4x4 WorldToLocal => Transform.worldToLocalMatrix; - - /// - public Matrix4x4 LocalToWorld => Transform.localToWorldMatrix; - /// /// If the player this object represents is currently online. Set to *after* the leave event is fired. /// diff --git a/UncreatedWarfare/Plugins/WarfarePluginLoader.cs b/UncreatedWarfare/Plugins/WarfarePluginLoader.cs index fde80e98..d9248992 100644 --- a/UncreatedWarfare/Plugins/WarfarePluginLoader.cs +++ b/UncreatedWarfare/Plugins/WarfarePluginLoader.cs @@ -129,7 +129,7 @@ internal void ConfigureServices(ContainerBuilder bldr) bldr.RegisterInstance(plugin) .As() - .Named(plugin.AssemblyName.Name) + .Named(plugin.AssemblyName.Name) .OwnedByLifetimeScope(); } } @@ -209,7 +209,7 @@ private object[] InjectParameters(ParameterInfo[] parameters, Type type, Contain int index = Array.FindIndex(ctors, x => x.IsDefinedSafe()); - if (index < 0) + if (index >= 0) { ConstructorInfo expectedCtor = ctors[index]; expectedParameters = expectedCtor.GetParameters(); diff --git a/UncreatedWarfare/UncreatedWarfare.csproj b/UncreatedWarfare/UncreatedWarfare.csproj index 7629dd5a..416d6241 100644 --- a/UncreatedWarfare/UncreatedWarfare.csproj +++ b/UncreatedWarfare/UncreatedWarfare.csproj @@ -225,16 +225,11 @@ - - - C:/SteamCMD/steamapps/common/U3DS - - - + - $(U3DSPath)/Unturned_Data/Managed + $(ServerPath)/Unturned_Data/Managed @@ -310,7 +305,7 @@ - + \ No newline at end of file diff --git a/UncreatedWarfare/Util/Transform/ITransformObject.cs b/UncreatedWarfare/Util/Transform/ITransformObject.cs index 695ac820..c76dec38 100644 --- a/UncreatedWarfare/Util/Transform/ITransformObject.cs +++ b/UncreatedWarfare/Util/Transform/ITransformObject.cs @@ -29,16 +29,6 @@ public interface ITransformObject /// Not able to set scale. Vector3 Scale { get; set; } - /// - /// A matrix transforming from world space to local space. - /// - Matrix4x4 WorldToLocal { get; } - - /// - /// A matrix transforming from local space to world space. - /// - Matrix4x4 LocalToWorld { get; } - /// /// Set the position and rotation at the same time. /// diff --git a/UncreatedWarfare/Vehicles/VehicleService.cs b/UncreatedWarfare/Vehicles/VehicleService.cs index 746f67b9..50191725 100644 --- a/UncreatedWarfare/Vehicles/VehicleService.cs +++ b/UncreatedWarfare/Vehicles/VehicleService.cs @@ -57,7 +57,7 @@ public async UniTask SpawnVehicleAsync(VehicleSpawnInfo spa Vector3 spawnPosition = spawner.Position + Vector3.up * VehicleSpawnOffset; - InteractableVehicle vehicle = await SpawnVehicleAsync(spawn.Vehicle, spawnPosition, spawnRotation, group: new CSteamID(spawner.Group), token: token); + InteractableVehicle vehicle = await SpawnVehicleAsync(spawn.Vehicle, spawnPosition, spawnRotation, group: spawner.Group, token: token); await UniTask.SwitchToMainThread(token); spawn.LinkVehicle(vehicle); diff --git a/UncreatedWarfare/Zones/ActiveZoneCluster.cs b/UncreatedWarfare/Zones/ActiveZoneCluster.cs index 1634fdb2..e54243ec 100644 --- a/UncreatedWarfare/Zones/ActiveZoneCluster.cs +++ b/UncreatedWarfare/Zones/ActiveZoneCluster.cs @@ -1,10 +1,8 @@ -using Microsoft.Extensions.DependencyInjection; -using System; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text.Json.Serialization; using Uncreated.Warfare.Players; -using Uncreated.Warfare.Players.Management; using Uncreated.Warfare.Proximity; using Uncreated.Warfare.Util; using Uncreated.Warfare.Util.List; @@ -17,7 +15,6 @@ namespace Uncreated.Warfare.Zones; public class ActiveZoneCluster : IDisposable { private readonly ZoneProximity[] _zones; - private readonly IPlayerService _playerService; private bool _disposed; private readonly TrackingList _players = new TrackingList(8); @@ -64,8 +61,6 @@ internal ActiveZoneCluster(ZoneProximity[] zones, IServiceProvider serviceProvid Players = new ReadOnlyTrackingList(_players); - _playerService = serviceProvider.GetRequiredService(); - // move primary to the front of the array for (int i = 1; i < zones.Length; ++i) {