public void NextWordsWorksAsExpectedFromDifferentIndex() { byte[] bytes = { 0xff, 0x00, 0x0f, 0xf0, 0xff, 0x00, 0x0f, 0xf0, 0xff, 0x00, 0x0f, 0xf0, 0xff, 0x00, 0x0f, 0xf0 }; var reader = new MemoryReader(bytes, 8); ushort[][] expected = { new ushort[] { 0xff00, 0x0ff0 }, new ushort[] { 0xff00, 0x0ff0 }, new ushort[] { 0xff00, 0x0ff0 }, new ushort[] { 0xff00, 0x0ff0 } }; for (int i = 2; i < expected.Length; i++) { var w = reader.NextWords(2); Assert.That(w, Is.EqualTo(expected[i])); } Assert.That(reader.Address, Is.EqualTo(reader.Size)); Assert.That(() => reader.NextWords(2), Throws.InstanceOf <InvalidOperationException>()); }
internal ZDictionary(Story story, ZText ztext) { this.story = story; this.ztext = ztext; this.address = Header.ReadDictionaryAddress(story.Memory); var reader = new MemoryReader(story.Memory, address); int wordSepCount = reader.NextByte(); this.wordSeparators = reader.NextBytes(wordSepCount).ConvertAll(b => (char)b).AsReadOnly(); int entryLength = reader.NextByte(); int entryCount = reader.NextWord(); int zwordsSize = story.Version <= 3 ? 2 : 3; int dataSize = entryLength - (zwordsSize * 2); this.entries = new List <ZDictionaryEntry>(entryCount); for (int i = 0; i < entryCount; i++) { var entryAddress = reader.Address; var entryZWords = reader.NextWords(zwordsSize); var entryData = reader.NextBytes(dataSize); var entryZText = ztext.ZWordsAsString(entryZWords, ZTextFlags.All); entries.Add(new ZDictionaryEntry(entryAddress, i, entryZWords, entryZText, entryData)); } }
//private void MemoryChanged(object sender, MemoryEventArgs e) //{ // // Replace affected lines // int firstLineIndex = e.Address / 16; // int lastLineIndex = (e.Address + e.Length) / 16; // var reader = e.Memory.CreateReader(firstLineIndex * 16); // for (int i = firstLineIndex; i <= lastLineIndex; i++) // { // var address = reader.Address; // var count = Math.Min(8, reader.RemainingBytes); // var values = reader.NextWords(count); // lines[i] = new MemoryLineViewModel(address, values); // } // // TODO: Highlight modified memory //} private void StoryService_StoryOpened(object sender, StoryOpenedEventArgs e) { var reader = new MemoryReader(e.Story.Memory, 0); lines.BeginBulkOperation(); try { while (reader.RemainingBytes > 0) { var address = reader.Address; ushort[] values; if (reader.RemainingBytes >= 16 || reader.RemainingBytes % 2 == 0) { var count = Math.Min(8, reader.RemainingBytes / 2); values = reader.NextWords(count); } else { // if the last line is an odd number of bytes... // TODO: memory view always shows even number of bytes // (padding with zeroes if necessry). Need to fix it to show odd // number of bytes if that's the case. var valueList = new List <ushort>(); while (reader.RemainingBytes > 0) { if (reader.RemainingBytes > 2) { valueList.Add(reader.NextWord()); } else { valueList.Add((ushort)(reader.NextByte() << 8)); } } values = valueList.ToArray(); } lines.Add(new MemoryLineViewModel(address, values)); } } finally { lines.EndBulkOperation(); } PropertyChanged("HasStory"); }