Logger and Configuration Refactoring (#573)

* Logging: Refactor log targets into Ryujinx.Common

* Logger: Implement JSON Log Target

* Logger: Optimize Console/File logging targets

Implement a simple ObjectPool to pool up StringBuilders to avoid causing excessive GCing of gen1/2 items when large amounts of log entries are being generated.

We can also pre-determine the async overflow action at initialization time, allowing for an easy optimization in the message enqueue function, avoiding a number of comparisons.

* Logger: Implement LogFormatters

* Config: Refactor configuration file and loading

* Config: Rename to .jsonc to avoid highlighting issues in VSC and GitHub

* Resolve style nits

* Config: Resolve incorrect default key binding

* Config: Also update key binding default in schema

* Tidy up namespace imports

* Config: Update CONFIG.md to reflect new Config file
This commit is contained in:
jduncanator 2019-02-11 23:00:32 +11:00 committed by gdkchan
parent a694420d11
commit d306115750
31 changed files with 1844 additions and 691 deletions

View file

@ -142,24 +142,24 @@ namespace Ryujinx
{
KeyboardState keyboard = _keyboard.Value;
currentButton = Config.NpadKeyboard.GetButtons(keyboard);
currentButton = Configuration.Instance.KeyboardControls.GetButtons(keyboard);
(leftJoystickDx, leftJoystickDy) = Config.NpadKeyboard.GetLeftStick(keyboard);
(leftJoystickDx, leftJoystickDy) = Configuration.Instance.KeyboardControls.GetLeftStick(keyboard);
(rightJoystickDx, rightJoystickDy) = Config.NpadKeyboard.GetRightStick(keyboard);
(rightJoystickDx, rightJoystickDy) = Configuration.Instance.KeyboardControls.GetRightStick(keyboard);
}
currentButton |= Config.NpadController.GetButtons();
currentButton |= Configuration.Instance.GamepadControls.GetButtons();
//Keyboard has priority stick-wise
if (leftJoystickDx == 0 && leftJoystickDy == 0)
{
(leftJoystickDx, leftJoystickDy) = Config.NpadController.GetLeftStick();
(leftJoystickDx, leftJoystickDy) = Configuration.Instance.GamepadControls.GetLeftStick();
}
if (rightJoystickDx == 0 && rightJoystickDy == 0)
{
(rightJoystickDx, rightJoystickDy) = Config.NpadController.GetRightStick();
(rightJoystickDx, rightJoystickDy) = Configuration.Instance.GamepadControls.GetRightStick();
}
leftJoystick = new HidJoystickPosition

View file

@ -1,140 +0,0 @@
using Ryujinx.Common.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
namespace Ryujinx
{
static class Log
{
private static readonly string _path;
private static StreamWriter _logWriter;
private static Thread _messageThread;
private static BlockingCollection<LogEventArgs> _messageQueue;
private static Dictionary<LogLevel, ConsoleColor> _logColors;
static Log()
{
_logColors = new Dictionary<LogLevel, ConsoleColor>()
{
{ LogLevel.Stub, ConsoleColor.DarkGray },
{ LogLevel.Info, ConsoleColor.White },
{ LogLevel.Warning, ConsoleColor.Yellow },
{ LogLevel.Error, ConsoleColor.Red }
};
_messageQueue = new BlockingCollection<LogEventArgs>(10);
_messageThread = new Thread(() =>
{
while (!_messageQueue.IsCompleted)
{
try
{
PrintLog(_messageQueue.Take());
}
catch (InvalidOperationException)
{
// IOE means that Take() was called on a completed collection.
// Some other thread can call CompleteAdding after we pass the
// IsCompleted check but before we call Take.
// We can simply catch the exception since the loop will break
// on the next iteration.
}
}
});
_path = Path.Combine(Environment.CurrentDirectory, "Ryujinx.log");
if (Logger.EnableFileLog)
{
_logWriter = new StreamWriter(File.Open(_path,FileMode.Create, FileAccess.Write));
}
_messageThread.IsBackground = true;
_messageThread.Start();
}
private static void PrintLog(LogEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", e.Time);
sb.Append(" | ");
sb.AppendFormat("{0:d4}", e.ThreadId);
sb.Append(' ');
sb.Append(e.Message);
if (e.Data != null)
{
PropertyInfo[] props = e.Data.GetType().GetProperties();
sb.Append(' ');
foreach (var prop in props)
{
sb.Append(prop.Name);
sb.Append(": ");
sb.Append(prop.GetValue(e.Data));
sb.Append(" - ");
}
// We remove the final '-' from the string
if (props.Length > 0)
{
sb.Remove(sb.Length - 3, 3);
}
}
string message = sb.ToString();
if (_logColors.TryGetValue(e.Level, out ConsoleColor color))
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
}
else
{
Console.WriteLine(message);
}
if (Logger.EnableFileLog)
{
_logWriter.WriteLine(message);
}
}
public static void LogMessage(object sender, LogEventArgs e)
{
if (!_messageQueue.IsAddingCompleted)
{
_messageQueue.Add(e);
}
}
public static void Close()
{
_messageQueue.CompleteAdding();
_messageThread.Join();
if (Logger.EnableFileLog)
{
_logWriter.Flush();
_logWriter.Close();
_logWriter.Dispose();
}
}
}
}

