Exemplo n.º 1
0
        private void ReadDataArea(BinaryReader br, int size)
        {
            //Load bytes
            int count = br.ReadInt32();

            if (mByteArray == null || mByteArray.Length < count)
            {
                int c = count < MIN_BYTE_COUNT ? MIN_BYTE_COUNT : count;
                mByteArray = new byte[c];
            }
            if (count > 0)
            {
                Array.Copy(br.ReadBytes(count), 0, mByteArray, 0, count);
                var padding = 4 - count % 4;
                if (padding > 0 && padding < 4)
                {
                    br.ReadBytes(padding);
                }
            }

            count = br.ReadInt32();
            // load short
            if (mShortArray == null || mShortArray.Length < count)
            {
                int c = count < MIN_SHORT_COUNT ? MIN_SHORT_COUNT : count;
                mShortArray = new short[c];
            }
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    mShortArray[i] = br.ReadInt16();
                }
                var padding = 4 - (count * 2) % 4;
                if (padding > 0 && padding < 4)
                {
                    br.ReadBytes(padding);
                }
            }

            count = br.ReadInt32();
            //Load int
            if (mIntArray == null || mIntArray.Length < count)
            {
                int c = count < MIN_INT_COUNT ? MIN_INT_COUNT : count;
                mIntArray = new int[c];
            }
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    mIntArray[i] = br.ReadInt32();
                }
            }

            count = br.ReadInt32();
            // load long
            if (mLongArray == null || mLongArray.Length < count)
            {
                int c = count < MIN_LONG_COUNT ? MIN_LONG_COUNT : count;
                mLongArray = new long[c];
            }
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    mLongArray[i] = br.ReadInt64();
                }
            }

            count = br.ReadInt32();
            // load double
            if (mDoubleArray == null || mDoubleArray.Length < count)
            {
                int c = count < MIN_DOUBLE_COUNT ? MIN_DOUBLE_COUNT : count;
                mDoubleArray = new double[c];
            }
            if (count > 0)
            {
                //TODO: Remove it
                if (mDoubleTmpArray == null || mDoubleTmpArray.Length < count)
                {
                    int c = count < MIN_DOUBLE_COUNT ? MIN_DOUBLE_COUNT : count;
                    mDoubleTmpArray = new long[c];
                }
                for (int i = 0; i < count; i++)
                {
                    mDoubleArray[i] = br.ReadDouble();
                }
            }

            count = br.ReadInt32();
            //load string
            if (mStringArray == null || mStringArray.Length < count)
            {
                int c = count < MIN_STRING_COUNT ? MIN_STRING_COUNT : count;
                mStringArray = new string[c];
            }
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    int    len = br.ReadInt32();
                    char[] ch  = new char[len];
                    for (int j = 0; j < len; j++)
                    {
                        ch[j] = (char)br.ReadInt16();
                    }
                    mStringArray[i] = Tjs.MapGlobalStringMap(new string(ch));
                    var padding = 4 - (len * 2) % 4;
                    if (padding > 0 && padding < 4)
                    {
                        br.ReadBytes(padding);
                    }
                }
            }

            count = br.ReadInt32();
            //load bytebuffer
            if (mByteBufferArray == null || mByteBufferArray.Length < count)
            {
                mByteBufferArray = new ByteBuffer[count];
            }
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    int    len = br.ReadInt32();
                    byte[] tmp = new byte[len];
                    System.Array.Copy(br.ReadBytes(len), 0, tmp, 0, len);
                    mByteBufferArray[i] = ByteBuffer.Wrap(tmp);
                    mByteBufferArray[i].Position(len);
                    var padding = 4 - len % 4;
                    if (padding > 0 && padding < 4)
                    {
                        br.ReadBytes(padding);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <exception cref="Kirikiri.Tjs2.TJSException"></exception>
        public virtual ScriptBlock ReadByteCode(Tjs owner, string name, BinaryStream input)
        {
            try
            {
                var br   = new BinaryReader(input.GetInputStream());
                int size = (int)input.GetSize();

                // TJS2
                var tag = br.ReadChars(4).ToRealString();
                if (tag != FILE_TAG_LE)
                {
                    return(null);
                }
                // 100'\0'
                if (br.ReadChars(3).ToRealString() != VER_TAG_LE)
                {
                    return(null);
                }
                br.ReadChar();

                int filesize = br.ReadInt32();
                if (filesize != size)
                {
                    return(null);
                }
                //// DATA
                if (br.ReadChars(4).ToRealString() != DATA_TAG_LE)
                {
                    return(null);
                }
                size = br.ReadInt32();
                ReadDataArea(br, size);
                // これがデータエリア后の位置
                // OBJS
                if (br.ReadChars(4).ToRealString() != OBJ_TAG_LE)
                {
                    return(null);
                }
                //int objsize = ibuff.get();
                ScriptBlock block = new ScriptBlock(owner, name, 0, null, null);
                ReadObjects(block, br);
                return(block);
            }
            finally
            {
                if (mDeleteBuffer)
                {
                    mReadBuffer      = null;
                    mByteArray       = null;
                    mShortArray      = null;
                    mIntArray        = null;
                    mLongArray       = null;
                    mDoubleArray     = null;
                    mDoubleTmpArray  = null;
                    mStringArray     = null;
                    mByteBufferArray = null;
                    mObjectsCache.Release();
                    mVariantTypeData = null;
                }
            }
        }