Enable nullability in Content.Shared (#3626)

* Enable nullability in Content.Shared

* Fix null errors in server

* aye github i swear on me mom
This commit is contained in:
DrSmugleaf
2021-03-15 21:55:49 +01:00
committed by GitHub
parent 04201e944c
commit 6f012cb9ad
31 changed files with 167 additions and 117 deletions

View File

@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Robust.Shared.Prototypes;
using System.Diagnostics.CodeAnalysis;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -12,12 +12,13 @@ namespace Content.Shared.Construction
{
[DataField("actions", serverOnly: true)]
private List<IGraphAction> _actions = new();
[DataField("edges")]
private List<ConstructionGraphEdge> _edges = new();
[ViewVariables]
[DataField("node")]
public string Name { get; private set; }
[DataField("node", required: true)]
public string Name { get; private set; } = default!;
[ViewVariables]
public IReadOnlyList<ConstructionGraphEdge> Edges => _edges;
@@ -27,9 +28,9 @@ namespace Content.Shared.Construction
[ViewVariables]
[DataField("entity")]
public string Entity { get; private set; }
public string? Entity { get; private set; }
public ConstructionGraphEdge GetEdge(string target)
public ConstructionGraphEdge? GetEdge(string target)
{
foreach (var edge in _edges)
{
@@ -39,5 +40,10 @@ namespace Content.Shared.Construction
return null;
}
public bool TryGetEdge(string target, [NotNullWhen(true)] out ConstructionGraphEdge? edge)
{
return (edge = GetEdge(target)) != null;
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
@@ -12,8 +13,8 @@ namespace Content.Shared.Construction
public class ConstructionGraphPrototype : IPrototype, ISerializationHooks
{
private readonly Dictionary<string, ConstructionGraphNode> _nodes = new();
private readonly Dictionary<ValueTuple<string, string>, ConstructionGraphNode[]> _paths = new();
private readonly Dictionary<string, Dictionary<ConstructionGraphNode, ConstructionGraphNode>> _pathfinding = new();
private readonly Dictionary<ValueTuple<string, string>, ConstructionGraphNode[]?> _paths = new();
private readonly Dictionary<string, Dictionary<ConstructionGraphNode, ConstructionGraphNode?>> _pathfinding = new();
[ViewVariables]
[field: DataField("id", required: true)]
@@ -21,7 +22,7 @@ namespace Content.Shared.Construction
[ViewVariables]
[field: DataField("start")]
public string Start { get; }
public string? Start { get; }
[DataField("graph", priority: 0)]
private List<ConstructionGraphNode> _graph = new();
@@ -35,20 +36,30 @@ namespace Content.Shared.Construction
foreach (var graphNode in _graph)
{
if (string.IsNullOrEmpty(graphNode.Name))
{
throw new InvalidDataException($"Name of graph node is null in construction graph {ID}!");
}
_nodes[graphNode.Name] = graphNode;
}
if(string.IsNullOrEmpty(Start) || !_nodes.ContainsKey(Start))
if (string.IsNullOrEmpty(Start) || !_nodes.ContainsKey(Start))
throw new InvalidDataException($"Starting node for construction graph {ID} is null, empty or invalid!");
}
public ConstructionGraphEdge Edge(string startNode, string nextNode)
public ConstructionGraphEdge? Edge(string startNode, string nextNode)
{
var start = _nodes[startNode];
return start.GetEdge(nextNode);
}
public ConstructionGraphNode[] Path(string startNode, string finishNode)
public bool TryPath(string startNode, string finishNode, [NotNullWhen(true)] out ConstructionGraphNode[]? path)
{
return (path = Path(startNode, finishNode)) != null;
}
public ConstructionGraphNode[]? Path(string startNode, string finishNode)
{
var tuple = new ValueTuple<string, string>(startNode, finishNode);
@@ -57,7 +68,7 @@ namespace Content.Shared.Construction
// Get graph given the current start.
Dictionary<ConstructionGraphNode, ConstructionGraphNode> pathfindingForStart;
Dictionary<ConstructionGraphNode, ConstructionGraphNode?> pathfindingForStart;
if (_pathfinding.ContainsKey(startNode))
{
pathfindingForStart = _pathfinding[startNode];
@@ -76,8 +87,6 @@ namespace Content.Shared.Construction
var path = new List<ConstructionGraphNode>();
while (current != start)
{
path.Add(current);
// No path.
if (current == null || !pathfindingForStart.ContainsKey(current))
{
@@ -86,6 +95,8 @@ namespace Content.Shared.Construction
return null;
}
path.Add(current);
current = pathfindingForStart[current];
}
@@ -97,13 +108,13 @@ namespace Content.Shared.Construction
/// Uses breadth first search for pathfinding.
/// </summary>
/// <param name="start"></param>
private Dictionary<ConstructionGraphNode, ConstructionGraphNode> PathsForStart(string start)
private Dictionary<ConstructionGraphNode, ConstructionGraphNode?> PathsForStart(string start)
{
// TODO: Make this use A* or something, although it's not that important.
var startNode = _nodes[start];
var frontier = new Queue<ConstructionGraphNode>();
var cameFrom = new Dictionary<ConstructionGraphNode, ConstructionGraphNode>();
var cameFrom = new Dictionary<ConstructionGraphNode, ConstructionGraphNode?>();
frontier.Enqueue(startNode);
cameFrom[startNode] = null;

View File

@@ -10,6 +10,7 @@
<Configurations>Release;Debug</Configurations>
<Platforms>AnyCPU</Platforms>
<WarningsAsErrors>nullable</WarningsAsErrors>
<Nullable>enable</Nullable>
</PropertyGroup>
<Import Project="..\RobustToolbox\MSBuild\Robust.DefineConstants.targets" />
<ItemGroup>

View File

@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Movement;
using Content.Shared.GameObjects.EntitySystems.EffectBlocker;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Reflection;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
@@ -16,16 +14,14 @@ namespace Content.Shared.GameObjects.Components.Inventory
{
public abstract class SharedInventoryComponent : Component, IMoveSpeedModifier
{
// ReSharper disable UnassignedReadonlyField
[Dependency] protected readonly IReflectionManager ReflectionManager;
[Dependency] protected readonly IDynamicTypeFactory DynamicTypeFactory;
// ReSharper restore UnassignedReadonlyField
[Dependency] protected readonly IReflectionManager ReflectionManager = default!;
[Dependency] protected readonly IDynamicTypeFactory DynamicTypeFactory = default!;
public sealed override string Name => "Inventory";
public sealed override uint? NetID => ContentNetIDs.STORAGE;
[ViewVariables]
protected Inventory InventoryInstance { get; private set; }
protected Inventory InventoryInstance { get; private set; } = default!;
[ViewVariables]
[DataField("Template")]
@@ -42,7 +38,7 @@ namespace Content.Shared.GameObjects.Components.Inventory
{
var type = ReflectionManager.LooseGetType(_templateName);
DebugTools.Assert(type != null);
InventoryInstance = DynamicTypeFactory.CreateInstance<Inventory>(type);
InventoryInstance = DynamicTypeFactory.CreateInstance<Inventory>(type!);
}
/// <returns>true if the item is equipped to an equip slot (NOT inside an equipped container

View File

@@ -40,13 +40,13 @@ namespace Content.Shared.GameObjects.Components.Materials
[DataDefinition]
public class MaterialDataEntry : ISerializationHooks
{
public object Key;
public object Key = default!;
[DataField("key")]
public string StringKey;
[DataField("key", required: true)]
public string StringKey = default!;
[DataField("mat")]
public string Value;
[DataField("mat", required: true)]
public string Value = default!;
void ISerializationHooks.AfterDeserialization()
{

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
@@ -22,10 +21,10 @@ namespace Content.Shared.GameObjects.Components
{
public float Pressure;
public float Temperature;
public GasEntry[] Gases;
public string Error = string.Empty;
public GasEntry[]? Gases;
public string? Error;
public GasAnalyzerBoundUserInterfaceState(float pressure, float temperature, GasEntry[] gases, string error = null)
public GasAnalyzerBoundUserInterfaceState(float pressure, float temperature, GasEntry[]? gases, string? error = null)
{
Pressure = pressure;
Temperature = temperature;

View File

@@ -76,7 +76,7 @@ namespace Content.Shared.GameObjects.Components
return new StackComponentState(Count, MaxCount);
}
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is not StackComponentState cast)
{

View File

@@ -4,10 +4,8 @@ using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Physics;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Broadphase;
@@ -40,7 +38,7 @@ namespace Content.Shared.GameObjects.EntitySystems
MapCoordinates origin,
MapCoordinates other,
int collisionMask = (int) CollisionGroup.Impassable,
Ignored predicate = null)
Ignored? predicate = null)
{
var dir = other.Position - origin.Position;
@@ -69,7 +67,7 @@ namespace Content.Shared.GameObjects.EntitySystems
MapCoordinates origin,
MapCoordinates other,
int collisionMask = (int) CollisionGroup.Impassable,
IEntity ignoredEnt = null)
IEntity? ignoredEnt = null)
{
var predicate = ignoredEnt == null
? null
@@ -111,7 +109,7 @@ namespace Content.Shared.GameObjects.EntitySystems
MapCoordinates other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false)
{
if (!origin.InRange(other, range)) return false;
@@ -132,7 +130,7 @@ namespace Content.Shared.GameObjects.EntitySystems
foreach (var result in rayResults)
{
if (!result.HitEntity.TryGetComponent(out IPhysBody p))
if (!result.HitEntity.TryGetComponent(out IPhysBody? p))
{
continue;
}
@@ -186,7 +184,7 @@ namespace Content.Shared.GameObjects.EntitySystems
IEntity other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
@@ -242,7 +240,7 @@ namespace Content.Shared.GameObjects.EntitySystems
IComponent other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
@@ -298,7 +296,7 @@ namespace Content.Shared.GameObjects.EntitySystems
EntityCoordinates other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
@@ -354,7 +352,7 @@ namespace Content.Shared.GameObjects.EntitySystems
MapCoordinates other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
@@ -408,7 +406,7 @@ namespace Content.Shared.GameObjects.EntitySystems
ITargetedInteractEventArgs args,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
@@ -453,7 +451,7 @@ namespace Content.Shared.GameObjects.EntitySystems
DragDropEventArgs args,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
@@ -521,7 +519,7 @@ namespace Content.Shared.GameObjects.EntitySystems
AfterInteractEventArgs args,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
Ignored? predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{

View File

@@ -23,8 +23,14 @@ namespace Content.Shared.Interfaces.GameObjects.Components
public class ActivateEventArgs : EventArgs, ITargetedInteractEventArgs
{
public IEntity User { get; set; }
public IEntity Target { get; set; }
public ActivateEventArgs(IEntity user, IEntity target)
{
User = user;
Target = target;
}
public IEntity User { get; }
public IEntity Target { get; }
}
/// <summary>

View File

@@ -20,8 +20,14 @@ namespace Content.Shared.Interfaces.GameObjects.Components
public class InteractHandEventArgs : EventArgs, ITargetedInteractEventArgs
{
public IEntity User { get; set; }
public IEntity Target { get; set; }
public InteractHandEventArgs(IEntity user, IEntity target)
{
User = user;
Target = target;
}
public IEntity User { get; }
public IEntity Target { get; }
}

View File

@@ -29,10 +29,18 @@ namespace Content.Shared.Interfaces.GameObjects.Components
public class InteractUsingEventArgs : EventArgs, ITargetedInteractEventArgs
{
public IEntity User { get; set; }
public EntityCoordinates ClickLocation { get; set; }
public IEntity Using { get; set; }
public IEntity Target { get; set; }
public InteractUsingEventArgs(IEntity user, EntityCoordinates clickLocation, IEntity @using, IEntity target)
{
User = user;
ClickLocation = clickLocation;
Using = @using;
Target = target;
}
public IEntity User { get; }
public EntityCoordinates ClickLocation { get; }
public IEntity Using { get; }
public IEntity Target { get; }
}
/// <summary>

View File

@@ -23,9 +23,16 @@ namespace Content.Shared.Interfaces.GameObjects.Components
[PublicAPI]
public class RangedInteractEventArgs : EventArgs
{
public IEntity User { get; set; }
public IEntity Using { get; set; }
public EntityCoordinates ClickLocation { get; set; }
public RangedInteractEventArgs(IEntity user, IEntity @using, EntityCoordinates clickLocation)
{
User = user;
Using = @using;
ClickLocation = clickLocation;
}
public IEntity User { get; }
public IEntity Using { get; }
public EntityCoordinates ClickLocation { get; }
}
/// <summary>

View File

@@ -21,7 +21,12 @@ namespace Content.Shared.Interfaces.GameObjects.Components
public class UseEntityEventArgs : EventArgs
{
public IEntity User { get; set; }
public UseEntityEventArgs(IEntity user)
{
User = user;
}
public IEntity User { get; }
}
/// <summary>