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

231 lines
4.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
using System.Linq;
2014-12-05 00:59:00 +00:00
using BizHawk.Emulation.Common.IEmulatorExtensions;
namespace BizHawk.Client.Common
{
public class TasLagLog
{
// TODO: Change this into a regular list.
2017-05-19 18:17:07 +00:00
private List<bool> _lagLog = new List<bool>();
private List<bool> _wasLag = new List<bool>();
public bool? this[int frame]
{
get
{
2017-05-19 18:17:07 +00:00
if (frame < _lagLog.Count)
{
if (frame < 0)
{
return null;
}
2017-05-19 18:17:07 +00:00
return _lagLog[frame];
}
2017-05-19 18:17:07 +00:00
if (frame == Global.Emulator.Frame && frame == _lagLog.Count)
{
2017-05-19 18:17:07 +00:00
////LagLog[frame] = Global.Emulator.AsInputPollable().IsLagFrame; // Note: Side effects!
2014-12-05 00:59:00 +00:00
return Global.Emulator.AsInputPollable().IsLagFrame;
}
return null;
}
set
{
if (!value.HasValue)
{
2017-05-19 18:17:07 +00:00
_lagLog.RemoveAt(frame);
return;
}
if (frame < 0)
{
return; // Nothing to do
}
2017-05-19 18:17:07 +00:00
if (frame > _lagLog.Count)
{
2017-05-19 18:17:07 +00:00
System.Diagnostics.Debug.Print("Lag Log error. f" + frame + ", log: " + _lagLog.Count);
return; // Can this break anything?
}
2015-03-22 16:55:34 +00:00
bool wasValue;
2017-05-19 18:17:07 +00:00
if (frame < _lagLog.Count)
2017-05-17 18:18:26 +00:00
{
2017-05-19 18:17:07 +00:00
wasValue = _lagLog[frame];
2017-05-17 18:18:26 +00:00
}
2017-05-19 18:17:07 +00:00
else if (frame == _wasLag.Count)
2017-05-17 18:18:26 +00:00
{
2015-03-22 16:55:34 +00:00
wasValue = value.Value;
2017-05-17 18:18:26 +00:00
}
else
2017-05-17 18:18:26 +00:00
{
2017-05-19 18:17:07 +00:00
wasValue = _wasLag[frame];
2017-05-17 18:18:26 +00:00
}
2017-05-19 18:17:07 +00:00
if (frame == _wasLag.Count)
2017-05-17 18:18:26 +00:00
{
2017-05-19 18:17:07 +00:00
_wasLag.Add(wasValue);
2017-05-17 18:18:26 +00:00
}
else
2017-05-17 18:18:26 +00:00
{
2017-05-19 18:17:07 +00:00
_wasLag[frame] = wasValue;
2017-05-17 18:18:26 +00:00
}
2015-03-22 16:55:34 +00:00
2015-03-23 20:15:35 +00:00
if (frame != 0)
2017-05-17 18:18:26 +00:00
{
2017-05-19 18:17:07 +00:00
_wasLag[frame - 1] = _lagLog[frame - 1];
2017-05-17 18:18:26 +00:00
}
2017-05-19 18:17:07 +00:00
if (frame >= _lagLog.Count)
2017-05-17 18:18:26 +00:00
{
2017-05-19 18:17:07 +00:00
_lagLog.Add(value.Value);
2017-05-17 18:18:26 +00:00
}
2015-03-22 16:55:34 +00:00
else
2017-05-17 18:18:26 +00:00
{
2017-05-19 18:17:07 +00:00
_lagLog[frame] = value.Value;
2017-05-17 18:18:26 +00:00
}
}
}
public void Clear()
{
2017-05-19 18:17:07 +00:00
_lagLog.Clear();
}
public bool RemoveFrom(int frame)
{
2017-05-19 18:17:07 +00:00
if (_lagLog.Count > frame && frame >= 0)
{
2017-05-19 18:17:07 +00:00
_lagLog.RemoveRange(frame + 1, _lagLog.Count - frame - 1);
return true;
}
return false;
}
public void RemoveHistoryAt(int frame)
{
2017-05-19 18:17:07 +00:00
_wasLag.RemoveAt(frame);
}
public void InsertHistoryAt(int frame, bool isLag)
{
// LagLog was invalidated when the frame was inserted
2017-05-19 18:17:07 +00:00
if (frame <= _lagLog.Count)
{
2017-05-19 18:17:07 +00:00
_lagLog.Insert(frame, isLag);
}
2017-05-19 18:17:07 +00:00
_wasLag.Insert(frame, isLag);
}
public void Save(BinaryWriter bw)
{
bw.Write((byte)1); // New saving format.
2017-05-19 18:17:07 +00:00
bw.Write(_lagLog.Count);
bw.Write(_wasLag.Count);
for (int i = 0; i < _lagLog.Count; i++)
{
2017-05-19 18:17:07 +00:00
bw.Write(_lagLog[i]);
bw.Write(_wasLag[i]);
}
2017-05-19 18:17:07 +00:00
for (int i = _lagLog.Count; i < _wasLag.Count; i++)
{
2017-05-19 18:17:07 +00:00
bw.Write(_wasLag[i]);
}
}
public void Load(BinaryReader br)
{
2017-05-19 18:17:07 +00:00
_lagLog.Clear();
_wasLag.Clear();
2017-05-09 18:19:55 +00:00
////if (br.BaseStream.Length > 0)
////{ BaseStream.Length does not return the expected value.
2015-03-23 20:15:35 +00:00
int formatVersion = br.ReadByte();
if (formatVersion == 0)
{
int length = (br.ReadByte() << 8) | formatVersion; // The first byte should be a part of length.
length = (br.ReadInt16() << 16) | length;
for (int i = 0; i < length; i++)
{
2015-03-23 20:15:35 +00:00
br.ReadInt32();
2017-05-19 18:17:07 +00:00
_lagLog.Add(br.ReadBoolean());
_wasLag.Add(_lagLog.Last());
}
2015-03-23 20:15:35 +00:00
}
else if (formatVersion == 1)
{
int length = br.ReadInt32();
int lenWas = br.ReadInt32();
for (int i = 0; i < length; i++)
{
2017-05-19 18:17:07 +00:00
_lagLog.Add(br.ReadBoolean());
_wasLag.Add(br.ReadBoolean());
}
2017-05-09 18:19:55 +00:00
2015-03-23 20:15:35 +00:00
for (int i = length; i < lenWas; i++)
2017-05-09 18:19:55 +00:00
{
2017-05-19 18:17:07 +00:00
_wasLag.Add(br.ReadBoolean());
2017-05-09 18:19:55 +00:00
}
2015-03-23 20:15:35 +00:00
}
2017-05-09 18:19:55 +00:00
////}
}
public bool? History(int frame)
{
2017-05-19 18:17:07 +00:00
if (frame < _wasLag.Count)
{
if (frame < 0)
2017-05-09 18:19:55 +00:00
{
return null;
2017-05-09 18:19:55 +00:00
}
2017-05-19 18:17:07 +00:00
return _wasLag[frame];
}
return null;
}
public int LastValidFrame
{
get
{
2017-05-19 18:17:07 +00:00
if (_lagLog.Count == 0)
2017-05-09 18:19:55 +00:00
{
return 0;
2017-05-09 18:19:55 +00:00
}
2017-05-19 18:17:07 +00:00
return _lagLog.Count - 1;
}
}
public TasLagLog Clone()
{
2017-05-09 18:19:55 +00:00
return new TasLagLog
{
2017-05-19 18:17:07 +00:00
_lagLog = _lagLog.ToList(),
_wasLag = _wasLag.ToList()
2017-05-09 18:19:55 +00:00
};
}
public void FromLagLog(TasLagLog log)
{
2017-05-19 18:17:07 +00:00
_lagLog = log._lagLog.ToList();
_wasLag = log._wasLag.ToList();
}
public void StartFromFrame(int index)
{
2017-05-19 18:17:07 +00:00
_lagLog.RemoveRange(0, index);
_wasLag.RemoveRange(0, index);
}
}
}