|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class Manager {
|
|
|
|
|
|
|
|
|
|
[DllImport("__Internal")]
|
|
|
|
|
private static extern void SyncFiles();
|
|
|
|
|
|
|
|
|
|
[DllImport("__Internal")]
|
|
|
|
|
private static extern void WindowAlert(string message);
|
|
|
|
|
|
|
|
|
|
public static PlayerStatistics playerStatistics = Load();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void Save(PlayerStatistics playerStatistics) {
|
|
|
|
|
string dataPath = string.Format("{0}/PlayerStatistics.dat", Application.persistentDataPath);
|
|
|
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
|
|
|
|
FileStream fileStream;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (File.Exists(dataPath)) {
|
|
|
|
|
File.WriteAllText(dataPath, string.Empty);
|
|
|
|
|
fileStream = File.Open(dataPath, FileMode.Open);
|
|
|
|
|
} else {
|
|
|
|
|
fileStream = File.Create(dataPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
binaryFormatter.Serialize(fileStream, playerStatistics);
|
|
|
|
|
fileStream.Close();
|
|
|
|
|
|
|
|
|
|
if (Application.platform == RuntimePlatform.WebGLPlayer) {
|
|
|
|
|
SyncFiles();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
PlatformSafeMessage("Failed to Save: " + e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PlayerStatistics Load() {
|
|
|
|
|
PlayerStatistics playerStatistics = null;
|
|
|
|
|
string dataPath = string.Format("{0}/PlayerStatistics.dat", Application.persistentDataPath);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (File.Exists(dataPath)) {
|
|
|
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
|
|
|
|
FileStream fileStream = File.Open(dataPath, FileMode.Open);
|
|
|
|
|
|
|
|
|
|
playerStatistics = (PlayerStatistics)binaryFormatter.Deserialize(fileStream);
|
|
|
|
|
fileStream.Close();
|
|
|
|
|
} else {
|
|
|
|
|
playerStatistics = new PlayerStatistics();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
PlatformSafeMessage("Failed to Load: " + e.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return playerStatistics;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void PlatformSafeMessage(string message) {
|
|
|
|
|
if (Application.platform == RuntimePlatform.WebGLPlayer) {
|
|
|
|
|
WindowAlert(message);
|
|
|
|
|
} else {
|
|
|
|
|
Debug.Log(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|