protected void SaveStatusJSON(string statusFileName, StatusSaver statusSaver) { using (StreamWriter stream = new StreamWriter(statusFileName)) { stream.Write(JsonUtility.ToJson(statusSaver)); } }
protected void SaveStatusBinary(string statusFileName, StatusSaver statusSaver) { using (FileStream fs = new FileStream(statusFileName, FileMode.Create)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, statusSaver); } }
protected StatusSaver LoadStatusBinary(string statusFileName) { StatusSaver statusSaver = null; using (FileStream fs = new FileStream(statusFileName, FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); statusSaver = (StatusSaver)bf.Deserialize(fs); } return(statusSaver); }
protected StatusSaver LoadStatusJSON(string statusFileName) { StatusSaver statusSaver = null; using (StreamReader stream = new StreamReader(statusFileName)) { string filedata = stream.ReadToEnd(); if (filedata != null) { statusSaver = JsonUtility.FromJson <StatusSaver>(filedata); } } return(statusSaver); }
// real save procedure protected void RealSave() { string statusFileName = StatusFileName; try { StatusSaver status = new StatusSaver { statusList = new List <StatusChunk>() }; foreach (KeyValuePair <int, List <GenericManager> > list in managers) { foreach (GenericManager manager in list.Value) { string signature = manager.SaveSignature(); if (!string.IsNullOrEmpty(signature)) { object prototype = manager.Save(); if (prototype != null) { status.statusList.Add(new StatusChunk { signature = signature, prototype = prototype }); } } } } if (status.statusList.Count > 0) { // choose either save procedure //Debug.Log("Saving managers status to " + statusFileName); SaveStatusBinary(statusFileName, status); //SaveStatusJSON(statusFileName, status); } } catch (System.Exception e) { Debug.Log("GlobalManager.Save failed: " + e.ToString()); //throw; } saveStatus = SaveStatus.Unchanged; }
void Load() { StatusSaver status = LoadStatus(); if (status != null) { foreach (StatusChunk chunk in status.statusList) { foreach (KeyValuePair <int, List <GenericManager> > list in managers) { foreach (GenericManager manager in list.Value) { if (manager.CheckSignature(chunk.signature)) { manager.Load(chunk.prototype); break; } } } } } }
protected StatusSaver LoadStatus() { string statusFileName = StatusFileName; StatusSaver statusSaver = null; try { if (File.Exists(statusFileName)) { //choose either loading procedure //Debug.Log("Loading managers status from " + statusFileName); statusSaver = LoadStatusBinary(statusFileName); //statusSaver = LoadStatusJSON(statusFileName); } } catch (System.Exception e) { Debug.Log("Exception: GlobalManager.Load failed: " + e.ToString()); //throw; } return(statusSaver); }