Initial commit

This commit is contained in:
Gericom
2025-11-22 11:08:28 +01:00
commit 9cf3ffbfcf
358 changed files with 58350 additions and 0 deletions

367
tools/PicoLoaderConverter/.gitignore vendored Normal file
View File

@@ -0,0 +1,367 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
.idea/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# Wavefront obj resources
!*/Resources/Models/*.obj

View File

@@ -0,0 +1,22 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PicoLoaderConverter", "PicoLoaderConverter\PicoLoaderConverter.csproj", "{6E300AFC-8AC1-45C8-8B87-51CFED9B7DF1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6E300AFC-8AC1-45C8-8B87-51CFED9B7DF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E300AFC-8AC1-45C8-8B87-51CFED9B7DF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E300AFC-8AC1-45C8-8B87-51CFED9B7DF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E300AFC-8AC1-45C8-8B87-51CFED9B7DF1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,11 @@
namespace PicoLoaderConverter.ApList;
sealed class ApList
{
public IReadOnlyList<ApListEntry> Entries { get; }
public ApList(IEnumerable<ApListEntry> entries)
{
Entries = entries.ToArray();
}
}

View File

@@ -0,0 +1,15 @@
namespace PicoLoaderConverter.ApList;
sealed record ApListEntry(
uint GameCode,
byte GameVersion,
DSProtectVersion DSProtectVersion,
byte DSProtectFunctionMask,
ushort RegularOverlayId,
ushort SOverlayId,
uint RegularOffset,
uint SOffset)
{
public const ushort OVERLAY_ID_STATIC_ARM9 = 0xFFFE;
public const ushort OVERLAY_ID_INVALID = 0xFFFF;
}

View File

@@ -0,0 +1,207 @@
using CsvHelper.Configuration;
using CsvHelper;
using System.Globalization;
namespace PicoLoaderConverter.ApList;
sealed class ApListFactory
{
private const int BINARY_AP_LIST_ENTRY_SIZE = 16;
public ApList FromBinary(byte[] data)
{
int entryCount = data.Length / BINARY_AP_LIST_ENTRY_SIZE;
var entries = new ApListEntry[entryCount];
using (var reader = new BinaryReader(new MemoryStream(data)))
{
for (int i = 0; i < entryCount; i++)
{
entries[i] = ReadBinaryApListEntry(reader);
}
}
return new ApList(entries);
}
public byte[] ToBinary(ApList apList)
{
var memoryStream = new MemoryStream();
using var writer = new BinaryWriter(memoryStream);
foreach (var entry in apList.Entries)
{
WriteBinaryApListEntry(writer, entry);
}
return memoryStream.ToArray();
}
public ApList FromCsv(string csv)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ";"
};
using var reader = new StringReader(csv);
using var csvReader = new CsvReader(reader, config);
var csvEntries = csvReader
.GetRecords<CsvApListEntry>()
.Select(ConvertFromCvsApListEntry)
.OrderBy(entry => entry.GameCode)
.ThenBy(entry => entry.GameVersion)
.ToArray();
return new ApList(csvEntries);
}
public string ToCsv(ApList apList)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ";"
};
using var writer = new StringWriter();
using var csvWriter = new CsvWriter(writer, config);
csvWriter.WriteRecords(
apList.Entries
.Select(ConvertToCvsApListEntry)
.OrderBy(entry => entry.GameCode)
.ThenBy(entry => entry.GameVersion));
return writer.ToString();
}
private ApListEntry ReadBinaryApListEntry(BinaryReader reader)
{
uint gameCode = reader.ReadUInt32();
ushort info = reader.ReadUInt16();
byte gameVersion = (byte)(info & 0x1F);
var dsProtectVersion = (DSProtectVersion)((info >> 5) & 0x1F);
byte dsProtectFunctionMask = (byte)(info >> 10);
ushort regularOverlayId = reader.ReadUInt16();
ushort sOverlayId = reader.ReadUInt16();
uint regularOffset = ReadUInt24(reader);
uint sOffset = ReadUInt24(reader);
return new ApListEntry(
gameCode,
gameVersion,
dsProtectVersion,
dsProtectFunctionMask,
regularOverlayId,
sOverlayId,
regularOffset,
sOffset);
}
private uint ReadUInt24(BinaryReader reader)
{
var bytes = reader.ReadBytes(3);
return (uint)(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16));
}
public void WriteBinaryApListEntry(BinaryWriter writer, ApListEntry entry)
{
writer.Write(entry.GameCode);
writer.Write((ushort)(entry.GameVersion | ((byte)entry.DSProtectVersion << 5) | (entry.DSProtectFunctionMask << 10)));
writer.Write(entry.RegularOverlayId);
writer.Write(entry.SOverlayId);
writer.Write((byte)(entry.RegularOffset & 0xFF));
writer.Write((byte)((entry.RegularOffset >> 8) & 0xFF));
writer.Write((byte)((entry.RegularOffset >> 16) & 0xFF));
writer.Write((byte)(entry.SOffset & 0xFF));
writer.Write((byte)((entry.SOffset >> 8) & 0xFF));
writer.Write((byte)((entry.SOffset >> 16) & 0xFF));
}
private ApListEntry ConvertFromCvsApListEntry(CsvApListEntry csvApListEntry)
{
return new ApListEntry(
GameCodeToUint(csvApListEntry.GameCode),
(byte)csvApListEntry.GameVersion,
ParseDSProtectVersion(csvApListEntry.DSProtectVersion),
(byte)csvApListEntry.DSProtectFunctionMask,
(ushort)csvApListEntry.RegularOverlayId,
(ushort)csvApListEntry.SOverlayId,
(uint)csvApListEntry.RegularOffset,
(uint)csvApListEntry.SOffset);
}
private CsvApListEntry ConvertToCvsApListEntry(ApListEntry apListEntry)
{
return new CsvApListEntry
{
GameCode = $"{(char)(apListEntry.GameCode & 0xFF)}{(char)((apListEntry.GameCode >> 8) & 0xFF)}" +
$"{(char)((apListEntry.GameCode >> 16) & 0xFF)}{(char)(apListEntry.GameCode >> 24)}",
GameVersion = apListEntry.GameVersion,
DSProtectVersion = FormatDSProtectVersion(apListEntry.DSProtectVersion),
DSProtectFunctionMask = apListEntry.DSProtectFunctionMask,
RegularOverlayId = (short)apListEntry.RegularOverlayId,
SOverlayId = (short)apListEntry.SOverlayId,
RegularOffset = (int)apListEntry.RegularOffset,
SOffset = (int)apListEntry.SOffset
};
}
private uint GameCodeToUint(string gameCode)
{
if (gameCode.Length != 4)
{
throw new ArgumentException(
$"Game code '{gameCode}' is not valid. It must consist of exactly 4 characters.", nameof(gameCode));
}
return (uint)gameCode[0] | ((uint)gameCode[1] << 8) | ((uint)gameCode[2] << 16) | ((uint)gameCode[3] << 24);
}
private DSProtectVersion ParseDSProtectVersion(string dsProtectVersion)
{
return dsProtectVersion switch
{
"1.05" => DSProtectVersion.V1_05,
"1.06" => DSProtectVersion.V1_06,
"1.08" => DSProtectVersion.V1_08,
"1.10" => DSProtectVersion.V1_10,
"1.20" => DSProtectVersion.V1_20,
"1.22" => DSProtectVersion.V1_22,
"1.23" => DSProtectVersion.V1_23,
"1.23Z" => DSProtectVersion.V1_23Z,
"1.25" => DSProtectVersion.V1_25,
"1.26" => DSProtectVersion.V1_26,
"1.27" => DSProtectVersion.V1_27,
"1.28" => DSProtectVersion.V1_28,
"2.00" => DSProtectVersion.V2_00,
"2.01" => DSProtectVersion.V2_01,
"2.03" => DSProtectVersion.V2_03,
"2.05" => DSProtectVersion.V2_05,
"2.00s" => DSProtectVersion.V2_00s,
"2.01s" => DSProtectVersion.V2_01s,
"2.03s" => DSProtectVersion.V2_03s,
"2.05s" => DSProtectVersion.V2_05s,
_ => throw new ArgumentException(
$"DS Protect Version '{dsProtectVersion}' could not be parsed.", nameof(dsProtectVersion))
};
}
private string FormatDSProtectVersion(DSProtectVersion dsProtectVersion)
{
return dsProtectVersion switch
{
DSProtectVersion.V1_05 => "1.05",
DSProtectVersion.V1_06 => "1.06",
DSProtectVersion.V1_08 => "1.08",
DSProtectVersion.V1_10 => "1.10",
DSProtectVersion.V1_20 => "1.20",
DSProtectVersion.V1_22 => "1.22",
DSProtectVersion.V1_23 => "1.23",
DSProtectVersion.V1_23Z => "1.23Z",
DSProtectVersion.V1_25 => "1.25",
DSProtectVersion.V1_26 => "1.26",
DSProtectVersion.V1_27 => "1.27",
DSProtectVersion.V1_28 => "1.28",
DSProtectVersion.V2_00 => "2.00",
DSProtectVersion.V2_01 => "2.01",
DSProtectVersion.V2_03 => "2.03",
DSProtectVersion.V2_05 => "2.05",
DSProtectVersion.V2_00s => "2.00s",
DSProtectVersion.V2_01s => "2.01s",
DSProtectVersion.V2_03s => "2.03s",
DSProtectVersion.V2_05s => "2.05s",
_ => throw new ArgumentException("Invalid DS Protect Version.", nameof(dsProtectVersion))
};
}
}

View File

@@ -0,0 +1,34 @@
using CsvHelper.Configuration.Attributes;
using PicoLoaderConverter.Csv;
namespace PicoLoaderConverter.ApList;
sealed class CsvApListEntry
{
[Name("gameCode")]
public string GameCode { get; set; } = string.Empty;
[Name("gameVersion")]
public int GameVersion { get; set; }
[Name("dsprotVersion")]
public string DSProtectVersion { get; set; } = string.Empty;
[Name("dsprotFuncMask")]
[TypeConverter(typeof(BinaryNumberConverter))]
public int DSProtectFunctionMask { get; set; }
[Name("regularOvlId")]
public int RegularOverlayId { get; set; }
[Name("regularOffset")]
[TypeConverter(typeof(HexNumberConverter))]
public int RegularOffset { get; set; }
[Name("sOvlId")]
public int SOverlayId { get; set; }
[Name("sOffset")]
[TypeConverter(typeof(HexNumberConverter))]
public int SOffset { get; set; }
}

View File

@@ -0,0 +1,25 @@
namespace PicoLoaderConverter.ApList;
enum DSProtectVersion
{
V1_06,
V1_05,
V1_08,
V1_10,
V1_20,
V1_22,
V1_23,
V1_23Z,
V1_25,
V1_26,
V1_27,
V1_28,
V2_00,
V2_01,
V2_03,
V2_05,
V2_00s,
V2_01s,
V2_03s,
V2_05s
}

View File

@@ -0,0 +1,22 @@
using CsvHelper;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
namespace PicoLoaderConverter.Csv;
sealed class BinaryNumberConverter : DefaultTypeConverter
{
public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
{
if (text?.StartsWith("0b") is true)
{
text = text[2..];
}
return Convert.ToInt32(text, 2);
}
public override string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)
{
return Convert.ToString(Convert.ToInt32(value), 2);
}
}

View File

@@ -0,0 +1,22 @@
using CsvHelper;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
namespace PicoLoaderConverter.Csv;
sealed class HexNumberConverter : DefaultTypeConverter
{
public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
{
if (text?.StartsWith("0x") is true)
{
text = text[2..];
}
return Convert.ToInt32(text, 16);
}
public override string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)
{
return "0x" + Convert.ToString(Convert.ToInt32(value), 16);
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="CsvHelper" Version="33.1.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,24 @@
using CommandLine;
using PicoLoaderConverter.Verbs;
namespace PicoLoaderConverter;
static class Program
{
/// <summary>
/// List of verbs supported by the Pico Loader Converter.
/// To make a new verb, create a class implementing <see cref="IConverterVerb"/> and
/// add it to this list.
/// </summary>
private static readonly Type[] sVerbs = [
typeof(ApListConverterVerb),
typeof(SaveListConverterVerb)
];
static void Main(string[] args)
{
Parser.Default
.ParseArguments(args, sVerbs)
.WithParsed<IConverterVerb>(o => o.Run());
}
}

View File

@@ -0,0 +1,9 @@
namespace PicoLoaderConverter.SaveList;
enum CardSaveType
{
None = 0,
Eeprom = 1,
Flash = 2,
Nand = 3
}

View File

@@ -0,0 +1,17 @@
using CsvHelper.Configuration.Attributes;
using PicoLoaderConverter.Csv;
namespace PicoLoaderConverter.SaveList;
sealed class CsvSaveListEntry
{
[Name("gameCode")]
public string GameCode { get; set; } = string.Empty;
[Name("saveType")]
public string SaveType { get; set; } = string.Empty;
[Name("saveSize")]
[TypeConverter(typeof(HexNumberConverter))]
public int SaveSize { get; set; }
}

View File

@@ -0,0 +1,11 @@
namespace PicoLoaderConverter.SaveList;
sealed class SaveList
{
public IReadOnlyList<SaveListEntry> Entries { get; }
public SaveList(IEnumerable<SaveListEntry> entries)
{
Entries = entries.ToArray();
}
}

View File

@@ -0,0 +1,6 @@
namespace PicoLoaderConverter.SaveList;
sealed record SaveListEntry(
uint GameCode,
CardSaveType SaveType,
byte SaveSize);

View File

@@ -0,0 +1,150 @@
using CsvHelper.Configuration;
using CsvHelper;
using System.Globalization;
using System.Numerics;
namespace PicoLoaderConverter.SaveList;
sealed class SaveListFactory
{
private const int BINARY_SAVE_LIST_ENTRY_SIZE = 8;
public SaveList FromBinary(byte[] data)
{
int entryCount = data.Length / BINARY_SAVE_LIST_ENTRY_SIZE;
var entries = new SaveListEntry[entryCount];
using (var reader = new BinaryReader(new MemoryStream(data)))
{
for (int i = 0; i < entryCount; i++)
{
entries[i] = ReadBinarySaveListEntry(reader);
}
}
return new SaveList(entries);
}
public byte[] ToBinary(SaveList saveList)
{
var memoryStream = new MemoryStream();
using var writer = new BinaryWriter(memoryStream);
foreach (var entry in saveList.Entries)
{
WriteBinarySaveListEntry(writer, entry);
}
return memoryStream.ToArray();
}
public SaveList FromCsv(string csv)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ";"
};
using var reader = new StringReader(csv);
using var csvReader = new CsvReader(reader, config);
var csvEntries = csvReader
.GetRecords<CsvSaveListEntry>()
.Select(ConvertFromCvsSaveListEntry)
.OrderBy(entry => entry.GameCode)
.ToArray();
return new SaveList(csvEntries);
}
public string ToCsv(SaveList saveList)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ";"
};
using var writer = new StringWriter();
using var csvWriter = new CsvWriter(writer, config);
csvWriter.WriteRecords(
saveList.Entries
.Select(ConvertToCvsSaveListEntry)
.OrderBy(entry => entry.GameCode));
return writer.ToString();
}
private SaveListEntry ReadBinarySaveListEntry(BinaryReader reader)
{
uint gameCode = reader.ReadUInt32();
var saveType = (CardSaveType)reader.ReadByte();
byte saveSize = reader.ReadByte();
reader.ReadUInt16();
return new SaveListEntry(
gameCode,
saveType,
saveSize);
}
public void WriteBinarySaveListEntry(BinaryWriter writer, SaveListEntry entry)
{
writer.Write(entry.GameCode);
writer.Write((byte)entry.SaveType);
writer.Write(entry.SaveSize);
writer.Write((ushort)0);
}
private SaveListEntry ConvertFromCvsSaveListEntry(CsvSaveListEntry csvSaveListEntry)
{
if (csvSaveListEntry.SaveSize < 0 ||
(csvSaveListEntry.SaveSize > 0 && !BitOperations.IsPow2(csvSaveListEntry.SaveSize)))
{
throw new ArgumentException(
$"Save size 0x{csvSaveListEntry.SaveSize:X} is not supported. It must be a power of two.",
nameof(csvSaveListEntry));
}
return new SaveListEntry(
GameCodeToUint(csvSaveListEntry.GameCode),
ParseSaveType(csvSaveListEntry.SaveType),
(byte)(csvSaveListEntry.SaveSize == 0 ? 0 : BitOperations.Log2((uint)csvSaveListEntry.SaveSize)));
}
private CsvSaveListEntry ConvertToCvsSaveListEntry(SaveListEntry saveListEntry)
{
return new CsvSaveListEntry
{
GameCode = $"{(char)(saveListEntry.GameCode & 0xFF)}{(char)(saveListEntry.GameCode >> 8 & 0xFF)}" +
$"{(char)(saveListEntry.GameCode >> 16 & 0xFF)}{(char)(saveListEntry.GameCode >> 24)}",
SaveType = FormatSaveType(saveListEntry.SaveType),
SaveSize = saveListEntry.SaveSize == 0 ? 0 : (1 << saveListEntry.SaveSize)
};
}
private uint GameCodeToUint(string gameCode)
{
if (gameCode.Length != 4)
{
throw new ArgumentException(
$"Game code '{gameCode}' is not valid. It must consist of exactly 4 characters.", nameof(gameCode));
}
return gameCode[0] | (uint)gameCode[1] << 8 | (uint)gameCode[2] << 16 | (uint)gameCode[3] << 24;
}
private CardSaveType ParseSaveType(string saveType)
{
return saveType.ToLowerInvariant() switch
{
"none" => CardSaveType.None,
"eeprom" => CardSaveType.Eeprom,
"flash" => CardSaveType.Flash,
"nand" => CardSaveType.Nand,
_ => throw new ArgumentException(
$"Save type '{saveType}' could not be parsed.", nameof(saveType))
};
}
private string FormatSaveType(CardSaveType saveType)
{
return saveType switch
{
CardSaveType.None => "none",
CardSaveType.Eeprom => "eeprom",
CardSaveType.Flash => "flash",
CardSaveType.Nand => "nand",
_ => throw new ArgumentException("Invalid card save type.", nameof(saveType))
};
}
}

View File

@@ -0,0 +1,23 @@
using CommandLine;
using PicoLoaderConverter.ApList;
namespace PicoLoaderConverter.Verbs;
[Verb("aplist", HelpText = "Convert ap list from csv to bin.")]
sealed class ApListConverterVerb : IConverterVerb
{
[Option('i', Required = true, HelpText = "Input .csv file.")]
public required string InputFile { get; init; }
[Option('o', Required = true, HelpText = "Output .bin file.")]
public required string OutputFile { get; init; }
public void Run()
{
// Convert ap list from csv to bin
var factory = new ApListFactory();
var apList = factory.FromCsv(File.ReadAllText(InputFile));
var binaryList = factory.ToBinary(apList);
File.WriteAllBytes(OutputFile, binaryList);
}
}

View File

@@ -0,0 +1,12 @@
namespace PicoLoaderConverter.Verbs;
/// <summary>
/// Interface representing a verb supported by the Splatoon DS Converter.
/// </summary>
interface IConverterVerb
{
/// <summary>
/// Runs the verb action.
/// </summary>
void Run();
}

View File

@@ -0,0 +1,23 @@
using CommandLine;
using PicoLoaderConverter.SaveList;
namespace PicoLoaderConverter.Verbs;
[Verb("savelist", HelpText = "Convert save list from csv to bin.")]
sealed class SaveListConverterVerb : IConverterVerb
{
[Option('i', Required = true, HelpText = "Input .csv file.")]
public required string InputFile { get; init; }
[Option('o', Required = true, HelpText = "Output .bin file.")]
public required string OutputFile { get; init; }
public void Run()
{
// Convert save list from csv to bin
var factory = new SaveListFactory();
var apList = factory.FromCsv(File.ReadAllText(InputFile));
var binaryList = factory.ToBinary(apList);
File.WriteAllBytes(OutputFile, binaryList);
}
}