Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Threading;
- using Facebook.Unity;
- using Islands.Upgrades;
- using Logging.amplitude;
- using utils;
- using Ui;
- using UnityEditor;
- using UnityEngine;
- namespace User
- {
- public class UserFactory
- {
- public static UserFactory instance = null;
- static Mutex SaveWriteMutex = new Mutex();
- private User CurrentUser = null;
- private string SaveName = "/UserSave.gd";
- private string SaveNameBackup = "/UserSaveBackup.gd";
- public string StoragePath
- {
- get
- {
- var path = Application.persistentDataPath;
- return path;
- }
- }
- public static UserFactory GetInstance()
- {
- if (instance == null)
- {
- instance = new UserFactory();
- }
- return instance;
- }
- public User GetCurrentUser()
- {
- if (CurrentUser == null)
- {
- CurrentUser = Load();
- }
- return CurrentUser;
- }
- public static User User()
- {
- return GetInstance().GetCurrentUser();
- }
- public void Save(User UserForSave)
- {
- SaveWriteMutex.WaitOne();
- CurrentUser = UserForSave;
- try
- {
- var file = File.Create(this.StoragePath + SaveName);
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(file, UserForSave);
- file.Close();
- if (CheckSaveFile(this.StoragePath + SaveName))
- {
- ChangeBackUp();
- }
- }
- catch (Exception ex)
- {
- Debug.Log("Save File Error");
- var path = this.StoragePath + "/CrushReport" + DateTime.Now.TotalSeconds() +".txt";
- using (StreamWriter sw = File.CreateText(path))
- {
- sw.Write(ex.Message + "\n" + ex.StackTrace + "\n" + ex.ToString());
- sw.Flush();
- }
- }
- SaveWriteMutex.ReleaseMutex();
- }
- private void ChangeBackUp()
- {
- if (File.Exists(this.StoragePath + SaveNameBackup))
- {
- File.Copy(this.StoragePath +SaveName, this.StoragePath + SaveNameBackup, true);
- }
- else
- {
- File.Copy(this.StoragePath +SaveName, this.StoragePath + SaveNameBackup);
- }
- }
- private bool CheckSaveFile(string FilePath)
- {
- try
- {
- var file = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
- BinaryFormatter bf = new BinaryFormatter();
- var loadedUser = (User) bf.Deserialize(file);
- file.Close();
- }
- catch (Exception ex)
- {
- return false;
- }
- return true;
- }
- public void RemoveSave(string SavePath = null)
- {
- if (SavePath == null)
- {
- SavePath = SaveName;
- }
- File.Delete(this.StoragePath + SavePath);
- #if UNITY_EDITOR
- UnityEditor.AssetDatabase.Refresh();
- #endif
- CurrentUser = null;
- }
- private User Load()
- {
- User loadedUser = null;
- try
- {
- var file = File.Open(this.StoragePath + SaveName, FileMode.OpenOrCreate);
- BinaryFormatter bf = new BinaryFormatter();
- loadedUser = (User) bf.Deserialize(file);
- file.Close();
- }
- catch (Exception ex)
- {
- if (File.Exists(this.StoragePath + SaveNameBackup))
- {
- var file = File.Open(this.StoragePath + SaveNameBackup, FileMode.OpenOrCreate);
- BinaryFormatter bf = new BinaryFormatter();
- loadedUser = (User) bf.Deserialize(file);
- file.Close();
- }
- }
- Debug.Log("LOAD " + this.StoragePath + SaveName);
- if (loadedUser == null)
- {
- loadedUser = this.CreateUser();
- }
- else
- {
- loadedUser.IsStored = true;
- }
- return loadedUser;
- }
- protected User CreateUser()
- {
- var user = new User();
- user.TimestampToGiveSpins = TimeUtil.unixTimestamp;
- user.RegisterTime = TimeUtil.unixTimestamp;
- user.UpgradeVersionId = GameMono.Instance.UpgradeMilestones.VersionId;
- //Set defulat Values
- foreach (Resource id in Enum.GetValues(typeof(Resource)))
- {
- var resCfg = GameMono.Instance.GetItemById(id);
- if (resCfg!=null)
- {
- user.Res.SetResourceCount(resCfg.id, resCfg.DefaultValue);
- }
- }
- return user;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement