Example #1
0
 public object DeserializeExactNonNullItem(MapItemInfo info)
 {
     _currentHeader = new BitSource()
     {
         Deserializer = this
     };
     _readHeader = false;
     return(DeserializeItemNoSetup(info, true));
 }
Example #2
0
        public object?DeserializeItem(MapItemInfo info, ref BitSource header)
        {
            // Do null checks
            if (!info.IsValueTypeItem && !header.ReadBit())
            {
                return(null);
            }

            _currentHeader = header;
            _readHeader    = true;
            return(DeserializeNullableItem(info, false));
        }
Example #3
0
        // Returns: Whether the sub-type was converted in here and we should return now.
        object DeserializeActualType(SaveInheritanceAttribute info, Type baseType)
        {
            Type?actualType = info.Mode switch
            {
                SaveInheritanceMode.Index => TryReadListInheritance(info, baseType),
                SaveInheritanceMode.Key => TryReadKeyInheritance(info, baseType),
                SaveInheritanceMode.IndexOrKey => _currentHeader.ReadBit() ? TryReadListInheritance(info, baseType) : TryReadKeyInheritance(info, baseType),
                _ => throw new Exception("Invalid save inheritance mode")
            };

            if (actualType == null)
            {
                throw new InvalidSubTypeInfoException(baseType);
            }

            // Deserialize the actual type.
            return(DeserializeItemNoSetup(GetRuntimeMapItem(actualType), true));
        }

        Type?TryReadListInheritance(SaveInheritanceAttribute info, Type baseType)
        {
            uint key = ReadCompressedInt(ref _currentHeader);

            return(info.IndexDeserializeCache.GetValueOrDefault(key));
        }

        Type?TryReadKeyInheritance(SaveInheritanceAttribute info, Type baseType)
        {
            // Make sure the info is initialized for deserialization.
            KeyInheritanceHandler.EnsureHasAllTypeCache(baseType, info);

            // Read in the key from the source.
            string key = ReadString(ref _currentHeader);

            // See if there's an item with that key.
            return(info.KeyDeserializeCache !.GetValueOrDefault(key));
        }

        void EnsureReadHeader()
        {
            if (!_readHeader)
            {
                _currentHeader = new BitSource(this, 8);
                _readHeader    = true;
            }
        }
    }
Example #4
0
        internal void HandleVersionNumber(Converter item, out VersionInfo info, ref BitSource header)
        {
            if (item._instanceId >= _currentVersionInfos.Length)
            {
                Array.Resize(ref _currentVersionInfos, (int)Map._highestConverterInstanceId);
            }

            // If the version has already been read, do nothing
            VersionInfo?existingInfo = _currentVersionInfos[item._instanceId];

            if (existingInfo != null)
            {
                info = existingInfo;
                return;
            }

            uint version = ReadNewVersionInfo(ref header);

            info = Map.GetVersionInfo(item, version);
            _currentVersionInfos[item._instanceId] = info;
        }
        public string ReadString(ref BitSource header)
        {
            if (Settings.UseUTF8)
            {
                int byteSize = (int)ReadCompressedInt(ref header);

                // Read the data
                Span <byte> buffer = byteSize <= ABSaveUtils.MAX_STACK_SIZE ? stackalloc byte[byteSize] : GetStringBuffer(byteSize);
                ReadBytes(buffer);

                // Encode
                return(Encoding.UTF8.GetString(buffer));
            }
            else
            {
                int size = (int)header.Deserializer.ReadCompressedInt(ref header);
                return(string.Create <object?>(size, null, (chars, state) =>
                {
                    FastReadShorts(MemoryMarshal.Cast <char, short>(chars));
                }));
            }
        }
Example #6
0
        public object?DeserializeItem(MapItemInfo info)
        {
            // Do null checks
            if (info.IsValueTypeItem)
            {
                _currentHeader = new BitSource()
                {
                    Deserializer = this
                };
                _readHeader = false;
            }
            else
            {
                _currentHeader = new BitSource(this);
                if (!_currentHeader.ReadBit())
                {
                    return(null);
                }

                _readHeader = true;
            }

            return(DeserializeNullableItem(info, false));
        }
Example #7
0
 uint ReadNewVersionInfo(ref BitSource header) => ReadCompressedInt(ref header);
Example #8
0
 public object DeserializeExactNonNullItem(MapItemInfo info, ref BitSource header)
 {
     _currentHeader = header;
     _readHeader    = true;
     return(DeserializeItemNoSetup(info, true));
 }
        public T ReadUTF8 <T>(Func <int, T> createDest, Func <T, Memory <char> > castDest, ref BitSource header)
        {
            int byteSize = (int)ReadCompressedInt(ref header);

            // Read the data
            Span <byte> buffer = byteSize <= ABSaveUtils.MAX_STACK_SIZE ? stackalloc byte[byteSize] : GetStringBuffer(byteSize);

            ReadBytes(buffer);

            // Allocate the destination with the correct size.
            int           charSize = Encoding.UTF8.GetCharCount(buffer);
            T             dest     = createDest(byteSize);
            Memory <char> destMem  = castDest(dest);

            // Encode
            Encoding.UTF8.GetChars(buffer, destMem.Span);
            return(dest);
        }
        public string ReadString()
        {
            var header = new BitSource(this);

            return(ReadString(ref header));
        }