public static PartWrapper Load <TPart, TSerial>(string file, ISaveDataPartFactory <TPart, TSerial> factory, double version) where TPart : ISaveDataPart <TSerial> { if (Exists(file, factory.GetId())) { throw new IOException("File not found"); } var f = file + "/" + factory.GetId() + ".part"; using (var reader = new StreamReader(f, Encoding.UTF8)) { var content = reader.ReadToEnd(); var o = JsonUtility.FromJson(content, factory.GetSerialType()); var saveDataPart = factory.Deserialize((TSerial)o, version); return(new PartWrapper((ISaveDataPart <object>)saveDataPart, f)); } }
/// <summary> /// Remove part from the save and delete all files linked to it /// </summary> /// <param name="factory">part factory</param> /// <typeparam name="TPart">part type</typeparam> /// <typeparam name="TSerial">part serializable object type</typeparam> /// <exception cref="IOException"></exception> public void Remove <TPart, TSerial>(ISaveDataPartFactory <TPart, TSerial> factory) where TPart : ISaveDataPart <TSerial> { var id = factory.GetId(); _partList.Remove(id); _versionMap.Remove(id); if (_parts.ContainsKey(id)) { _parts.Remove(id); } PartWrapper.Delete(_file, id); }
/// <summary> /// Check if this save has the part /// </summary> /// <param name="factory">part factory</param> /// <typeparam name="TPart">part type</typeparam> /// <typeparam name="TSerial">part serializable object type</typeparam> /// <returns><code>true</code> - save contains the part,otherwise - <code>false</code></returns> public bool Contains <TPart, TSerial>(ISaveDataPartFactory <TPart, TSerial> factory) where TPart : ISaveDataPart <TSerial> { return(_partList.Contains(factory.GetId()) && _versionMap.GetVersion(factory.GetId()) != null && PartWrapper.Exists(_file, factory.GetId())); }
/// <summary> /// Get/create part from save /// </summary> /// <param name="factory">part factory</param> /// <typeparam name="TPart">part type</typeparam> /// <typeparam name="TSerial">part serializable object type</typeparam> /// <returns>existing/new part</returns> /// <exception cref="IOException"></exception> public TPart GetPart <TPart, TSerial>(ISaveDataPartFactory <TPart, TSerial> factory) where TPart : ISaveDataPart <TSerial> { if (_parts.ContainsKey(factory.GetId())) { return(_parts[factory.GetId()].Get <TPart>()); } if (_partList.Contains(factory.GetId()) && PartWrapper.Exists(_file, factory.GetId())) { var version = _versionMap.GetVersion(factory.GetId()); if (version != null) { var wrapper = PartWrapper.Load(_file, factory, (double)version); if (!Equals(version, factory.GetVersion())) { wrapper.Save(); _versionMap.SetVersion(factory.GetId(), factory.GetVersion()); } _parts.Add(factory.GetId(), wrapper); return(wrapper.Get <TPart>()); } } var w = new PartWrapper((ISaveDataPart <object>)factory.Create(), _file, factory.GetId()); w.Save(); _partList.Add(factory.GetId()); _versionMap.SetVersion(factory.GetId(), factory.GetVersion()); _parts.Add(factory.GetId(), w); return(w.Get <TPart>()); }