update EcsDebug

This commit is contained in:
Mikhail 2024-04-27 17:55:44 +08:00
parent 7cd50524e5
commit 0fc0e1f7e4
2 changed files with 121 additions and 28 deletions

View File

@ -22,6 +22,20 @@
public const int MAGIC_PRIME = 314159; public const int MAGIC_PRIME = 314159;
/// defs /// defs
public const bool ENABLE_DRAGONECS_DEBUGGER =
#if ENABLE_DRAGONECS_DEBUGGER
true;
#else
false;
#endif
public const bool ENABLE_DRAGONECS_ASSERT_CHEKS =
#if ENABLE_DRAGONECS_ASSERT_CHEKS
true;
#else
false;
#endif
public const bool REFLECTION_DISABLED = public const bool REFLECTION_DISABLED =
#if REFLECTION_DISABLED #if REFLECTION_DISABLED
true; true;
@ -34,12 +48,7 @@
#else #else
false; false;
#endif #endif
public const bool ENABLE_DRAGONECS_ASSERT_CHEKS =
#if ENABLE_DRAGONECS_ASSERT_CHEKS
true;
#else
false;
#endif
public const bool ENABLE_DUMMY_SPAN = public const bool ENABLE_DUMMY_SPAN =
#if ENABLE_DUMMY_SPAN #if ENABLE_DUMMY_SPAN
true; true;

View File

@ -50,56 +50,57 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PrintWarning(object v) public static void PrintWarning(object v)
{ {
#if !DISABLE_DRAGONECS_DEBUGGER
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.PrintWarning(v); DebugService.Instance.PrintWarning(v);
#endif #endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PrintError(object v) public static void PrintError(object v)
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.PrintError(v); DebugService.Instance.PrintError(v);
#endif #endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PrintErrorAndBreak(object v) public static void PrintErrorAndBreak(object v)
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.PrintErrorAndBreak(v); DebugService.Instance.PrintErrorAndBreak(v);
#endif #endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PrintPass(object v) public static void PrintPass(object v)
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.PrintPass(v); DebugService.Instance.PrintPass(v);
#endif #endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Print() public static void Print()
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.Print(); DebugService.Instance.Print();
#endif #endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Print(object v) public static void Print(object v)
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.Print(v); DebugService.Instance.Print(v);
#endif #endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Print(string tag, object v) public static void Print(string tag, object v)
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.Print(tag, v); DebugService.Instance.Print(tag, v);
#endif #endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Break() public static void Break()
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER
DebugService.Instance.Break(); DebugService.Instance.Break();
#endif #endif
} }
@ -191,15 +192,26 @@ namespace DCFApixels.DragonECS
} }
public sealed class DefaultDebugService : DebugService public sealed class DefaultDebugService : DebugService
{ {
private Stopwatch[] _stopwatchs; private const string PROFILER_MARKER = "ProfilerMark";
private string[] _stopwatchsNames; private struct MarkerData
{
public Stopwatch stopwatch;
public string name;
public MarkerData(Stopwatch stopwatch, string name)
{
this.stopwatch = stopwatch;
this.name = name;
}
}
private MarkerData[] _stopwatchs;
[ThreadStatic]
private static char[] _buffer;
public DefaultDebugService() public DefaultDebugService()
{ {
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Black;
#if !DISABLE_DRAGONECS_DEBUGGER #if !DISABLE_DRAGONECS_DEBUGGER
_stopwatchs = new Stopwatch[64]; _stopwatchs = new MarkerData[64];
_stopwatchsNames = new string[64];
#endif #endif
} }
@ -236,37 +248,109 @@ namespace DCFApixels.DragonECS
Console.ReadKey(); Console.ReadKey();
Console.ForegroundColor = color; Console.ForegroundColor = color;
} }
private const string PROFILER_MARKER_CACHE = "[" + PROFILER_MARKER + "] ";
public override void ProfilerMarkBegin(int id) public override void ProfilerMarkBegin(int id)
{ {
#if !UNITY_EDITOR
var color = Console.ForegroundColor; var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.DarkGray;
_stopwatchs[id].Start(); _stopwatchs[id].stopwatch.Start();
Print("ProfilerMark", $"{_stopwatchsNames[id]} start <");
Console.Write(PROFILER_MARKER_CACHE);
Console.Write(_stopwatchs[id].name);
Console.WriteLine("> ");
Console.ForegroundColor = color; Console.ForegroundColor = color;
#endif
} }
public override void ProfilerMarkEnd(int id) public override void ProfilerMarkEnd(int id)
{ {
#if !UNITY_EDITOR
var color = Console.ForegroundColor; var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.DarkGray;
_stopwatchs[id].Stop(); _stopwatchs[id].stopwatch.Stop();
var time = _stopwatchs[id].Elapsed; var time = _stopwatchs[id].stopwatch.Elapsed;
_stopwatchs[id].Reset(); _stopwatchs[id].stopwatch.Reset();
Print("ProfilerMark", $"> {_stopwatchsNames[id]} s:{time.TotalSeconds}");
Console.Write(PROFILER_MARKER_CACHE);
Console.Write("> ");
Console.Write(_stopwatchs[id].name);
Console.Write(" s:");
int written = 0;
if (_buffer == null) { _buffer = new char[128]; }
ConvertDoubleToText(time.TotalSeconds, _buffer, ref written);
Console.WriteLine(_buffer, 0, written);
Console.ForegroundColor = color; Console.ForegroundColor = color;
#endif
} }
protected override void OnDelProfilerMark(int id) protected override void OnDelProfilerMark(int id)
{ {
_stopwatchs[id] = null; _stopwatchs[id] = default;
} }
protected override void OnNewProfilerMark(int id, string name) protected override void OnNewProfilerMark(int id, string name)
{ {
if (id >= _stopwatchs.Length) if (id >= _stopwatchs.Length)
{ {
Array.Resize(ref _stopwatchs, _stopwatchs.Length << 1); Array.Resize(ref _stopwatchs, _stopwatchs.Length << 1);
Array.Resize(ref _stopwatchsNames, _stopwatchsNames.Length << 1);
} }
_stopwatchs[id] = new Stopwatch(); _stopwatchs[id] = new MarkerData(new Stopwatch(), name);
_stopwatchsNames[id] = name; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void ConvertDoubleToText(double value, char[] stringBuffer, ref int written)
{
int bufferLength = stringBuffer.Length - 1;
decimal decimalValue = (decimal)value;
int intValue = (int)decimalValue;
decimal decimalPartValue = decimalValue - intValue;
int index = written;
if (intValue == 0)
{
stringBuffer[index++] = '0';
}
else
{
while (intValue > 0)
{
int digit = intValue % 10;
stringBuffer[index++] = (char)('0' + digit);
intValue /= 10;
}
Array.Reverse(stringBuffer, 0, index);
}
if (decimalPartValue != 0)
{
stringBuffer[index++] = '.';
}
int pathBufferLength = bufferLength - index;
int zeroPartLength = 0;
for (int i = 0; i < pathBufferLength; i++)
{
decimalPartValue = 10 * decimalPartValue;
int digit = (int)decimalPartValue;
if (digit == 0)
{
zeroPartLength++;
}
else
{
zeroPartLength = 0;
}
stringBuffer[index++] = (char)('0' + digit);
decimalPartValue -= digit;
}
written = bufferLength - zeroPartLength;
} }
} }
} }