public void EnsureHasAllTypeCache_Existing()
        {
            SaveInheritanceAttribute attribute = typeof(KeyBase).GetCustomAttribute <SaveInheritanceAttribute>();

            // Simulate it having already been done.
            attribute.HasGeneratedFullKeyCache = true;

            KeyInheritanceHandler.EnsureHasAllTypeCache(typeof(KeyBase), attribute);

            // Since nothing has actually been done these will just be null.
            Assert.IsNull(attribute.KeySerializeCache);
            Assert.IsNull(attribute.KeyDeserializeCache);
        }
        public void GetOrAddTypeKeyFromCache_New()
        {
            SaveInheritanceAttribute attribute = typeof(KeyBase).GetCustomAttribute <SaveInheritanceAttribute>();

            string key = KeyInheritanceHandler.GetOrAddTypeKeyFromCache(typeof(KeyBase), typeof(KeySubFirst), attribute);

            Assert.AreEqual("First", key);

            Assert.AreEqual(1, attribute.KeySerializeCache.Count);
            Assert.AreEqual(1, attribute.KeyDeserializeCache.Count);

            Assert.AreEqual("First", attribute.KeySerializeCache[typeof(KeySubFirst)]);
            Assert.AreEqual(typeof(KeySubFirst), attribute.KeyDeserializeCache["First"]);
        }
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;
            }
        }
    }
        public void EnsureHasAllTypeCache_New_CrossAssembly()
        {
            SaveInheritanceAttribute attribute = typeof(KeyBase).GetCustomAttribute <SaveInheritanceAttribute>();

            KeyInheritanceHandler.EnsureHasAllTypeCache(typeof(OtherAssemblyBase), attribute);

            Assert.AreEqual(2, attribute.KeySerializeCache.Count);
            Assert.AreEqual(2, attribute.KeyDeserializeCache.Count);

            Assert.AreEqual("First", attribute.KeySerializeCache[typeof(OtherAssemblySub)]);
            Assert.AreEqual(typeof(OtherAssemblySub), attribute.KeyDeserializeCache["First"]);

            Assert.AreEqual("Second", attribute.KeySerializeCache[typeof(CrossAssemblySub)]);
            Assert.AreEqual(typeof(CrossAssemblySub), attribute.KeyDeserializeCache["Second"]);

            Assert.IsTrue(attribute.HasGeneratedFullKeyCache);
        }
        public void GetOrAddTypeKeyFromCache_Exists()
        {
            SaveInheritanceAttribute attribute = typeof(KeyBase).GetCustomAttribute <SaveInheritanceAttribute>();

            // Fill the cache up.
            var oldSerializeCache   = attribute.KeySerializeCache = new Dictionary <Type, string>();
            var oldDeserializeCache = attribute.KeyDeserializeCache = new Dictionary <string, Type>();

            oldSerializeCache.Add(typeof(KeySubFirst), "First");
            oldDeserializeCache.Add("First", typeof(KeySubFirst));

            string key = KeyInheritanceHandler.GetOrAddTypeKeyFromCache(typeof(KeyBase), typeof(KeySubFirst), attribute);

            Assert.AreEqual("First", key);

            // Ensure it didn't add to the cache.
            Assert.AreEqual(oldSerializeCache, attribute.KeySerializeCache);
            Assert.AreEqual(oldDeserializeCache, attribute.KeyDeserializeCache);

            Assert.AreEqual(1, attribute.KeySerializeCache.Count);
            Assert.AreEqual(1, attribute.KeyDeserializeCache.Count);
        }
Example #6
0
        void WriteKeyInheritance(SaveInheritanceAttribute info, Type baseType, Type actualType, ref BitTarget header)
        {
            string key = KeyInheritanceHandler.GetOrAddTypeKeyFromCache(baseType, actualType, info);

            WriteString(key, ref header);
        }