-As frames and subtitles actually occur more than once, I put them on the top of the big if / else if block for ImportText so that it doesn't have to do unnecessary ToLower() and StartsWith("name") calls.

-Put frames and subtitles into functions because vec\ gives good advice.
This commit is contained in:
brandman211 2012-07-04 07:34:52 +00:00
parent c06a44c495
commit 5ce6050cb4
1 changed files with 112 additions and 95 deletions

View File

@ -85,137 +85,26 @@ namespace BizHawk.MultiClient
return false; return false;
} }
// Import a text-based movie format. This works for .FM2 and .MC2. // Import a frame from a text-based format.
private static Movie ImportText(string path, out string errorMsg, out string warningMsg) private static Movie ImportTextFrame(string line, int lineNum, Movie m, string path, ref string warningMsg)
{ {
errorMsg = "";
warningMsg = "";
Movie m = new Movie(path + "." + Global.Config.MovieExtension, MOVIEMODE.PLAY);
FileInfo file = new FileInfo(path);
StreamReader sr = file.OpenText();
string[] buttons = new string[] { }; string[] buttons = new string[] { };
string controller = ""; string controller = "";
string emulator = "";
string platform = "";
switch (Path.GetExtension(path).ToUpper()) switch (Path.GetExtension(path).ToUpper())
{ {
case ".FM2": case ".FM2":
buttons = new string[8] { "Right", "Left", "Down", "Up", "Start", "Select", "B", "A" }; buttons = new string[8] { "Right", "Left", "Down", "Up", "Start", "Select", "B", "A" };
controller = "NES Controller"; controller = "NES Controller";
emulator = "FCEUX";
platform = "NES";
break; break;
case ".MC2": case ".MC2":
buttons = new string[8] { "Up", "Down", "Left", "Right", "B1", "B2", "Run", "Select" }; buttons = new string[8] { "Up", "Down", "Left", "Right", "B1", "B2", "Run", "Select" };
controller = "PC Engine Controller"; controller = "PC Engine Controller";
emulator = "Mednafen/PCEjin";
platform = "PCE";
break; break;
} }
m.Header.SetHeaderLine(MovieHeader.PLATFORM, platform);
SimpleController controllers = new SimpleController(); SimpleController controllers = new SimpleController();
controllers.Type = new ControllerDefinition(); controllers.Type = new ControllerDefinition();
controllers.Type.Name = controller; controllers.Type.Name = controller;
MnemonicsGenerator mg = new MnemonicsGenerator(); MnemonicsGenerator mg = new MnemonicsGenerator();
int lineNum = 0;
string line = "";
while ((line = sr.ReadLine()) != null)
{
lineNum++;
if (line == "")
continue;
if (line.ToLower().StartsWith("emuversion"))
m.Header.Comments.Add(EMULATIONORIGIN + " " + emulator + " version " + ParseHeader(line, "emuVersion"));
else if (line.ToLower().StartsWith("version"))
{
string version = ParseHeader(line, "version");
m.Header.Comments.Add(
MOVIEORIGIN + " " + Path.GetExtension(path) + " version " + version
);
if (platform == "NES" && version != "3")
{
errorMsg = ".FM2 movie version must always be 3.";
sr.Close();
return null;
}
}
else if (line.ToLower().StartsWith("romfilename"))
m.Header.SetHeaderLine(MovieHeader.GAMENAME, ParseHeader(line, "romFilename"));
else if (line.ToLower().StartsWith("romchecksum"))
{
string blob = ParseHeader(line, "romChecksum");
byte[] MD5 = DecodeBlob(blob);
if (MD5 != null && MD5.Length == 16)
m.Header.SetHeaderLine("MD5", BizHawk.Util.BytesToHexString(MD5).ToLower());
else
warningMsg = "Bad ROM checksum.";
}
else if (line.ToLower().StartsWith("comment author"))
m.Header.SetHeaderLine(MovieHeader.AUTHOR, ParseHeader(line, "comment author"));
else if (line.ToLower().StartsWith("rerecordcount"))
{
int rerecordCount;
// Try to parse the re-record count as an integer, defaulting to 0 if it fails.
try
{
rerecordCount = int.Parse(ParseHeader(line, "rerecordCount"));
}
catch
{
rerecordCount = 0;
}
m.SetRerecords(rerecordCount);
}
else if (line.ToLower().StartsWith("guid"))
m.Header.SetHeaderLine(MovieHeader.GUID, ParseHeader(line, "guid"));
else if (line.ToLower().StartsWith("startsfromsavestate"))
{
// If this movie starts from a savestate, we can't support it.
if (ParseHeader(line, "StartsFromSavestate") == "1")
{
errorMsg = "Movies that begin with a savestate are not supported.";
sr.Close();
return null;
}
}
else if (line.ToLower().StartsWith("palflag"))
{
bool pal = (ParseHeader(line, "palFlag") == "1");
m.Header.SetHeaderLine("PAL", pal.ToString());
}
else if (line.ToLower().StartsWith("fourscore"))
{
bool fourscore = (ParseHeader(line, "fourscore") == "1");
m.Header.SetHeaderLine(MovieHeader.FOURSCORE, fourscore.ToString());
}
else if (line.ToLower().StartsWith("sub"))
{
Subtitle s = new Subtitle();
// Reduce all whitespace to single spaces.
line = line.Replace("\t", " ");
line = line.Replace("\n", " ");
line = line.Replace("\r", " ");
line = line.Replace("\r\n", " ");
string prev;
do
{
prev = line;
line = line.Replace(" ", " ");
}
while (prev != line);
// The header name, frame, and message are separated by whitespace.
int first = line.IndexOf(' ');
int second = line.IndexOf(' ', first + 1);
if (first != -1 && second != -1)
{
// Concatenate the frame and message with default values for the additional fields.
string frame = line.Substring(first + 1, second - first - 1);
string message = line.Substring(second + 1).Trim();
m.Subtitles.AddSubtitle("subtitle " + frame + " 0 0 200 FFFFFFFF " + message);
}
}
else if (line[0] == '|')
{
// Split up the sections of the frame. // Split up the sections of the frame.
string[] sections = line.Split('|'); string[] sections = line.Split('|');
if (Path.GetExtension(path).ToUpper() == ".FM2" && sections[1].Length != 0) if (Path.GetExtension(path).ToUpper() == ".FM2" && sections[1].Length != 0)
@ -270,6 +159,134 @@ namespace BizHawk.MultiClient
// Convert the data for the controllers to a mnemonic and add it as a frame. // Convert the data for the controllers to a mnemonic and add it as a frame.
mg.SetSource(controllers); mg.SetSource(controllers);
m.AppendFrame(mg.GetControllersAsMnemonic()); m.AppendFrame(mg.GetControllersAsMnemonic());
return m;
}
// Import a subtitle from a text-based format.
private static Movie ImportTextSubtitle(string line, Movie m)
{
Subtitle s = new Subtitle();
// Reduce all whitespace to single spaces.
line = line.Replace("\t", " ");
line = line.Replace("\n", " ");
line = line.Replace("\r", " ");
line = line.Replace("\r\n", " ");
string prev;
do
{
prev = line;
line = line.Replace(" ", " ");
}
while (prev != line);
// The header name, frame, and message are separated by whitespace.
int first = line.IndexOf(' ');
int second = line.IndexOf(' ', first + 1);
if (first != -1 && second != -1)
{
// Concatenate the frame and message with default values for the additional fields.
string frame = line.Substring(first + 1, second - first - 1);
string message = line.Substring(second + 1).Trim();
m.Subtitles.AddSubtitle("subtitle " + frame + " 0 0 200 FFFFFFFF " + message);
}
return m;
}
// 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 = "";
warningMsg = "";
Movie m = new Movie(path + "." + Global.Config.MovieExtension, MOVIEMODE.PLAY);
FileInfo file = new FileInfo(path);
StreamReader sr = file.OpenText();
string emulator = "";
string platform = "";
switch (Path.GetExtension(path).ToUpper())
{
case ".FM2":
emulator = "FCEUX";
platform = "NES";
break;
case ".MC2":
emulator = "Mednafen/PCEjin";
platform = "PCE";
break;
}
m.Header.SetHeaderLine(MovieHeader.PLATFORM, platform);
int lineNum = 0;
string line = "";
while ((line = sr.ReadLine()) != null)
{
lineNum++;
if (line == "")
continue;
else if (line[0] == '|')
m = ImportTextFrame(line, lineNum, m, path, ref warningMsg);
else if (line.ToLower().StartsWith("sub"))
m = ImportTextSubtitle(line, m);
else if (line.ToLower().StartsWith("emuversion"))
m.Header.Comments.Add(EMULATIONORIGIN + " " + emulator + " version " + ParseHeader(line, "emuVersion"));
else if (line.ToLower().StartsWith("version"))
{
string version = ParseHeader(line, "version");
m.Header.Comments.Add(
MOVIEORIGIN + " " + Path.GetExtension(path) + " version " + version
);
if (Path.GetExtension(path).ToUpper() == ".FM2" && version != "3")
{
errorMsg = ".FM2 movie version must always be 3.";
sr.Close();
return null;
}
}
else if (line.ToLower().StartsWith("romfilename"))
m.Header.SetHeaderLine(MovieHeader.GAMENAME, ParseHeader(line, "romFilename"));
else if (line.ToLower().StartsWith("romchecksum"))
{
string blob = ParseHeader(line, "romChecksum");
byte[] MD5 = DecodeBlob(blob);
if (MD5 != null && MD5.Length == 16)
m.Header.SetHeaderLine("MD5", BizHawk.Util.BytesToHexString(MD5).ToLower());
else
warningMsg = "Bad ROM checksum.";
}
else if (line.ToLower().StartsWith("comment author"))
m.Header.SetHeaderLine(MovieHeader.AUTHOR, ParseHeader(line, "comment author"));
else if (line.ToLower().StartsWith("rerecordcount"))
{
int rerecordCount;
// Try to parse the re-record count as an integer, defaulting to 0 if it fails.
try
{
rerecordCount = int.Parse(ParseHeader(line, "rerecordCount"));
}
catch
{
rerecordCount = 0;
}
m.SetRerecords(rerecordCount);
}
else if (line.ToLower().StartsWith("guid"))
m.Header.SetHeaderLine(MovieHeader.GUID, ParseHeader(line, "guid"));
else if (line.ToLower().StartsWith("startsfromsavestate"))
{
// If this movie starts from a savestate, we can't support it.
if (ParseHeader(line, "StartsFromSavestate") == "1")
{
errorMsg = "Movies that begin with a savestate are not supported.";
sr.Close();
return null;
}
}
else if (line.ToLower().StartsWith("palflag"))
{
bool pal = (ParseHeader(line, "palFlag") == "1");
m.Header.SetHeaderLine("PAL", pal.ToString());
}
else if (line.ToLower().StartsWith("fourscore"))
{
bool fourscore = (ParseHeader(line, "fourscore") == "1");
m.Header.SetHeaderLine(MovieHeader.FOURSCORE, fourscore.ToString());
} }
else else
// Everything not explicitly defined is treated as a comment. // Everything not explicitly defined is treated as a comment.