public static ZwinderStateManager Create(BinaryReader br, ZwinderStateManagerSettings settings, Func <int, bool> reserveCallback) { // Initial format had no version number, but I think it's a safe bet no valid file has buffer size 2^56 or more so this should work. int version = br.ReadByte(); var current = ZwinderBuffer.Create(br, settings.Current(), version == 0); var recent = ZwinderBuffer.Create(br, settings.Recent()); var gaps = ZwinderBuffer.Create(br, settings.GapFiller()); if (version == 0) { settings.AncientStateInterval = br.ReadInt32(); } var ret = new ZwinderStateManager(current, recent, gaps, reserveCallback, settings); var ancientCount = br.ReadInt32(); for (var i = 0; i < ancientCount; i++) { var key = br.ReadInt32(); var length = br.ReadInt32(); var data = br.ReadBytes(length); ret._reserved.Add(key, data); } ret.RebuildStateCache(); return(ret); }
public void UpdateSettings(ZwinderStateManagerSettings settings, bool keepOldStates = false) { Settings = settings; _current = UpdateBuffer(_current, settings.Current(), keepOldStates); _recent = UpdateBuffer(_recent, settings.Recent(), keepOldStates); _gapFiller = UpdateBuffer(_gapFiller, settings.GapFiller(), keepOldStates); if (keepOldStates) { // For ancients ... lets just make sure we aren't keeping states with a gap below the new interval if (settings.AncientStateInterval > _ancientInterval) { int lastReserved = 0; List <int> framesToRemove = new List <int>(); foreach (int f in _reserved.Keys) { if (!_reserveCallback(f) && f - lastReserved < settings.AncientStateInterval) { framesToRemove.Add(f); } else { lastReserved = f; } } foreach (int f in framesToRemove) { if (f != 0) { EvictReserved(f); } } } } else { List <int> framesToRemove = new List <int>(); foreach (int f in _reserved.Keys) { if (f != 0 && !_reserveCallback(f)) { framesToRemove.Add(f); } } foreach (int f in framesToRemove) { EvictReserved(f); } } _ancientInterval = settings.AncientStateInterval; RebuildStateCache(); }