public void LoadExportData() { /* * generates a save string as export data * and places it in the export field */ if (exportField == null) { Log.Warn("No 'export field' defined.", this); return; } if (exportSettings == null) { Log.Warn("No 'export settings button'.", this); return; } Log.Print("Exporting save data.", this); string save_data = ""; if (exportSettings.isOn == true) { Save save = new Save(); save.music_volume = PlayerPrefs.GetFloat("music_volume").ToString(); save.sound_volume = PlayerPrefs.GetFloat("sound_volume").ToString(); save.speech_volume = PlayerPrefs.GetFloat("speech_volume").ToString(); save.lvls_unlocked = PlayerPrefs.GetInt("lvls_unlocked").ToString(); save.lvl1_timer = PlayerPrefs.GetString("lvl1_timer"); save.lvl2_timer = PlayerPrefs.GetString("lvl2_timer"); save.lvl3_timer = PlayerPrefs.GetString("lvl3_timer"); save.lvl4_timer = PlayerPrefs.GetString("lvl4_timer"); save.lvl5_timer = PlayerPrefs.GetString("lvl5_timer"); save.lvl6_timer = PlayerPrefs.GetString("lvl6_timer"); save.lvl7_timer = PlayerPrefs.GetString("lvl7_timer"); save.lvl8_timer = PlayerPrefs.GetString("lvl8_timer"); save.lvl9_timer = PlayerPrefs.GetString("lvl9_timer"); save.lvl10_timer = PlayerPrefs.GetString("lvl10_timer"); save_data = JsonUtility.ToJson(save); } else { SaveNoSettings save = new SaveNoSettings(); save.lvls_unlocked = PlayerPrefs.GetInt("lvls_unlocked").ToString(); save.lvl1_timer = PlayerPrefs.GetString("lvl1_timer"); save.lvl2_timer = PlayerPrefs.GetString("lvl2_timer"); save.lvl3_timer = PlayerPrefs.GetString("lvl3_timer"); save.lvl4_timer = PlayerPrefs.GetString("lvl4_timer"); save.lvl5_timer = PlayerPrefs.GetString("lvl5_timer"); save.lvl6_timer = PlayerPrefs.GetString("lvl6_timer"); save.lvl7_timer = PlayerPrefs.GetString("lvl7_timer"); save.lvl8_timer = PlayerPrefs.GetString("lvl8_timer"); save.lvl9_timer = PlayerPrefs.GetString("lvl9_timer"); save.lvl10_timer = PlayerPrefs.GetString("lvl10_timer"); save_data = JsonUtility.ToJson(save); } // encode with base64 byte[] bytesToEncode = Encoding.UTF8.GetBytes(save_data); save_data = System.Convert.ToBase64String(bytesToEncode); // encrypt the save data save_data = Cipher.Encrypt(save_data, encoding_pass_phrase); exportField.text = save_data; }
public void ImportSave() { /* * imports a save text from the 'import save' text field * and loads the settings/progress data into player prefs */ string save_data = importField.text; try { // decrypt string save_data = Cipher.Decrypt(save_data, encoding_pass_phrase); // decode base64 byte[] decodedBytes = System.Convert.FromBase64String(save_data); save_data = Encoding.UTF8.GetString(decodedBytes); } catch (System.Exception e) { Log.Warn($"Could not import save data: {e}", this); // load error message errorMessage.gameObject.SetActive(true); errorMessage.alpha = 1; errorMessage.interactable = true; return; } // test if settings had been exported as well bool exported_settings = Regex.IsMatch(save_data, "music_volume"); Log.Print($"Importing save data {(exported_settings ? " with export settings" : "")}.", this); // convert JSON string back to object if (exported_settings) { Save save = new Save(); save = JsonUtility.FromJson <Save>(save_data); PlayerPrefs.SetFloat("music_volume", float.Parse(save.music_volume)); PlayerPrefs.SetFloat("sound_volume", float.Parse(save.sound_volume)); PlayerPrefs.SetFloat("speech_volume", float.Parse(save.speech_volume)); saveInPlayerPrefs(save.lvls_unlocked, new string[10] { save.lvl1_timer, save.lvl2_timer, save.lvl3_timer, save.lvl4_timer, save.lvl5_timer, save.lvl6_timer, save.lvl7_timer, save.lvl8_timer, save.lvl9_timer, save.lvl10_timer }); VolumeController.Instance.loadPlayerPrefs(); } else { SaveNoSettings save = new SaveNoSettings(); save = JsonUtility.FromJson <SaveNoSettings>(save_data); saveInPlayerPrefs(save.lvls_unlocked, new string[10] { save.lvl1_timer, save.lvl2_timer, save.lvl3_timer, save.lvl4_timer, save.lvl5_timer, save.lvl6_timer, save.lvl7_timer, save.lvl8_timer, save.lvl9_timer, save.lvl10_timer }); } void saveInPlayerPrefs(string lvls_unlocked, string[] timers) { /* * saves the given progress settings in player prefs */ // add amount of levels that were unlocked PlayerPrefs.SetInt("lvls_unlocked", System.Int32.Parse(lvls_unlocked)); // go through all timer values and add them to player prefs for (int i = 0; i < 10; i++) { // test if timer has proper format if (timerIsInRightFormat(timers[i])) { PlayerPrefs.SetString("lvl" + (i + 1) + "_timer", timers[i]); } else { // if format is wrong, set default timer PlayerPrefs.SetString("lvl" + (i + 1) + "_timer", def_timer); } } } LevelManager.Instance.LoadLevelProgess(); // reset 'import save' input field importField.text = ""; // load 'successful import' message successMessage.gameObject.SetActive(true); successMessage.alpha = 1; successMessage.interactable = true; }