TasBranchCollection - encapsulate unique identifier logic into TasBranchCollection

This commit is contained in:
adelikat 2015-10-04 10:43:36 -04:00
parent 4625bdce0f
commit 9f67d8e59b
3 changed files with 17 additions and 11 deletions

View File

@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using BizHawk.Bizware.BizwareGL;
@ -9,14 +10,6 @@ namespace BizHawk.Client.Common
{
public class TasBranch
{
public TasBranch()
{
do
{
UniqueIdentifier = Guid.NewGuid();
} while (TasMovie.BranchIndexByHash(UniqueIdentifier.GetHashCode()) != -1);
}
public int Frame { get; set; }
public byte[] CoreData { get; set; }
public List<string> InputLog { get; set; }
@ -30,6 +23,19 @@ namespace BizHawk.Client.Common
public class TasBranchCollection : List<TasBranch>
{
public new void Add(TasBranch item)
{
var currentHashes = this.Select(b => b.UniqueIdentifier.GetHashCode()).ToList();
// TODO: loop until this is unique
if (currentHashes.Contains(item.UniqueIdentifier.GetHashCode()))
{
item.UniqueIdentifier = Guid.NewGuid();
}
base.Add(item);
}
public void Save(BinaryStateSaver bs)
{
var nheader = new IndexedStateLump(BinaryStateLump.BranchHeader);

View File

@ -22,7 +22,7 @@ namespace BizHawk.Client.Common
private readonly Dictionary<int, IController> InputStateCache = new Dictionary<int, IController>();
public readonly List<string> VerificationLog = new List<string>(); // For movies that do not begin with power-on, this is the input required to get into the initial state
public static readonly TasBranchCollection Branches = new TasBranchCollection();
public readonly TasBranchCollection Branches = new TasBranchCollection();
private BackgroundWorker _progressReportWorker = null;
public void NewBGWorker(BackgroundWorker newWorker)
@ -85,7 +85,7 @@ namespace BizHawk.Client.Common
public TasBranch GetBranch(int index) { return Branches[index]; }
public int BranchHashByIndex(int index) { return Branches[index].UniqueIdentifier.GetHashCode(); }
public static int BranchIndexByHash(int hash)
public int BranchIndexByHash(int hash)
{
TasBranch branch = Branches.Where(b => b.UniqueIdentifier.GetHashCode() == hash).SingleOrDefault();
if (branch == null)

View File

@ -710,7 +710,7 @@ namespace BizHawk.Client.Common
SortedList<int, StateManagerState> stateList = BranchStates[frame];
for (int i = 0; i < _movie.BranchCount; i++)
{
if (i == TasMovie.BranchIndexByHash(branchHash))
if (i == _movie.BranchIndexByHash(branchHash))
continue;
if (stateList != null && stateList.ContainsKey(i) && stateList[i] == stateToMatch)