/// <summary>
        /// Performs a lookup based on the contents of the passed byte buffer. If the byte contents match a known string, the cached string instance is returned.
        /// Otherwise a new string is created by decoding the buffer contents.
        /// </summary>
        /// <param name="buffer">The byte array segment to read from</param>
        /// <returns>A string instance that match the decoded value of the passed byte buffer.</returns>
        public string DecodeString(ArraySegment <byte> buffer)
        {
            var    searchKey = new BufferKey(buffer);
            string knownString;

            if (this.knownStrings.TryGetValue(searchKey, out knownString))
            {
                return(knownString);
            }

            return(Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count));
        }
        /// <summary>
        /// Adds a known string. Must be called before it is set to Encoder.StringDecoder.
        /// </summary>
        /// <param name="knownString">The known string instance</param>
        public void AddWellknownString(string knownString)
        {
            if (knownString == null)
            {
                throw new ArgumentNullException("knownString");
            }

            byte[] encodedString = Encoding.UTF8.GetBytes(knownString);
            var    key           = new BufferKey(encodedString);

            this.knownStrings[key] = knownString;
        }
Beispiel #3
0
        public VertexBuffer GetVertexBuffer(MeshData mesh)
        {
            VertexBuffer vb;
            BufferKey    key = new BufferKey(mesh);

            if (!_vbs.TryGetValue(key, out vb))
            {
                vb = mesh.CreateVertexBuffer(_factory);
                if (!_vbs.TryAdd(key, vb))
                {
                    vb.Dispose();
                    return(_vbs[key]);
                }
            }

            return(vb);
        }
Beispiel #4
0
        public IndexBufferAndCount GetIndexBufferAndCount(MeshData mesh)
        {
            int indexCount;
            IndexBufferAndCount bufferAndCount;
            BufferKey           key = new BufferKey(mesh);

            if (!_ibs.TryGetValue(key, out bufferAndCount))
            {
                var indexBuffer = mesh.CreateIndexBuffer(_factory, out indexCount);
                bufferAndCount = new IndexBufferAndCount(indexBuffer, indexCount);
                if (!_ibs.TryAdd(key, bufferAndCount))
                {
                    indexBuffer.Dispose();
                    return(_ibs[key]);
                }
            }
            else
            {
                indexCount = bufferAndCount.IndexCount;
            }

            return(bufferAndCount);
        }