Skip to content

Commit

Permalink
Some minor code style fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Jan 8, 2024
1 parent b71bffc commit d03d485
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/cli/CliExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Dispose()
public static async Task<T> RunTaskAsync<T>(
this ProgressContext context, string description, Func<Task<T>> function)
{
using var task = new ProgressTaskWrapper(context, description, 1, true);
using var task = new ProgressTaskWrapper(context, description, goal: 1, indeterminate: true);

var result = await function().ConfigureAwait(false);

Expand All @@ -52,7 +52,7 @@ public static async Task RunTaskAsync(this ProgressContext context, string descr
public static async Task<T> RunTaskAsync<T>(
this ProgressContext context, string description, int goal, Func<Action, Task<T>> function)
{
using var task = new ProgressTaskWrapper(context, description, goal, false);
using var task = new ProgressTaskWrapper(context, description, goal, indeterminate: false);

return await function(task.Increment).ConfigureAwait(false);
}
Expand Down
24 changes: 18 additions & 6 deletions src/client/GameMessageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private protected unsafe GameMessageServer()
var handle = default(GCHandle);
var atom = 0;
var hwnd = default(HWND);
var hwnd = HWND.Null;
try
{
Expand All @@ -44,22 +44,34 @@ private protected unsafe GameMessageServer()
if (atom == 0)
throw new Win32Exception();
hwnd = CreateWindowExW(0, className, windowName, 0, 0, 0, 0, 0, default, null, null, null);
hwnd = CreateWindowExW(
dwExStyle: 0,
className,
windowName,
dwStyle: 0,
X: 0,
Y: 0,
nWidth: 0,
nHeight: 0,
hWndParent: HWND.Null,
hMenu: null,
hInstance: null,
lpParam: null);
if ((nint)hwnd == 0)
throw new Win32Exception();
_ = SetWindowLongPtrW(hwnd, 0, (nint)handle);
_ = SetWindowLongPtrW(hwnd, nIndex: 0, (nint)handle);
_done.Task.GetAwaiter().GetResult();
}
finally
{
if ((nint)hwnd != 0)
_ = DefWindowProcW(hwnd, WM_CLOSE, 0, 0);
_ = DefWindowProcW(hwnd, WM_CLOSE, wParam: 0, lParam: 0);
if (atom != 0)
_ = UnregisterClassW(className, null);
_ = UnregisterClassW(className, hInstance: null);
if (handle.IsAllocated)
handle.Free();
Expand Down Expand Up @@ -105,7 +117,7 @@ private static unsafe LRESULT WindowProcedure(HWND hWnd, uint msg, WPARAM wParam
var id = cds.dwData;
var payload = new ReadOnlySpan<byte>(cds.lpData, (int)cds.cbData);

var @this = Unsafe.As<GameMessageServer>(((GCHandle)GetWindowLongPtrW(hWnd, 0)).Target!);
var @this = Unsafe.As<GameMessageServer>(((GCHandle)GetWindowLongPtrW(hWnd, nIndex: 0)).Target!);

@this.MessageReceived?.Invoke(payload, id);

Expand Down
8 changes: 4 additions & 4 deletions src/common/Diagnostics/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void Assert(
public static void Argument([DoesNotReturnIf(false)] bool condition)
{
if (!condition)
throw new ArgumentException(null);
throw new ArgumentException(message: null);
}

public static void Argument<T>(
Expand All @@ -126,7 +126,7 @@ public static void Argument<T>(
_ = value;

if (!condition)
throw new ArgumentException(null, name);
throw new ArgumentException(message: null, name);
}

// TODO: https://github.com/dotnet/csharplang/issues/1148
Expand All @@ -138,7 +138,7 @@ public static void Argument<T>(
_ = value;

if (!condition)
throw new ArgumentException(null, name);
throw new ArgumentException(message: null, name);
}

public static void Null([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? name = null)
Expand Down Expand Up @@ -210,6 +210,6 @@ public static void All<T>(
{
foreach (var item in value)
if (!predicate(item))
throw new ArgumentException(null, name);
throw new ArgumentException(message: null, name);
}
}
4 changes: 2 additions & 2 deletions src/formats/Data/DataCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal static Aes CreateCipher(ReadOnlyMemory<byte> key, ReadOnlyMemory<byte>

public static DataCenterNode Create()
{
return new UserDataCenterNode(null, DataCenterConstants.RootNodeName);
return new UserDataCenterNode(parent: null, DataCenterConstants.RootNodeName);
}

public static Task<DataCenterNode> LoadAsync(
Expand All @@ -54,7 +54,7 @@ public static Task<DataCenterNode> LoadAsync(
(DataCenterLoaderMode.Lazy, _) => new LazyMutableDataCenterReader(options),
(DataCenterLoaderMode.Eager, DataCenterMutability.Immutable) => new EagerImmutableDataCenterReader(options),
(DataCenterLoaderMode.Eager, _) => new EagerMutableDataCenterReader(options),
_ => throw new ArgumentException(null, nameof(options)),
_ => throw new ArgumentException(message: null, nameof(options)),
};

return reader.ReadAsync(stream, cancellationToken);
Expand Down
16 changes: 10 additions & 6 deletions src/formats/Data/DataCenterValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Vezel.Novadrop.Data;
IComparable<DataCenterValue>,
IComparisonOperators<DataCenterValue, DataCenterValue, bool>
{
public static DataCenterValue Null { get; } = new(DataCenterTypeCode.Null, primitiveValue: 0, stringValue: null);

public DataCenterTypeCode TypeCode { get; }

public bool IsNull => TypeCode == DataCenterTypeCode.Null;
Expand Down Expand Up @@ -67,16 +69,18 @@ public bool AsBoolean
}
}

[SuppressMessage("", "IDE0032")]
internal int UnsafeAsInt32 => _primitiveValue;

internal float UnsafeAsSingle => Unsafe.As<int, float>(ref Unsafe.AsRef(in _primitiveValue));
internal float UnsafeAsSingle => Unsafe.BitCast<int, float>(_primitiveValue);

internal string UnsafeAsString => _stringValue!;

internal bool UnsafeAsBoolean => Unsafe.As<int, bool>(ref Unsafe.AsRef(in _primitiveValue));
internal bool UnsafeAsBoolean => Unsafe.BitCast<int, bool>(_primitiveValue);

private readonly string? _stringValue;

[SuppressMessage("", "IDE0032")]
private readonly int _primitiveValue;

private DataCenterValue(DataCenterTypeCode typeCode, int primitiveValue, string? stringValue)
Expand All @@ -87,23 +91,23 @@ private DataCenterValue(DataCenterTypeCode typeCode, int primitiveValue, string?
}

public DataCenterValue(int value)
: this(DataCenterTypeCode.Int32, value, null)
: this(DataCenterTypeCode.Int32, value, stringValue: null)
{
}

public DataCenterValue(float value)
: this(DataCenterTypeCode.Single, Unsafe.As<float, int>(ref value), null)
: this(DataCenterTypeCode.Single, Unsafe.BitCast<float, int>(value), stringValue: null)
{
}

public DataCenterValue(string value)
: this(DataCenterTypeCode.String, 0, value)
: this(DataCenterTypeCode.String, primitiveValue: 0, value)
{
Check.Null(value);
}

public DataCenterValue(bool value)
: this(DataCenterTypeCode.Boolean, value ? 1 : 0, null)
: this(DataCenterTypeCode.Boolean, value ? 1 : 0, stringValue: null)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/formats/Data/Nodes/UserDataCenterNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class UserDataCenterNode : MutableDataCenterNode
private List<DataCenterNode>? _children;

public UserDataCenterNode(DataCenterNode? parent, string name)
: base(parent, name, default, DataCenterKeys.None)
: base(parent, name, value: null, DataCenterKeys.None)
{
}
}
4 changes: 2 additions & 2 deletions src/formats/Data/Serialization/DataCenterWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,15 @@ public Task WriteAsync(Stream stream, DataCenterNode root, CancellationToken can
using var aes = DataCenter.CreateCipher(_options.Key, _options.IV);
using var encryptor = aes.CreateEncryptor();
using var padder = new FakePaddingCryptoTransform(encryptor);
var cryptoStream = new CryptoStream(stream, padder, CryptoStreamMode.Write, true);
var cryptoStream = new CryptoStream(stream, padder, CryptoStreamMode.Write, leaveOpen: true);
await using (cryptoStream.ConfigureAwait(false))
{
await new StreamBinaryWriter(cryptoStream)
.WriteUInt32Async((uint)memoryStream.Length, cancellationToken)
.ConfigureAwait(false);
var zlibStream = new ZLibStream(cryptoStream, _options.CompressionLevel, true);
var zlibStream = new ZLibStream(cryptoStream, _options.CompressionLevel, leaveOpen: true);
await using (zlibStream.ConfigureAwait(false))
{
Expand Down
10 changes: 5 additions & 5 deletions src/formats/Data/Serialization/Readers/DataCenterReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ protected void ReadChildren<T>(
(1, 1, _) => true,
(2, not 0, _) when _options.Strict =>
throw new InvalidDataException($"Attribute has invalid extended type code {extCode}."),
(2, _, var value) => Unsafe.As<int, float>(ref value),
(3, _, _) => default(DataCenterValue), // Handled below.
(2, _, var value) => Unsafe.BitCast<int, float>(value),
(3, _, _) => DataCenterValue.Null, // Handled below.
_ => throw new InvalidDataException($"Attribute has invalid type code {typeCode}."),
};

Expand Down Expand Up @@ -202,15 +202,15 @@ public Task<DataCenterNode> ReadAsync(Stream stream, CancellationToken cancellat
using var aes = DataCenter.CreateCipher(_options.Key, _options.IV);
using var decryptor = aes.CreateDecryptor();
using var padder = new FakePaddingCryptoTransform(decryptor);
var cryptoStream = new CryptoStream(stream, padder, CryptoStreamMode.Read, true);
var cryptoStream = new CryptoStream(stream, padder, CryptoStreamMode.Read, leaveOpen: true);
await using (cryptoStream.ConfigureAwait(false))
{
var size = await new StreamBinaryReader(cryptoStream)
.ReadUInt32Async(cancellationToken)
.ConfigureAwait(false);
var zlibStream = new ZLibStream(cryptoStream, CompressionMode.Decompress, true);
var zlibStream = new ZLibStream(cryptoStream, CompressionMode.Decompress, leaveOpen: true);
await using (zlibStream.ConfigureAwait(false))
{
Expand All @@ -235,7 +235,7 @@ public Task<DataCenterNode> ReadAsync(Stream stream, CancellationToken cancellat
}
}
var root = CreateNode(DataCenterAddress.MinValue, null, cancellationToken);
var root = CreateNode(DataCenterAddress.MinValue, parent: null, cancellationToken);
Check.Data(root != null, $"Root node is empty.");
Check.Data(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ protected override LazyImmutableDataCenterNode AllocateNode(
children = new(raw.ChildCount);
@this.ReadChildren(
raw, node, children, static (children, node) => children.Add(node), default);
raw,
node,
children,
static (children, node) => children.Add(node),
CancellationToken.None);
}
return children;
Expand All @@ -82,6 +86,6 @@ protected override LazyImmutableDataCenterNode AllocateNode(
DataCenterAddress address, DataCenterNode? parent, CancellationToken cancellationToken)
{
return _cache.GetValueOrDefault(address) ??
Unsafe.As<LazyImmutableDataCenterNode>(CreateNode(address, parent, default));
Unsafe.As<LazyImmutableDataCenterNode>(CreateNode(address, parent, CancellationToken.None));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ protected override LazyMutableDataCenterNode AllocateNode(
{
var children = new List<DataCenterNode>(raw.ChildCount);
ReadChildren(raw, node, children, static (children, node) => children.Add(node), default);
ReadChildren(
raw, node, children, static (children, node) => children.Add(node), CancellationToken.None);
return children;
});
Expand All @@ -53,6 +54,6 @@ protected override LazyMutableDataCenterNode AllocateNode(
protected override LazyMutableDataCenterNode? ResolveNode(
DataCenterAddress address, DataCenterNode? parent, CancellationToken cancellationToken)
{
return Unsafe.As<LazyMutableDataCenterNode>(CreateNode(address, parent, default));
return Unsafe.As<LazyMutableDataCenterNode>(CreateNode(address, parent, CancellationToken.None));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ protected override DataCenterNode AllocateNode(
{
children = new(raw.ChildCount);
ReadChildren(raw, node, children, static (children, node) => children.Add(node), default);
ReadChildren(
raw, node, children, static (children, node) => children.Add(node), CancellationToken.None);
}
return children;
Expand All @@ -71,6 +72,6 @@ protected override DataCenterNode AllocateNode(
protected override DataCenterNode? ResolveNode(
DataCenterAddress address, DataCenterNode? parent, CancellationToken cancellationToken)
{
return CreateNode(address, parent, default);
return CreateNode(address, parent, CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public DataCenterSegmentedSimpleRegion(int count)
var segs = new List<DataCenterSimpleRegion<T>>(count);

for (var i = 0; i < count; i++)
segs.Add(new(false));
segs.Add(new(offByOne: false));

Segments = segs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Vezel.Novadrop.Data.Serialization.Tables;

internal sealed class DataCenterKeysTableReader
{
private readonly DataCenterSimpleRegion<DataCenterRawKeys> _keys = new(false);
private readonly DataCenterSimpleRegion<DataCenterRawKeys> _keys = new(offByOne: false);

private readonly List<DataCenterKeys> _byIndex = new(ushort.MaxValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Vezel.Novadrop.Data.Serialization.Tables;

internal sealed class DataCenterKeysTableWriter
{
private readonly DataCenterSimpleRegion<DataCenterRawKeys> _keys = new(false);
private readonly DataCenterSimpleRegion<DataCenterRawKeys> _keys = new(offByOne: false);

private readonly Dictionary<(string?, string?, string?, string?), int> _indexes = new(ushort.MaxValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class DataCenterStringTableReader

private readonly DataCenterSegmentedSimpleRegion<DataCenterRawString> _strings;

private readonly DataCenterSimpleRegion<DataCenterRawAddress> _addresses = new(true);
private readonly DataCenterSimpleRegion<DataCenterRawAddress> _addresses = new(offByOne: true);

private readonly Dictionary<DataCenterAddress, string> _byAddress = new(ushort.MaxValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class DataCenterStringTableWriter

private readonly DataCenterSegmentedSimpleRegion<DataCenterRawString> _strings;

private readonly DataCenterSimpleRegion<DataCenterRawAddress> _addresses = new(true);
private readonly DataCenterSimpleRegion<DataCenterRawAddress> _addresses = new(offByOne: true);

private readonly Dictionary<string, DataCenterRawString> _entries = new(ushort.MaxValue);

Expand Down
8 changes: 4 additions & 4 deletions src/formats/Resources/ResourceContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static Task<ResourceContainer> LoadAsync(
using var xor = CreateCipher(options.Key);
using var decryptor = xor.CreateDecryptor();
var dirStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read, true);
var dirStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read, leaveOpen: true);
var entries = new List<(string, int, int)>();
await using (dirStream.ConfigureAwait(false))
Expand Down Expand Up @@ -95,7 +95,7 @@ public static Task<ResourceContainer> LoadAsync(
{
stream.Position = offset;
var entryStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read, true);
var entryStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read, leaveOpen: true);
var data = GC.AllocateUninitializedArray<byte>(length);
await using (entryStream.ConfigureAwait(false))
Expand Down Expand Up @@ -136,7 +136,7 @@ public Task SaveAsync(
foreach (var (name, entry) in _entries)
{
var entryStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write, true);
var entryStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write, leaveOpen: true);
var entryWriter = new StreamBinaryWriter(entryStream);
await using (entryStream.ConfigureAwait(false))
Expand All @@ -151,7 +151,7 @@ public Task SaveAsync(
var dirStart = stream.Position;
var dirStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write, true);
var dirStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write, leaveOpen: true);
var dirWriter = new StreamBinaryWriter(dirStream);
await using (dirStream.ConfigureAwait(false))
Expand Down
Loading

0 comments on commit d03d485

Please sign in to comment.