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); }
// 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); }