using System.Collections.Generic;
namespace Ubisoft
{
///
/// Factory that is able to instantiate the implementations of IHtPlayePrefs supported out of the box.
/// Feel free to extend this class to customise it.
///
public class HtPlayerPrefsFactory
{
public HtLogger.Channel Logger { get; }
protected Dictionary Catalog { get; }
public HtPlayerPrefsFactory(HtLogger.Channel logger = null)
{
if (logger == null)
{
logger = HtLogger.Instance.CreateHotelChannel("PlayerPrefs");
}
Logger = logger;
Catalog = new Dictionary();
}
public void AddConfig(HtPlayerPrefsConfig config)
{
if (config == null)
{
Logger.LogError("Config needs to be a non-null object.");
}
else
{
string channel = config.Channel;
HtPlayerPrefsConfig formerValue = GetConfig(channel);
if (formerValue == null)
{
Catalog.Add(channel, config);
}
else
{
Logger.LogError($"A config for channel {Debug.FormatTextInUserContext(channel)} already exists.");
}
}
}
public HtPlayerPrefsConfig GetConfig(string channel)
{
_ = Catalog.TryGetValue(channel, out HtPlayerPrefsConfig returnValue);
return returnValue;
}
///
/// Create an object that conforms to IHtPlayerPrefs by using the implementation stated by implementationId.
///
/// Id of the implementation of IHtPlayerPrefsto use to instantiate.
/// Channel to store preferences to.
/// Object instatiated.
internal IHtPlayerPrefs Create(string channel)
{
IHtPlayerPrefs returnValue = null;
HtPlayerPrefsConfig config = GetConfig(channel);
if (config == null)
{
Logger.LogError($"No config defined for channel {Debug.FormatTextInUserContext(channel)}.");
}
else
{
returnValue = ExtendedCreate(config);
if (returnValue == null)
{
Logger.LogError($"No implementation id supported: {Debug.FormatTextInUserContext(config.ImplementationId.ToString())} when instatiating an {Debug.FormatTextInCodeContext("IHtPlayerPrefs")} object for channel {Debug.FormatTextInUserContext(channel)}.");
}
}
return returnValue;
}
protected virtual IHtPlayerPrefs ExtendedCreate(HtPlayerPrefsConfig config)
{
IHtPlayerPrefs returnValue = null;
switch (config.ImplementationId)
{
case HtPlayerPrefsConfig.EImplementationId.UnityPlayerPrefs:
returnValue = new HtPlayerPrefs_Unity(config.Channel, config.Logger, config.IsVerbose);
break;
case HtPlayerPrefsConfig.EImplementationId.UnityPlayerPrefsCached:
returnValue = new HtPlayerPrefs_Unity_Cached(config.Channel, config.Logger, config.IsVerbose);
break;
case HtPlayerPrefsConfig.EImplementationId.MemoryPlayerPrefs:
returnValue = new HtPlayerPrefs_Memory(config.Channel, config.Logger, config.IsVerbose);
break;
}
return returnValue;
}
}
}