Skip to content

Commit

Permalink
Add nullability annotations to public API.
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Jul 31, 2024
1 parent 64d9bc1 commit fe1e6ae
Show file tree
Hide file tree
Showing 52 changed files with 443 additions and 267 deletions.
2 changes: 1 addition & 1 deletion Chroma/Audio/AudioInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class AudioInput
private readonly List<AudioDevice> _devices = new();
private readonly List<AudioCapture> _activeCaptures = new();

private static AudioInput _instance;
private static AudioInput? _instance;
internal static AudioInput Instance => _instance ??= new();

public IReadOnlyList<AudioDevice> Devices => _devices;
Expand Down
4 changes: 2 additions & 2 deletions Chroma/Audio/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public sealed class AudioManager

public IReadOnlyList<string> AudioDrivers => _audioDrivers;

public event EventHandler<AudioDeviceEventArgs> DeviceConnected;
public event EventHandler<AudioDeviceEventArgs> DeviceDisconnected;
public event EventHandler<AudioDeviceEventArgs>? DeviceConnected;
public event EventHandler<AudioDeviceEventArgs>? DeviceDisconnected;

internal AudioManager()
{
Expand Down
17 changes: 9 additions & 8 deletions Chroma/Audio/AudioOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class AudioOutput
private readonly List<AudioDevice> _devices = new();
private readonly List<Decoder> _decoders = new();

private static AudioOutput _instance;
private static AudioOutput? _instance;
internal static AudioOutput Instance => _instance ??= new();

private bool _mixerInitialized;
Expand All @@ -29,7 +29,7 @@ public sealed class AudioOutput
public int Frequency { get; private set; }
public ushort SampleCount { get; private set; }

public event EventHandler<AudioSourceEventArgs> AudioSourceFinished;
public event EventHandler<AudioSourceEventArgs>? AudioSourceFinished;

public float MasterVolume
{
Expand All @@ -48,7 +48,7 @@ public float MasterVolume
}
}

public AudioDevice CurrentOutputDevice => _devices.FirstOrDefault(
public AudioDevice? CurrentOutputDevice => _devices.FirstOrDefault(
x => x.OpenIndex == SDL2_nmix.NMIX_GetAudioDevice()
);

Expand All @@ -62,7 +62,7 @@ public void PauseAll()
SDL2_nmix.NMIX_PausePlayback(_playbackPaused);
}

