Movies - round 1 of code cleanup (no functional changes)

This commit is contained in:
adelikat 2012-09-03 15:05:09 +00:00
parent eae73accf3
commit 27f4663125
11 changed files with 337 additions and 271 deletions

View File

@ -342,11 +342,11 @@ namespace BizHawk.MultiClient
{
if (Global.MovieSession.Movie.Mode == MOVIEMODE.FINISHED)
{
return Global.Emulator.Frame.ToString() + "/" + Global.MovieSession.Movie.LogLength().ToString() + " (Finished)";
return Global.Emulator.Frame.ToString() + "/" + Global.MovieSession.Movie.TotalFrames.ToString() + " (Finished)";
}
else if (Global.MovieSession.Movie.Mode == MOVIEMODE.PLAY)
{
return Global.Emulator.Frame.ToString() + "/" + Global.MovieSession.Movie.LogLength().ToString();
return Global.Emulator.Frame.ToString() + "/" + Global.MovieSession.Movie.TotalFrames.ToString();
}
else if (Global.MovieSession.Movie.Mode == MOVIEMODE.RECORD)
return Global.Emulator.Frame.ToString();

View File

@ -1813,7 +1813,7 @@ namespace BizHawk.MultiClient
public int movie_length()
{
return Global.MovieSession.Movie.LogLength();
return Global.MovieSession.Movie.TotalFrames;
}
public string movie_filename()

View File

@ -242,8 +242,8 @@ namespace BizHawk.MultiClient
Global.MovieSession.Movie.CommitFrame(Global.Emulator.Frame, Global.MovieOutputHardpoint);
break;
case MOVIEMODE.PLAY:
int x = Global.MovieSession.Movie.LogLength();
if (Global.Emulator.Frame >= Global.MovieSession.Movie.LogLength())
int x = Global.MovieSession.Movie.TotalFrames;
if (Global.Emulator.Frame >= Global.MovieSession.Movie.TotalFrames)
{
Global.MovieSession.Movie.SetMovieFinished();
}
@ -255,8 +255,8 @@ namespace BizHawk.MultiClient
x++;
break;
case MOVIEMODE.FINISHED:
int xx = Global.MovieSession.Movie.LogLength();
if (Global.Emulator.Frame < Global.MovieSession.Movie.LogLength()) //This scenario can happen from rewinding (suddenly we are back in the movie, so hook back up to the movie
int xx = Global.MovieSession.Movie.TotalFrames;
if (Global.Emulator.Frame < Global.MovieSession.Movie.TotalFrames) //This scenario can happen from rewinding (suddenly we are back in the movie, so hook back up to the movie
{
Global.MovieSession.Movie.StartPlayback();
Global.MovieSession.LatchInputFromLog();

View File

@ -226,7 +226,7 @@ namespace BizHawk.MultiClient
ReadOnly = true;
// if user is dumping and didnt supply dump length, make it as long as the loaded movie
if (autoDumpLength == 0)
autoDumpLength = m.LogLength();
autoDumpLength = m.TotalFrames;
StartNewMovie(m, false);
Global.Config.RecentMovies.Add(cmdMovie);
}

View File

@ -30,13 +30,21 @@ namespace BizHawk.MultiClient
{
text = "";
if (column == 0) //File
{
text = Path.GetFileName(MovieList[index].Filename);
}
if (column == 1) //System
text = MovieList[index].SysID();
{
text = MovieList[index].SysID;
}
if (column == 2) //Game
text = MovieList[index].GetGameName();
{
text = MovieList[index].GameName;
}
if (column == 3) //Time
{
text = MovieList[index].GetTime(true);
}
}
private void MovieView_QueryItemBkColor(int index, int column, ref Color color)
@ -88,7 +96,7 @@ namespace BizHawk.MultiClient
{
Movie m = new Movie(file.FullName, MOVIEMODE.INACTIVE);
m.LoadMovie(); //State files will have to load everything unfortunately
if (m.LogLength() == 0)
if (m.TotalFrames == 0)
{
MessageBox.Show("No input log detected in this savestate, aborting", "Can not load file", MessageBoxButtons.OK, MessageBoxIcon.Hand);
return;
@ -119,7 +127,7 @@ namespace BizHawk.MultiClient
{
Movie m = new Movie(file.CanonicalFullPath, MOVIEMODE.INACTIVE);
m.LoadMovie(); //State files will have to load everything unfortunately
if (m.LogLength() > 0)
if (m.TotalFrames > 0)
{
MovieList.Add(m);
sortReverse = false;
@ -201,7 +209,7 @@ namespace BizHawk.MultiClient
//Pull out matching names
for (int x = 0; x < MovieList.Count; x++)
{
if (PathManager.FilesystemSafeName(Global.Game) == MovieList[x].GetGameName())
if (PathManager.FilesystemSafeName(Global.Game) == MovieList[x].GameName)
Indexes.Add(x);
}
if (Indexes.Count == 0) return;

View File

@ -75,7 +75,7 @@ namespace BizHawk.MultiClient
if (StartFromCombo.SelectedItem.ToString() == "Now")
{
MovieToRecord.SetStartsFromSavestate(true);
MovieToRecord.FlagStartsFromSavestate();
var temppath = path + ".tmp";
var writer = new StreamWriter(temppath);
Global.Emulator.SaveStateText(writer);

View File

@ -163,7 +163,7 @@ namespace BizHawk.MultiClient
{
for (int i = 0; i < frames; i++)
{
if (RewindBuf.Count == 0 || (true == Global.MovieSession.Movie.Loaded && 0 == Global.MovieSession.Movie.LogLength()))
if (RewindBuf.Count == 0 || (true == Global.MovieSession.Movie.Loaded && 0 == Global.MovieSession.Movie.TotalFrames))
{
return;
}

View File

@ -12,34 +12,16 @@ namespace BizHawk.MultiClient
public enum MOVIEMODE { INACTIVE, PLAY, RECORD, FINISHED };
public class Movie
{
public MovieHeader Header = new MovieHeader();
public SubtitleList Subtitles = new SubtitleList();
public bool MakeBackup = true; //make backup before altering movie
//Remove this once the memory mangement issues with save states for tastudio has been solved.
public bool TastudioOn = false;
public bool IsText { get; private set; }
public string Filename { get; private set; }
public MOVIEMODE Mode { get; set; }
public int Rerecords { get; private set; }
private int Frames;
public bool RerecordCounting { get; set; }
private MovieLog Log = new MovieLog();
private int lastLog;
public bool StartsFromSavestate { get; private set; }
public bool Loaded { get; private set; }
#region Constructors
public Movie(string filename, MOVIEMODE m)
{
Mode = m;
lastLog = 0;
lastlog = 0;
Rerecords = 0;
this.Filename = filename;
IsText = true;
Frames = 0;
frames = 0;
RerecordCounting = true;
StartsFromSavestate = false;
if (filename.Length > 0)
@ -51,35 +33,87 @@ namespace BizHawk.MultiClient
Filename = "";
Mode = MOVIEMODE.INACTIVE;
IsText = true;
Frames = 0;
frames = 0;
StartsFromSavestate = false;
Loaded = false;
RerecordCounting = true;
}
public string SysID()
#endregion
#region Properties
public MovieHeader Header = new MovieHeader();
public SubtitleList Subtitles = new SubtitleList();
public bool MakeBackup = true; //make backup before altering movie
public bool TastudioOn = false; //Remove this once the memory mangement issues with save states for tastudio has been solved.
public bool IsText { get; private set; }
public string Filename { get; private set; }
public MOVIEMODE Mode { get; set; }
public int Rerecords { get; private set; }
public bool RerecordCounting { get; set; }
public bool StartsFromSavestate { get; private set; }
public bool Loaded { get; private set; }
public string SysID
{
return Header.GetHeaderLine(MovieHeader.PLATFORM);
get
{
return Header.GetHeaderLine(MovieHeader.PLATFORM);
}
}
public string GUID()
public string GUID
{
return Header.GetHeaderLine(MovieHeader.GUID);
get
{
return Header.GetHeaderLine(MovieHeader.GUID);
}
}
public string GetGameName()
public string GameName
{
return Header.GetHeaderLine(MovieHeader.GAMENAME);
get
{
return Header.GetHeaderLine(MovieHeader.GAMENAME);
}
}
public int LogLength()
public int TotalFrames
{
if (Loaded)
return Log.MovieLength();
else
return Frames;
get
{
if (Loaded)
{
return Log.MovieLength();
}
else
{
return frames;
}
}
}
public int StateFirstIndex
{
get
{
return Log.StateFirstIndex();
}
}
public int StateLastIndex
{
get
{
return Log.StateLastIndex();
}
}
#endregion
#region Public Methods
public void UpdateFileName(string filename)
{
this.Filename = filename;
@ -88,7 +122,10 @@ namespace BizHawk.MultiClient
public void StopMovie()
{
if (Mode == MOVIEMODE.RECORD)
{
WriteMovie();
}
Mode = MOVIEMODE.INACTIVE;
}
@ -154,9 +191,9 @@ namespace BizHawk.MultiClient
public void DeleteFrame(int frame)
{
if (frame <= StateLastIndex())
if (frame <= StateLastIndex)
{
if (frame <= StateFirstIndex())
if (frame <= StateFirstIndex)
{
RewindToFrame(0);
}
@ -168,16 +205,6 @@ namespace BizHawk.MultiClient
Log.DeleteFrame(frame);
}
public int StateFirstIndex()
{
return Log.StateFirstIndex();
}
public int StateLastIndex()
{
return Log.StateLastIndex();
}
public void ClearSaveRAM()
{
string x = PathManager.SaveRamPath(Global.Game);
@ -203,7 +230,7 @@ namespace BizHawk.MultiClient
{
ClearSaveRAM();
Mode = MOVIEMODE.PLAY;
Global.MainForm.StopOnFrame = LogLength();
Global.MainForm.StopOnFrame = TotalFrames;
}
public void ResumeRecording()
@ -231,7 +258,7 @@ namespace BizHawk.MultiClient
public string GetInputFrame(int frame)
{
lastLog = frame;
lastlog = frame;
if (frame < Log.MovieLength())
return Log.GetFrame(frame);
else
@ -291,92 +318,6 @@ namespace BizHawk.MultiClient
}
private void WriteText(string file)
{
if (file.Length == 0) return; //Nothing to write
int length = Log.MovieLength();
using (StreamWriter sw = new StreamWriter(file))
{
Header.WriteText(sw);
Subtitles.WriteText(sw);
Log.WriteText(sw);
}
}
private void WriteBinary(string file)
{
}
private bool LoadText()
{
var file = new FileInfo(Filename);
if (file.Exists == false)
{
Loaded = false;
return false;
}
else
{
Header.Clear();
Log.Clear();
}
using (StreamReader sr = file.OpenText())
{
string str = "";
string rerecordStr = "";
while ((str = sr.ReadLine()) != null)
{
if (str == "")
{
continue;
}
if (str.Contains(MovieHeader.RERECORDS))
{
rerecordStr = ParseHeader(str, MovieHeader.RERECORDS);
try
{
Rerecords = int.Parse(rerecordStr);
}
catch
{
Rerecords = 0;
}
}
else if (str.Contains(MovieHeader.STARTSFROMSAVESTATE))
{
str = ParseHeader(str, MovieHeader.STARTSFROMSAVESTATE);
if (str == "1")
StartsFromSavestate = true;
}
if (Header.AddHeaderFromLine(str))
continue;
if (str.StartsWith("subtitle") || str.StartsWith("sub"))
{
Subtitles.AddSubtitle(str);
}
else if (str[0] == '|')
{
Log.AddFrame(str);
}
else
{
Header.Comments.Add(str);
}
}
}
Loaded = true;
return true;
}
/// <summary>
/// Load Header information only for displaying file information in dialogs such as play movie
/// </summary>
@ -412,7 +353,7 @@ namespace BizHawk.MultiClient
length++;
length++;
// Count the remaining frames and the current one.
this.Frames = (frames.Length / length) + 1;
this.frames = (frames.Length / length) + 1;
break;
}
else
@ -424,11 +365,6 @@ namespace BizHawk.MultiClient
return true;
}
private bool LoadBinary()
{
return true;
}
public bool LoadMovie()
{
var file = new FileInfo(Filename);
@ -561,7 +497,9 @@ namespace BizHawk.MultiClient
public void SetMovieFinished()
{
if (Mode == MOVIEMODE.PLAY)
{
Mode = MOVIEMODE.FINISHED;
}
}
public string GetTime(bool preLoad)
@ -570,102 +508,28 @@ namespace BizHawk.MultiClient
double seconds;
if (preLoad)
seconds = GetSeconds(Frames);
{
seconds = GetSeconds(frames);
}
else
{
seconds = GetSeconds(Log.MovieLength());
}
int hours = ((int)seconds) / 3600;
int minutes = (((int)seconds) / 60) % 60;
double sec = seconds % 60;
if (hours > 0)
{
time += MakeDigits(hours) + ":";
}
time += MakeDigits(minutes) + ":";
time += Math.Round((decimal)sec, 2).ToString();
return time;
}
private string MakeDigits(decimal num)
{
return MakeDigits((int)num);
}
private string MakeDigits(int num)
{
if (num < 10)
return "0" + num.ToString();
else
return num.ToString();
}
private double GetSeconds(int frameCount)
{
const double NES_PAL = 50.006977968268290849;
const double NES_NTSC = (double)60.098813897440515532;
const double PCE = (7159090.90909090 / 455 / 263); //~59.826
const double SMS_NTSC = (3579545 / 262.0 / 228.0);
const double SMS_PAL = (3546893 / 313.0 / 228.0);
const double NGP = (6144000.0 / (515 * 198));
const double VBOY = (20000000 / (259 * 384 * 4)); //~50.273
const double LYNX = 59.8;
const double WSWAN = (3072000.0 / (159 * 256));
double seconds = 0;
double frames = (double)frameCount;
if (frames < 1)
return seconds;
bool pal = false; //TODO: pal flag
switch (Header.GetHeaderLine(MovieHeader.PLATFORM))
{
case "GG":
case "SG":
case "SMS":
if (pal)
return frames / SMS_PAL;
else
return frames / SMS_NTSC;
case "FDS":
case "NES":
case "SNES":
if (pal)
return frames / NES_PAL;
else
return frames / NES_NTSC;
case "PCE":
case "PCECD":
return frames / PCE;
//One Day!
case "VBOY":
return frames / VBOY;
case "NGP":
return frames / NGP;
case "LYNX":
return frames / LYNX;
case "WSWAN":
return frames / WSWAN;
//********
case "":
default:
if (pal)
return frames / 50.0;
else
return frames / 60.0;
}
}
private bool IsStateFromAMovie(StreamReader reader)
{
while (true)
{
if (reader.ReadLine().Contains("GUID"))
break;
if (reader.EndOfStream)
return false;
}
return true;
}
public bool CheckTimeLines(string path, bool OnlyGUID)
{
//This function will compare the movie data to the savestate movie data to see if they match
@ -779,6 +643,217 @@ namespace BizHawk.MultiClient
return true;
}
public void FlagStartsFromSavestate()
{
StartsFromSavestate = true;
Header.AddHeaderLine(MovieHeader.STARTSFROMSAVESTATE, "1");
}
public void TruncateMovie(int frame)
{
Log.TruncateMovie(frame);
}
#endregion
#region Private Fields
private int frames; //Not a a reliable number, used for preloading (when no log has yet been loaded), this is only for quick stat compilation for dialogs such as play movie
private MovieLog Log = new MovieLog();
private int lastlog;
#endregion
#region Private Methods
private void WriteText(string file)
{
if (file.Length == 0) return; //Nothing to write
int length = Log.MovieLength();
using (StreamWriter sw = new StreamWriter(file))
{
Header.WriteText(sw);
Subtitles.WriteText(sw);
Log.WriteText(sw);
}
}
private void WriteBinary(string file)
{
}
private bool LoadText()
{
var file = new FileInfo(Filename);
if (file.Exists == false)
{
Loaded = false;
return false;
}
else
{
Header.Clear();
Log.Clear();
}
using (StreamReader sr = file.OpenText())
{
string str = "";
string rerecordStr = "";
while ((str = sr.ReadLine()) != null)
{
if (str == "")
{
continue;
}
if (str.Contains(MovieHeader.RERECORDS))
{
rerecordStr = ParseHeader(str, MovieHeader.RERECORDS);
try
{
Rerecords = int.Parse(rerecordStr);
}
catch
{
Rerecords = 0;
}
}
else if (str.Contains(MovieHeader.STARTSFROMSAVESTATE))
{
str = ParseHeader(str, MovieHeader.STARTSFROMSAVESTATE);
if (str == "1")
StartsFromSavestate = true;
}
if (Header.AddHeaderFromLine(str))
continue;
if (str.StartsWith("subtitle") || str.StartsWith("sub"))
{
Subtitles.AddSubtitle(str);
}
else if (str[0] == '|')
{
Log.AddFrame(str);
}
else
{
Header.Comments.Add(str);
}
}
}
Loaded = true;
return true;
}
private bool LoadBinary()
{
return true;
}
private string MakeDigits(decimal num)
{
return MakeDigits((int)num);
}
private string MakeDigits(int num)
{
if (num < 10)
return "0" + num.ToString();
else
return num.ToString();
}
private double GetSeconds(int frameCount)
{
const double NES_PAL = 50.006977968268290849;
const double NES_NTSC = (double)60.098813897440515532;
const double PCE = (7159090.90909090 / 455 / 263); //~59.826
const double SMS_NTSC = (3579545 / 262.0 / 228.0);
const double SMS_PAL = (3546893 / 313.0 / 228.0);
const double NGP = (6144000.0 / (515 * 198));
const double VBOY = (20000000 / (259 * 384 * 4)); //~50.273
const double LYNX = 59.8;
const double WSWAN = (3072000.0 / (159 * 256));
double seconds = 0;
double frames = (double)frameCount;
if (frames < 1)
return seconds;
bool pal = false; //TODO: pal flag
switch (Header.GetHeaderLine(MovieHeader.PLATFORM))
{
case "GG":
case "SG":
case "SMS":
if (pal)
return frames / SMS_PAL;
else
return frames / SMS_NTSC;
case "FDS":
case "NES":
case "SNES":
if (pal)
return frames / NES_PAL;
else
return frames / NES_NTSC;
case "PCE":
case "PCECD":
return frames / PCE;
//One Day!
case "VBOY":
return frames / VBOY;
case "NGP":
return frames / NGP;
case "LYNX":
return frames / LYNX;
case "WSWAN":
return frames / WSWAN;
//********
case "":
default:
if (pal)
return frames / 50.0;
else
return frames / 60.0;
}
}
private bool IsStateFromAMovie(StreamReader reader)
{
while (true)
{
if (reader.ReadLine().Contains("GUID"))
break;
if (reader.EndOfStream)
return false;
}
return true;
}
private static string ParseHeader(string line, string headerName)
{
string str;
int x = line.LastIndexOf(headerName) + headerName.Length;
str = line.Substring(x + 1, line.Length - x - 1);
return str;
}
#endregion
#region ComparisonLogic
public int CompareTo(Movie Other, string parameter)
{
int compare = 0;
@ -851,8 +926,8 @@ namespace BizHawk.MultiClient
private int CompareSysID(Movie Other)
{
string otherSysID = Other.SysID();
string thisSysID = this.SysID();
string otherSysID = Other.SysID;
string thisSysID = this.SysID;
if (thisSysID == null && otherSysID == null)
return 0;
@ -866,8 +941,8 @@ namespace BizHawk.MultiClient
private int CompareGameName(Movie Other)
{
string otherGameName = Other.GetGameName();
string thisGameName = this.GetGameName();
string otherGameName = Other.GameName;
string thisGameName = this.GameName;
if (thisGameName == null && otherGameName == null)
return 0;
@ -881,8 +956,8 @@ namespace BizHawk.MultiClient
private int CompareLength(Movie Other)
{
int otherLength = Other.Frames;
int thisLength = this.Frames;
int otherLength = Other.frames;
int thisLength = this.frames;
if (thisLength < otherLength)
return -1;
@ -892,23 +967,6 @@ namespace BizHawk.MultiClient
return 0;
}
private static string ParseHeader(string line, string headerName)
{
string str;
int x = line.LastIndexOf(headerName) + headerName.Length;
str = line.Substring(x + 1, line.Length - x - 1);
return str;
}
public void SetStartsFromSavestate(bool savestate)
{
StartsFromSavestate = true;
Header.AddHeaderLine(MovieHeader.STARTSFROMSAVESTATE, "1");
}
public void TruncateMovie(int frame)
{
Log.TruncateMovie(frame);
}
#endregion
}
}

View File

@ -120,7 +120,7 @@ namespace BizHawk.MultiClient
case '1':
break;
case '2':
if (m.LogLength() != 0)
if (m.TotalFrames != 0)
warningMsg = "hard reset";
break;
case '4':

View File

@ -22,7 +22,7 @@
Global.MovieSession.Movie.TastudioOn = false;
Global.MovieSession.Movie.ClearStates();
Global.MainForm.StopOnFrame = Global.MovieSession.Movie.LogLength();
Global.MainForm.StopOnFrame = Global.MovieSession.Movie.TotalFrames;
base.Dispose(disposing);
}

View File

@ -122,9 +122,9 @@ namespace BizHawk.MultiClient
private void TASView_QueryItemBkColor(int index, int column, ref Color color)
{
if (0 == index && 0 == Global.MovieSession.Movie.StateFirstIndex())
if (0 == index && 0 == Global.MovieSession.Movie.StateFirstIndex)
color = Color.LightGreen; //special case for frame 0. Normally we need to go back an extra frame, but for frame 0 we can reload the rom.
if (index > Global.MovieSession.Movie.StateFirstIndex() && index <= Global.MovieSession.Movie.StateLastIndex())
if (index > Global.MovieSession.Movie.StateFirstIndex && index <= Global.MovieSession.Movie.StateLastIndex)
color = Color.LightGreen;
if ("" != Global.MovieSession.Movie.GetInputFrame(index) &&
Global.COMMANDS[Global.MovieInputSourceAdapter.Type.Name].ContainsKey("Lag") &&
@ -141,7 +141,7 @@ namespace BizHawk.MultiClient
text = "";
//If this is just for an actual frame and not just the list view cursor at the end
if (Global.MovieSession.Movie.LogLength() != index)
if (Global.MovieSession.Movie.TotalFrames != index)
{
if (column == 0)
text = String.Format("{0:#,##0}", index);
@ -152,8 +152,8 @@ namespace BizHawk.MultiClient
private void DisplayList()
{
TASView.ItemCount = Global.MovieSession.Movie.LogLength();
if (Global.MovieSession.Movie.LogLength() == Global.Emulator.Frame && Global.MovieSession.Movie.StateLastIndex() == Global.Emulator.Frame - 1)
TASView.ItemCount = Global.MovieSession.Movie.TotalFrames;
if (Global.MovieSession.Movie.TotalFrames == Global.Emulator.Frame && Global.MovieSession.Movie.StateLastIndex == Global.Emulator.Frame - 1)
{
//If we're at the end of the movie add one to show the cursor as a blank frame
TASView.ItemCount++;
@ -360,7 +360,7 @@ namespace BizHawk.MultiClient
if (Global.MovieSession.Movie.Mode == MOVIEMODE.FINISHED || Global.MovieSession.Movie.Mode == MOVIEMODE.INACTIVE)
{
Global.MainForm.Rewind(1);
if (Global.Emulator.Frame <= Global.MovieSession.Movie.LogLength())
if (Global.Emulator.Frame <= Global.MovieSession.Movie.TotalFrames)
{
Global.MovieSession.Movie.StartPlayback();
}
@ -421,7 +421,7 @@ namespace BizHawk.MultiClient
}
else
{
Global.MainForm.StopOnFrame = Global.MovieSession.Movie.LogLength();
Global.MainForm.StopOnFrame = Global.MovieSession.Movie.TotalFrames;
}
this.FastFowardToEnd.Checked ^= true;