void LoadData() { if (!File.Exists(saveFilename)) { Debug.Log("File doesn't exist."); noSavedFile = true; } else if (new FileInfo(saveFilename).Length == 0 && new FileInfo(backupFilename).Length == 0) { Debug.Log("File length is zero."); noSavedFile = true; } else { Debug.Log("Loading File"); BinaryFormatter bf = new BinaryFormatter(); FileStream file; if (new FileInfo(backupFilename).Length < new FileInfo(saveFilename).Length) { file = File.Open(saveFilename, FileMode.Open); } else { file = File.Open(backupFilename, FileMode.Open); } MapMap data = (MapMap)bf.Deserialize(file); file.Close(); dict = data.dict; foreach (KeyValuePair <String, TileData> k in dict) { IncrementTiles(); string[] xy = k.Key.Split(','); AddTotalVisits(k.Value.visitCount); AddTotalSeconds(k.Value.secondCount); groundClone = Instantiate(groundPrefab, new Vector3(Int32.Parse(xy[0]), Int32.Parse(xy[1])), Quaternion.identity) as GameObject; } noSavedFile = false; saveFileSynced = true; SetHomeGPS(data.homeSquareLat, data.homeSquareLong); SetPlayerLevel(data.playerLevel); SetTotalPlayerPoints(data.playerPointCount); } }
void SaveData() { String saveFilename = Application.persistentDataPath + "/map.dat"; if (noSavedFile || saveFileSynced) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(saveFilename); FileStream backupFile = File.Create(backupFilename); MapMap data = new MapMap(); data.saved = "YES"; data.dict = dict; data.playerLevel = playerLevel; data.playerPointCount = playerPointCount; data.homeSquareLat = homeSquareLat; data.homeSquareLong = homeSquareLong; if (!noSavedFile && saveFileSynced) { bf.Serialize(backupFile, data); long backupLength = new FileInfo(backupFilename).Length; long saveLength = new FileInfo(saveFilename).Length; if (backupLength > saveLength) { System.IO.File.Copy(backupFilename, saveFilename, true); } } else if (saveFileSynced) { bf.Serialize(file, data); } file.Close(); backupFile.Close(); saveFileSynced = true; noSavedFile = false; } }