Formatting rules. Bleh

This commit is contained in:
Iris Ward 2015-09-22 01:02:49 +01:00
parent 1df8397b1c
commit 3f899b60c9
4 changed files with 560 additions and 478 deletions

View File

@ -68,19 +68,22 @@ namespace BizHawk.Client.Common
}
// Attempt to import another type of movie file into a movie object.
public static Bk2Movie ImportFile(string path, out string errorMsg, out string warningMsg) {
public static Bk2Movie ImportFile(string path, out string errorMsg, out string warningMsg)
{
errorMsg = string.Empty;
warningMsg = string.Empty;
string ext = path != null ? Path.GetExtension(path).ToUpper() : string.Empty;
if (UsesLegacyImporter(ext)) {
if (UsesLegacyImporter(ext))
{
return LegacyImportFile(ext, path, out errorMsg, out warningMsg).ToBk2();
}
var importers = ImportersForExtension(ext);
var importerType = importers.FirstOrDefault();
if (importerType == default(Type)) {
if (importerType == default(Type))
{
errorMsg = "No importer found for file type " + ext;
return null;
}
@ -91,19 +94,23 @@ namespace BizHawk.Client.Common
Bk2Movie movie = null;
try {
try
{
var result = importer.Import(path);
if (result.Errors.Count() > 0) errorMsg = result.Errors.First();
if (result.Warnings.Count() > 0) warningMsg = result.Warnings.First();
movie = result.Movie;
} catch (Exception ex) {
}
catch (Exception ex)
{
errorMsg = ex.ToString();
}
return movie;
}
private static IEnumerable<Type> ImportersForExtension(string ext) {
private static IEnumerable<Type> ImportersForExtension(string ext)
{
var info = typeof(MovieImport).Module;
var importers = from t in info.GetTypes()
where typeof(IMovieImport).IsAssignableFrom(t)
@ -113,25 +120,32 @@ namespace BizHawk.Client.Common
return importers;
}
private static bool TypeImportsExtension(Type t, string ext) {
private static bool TypeImportsExtension(Type t, string ext)
{
var attrs = (ImportExtension[])t.GetCustomAttributes(typeof(ImportExtension), inherit: false);
if (attrs.Where(a => a.Extension.ToUpper() == ext.ToUpper()).Count() > 0) {
if (attrs.Where(a => a.Extension.ToUpper() == ext.ToUpper()).Count() > 0)
{
return true;
} else {
}
else
{
return false;
}
}
private static BkmMovie LegacyImportFile(string ext, string path, out string errorMsg, out string warningMsg) {
private static BkmMovie LegacyImportFile(string ext, string path, out string errorMsg, out string warningMsg)
{
errorMsg = string.Empty;
warningMsg = string.Empty;
BkmMovie m = new BkmMovie();
try {
switch (ext) {
try
{
switch (ext)
{
case ".FCM":
m = ImportFCM(path, out errorMsg, out warningMsg);
break;
@ -179,7 +193,9 @@ namespace BizHawk.Client.Common
m.Load(false);
break;
}
} catch (Exception except) {
}
catch (Exception except)
{
errorMsg = except.ToString();
}
@ -187,7 +203,8 @@ namespace BizHawk.Client.Common
}
// Return whether or not the type of file provided can currently be imported.
public static bool IsValidMovieExtension(string extension) {
public static bool IsValidMovieExtension(string extension)
{
// TODO: Other movie formats that don't use a legacy importer (PJM/PXM, etc),
// when those are implemented
return UsesLegacyImporter(extension);
@ -253,7 +270,7 @@ namespace BizHawk.Client.Common
controller = "Saturn Controller";
break;
}
var controllers = new SimpleController {Type = new ControllerDefinition {Name = controller}};
var controllers = new SimpleController { Type = new ControllerDefinition { Name = controller } };
// Split up the sections of the frame.
string[] sections = line.Split('|');
if (ext == ".FM2" && sections.Length >= 2 && sections[1].Length != 0)
@ -683,7 +700,7 @@ namespace BizHawk.Client.Common
m.Header[HeaderKeys.AUTHOR] = author;
// Advance to first byte of input data.
r.BaseStream.Position = firstFrameOffset;
SimpleController controllers = new SimpleController {Type = new ControllerDefinition {Name = "NES Controller"}};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition { Name = "NES Controller" } };
string[] buttons = { "A", "B", "Select", "Start", "Up", "Down", "Left", "Right" };
bool fds = false;
bool fourscore = false;
@ -914,7 +931,7 @@ namespace BizHawk.Client.Common
*/
m.Header[HeaderKeys.PAL] = "False";
// 090 frame data begins here
SimpleController controllers = new SimpleController {Type = new ControllerDefinition {Name = "NES Controller"}};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition { Name = "NES Controller" } };
/*
* 01 Right
* 02 Left
@ -1418,7 +1435,7 @@ namespace BizHawk.Client.Common
r.ReadBytes(103);
// TODO: Verify if NTSC/"PAL" mode used for the movie can be detected or not.
// 100 variable Input data
SimpleController controllers = new SimpleController {Type = new ControllerDefinition {Name = name + " Controller"}};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition { Name = name + " Controller" } };
int bytes = 256;
// The input stream consists of 1 byte for power-on and reset, and then X bytes per each input port per frame.
if (platform == "nes")
@ -1539,7 +1556,7 @@ namespace BizHawk.Client.Common
// 00e4-00f3: binary: rom MD5 digest
byte[] md5 = r.ReadBytes(16);
m.Header[MD5] = string.Format("{0:x8}", md5.BytesToHexString().ToLower());
var controllers = new SimpleController { Type = new ControllerDefinition { Name = "SMS Controller" }};
var controllers = new SimpleController { Type = new ControllerDefinition { Name = "SMS Controller" } };
/*
76543210
* bit 0 (0x01): up
@ -1764,7 +1781,7 @@ namespace BizHawk.Client.Common
// ... 4-byte little-endian unsigned int: length of controller data in bytes
uint length = r.ReadUInt32();
// ... (variable) controller data
SimpleController controllers = new SimpleController {Type = new ControllerDefinition {Name = "NES Controller"}};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition { Name = "NES Controller" } };
/*
Standard controllers store data in the following format:
* 01: A
@ -1867,7 +1884,7 @@ namespace BizHawk.Client.Common
* bit 4: controller 5 in use
* other: reserved, set to 0
*/
SimpleController controllers = new SimpleController {Type = new ControllerDefinition {Name = "SNES Controller"}};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition { Name = "SNES Controller" } };
bool[] controllersUsed = new bool[5];
for (int controller = 1; controller <= controllersUsed.Length; controller++)
{
@ -2278,7 +2295,7 @@ namespace BizHawk.Client.Common
string movieDescription = NullTerminated(r.ReadStringFixedAscii(128));
m.Comments.Add(COMMENT + " " + movieDescription);
r.BaseStream.Position = firstFrameOffset;
SimpleController controllers = new SimpleController {Type = new ControllerDefinition()};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition() };
if (platform != "GBA")
{
controllers.Type.Name = "Gameboy Controller";
@ -2456,7 +2473,7 @@ namespace BizHawk.Client.Common
return m;
}
r.BaseStream.Position = firstFrameOffset;
SimpleController controllers = new SimpleController {Type = new ControllerDefinition {Name = "NES Controller"}};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition { Name = "NES Controller" } };
/*
* 01 A
* 02 B
@ -2693,7 +2710,7 @@ namespace BizHawk.Client.Common
uint savestateSize = (uint)((r.ReadByte() | (r.ReadByte() << 8) | (r.ReadByte() << 16)) & 0x7FFFFF);
// Next follows a ZST format savestate.
r.ReadBytes((int)savestateSize);
SimpleController controllers = new SimpleController {Type = new ControllerDefinition {Name = "SNES Controller"}};
SimpleController controllers = new SimpleController { Type = new ControllerDefinition { Name = "SNES Controller" } };
/*
* bit 11: A
* bit 10: X

View File

@ -3,7 +3,8 @@
using System;
using System.IO;
namespace BizHawk.Client.Common {
namespace BizHawk.Client.Common
{
[ImportExtension(".pjm")]
public class PJMImport : MovieImporter
{
@ -14,15 +15,20 @@ namespace BizHawk.Client.Common {
movie.HeaderEntries.Add(HeaderKeys.PLATFORM, "PSX");
using (var fs = SourceFile.OpenRead()) {
using (var br = new BinaryReader(fs)) {
using (var fs = SourceFile.OpenRead())
{
using (var br = new BinaryReader(fs))
{
info = parseHeader(movie, "PJM ", br);
fs.Seek(info.controllerDataOffset, SeekOrigin.Begin);
if(info.binaryFormat) {
if (info.binaryFormat)
{
parseBinaryInputLog(br, movie, info);
} else {
}
else
{
parseTextInputLog(br, movie, info);
}
}
@ -31,17 +37,20 @@ namespace BizHawk.Client.Common {
movie.Save();
}
protected MiscHeaderInfo parseHeader(Bk2Movie movie, string expectedMagic, BinaryReader br) {
protected MiscHeaderInfo parseHeader(Bk2Movie movie, string expectedMagic, BinaryReader br)
{
var info = new MiscHeaderInfo();
string magic = new string(br.ReadChars(4));
if (magic != expectedMagic) {
if (magic != expectedMagic)
{
Result.Errors.Add("Not a " + expectedMagic + "file: invalid magic number in file header.");
return info;
}
UInt32 movieVersionNumber = br.ReadUInt32();
if (movieVersionNumber != 2) {
if (movieVersionNumber != 2)
{
Result.Warnings.Add(String.Format("Unexpected movie version: got {0}, expecting 2", movieVersionNumber));
}
@ -50,31 +59,39 @@ namespace BizHawk.Client.Common {
byte flags = br.ReadByte();
byte flags2 = br.ReadByte();
if ((flags & 0x02) != 0) {
if ((flags & 0x02) != 0)
{
Result.Errors.Add("Movie starts from savestate; this is currently unsupported.");
}
if ((flags & 0x04) != 0) {
if ((flags & 0x04) != 0)
{
movie.HeaderEntries.Add(HeaderKeys.PAL, "1");
}
if ((flags & 0x08) != 0) {
if ((flags & 0x08) != 0)
{
Result.Errors.Add("Movie contains embedded memory cards; this is currently unsupported.");
}
if ((flags & 0x10) != 0) {
if ((flags & 0x10) != 0)
{
Result.Errors.Add("Movie contains embedded cheat list; this is currently unsupported.");
}
if ((flags & 0x20) != 0 || (flags2 & 0x06) != 0) {
if ((flags & 0x20) != 0 || (flags2 & 0x06) != 0)
{
Result.Errors.Add("Movie relies on emulator hacks; this is currently unsupported.");
}
if ((flags & 0x40) != 0) {
if ((flags & 0x40) != 0)
{
info.binaryFormat = false;
}
if ((flags & 0x80) != 0 || (flags2 & 0x01) != 0) {
if ((flags & 0x80) != 0 || (flags2 & 0x01) != 0)
{
Result.Errors.Add("Movie uses multitap; this is currently unsupported.");
return info;
}
// Player 1 controller type
switch (br.ReadByte()) {
switch (br.ReadByte())
{
// It seems to be inconsistent in the files I looked at which of these is used
// to mean no controller present.
case 0:
@ -93,7 +110,8 @@ namespace BizHawk.Client.Common {
}
// Player 2 controller type
switch (br.ReadByte()) {
switch (br.ReadByte())
{
case 0:
case 8:
info.player2Type.IsConnected = false;
@ -135,7 +153,8 @@ namespace BizHawk.Client.Common {
return info;
}
protected void parseBinaryInputLog(BinaryReader br, Bk2Movie movie, MiscHeaderInfo info) {
protected void parseBinaryInputLog(BinaryReader br, Bk2Movie movie, MiscHeaderInfo info)
{
Octoshock.SyncSettings settings = new Octoshock.SyncSettings();
SimpleController controllers = new SimpleController();
settings.Controllers = new[] { info.player1Type, info.player2Type };
@ -146,22 +165,27 @@ namespace BizHawk.Client.Common {
bool isCdTrayOpen = false;
for (int frame = 0; frame < info.frameCount; ++frame) {
if (info.player1Type.IsConnected) {
for (int frame = 0; frame < info.frameCount; ++frame)
{
if (info.player1Type.IsConnected)
{
UInt16 controllerState = br.ReadUInt16();
// As L3 and R3 don't exist on a standard gamepad, handle them separately later. Unfortunately
// due to the layout, we handle select separately too first.
controllers["P1 Select"] = (controllerState & 0x1) != 0;
for (int button = 3; button < buttons.Length; button++) {
for (int button = 3; button < buttons.Length; button++)
{
controllers["P1 " + buttons[button]] = (((controllerState >> button) & 0x1) != 0);
if (((controllerState >> button) & 0x1) != 0 && button > 15) {
if (((controllerState >> button) & 0x1) != 0 && button > 15)
{
continue;
}
}
if(info.player1Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad) {
if (info.player1Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad)
{
controllers["P1 L3"] = (controllerState & 0x2) != 0;
controllers["P1 R3"] = (controllerState & 0x4) != 0;
Tuple<string, float> leftX = new Tuple<string, float>("P1 LStick X", (float)br.ReadByte());
@ -173,16 +197,20 @@ namespace BizHawk.Client.Common {
}
}
if (info.player2Type.IsConnected) {
if (info.player2Type.IsConnected)
{
UInt16 controllerState = br.ReadUInt16();
for (int button = 0; button < buttons.Length; button++) {
for (int button = 0; button < buttons.Length; button++)
{
controllers["P2 " + buttons[button]] = (((controllerState >> button) & 0x1) != 0);
if (((controllerState >> button) & 0x1) != 0 && button > 15) {
if (((controllerState >> button) & 0x1) != 0 && button > 15)
{
continue;
}
}
if (info.player2Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad) {
if (info.player2Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad)
{
Tuple<string, float> leftX = new Tuple<string, float>("P2 LStick X", (float)br.ReadByte());
Tuple<string, float> leftY = new Tuple<string, float>("P2 LStick Y", (float)br.ReadByte());
Tuple<string, float> rightX = new Tuple<string, float>("P2 RStick X", (float)br.ReadByte());
@ -194,19 +222,26 @@ namespace BizHawk.Client.Common {
byte controlState = br.ReadByte();
controllers["Reset"] = (controlState & 0x02) != 0;
if((controlState & 0x04) != 0) {
if(isCdTrayOpen) {
if ((controlState & 0x04) != 0)
{
if (isCdTrayOpen)
{
controllers["Close"] = true;
} else {
}
else
{
controllers["Open"] = true;
}
isCdTrayOpen = !isCdTrayOpen;
} else {
}
else
{
controllers["Close"] = false;
controllers["Open"] = false;
}
if((controlState & 0xFC) != 0) {
if ((controlState & 0xFC) != 0)
{
Result.Warnings.Add("Ignored toggle hack flag on frame " + frame.ToString());
}
@ -214,7 +249,8 @@ namespace BizHawk.Client.Common {
}
}
protected void parseTextInputLog(BinaryReader br, Bk2Movie movie, MiscHeaderInfo info) {
protected void parseTextInputLog(BinaryReader br, Bk2Movie movie, MiscHeaderInfo info)
{
Octoshock.SyncSettings settings = new Octoshock.SyncSettings();
SimpleController controllers = new SimpleController();
settings.Controllers = new[] { info.player1Type, info.player2Type };
@ -225,22 +261,27 @@ namespace BizHawk.Client.Common {
bool isCdTrayOpen = false;
for (int frame = 0; frame < info.frameCount; ++frame) {
if (info.player1Type.IsConnected) {
for (int frame = 0; frame < info.frameCount; ++frame)
{
if (info.player1Type.IsConnected)
{
// As L3 and R3 don't exist on a standard gamepad, handle them separately later. Unfortunately
// due to the layout, we handle select separately too first.
controllers["P1 Select"] = br.ReadChar() != '.';
if(info.player1Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad) {
if (info.player1Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad)
{
controllers["P1 L3"] = br.ReadChar() != '.';
controllers["P1 R3"] = br.ReadChar() != '.';
}
for (int button = 3; button < buttons.Length; button++) {
for (int button = 3; button < buttons.Length; button++)
{
controllers["P1 " + buttons[button]] = br.ReadChar() != '.';
}
if (info.player1Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad) {
if (info.player1Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad)
{
// The analog controls are encoded as four space-separated numbers with a leading space
string leftXRaw = new string(br.ReadChars(4)).Trim();
string leftYRaw = new string(br.ReadChars(4)).Trim();
@ -260,21 +301,25 @@ namespace BizHawk.Client.Common {
// Each controller is terminated with a pipeline.
br.ReadChar();
if (info.player2Type.IsConnected) {
if (info.player2Type.IsConnected)
{
// As L3 and R3 don't exist on a standard gamepad, handle them separately later. Unfortunately
// due to the layout, we handle select separately too first.
controllers["P2 Select"] = br.ReadChar() != '.';
if (info.player2Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad) {
if (info.player2Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad)
{
controllers["P2 L3"] = br.ReadChar() != '.';
controllers["P2 R3"] = br.ReadChar() != '.';
}
for (int button = 3; button < buttons.Length; button++) {
for (int button = 3; button < buttons.Length; button++)
{
controllers["P2 " + buttons[button]] = br.ReadChar() != '.';
}
if (info.player2Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad) {
if (info.player2Type.Type != Octoshock.ControllerSetting.ControllerType.Gamepad)
{
// The analog controls are encoded as four space-separated numbers with a leading space
string leftXRaw = new string(br.ReadChars(4)).Trim();
string leftYRaw = new string(br.ReadChars(4)).Trim();
@ -296,19 +341,26 @@ namespace BizHawk.Client.Common {
byte controlState = br.ReadByte();
controllers["Reset"] = (controlState & 0x02) != 0;
if ((controlState & 0x04) != 0) {
if (isCdTrayOpen) {
if ((controlState & 0x04) != 0)
{
if (isCdTrayOpen)
{
controllers["Close"] = true;
} else {
}
else
{
controllers["Open"] = true;
}
isCdTrayOpen = !isCdTrayOpen;
} else {
}
else
{
controllers["Close"] = false;
controllers["Open"] = false;
}
if ((controlState & 0xFC) != 0) {
if ((controlState & 0xFC) != 0)
{
Result.Warnings.Add("Ignored toggle hack flag on frame " + frame.ToString());
}
@ -319,7 +371,8 @@ namespace BizHawk.Client.Common {
}
}
protected class MiscHeaderInfo {
protected class MiscHeaderInfo
{
public bool binaryFormat = true;
public UInt32 controllerDataOffset;
public UInt32 frameCount;

View File

@ -1,6 +1,7 @@
using System.IO;
namespace BizHawk.Client.Common.movie.import {
namespace BizHawk.Client.Common.movie.import
{
// PXM files are directly compatible with binary-format PJM files, with the only
// difference being fewer flags implemented in the header, hence just calling the
@ -9,22 +10,29 @@ namespace BizHawk.Client.Common.movie.import {
// However, the magic number/file signature is slightly different, requiring some
// refactoring to avoid PXM-specific code in the PJMImport class.
[ImportExtension(".pxm")]
class PXMImport : PJMImport {
protected override void RunImport() {
class PXMImport : PJMImport
{
protected override void RunImport()
{
Bk2Movie movie = Result.Movie;
MiscHeaderInfo info;
movie.HeaderEntries.Add(HeaderKeys.PLATFORM, "PSX");
using (var fs = SourceFile.OpenRead()) {
using (var br = new BinaryReader(fs)) {
using (var fs = SourceFile.OpenRead())
{
using (var br = new BinaryReader(fs))
{
info = parseHeader(movie, "PXM ", br);
fs.Seek(info.controllerDataOffset, SeekOrigin.Begin);
if (info.binaryFormat) {
if (info.binaryFormat)
{
parseBinaryInputLog(br, movie, info);
} else {
}
else
{
parseTextInputLog(br, movie, info);
}
}

View File

@ -36,7 +36,8 @@ namespace BizHawk.Emulation.Cores.Sony.PSX
{
public string SystemId { get { return "PSX"; } }
public static ControllerDefinition CreateControllerDefinition(SyncSettings syncSettings) {
public static ControllerDefinition CreateControllerDefinition(SyncSettings syncSettings)
{
ControllerDefinition definition = new ControllerDefinition();
definition.Name = syncSettings.Controllers.All(c => c.Type == ControllerSetting.ControllerType.Gamepad)
? "PSX Gamepad Controller"
@ -45,8 +46,10 @@ namespace BizHawk.Emulation.Cores.Sony.PSX
definition.BoolButtons.Clear();
definition.FloatControls.Clear();
for (int i = 0; i < syncSettings.Controllers.Length; i++) {
if (syncSettings.Controllers[i].IsConnected) {
for (int i = 0; i < syncSettings.Controllers.Length; i++)
{
if (syncSettings.Controllers[i].IsConnected)
{
definition.BoolButtons.AddRange(new[]
{
"P" + (i + 1) + " Up",
@ -65,7 +68,8 @@ namespace BizHawk.Emulation.Cores.Sony.PSX
"P" + (i + 1) + " R2",
});
if (syncSettings.Controllers[i].Type != ControllerSetting.ControllerType.Gamepad) {
if (syncSettings.Controllers[i].Type != ControllerSetting.ControllerType.Gamepad)
{
definition.BoolButtons.Add("P" + (i + 1) + " L3");
definition.BoolButtons.Add("P" + (i + 1) + " R3");
definition.BoolButtons.Add("P" + (i + 1) + " MODE");