2

I have a game where I use a couple of tables to store player's progress. It is quite long game so from time to time I'm having this legitimate user feedback about why I don't have cloud save option?

Therefore I do use Google Play Games sign in to have achievements, I thought I could simply implement Google Saved Games as well, straightforward.

As I see, it uses a simple byte[] array to save & load game progression from the cloud. (Google Drive, actually.)

My question is, how can I create a byte[] array which holds the FULL database of a player's saved game instance?

I'm using Sqlite Database like:

private static DatabaseHelper mInstance = null;


private DatabaseHelper(Context context) {
    super(context, "duels_rpg_db", null, 123);
  

    this.ctx = context;

 
    getWritableDatabase();
}


public static DatabaseHelper getInstance(Context ctx) {

   
    if (mInstance == null) {
        mInstance = new DatabaseHelper(ctx.getApplicationContext());
    }
    return mInstance;
}

and so on, nothing extraordinary.

Thanks in advance,

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

2 Answers2

2

If you were using C it would be easy to serialize the entire database, but in Android I think you would have to locate the database file, convert it to a byte array using the ByteArrayOutputStream class, store it in Google Saved Games and later on convert the byte array back to a file if necessary. For an example of converting a file to a byte array and vice-versa see this link.

Dan M
  • 4,340
  • 8
  • 20
1

It would be a good idea to serialize your user data instead of the db file instead. This way if you update your app, you wont app to worry about using a newer version of the DB in a not-yet-updated version of the app.

So I would recommand convert your progress to and from org.json.JSONObject. With the JSON, it's easy to convert to an byteArray with json.toString().getBytes("utf-8")

On fetching the data, you can convert it back to json and wirrrte a method that load your sqlite db with the data of the json

Florian Burel
  • 3,408
  • 1
  • 19
  • 20
  • Good idea but that would be only okay if the database would be small, and I would know that it would never will be bigger. But right now I have 20+ tables. Writing a serialization operation is out of the question, sadly. The safe and code less version would be to simply grab the whole database and override the old one. I will have to handle version checking with both methods anyway. – Adam Varhegyi Dec 04 '20 at 14:27
  • 1
    I can relate! :) Then this link my provide insight on how to customize the location of your db file and access / overwrite it. https://riptutorial.com/fr/android-sqlite/example/16326/sqliteopenhelper-avec-nom-de-chemin-d-acces---base-de-donnees-de-base-de-donnees-qualifie-complet-dans-le-dossier-public – Florian Burel Dec 04 '20 at 14:48
  • It is not a real answer to the question but I ended up using this solution. Sqlite wrappers are a pain in the a** – Adam Varhegyi Dec 07 '20 at 14:01