From eb7f0140bfca8556f88d7c14a7457d04c62511c1 Mon Sep 17 00:00:00 2001 From: Vespura <31419184+TomGrobbe@users.noreply.github.com> Date: Mon, 28 May 2018 23:11:51 +0200 Subject: [PATCH] added loading/saving of json strings --- vMenu/StorageManager.cs | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/vMenu/StorageManager.cs b/vMenu/StorageManager.cs index de730e2e..54185f02 100644 --- a/vMenu/StorageManager.cs +++ b/vMenu/StorageManager.cs @@ -230,5 +230,55 @@ public CommonFunctions.VehicleInfo GetSavedVehicleInfo(string saveName) MainMenu.Cf.Log(json + "\n"); return vi; } + + /// + /// Save json data. Returns true if save was successfull. + /// + /// Name to store the data under. + /// The data to store. + /// If the saveName is already in use, can we override it? + /// Whether or not the data was saved successfully. + public bool SaveJsonData(string saveName, string jsonData, bool overrideExistingData) + { + if (!string.IsNullOrEmpty(saveName) && !string.IsNullOrEmpty(jsonData)) + { + string existingData = GetResourceKvpString(saveName); // check for existing data. + + if (!string.IsNullOrEmpty(existingData)) // data already exists for this save name. + { + if (!overrideExistingData) + { + return false; // data already exists, and we are not allowed to override it. + } + } + + // write data. + SetResourceKvp(saveName, jsonData); + + // return true if the data is successfully written, otherwise return false. + return (GetResourceKvpString(saveName) ?? "") == jsonData; + } + return false; // input parameters are invalid. + } + + /// + /// Returns the saved json data for the provided save name. Returns null if no data exists. + /// + /// + /// + public string GetJsonData(string saveName) + { + if (!string.IsNullOrEmpty(saveName)) + { + //Debug.WriteLine("not null"); + string data = GetResourceKvpString(saveName); + //Debug.Write(data + "\n"); + if (!string.IsNullOrEmpty(data)) + { + return data; + } + } + return null; + } } }