using System;
using System.Collections.Generic;
namespace Ubisoft
{
///
/// Abstract class responsible for offering some common implementation to share among all classes that implement IHtPlayerPrefs.
///
/// This class expands Unity 'PlayerPrefs':
/// 1) It support channels. A channel is a collection of key-value pairs that can be handled separately.
///
public abstract class HtPlayerPrefs : IHtPlayerPrefs
{
protected const char STORED_KEYS_KEY_SEPARATOR = ',';
protected const char STORED_KEYS_TOKEN_SEPARATOR = ':';
private static readonly List TYPE_KEYS = new List(Enum.GetNames(typeof(IHtPlayerPrefs.EType)));
protected static IHtPlayerPrefs.EType KeyToEType(string value)
{
int index = TYPE_KEYS.IndexOf(value);
return index == -1 ? IHtPlayerPrefs.EType.None : (IHtPlayerPrefs.EType)index;
}
protected static string ETypeToKey(IHtPlayerPrefs.EType value)
{
return TYPE_KEYS[(int)value];
}
public string Channel { get; }
protected HtLogger.Channel Logger { get; }
protected bool IsVerbose { get; }
public bool AutomaticSave { get; set; }
public virtual bool NeedsToSave { get; set; }
///
/// Create an HtPlayerPrefs object to store data in the channel passed in.
///
/// Name of the channel to use to store data saved through this object.
/// An HtLogger.Channel object to use to log data to.
/// If no object is passed in then a default one called 'HT_PlayerPrefs('channel')' is created.
/// When true logs are printed out. Use it only for debug purposes.
/// Make sure that Trace is the minimum log level set in your Logger configuration.
protected HtPlayerPrefs(string channel, HtLogger.Channel logger = null, bool isVerbose = false)
{
AutomaticSave = true;
NeedsToSave = false;
Channel = channel;
if (logger == null)
{
logger = HtLogger.Instance.CreateHotelChannel($"PlayerPrefs({channel})");
}
Logger = logger;
IsVerbose = isVerbose;
}
public void DeleteAll()
{
Logger.LogInformation($"DeleteAll");
ExtendedDeleteAll();
}
public void DeleteKey(string key)
{
Logger.LogInformation($"DeleteKey {key}");
ExtendedDeleteKey(key);
}
public abstract bool GetBool(string key, bool defaultValue = false);
public abstract float GetFloat(string key, float defaultValue = 0f);
public abstract int GetInt(string key, int defaultValue = 0);
public abstract long GetLong(string key, long defaultValue = 0);
public abstract string GetString(string key, string defaultValue = "");
public object GetAsObject(string key)
{
IHtPlayerPrefs.EType type = GetType(key);
switch (type)
{
case IHtPlayerPrefs.EType.Bool:
return GetBool(key);
case IHtPlayerPrefs.EType.Float:
return GetFloat(key);
case IHtPlayerPrefs.EType.Int:
return GetInt(key);
case IHtPlayerPrefs.EType.Long:
return GetLong(key);
case IHtPlayerPrefs.EType.String:
return GetString(key);
default:
return null;
}
}
public abstract bool HasKey(string key);
public abstract IHtPlayerPrefs.EType GetType(string key);
public void Save()
{
if (IsVerbose)
{
Logger.LogTrace($"Save NeedsToSave: {NeedsToSave}");
}
if (NeedsToSave)
{
NeedsToSave = false;
ExtendedSave();
}
}
public virtual void Update() { }
public void SetBool(string key, bool value)
{
if (IsVerbose)
{
Logger.LogTrace($"SetBool: {key}:{value}");
}
ExtendedSetBool(key, value);
}
public void SetFloat(string key, float value)
{
if (IsVerbose)
{
Logger.LogTrace($"SetFloat: {key}:{value}");
}
ExtendedSetFloat(key, value);
}
public void SetInt(string key, int value)
{
if (IsVerbose)
{
Logger.LogTrace($"SetInt: {key}:{value}");
}
ExtendedSetInt(key, value);
}
public void SetLong(string key, long value)
{
if (IsVerbose)
{
Logger.LogTrace($"SetInt: {key}:{value}");
}
ExtendedSetLong(key, value);
}
public void SetString(string key, string value)
{
if (IsVerbose)
{
Logger.LogTrace($"SetString: {key}:{value}");
}
ExtendedSetString(key, value);
}
public abstract override string ToString();
protected abstract void ExtendedDeleteAll();
protected abstract void ExtendedDeleteKey(string key);
protected abstract void ExtendedSave();
protected abstract void ExtendedSetBool(string key, bool value);
protected abstract void ExtendedSetFloat(string key, float value);
protected abstract void ExtendedSetInt(string key, int value);
protected abstract void ExtendedSetLong(string key, long value);
protected abstract void ExtendedSetString(string key, string value);
protected string AddRawStoredKey(string key, IHtPlayerPrefs.EType type, string rawStoredKeys)
{
if (!string.IsNullOrEmpty(rawStoredKeys))
{
rawStoredKeys += $"{STORED_KEYS_KEY_SEPARATOR}";
}
rawStoredKeys = $"{rawStoredKeys}{key}{STORED_KEYS_TOKEN_SEPARATOR}{ETypeToKey(type)}";
return rawStoredKeys;
}
}
}