View file

@ -8,28 +8,24 @@ namespace Ryujinx.UI.Input
public enum ControllerInputId
{
Invalid,
LStick,
RStick,
LShoulder,
RShoulder,
LTrigger,
RTrigger,
LJoystick,
RJoystick,
DPadUp,
DPadDown,
DPadLeft,
DPadRight,
Start,
Back,
LShoulder,
RStick,
A,
B,
X,
Y,
Start,
RShoulder,
LTrigger,
RTrigger,
LJoystick,
RJoystick
Y
}
public struct NpadControllerLeft
@ -60,34 +56,55 @@ namespace Ryujinx.UI.Input
public class NpadController
{
public bool Enabled { private set; get; }
public int Index { private set; get; }
public float Deadzone { private set; get; }
public float TriggerThreshold { private set; get; }
/// <summary>
/// Enables or disables controller support
/// </summary>
public bool Enabled { get; private set; }
public NpadControllerLeft Left { private set; get; }
public NpadControllerRight Right { private set; get; }
/// <summary>
/// Controller Device Index
/// </summary>
public int Index { get; private set; }
/// <summary>
/// Controller Analog Stick Deadzone
/// </summary>
public float Deadzone { get; private set; }
/// <summary>
/// Controller Trigger Threshold
/// </summary>
public float TriggerThreshold { get; private set; }
/// <summary>
/// Left JoyCon Controller Bindings
/// </summary>
public NpadControllerLeft LeftJoycon { get; private set; }
/// <summary>
/// Right JoyCon Controller Bindings
/// </summary>
public NpadControllerRight RightJoycon { get; private set; }
public NpadController(
bool enabled,
int index,
float deadzone,
float triggerThreshold,
NpadControllerLeft left,
NpadControllerRight right)
bool enabled,
int index,
float deadzone,
float triggerThreshold,
NpadControllerLeft leftJoycon,
NpadControllerRight rightJoycon)
{
Enabled = enabled;
Index = index;
Deadzone = deadzone;
TriggerThreshold = triggerThreshold;
Left = left;
Right = right;
LeftJoycon = leftJoycon;
RightJoycon = rightJoycon;
}
//Unmapped controllers are problematic, skip them
if (GamePad.GetName(index) == "Unmapped Controller")
{
Enabled = false;
}
public void SetEnabled(bool enabled)
{
Enabled = enabled;
}
public HidControllerButtons GetButtons()
@ -101,23 +118,23 @@ namespace Ryujinx.UI.Input
HidControllerButtons buttons = 0;
if (IsPressed(gpState, Left.DPadUp)) buttons |= HidControllerButtons.DpadUp;
if (IsPressed(gpState, Left.DPadDown)) buttons |= HidControllerButtons.DpadDown;
if (IsPressed(gpState, Left.DPadLeft)) buttons |= HidControllerButtons.DpadLeft;
if (IsPressed(gpState, Left.DPadRight)) buttons |= HidControllerButtons.DPadRight;
if (IsPressed(gpState, Left.StickButton)) buttons |= HidControllerButtons.StickLeft;
if (IsPressed(gpState, Left.ButtonMinus)) buttons |= HidControllerButtons.Minus;
if (IsPressed(gpState, Left.ButtonL)) buttons |= HidControllerButtons.L;
if (IsPressed(gpState, Left.ButtonZl)) buttons |= HidControllerButtons.Zl;
if (IsPressed(gpState, LeftJoycon.DPadUp)) buttons |= HidControllerButtons.DpadUp;
if (IsPressed(gpState, LeftJoycon.DPadDown)) buttons |= HidControllerButtons.DpadDown;
if (IsPressed(gpState, LeftJoycon.DPadLeft)) buttons |= HidControllerButtons.DpadLeft;
if (IsPressed(gpState, LeftJoycon.DPadRight)) buttons |= HidControllerButtons.DPadRight;
if (IsPressed(gpState, LeftJoycon.StickButton)) buttons |= HidControllerButtons.StickLeft;
if (IsPressed(gpState, LeftJoycon.ButtonMinus)) buttons |= HidControllerButtons.Minus;
if (IsPressed(gpState, LeftJoycon.ButtonL)) buttons |= HidControllerButtons.L;
if (IsPressed(gpState, LeftJoycon.ButtonZl)) buttons |= HidControllerButtons.Zl;
if (IsPressed(gpState, Right.ButtonA)) buttons |= HidControllerButtons.A;
if (IsPressed(gpState, Right.ButtonB)) buttons |= HidControllerButtons.B;
if (IsPressed(gpState, Right.ButtonX)) buttons |= HidControllerButtons.X;
if (IsPressed(gpState, Right.ButtonY)) buttons |= HidControllerButtons.Y;
if (IsPressed(gpState, Right.StickButton)) buttons |= HidControllerButtons.StickRight;
if (IsPressed(gpState, Right.ButtonPlus)) buttons |= HidControllerButtons.Plus;
if (IsPressed(gpState, Right.ButtonR)) buttons |= HidControllerButtons.R;
if (IsPressed(gpState, Right.ButtonZr)) buttons |= HidControllerButtons.Zr;
if (IsPressed(gpState, RightJoycon.ButtonA)) buttons |= HidControllerButtons.A;
if (IsPressed(gpState, RightJoycon.ButtonB)) buttons |= HidControllerButtons.B;
if (IsPressed(gpState, RightJoycon.ButtonX)) buttons |= HidControllerButtons.X;
if (IsPressed(gpState, RightJoycon.ButtonY)) buttons |= HidControllerButtons.Y;
if (IsPressed(gpState, RightJoycon.StickButton)) buttons |= HidControllerButtons.StickRight;
if (IsPressed(gpState, RightJoycon.ButtonPlus)) buttons |= HidControllerButtons.Plus;
if (IsPressed(gpState, RightJoycon.ButtonR)) buttons |= HidControllerButtons.R;
if (IsPressed(gpState, RightJoycon.ButtonZr)) buttons |= HidControllerButtons.Zr;
return buttons;
}
@ -129,7 +146,7 @@ namespace Ryujinx.UI.Input
return (0, 0);
}
return GetStick(Left.Stick);
return GetStick(LeftJoycon.Stick);
}
public (short, short) GetRightStick()
@ -139,7 +156,7 @@ namespace Ryujinx.UI.Input
return (0, 0);
}
return GetStick(Right.Stick);
return GetStick(RightJoycon.Stick);
}
private (short, short) GetStick(ControllerInputId joystick)

