misc: chore: Use explicit types in common project

This commit is contained in:
Evan Husted 2025-01-25 14:04:12 -06:00
parent 97188556d8
commit a97fd4beb1
15 changed files with 59 additions and 56 deletions

View file

@ -34,7 +34,7 @@ namespace Ryujinx.Common
{
try
{
foreach (var item in _queue.GetConsumingEnumerable(_cts.Token))
foreach (T item in _queue.GetConsumingEnumerable(_cts.Token))
{
_workerAction(item);
}

View file

@ -23,7 +23,7 @@ namespace Ryujinx.Common.Configuration
public static EnabledDirtyHack Unpack(ulong packedHack)
{
var unpackedFields = packedHack.UnpackBitFields(PackedFormat);
uint[] unpackedFields = packedHack.UnpackBitFields(PackedFormat);
if (unpackedFields is not [var hack, var value])
throw new Exception("The unpack operation on the integer resulted in an invalid unpacked result.");
@ -53,7 +53,7 @@ namespace Ryujinx.Common.Configuration
public static implicit operator DirtyHacks(EnabledDirtyHack[] hacks) => new(hacks);
public static implicit operator DirtyHacks(ulong[] packedHacks) => new(packedHacks);
public new int this[DirtyHack hack] => TryGetValue(hack, out var value) ? value : -1;
public new int this[DirtyHack hack] => TryGetValue(hack, out int value) ? value : -1;
public bool IsEnabled(DirtyHack hack) => ContainsKey(hack);
}

View file

@ -1,6 +1,7 @@
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -16,15 +17,15 @@ namespace Ryujinx.Common.Extensions
/// <param name="fileFullName">The path and name of the file to create and dump to</param>
public static void DumpToFile(this ref SequenceReader<byte> reader, string fileFullName)
{
var initialConsumed = reader.Consumed;
long initialConsumed = reader.Consumed;
reader.Rewind(initialConsumed);
using (var fileStream = System.IO.File.Create(fileFullName, 4096, System.IO.FileOptions.None))
using (FileStream fileStream = System.IO.File.Create(fileFullName, 4096, System.IO.FileOptions.None))
{
while (reader.End == false)
{
var span = reader.CurrentSpan;
ReadOnlySpan<byte> span = reader.CurrentSpan;
fileStream.Write(span);
reader.Advance(span.Length);
}

View file

@ -101,7 +101,7 @@ namespace Ryujinx.Common.Helper
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@$"Software\Classes\{ext}");
var openCmd = key?.OpenSubKey(@"shell\open\command");
RegistryKey openCmd = key?.OpenSubKey(@"shell\open\command");
if (openCmd is null)
{
@ -143,7 +143,7 @@ namespace Ryujinx.Common.Helper
}
else
{
using var key = Registry.CurrentUser.CreateSubKey(keyString);
using RegistryKey key = Registry.CurrentUser.CreateSubKey(keyString);
if (key is null)
{
@ -151,7 +151,7 @@ namespace Ryujinx.Common.Helper
}
Logger.Debug?.Print(LogClass.Application, $"Adding type association {ext}");
using var openCmd = key.CreateSubKey(@"shell\open\command");
using RegistryKey openCmd = key.CreateSubKey(@"shell\open\command");
openCmd.SetValue(string.Empty, $"\"{Environment.ProcessPath}\" \"%1\"");
Logger.Debug?.Print(LogClass.Application, $"Added type association {ext}");

View file

@ -24,7 +24,7 @@ namespace Ryujinx.Common.Helper
return null;
}
foreach (var searchPath in pathVar.Split(":", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries))
foreach (string searchPath in pathVar.Split(":", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries))
{
string binaryPath = Path.Combine(searchPath, binary);

View file

@ -60,7 +60,7 @@ namespace Ryujinx.Common.Helper
{
ObjectiveC.NSString nsStringPath = new(path);
ObjectiveC.Object nsUrl = new("NSURL");
var urlPtr = nsUrl.GetFromMessage("fileURLWithPath:", nsStringPath);
ObjectiveC.Object urlPtr = nsUrl.GetFromMessage("fileURLWithPath:", nsStringPath);
ObjectiveC.Object nsArray = new("NSArray");
ObjectiveC.Object urlArray = nsArray.GetFromMessage("arrayWithObject:", urlPtr);
@ -99,7 +99,7 @@ namespace Ryujinx.Common.Helper
{
ObjectiveC.NSString nsStringPath = new(url);
ObjectiveC.Object nsUrl = new("NSURL");
var urlPtr = nsUrl.GetFromMessage("URLWithString:", nsStringPath);
ObjectiveC.Object urlPtr = nsUrl.GetFromMessage("URLWithString:", nsStringPath);
ObjectiveC.Object nsWorkspace = new("NSWorkspace");
ObjectiveC.Object sharedWorkspace = nsWorkspace.GetFromMessage("sharedWorkspace");

View file

@ -41,7 +41,7 @@ namespace Ryujinx.Common.Logging.Formatters
sb.Append('{');
foreach (var prop in props)
foreach (PropertyInfo prop in props)
{
sb.Append(prop.Name);
sb.Append(": ");
@ -52,7 +52,7 @@ namespace Ryujinx.Common.Logging.Formatters
if (array is not null)
{
foreach (var item in array)
foreach (object? item in array)
{
sb.Append(item);
sb.Append(", ");

View file

@ -193,7 +193,7 @@ namespace Ryujinx.Common.Logging
_stdErrAdapter.Dispose();
foreach (var target in _logTargets)
foreach (ILogTarget target in _logTargets)
{
target.Dispose();
}
@ -203,9 +203,9 @@ namespace Ryujinx.Common.Logging
public static IReadOnlyCollection<LogLevel> GetEnabledLevels()
{
var logs = new[] { Debug, Info, Warning, Error, Guest, AccessLog, Stub, Trace };
Log?[] logs = new[] { Debug, Info, Warning, Error, Guest, AccessLog, Stub, Trace };
List<LogLevel> levels = new(logs.Length);
foreach (var log in logs)
foreach (Log? log in logs)
{
if (log.HasValue)
levels.Add(log.Value.Level);

View file

@ -26,7 +26,7 @@ namespace Ryujinx.Common.Logging.Targets
public void Log(object sender, LogEventArgs e)
{
var logEventArgsJson = LogEventArgsJson.FromLogEventArgs(e);
LogEventArgsJson logEventArgsJson = LogEventArgsJson.FromLogEventArgs(e);
JsonHelper.SerializeToStream(_stream, logEventArgsJson, LogEventJsonSerializerContext.Default.LogEventArgsJson);
}

View file

@ -19,21 +19,21 @@ namespace Ryujinx.Common
public static byte[] Read(string filename)
{
var (assembly, path) = ResolveManifestPath(filename);
(Assembly assembly, string path) = ResolveManifestPath(filename);
return Read(assembly, path);
}
public static Task<byte[]> ReadAsync(string filename)
{
var (assembly, path) = ResolveManifestPath(filename);
(Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadAsync(assembly, path);
}
public static byte[] Read(Assembly assembly, string filename)
{
using var stream = GetStream(assembly, filename);
using Stream stream = GetStream(assembly, filename);
if (stream == null)
{
return null;
@ -44,14 +44,14 @@ namespace Ryujinx.Common
public static MemoryOwner<byte> ReadFileToRentedMemory(string filename)
{
var (assembly, path) = ResolveManifestPath(filename);
(Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadFileToRentedMemory(assembly, path);
}
public static MemoryOwner<byte> ReadFileToRentedMemory(Assembly assembly, string filename)
{
using var stream = GetStream(assembly, filename);
using Stream stream = GetStream(assembly, filename);
return stream is null
? null
@ -60,7 +60,7 @@ namespace Ryujinx.Common
public async static Task<byte[]> ReadAsync(Assembly assembly, string filename)
{
using var stream = GetStream(assembly, filename);
using Stream stream = GetStream(assembly, filename);
if (stream == null)
{
return null;
@ -71,55 +71,55 @@ namespace Ryujinx.Common
public static string ReadAllText(string filename)
{
var (assembly, path) = ResolveManifestPath(filename);
(Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadAllText(assembly, path);
}
public static Task<string> ReadAllTextAsync(string filename)
{
var (assembly, path) = ResolveManifestPath(filename);
(Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadAllTextAsync(assembly, path);
}
public static string ReadAllText(Assembly assembly, string filename)
{
using var stream = GetStream(assembly, filename);
using Stream stream = GetStream(assembly, filename);
if (stream == null)
{
return null;
}
using var reader = new StreamReader(stream);
using StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
public async static Task<string> ReadAllTextAsync(Assembly assembly, string filename)
{
using var stream = GetStream(assembly, filename);
using Stream stream = GetStream(assembly, filename);
if (stream == null)
{
return null;
}
using var reader = new StreamReader(stream);
using StreamReader reader = new StreamReader(stream);
return await reader.ReadToEndAsync();
}
public static Stream GetStream(string filename)
{
var (assembly, path) = ResolveManifestPath(filename);
(Assembly assembly, string path) = ResolveManifestPath(filename);
return GetStream(assembly, path);
}
public static Stream GetStream(Assembly assembly, string filename)
{
var @namespace = assembly.GetName().Name;
var manifestUri = @namespace + "." + filename.Replace('/', '.');
string @namespace = assembly.GetName().Name;
string manifestUri = @namespace + "." + filename.Replace('/', '.');
var stream = assembly.GetManifestResourceStream(manifestUri);
Stream stream = assembly.GetManifestResourceStream(manifestUri);
return stream;
}
@ -133,11 +133,11 @@ namespace Ryujinx.Common
private static (Assembly, string) ResolveManifestPath(string filename)
{
var segments = filename.Split('/', 2, StringSplitOptions.RemoveEmptyEntries);
string[] segments = filename.Split('/', 2, StringSplitOptions.RemoveEmptyEntries);
if (segments.Length >= 2)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.GetName().Name == segments[0])
{

View file

@ -9,7 +9,7 @@ namespace Ryujinx.Common.Utilities
public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
DirectoryInfo dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
@ -49,7 +49,7 @@ namespace Ryujinx.Common.Utilities
public static string SanitizeFileName(string fileName)
{
var reservedChars = new HashSet<char>(Path.GetInvalidFileNameChars());
HashSet<char> reservedChars = new HashSet<char>(Path.GetInvalidFileNameChars());
return string.Concat(fileName.Select(c => reservedChars.Contains(c) ? '_' : c));
}
}

View file

@ -1,5 +1,6 @@
using MsgPack;
using System;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.Common.Utilities
@ -18,7 +19,7 @@ namespace Ryujinx.Common.Utilities
public static string Format(MessagePackObject obj)
{
var builder = new IndentedStringBuilder();
IndentedStringBuilder builder = new IndentedStringBuilder();
FormatMsgPackObj(obj, builder);
@ -41,7 +42,7 @@ namespace Ryujinx.Common.Utilities
}
else
{
var literal = obj.ToObject();
object literal = obj.ToObject();
if (literal is String)
{
@ -88,7 +89,7 @@ namespace Ryujinx.Common.Utilities
{
builder.Append("[ ");
foreach (var b in arr)
foreach (byte b in arr)
{
builder.Append("0x");
builder.Append(ToHexChar(b >> 4));
@ -111,7 +112,7 @@ namespace Ryujinx.Common.Utilities
builder.Append("0x");
}
foreach (var b in arr)
foreach (byte b in arr)
{
builder.Append(ToHexChar(b >> 4));
builder.Append(ToHexChar(b & 0xF));
@ -122,7 +123,7 @@ namespace Ryujinx.Common.Utilities
private static void FormatMsgPackMap(MessagePackObject obj, IndentedStringBuilder builder)
{
var map = obj.AsDictionary();
MessagePackObjectDictionary map = obj.AsDictionary();
builder.Append('{');
@ -130,7 +131,7 @@ namespace Ryujinx.Common.Utilities
builder.IncreaseIndent()
.AppendLine();
foreach (var item in map)
foreach (KeyValuePair<MessagePackObject, MessagePackObject> item in map)
{
FormatMsgPackObj(item.Key, builder);
@ -154,11 +155,11 @@ namespace Ryujinx.Common.Utilities
private static void FormatMsgPackArray(MessagePackObject obj, IndentedStringBuilder builder)
{
var arr = obj.AsList();
IList<MessagePackObject> arr = obj.AsList();
builder.Append("[ ");
foreach (var item in arr)
foreach (MessagePackObject item in arr)
{
FormatMsgPackObj(item, builder);

View file

@ -1,5 +1,6 @@
using Microsoft.IO;
using Ryujinx.Common.Memory;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@ -27,7 +28,7 @@ namespace Ryujinx.Common.Utilities
MemoryOwner<byte> ownedMemory = MemoryOwner<byte>.Rent(checked((int)bytesExpected));
var destSpan = ownedMemory.Span;
Span<byte> destSpan = ownedMemory.Span;
int totalBytesRead = 0;

View file

@ -18,7 +18,7 @@ namespace Ryujinx.Common.Utilities
{
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var enumValue = reader.GetString();
string? enumValue = reader.GetString();
if (Enum.TryParse(enumValue, out TEnum value))
{

View file

@ -46,7 +46,7 @@ namespace Ryujinx.Common.Utilities
{
if (Path.GetExtension(filename).Equals(".XCI", StringComparison.InvariantCultureIgnoreCase))
{
var trimmer = new XCIFileTrimmer(filename, log);
XCIFileTrimmer trimmer = new XCIFileTrimmer(filename, log);
return trimmer.CanBeTrimmed;
}
@ -57,7 +57,7 @@ namespace Ryujinx.Common.Utilities
{
if (Path.GetExtension(filename).Equals(".XCI", StringComparison.InvariantCultureIgnoreCase))
{
var trimmer = new XCIFileTrimmer(filename, log);
XCIFileTrimmer trimmer = new XCIFileTrimmer(filename, log);
return trimmer.CanBeUntrimmed;
}
@ -201,7 +201,7 @@ namespace Ryujinx.Common.Utilities
{
long maxReads = readSizeB / XCIFileTrimmer.BufferSize;
long read = 0;
var buffer = new byte[BufferSize];
byte[] buffer = new byte[BufferSize];
while (true)
{
@ -267,7 +267,7 @@ namespace Ryujinx.Common.Utilities
try
{
var info = new FileInfo(Filename);
FileInfo info = new FileInfo(Filename);
if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
try
@ -288,7 +288,7 @@ namespace Ryujinx.Common.Utilities
return OperationOutcome.FileSizeChanged;
}
var outfileStream = new FileStream(_filename, FileMode.Open, FileAccess.Write, FileShare.Write);
FileStream outfileStream = new FileStream(_filename, FileMode.Open, FileAccess.Write, FileShare.Write);
try
{
@ -327,7 +327,7 @@ namespace Ryujinx.Common.Utilities
{
Log?.Write(LogType.Info, "Untrimming...");
var info = new FileInfo(Filename);
FileInfo info = new FileInfo(Filename);
if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
try
@ -348,7 +348,7 @@ namespace Ryujinx.Common.Utilities
return OperationOutcome.FileSizeChanged;
}
var outfileStream = new FileStream(_filename, FileMode.Append, FileAccess.Write, FileShare.Write);
FileStream outfileStream = new FileStream(_filename, FileMode.Append, FileAccess.Write, FileShare.Write);
long bytesToWriteB = UntrimmedFileSizeB - FileSizeB;
try
@ -393,7 +393,7 @@ namespace Ryujinx.Common.Utilities
try
{
var buffer = new byte[BufferSize];
byte[] buffer = new byte[BufferSize];
Array.Fill<byte>(buffer, XCIFileTrimmer.PaddingByte);
while (bytesLeftToWriteB > 0)