PKHeX/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockUtil.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

144 lines
4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace PKHeX.Core;
/// <summary>
/// Utility logic for dumping <see cref="SCBlock"/> lists for external analysis
/// </summary>
public static class SCBlockUtil
{
public static void ExportAllBlocksAsSingleFile(IReadOnlyList<SCBlock> blocks, string path, SCBlockExportOption option = SCBlockExportOption.All)
{
var data = ExportAllBlocks(blocks, option);
File.WriteAllBytes(path, data);
}
public static byte[] ExportAllBlocks(IReadOnlyList<SCBlock> blocks, SCBlockExportOption option = SCBlockExportOption.None)
{
if (option == SCBlockExportOption.None)
return SwishCrypto.GetDecryptedRawData(blocks);
using var stream = new MemoryStream();
using var bw = new BinaryWriter(stream);
for (var i = 0; i < blocks.Count; i++)
ExportBlock(blocks[i], bw, i, option);
return stream.ToArray();
}
private static void ExportBlock(SCBlock block, BinaryWriter bw, int blockIndex, SCBlockExportOption option)
{
if (option.HasFlag(SCBlockExportOption.DataOnly) && block.Data.Length == 0)
return;
if (option.HasFlag(SCBlockExportOption.FakeHeader))
bw.Write($"BLOCK{blockIndex:0000} {block.Key:X8}");
if (option.HasFlag(SCBlockExportOption.Key))
bw.Write(block.Key);
if (option.HasFlag(SCBlockExportOption.TypeInfo))
{
bw.Write((byte) block.Type);
bw.Write((byte) block.SubType);
}
bw.Write(block.Data);
}
public static string GetBlockFileNameWithoutExtension(SCBlock block)
{
var key = block.Key;
var name = $"{key:X8}";
if (block.HasValue())
name += $" {block.GetValue()}";
return name;
}
public static string GetBlockSummary(SCBlock b)
{
var sb = new StringBuilder(64);
sb.Append("Key: ").AppendFormat("{0:X8}", b.Key).AppendLine();
sb.Append("Type: ").Append(b.Type).AppendLine();
if (b.Data.Length != 0)
sb.Append("Length: ").AppendFormat("{0:X8}", b.Data.Length).AppendLine();
if (b.SubType != 0)
sb.Append("SubType: ").Append(b.SubType).AppendLine();
else if (b.HasValue())
sb.Append("Value: ").Append(b.GetValue()).AppendLine();
return sb.ToString();
}
public static List<string> ImportBlocksFromFolder(string path, ISCBlockArray sav)
{
var failed = new List<string>();
var files = Directory.EnumerateFiles(path);
foreach (var f in files)
{
var fn = Path.GetFileNameWithoutExtension(f);
// Trim off Value summary if present
var space = fn.IndexOf(' ');
if (space != -1)
fn = fn[..space];
var hex = Util.GetHexValue(fn);
try
{
var block = sav.Accessor.GetBlock(hex);
var len = block.Data.Length;
var fi = new FileInfo(f);
if (fi.Length != len)
{
failed.Add(fn);
continue;
}
var data = File.ReadAllBytes(f);
block.ChangeData(data);
}
catch
{
failed.Add(fn);
}
}
return failed;
}
}
[Flags]
public enum SCBlockExportOption
{
None = 0,
/// <summary>
/// Will only export blocks with backing data.
/// </summary>
/// <remarks>Excludes Bool flags from the dump.</remarks>
DataOnly = 1,
/// <summary>
/// Includes the Block Key ahead of the data.
/// </summary>
Key = 2,
/// <summary>
/// Includes the Block Info ahead of the data.
/// </summary>
TypeInfo = 4,
/// <summary>
/// Includes a fake header indicating which block it is in ASCII.
/// </summary>
FakeHeader = 8,
/// <summary>
/// Standard export options.
/// </summary>
All = DataOnly | Key | TypeInfo | FakeHeader,
}