internal DataCenterElement Materialize(DataCenterAddress address)
        {
            DataCenterElement Create(DataCenterAddress address)
            {
                return(new DataCenterElement(this, address));
            }

            switch (Mode)
            {
            case DataCenterMode.Persistent:
                return(_elements.GetOrAdd(address, Create));

            case DataCenterMode.Transient:
                return(Create(address));

            case DataCenterMode.Weak:
                var weak = _weakElements.GetOrAdd(address,
                                                  a => new WeakReference <DataCenterElement>(Create(a)));

                if (!weak.TryGetTarget(out var elem))
                {
                    weak.SetTarget(elem = Create(address));
                }

                return(elem);

            default:
                throw Assert.Unreachable();
            }
        }
Exemple #2
0
        public TeraBinaryReader GetReader(DataCenterAddress address)
        {
            if (address.SegmentIndex >= Segments.Count)
            {
                throw new InvalidDataException();
            }

            return(Segments[address.SegmentIndex].GetReader(address.ElementIndex));
        }
        internal string GetString(DataCenterAddress address)
        {
            return(_strings.GetOrAdd(address, a =>
            {
                var str = _stringRegion.GetReader(address).ReadString();

                return _intern ? string.Intern(str) : str;
            }));
        }
        public GameBinaryReader GetReader(DataCenterAddress address)
        {
            if (address.SegmentIndex >= Segments.Count)
            {
                throw new InvalidDataException(
                          $"Segment index {address.SegmentIndex} is greater than {Segments.Count}.");
            }

            return(Segments[address.SegmentIndex].GetReader(address.ElementIndex));
        }
        public string Get(DataCenterAddress address)
        {
            if (address == DataCenterAddress.Invalid)
            {
                throw new InvalidDataException($"String address {address} is invalid.");
            }

            return(_strings.GetOrAdd(address, a =>
            {
                var str = _data.GetReader(a).ReadString();

                return _intern ? string.Intern(str) : str;
            }));
        }
Exemple #6
0
 static string ReadString(
     DataCenterSegmentedRegion region, DataCenterAddress address)
 {
     return(region.GetReader(address).ReadString());
 }
Exemple #7
0
 internal string GetString(DataCenterAddress address)
 {
     return(_strings.GetOrAdd(address, a =>
                              _stringRegion.GetReader(address).ReadString()));
 }
        internal DataCenterElement(DataCenter center,
                                   DataCenterAddress address)
        {
            ushort            attrCount;
            ushort            childCount;
            DataCenterAddress attrAddr;
            DataCenterAddress childAddr;

            try
            {
                center.Lock.EnterReadLock();

                if (center.IsDisposed)
                {
                    throw new ObjectDisposedException(center.GetType().FullName);
                }

                var reader    = center.Elements.GetReader(address);
                var nameIndex = reader.ReadUInt16() - 1;

                if (nameIndex == -1)
                {
                    throw new DataCenterPlaceholderException();
                }

                if (nameIndex >= center.Names.Count)
                {
                    throw new InvalidDataException();
                }

                Name = center.Names[nameIndex];

                reader.ReadUInt16();

                attrCount  = reader.ReadUInt16();
                childCount = reader.ReadUInt16();
                attrAddr   = DataCenter.ReadAddress(reader);
                childAddr  = DataCenter.ReadAddress(reader);
            }
            finally
            {
                center.Lock.ExitReadLock();
            }

            _attributes = new Lazy <IReadOnlyList <DataCenterAttribute> >(() =>
            {
                var attributes = new List <DataCenterAttribute>();

                try
                {
                    center.Lock.EnterReadLock();

                    if (center.IsDisposed)
                    {
                        throw new ObjectDisposedException(center.GetType().FullName);
                    }

                    for (var i = 0; i < attrCount; i++)
                    {
                        var addr = new DataCenterAddress(attrAddr.SegmentIndex,
                                                         (ushort)(attrAddr.ElementIndex + i));
                        var attrReader    = center.Attributes.GetReader(addr);
                        var attrNameIndex = attrReader.ReadUInt16() - 1;

                        if (attrNameIndex >= center.Names.Count)
                        {
                            throw new InvalidDataException();
                        }

                        var typeCode = (DataCenterTypeCode)attrReader.ReadUInt16();

                        if (typeCode != DataCenterTypeCode.Int32 &&
                            typeCode != DataCenterTypeCode.Single &&
                            typeCode != DataCenterTypeCode.Boolean)
                        {
                            typeCode = DataCenterTypeCode.String;
                        }

                        var primitiveValue = attrReader.ReadInt32();
                        string stringValue = null;

                        if (typeCode == DataCenterTypeCode.String)
                        {
                            attrReader.Position -= sizeof(int);

                            var strAddr = DataCenter.ReadAddress(attrReader);

                            stringValue = center.GetString(strAddr);
                        }

                        attributes.Add(new DataCenterAttribute(
                                           center.Names[attrNameIndex], typeCode,
                                           primitiveValue, stringValue));
                    }
                }
                finally
                {
                    center.Lock.ExitReadLock();
                }

                return(attributes);
            });

            _children = new Lazy <IReadOnlyList <DataCenterElement> >(() =>
            {
                var children = new List <DataCenterElement>();

                for (var i = 0; i < childCount; i++)
                {
                    var addr = new DataCenterAddress(childAddr.SegmentIndex,
                                                     (ushort)(childAddr.ElementIndex + i));

                    try
                    {
                        children.Add(new DataCenterElement(center, addr)
                        {
                            Parent = this,
                        });
                    }
                    catch (DataCenterPlaceholderException)
                    {
                    }
                }

                return(children);
            });
        }