Skip to content

Commit

Permalink
Add nullable annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanite committed Jul 4, 2024
1 parent ce74820 commit 4473c66
Show file tree
Hide file tree
Showing 42 changed files with 125 additions and 118 deletions.
4 changes: 2 additions & 2 deletions Collections/RingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public T Dequeue()
/// Tries to remove and return the object at the beginning of the
/// <see cref="RingBuffer{T}"/>.
/// </summary>
public bool TryDequeue(out T value)
public bool TryDequeue(out T? value)
{
if (IsEmpty)
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public T Peek()
/// Tries to returns the object at the beginning of the
/// <see cref="RingBuffer{T}"/> without removing it.
/// </summary>
public bool TryPeek(out T value)
public bool TryPeek(out T? value)
{
if (IsEmpty)
{
Expand Down
4 changes: 2 additions & 2 deletions Components/SingletonBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Exanite.Core.Components
[DefaultExecutionOrder(-100)]
public class SingletonBehaviour<T> : MonoBehaviour where T : SingletonBehaviour<T>
{
private static T instance;
private static T? instance;

public bool dontDestroyOnLoad;

Expand All @@ -16,7 +16,7 @@ public static T Instance
{
get
{
if (instance)
if (instance != null)
{
return instance;
}
Expand Down
8 changes: 7 additions & 1 deletion Components/UiScaleHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif
using UnityEngine;
using UnityEngine.UI;

Expand All @@ -7,7 +10,10 @@ public class UiScaleHandler : MonoBehaviour
{
private static readonly string ScaleName = "Project_UiScale";

public CanvasScaler Scaler;
#if ODIN_INSPECTOR
[Required]
#endif
public CanvasScaler Scaler = null!;

private void Start()
{
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/BindTypeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum BindTypeFilter

/// <summary>
/// Binds the object's implemented interfaces and its inheritance hierarchy,
/// stopping once one of the types defined in <see cref=ObjectBindingg.IgnoredTypes"/> are reached.
/// stopping once one of the types defined in <see cref="ObjectBinding.IgnoredTypes"/> are reached.
/// </summary>
Smart = 1 << 1,

Expand Down
10 changes: 5 additions & 5 deletions DependencyInjection/DiBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DiBinding : MonoBehaviour

[SerializeField] private bool useSceneContext = false;
[DisableIf(nameof(useSceneContext))] [SerializeField]
private Context context = null;
private Context? context = null;

private void Awake()
{
Expand All @@ -29,15 +29,15 @@ private void Awake()
}
}

context.AddNormalInstaller(new DiBindingInstaller(bindings));
context!.AddNormalInstaller(new DiBindingInstaller(bindings));
}

public void Start()
{
// Define this method so we expose the enabled check box
}

private Context FindContext()
private Context? FindContext()
{
if (useSceneContext)
{
Expand All @@ -53,12 +53,12 @@ private Context FindContext()
return FindSceneContext();
}

private Context FindContextInParent()
private Context? FindContextInParent()
{
return GetComponentInParent<Context>();
}

private SceneContext FindSceneContext()
private SceneContext? FindSceneContext()
{
foreach (var rootGameObject in gameObject.scene.GetRootGameObjects())
{
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/ObjectBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private IEnumerable<Type> GetTypesToBind()
types.UnionWith(customTypes);
}

types.Remove(null);
types.Remove(null!);

return types;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()
{
try
{
customTypes.Add(SerializationUtility.DeserializeType(serializedBindType));
customTypes.Add(SerializationUtility.DeserializeType(serializedBindType)!);
}
catch (Exception e)
{
Expand Down
4 changes: 2 additions & 2 deletions Editor/AssetDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Exanite.Core.Editor
{
public static class AssetDependencies
{
private static Dictionary<string, HashSet<string>> AssetDependents;
private static Dictionary<string, HashSet<string>>? AssetDependents;

/// <remarks>
/// This behaves slightly differently compared to Unity's "Select Dependencies" option.
Expand Down Expand Up @@ -99,7 +99,7 @@ private static HashSet<string> GetDependentAssetPaths(IEnumerable<string> assetP
var results = new HashSet<string>();
foreach (var assetPath in assetPaths)
{
if (AssetDependents.TryGetValue(assetPath, out var dependents))
if (AssetDependents!.TryGetValue(assetPath, out var dependents))
{
results.UnionWith(dependents);
}
Expand Down
2 changes: 1 addition & 1 deletion Editor/CleanEmptyFolders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static void DeleteFolders(List<DirectoryInfo> folders)

foreach (var folder in folders)
{
var metaFile = folder.Parent.GetFiles($"{folder.Name}.meta", SearchOption.TopDirectoryOnly).FirstOrDefault();
var metaFile = folder.Parent!.GetFiles($"{folder.Name}.meta", SearchOption.TopDirectoryOnly).FirstOrDefault();

Debug.Log($"Deleting empty folder: {folder.FullName}");
folder.Delete();
Expand Down
20 changes: 9 additions & 11 deletions Editor/ScriptableObjectCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace Exanite.Core.Editor
/// </summary>
public class ScriptableObjectCreator : OdinEditorWindow
{
private List<Type> validTypes;
private List<Assembly> validAssemblies;
private List<Type>? validTypes;
private List<Assembly>? validAssemblies;

private Type typeToCreate;
private Assembly assemblyFilter;
private Type? typeToCreate;
private Assembly? assemblyFilter;

private List<Type> filteredTypes;
private List<Type>? filteredTypes;

/// <summary>
/// Editor window name.
Expand All @@ -38,10 +38,9 @@ public class ScriptableObjectCreator : OdinEditorWindow
[TitleGroup("$name", HorizontalLine = true)]
[ShowInInspector]
[ValueDropdown(nameof(ValidAssemblies), SortDropdownItems = true, CopyValues = false, NumberOfItemsBeforeEnablingSearch = 0)]
public Assembly AssemblyFilter
public Assembly? AssemblyFilter
{
get => assemblyFilter;

set
{
assemblyFilter = value;
Expand All @@ -56,10 +55,9 @@ public Assembly AssemblyFilter
/// </summary>
[ShowInInspector]
[ValueDropdown(nameof(FilteredTypes), SortDropdownItems = true, CopyValues = false, NumberOfItemsBeforeEnablingSearch = 0)]
public Type TypeToCreate
public Type? TypeToCreate
{
get => typeToCreate;

set
{
typeToCreate = value;
Expand All @@ -76,7 +74,7 @@ public Type TypeToCreate
[TitleGroup("Preview", HorizontalLine = true)]
[InlineEditor(DrawGUI = true, DrawHeader = false, DrawPreview = false, Expanded = true,
ObjectFieldMode = InlineEditorObjectFieldModes.CompletelyHidden)]
public ScriptableObject Preview { get; protected set; }
public ScriptableObject? Preview { get; protected set; }

/// <summary>
/// The types that this <see cref="EditorWindow"/> is allowed to create.
Expand Down Expand Up @@ -177,7 +175,7 @@ public virtual void Create()
throw new InvalidOperationException("Cannot create a new ScriptableObject at this time");
}

var path = EditorUtility.SaveFilePanel($"Save {TypeToCreate.Name} to folder", "Assets", $"new {TypeToCreate.Name}", "asset");
var path = EditorUtility.SaveFilePanel($"Save {TypeToCreate!.Name} to folder", "Assets", $"new {TypeToCreate.Name}", "asset");

if (path.IsNullOrWhitespace())
{
Expand Down
1 change: 1 addition & 0 deletions Editor/UnityEntrypoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Exanite.Core.Editor
{
internal class UnityEntrypoints : AssetPostprocessor
{
// ReSharper disable once Unity.IncorrectMethodSignature - ReSharper is outdated
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
{
AssetDependencies.ClearAssetDependentsCache();
Expand Down
10 changes: 5 additions & 5 deletions Interpolation/Interpolator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ namespace Exanite.Core.Interpolation
{
public abstract class Interpolator<T>
{
public T Current;
public T Previous;
public T? Current;
public T? Previous;

private float lastPushTime;

public T Update(float time, float timeBetweenPushes, float extrapolationMultiplier = 0.5f)
public T? Update(float time, float timeBetweenPushes, float extrapolationMultiplier = 0.5f)
{
var timeSinceLastPush = time - lastPushTime;
timeSinceLastPush += extrapolationMultiplier * timeBetweenPushes;
Expand All @@ -17,14 +17,14 @@ public T Update(float time, float timeBetweenPushes, float extrapolationMultipli
return Lerp(Previous, Current, t);
}

public void PushNext(T next, float time)
public void PushNext(T? next, float time)
{
Previous = Current;
Current = next;

lastPushTime = time;
}

public abstract T Lerp(T previous, T current, float time);
public abstract T? Lerp(T? previous, T? current, float time);
}
}
2 changes: 1 addition & 1 deletion Numerics/LargeNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static implicit operator LargeNumber(double value)
return lhs.CompareTo(rhs) <= 0;
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is LargeNumber largeNumber)
{
Expand Down
4 changes: 2 additions & 2 deletions OdinInspector/InlineAttribute.Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void ProcessSelfAttributes(InspectorProperty property, List<Attr

public override void ProcessChildMemberAttributes(InspectorProperty parentProperty, MemberInfo member, List<Attribute> attributes)
{
Type memberType = null;
Type? memberType = null;

if (member is FieldInfo field)
{
Expand Down Expand Up @@ -86,7 +86,7 @@ private static void Set(InlineAttribute inline, List<Attribute> attributes, bool
public class InlineAttributeDrawer<TAttribute> : OdinAttributeDrawer<TAttribute>
where TAttribute : InlineAttribute
{
protected override void DrawPropertyLayout(GUIContent label)
protected override void DrawPropertyLayout(GUIContent? label)
{
if (Property.ValueEntry.WeakSmartValue == null &&
Property.Attributes.Any(attr => attr is SerializableAttribute) &&
Expand Down
18 changes: 9 additions & 9 deletions Properties/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public abstract class Property
public string Key { get; }
public Type Type { get; }

public abstract object UntypedValue { get; set; }
public abstract object? UntypedValue { get; set; }

public event EventHandler<Property, PropertyValueChangedEventArgs<object>> UntypedValueChanged;
public event EventHandler<Property, PropertyValueChangedEventArgs<object>>? UntypedValueChanged;

protected Property(string key, Type type)
{
Expand All @@ -22,7 +22,7 @@ protected Property(string key, Type type)
Type = type;
}

protected void OnValueChangedUntyped(object previousValue, object newValue)
protected void OnValueChangedUntyped(object? previousValue, object? newValue)
{
propertyValueChangedEventArgs.PreviousValue = previousValue;
propertyValueChangedEventArgs.NewValue = newValue;
Expand All @@ -33,11 +33,11 @@ protected void OnValueChangedUntyped(object previousValue, object newValue)

public class Property<T> : Property
{
private T value;
private T? value;

private readonly PropertyValueChangedEventArgs<T> propertyValueChangedEventArgs;

public T Value
public T? Value
{
get => value;
set
Expand All @@ -53,20 +53,20 @@ public T Value
}
}

public override object UntypedValue
public override object? UntypedValue
{
get => value;
set => this.value = (T)value;
set => this.value = (T?)value;
}

public event EventHandler<Property, PropertyValueChangedEventArgs<T>> ValueChanged;
public event EventHandler<Property, PropertyValueChangedEventArgs<T>>? ValueChanged;

public Property(string key) : base(key, typeof(T))
{
propertyValueChangedEventArgs = new PropertyValueChangedEventArgs<T>(this);
}

private void OnValueChanged(T previousValue, T newValue)
private void OnValueChanged(T? previousValue, T? newValue)
{
propertyValueChangedEventArgs.PreviousValue = previousValue;
propertyValueChangedEventArgs.NewValue = newValue;
Expand Down
19 changes: 10 additions & 9 deletions Properties/PropertyCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Exanite.Core.Events;

namespace Exanite.Core.Properties
Expand All @@ -10,12 +11,12 @@ public class PropertyCollection

public IReadOnlyDictionary<string, Property> PropertiesByKey => properties;

public event EventHandler<PropertyCollection, Property> PropertyAdded;
public event EventHandler<PropertyCollection, Property> PropertyRemoved;
public event EventHandler<PropertyCollection, Property>? PropertyAdded;
public event EventHandler<PropertyCollection, Property>? PropertyRemoved;

public event EventHandler<PropertyCollection, PropertyValueChangedEventArgs<object>> PropertyValueChanged;
public event EventHandler<PropertyCollection, PropertyValueChangedEventArgs<object>>? PropertyValueChanged;

public T GetPropertyValue<T>(PropertyDefinition<T> definition, bool addIfDoesNotExist = false)
public T? GetPropertyValue<T>(PropertyDefinition<T> definition, bool addIfDoesNotExist = false)
{
var property = addIfDoesNotExist ? GetOrAddProperty(definition) : GetProperty(definition);

Expand All @@ -41,7 +42,7 @@ public void SetPropertyValue<T>(PropertyDefinition<T> definition, T value, bool
}
}

public Property GetProperty(string key)
public Property? GetProperty(string key)
{
if (!properties.TryGetValue(key, out var untypedProperty))
{
Expand All @@ -51,7 +52,7 @@ public Property GetProperty(string key)
return untypedProperty;
}

public Property GetProperty(PropertyDefinition definition)
public Property? GetProperty(PropertyDefinition definition)
{
if (!properties.TryGetValue(definition.Key, out var untypedProperty))
{
Expand All @@ -66,12 +67,12 @@ public Property GetProperty(PropertyDefinition definition)
return untypedProperty;
}

public Property<T> GetProperty<T>(PropertyDefinition<T> definition)
public Property<T>? GetProperty<T>(PropertyDefinition<T> definition)
{
return (Property<T>)GetProperty((PropertyDefinition)definition);
return (Property<T>?)GetProperty((PropertyDefinition)definition);
}

public bool TryGetProperty<T>(PropertyDefinition<T> definition, out Property<T> property)
public bool TryGetProperty<T>(PropertyDefinition<T> definition, [NotNullWhen(true)] out Property<T>? property)
{
property = GetProperty(definition);

Expand Down
Loading

0 comments on commit 4473c66

Please sign in to comment.