Page 1 of 1

Migrating save data question

Posted: Thu Mar 02, 2023 9:19 am
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!

Re: Migrating save data question

Posted: Thu Mar 02, 2023 10:27 am
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

Re: Migrating save data question

Posted: Thu Mar 02, 2023 11:11 am
by blackert
This works great thank you! Good to know using bytes is useful in these cases