public void Load(ISaveable saveable) { if (IsRegistered(saveable)) { string saveString = storage?.GetString(saveable.SaveKey, string.Empty); if (string.IsNullOrEmpty(saveString)) { saveable.LoadDefaults(); } else { try { object save = JsonConvert.DeserializeObject(saveString, saveable.SaveType); saveable.LoadSave(save); } catch (System.Exception exception) { UDebug.LogError(exception.Message); saveable.LoadDefaults(); } } } else { throw new UnityException($"Load: saveable of type => {saveable.GetType().Name} is not registered on ISaveService"); } }
public void Register(ISaveable saveable) { registeredSaveables[saveable.GetType()] = saveable; if (!saveable.IsLoaded) { Load(saveable); } }
public override string GetValue(IModifiable source) { ISaveable value = Getter(source); if (value == null) { return("null"); } else { return(value.GetType().Name + " " + value.GetSaveID()); } }
public static void UpdateAndSave <T>( this ISaveable instance, Expression <Func <T> > propertyExpression, T newValue) { // Gets the property name string propertyName = ((MemberExpression)propertyExpression.Body).Member.Name; // Updates its value PropertyInfo prop = instance.GetType().GetProperty(propertyName); prop.SetValue(instance, newValue, null); // Now call Save instance.Save(); }
public void Save(ISaveable saveable, bool flush = true) { if (IsRegistered(saveable)) { if (saveable.IsLoaded) { try { string saveString = JsonConvert.SerializeObject(saveable.GetSave()); storage?.SetString(saveable.SaveKey, saveString, flush); //UDebug.Log($"SAVED: {saveable.SaveKey} => {saveString}".Colored(ConsoleTextColor.magenta)); } catch (System.Exception exception) { Services.GetService <IConsoleService>().AddOutput($"exception when save {saveable.SaveType.Name}", ConsoleTextColor.red, true); Services.GetService <IConsoleService>().AddOutput(exception.Message, ConsoleTextColor.red, true); Services.GetService <IConsoleService>().AddOutput(exception.StackTrace, ConsoleTextColor.red, true); } } } else { throw new UnityException($"Save: saveable of type => {saveable.GetType().Name} is not registered on ISaveService"); } }
public static Metadata GetMetadata(this ISaveable saveable) { return(Metadata.LoadedClasses[saveable.GetType()]); }
public bool IsRegistered(ISaveable saveable) { return(registeredSaveables.ContainsKey(saveable.GetType())); }
private static void writeHeader(uint id, ISaveable saveable, ISaveable root, BinaryWriter writer) { writer.Write(id); writer.Write(saveable.GetType().AssemblyQualifiedName); writer.Write((saveable == root)); }