Files
stgsharp/源代码/StgSharp/Control/IO/InternalIO.cs
T

127 lines
3.5 KiB
C#

using StgSharp.Geometries;
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace StgSharp
{
public enum LogType
{
Error,
Warning,
Info,
InfoError,
InfoWarning,
}
internal class LibHandle : SafeHandle
{
public LibHandle(IntPtr invalidHandleValue, bool ownsHandle) : base(invalidHandleValue, ownsHandle)
{
}
public override bool IsInvalid => throw new NotImplementedException();
protected override bool ReleaseHandle()
{
return PInvoke.FreeLibrary(new HMODULE(handle));
}
}
internal static unsafe partial class InternalIO
{
internal static Dictionary<TypeCode, uint> GLtype =
new Dictionary<TypeCode, uint>();
internal static IntPtr SSClibPtr = IntPtr.Zero;
internal const string SS_errorlog =
"SS_errorlog.log";
internal const string SSC_libname =
"StgSharpC";
static InternalIO()
{
GLtype.Add(TypeCode.Single, GLconst.FLOAT);
GLtype.Add(TypeCode.Int32, GLconst.INT);
GLtype.Add(TypeCode.UInt32, GLconst.UNSIGNED_INT);
GLtype.Add(TypeCode.Int16, GLconst.SHORT);
GLtype.Add(TypeCode.UInt16, GLconst.UNSIGNED_SHORT);
GLtype.Add(TypeCode.SByte, GLconst.BYTE);
GLtype.Add(TypeCode.Byte, GLconst.UNSIGNED_BYTE);
}
internal static T InternalLoadProc<T>(LibHandle lib, string name) where T : Delegate
{
try
{
IntPtr procPtr =
#if Windows
PInvoke.GetProcAddress(lib, name);
#elif Linux
#endif
T ret = Marshal.GetDelegateForFunctionPointer<T>(procPtr);
return ret;
}
catch (Exception)
{
throw;
}
}
internal static void InternalAppendLog(string log)
{
if (!File.Exists(SS_errorlog))
{
FileStream stream = File.Create(SS_errorlog);
stream.Close();
}
using (StreamWriter logStream = File.AppendText(SS_errorlog))
{
logStream.WriteLine(log);
}
}
internal static void InternalWriteLog(string beforeTime, string logLine, LogType logType)
{
if (!File.Exists(InternalIO.SS_errorlog))
{
FileStream stream = File.Create(InternalIO.SS_errorlog);
stream.Close();
}
using (StreamWriter logStream = File.AppendText(SS_errorlog))
{
logStream.WriteLine(beforeTime);
logStream.WriteLine($"{logType}\t@\t{DateTime.Now.ToString("o")}");
logStream.WriteLine(logLine);
}
}
internal static void InternalWriteLog(string logLine, LogType logType)
{
if (!File.Exists(SS_errorlog))
{
FileStream stream = File.Create(SS_errorlog);
stream.Close();
}
using (StreamWriter logStream = File.AppendText(SS_errorlog))
{
logStream.WriteLine($"{logType}\t@\t{DateTime.Now.ToString("o")}");
logStream.WriteLine(logLine);
}
}
}
}