public TagMap(byte[] data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } Reader = new BufferMemoryReader(data); if (data.Length > 0) { Count = Reader.Read <int>(0x00); Entries = new Entry[Count]; var offset = 0x04; for (int i = 0; i < Count; i++) { Entries[i] = new Entry(); Entries[i].Type = Reader.Read <int>(offset); offset += 4; Entries[i].Key = Reader.Read <int>(offset); offset += 4; switch (Entries[i].Type) { case 0: Entries[i].Value = Reader.Read <int>(offset); offset += 4; break; case 1: Entries[i].Value = Reader.Read <float>(offset); offset += 4; break; case 2: Entries[i].Value = Reader.Read <SNO>(offset); offset += 4; break; case 3: Entries[i].Value = Reader.Read <GBID>(offset); offset += 4; break; case 4: Entries[i].Value = new ScriptFormula(Reader, ref offset); break; case 5: // TODO: Better type Entries[i].Value = Reader.Read <int>(offset); offset += 4; break; case 6: Entries[i].Value = Reader.Read <AttributeId>(offset); offset += 4; break; case 7: // TODO: Better type Entries[i].Value = Reader.Read <int>(offset); offset += 4; break; default: throw new NotImplementedException($"Entry type {Entries[i].Type} deserialization not implemented."); } } } }
public void Update() { Container.TakeSnapshot(); if (PreviousData.Length != CurrentData.Length) { Array.Resize(ref PreviousData, CurrentData.Length); } Buffer.BlockCopy(CurrentData, 0, PreviousData, 0, CurrentData.Length); Container.GetAllocatedBytes(ref CurrentData); if (PreviousMapping.Length != CurrentMapping.Length) { Array.Resize(ref PreviousMapping, CurrentMapping.Length); } Buffer.BlockCopy(CurrentMapping, 0, PreviousMapping, 0, CurrentMapping.Length * sizeof(int)); var count = CurrentData.Length / Container.ItemSize; var mr = new BufferMemoryReader(CurrentData); if (CurrentMapping.Length != count) { Array.Resize(ref CurrentMapping, count); } for (int i = 0; i < count; i++) { CurrentMapping[i] = mr.Read <int>(i * Container.ItemSize); } if (PreviousMapping.Length == CurrentMapping.Length) { for (int i = 0; i < CurrentMapping.Length; i++) { if (CurrentMapping[i] != PreviousMapping[i]) { if (PreviousMapping[i] != -1) { OnItemRemoved(i); } if (CurrentMapping[i] != -1) { OnItemAdded(i); } } } } //var state = (Container.NextHash << 16) + Container.NextIndex; //if (state != State) // OnCollectionModified(); //State = state; }
public static SNOFile <T> LoadFile <T>(string path) where T : SerializeMemoryObject { var buffer = new BufferMemoryReader(File.ReadAllBytes(path)); var file = buffer.Read <SNOFile <T> >(0x00); if (!file.Header.IsValid) { throw new InvalidDataException("Invalid header bytes."); } return(file); }
public ScriptFormula(BufferMemoryReader reader, ref int offset) { x00 = reader.Read <int>(offset + 0x00); x04 = reader.Read <int>(offset + 0x04); x08 = reader.Read <int>(offset + 0x08); x0C = reader.Read <int>(offset + 0x0C); x10 = reader.Read <int>(offset + 0x10); OpCodeNameSize = reader.Read <int>(offset + 0x14); x18 = reader.Read <int>(offset + 0x18); OpCodeSize = reader.Read <int>(offset + 0x1C); OpCodeName = reader.ReadString(offset + 0x20, OpCodeNameSize); OpCode = reader.ReadBytes(offset + 0x20 + MemoryObject.AlignedSize(OpCodeNameSize, 4), OpCodeSize); offset += 0x20 + MemoryObject.AlignedSize(OpCodeNameSize, 4) + MemoryObject.AlignedSize(OpCodeSize, 4); }
public T Read(MemoryAddress address) { var block = _blocks.FirstOrDefault(x => x.Contains(address)); if (block == null) { throw new ArgumentException("Address not contained in the cache.", nameof(address)); } var reader = new BufferMemoryReader(block.Data, 0, block.Data.Length, _allocator.Memory.Reader.PointerSize); var offset = address - block.Address; var value = reader.Read <T>(offset); return(value); }
public void Update() { Container.TakeSnapshot(); if (PreviousData.Length != CurrentData.Length) { Array.Resize(ref PreviousData, CurrentData.Length); } Buffer.BlockCopy(CurrentData, 0, PreviousData, 0, CurrentData.Length); Container.GetAllocatedBytes(ref CurrentData); if (PreviousMapping.Length != CurrentMapping.Length) { Array.Resize(ref PreviousMapping, CurrentMapping.Length); } Buffer.BlockCopy(CurrentMapping, 0, PreviousMapping, 0, CurrentMapping.Length * sizeof(int)); var count = CurrentData.Length / Container.ItemSize; var mr = new BufferMemoryReader(CurrentData); if (CurrentMapping.Length != count) { Array.Resize(ref CurrentMapping, count); } for (int i = 0; i < count; i++) { CurrentMapping[i] = mr.Read <int>(i * Container.ItemSize); } NewItems.Clear(); OldItems.Clear(); // Compare against previous where there is a value. for (int i = 0; i < Math.Min(PreviousMapping.Length, CurrentMapping.Length); i++) { if (CurrentMapping[i] != PreviousMapping[i]) { if (PreviousMapping[i] != -1) { OnItemRemoved(i); OldItems.Add(MemoryObjectFactory.UnsafeCreate <T>(new BufferMemoryReader(PreviousData), i * Container.ItemSize)); } if (CurrentMapping[i] != -1) { OnItemAdded(i); NewItems.Add(MemoryObjectFactory.UnsafeCreate <T>(new BufferMemoryReader(CurrentData), i * Container.ItemSize)); } } } // Check expanded area. for (int i = PreviousMapping.Length; i < CurrentMapping.Length; i++) { if (CurrentMapping[i] != -1) { OnItemAdded(i); NewItems.Add(MemoryObjectFactory.UnsafeCreate <T>(new BufferMemoryReader(CurrentData), i * Container.ItemSize)); } } // Check reduced area. for (int i = CurrentMapping.Length; i < PreviousMapping.Length; i++) { if (PreviousMapping[i] != -1) { OnItemRemoved(i); OldItems.Add(MemoryObjectFactory.UnsafeCreate <T>(new BufferMemoryReader(PreviousData), i * Container.ItemSize)); } } }
/// <summary> /// Update the container buffer and item snapshots. Old (removed) items will be added to /// <see cref="OldItems"/> and new items to <see cref="NewItems"/>. /// </summary> public void Update() { _container.TakeSnapshot(); _previousSegments = _currentSegments; if (_previousData.Length != _currentData.Length) { Array.Resize(ref _previousData, _currentData.Length); } Buffer.BlockCopy(_currentData, 0, _previousData, 0, _currentData.Length); _currentSegments = _container.GetAllocatedBytes(ref _currentData); if (_currentData.Length != _previousData.Length) // buffer was resized (and replaced), update underlying buffer for all items { for (int i = 0; i < _items.Length; i++) { if (_items[i] == null) { continue; } _items[i].SetSnapshot(_currentData, i * _container.ItemSize, _container.ItemSize); } } if (_previousMapping.Length != _currentMapping.Length) { Array.Resize(ref _previousMapping, _currentMapping.Length); } Buffer.BlockCopy(_currentMapping, 0, _previousMapping, 0, _currentMapping.Length * sizeof(int)); var count = _currentData.Length / _container.ItemSize; var mr = new BufferMemoryReader(_currentData); if (_currentMapping.Length != count) { Array.Resize(ref _currentMapping, count); } for (int i = 0; i <= _container.MaxIndex; i++) { _currentMapping[i] = mr.Read <int>(i * _container.ItemSize); } for (int i = _container.MaxIndex + 1; i < count; i++) { _currentMapping[i] = -1; } NewItems.Clear(); OldItems.Clear(); if (_items.Length != _container.Capacity) { Array.Resize(ref _items, _container.Capacity); } // Compare against previous where there is a value. for (int i = 0; i < Math.Min(_previousMapping.Length, _currentMapping.Length); i++) { if (_currentMapping[i] != _previousMapping[i]) { if (_previousMapping[i] != -1) { var item = CreatePreviousItem(i); OnItemRemoved(i, item); OldItems.Add(item); } if (_currentMapping[i] != -1 && _currentMapping[i] != 0) // NB: New item starts with ID 0 { var item = CreateCurrentItem(i); OnItemAdded(i, item); NewItems.Add(item); } } } // Check expanded area. for (int i = _previousMapping.Length; i < _currentMapping.Length; i++) { if (_currentMapping[i] != -1) { var item = CreateCurrentItem(i); OnItemAdded(i, item); NewItems.Add(item); } } // Check reduced area. for (int i = _currentMapping.Length; i < _previousMapping.Length; i++) { if (_previousMapping[i] != -1) { var item = CreatePreviousItem(i); OnItemRemoved(i, item); OldItems.Add(item); } } }