-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,6 +85,112 @@ namespace BizHawk.MultiClient
return false;
}
// Import a frame from a text-based format.
private static Movie ImportTextFrame(string line, int lineNum, Movie m, string path, ref string warningMsg)
{
string[] buttons = new string[] { };
string controller = "";
switch (Path.GetExtension(path).ToUpper())
{
case ".FM2":
buttons = new string[8] { "Right", "Left", "Down", "Up", "Start", "Select", "B", "A" };
controller = "NES Controller";
break;
case ".MC2":
buttons = new string[8] { "Up", "Down", "Left", "Right", "B1", "B2", "Run", "Select" };
controller = "PC Engine Controller";
break;
}
SimpleController controllers = new SimpleController();
controllers.Type = new ControllerDefinition();
controllers.Type.Name = controller;
MnemonicsGenerator mg = new MnemonicsGenerator();
// Split up the sections of the frame.
string[] sections = line.Split('|');
if (Path.GetExtension(path).ToUpper() == ".FM2" && sections[1].Length != 0)
{
controllers["Reset"] = (sections[1][0] == '1');
// Get the first invalid command warning message that arises.
if (warningMsg == "")
{
switch (sections[1][0])
{
case '0':
break;
case '1':
break;
case '2':
if (m.LogLength() != 0)
warningMsg = "hard reset";
break;
case '4':
warningMsg = "FDS Insert";
break;
case '8':
warningMsg = "FDS Select Side";
break;
default:
warningMsg = "unknown";
break;
}
if (warningMsg != "")
warningMsg = "Unable to import " + warningMsg + " command on line " + lineNum + ".";
}
}
/*
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++)
{
// The player number is one less than the section number for the reasons explained above.
int player = section - 1;
// Only count lines with that have the right number of buttons and are for valid players.
if (
sections[section].Length == buttons.Length &&
player <= Global.PLAYERS[controllers.Type.Name]
)
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]] = (
sections[section][button] != '.'
);
}
// Convert the data for the controllers to a mnemonic and add it as a frame.
mg.SetSource(controllers);
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)
{
@ -93,30 +199,20 @@ namespace BizHawk.MultiClient
Movie m = new Movie(path + "." + Global.Config.MovieExtension, MOVIEMODE.PLAY);
FileInfo file = new FileInfo(path);
StreamReader sr = file.OpenText();
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);
SimpleController controllers = new SimpleController();
controllers.Type = new ControllerDefinition();
controllers.Type.Name = controller;
MnemonicsGenerator mg = new MnemonicsGenerator();
int lineNum = 0;
string line = "";
while ((line = sr.ReadLine()) != null)
@ -124,7 +220,11 @@ namespace BizHawk.MultiClient
lineNum++;
if (line == "")
continue;
if (line.ToLower().StartsWith("emuversion"))
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"))
{
@ -132,7 +232,7 @@ namespace BizHawk.MultiClient
m.Header.Comments.Add(
MOVIEORIGIN + " " + Path.GetExtension(path) + " version " + version
);
if (platform == "NES" && version != "3")
if (Path.GetExtension(path).ToUpper() == ".FM2" && version != "3")
{
errorMsg = ".FM2 movie version must always be 3.";
sr.Close();
@ -188,89 +288,6 @@ namespace BizHawk.MultiClient
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.
string[] sections = line.Split('|');
if (Path.GetExtension(path).ToUpper() == ".FM2" && sections[1].Length != 0)
{
controllers["Reset"] = (sections[1][0] == '1');
// Get the first invalid command warning message that arises.
if (warningMsg == "")
{
switch (sections[1][0])
{
case '0':
break;
case '1':
break;
case '2':
if (m.LogLength() != 0)
warningMsg = "hard reset";
break;
case '4':
warningMsg = "FDS Insert";
break;
case '8':
warningMsg = "FDS Select Side";
break;
default:
warningMsg = "unknown";
break;
}
if (warningMsg != "")
warningMsg = "Unable to import " + warningMsg + " command on line " + lineNum + ".";
}
}
/*
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++)
{
// The player number is one less than the section number for the reasons explained above.
int player = section - 1;
// Only count lines with that have the right number of buttons and are for valid players.
if (
sections[section].Length == buttons.Length &&
player <= Global.PLAYERS[controllers.Type.Name]
)
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]] = (
sections[section][button] != '.'
);
}
// Convert the data for the controllers to a mnemonic and add it as a frame.
mg.SetSource(controllers);
m.AppendFrame(mg.GetControllersAsMnemonic());
}
else
// Everything not explicitly defined is treated as a comment.
m.Header.Comments.Add(line);