public void Open(AudioDevice device = null, int frequency = 44100, ushort sampleCount = 1024)
public void Open(AudioDevice? device = null, int frequency = 44100, ushort sampleCount = 1024)
{
Close();

Expand Down Expand Up @@ -200,10 +200,11 @@ private void EnumerateDecoders()

_decoders.Add(
new Decoder(
Marshal.PtrToStringAnsi(decoderList[i]->description),
Marshal.PtrToStringUTF8(decoderList[i]->author),
Marshal.PtrToStringAnsi(decoderList[i]->url)
) { SupportedFormats = fmts }
Marshal.PtrToStringAnsi(decoderList[i]->description) ?? string.Empty,
Marshal.PtrToStringUTF8(decoderList[i]->author) ?? string.Empty,
Marshal.PtrToStringAnsi(decoderList[i]->url) ?? string.Empty,
supportedFormats: fmts
)
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions Chroma/Audio/Captures/AudioCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Chroma.Audio.Captures;
public abstract class AudioCapture : DisposableResource
{
private static readonly Log _log = LogManager.GetForCurrentAssembly();
private CancellationTokenSource _finishTokenSource;
private CancellationTokenSource? _finishTokenSource;

private uint _deviceId;

Expand All @@ -23,8 +23,8 @@ public abstract class AudioCapture : DisposableResource
public ulong TotalSize { get; private set; }

protected AudioCapture(
AudioDevice device = null,
AudioFormat format = null,
AudioDevice? device = null,
AudioFormat? format = null,
ChannelMode channelMode = ChannelMode.Mono,
int frequency = 44100,
ushort bufferSize = 4096)
Expand Down Expand Up @@ -158,7 +158,7 @@ private void Record()
{
while (true)
{
_finishTokenSource.Token.ThrowIfCancellationRequested();
_finishTokenSource?.Token.ThrowIfCancellationRequested();

var dataSize = SDL2.SDL_GetQueuedAudioSize(_deviceId);

Expand Down
4 changes: 2 additions & 2 deletions Chroma/Audio/Captures/StreamAudioCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class StreamAudioCapture : AudioCapture

public StreamAudioCapture(
Stream stream,
AudioDevice device = null,
AudioFormat format = null,
AudioDevice? device = null,
AudioFormat? format = null,
ChannelMode channelMode = ChannelMode.Mono,
int frequency = 44100, ushort bufferSize = 4096)
: base(device, format, channelMode, frequency, bufferSize)
Expand Down
5 changes: 3 additions & 2 deletions Chroma/Audio/Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ public sealed class Decoder
public string Author { get; }
public string Url { get; }

public IReadOnlyList<string> SupportedFormats { get; internal set; }
public IReadOnlyList<string> SupportedFormats { get; }

internal Decoder(string description, string author, string url)
internal Decoder(string description, string author, string url, IReadOnlyList<string> supportedFormats)
{
Description = description;
Author = author;
Url = url;
SupportedFormats = supportedFormats;
}
}
11 changes: 5 additions & 6 deletions Chroma/Audio/Sources/FileBasedAudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public abstract class FileBasedAudioSource : AudioSource

private readonly SdlRwOps _sdlRwOps;

private SDL2_nmix.NMIX_SourceCallback _originalSourceCallback;
private SDL2_nmix.NMIX_SourceCallback _internalSourceCallback;
private SDL2_nmix.NMIX_SourceCallback? _originalSourceCallback;
private SDL2_nmix.NMIX_SourceCallback? _internalSourceCallback;

internal IntPtr FileSourceHandle { get; private set; }

Expand Down Expand Up @@ -71,7 +71,7 @@ internal FileBasedAudioSource(string filePath, bool decodeWhole)
{
}

internal FileBasedAudioSource(Stream stream, bool decodeWhole, string fileFormat = null)
internal FileBasedAudioSource(Stream stream, bool decodeWhole, string? fileFormat = null)
{
_sdlRwOps = new SdlRwOps(stream, true);

Expand Down Expand Up @@ -145,7 +145,6 @@ public override void Play()
if (SDL2_nmix.NMIX_Play(Handle) < 0)
{
_log.Error($"Failed to play the audio source [play]: {SDL2.SDL_GetError()}");
return;
}
else
{
Expand Down Expand Up @@ -287,9 +286,9 @@ private void InternalSourceCallback(IntPtr userdata, IntPtr buffer, int bufferSi
var actualAudioFormat = AudioFormat.FromSdlFormat(SoundSample->actual.format);

for (var i = 0; i < Filters.Count; i++)
Filters[i]?.Invoke(span, actualAudioFormat);
Filters[i](span, actualAudioFormat);

_originalSourceCallback(userdata, buffer, bufferSize);
_originalSourceCallback?.Invoke(userdata, buffer, bufferSize);

var sampleDuration = (double)bufferSize / (
SoundSample->actual.rate * SoundSample->actual.channels * (actualAudioFormat.BitsPerSample / 8)
Expand Down
10 changes: 3 additions & 7 deletions Chroma/Audio/Sources/Waveform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
public class Waveform : AudioSource
{
private static readonly Log _log = LogManager.GetForCurrentAssembly();
private SDL2_nmix.NMIX_SourceCallback _internalCallback; // Needs to be a class field to avoid GC collection.
private SDL2_nmix.NMIX_SourceCallback? _internalCallback; // Needs to be a class field to avoid GC collection.

public AudioStreamDelegate SampleGenerator { get; set; }

public ChannelMode ChannelMode { get; }
public int Frequency { get; }

public Waveform(AudioFormat format, AudioStreamDelegate sampleGenerator,
ChannelMode channelMode = ChannelMode.Stereo, int frequency = 44100)
public Waveform(AudioFormat format, AudioStreamDelegate sampleGenerator, ChannelMode channelMode = ChannelMode.Stereo, int frequency = 44100)
{
_internalCallback = AudioCallback;
SampleGenerator = sampleGenerator;
Expand All @@ -41,17 +40,14 @@ public Waveform(AudioFormat format, AudioStreamDelegate sampleGenerator,

private void AudioCallback(IntPtr userData, IntPtr samples, int bufferSize)
{
if (SampleGenerator == null)
return;

unsafe
{
var span = new Span<byte>(
samples.ToPointer(),
bufferSize
);

SampleGenerator.Invoke(span, Format);
SampleGenerator(span, Format);
}
}
}
38 changes: 25 additions & 13 deletions Chroma/BootScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@

using System;
using System.Numerics;
using Chroma.Diagnostics.Logging;
using Chroma.Graphics;

internal sealed class BootScene
{
private const int FrameCount = 301;


private readonly Log _log = LogManager.GetForCurrentAssembly();
private readonly Game _game;
private readonly Vector2 _bootTextTextureOffset = new(0, 270);

private int _currentFrame;

private EmbeddedAssets.BootAnimationInfo CurrentBootTextProperties
=> EmbeddedAssets.BootAnimationData[0][_currentFrame];
private EmbeddedAssets.BootAnimationInfo? CurrentBootTextProperties
=> EmbeddedAssets.BootAnimationData?[0][_currentFrame];

private EmbeddedAssets.BootAnimationInfo CurrentBootWheelProperties
=> EmbeddedAssets.BootAnimationData[1][_currentFrame];
private EmbeddedAssets.BootAnimationInfo? CurrentBootWheelProperties
=> EmbeddedAssets.BootAnimationData?[1][_currentFrame];

private EmbeddedAssets.BootAnimationInfo CurrentBootLogoProperties
=> EmbeddedAssets.BootAnimationData[2][_currentFrame];
private EmbeddedAssets.BootAnimationInfo? CurrentBootLogoProperties
=> EmbeddedAssets.BootAnimationData?[2][_currentFrame];

internal event EventHandler Finished;
internal event EventHandler? Finished;

internal BootScene(Game game)
{
Expand All @@ -31,9 +33,19 @@ internal BootScene(Game game)

internal void FixedUpdate(float delta)
{
EmbeddedAssets.BootWheelTexture.ColorMask = new Color(1f, 1f, 1f, CurrentBootWheelProperties.Opacity);
EmbeddedAssets.BootLogoTexture.ColorMask = new Color(1f, 1f, 1f, CurrentBootLogoProperties.Opacity);
EmbeddedAssets.BootTextTexture.ColorMask = new Color(1f, 1f, 1f, CurrentBootTextProperties.Opacity);
if (CurrentBootWheelProperties == null
|| CurrentBootLogoProperties == null
|| CurrentBootTextProperties == null)
{
_log.Warning("Boot logo properties were null. This is abnormal. Aborting boot logo display.");
_currentFrame = FrameCount;
}
else
{
EmbeddedAssets.BootWheelTexture.ColorMask = new Color(1f, 1f, 1f, CurrentBootWheelProperties.Opacity);
EmbeddedAssets.BootLogoTexture.ColorMask = new Color(1f, 1f, 1f, CurrentBootLogoProperties.Opacity);
EmbeddedAssets.BootTextTexture.ColorMask = new Color(1f, 1f, 1f, CurrentBootTextProperties.Opacity);
}

if (_currentFrame >= FrameCount)
return;
Expand All @@ -51,13 +63,13 @@ internal void Draw(RenderContext context)
_game.Window.Center,
Vector2.One,
EmbeddedAssets.BootWheelTexture.Center,
CurrentBootWheelProperties.Rotation
CurrentBootWheelProperties?.Rotation ?? 0
);

context.DrawTexture(
EmbeddedAssets.BootLogoTexture,
_game.Window.Center,
new Vector2(CurrentBootLogoProperties.Scale),
new Vector2(CurrentBootLogoProperties?.Scale ?? 0),
EmbeddedAssets.BootLogoTexture.Center,
0
);
Expand Down
3 changes: 2 additions & 1 deletion Chroma/Chroma.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -18,7 +19,7 @@
</Description>

<PackageId>Chroma</PackageId>
<Version>0.64.1</Version>
<Version>0.65.0</Version>
<Author>vddCore</Author>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Chroma-2D/Chroma</RepositoryUrl>
Expand Down
14 changes: 7 additions & 7 deletions Chroma/ContentManagement/FileSystem/FileSystemContentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ public class FileSystemContentProvider : DisposableResource, IContentProvider

public string ContentRoot { get; }

public FileSystemContentProvider(string contentRoot = null)
public FileSystemContentProvider(string? contentRoot = null)
{
ContentRoot = contentRoot;

if (string.IsNullOrEmpty(ContentRoot))
if (string.IsNullOrEmpty(contentRoot))
{
ContentRoot = Path.Combine(AppContext.BaseDirectory, "Content");
contentRoot = Path.Combine(AppContext.BaseDirectory, "Content");
}

ContentRoot = contentRoot;

_loadedResources = new HashSet<DisposableResource>();
_importers = new Dictionary<Type, Func<string, object[], object>>();

RegisterImporters();
}

public T Load<T>(string relativePath, params object[] args) where T : DisposableResource
public T? Load<T>(string relativePath, params object[] args) where T : DisposableResource
{
var type = typeof(T);

Expand Down Expand Up @@ -173,7 +173,7 @@ private void RegisterImporters()
private string MakeAbsolutePath(string relativePath)
=> Path.Combine(ContentRoot, relativePath);

private void OnResourceDisposing(object sender, EventArgs e)
private void OnResourceDisposing(object? sender, EventArgs e)
{
if (sender is DisposableResource disposable)
{
Expand Down
2 changes: 1 addition & 1 deletion Chroma/ContentManagement/IContentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IContentProvider : IDisposable
{
string ContentRoot { get; }

T Load<T>(string relativePath, params object[] args) where T : DisposableResource;
T? Load<T>(string relativePath, params object[] args) where T : DisposableResource;
void Unload<T>(T resource) where T : DisposableResource;

byte[] Read(string relativePath);
Expand Down
8 changes: 4 additions & 4 deletions Chroma/Diagnostics/Logging/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ public void Exception(Exception e)
}

public void Info(object obj)
=> Info(obj.ToString());
=> Info(obj.ToString() ?? "<null>");

public void Warning(object obj)
=> Warning(obj.ToString());
=> Warning(obj.ToString() ?? "<null>");

public void Error(object obj)
=> Error(obj.ToString());
=> Error(obj.ToString() ?? "<null>");

public void Debug(object obj)
=> Debug(obj.ToString());
=> Debug(obj.ToString() ?? "<null>");

public Log WithOutputTemplate(string template)
{
Expand Down
6 changes: 3 additions & 3 deletions Chroma/Diagnostics/Logging/LogInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

using System.Reflection;

internal class LogInfo
internal class LogInfo(Assembly owningAssembly, Log log)
{
internal Assembly OwningAssembly { get; set; }
internal Log Log { get; set; }
internal Assembly OwningAssembly { get; } = owningAssembly;
internal Log Log { get; } = log;
}
Loading

0 comments on commit fe1e6ae

Please sign in to comment.