diff --git a/BizHawk.Client.Common/movie/MovieSession.cs b/BizHawk.Client.Common/movie/MovieSession.cs index c87c7ad1ea..e8b8f6b247 100644 --- a/BizHawk.Client.Common/movie/MovieSession.cs +++ b/BizHawk.Client.Common/movie/MovieSession.cs @@ -87,6 +87,12 @@ namespace BizHawk.Client.Common } } + public void MovieLoad() + { + Movie.Load(); + MovieControllerAdapter = Movie.LogGeneratorInstance().MovieControllerAdapter; + } + public void StopMovie(bool saveChanges = true) { var message = "Movie "; diff --git a/BizHawk.Client.Common/movie/bk2/Bk2ControllerAdapter.cs b/BizHawk.Client.Common/movie/bk2/Bk2ControllerAdapter.cs index 088de256e1..1ebb925c0a 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2ControllerAdapter.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2ControllerAdapter.cs @@ -10,6 +10,31 @@ namespace BizHawk.Client.Common { public class Bk2ControllerAdapter : IMovieController { + private string _logKey = string.Empty; + + public Bk2ControllerAdapter(string key) + { + _logKey = key; + SetLogOverride(); + } + + private void SetLogOverride() + { + if (!string.IsNullOrEmpty(_logKey)) + { + // TODO: this could be cleaned up into a LINQ select + List> controls = new List>(); + var groups = _logKey.Split(new[] { "#" }, StringSplitOptions.RemoveEmptyEntries); + foreach (var group in groups) + { + var buttons = group.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries).ToList(); + controls.Add(buttons); + } + + _type.ControlsFromLog = controls; + } + } + #region IController Implementation public bool this[string button] @@ -31,7 +56,21 @@ namespace BizHawk.Client.Common #region IMovieController Implementation - public ControllerDefinition Type { get; set; } + private Bk2ControllerDefinition _type = new Bk2ControllerDefinition(); + + public ControllerDefinition Type + { + get + { + return _type; + } + + set + { + _type = new Bk2ControllerDefinition(value); + SetLogOverride(); + } + } /// /// latches one player from the source @@ -79,6 +118,7 @@ namespace BizHawk.Client.Common { if (!string.IsNullOrWhiteSpace(mnemonic)) { + var def = Global.Emulator.ControllerDefinition; var trimmed = mnemonic.Replace("|", ""); var buttons = Type.ControlsOrdered.SelectMany(x => x).ToList(); var iterator = 0; @@ -87,20 +127,19 @@ namespace BizHawk.Client.Common for (int i = 0; i < buttons.Count; i++) { - if (Type.BoolButtons.Contains(buttons[i])) + var b = buttons[i]; + if (def.BoolButtons.Contains(buttons[i])) { - var boolBtn = Type.BoolButtons.First(x => x == buttons[i]); - MyBoolButtons[boolBtn] = trimmed[iterator] == '.' ? false : true; + MyBoolButtons[buttons[i]] = trimmed[iterator] == '.' ? false : true; iterator++; boolIt++; } - else if (Type.FloatControls.Contains(buttons[i])) + else if (def.FloatControls.Contains(buttons[i])) { var temp = trimmed.Substring(iterator, 3); var val = int.Parse(temp); - var floatBtn = Type.FloatControls.First(x => x == buttons[i]); - MyFloatControls[floatBtn] = val; + MyFloatControls[buttons[i]] = val; iterator += 4; floatIt++; } @@ -110,6 +149,38 @@ namespace BizHawk.Client.Common #endregion + public class Bk2ControllerDefinition : ControllerDefinition + { + public Bk2ControllerDefinition() + : base() + { + + } + + public Bk2ControllerDefinition(ControllerDefinition source) + : base(source) + { + + } + + public List> ControlsFromLog = new List>(); + + public override IEnumerable> ControlsOrdered + { + get + { + if (ControlsFromLog.Any()) + { + return ControlsFromLog; + } + + return base.ControlsOrdered; + } + } + } + + + private readonly WorkingDictionary MyBoolButtons = new WorkingDictionary(); private readonly WorkingDictionary MyFloatControls = new WorkingDictionary(); } diff --git a/BizHawk.Client.Common/movie/bk2/Bk2LogEntryGenerator.cs b/BizHawk.Client.Common/movie/bk2/Bk2LogEntryGenerator.cs index 3a556b3cc5..68903a21e8 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2LogEntryGenerator.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2LogEntryGenerator.cs @@ -11,12 +11,18 @@ namespace BizHawk.Client.Common { private readonly Bk2MnemonicConstants Mnemonics = new Bk2MnemonicConstants(); private IController _source; + private string _logKey = string.Empty; + + public Bk2LogEntryGenerator(string logKey) + { + _logKey = logKey; + } public IMovieController MovieControllerAdapter { get { - return new Bk2ControllerAdapter(); + return new Bk2ControllerAdapter(_logKey); } } @@ -69,8 +75,9 @@ namespace BizHawk.Client.Common var sb = new StringBuilder(); sb.Append("LogKey:"); - foreach (var group in _source.Type.ControlsOrdered) + foreach (var group in _source.Type.ControlsOrdered.Where(c => c.Any())) { + sb.Append("#"); foreach (var button in group) { sb diff --git a/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs b/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs index 2a7aa57c8d..5d55e850d9 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs @@ -107,14 +107,8 @@ namespace BizHawk.Client.Common bl.GetLump(BinaryStateLump.Input, true, delegate(TextReader tr) { - string line; - while ((line = tr.ReadLine()) != null) - { - if (line != null && line.StartsWith("|")) - { - _log.Add(line); - } - } + string errorMessage = string.Empty; + ExtractInputLog(tr, out errorMessage); }); if (StartsFromSavestate) diff --git a/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs b/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs index 3c6a2f3528..425db9cf30 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs @@ -10,6 +10,7 @@ namespace BizHawk.Client.Common public partial class Bk2Movie : IMovie { private readonly List _log = new List(); + private string _logKey = string.Empty; public string GetInputLog() { @@ -40,22 +41,11 @@ namespace BizHawk.Client.Common while (true) { var line = reader.ReadLine(); - if (line == null) + if (string.IsNullOrEmpty(line)) { break; } - - if (line.Trim() == string.Empty || line == "[Input]") - { - continue; - } - - if (line == "[/Input]") - { - break; - } - - if (line.Contains("Frame 0x")) // NES stores frame count in hex, yay + else if (line.Contains("Frame 0x")) // NES stores frame count in hex, yay { var strs = line.Split('x'); try @@ -81,6 +71,10 @@ namespace BizHawk.Client.Common return false; } } + else if (line.StartsWith("LogKey:")) + { + _logKey = line.Replace("LogKey:", ""); + } else if (line[0] == '|') { _log.Add(line); @@ -98,16 +92,6 @@ namespace BizHawk.Client.Common break; } - if (line.Trim() == string.Empty || line == "[Input]") - { - continue; - } - - if (line == "[/Input]") - { - break; - } - if (line.Contains("Frame 0x")) // NES stores frame count in hex, yay { var strs = line.Split('x'); @@ -134,6 +118,10 @@ namespace BizHawk.Client.Common return false; } } + else if (line.StartsWith("LogKey:")) + { + _logKey = line.Replace("LogKey:", ""); + } else if (line.StartsWith("|")) { SetFrameAt(i, line); @@ -185,7 +173,7 @@ namespace BizHawk.Client.Common var line = reader.ReadLine(); if (line == null) { - return false; + break; } if (line.Trim() == string.Empty) @@ -219,14 +207,6 @@ namespace BizHawk.Client.Common return false; } } - else if (line == "[Input]") - { - continue; - } - else if (line == "[/Input]") - { - break; - } else if (line[0] == '|') { newLog.Add(line); @@ -286,7 +266,7 @@ namespace BizHawk.Client.Common private StringBuilder RawInputLog() { - var lg = new Bk2LogEntryGenerator(); + var lg = new Bk2LogEntryGenerator(_logKey); lg.SetSource(Global.MovieOutputHardpoint); var sb = new StringBuilder(); diff --git a/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs b/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs index be5f15ddb6..7d69431bb8 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs @@ -44,7 +44,7 @@ namespace BizHawk.Client.Common public ILogEntryGenerator LogGeneratorInstance() { - return new Bk2LogEntryGenerator(); + return new Bk2LogEntryGenerator(_logKey); } public double FrameCount @@ -180,7 +180,7 @@ namespace BizHawk.Client.Common getframe = frame; } - var adapter = new Bk2ControllerAdapter(); + var adapter = new Bk2ControllerAdapter(_logKey); adapter.Type = Global.MovieSession.MovieControllerAdapter.Type; adapter.SetControllersAsMnemonic(_log[getframe]); return adapter; diff --git a/BizHawk.Client.EmuHawk/MainForm.Movie.cs b/BizHawk.Client.EmuHawk/MainForm.Movie.cs index bda78ae7fb..0e57aac2da 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Movie.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Movie.cs @@ -34,7 +34,7 @@ namespace BizHawk.Client.EmuHawk if (!record) { - Global.MovieSession.Movie.Load(); + Global.MovieSession.MovieLoad(); } try diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs index 58373548a7..bc3384067b 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs @@ -265,7 +265,7 @@ namespace BizHawk.Client.EmuHawk if (AskSave()) { _tas.Filename = path; - var loadResult = _tas.Load(); + var loadResult = _tas.Load(); // TODO: Global.MovieSession.MovieLoad() needs to be called in order to set up the Movie adapter properly if (!loadResult) { ToolHelpers.HandleLoadError(Global.Config.RecentTas, path);