Ejemplo n.º 1
0
    public Task <GameMetadata> Load(string GUID)
    {
        Debug.Log("Reading metadata file... " + GUID);
        StreamReader metadataReader = File.OpenText(Application.persistentDataPath
                                                    + $"/Saves/{GUID}/metadata.json");
        string jsonMetadata = metadataReader.ReadToEnd();

        Debug.Log("Read JSON file");
        metadataReader.Close();
        Debug.Log(jsonMetadata);
        GameMetadata gameMetadata = JsonConvert.DeserializeObject <GameMetadata>(jsonMetadata);

        Debug.Log("JSON Deserialized");
        Debug.Log("Metadata read and loaded as game metadata");

        Debug.Log("Copying game metadata");
        this.GUID = gameMetadata.GUID;
        SaveName  = gameMetadata.SaveName;
        Days      = gameMetadata.Days;
        Weather   = gameMetadata.Weather;
        LastVisit = gameMetadata.LastVisit;
        Debug.Log("Game metadata copied!");

        return(Task.FromResult(this));
    }
Ejemplo n.º 2
0
 public GameMetadata(string guid, int saveVersion, string saveName, int days, GameFlow.Weather weather, DateTime lastVisit)
 {
     GUID        = guid;
     SaveVersion = saveVersion != default ? saveVersion : 1;
     SaveName    = saveName;
     Days        = days;
     Weather     = weather;
     LastVisit   = lastVisit;
 }
Ejemplo n.º 3
0
    public async Task <GameData> Load(string GUID, bool isConversionLoad = false)
    {
        try
        {
            Debug.Log("Deserializing data... " + GUID);
            if (!File.Exists(Application.persistentDataPath
                             + $"/Saves/{GUID}/save.dat"))
            {
                Debug.LogWarning("Save file does not exist or no save file specified");
                throw new Exception("Save file does not exist or no save file specified");
            }
            // Deserialize game file
            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file =
                File.OpenRead(Application.persistentDataPath
                              + $"/Saves/{GUID}/save.dat");
            GameData deserializedData = (GameData)bf.Deserialize(file);
            file.Close();
            Debug.Log("Game data deserialized!");

            Debug.Log("Checking for game save format updates...");
            if (!isConversionLoad && deserializedData.GameMetadata.SaveVersion != new GameMetadata().SaveVersion)
            {
                await Convert(GUID, deserializedData.GameMetadata.SaveVersion);

                Debug.Log("Old format detected! Converting...");
            }
            else
            {
                Debug.Log("No conversions needed");
            }

            Debug.Log("Copying object data...");
            GameMetadata    = deserializedData.GameMetadata;
            PlayerGender    = deserializedData.PlayerGender;
            Days            = deserializedData.Days;
            InDebt          = deserializedData.InDebt;
            Money           = deserializedData.Money;
            BorrowedMoney   = deserializedData.BorrowedMoney;
            BorrowLimit     = deserializedData.BorrowLimit;
            Weather         = deserializedData.Weather;
            StorylinesSeen  = deserializedData.StorylinesSeen;
            ShopItemLevels  = deserializedData.ShopItemLevels;
            FlowerBedStates = deserializedData.FlowerBedStates;
            Debug.Log("Object data copied!");

            return(this);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            Debug.LogError("Failed to deserialize save!");

            Debug.Log("Saving loaded data and creating backup copy");
            Save(true, "load_failure_backup").Wait(); // this will create a backup of the old corrupted copy and by saving, we create a fresh save that will likely work

            return(new GameData()
            {
                GameMetadata = new GameMetadata()
                {
                    GUID = Guid.NewGuid().ToString(),
                    SaveName = "Corrupted Or Missing Save"
                }
            });
        }
    }