Files
stgsharp/源代码/StgSharpDebug/Environment/Platform.cs
T

141 lines
4.0 KiB
C#

using StgSharp.Envitronment;
using StgSharp.Envitronment.Win32;
using StgSharp.Graphics;
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Win32.UI.WindowsAndMessaging;
#if Windows
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.OpenGL;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI;
#endif
namespace StgSharp
{
internal enum GraphicAPI : byte
{
GL,
DX,
VK
}
public static partial class Platform
{
internal static GraphicAPI API = default;
private static uint markCount = 0;
public static void Mark(bool needConsole)
{
markCount++;
if (needConsole)
{
Console.WriteLine(markCount);
}
}
/// <summary>
/// Version of current StgSharp platform.
/// Main version represent in hundred's digit,
/// and sub version represent in ten's digit.
/// </summary>
public const long version = 430;
/// <summary>
/// Init an instance of OpenGL program,
/// This method should be called before other
/// graphic methods, after LoadGL method.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InitGL(int majorVersion, int minorVersion)
{
if (API != default)
{
return;
}
InternalIO.InternalInitGL(majorVersion, minorVersion);
}
/// <summary>
/// Write a log message to log file, and mark the level of this message.
/// </summary>
/// <param name="message">A string contains information of how current evironment runs.</param>
/// <param name="logType">The severity of current message.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteLog(string message, LogType logType)
{
InternalIO.InternalWriteLog(message, logType);
}
internal static TDele ConvertAPI<TDele>(IntPtr funcPtr)
{
if (funcPtr == IntPtr.Zero)
{
throw new Exception("Failed to convert api, the function pointer is zero.");
}
try
{
return Marshal.GetDelegateForFunctionPointer<TDele>(funcPtr);
}
catch (Exception)
{
throw;
}
}
internal static Delegate ConvertAPI(IntPtr funcPtr, Type T)
{
if (funcPtr == IntPtr.Zero)
{
throw new Exception("Failed to convert api, the function pointer is zero.");
}
try
{
return Marshal.GetDelegateForFunctionPointer(funcPtr, T);
}
catch (Exception)
{
throw;
}
}
public static void Track(
object target,
string message,
[CallerFilePath] string filePath = "",
[CallerMemberName] string callerName = "",
[CallerLineNumber] int lineNumber = 0
)
{
string log = $"Tracked object: {nameof(target)}\n";
Type t = target.GetType();
FieldInfo[] fieldInfo = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
log += "|\tFieldName\t|\tFieldType\t|\tFieldValue\t|\n";
foreach (FieldInfo field in fieldInfo)
{
log += $"|{field.Name}\t\t|{field.FieldType}\t\t|{field.GetValue(target)}\t\t|\n";
}
log += $"Tracking at:\n{filePath}\t{callerName}\tline {lineNumber}\n";
log += $"Additional information:\n{message}\n";
InternalIO.InternalWriteLog(log, LogType.Info);
}
}
}