186 lines
4.2 KiB
C#
186 lines
4.2 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
using BizHawk.Common;
|
|
|
|
namespace BizHawk.Client.Common
|
|
{
|
|
public partial class Bk2Movie
|
|
{
|
|
public void Save()
|
|
{
|
|
Write(Filename);
|
|
}
|
|
|
|
public void SaveBackup()
|
|
{
|
|
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);
|
|
}
|
|
|
|
public virtual bool Load()
|
|
{
|
|
var file = new FileInfo(Filename);
|
|
if (!file.Exists)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
using (var bl = BinaryStateLoader.LoadAndDetect(Filename, true))
|
|
{
|
|
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[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
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)
|
|
{
|
|
var errorMessage = string.Empty;
|
|
ExtractInputLog(tr, out errorMessage);
|
|
});
|
|
|
|
if (StartsFromSavestate)
|
|
{
|
|
bl.GetCoreState(
|
|
delegate(BinaryReader br)
|
|
{
|
|
BinarySavestate = br.ReadBytes((int)br.BaseStream.Length);
|
|
},
|
|
delegate(TextReader tr)
|
|
{
|
|
TextSavestate = tr.ReadToEnd();
|
|
});
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool PreLoadHeaderAndLength(HawkFile hawkFile)
|
|
{
|
|
// For now, preload simply loads everything
|
|
var file = new FileInfo(Filename);
|
|
if (!file.Exists)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Filename = file.FullName;
|
|
return Load();
|
|
}
|
|
|
|
protected virtual void Write(string fn)
|
|
{
|
|
var file = new FileInfo(fn);
|
|
if (!file.Directory.Exists)
|
|
{
|
|
Directory.CreateDirectory(file.Directory.ToString());
|
|
}
|
|
|
|
using (var fs = new FileStream(fn, FileMode.Create, FileAccess.Write))
|
|
using (var bs = new BinaryStateSaver(fs, false))
|
|
{
|
|
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()));
|
|
bs.PutLump(BinaryStateLump.SyncSettings, tw => tw.WriteLine(_syncSettingsJson));
|
|
|
|
bs.PutLump(BinaryStateLump.Input, tw => tw.WriteLine(RawInputLog()));
|
|
|
|
if (StartsFromSavestate)
|
|
{
|
|
if (TextSavestate != null)
|
|
{
|
|
bs.PutLump(BinaryStateLump.CorestateText, (TextWriter tw) => tw.Write(TextSavestate));
|
|
}
|
|
else
|
|
{
|
|
bs.PutLump(BinaryStateLump.Corestate, (BinaryWriter bw) => bw.Write(BinarySavestate));
|
|
}
|
|
}
|
|
}
|
|
|
|
Changes = false;
|
|
}
|
|
|
|
protected void ClearBeforeLoad()
|
|
{
|
|
Header.Clear();
|
|
_log.Clear();
|
|
Subtitles.Clear();
|
|
Comments.Clear();
|
|
_syncSettingsJson = string.Empty;
|
|
TextSavestate = null;
|
|
BinarySavestate = null;
|
|
}
|
|
}
|
|
}
|