예제 #1
0
 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");
     }
 }
예제 #2
0
 public void Register(ISaveable saveable)
 {
     registeredSaveables[saveable.GetType()] = saveable;
     if (!saveable.IsLoaded)
     {
         Load(saveable);
     }
 }
예제 #3
0
        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();
    }
예제 #5
0
 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");
     }
 }
예제 #6
0
 public static Metadata GetMetadata(this ISaveable saveable)
 {
     return(Metadata.LoadedClasses[saveable.GetType()]);
 }
예제 #7
0
 public bool IsRegistered(ISaveable saveable)
 {
     return(registeredSaveables.ContainsKey(saveable.GetType()));
 }
예제 #8
0
 private static void writeHeader(uint id, ISaveable saveable, ISaveable root, BinaryWriter writer)
 {
     writer.Write(id);
     writer.Write(saveable.GetType().AssemblyQualifiedName);
     writer.Write((saveable == root));
 }