-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (101 loc) · 4.71 KB
/
Program.cs
File metadata and controls
117 lines (101 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using CommandLine;
using GameboyAdvanced.Core;
using GameboyAdvanced.Core.Debug;
using GameboyAdvanced.Core.Rom;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using System.Diagnostics;
using System.Text;
namespace GameboyAdvanced.Sdl2;
internal class Program
{
public class Options
{
[Option('r', "romDirectory", Required = true)]
public string RomDirectory { get; }
[Option('o', "outputDirectory", Required = true)]
public string OutputDirectory { get; }
[Option('b', "bios", Required = true)]
public string Bios { get; }
public Options(string romDirectory, string bios, string outputDirectory)
{
RomDirectory = romDirectory ?? throw new ArgumentNullException(nameof(romDirectory));
Bios = bios ?? throw new ArgumentNullException(nameof(bios));
OutputDirectory = outputDirectory ?? throw new ArgumentNullException(nameof(outputDirectory));
}
}
internal static void Main(string[] args)
{
_ = Parser.Default.ParseArguments<Options>(args)
.WithParsed(o =>
{
var bios = File.ReadAllBytes(o.Bios);
var compatibilityDatabaseContents = new StringBuilder();
_ = compatibilityDatabaseContents
.AppendLine("# Autogenerated Compatibility")
.AppendLine("")
.AppendLine("This file contains a table with all ROMS tested along with some details parsed from the ROM and a screenshot from 100 frames in with no button presses")
.AppendLine("")
.AppendLine("<table>")
.AppendLine(" <thead>")
.AppendLine(" <tr>")
.Append("<td>Filename</td>")
.Append("<td>Game Title</td>")
.Append("<td>Bootable</td>")
.Append("<td>Save Type</td>")
.Append("<td>Image</td>")
.Append("<td>FPS</td>")
.AppendLine(" </tr>")
.AppendLine(" </thead>")
.AppendLine(" <tbody>");
_ = Directory.CreateDirectory(Path.Join(o.OutputDirectory, "images"));
var timer = new Stopwatch();
foreach (var romFile in Directory.EnumerateFiles(o.RomDirectory, "*.gba"))
{
var romFilename = Path.GetFileName(romFile);
var rom = File.ReadAllBytes(romFile);
var gamepak = new GamePak(rom);
var device = new Device(bios, gamepak, new TestDebugger(), true);
double fps = 0;
var bootable = ":heavy_check_mark:";
try
{
timer.Restart();
for (var ii = 0; ii < 500; ii++)
{
device.RunFrame();
}
timer.Stop();
fps = 500.0 / (timer.ElapsedMilliseconds / 1000.0);
}
catch (Exception ex)
{
bootable = $":x: - {ex.Message}";
}
var frameBuffer = device.GetFrame();
var outputFilePath = Path.Join(o.OutputDirectory, "images", romFilename.Replace(".gba", ".png"));
using var outputFile = File.Create(outputFilePath);
Image.LoadPixelData<Rgba32>(frameBuffer, Device.WIDTH, Device.HEIGHT).Save(outputFile, new PngEncoder());
var imgLink = bootable == ":x:"
? ""
: $"<img src=\"./images/{romFilename.Replace(".gba", ".png")}\" alt=\"{romFilename.Replace(".gba", "")}\"></img>";
_ = compatibilityDatabaseContents
.Append("<tr>")
.Append($"<td>{romFilename.Replace(".gba", "")}</td>")
.Append($"<td>{device.Gamepak.GameTitle}</td>")
.Append($"<td>{bootable}</td>")
.Append($"<td>{device.Gamepak.RomBackupType}</td>")
.Append($"<td>{imgLink}</td>")
.Append($"<td>{fps:F2}")
.AppendLine("</tr>");
}
_ = compatibilityDatabaseContents.AppendLine("</table>");
File.WriteAllText(Path.Join(o.OutputDirectory, "readme.md"), compatibilityDatabaseContents.ToString());
})
.WithNotParsed(o =>
{
Console.WriteLine(o);
});
}
}