-Commented / refactored code.

-Fixed the Platform for PCE.
-Added emuOrigin and MovieOrigin to store the original emulation and movie versions in a comment.
This commit is contained in:
brandman211 2012-02-25 03:04:22 +00:00
parent f801d04a46
commit f2a60888ce
1 changed files with 73 additions and 49 deletions

View File

@ -13,7 +13,7 @@ namespace BizHawk.MultiClient
{ {
public static Movie ImportFile(string path, out string errorMsg, out string warningMsg) public static Movie ImportFile(string path, out string errorMsg, out string warningMsg)
{ {
Movie m = new Movie(); ; Movie m = new Movie();
errorMsg = ""; errorMsg = "";
warningMsg = ""; warningMsg = "";
try try
@ -48,6 +48,7 @@ namespace BizHawk.MultiClient
m = ImportVBM(path, out errorMsg, out warningMsg); m = ImportVBM(path, out errorMsg, out warningMsg);
break; break;
} }
m.Header.SetHeaderLine(MovieHeader.MOVIEVERSION, MovieHeader.MovieVersion);
if (errorMsg == "") if (errorMsg == "")
{ {
m.WriteMovie(); m.WriteMovie();
@ -101,12 +102,33 @@ namespace BizHawk.MultiClient
return true; return true;
} }
private static Movie ImportText(string path, out string errorMsg, out string warningMsg, string emulator) // Import a text-based movie format. This works for .FM2 and .MC2.
private static Movie ImportText(string path, out string errorMsg, out string warningMsg)
{ {
errorMsg = ""; errorMsg = "";
warningMsg = ""; warningMsg = "";
Movie m = new Movie(Path.ChangeExtension(path, ".tas"), MOVIEMODE.PLAY); Movie m = new Movie(Path.ChangeExtension(path, ".tas"), MOVIEMODE.PLAY);
var file = new FileInfo(path); var file = new FileInfo(path);
string[] buttons = new string[] { };
string controller = "";
string emulator = "";
string platform = "";
switch (Path.GetExtension(path).ToUpper())
{
case ".FM2":
buttons = new string[8] { "Right", "Left", "Down", "Up", "Start", "Select", "B", "A" };
controller = "NES Controller";
emulator = "FCEUX";
platform = "NES";
break;
case ".MC2":
buttons = new string[8] { "Up", "Down", "Left", "Right", "B1", "B2", "Run", "Select" };
controller = "PC Engine Controller";
emulator = "Mednafen/PCEjin";
platform = "PCE";
break;
}
m.Header.SetHeaderLine(MovieHeader.PLATFORM, platform);
using (StreamReader sr = file.OpenText()) using (StreamReader sr = file.OpenText())
{ {
int line = 0; int line = 0;
@ -122,6 +144,7 @@ namespace BizHawk.MultiClient
if (str.Contains("rerecordCount")) if (str.Contains("rerecordCount"))
{ {
rerecordStr = ParseHeader(str, "rerecordCount"); rerecordStr = ParseHeader(str, "rerecordCount");
// Try to parse the Re-record count as an integer, defaulting to 0 if it fails.
try try
{ {
m.SetRerecords(int.Parse(rerecordStr)); m.SetRerecords(int.Parse(rerecordStr));
@ -134,6 +157,7 @@ namespace BizHawk.MultiClient
else if (str.Contains("StartsFromSavestate")) else if (str.Contains("StartsFromSavestate"))
{ {
str = ParseHeader(str, "StartsFromSavestate"); str = ParseHeader(str, "StartsFromSavestate");
// If this movie starts from a savestate, we can't support it.
if (str == "1") if (str == "1")
{ {
warningMsg = "Movies that begin with a savestate are not supported."; warningMsg = "Movies that begin with a savestate are not supported.";
@ -142,11 +166,13 @@ namespace BizHawk.MultiClient
} }
if (str.StartsWith("emuVersion")) if (str.StartsWith("emuVersion"))
{ {
m.Header.SetHeaderLine(MovieHeader.EMULATIONVERSION, emulator + " version " + ParseHeader(str, "emuVersion")); m.Header.Comments.Add("emuOrigin " + emulator + " version " + ParseHeader(str, "emuVersion"));
} }
else if (str.StartsWith("version")) else if (str.StartsWith("version"))
{ {
m.Header.SetHeaderLine(MovieHeader.MOVIEVERSION, ParseHeader(str, "version")); m.Header.Comments.Add(
"MovieOrigin " + Path.GetExtension(path) + " version " + ParseHeader(str, "version")
);
} }
else if (str.StartsWith("romFilename")) else if (str.StartsWith("romFilename"))
{ {
@ -166,56 +192,54 @@ namespace BizHawk.MultiClient
} }
else if (str[0] == '|') else if (str[0] == '|')
{ {
// Handle a frame of input.
ArrayList frame = new ArrayList(); ArrayList frame = new ArrayList();
// Split up the sections of the frame. // Split up the sections of the frame.
string[] sections = str.Split('|'); string[] sections = str.Split('|');
string[] buttons = new string[] {}; // Start building the data for the controllers.
SimpleController controllers = new SimpleController(); SimpleController controllers = new SimpleController();
controllers.Type = new ControllerDefinition(); controllers.Type = new ControllerDefinition();
switch (emulator) controllers.Type.Name = controller;
// Get the first invalid command warning message that arises.
if (Path.GetExtension(path).ToUpper() == ".FM2" && warningMsg == "" && sections[1].Length != 0)
{ {
case "FCEUX": switch (sections[1][0])
buttons = new string[8] { "Right", "Left", "Down", "Up", "Start", "Select", "B", "A" }; {
controllers.Type.Name = "NES Controller"; case '0':
if (warningMsg == "" && sections[1].Length != 0) break;
{ case '1':
switch (sections[1][0]) controllers["Reset"] = true;
break;
case '2':
if (m.Length() != 0)
{ {
case '0': warningMsg = "hard reset";
break;
case '1':
controllers["Reset"] = true;
break;
case '2':
if (m.Length() != 0)
{
warningMsg = "hard reset";
}
break;
case '4':
warningMsg = "FDS Insert";
break;
case '8':
warningMsg = "FDS Select";
break;
default:
warningMsg = "unknown";
break;
} }
if (warningMsg != "") break;
{ case '4':
warningMsg = "Unable to import " + warningMsg + " command on line " + line; warningMsg = "FDS Insert";
} break;
} case '8':
break; warningMsg = "FDS Select";
case "Mednafen/PCEjin": break;
buttons = new string[8] { "Up", "Down", "Left", "Right", "B1", "B2", "Run", "Select" }; default:
controllers.Type.Name = "PC Engine Controller"; warningMsg = "unknown";
break; break;
}
if (warningMsg != "")
{
warningMsg = "Unable to import " + warningMsg + " command on line " + line;
}
} }
/*
Skip the first two sections of the split, which consist of everything before the starting | and the
command. Do not use the section after the last |. In other words, get the sections for the players.
*/
for (int section = 2; section < sections.Length - 1; section++) for (int section = 2; section < sections.Length - 1; section++)
{ {
// The player number is one less than the section number for the reasons explained above.
int player = section - 1; int player = section - 1;
// Only count lines with that have the right number of buttons and are for valid players.
if ( if (
sections[section].Length == buttons.Length && sections[section].Length == buttons.Length &&
player <= Global.PLAYERS[controllers.Type.Name] player <= Global.PLAYERS[controllers.Type.Name]
@ -223,18 +247,21 @@ namespace BizHawk.MultiClient
{ {
for (int button = 0; button < buttons.Length; button++) for (int button = 0; button < buttons.Length; button++)
{ {
// Consider the button pressed so long as its spot is not occupied by a ".".
controllers["P" + (player).ToString() + " " + buttons[button]] = ( controllers["P" + (player).ToString() + " " + buttons[button]] = (
sections[section][button] != '.' sections[section][button] != '.'
); );
} }
} }
} }
// Convert the data for the controllers to a mnemonic and add it as a frame.
MnemonicsGenerator mg = new MnemonicsGenerator(); MnemonicsGenerator mg = new MnemonicsGenerator();
mg.SetSource(controllers); mg.SetSource(controllers);
m.AppendFrame(mg.GetControllersAsMnemonic()); m.AppendFrame(mg.GetControllersAsMnemonic());
} }
else else
{ {
// Everything not explicitly defined is treated as a comment.
m.Header.Comments.Add(str); m.Header.Comments.Add(str);
} }
} }
@ -280,7 +307,7 @@ namespace BizHawk.MultiClient
//TODO: ROM checksum movie header line (MD5) //TODO: ROM checksum movie header line (MD5)
UInt32 EmuVersion = r.ReadUInt32(); UInt32 EmuVersion = r.ReadUInt32();
m.Header.SetHeaderLine(MovieHeader.EMULATIONVERSION, "FCEU " + EmuVersion.ToString()); m.Header.Comments.Add("emuOrigin FCEU " + EmuVersion.ToString());
List<byte> romBytes = new List<byte>(); List<byte> romBytes = new List<byte>();
while (true) while (true)
@ -361,8 +388,7 @@ namespace BizHawk.MultiClient
{ {
errorMsg = ""; errorMsg = "";
warningMsg = ""; warningMsg = "";
Movie m = ImportText(path, out errorMsg, out warningMsg, "FCEUX"); Movie m = ImportText(path, out errorMsg, out warningMsg);
m.Header.SetHeaderLine(MovieHeader.PLATFORM, "NES");
return m; return m;
} }
@ -398,8 +424,7 @@ namespace BizHawk.MultiClient
{ {
errorMsg = ""; errorMsg = "";
warningMsg = ""; warningMsg = "";
Movie m = ImportText(path, out errorMsg, out warningMsg, "Mednafen/PCEjin"); Movie m = ImportText(path, out errorMsg, out warningMsg);
m.Header.SetHeaderLine(MovieHeader.PLATFORM, "MC2");
// TODO: PCECD equivalent. // TODO: PCECD equivalent.
return m; return m;
} }
@ -425,8 +450,7 @@ namespace BizHawk.MultiClient
} }
UInt32 version = r.ReadUInt32(); UInt32 version = r.ReadUInt32();
m.Header.SetHeaderLine(MovieHeader.MOVIEVERSION, "Dega version " + version.ToString()); m.Header.Comments.Add("MovieOrigin .mmv version " + version.ToString());
UInt32 framecount = r.ReadUInt32(); UInt32 framecount = r.ReadUInt32();
m.SetRerecords((int)r.ReadUInt32()); m.SetRerecords((int)r.ReadUInt32());