Skip to content

Commit

Permalink
Store Settings in Scriptable Object
Browse files Browse the repository at this point in the history
  • Loading branch information
koosemose committed Nov 30, 2016
1 parent 392cec4 commit 1be12ee
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*.suo
*.user
*.sln.docstates

UnityEngine.dll
# Build results
[Dd]ebug/
[Dd]ebugPublic/
Expand Down
20 changes: 20 additions & 0 deletions UnityDebugger/ChannelDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System;
using com.spacepuppy.Collections;


[Serializable]
public class ChannelDictionary : SerializableDictionaryBase<string, bool>
{
public ChannelDictionary()
{
}

public ChannelDictionary(ChannelDictionary channelState)
{
foreach (KeyValuePair<string,bool> kvp in channelState)
{
this.Add(kvp.Key, kvp.Value);
}
}
}
8 changes: 8 additions & 0 deletions UnityDebugger/ChannelSettingsSO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ChannelSettingsSO : ScriptableObject {
public bool DefaultState;
public ChannelDictionary ChannelState = new ChannelDictionary();
}
44 changes: 35 additions & 9 deletions UnityDebugger/Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;


namespace UnityDebugger
{
Expand All @@ -25,9 +28,10 @@ public AssertException() { }
public AssertException(string message) : base(message) { }
public AssertException(string message, Exception innerException) : base(message, innerException) { }
}

public static class Debugger
{
public static bool exists = false;
/// <summary>
/// If false, Debugger does nothing.
/// Default value: Debug.isDebugBuild
Expand All @@ -40,13 +44,30 @@ public static class Debugger
/// </summary>
public static LogLevel LogLevel { get; set; }

public static bool DefaultState;

public static Dictionary<string, bool> Channels { get; set; }

static Debugger()
{
Enabled = Debug.isDebugBuild;
LogLevel = LogLevel.Info;
Channels = new Dictionary<string, bool>();
exists = true;

DefaultState = true;
if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor)
{
ChannelSettingsSO channelSettings = Resources.Load<ChannelSettingsSO>("ChannelSettings");
if (channelSettings != null)
{
DefaultState = channelSettings.DefaultState;
foreach (string channelName in channelSettings.ChannelState.Keys.AsEnumerable())
{
Channels.Add(channelName, channelSettings.ChannelState[channelName]);
}
}
}
}

#region Asserts
Expand Down Expand Up @@ -98,6 +119,7 @@ public static void Log(string message, Object context = null)
/// <summary>
/// Writes info to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name = "channel">Channel to log to.</param>
/// <param name="message">Info to write.</param>
/// <param name="context">Context.</param>
public static void Log(string channel, string message, Object context = null)
Expand All @@ -112,7 +134,7 @@ public static void Log(string channel, string message, Object context = null)
/// Writes info to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="channel">Channel.</param>
/// <param name = "channel">Channel to log to.</param>
/// <param name="format">Format.</param>
/// <param name="args">Arguments.</param>
public static void LogFormat(Object context, string channel, string format, params object[] args)
Expand All @@ -123,7 +145,7 @@ public static void LogFormat(Object context, string channel, string format, para
/// <summary>
/// Writes info to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name="channel">Channel.</param>
/// <param name = "channel">Channel to log to.</param>
/// <param name="format">Format.</param>
/// <param name="args">Arguments.</param>
public static void LogFormat(string channel, string format, params object[] args)
Expand All @@ -147,6 +169,7 @@ public static void LogWarning(string message, Object context = null)
/// <summary>
/// Writes warning to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name = "channel">Channel to log to.</param>
/// <param name="message">Warning to write.</param>
/// <param name="context">Context.</param>
public static void LogWarning(string channel, string message, Object context = null)
Expand All @@ -161,7 +184,7 @@ public static void LogWarning(string channel, string message, Object context = n
/// Writes info to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="channel">Channel.</param>
/// <param name = "channel">Channel to log to.</param>
/// <param name="format">Format.</param>
/// <param name="args">Arguments.</param>
public static void LogWarningFormat(Object context, string channel, string format, params object[] args)
Expand All @@ -172,7 +195,7 @@ public static void LogWarningFormat(Object context, string channel, string forma
/// <summary>
/// Writes info to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name="channel">Channel.</param>
/// <param name = "channel">Channel to log to.</param>
/// <param name="format">Format.</param>
/// <param name="args">Arguments.</param>
public static void LogWarningFormat(string channel, string format, params object[] args)
Expand All @@ -196,6 +219,7 @@ public static void LogError(string message, Object context = null)
/// <summary>
/// Writes error to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name = "channel">Channel to log to.</param>
/// <param name="message">Error to write.</param>
/// <param name="context">Context.</param>
public static void LogError(string channel, string message, Object context = null)
Expand All @@ -210,7 +234,7 @@ public static void LogError(string channel, string message, Object context = nul
/// Writes info to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="channel">Channel.</param>
/// <param name = "channel">Channel to log to.</param>
/// <param name="format">Format.</param>
/// <param name="args">Arguments.</param>
public static void LogErrorFormat(Object context, string channel, string format, params object[] args)
Expand All @@ -221,7 +245,7 @@ public static void LogErrorFormat(Object context, string channel, string format,
/// <summary>
/// Writes info to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name="channel">Channel.</param>
/// <param name = "channel">Channel to log to.</param>
/// <param name="format">Format.</param>
/// <param name="args">Arguments.</param>
public static void LogErrorFormat(string channel, string format, params object[] args)
Expand All @@ -245,6 +269,7 @@ public static void LogException(Exception exception, Object context = null)
/// <summary>
/// Writes exception to console. Works only when Debugger is Enabled.
/// </summary>
/// <param name = "channel">Channel to log to.</param>
/// <param name="exception">Exception to write.</param>
/// <param name="context">Context.</param>
public static void LogException(string channel, Exception exception, Object context = null)
Expand Down Expand Up @@ -272,8 +297,9 @@ private static bool ChannelEnabled(string channel)
}
else
{
Channels.Add(channel, false);
return false;
bool channelSetting = DefaultState;
Channels.Add(channel, channelSetting);
return channelSetting;
}
}
#endregion
Expand Down
7 changes: 7 additions & 0 deletions UnityDebugger/SerializableDictionaryBase/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2015, Dylan Engelman, Jupiter Lighthouse Studio

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.
196 changes: 196 additions & 0 deletions UnityDebugger/SerializableDictionaryBase/SerializableDictionaryBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using System.Collections.Generic;
using System.Linq;

namespace com.spacepuppy.Collections
{

public abstract class DrawableDictionary
{

}

[System.Serializable()]
public class SerializableDictionaryBase<TKey, TValue> : DrawableDictionary, IDictionary<TKey, TValue>, UnityEngine.ISerializationCallbackReceiver
{

#region Fields

[System.NonSerialized()]
private Dictionary<TKey, TValue> _dict;

#endregion

#region IDictionary Interface

public int Count
{
get { return (_dict != null) ? _dict.Count : 0; }
}

public void Add(TKey key, TValue value)
{
if (_dict == null) _dict = new Dictionary<TKey, TValue>();
_dict.Add(key, value);
}

public bool ContainsKey(TKey key)
{
if (_dict == null) return false;
return _dict.ContainsKey(key);
}

public ICollection<TKey> Keys
{
get
{
if (_dict == null) _dict = new Dictionary<TKey, TValue>();
return _dict.Keys;
}
}

public bool Remove(TKey key)
{
if (_dict == null) return false;
return _dict.Remove(key);
}

public bool TryGetValue(TKey key, out TValue value)
{
if(_dict == null)
{
value = default(TValue);
return false;
}
return _dict.TryGetValue(key, out value);
}

public ICollection<TValue> Values
{
get
{
if (_dict == null) _dict = new Dictionary<TKey, TValue>();
return _dict.Values;
}
}

public TValue this[TKey key]
{
get
{
if (_dict == null) throw new KeyNotFoundException();
return _dict[key];
}
set
{
if (_dict == null) _dict = new Dictionary<TKey, TValue>();
_dict[key] = value;
}
}

public void Clear()
{
if (_dict != null) _dict.Clear();
}

void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
if (_dict == null) _dict = new Dictionary<TKey, TValue>();
(_dict as ICollection<KeyValuePair<TKey, TValue>>).Add(item);
}

bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
if (_dict == null) return false;
return (_dict as ICollection<KeyValuePair<TKey, TValue>>).Contains(item);
}

void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (_dict == null) return;
(_dict as ICollection<KeyValuePair<TKey, TValue>>).CopyTo(array, arrayIndex);
}

bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
if (_dict == null) return false;
return (_dict as ICollection<KeyValuePair<TKey, TValue>>).Remove(item);
}

bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get { return false; }
}

public Dictionary<TKey, TValue>.Enumerator GetEnumerator()
{
if (_dict == null) return default(Dictionary<TKey, TValue>.Enumerator);
return _dict.GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
if (_dict == null) return Enumerable.Empty<KeyValuePair<TKey, TValue>>().GetEnumerator();
return _dict.GetEnumerator();
}

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
if (_dict == null) return Enumerable.Empty<KeyValuePair<TKey, TValue>>().GetEnumerator();
return _dict.GetEnumerator();
}

#endregion

#region ISerializationCallbackReceiver

[UnityEngine.SerializeField()]
private TKey[] _keys;
[UnityEngine.SerializeField()]
private TValue[] _values;

void UnityEngine.ISerializationCallbackReceiver.OnAfterDeserialize()
{
if(_keys != null && _values != null)
{
if (_dict == null) _dict = new Dictionary<TKey, TValue>(_keys.Length);
else _dict.Clear();
for(int i = 0; i < _keys.Length; i++)
{
if (i < _values.Length)
_dict[_keys[i]] = _values[i];
else
_dict[_keys[i]] = default(TValue);
}
}

_keys = null;
_values = null;
}

void UnityEngine.ISerializationCallbackReceiver.OnBeforeSerialize()
{
if(_dict == null || _dict.Count == 0)
{
_keys = null;
_values = null;
}
else
{
int cnt = _dict.Count;
_keys = new TKey[cnt];
_values = new TValue[cnt];
int i = 0;
var e = _dict.GetEnumerator();
while (e.MoveNext())
{
_keys[i] = e.Current.Key;
_values[i] = e.Current.Value;
i++;
}
}
}

#endregion

}
}
Loading

0 comments on commit 1be12ee

Please sign in to comment.