Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new z machine character encoder
        /// </summary>
        /// <param name="buf">game memory buffer</param>
        public ZTextEncoder(MemoryBuffer buf)
        {
            //Store buffer
            this.buf = buf;

            //Get version
            this.machineVersion = buf.GetByte(0);

            //Find unicode table address
            int unicodeTable = 0;

            if (machineVersion >= 5)
            {
                //Search the extension header table
                ushort extensionHdr = buf.GetUShort(0x36);

                if (extensionHdr != 0 && buf.GetUShort(extensionHdr) >= 3)
                {
                    unicodeTable = buf.GetUShort(extensionHdr + 6);
                }
            }

            //Create unicode cache
            this.unicodeCache = CreateUnicodeCache(unicodeTable);

            //Create reverse cache
            // Go in reverse so that ASCII takes priority over custom unicode
            for(int i = 255; i > 0; i--)
            {
                //Add character
                if (unicodeCache[i] != InvalidChar)
                {
                    reverseUnicodeCache[unicodeCache[i]] = (byte) i;
                }
            }

            //Create alphabet cache
            if (machineVersion >= 5 && buf.GetUShort(0x34) != 0)
            {
                this.alphabetCache = CreateAlphabetCache(buf.GetUShort(0x34));
            }
            else
            {
                //Use default alphabet
                this.alphabetCache = machineVersion == 1 ? DEFAULT_ALPHABET_TABLE_V1 : DEFAULT_ALPHABET_TABLE;
            }

            //Create reverse alphabet cache
            for (int i = 0; i < 78; i++)
            {
                reverseAlphabetCache[reverseUnicodeCache[alphabetCache[i]]] = (byte) (i + 1);
            }

            //Create abbreviation cache
            if (machineVersion >= 2 && buf.GetUShort(0x18) != 0)
            {
                this.abbreviationCache = CreateAbbreviationCache(buf.GetUShort(0x18));
            }

            //Cache dictionary information
            mainDictionaryAddress = buf.GetUShort(0x8);
            mainDictionary = GetDictionaryInfo(mainDictionaryAddress);
        }