Some minor cleanups and optimizations (#4174)

* Replace Array.Clear(x, 0, x.Length) with Array.Clear(x)

* Use DateTime.UnixEpoch field

* Replace SHA256.ComputeHash calls with static SHA256.HashData call

More performant and avoids the need to initialize a SHA256 instance.
This commit is contained in:
Berkan Diler 2022-12-24 18:30:39 +01:00 committed by GitHub
parent f906eb06c2
commit 37d27c4c99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 33 deletions

View file

@ -140,7 +140,7 @@ namespace ARMeilleure.Translation
} }
} }
Array.Clear(localDefs, 0, localDefs.Length); Array.Clear(localDefs);
} }
} }

View file

@ -4,8 +4,6 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
{ {
public class UserProfile public class UserProfile
{ {
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public UserId UserId { get; } public UserId UserId { get; }
public long LastModifiedTimestamp { get; set; } public long LastModifiedTimestamp { get; set; }
@ -83,7 +81,7 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
private void UpdateLastModifiedTimestamp() private void UpdateLastModifiedTimestamp()
{ {
LastModifiedTimestamp = (long)(DateTime.Now - Epoch).TotalSeconds; LastModifiedTimestamp = (long)(DateTime.Now - DateTime.UnixEpoch).TotalSeconds;
} }
} }
} }

View file

@ -100,27 +100,24 @@ namespace Ryujinx.HLE.HOS.Services.Caps
Unknown0x1f = 1 Unknown0x1f = 1
}; };
using (SHA256 sha256Hash = SHA256.Create()) // NOTE: The hex hash is a HMAC-SHA256 (first 32 bytes) using a hardcoded secret key over the titleId, we can simulate it by hashing the titleId instead.
string hash = BitConverter.ToString(SHA256.HashData(BitConverter.GetBytes(titleId))).Replace("-", "").Remove(0x20);
string folderPath = Path.Combine(_sdCardPath, "Nintendo", "Album", currentDateTime.Year.ToString("00"), currentDateTime.Month.ToString("00"), currentDateTime.Day.ToString("00"));
string filePath = GenerateFilePath(folderPath, applicationAlbumEntry, currentDateTime, hash);
// TODO: Handle that using the FS service implementation and return the right error code instead of throwing exceptions.
Directory.CreateDirectory(folderPath);
while (File.Exists(filePath))
{ {
// NOTE: The hex hash is a HMAC-SHA256 (first 32 bytes) using a hardcoded secret key over the titleId, we can simulate it by hashing the titleId instead. applicationAlbumEntry.AlbumFileDateTime.UniqueId++;
string hash = BitConverter.ToString(sha256Hash.ComputeHash(BitConverter.GetBytes(titleId))).Replace("-", "").Remove(0x20);
string folderPath = Path.Combine(_sdCardPath, "Nintendo", "Album", currentDateTime.Year.ToString("00"), currentDateTime.Month.ToString("00"), currentDateTime.Day.ToString("00"));
string filePath = GenerateFilePath(folderPath, applicationAlbumEntry, currentDateTime, hash);
// TODO: Handle that using the FS service implementation and return the right error code instead of throwing exceptions. filePath = GenerateFilePath(folderPath, applicationAlbumEntry, currentDateTime, hash);
Directory.CreateDirectory(folderPath);
while (File.Exists(filePath))
{
applicationAlbumEntry.AlbumFileDateTime.UniqueId++;
filePath = GenerateFilePath(folderPath, applicationAlbumEntry, currentDateTime, hash);
}
// NOTE: The saved JPEG file doesn't have the limitation in the extra EXIF data.
Image.LoadPixelData<Rgba32>(screenshotData, 1280, 720).SaveAsJpegAsync(filePath);
} }
// NOTE: The saved JPEG file doesn't have the limitation in the extra EXIF data.
Image.LoadPixelData<Rgba32>(screenshotData, 1280, 720).SaveAsJpegAsync(filePath);
return ResultCode.Success; return ResultCode.Success;
} }

View file

@ -24,9 +24,7 @@ namespace Ryujinx.HLE.HOS.Services.Pcv.Bpc
public static ResultCode GetExternalRtcValue(out ulong rtcValue) public static ResultCode GetExternalRtcValue(out ulong rtcValue)
{ {
// TODO: emulate MAX77620/MAX77812 RTC // TODO: emulate MAX77620/MAX77812 RTC
DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); rtcValue = (ulong)(DateTime.Now.ToUniversalTime() - DateTime.UnixEpoch).TotalSeconds;
rtcValue = (ulong)(DateTime.Now.ToUniversalTime() - unixEpoch).TotalSeconds;
return ResultCode.Success; return ResultCode.Success;
} }

View file

@ -142,14 +142,9 @@ namespace Ryujinx.HLE.HOS.Services.Ro
_owner.CpuMemory.Read(nroAddress, nroData); _owner.CpuMemory.Read(nroAddress, nroData);
byte[] nroHash = null;
MemoryStream stream = new MemoryStream(nroData); MemoryStream stream = new MemoryStream(nroData);
using (SHA256 hasher = SHA256.Create()) byte[] nroHash = SHA256.HashData(stream);
{
nroHash = hasher.ComputeHash(stream);
}
if (!IsNroHashPresent(nroHash)) if (!IsNroHashPresent(nroHash))
{ {

View file

@ -10,8 +10,6 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
public static readonly TimeSpanType Zero = new TimeSpanType(0); public static readonly TimeSpanType Zero = new TimeSpanType(0);
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public long NanoSeconds; public long NanoSeconds;
public TimeSpanType(long nanoSeconds) public TimeSpanType(long nanoSeconds)
@ -31,7 +29,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
public bool IsDaylightSavingTime() public bool IsDaylightSavingTime()
{ {
return UnixEpoch.AddSeconds(ToSeconds()).ToLocalTime().IsDaylightSavingTime(); return DateTime.UnixEpoch.AddSeconds(ToSeconds()).ToLocalTime().IsDaylightSavingTime();
} }
public static TimeSpanType FromSeconds(long seconds) public static TimeSpanType FromSeconds(long seconds)