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 try
{ {
foreach (var item in _queue.GetConsumingEnumerable(_cts.Token)) foreach (T item in _queue.GetConsumingEnumerable(_cts.Token))
{ {
_workerAction(item); _workerAction(item);
} }

View file

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

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Buffers; using System.Buffers;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; 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> /// <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) public static void DumpToFile(this ref SequenceReader<byte> reader, string fileFullName)
{ {
var initialConsumed = reader.Consumed; long initialConsumed = reader.Consumed;
reader.Rewind(initialConsumed); 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) while (reader.End == false)
{ {
var span = reader.CurrentSpan; ReadOnlySpan<byte> span = reader.CurrentSpan;
fileStream.Write(span); fileStream.Write(span);
reader.Advance(span.Length); reader.Advance(span.Length);
} }

View file

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

View file

@ -24,7 +24,7 @@ namespace Ryujinx.Common.Helper
return null; 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); string binaryPath = Path.Combine(searchPath, binary);

View file

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

View file

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

View file

@ -193,7 +193,7 @@ namespace Ryujinx.Common.Logging
_stdErrAdapter.Dispose(); _stdErrAdapter.Dispose();
foreach (var target in _logTargets) foreach (ILogTarget target in _logTargets)
{ {
target.Dispose(); target.Dispose();
} }
@ -203,9 +203,9 @@ namespace Ryujinx.Common.Logging
public static IReadOnlyCollection<LogLevel> GetEnabledLevels() 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); List<LogLevel> levels = new(logs.Length);
foreach (var log in logs) foreach (Log? log in logs)
{ {
if (log.HasValue) if (log.HasValue)
levels.Add(log.Value.Level); levels.Add(log.Value.Level);

View file

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

View file

@ -19,21 +19,21 @@ namespace Ryujinx.Common
public static byte[] Read(string filename) public static byte[] Read(string filename)
{ {
var (assembly, path) = ResolveManifestPath(filename); (Assembly assembly, string path) = ResolveManifestPath(filename);
return Read(assembly, path); return Read(assembly, path);
} }
public static Task<byte[]> ReadAsync(string filename) public static Task<byte[]> ReadAsync(string filename)
{ {
var (assembly, path) = ResolveManifestPath(filename); (Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadAsync(assembly, path); return ReadAsync(assembly, path);
} }
public static byte[] Read(Assembly assembly, string filename) public static byte[] Read(Assembly assembly, string filename)
{ {
using var stream = GetStream(assembly, filename); using Stream stream = GetStream(assembly, filename);
if (stream == null) if (stream == null)
{ {
return null; return null;
@ -44,14 +44,14 @@ namespace Ryujinx.Common
public static MemoryOwner<byte> ReadFileToRentedMemory(string filename) public static MemoryOwner<byte> ReadFileToRentedMemory(string filename)
{ {
var (assembly, path) = ResolveManifestPath(filename); (Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadFileToRentedMemory(assembly, path); return ReadFileToRentedMemory(assembly, path);
} }
public static MemoryOwner<byte> ReadFileToRentedMemory(Assembly assembly, string filename) 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 return stream is null
? null ? null
@ -60,7 +60,7 @@ namespace Ryujinx.Common
public async static Task<byte[]> ReadAsync(Assembly assembly, string filename) 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) if (stream == null)
{ {
return null; return null;
@ -71,55 +71,55 @@ namespace Ryujinx.Common
public static string ReadAllText(string filename) public static string ReadAllText(string filename)
{ {
var (assembly, path) = ResolveManifestPath(filename); (Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadAllText(assembly, path); return ReadAllText(assembly, path);
} }
public static Task<string> ReadAllTextAsync(string filename) public static Task<string> ReadAllTextAsync(string filename)
{ {
var (assembly, path) = ResolveManifestPath(filename); (Assembly assembly, string path) = ResolveManifestPath(filename);
return ReadAllTextAsync(assembly, path); return ReadAllTextAsync(assembly, path);
} }
public static string ReadAllText(Assembly assembly, string filename) public static string ReadAllText(Assembly assembly, string filename)
{ {
using var stream = GetStream(assembly, filename); using Stream stream = GetStream(assembly, filename);
if (stream == null) if (stream == null)
{ {
return null; return null;
} }
using var reader = new StreamReader(stream); using StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd(); return reader.ReadToEnd();
} }
public async static Task<string> ReadAllTextAsync(Assembly assembly, string filename) 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) if (stream == null)
{ {
return null; return null;
} }
using var reader = new StreamReader(stream); using StreamReader reader = new StreamReader(stream);
return await reader.ReadToEndAsync(); return await reader.ReadToEndAsync();
} }
public static Stream GetStream(string filename) public static Stream GetStream(string filename)
{ {
var (assembly, path) = ResolveManifestPath(filename); (Assembly assembly, string path) = ResolveManifestPath(filename);
return GetStream(assembly, path); return GetStream(assembly, path);
} }
public static Stream GetStream(Assembly assembly, string filename) public static Stream GetStream(Assembly assembly, string filename)
{ {
var @namespace = assembly.GetName().Name; string @namespace = assembly.GetName().Name;
var manifestUri = @namespace + "." + filename.Replace('/', '.'); string manifestUri = @namespace + "." + filename.Replace('/', '.');
var stream = assembly.GetManifestResourceStream(manifestUri); Stream stream = assembly.GetManifestResourceStream(manifestUri);
return stream; return stream;
} }
@ -133,11 +133,11 @@ namespace Ryujinx.Common
private static (Assembly, string) ResolveManifestPath(string filename) 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) if (segments.Length >= 2)
{ {
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{ {
if (assembly.GetName().Name == segments[0]) 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) public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{ {
// Get information about the source directory // Get information about the source directory
var dir = new DirectoryInfo(sourceDir); DirectoryInfo dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists // Check if the source directory exists
if (!dir.Exists) if (!dir.Exists)
@ -49,7 +49,7 @@ namespace Ryujinx.Common.Utilities
public static string SanitizeFileName(string fileName) 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)); return string.Concat(fileName.Select(c => reservedChars.Contains(c) ? '_' : c));
} }
} }

View file

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

View file

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

View file

@ -18,7 +18,7 @@ namespace Ryujinx.Common.Utilities
{ {
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) 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)) 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)) if (Path.GetExtension(filename).Equals(".XCI", StringComparison.InvariantCultureIgnoreCase))
{ {
var trimmer = new XCIFileTrimmer(filename, log); XCIFileTrimmer trimmer = new XCIFileTrimmer(filename, log);
return trimmer.CanBeTrimmed; return trimmer.CanBeTrimmed;
} }
@ -57,7 +57,7 @@ namespace Ryujinx.Common.Utilities
{ {
if (Path.GetExtension(filename).Equals(".XCI", StringComparison.InvariantCultureIgnoreCase)) if (Path.GetExtension(filename).Equals(".XCI", StringComparison.InvariantCultureIgnoreCase))
{ {
var trimmer = new XCIFileTrimmer(filename, log); XCIFileTrimmer trimmer = new XCIFileTrimmer(filename, log);
return trimmer.CanBeUntrimmed; return trimmer.CanBeUntrimmed;
} }
@ -201,7 +201,7 @@ namespace Ryujinx.Common.Utilities
{ {
long maxReads = readSizeB / XCIFileTrimmer.BufferSize; long maxReads = readSizeB / XCIFileTrimmer.BufferSize;
long read = 0; long read = 0;
var buffer = new byte[BufferSize]; byte[] buffer = new byte[BufferSize];
while (true) while (true)
{ {
@ -267,7 +267,7 @@ namespace Ryujinx.Common.Utilities
try try
{ {
var info = new FileInfo(Filename); FileInfo info = new FileInfo(Filename);
if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{ {
try try
@ -288,7 +288,7 @@ namespace Ryujinx.Common.Utilities
return OperationOutcome.FileSizeChanged; 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 try
{ {
@ -327,7 +327,7 @@ namespace Ryujinx.Common.Utilities
{ {
Log?.Write(LogType.Info, "Untrimming..."); Log?.Write(LogType.Info, "Untrimming...");
var info = new FileInfo(Filename); FileInfo info = new FileInfo(Filename);
if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{ {
try try
@ -348,7 +348,7 @@ namespace Ryujinx.Common.Utilities
return OperationOutcome.FileSizeChanged; 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; long bytesToWriteB = UntrimmedFileSizeB - FileSizeB;
try try
@ -393,7 +393,7 @@ namespace Ryujinx.Common.Utilities
try try
{ {
var buffer = new byte[BufferSize]; byte[] buffer = new byte[BufferSize];
Array.Fill<byte>(buffer, XCIFileTrimmer.PaddingByte); Array.Fill<byte>(buffer, XCIFileTrimmer.PaddingByte);
while (bytesLeftToWriteB > 0) while (bytesLeftToWriteB > 0)