Migrating save data question

Discussion and help for Easy Save 3
Post Reply
blackert
Posts: 2
Joined: Thu Mar 02, 2023 9:16 am

Migrating save data question

Post by blackert »

Hi,

I have a save data that is not encrypted and does not use compression. I'd like to change this and store it compressed instead while maintaining the save file compatibility.

So, what I have tried is caching the old file and then trying to save it in a compressed state so that I can use CopyFile etc. but this didn't work. Also tried renaming, caching and then storing the compressed version but same result. Is there a way to do this? Thanks!
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Migrating save data question

Post by Joel »

Hi there,

Something along the lines of this would check whether a file is uncompressed and resave it with compression enabled:

Code: Select all

using UnityEngine;

public class CompressIfUncompressed : MonoBehaviour
{
    void Start()
    {
        var uncompressedSettings = new ES3Settings(ES3.CompressionType.None);
        var compressedSettings = new ES3Settings(ES3.CompressionType.Gzip);

        // Create a new uncompressed file for testing purposes.
        ES3.DeleteFile();
        ES3.Save("myInt", 123, uncompressedSettings);

        try
        {
            // Try to load from the file with compression enabled.
            ES3.LoadRawBytes(compressedSettings);
        }
        // If it fails with an IOException, the data is likely incompressed, so compress it.
        catch (System.IO.IOException)
        {
            var bytes = ES3.LoadRawBytes(uncompressedSettings);
            ES3.SaveRaw(bytes, compressedSettings);
        }

        // Loads correctly.
        var myInt = ES3.Load<int>("myInt", compressedSettings);
    }
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
blackert
Posts: 2
Joined: Thu Mar 02, 2023 9:16 am

Re: Migrating save data question

Post by blackert »

This works great thank you! Good to know using bytes is useful in these cases
Post Reply