Exemplo n.º 1
0
        public void Dispose()
        {
            if (IsFrozen)
            {
                throw new InvalidOperationException("Data center is frozen.");
            }

            try
            {
                Lock.EnterWriteLock();

                IsDisposed = true;

                Root          = null;
                Attributes    = null;
                Elements      = null;
                Names         = null;
                _strings      = null;
                _stringRegion = null;
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
Exemplo n.º 2
0
        public unsafe DataCenter(string fileName, bool intern)
        {
            _intern = intern;

            using var reader = new GameBinaryReader(File.OpenRead(fileName));

            Header = ReadHeader(reader);

            ReadSimpleRegion(reader, false, Unknown1Size);

            var attributeRegion = ReadSegmentedRegion(reader, AttributeSize);
            var elementRegion   = ReadSegmentedRegion(reader, ElementSize);

            _stringRegion = ReadSegmentedRegion(reader, sizeof(char));

            ReadSimpleSegmentedRegion(reader, 1024, Unknown2Size);
            ReadSimpleRegion(reader, true, (uint)sizeof(DataCenterAddress));

            var nameRegion = ReadSegmentedRegion(reader, sizeof(char));

            ReadSimpleSegmentedRegion(reader, 512, Unknown2Size);

            var nameAddressRegion = ReadSimpleRegion(reader, true, (uint)sizeof(DataCenterAddress));

            Footer     = ReadFooter(reader);
            Attributes = attributeRegion;
            Elements   = elementRegion;
            Names      = ReadAddresses(nameAddressRegion).Select(x => ReadString(nameRegion, x)).ToArray();

            Reset();
        }
        public DataCenterStringTable(DataCenterSegmentedRegion data,
                                     DataCenterSegmentedSimpleRegion table, DataCenterSimpleRegion addresses,
                                     bool names, DataCenterStringOptions options)
        {
            var count = (int)addresses.Count;

            _intern  = options.HasFlag(DataCenterStringOptions.Intern);
            _strings = new ConcurrentDictionary <DataCenterAddress, string>(
                Environment.ProcessorCount, count);

            var lazy = options.HasFlag(DataCenterStringOptions.Lazy);

            // We only need the address list if we are eagerly loading strings
            // and thus verifying the integrity of the regions, or if we are
            // loading the names table which is always accessed by index. The
            // values table is never accessed by index.
            if (names || !lazy)
            {
                var addrs = new List <DataCenterAddress>(count);

                for (uint i = 0; i < addresses.Count; i++)
                {
                    var addr = DataCenter.ReadAddress(addresses.GetReader(i));

                    if (addr == DataCenterAddress.Invalid)
                    {
                        throw new InvalidDataException($"String address {addr} is invalid.");
                    }

                    addrs.Add(addr);
                }

                _addresses = addrs;
            }

            if (!lazy)
            {
                foreach (var(i, segment) in table.Segments.WithIndex())
                {
                    long last = -1;

                    for (uint j = 0; j < segment.Count; j++)
                    {
                        var reader = segment.GetReader(j);

                        // This hash only has a tiny amount of collisions in a
                        // typical data center.
                        var hash = reader.ReadUInt32();

                        if (hash < last)
                        {
                            throw new InvalidDataException(
                                      $"String hash {hash} is less than previous hash {last}.");
                        }

                        last = hash;

                        var bucket = (hash ^ hash >> 16) % table.Segments.Count;

                        if (bucket != i)
                        {
                            throw new InvalidDataException(
                                      $"String bucket index {i} does not match expected index {bucket}.");
                        }

                        // This includes the NUL character.
                        var length = reader.ReadUInt32() - 1;

                        var index = reader.ReadUInt32() - 1;

                        if (index >= _addresses.Count)
                        {
                            throw new InvalidDataException(
                                      $"String index {index} is greater than {_addresses.Count}.");
                        }

                        var address1 = DataCenter.ReadAddress(reader);
                        var address2 = _addresses[(int)index];

                        if (address1 != address2)
                        {
                            throw new InvalidDataException(
                                      $"String address {address1} does not match expected address {address2}.");
                        }

                        var value = data.GetReader(address1).ReadString();

                        if (value.Length != length)
                        {
                            throw new InvalidDataException(
                                      $"String length {value.Length} does not match recorded length {length}.");
                        }

                        if (!_strings.TryAdd(address1, _intern ? string.Intern(value) : value))
                        {
                            throw new InvalidDataException(
                                      $"String address {address1} already added to the table.");
                        }
                    }
                }
            }
            else
            {
                _data = data;
            }
        }