Skip to content

Commit

Permalink
Hacking in config and stuff like that.
Browse files Browse the repository at this point in the history
Scratching out sound.
  • Loading branch information
davidwhitney committed Apr 18, 2020
1 parent c33f4f0 commit 6c3cd14
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 878 deletions.
12 changes: 3 additions & 9 deletions CoreBoy/CoreBoy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
<LangVersion>8.0</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>CoreBoy</RootNamespace>
<UseWindowsForms>true</UseWindowsForms>
<AssemblyName>CoreBoy</AssemblyName>
<StartupObject>CoreBoy.Program</StartupObject>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<Compile Remove="debugging\Console.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="mario.gb" />
<None Remove="tetris.gb" />
Expand All @@ -29,10 +25,8 @@
</ItemGroup>

<ItemGroup>
<None Include="debugging\Console.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.7.82" />
<PackageReference Include="NAudio" Version="1.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
</ItemGroup>
Expand Down
83 changes: 71 additions & 12 deletions CoreBoy/GameboyOptions.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,70 @@
using System;
using System.Collections.Generic;
using System.IO;
using CommandLine;

namespace CoreBoy
{
public class GameboyOptions
{
public FileInfo RomFile { get; }
public bool ForceDmg { get; }
public bool ForceCgb { get; }
public bool UseBootstrap { get; }
public bool DisableBatterySaves { get; }
public bool Debug { get; }
public bool Headless { get; }
public FileInfo? RomFile => string.IsNullOrWhiteSpace(Rom) ? null : new FileInfo(Rom);

[Option('r', "rom", Required = false, HelpText = "Rom file.")]
public string Rom { get; set; }

[Option('d', "force-dmg", Required = false, HelpText = "ForceDmg.")]
public bool ForceDmg { get; set; }

[Option('c', "force-cgb", Required = false, HelpText = "ForceCgb.")]
public bool ForceCgb { get; set; }

[Option('b', "use-bootstrap", Required = false, HelpText = "UseBootstrap.")]
public bool UseBootstrap { get; set; }

[Option("disable-battery-saves", Required = false, HelpText = "disable-battery-saves.")]
public bool DisableBatterySaves { get; set; }

[Option("debug", Required = false, HelpText = "Debug.")]
public bool Debug { get; set; }

[Option("headless", Required = false, HelpText = "headless.")]
public bool Headless { get; set; }

public bool ShowUi => !Headless;

public bool IsSupportBatterySaves() => !DisableBatterySaves;

public bool RomSpecified => !string.IsNullOrWhiteSpace(Rom);

public GameboyOptions()
{
}

public GameboyOptions(FileInfo romFile) : this(romFile, new string[0], new string[0])
{
}

public GameboyOptions(FileInfo romFile, ICollection<string> longParameters, ICollection<string> shortParams)
{
RomFile = romFile;
Rom = romFile.FullName;
ForceDmg = longParameters.Contains("force-dmg") || shortParams.Contains("d");
ForceCgb = longParameters.Contains("force-cgb") || shortParams.Contains("c");

if (ForceDmg && ForceCgb)
{
throw new ArgumentException("force-dmg and force-cgb options are can't be used together");
}

UseBootstrap = longParameters.Contains("use-bootstrap") || shortParams.Contains("b");
DisableBatterySaves = longParameters.Contains("disable-battery-saves") || shortParams.Contains("db");
Debug = longParameters.Contains("debug");
Headless = longParameters.Contains("headless");

Verify();
}

public void Verify()
{
if (ForceDmg && ForceCgb)
{
throw new ArgumentException("force-dmg and force-cgb options are can't be used together");
}
}

public static void PrintUsage(TextWriter stream)
Expand All @@ -50,6 +81,34 @@ public static void PrintUsage(TextWriter stream)
stream.WriteLine(" --headless Start in the headless mode");
stream.Flush();
}

public static GameboyOptions Parse(string[] args)
{
var parser = new Parser(cfg =>
{
cfg.AutoHelp = true;
cfg.HelpWriter = Console.Out;
});

var result = parser.ParseArguments<GameboyOptions>(args)
.WithParsed(o => { o.Verify(); });


if (result is Parsed<GameboyOptions> parsed)
{
if (args.Length == 1 && args[0].Contains(".gb"))
{
parsed.Value.Rom = args[0];
}

return parsed.Value;
}
else
{
Console.WriteLine("Failed to parsed!");
return null;
}
}
}
}

60 changes: 32 additions & 28 deletions CoreBoy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,56 @@
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using CommandLine;
using CoreBoy.gui;

namespace CoreBoy
{
static class Program
public static class Program
{
[STAThread]
static void Main(string[] args)
public static void Main(string[] args)
{
var cancellation = new CancellationTokenSource();
var arguments = GameboyOptions.Parse(args);

Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();

var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;

var arguments = new List<string>(args);

PromptForRom(arguments);

var emulator = new Emulator(arguments);
var ui = new WinFormsEmulatorSurface();

emulator.Controller = ui;
emulator.Display.OnFrameProduced += ui.UpdateDisplay;
ui.Closed += (sender, e) =>
if (!arguments.RomSpecified && arguments.ShowUi)
{
cancellationTokenSource.Cancel();
};

emulator.Run(token);
Application.Run(ui);
}

private static void PromptForRom(List<string> arguments)
{
if (arguments.Any()) return;
var (success, romPath) = WinFormsEmulatorSurface.PromptForRom();
arguments.Rom = success ? romPath : string.Empty;
}

using var openFileDialog = new OpenFileDialog
if (!arguments.RomSpecified)
{
GameboyOptions.PrintUsage(Console.Out);
Console.Out.Flush();
Environment.Exit(1);
}

if (arguments.ShowUi)
{
Filter = "Gameboy ROM (*.gb)|*.gb| All files(*.*) |*.*", FilterIndex = 0, RestoreDirectory = true
};
var ui = new WinFormsEmulatorSurface();
ui.Closed += (_, e) => { cancellation.Cancel(); };
emulator.Controller = ui;
emulator.Display.OnFrameProduced += ui.UpdateDisplay;

if (openFileDialog.ShowDialog() == DialogResult.OK)
emulator.Run(cancellation.Token);
Application.Run(ui);
}
else
{
arguments.Add(openFileDialog.FileName);
emulator.Run(cancellation.Token);
Console.WriteLine("Emulator running headless.");
Console.WriteLine("Press ANY key to exit.");
Console.ReadKey(true);

cancellation.Cancel();
}
}
}
Expand Down
114 changes: 0 additions & 114 deletions CoreBoy/debugging/Console.cs

This file was deleted.

11 changes: 0 additions & 11 deletions CoreBoy/debugging/ConsoleUtil.java

This file was deleted.

22 changes: 0 additions & 22 deletions CoreBoy/debugging/command/Quit.java

This file was deleted.

Loading

0 comments on commit 6c3cd14

Please sign in to comment.