Skip to content

Instantly share code, notes, and snippets.

@daemon3000
Last active August 29, 2015 14:24
Show Gist options
  • Save daemon3000/6653b8b090066bb15096 to your computer and use it in GitHub Desktop.
Save daemon3000/6653b8b090066bb15096 to your computer and use it in GitHub Desktop.
ProfilePrefs
#region [Copyright (c) 2015 Cristian Alexandru Geambasu]
// Distributed under the terms of an MIT-style license:
//
// The MIT License
//
// Copyright (c) 2015 Cristian Alexandru Geambasu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion
using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using TeamUtility.IO;
namespace TeamUtility
{
public static class ProfilePrefs
{
private static Dictionary<string, object> m_data;
private static void SetObject(string key, object value)
{
if(m_data != null)
{
if(m_data.ContainsKey(key))
m_data[key] = value;
else
m_data.Add(key, value);
}
}
public static void SetString(string key, string value)
{
SetObject(key, value);
}
public static string GetString(string key, string defaultValue)
{
if(m_data != null)
{
object value = null;
if(m_data.TryGetValue(key, out value) && value is string)
return (string)value;
return defaultValue;
}
else
{
return defaultValue;
}
}
public static void SetInt(string key, int value)
{
SetObject(key, value);
}
public static int GetInt(string key, int defaultValue)
{
if(m_data != null)
{
object value = null;
if(m_data.TryGetValue(key, out value) && value is int)
return (int)value;
return defaultValue;
}
else
{
return defaultValue;
}
}
public static void SetFloat(string key, float value)
{
SetObject(key, value);
}
public static float GetFloat(string key, float defaultValue)
{
if(m_data != null)
{
object value = null;
if(m_data.TryGetValue(key, out value) && value is float)
return (float)value;
return defaultValue;
}
else
{
return defaultValue;
}
}
public static void SetBool(string key, bool value)
{
SetObject(key, value);
}
public static bool GetBool(string key, bool defaultValue)
{
if(m_data != null)
{
object value = null;
if(m_data.TryGetValue(key, out value) && value is bool)
return (bool)value;
return defaultValue;
}
else
{
return defaultValue;
}
}
public static bool HasKey(string key)
{
if(m_data != null)
return m_data.ContainsKey(key);
else
return false;
}
public static void DeleteAllKeys()
{
m_data.Clear();
}
public static void Save()
{
try
{
string profileFolder = GetProfilePath();
string profilePath = GetProfilePrefsPath();
if(!Directory.Exists(profileFolder))
Directory.CreateDirectory(profileFolder);
TeamUtility.IO.BinaryFormatter bf = new TeamUtility.IO.BinaryFormatter();
bf.Serialize(m_data, profilePath);
}
catch(System.Exception ex)
{
Debug.LogException(ex);
Debug.LogError("There has been an error while saving the profile prefs");
}
}
public static void Load()
{
string profilePath = GetProfilePrefsPath();
if(File.Exists(profilePath))
{
try
{
TeamUtility.IO.BinaryFormatter bf = new TeamUtility.IO.BinaryFormatter();
m_data = (Dictionary<string, object>)bf.Deserialize(profilePath);
if(m_data == null)
LoadDefaults();
}
catch(System.Exception ex)
{
Debug.LogException(ex);
Debug.LogError("There has been an error while loading the existing profile prefs");
LoadDefaults();
}
}
else
{
Debug.Log("Unable to find existing profile prefs");
LoadDefaults();
}
}
private static void LoadDefaults()
{
if(m_data == null)
m_data = new Dictionary<string, object>();
else
m_data.Clear();
}
private static string GetProfilePath()
{
return Application.persistentDataPath;
}
public static string GetProfilePrefsPath()
{
return Application.persistentDataPath + "/profile.dat";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment