Added multiplayer plugin

This commit is contained in:
Anthony Berg
2020-11-30 08:12:07 +00:00
parent 9cb342dd42
commit f64cf54803
450 changed files with 33131 additions and 10 deletions

View File

@@ -0,0 +1,44 @@
using System;
using UnityEngine;
namespace Mirror.Logging
{
public class ConsoleColorLogHandler : ILogHandler
{
readonly bool showExceptionStackTrace;
public ConsoleColorLogHandler(bool showExceptionStackTrace)
{
this.showExceptionStackTrace = showExceptionStackTrace;
}
public void LogException(Exception exception, UnityEngine.Object context)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Exception: {exception.Message}");
if (showExceptionStackTrace)
{
Console.WriteLine($" {exception.StackTrace}");
}
Console.ResetColor();
}
public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
{
switch (logType)
{
case LogType.Exception:
case LogType.Error:
case LogType.Assert:
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogType.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
}
Console.WriteLine(string.Format(format, args));
Console.ResetColor();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2a9618569c20a504aa86feb5913c70e9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,50 @@
using UnityEditor;
using UnityEngine;
namespace Mirror.Logging
{
#if UNITY_EDITOR
public static class EditorLogSettingsLoader
{
[InitializeOnLoadMethod]
static void Init()
{
// load settings first time LogFactory is used in the editor
LoadLogSettingsIntoDictionary();
}
public static void LoadLogSettingsIntoDictionary()
{
LogSettings settings = FindLogSettings();
if (settings != null)
{
settings.LoadIntoDictionary(LogFactory.loggers);
}
}
static LogSettings cache;
public static LogSettings FindLogSettings()
{
if (cache != null)
return cache;
string[] assetGuids = AssetDatabase.FindAssets("t:" + nameof(LogSettings));
if (assetGuids.Length == 0)
return null;
string firstGuid = assetGuids[0];
string path = AssetDatabase.GUIDToAssetPath(firstGuid);
cache = AssetDatabase.LoadAssetAtPath<LogSettings>(path);
if (assetGuids.Length > 2)
{
Debug.LogWarning("Found more than one LogSettings, Delete extra settings. Using first asset found: " + path);
}
Debug.Assert(cache != null, "Failed to load asset at: " + path);
return cache;
}
}
#endif
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a39aa1e48aa54eb4e964f0191c1dcdce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,100 @@
using System.Collections.Generic;
using UnityEngine;
namespace Mirror
{
public static class LogFactory
{
internal static readonly SortedDictionary<string, ILogger> loggers = new SortedDictionary<string, ILogger>();
public static SortedDictionary<string, ILogger>.ValueCollection AllLoggers => loggers.Values;
/// <summary>
/// logHandler used for new loggers
/// </summary>
static ILogHandler defaultLogHandler = Debug.unityLogger;
/// <summary>
/// if true sets all log level to LogType.Log
/// </summary>
static bool debugMode = false;
public static ILogger GetLogger<T>(LogType defaultLogLevel = LogType.Warning)
{
return GetLogger(typeof(T).Name, defaultLogLevel);
}
public static ILogger GetLogger(System.Type type, LogType defaultLogLevel = LogType.Warning)
{
return GetLogger(type.Name, defaultLogLevel);
}
public static ILogger GetLogger(string loggerName, LogType defaultLogLevel = LogType.Warning)
{
if (loggers.TryGetValue(loggerName, out ILogger logger))
{
return logger;
}
logger = new Logger(defaultLogHandler)
{
// by default, log warnings and up
filterLogType = debugMode ? LogType.Log : defaultLogLevel
};
loggers[loggerName] = logger;
return logger;
}
/// <summary>
/// Makes all log levels LogType.Log, this is so that NetworkManger.showDebugMessages can still be used
/// </summary>
public static void EnableDebugMode()
{
debugMode = true;
foreach (ILogger logger in loggers.Values)
{
logger.filterLogType = LogType.Log;
}
}
/// <summary>
/// Replacing log handler for all existing loggers and sets defaultLogHandler for new loggers
/// </summary>
/// <param name="logHandler"></param>
public static void ReplaceLogHandler(ILogHandler logHandler)
{
defaultLogHandler = logHandler;
foreach (ILogger logger in loggers.Values)
{
logger.logHandler = logHandler;
}
}
}
public static class ILoggerExtensions
{
public static void LogError(this ILogger logger, object message)
{
logger.Log(LogType.Error, message);
}
public static void Assert(this ILogger logger, bool condition, string message)
{
if (!condition)
logger.Log(LogType.Assert, message);
}
public static void LogWarning(this ILogger logger, object message)
{
logger.Log(LogType.Warning, message);
}
public static bool LogEnabled(this ILogger logger) => logger.IsLogTypeAllowed(LogType.Log);
public static bool WarnEnabled(this ILogger logger) => logger.IsLogTypeAllowed(LogType.Warning);
public static bool ErrorEnabled(this ILogger logger) => logger.IsLogTypeAllowed(LogType.Error);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d06522432d5a44e1587967a4731cd279
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Mirror.Logging
{
public class LogSettings : ScriptableObject
{
public List<LoggerSettings> loglevels = new List<LoggerSettings>();
[Serializable]
public struct LoggerSettings
{
public string name;
public LogType logLevel;
}
}
public static class LogSettingsExt
{
public static void SaveFromDictionary(this LogSettings settings, SortedDictionary<string, ILogger> dictionary)
{
if (settings == null)
{
Debug.LogWarning("Could not SaveFromDictionary because LogSettings were null");
return;
}
settings.loglevels.Clear();
foreach (KeyValuePair<string, ILogger> kvp in dictionary)
{
settings.loglevels.Add(new LogSettings.LoggerSettings { name = kvp.Key, logLevel = kvp.Value.filterLogType });
}
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(settings);
#endif
}
public static void LoadIntoDictionary(this LogSettings settings, SortedDictionary<string, ILogger> dictionary)
{
if (settings == null)
{
Debug.LogWarning("Could not LoadIntoDictionary because LogSettings were null");
return;
}
foreach (LogSettings.LoggerSettings logLevel in settings.loglevels)
{
if (dictionary.TryGetValue(logLevel.name, out ILogger logger))
{
logger.filterLogType = logLevel.logLevel;
}
else
{
logger = new Logger(Debug.unityLogger)
{
filterLogType = logLevel.logLevel
};
dictionary[logLevel.name] = logger;
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 633889a39717fde4fa28dd6b948dfac7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,24 @@
using UnityEngine;
namespace Mirror.Logging
{
/// <summary>
/// Used to replace log hanlder with Console Color LogHandler
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkHeadlessLogger")]
[HelpURL("https://mirror-networking.com/docs/Components/NetworkHeadlessLogger.html")]
public class NetworkHeadlessLogger : MonoBehaviour
{
#pragma warning disable CS0414 // unused private members
[SerializeField] bool showExceptionStackTrace = false;
#pragma warning restore CS0414 // unused private members
void Awake()
{
#if UNITY_SERVER
LogFactory.ReplaceLogHandler(new ConsoleColorLogHandler(showExceptionStackTrace));
#endif
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c7627623f2b9fad4484082517cd73e67
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,46 @@
using UnityEngine;
namespace Mirror.Logging
{
/// <summary>
/// Used to load LogSettings in build
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkLogSettings")]
[HelpURL("https://mirror-networking.com/docs/Components/NetworkLogSettings.html")]
public class NetworkLogSettings : MonoBehaviour
{
[Header("Log Settings Asset")]
[SerializeField] internal LogSettings settings;
#if UNITY_EDITOR
// called when component is added to GameObject
void Reset()
{
LogSettings existingSettings = EditorLogSettingsLoader.FindLogSettings();
if (existingSettings != null)
{
settings = existingSettings;
UnityEditor.EditorUtility.SetDirty(this);
}
}
#endif
void Awake()
{
RefreshDictionary();
}
void OnValidate()
{
// if settings field is changed
RefreshDictionary();
}
void RefreshDictionary()
{
settings.LoadIntoDictionary(LogFactory.loggers);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ac6e8eccf4b6f4dc7b24c276ef47fde8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant: