BizHawk/BizHawk.Client.Common/movie/tasproj/TasBranch.cs

227 lines
5.5 KiB
C#
Raw Normal View History

2015-07-19 19:57:40 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2015-07-19 19:57:40 +00:00
using Newtonsoft.Json;
2015-07-21 23:17:29 +00:00
using BizHawk.Bizware.BizwareGL;
namespace BizHawk.Client.Common
{
public class TasBranch
{
public int Frame { get; set; }
public byte[] CoreData { get; set; }
public IStringLog InputLog { get; set; }
2015-07-21 23:17:29 +00:00
public BitmapBuffer OSDFrameBuffer { get; set; }
public TasLagLog LagLog { get; set; }
public TasMovieChangeLog ChangeLog { get; set; }
public DateTime TimeStamp { get; set; }
public TasMovieMarkerList Markers { get; set; }
public Guid UniqueIdentifier { get; set; }
public string UserText { get; set; }
public TasBranch Clone()
{
return (TasBranch)MemberwiseClone();
}
}
public class TasBranchCollection : List<TasBranch>
{
public new void Add(TasBranch item)
{
if (item.UniqueIdentifier == Guid.Empty)
{
var currentHashes = this.Select(b => b.UniqueIdentifier.GetHashCode()).ToList();
do
{
item.UniqueIdentifier = Guid.NewGuid();
}
while (currentHashes.Contains(item.UniqueIdentifier.GetHashCode()));
}
base.Add(item);
}
2015-07-19 19:57:40 +00:00
public void Save(BinaryStateSaver bs)
{
2015-07-19 19:57:40 +00:00
var nheader = new IndexedStateLump(BinaryStateLump.BranchHeader);
var ncore = new IndexedStateLump(BinaryStateLump.BranchCoreData);
var ninput = new IndexedStateLump(BinaryStateLump.BranchInputLog);
var nframebuffer = new IndexedStateLump(BinaryStateLump.BranchFrameBuffer);
var nlaglog = new IndexedStateLump(BinaryStateLump.BranchLagLog);
var nmarkers = new IndexedStateLump(BinaryStateLump.BranchMarkers);
var nusertext = new IndexedStateLump(BinaryStateLump.BranchUserText);
2015-07-19 21:08:55 +00:00
foreach (var b in this)
2015-07-19 19:57:40 +00:00
{
bs.PutLump(nheader, delegate(TextWriter tw)
{
// if this header needs more stuff in it, handle it sensibly
tw.WriteLine(JsonConvert.SerializeObject(new
{
b.Frame,
b.TimeStamp,
b.UniqueIdentifier
}));
2015-07-19 19:57:40 +00:00
});
2015-07-19 19:57:40 +00:00
bs.PutLump(ncore, delegate(Stream s)
{
s.Write(b.CoreData, 0, b.CoreData.Length);
});
2015-07-19 19:57:40 +00:00
bs.PutLump(ninput, delegate(TextWriter tw)
{
2015-11-15 10:27:00 +00:00
int todo = b.InputLog.Count;
for (int i = 0; i < todo; i++)
{
tw.WriteLine(b.InputLog[i]);
}
2015-07-19 19:57:40 +00:00
});
2015-07-19 19:57:40 +00:00
bs.PutLump(nframebuffer, delegate(Stream s)
{
2015-07-21 23:17:29 +00:00
var vp = new BitmapBufferVideoProvider(b.OSDFrameBuffer);
QuickBmpFile.Save(vp, s, b.OSDFrameBuffer.Width, b.OSDFrameBuffer.Height);
2015-07-19 19:57:40 +00:00
});
2015-07-19 21:08:55 +00:00
bs.PutLump(nlaglog, delegate(BinaryWriter bw)
2015-07-19 19:57:40 +00:00
{
b.LagLog.Save(bw);
});
2017-05-09 18:19:55 +00:00
bs.PutLump(nmarkers, delegate(TextWriter tw)
{
tw.WriteLine(b.Markers.ToString());
});
bs.PutLump(nusertext, delegate(TextWriter tw)
{
tw.WriteLine(b.UserText);
});
2015-07-19 19:57:40 +00:00
nheader.Increment();
ncore.Increment();
ninput.Increment();
nframebuffer.Increment();
nlaglog.Increment();
nmarkers.Increment();
nusertext.Increment();
2015-07-19 19:57:40 +00:00
}
}
public void Load(BinaryStateLoader bl, TasMovie movie)
{
2015-07-19 19:57:40 +00:00
var nheader = new IndexedStateLump(BinaryStateLump.BranchHeader);
var ncore = new IndexedStateLump(BinaryStateLump.BranchCoreData);
var ninput = new IndexedStateLump(BinaryStateLump.BranchInputLog);
var nframebuffer = new IndexedStateLump(BinaryStateLump.BranchFrameBuffer);
var nlaglog = new IndexedStateLump(BinaryStateLump.BranchLagLog);
var nmarkers = new IndexedStateLump(BinaryStateLump.BranchMarkers);
var nusertext = new IndexedStateLump(BinaryStateLump.BranchUserText);
2015-07-19 21:08:55 +00:00
Clear();
2015-07-19 19:57:40 +00:00
while (true)
{
var b = new TasBranch();
if (!bl.GetLump(nheader, false, delegate(TextReader tr)
{
var header = (dynamic)JsonConvert.DeserializeObject(tr.ReadLine());
b.Frame = (int)header.Frame;
var timestamp = header.TimeStamp;
if (timestamp != null)
{
b.TimeStamp = (DateTime)timestamp;
}
else
{
b.TimeStamp = DateTime.Now;
}
var identifier = header.UniqueIdentifier;
if (identifier != null)
{
b.UniqueIdentifier = (Guid)identifier;
}
else
{
b.UniqueIdentifier = Guid.NewGuid();
}
2015-07-19 19:57:40 +00:00
}))
{
return;
}
bl.GetLump(ncore, true, delegate(Stream s, long length)
{
b.CoreData = new byte[length];
s.Read(b.CoreData, 0, b.CoreData.Length);
});
bl.GetLump(ninput, true, delegate(TextReader tr)
{
b.InputLog = StringLogUtil.MakeStringLog();
2015-07-19 19:57:40 +00:00
string line;
while ((line = tr.ReadLine()) != null)
{
2015-07-19 19:57:40 +00:00
b.InputLog.Add(line);
}
2015-07-19 19:57:40 +00:00
});
bl.GetLump(nframebuffer, true, delegate(Stream s, long length)
{
var vp = new QuickBmpFile.LoadedBMP();
2015-07-21 23:17:29 +00:00
QuickBmpFile.Load(vp, s);
b.OSDFrameBuffer = new BitmapBuffer(vp.BufferWidth, vp.BufferHeight, vp.VideoBuffer);
2015-07-19 19:57:40 +00:00
});
bl.GetLump(nlaglog, true, delegate(BinaryReader br)
{
b.LagLog = new TasLagLog();
b.LagLog.Load(br);
});
b.Markers = new TasMovieMarkerList(movie);
2017-05-09 18:19:55 +00:00
bl.GetLump(nmarkers, false, delegate(TextReader tr)
{
string line;
while ((line = tr.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
b.Markers.Add(new TasMovieMarker(line));
}
}
});
bl.GetLump(nusertext, false, delegate(TextReader tr)
{
string line;
if ((line = tr.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
b.UserText = line;
}
}
});
2015-07-19 21:08:55 +00:00
Add(b);
nheader.Increment();
ncore.Increment();
ninput.Increment();
nframebuffer.Increment();
nlaglog.Increment();
nmarkers.Increment();
nusertext.Increment();
2015-07-19 19:57:40 +00:00
}
}
}
}