View file

@ -5,70 +5,69 @@ namespace Ryujinx.UI.Input
{
public struct NpadKeyboardLeft
{
public int StickUp;
public int StickDown;
public int StickLeft;
public int StickRight;
public int StickButton;
public int DPadUp;
public int DPadDown;
public int DPadLeft;
public int DPadRight;
public int ButtonMinus;
public int ButtonL;
public int ButtonZl;
public Key StickUp;
public Key StickDown;
public Key StickLeft;
public Key StickRight;
public Key StickButton;
public Key DPadUp;
public Key DPadDown;
public Key DPadLeft;
public Key DPadRight;
public Key ButtonMinus;
public Key ButtonL;
public Key ButtonZl;
}
public struct NpadKeyboardRight
{
public int StickUp;
public int StickDown;
public int StickLeft;
public int StickRight;
public int StickButton;
public int ButtonA;
public int ButtonB;
public int ButtonX;
public int ButtonY;
public int ButtonPlus;
public int ButtonR;
public int ButtonZr;
public Key StickUp;
public Key StickDown;
public Key StickLeft;
public Key StickRight;
public Key StickButton;
public Key ButtonA;
public Key ButtonB;
public Key ButtonX;
public Key ButtonY;
public Key ButtonPlus;
public Key ButtonR;
public Key ButtonZr;
}
public class NpadKeyboard
{
public NpadKeyboardLeft Left;
public NpadKeyboardRight Right;
/// <summary>
/// Left JoyCon Keyboard Bindings
/// </summary>
public NpadKeyboardLeft LeftJoycon { get; private set; }
public NpadKeyboard(
NpadKeyboardLeft left,
NpadKeyboardRight right)
{
Left = left;
Right = right;
}
/// <summary>
/// Right JoyCon Keyboard Bindings
/// </summary>
public NpadKeyboardRight RightJoycon { get; private set; }
public HidControllerButtons GetButtons(KeyboardState keyboard)
{
HidControllerButtons buttons = 0;
if (keyboard[(Key)Left.StickButton]) buttons |= HidControllerButtons.StickLeft;
if (keyboard[(Key)Left.DPadUp]) buttons |= HidControllerButtons.DpadUp;
if (keyboard[(Key)Left.DPadDown]) buttons |= HidControllerButtons.DpadDown;
if (keyboard[(Key)Left.DPadLeft]) buttons |= HidControllerButtons.DpadLeft;
if (keyboard[(Key)Left.DPadRight]) buttons |= HidControllerButtons.DPadRight;
if (keyboard[(Key)Left.ButtonMinus]) buttons |= HidControllerButtons.Minus;
if (keyboard[(Key)Left.ButtonL]) buttons |= HidControllerButtons.L;
if (keyboard[(Key)Left.ButtonZl]) buttons |= HidControllerButtons.Zl;
if (keyboard[(Key)LeftJoycon.StickButton]) buttons |= HidControllerButtons.StickLeft;
if (keyboard[(Key)LeftJoycon.DPadUp]) buttons |= HidControllerButtons.DpadUp;
if (keyboard[(Key)LeftJoycon.DPadDown]) buttons |= HidControllerButtons.DpadDown;
if (keyboard[(Key)LeftJoycon.DPadLeft]) buttons |= HidControllerButtons.DpadLeft;
if (keyboard[(Key)LeftJoycon.DPadRight]) buttons |= HidControllerButtons.DPadRight;
if (keyboard[(Key)LeftJoycon.ButtonMinus]) buttons |= HidControllerButtons.Minus;
if (keyboard[(Key)LeftJoycon.ButtonL]) buttons |= HidControllerButtons.L;
if (keyboard[(Key)LeftJoycon.ButtonZl]) buttons |= HidControllerButtons.Zl;
if (keyboard[(Key)Right.StickButton]) buttons |= HidControllerButtons.StickRight;
if (keyboard[(Key)Right.ButtonA]) buttons |= HidControllerButtons.A;
if (keyboard[(Key)Right.ButtonB]) buttons |= HidControllerButtons.B;
if (keyboard[(Key)Right.ButtonX]) buttons |= HidControllerButtons.X;
if (keyboard[(Key)Right.ButtonY]) buttons |= HidControllerButtons.Y;
if (keyboard[(Key)Right.ButtonPlus]) buttons |= HidControllerButtons.Plus;
if (keyboard[(Key)Right.ButtonR]) buttons |= HidControllerButtons.R;
if (keyboard[(Key)Right.ButtonZr]) buttons |= HidControllerButtons.Zr;
if (keyboard[(Key)RightJoycon.StickButton]) buttons |= HidControllerButtons.StickRight;
if (keyboard[(Key)RightJoycon.ButtonA]) buttons |= HidControllerButtons.A;
if (keyboard[(Key)RightJoycon.ButtonB]) buttons |= HidControllerButtons.B;
if (keyboard[(Key)RightJoycon.ButtonX]) buttons |= HidControllerButtons.X;
if (keyboard[(Key)RightJoycon.ButtonY]) buttons |= HidControllerButtons.Y;
if (keyboard[(Key)RightJoycon.ButtonPlus]) buttons |= HidControllerButtons.Plus;
if (keyboard[(Key)RightJoycon.ButtonR]) buttons |= HidControllerButtons.R;
if (keyboard[(Key)RightJoycon.ButtonZr]) buttons |= HidControllerButtons.Zr;
return buttons;
}
@ -78,10 +77,10 @@ namespace Ryujinx.UI.Input
short dx = 0;
short dy = 0;
if (keyboard[(Key)Left.StickUp]) dy = short.MaxValue;
if (keyboard[(Key)Left.StickDown]) dy = -short.MaxValue;
if (keyboard[(Key)Left.StickLeft]) dx = -short.MaxValue;
if (keyboard[(Key)Left.StickRight]) dx = short.MaxValue;
if (keyboard[(Key)LeftJoycon.StickUp]) dy = short.MaxValue;
if (keyboard[(Key)LeftJoycon.StickDown]) dy = -short.MaxValue;
if (keyboard[(Key)LeftJoycon.StickLeft]) dx = -short.MaxValue;
if (keyboard[(Key)LeftJoycon.StickRight]) dx = short.MaxValue;
return (dx, dy);
}
@ -91,10 +90,10 @@ namespace Ryujinx.UI.Input
short dx = 0;
short dy = 0;
if (keyboard[(Key)Right.StickUp]) dy = short.MaxValue;
if (keyboard[(Key)Right.StickDown]) dy = -short.MaxValue;
if (keyboard[(Key)Right.StickLeft]) dx = -short.MaxValue;
if (keyboard[(Key)Right.StickRight]) dx = short.MaxValue;
if (keyboard[(Key)RightJoycon.StickUp]) dy = short.MaxValue;
if (keyboard[(Key)RightJoycon.StickDown]) dy = -short.MaxValue;
if (keyboard[(Key)RightJoycon.StickLeft]) dx = -short.MaxValue;
if (keyboard[(Key)RightJoycon.StickRight]) dx = short.MaxValue;
return (dx, dy);
}