Skip to content

Commit

Permalink
fix: logs now save properly when reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Nov 10, 2020
1 parent 7728d6e commit 19c86e7
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions Assets/Mirror/Editor/LogLevelWindow.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
Expand All @@ -8,8 +9,6 @@ namespace Mirror
[CustomEditor(typeof(LogSettings), true)]
public class LogSettingEditor : Editor
{
private static Dictionary<string, LogType> levels = new Dictionary<string, LogType>();

#region GUI
public override void OnInspectorGUI()
{
Expand All @@ -23,7 +22,6 @@ public override void OnInspectorGUI()
EditorGUI.BeginChangeCheck();

EditorGUILayout.BeginVertical(EditorStyles.inspectorDefaultMargins);
SetLevels();

foreach (KeyValuePair<string, ILogger> item in LogFactory.loggers)
{
Expand All @@ -37,18 +35,9 @@ public override void OnInspectorGUI()
}
}

private void SetLevels()
{
foreach (KeyValuePair<string, LogType> kvp in levels)
{
LogFactory.GetLogger(kvp.Key).filterLogType = kvp.Value;
}
}

void DrawLoggerField(string loggerName, ILogger logger)
{
logger.filterLogType = (LogType)EditorGUILayout.EnumPopup(new GUIContent(loggerName), logger.filterLogType);
levels[loggerName] = logger.filterLogType;
}

#endregion
Expand All @@ -66,4 +55,61 @@ private void SaveLevels()

#endregion
}

public struct LogLevelContainer
{
public List<LogSettings.Level> levels;

public LogLevelContainer(List<LogSettings.Level> levels)
{
this.levels = levels;
}
}

[InitializeOnLoad]
public static class LogSettingsSaver
{
static LogSettingsSaver()
{
EditorApplication.playModeStateChanged += OnChangePlayModeState;
Load();
}

private static void OnChangePlayModeState(PlayModeStateChange state)
{
// Create backups of the scenes before you enter play mode, because this thing is pretty destructive and you can lose work if it goes wrong.
if (state == PlayModeStateChange.ExitingPlayMode)
{
Save();
}
else if (state == PlayModeStateChange.EnteredEditMode)
{
Load();
}
}

private static void Load()
{
string leveljson = EditorPrefs.GetString("Log Levels", "{\"levels\": [] }");

var levelContainer = JsonUtility.FromJson<LogLevelContainer>(leveljson);
var levels = levelContainer.levels;

foreach (LogSettings.Level level in levels)
{
LogFactory.GetLogger(level.Name).filterLogType = level.level;
}
}

private static void Save()
{
var levels = LogFactory.loggers.Select(kvp => new LogSettings.Level { Name = kvp.Key, level = kvp.Value.filterLogType }).ToList();

var levelContainer = new LogLevelContainer(levels);

string leveljson = JsonUtility.ToJson(levelContainer);

EditorPrefs.SetString("Log Levels", leveljson);
}
}
}

0 comments on commit 19c86e7

Please sign in to comment.