BizHawk/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs

185 lines
4.2 KiB
C#
Raw Normal View History

2014-06-13 00:15:55 +00:00
using System;
using System.Collections.Generic;
2014-06-13 11:30:25 +00:00
using System.IO;
2014-06-13 00:15:55 +00:00
using System.Linq;
using System.Text;
using BizHawk.Common;
namespace BizHawk.Client.Common
{
public partial class Bk2Movie : IMovie
{
public void Save()
{
2014-06-13 11:30:25 +00:00
Write(Filename);
2014-06-13 00:15:55 +00:00
}
public void SaveBackup()
{
2014-06-13 11:30:25 +00:00
if (string.IsNullOrWhiteSpace(Filename))
{
return;
}
var backupName = Filename;
backupName = backupName.Insert(Filename.LastIndexOf("."), string.Format(".{0:yyyy-MM-dd HH.mm.ss}", DateTime.Now));
backupName = Path.Combine(Global.Config.PathEntries["Global", "Movie backups"].Path, Path.GetFileName(backupName));
var directory_info = new FileInfo(backupName).Directory;
if (directory_info != null)
{
Directory.CreateDirectory(directory_info.FullName);
}
Write(backupName);
2014-06-13 00:15:55 +00:00
}
public bool Load()
{
2014-06-13 11:30:25 +00:00
var file = new FileInfo(Filename);
if (!file.Exists)
{
return false;
}
using (BinaryStateLoader bl = BinaryStateLoader.LoadAndDetect(Filename, true))
2014-06-13 11:30:25 +00:00
{
if (bl == null)
{
return false;
}
ClearBeforeLoad();
bl.GetLump(BinaryStateLump.Movieheader, true, delegate(TextReader tr)
{
string line;
while ((line = tr.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
var pair = line.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
2014-06-19 02:30:22 +00:00
if (pair.Length > 1)
{
Header.Add(pair[0], pair[1]);
}
}
}
});
bl.GetLump(BinaryStateLump.Comments, true, delegate(TextReader tr)
{
string line;
while ((line = tr.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
Comments.Add(line);
}
}
});
bl.GetLump(BinaryStateLump.Subtitles, true, delegate(TextReader tr)
{
string line;
while ((line = tr.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
Subtitles.AddFromString(line);
}
}
});
bl.GetLump(BinaryStateLump.SyncSettings, true, delegate(TextReader tr)
{
string line;
while ((line = tr.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
_syncSettingsJson = line;
}
}
});
bl.GetLump(BinaryStateLump.Input, true, delegate(TextReader tr)
{
string line;
while ((line = tr.ReadLine()) != null)
{
if (line != null && line.StartsWith("|"))
{
_log.Add(line);
}
}
});
// Movies 2.0 TODO: be smart about text or binary state
if (StartsFromSavestate)
{
bl.GetLump(BinaryStateLump.CorestateText, true, delegate(TextReader tr)
{
string line;
while ((line = tr.ReadLine()) != null)
{
2014-06-14 18:33:22 +00:00
if (!string.IsNullOrWhiteSpace(line))
{
SavestateBinaryBase64Blob = line;
}
}
});
}
2014-06-13 11:30:25 +00:00
}
return true;
2014-06-13 00:15:55 +00:00
}
2014-06-14 19:13:21 +00:00
public bool PreLoadHeaderAndLength(HawkFile hawkFile)
2014-06-13 00:15:55 +00:00
{
// For now, preload simply loads everything
var file = new FileInfo(Filename);
if (!file.Exists)
{
return false;
}
Filename = file.FullName;
return Load();
2014-06-13 00:15:55 +00:00
}
2014-06-13 11:30:25 +00:00
private void Write(string fn)
{
using (FileStream fs = new FileStream(Filename, FileMode.Create, FileAccess.Write))
2014-06-14 20:17:07 +00:00
using (BinaryStateSaver bs = new BinaryStateSaver(fs, false))
2014-06-13 11:30:25 +00:00
{
bs.PutLump(BinaryStateLump.Movieheader, (tw) => tw.WriteLine(Header.ToString()));
bs.PutLump(BinaryStateLump.Comments, (tw) => tw.WriteLine(CommentsString()));
bs.PutLump(BinaryStateLump.Subtitles, (tw) => tw.WriteLine(Subtitles.ToString()));
2014-06-14 00:42:45 +00:00
bs.PutLump(BinaryStateLump.SyncSettings, (tw) => tw.WriteLine(_syncSettingsJson));
2014-06-13 11:30:25 +00:00
bs.PutLump(BinaryStateLump.Input, (tw) => tw.WriteLine(RawInputLog()));
2014-06-13 11:30:25 +00:00
if (StartsFromSavestate)
{
bs.PutLump(BinaryStateLump.CorestateText, (tw) => tw.WriteLine(SavestateBinaryBase64Blob));
}
}
Changes = false;
}
private void ClearBeforeLoad()
{
Header.Clear();
_log.Clear();
Subtitles.Clear();
Comments.Clear();
_syncSettingsJson = string.Empty;
_savestateBlob = string.Empty;
}
2014-06-13 00:15:55 +00:00
}
}