diff --git a/Mediapipe.Net/External/Glog.cs b/Mediapipe.Net/External/Glog.cs new file mode 100644 index 0000000..a5da3f6 --- /dev/null +++ b/Mediapipe.Net/External/Glog.cs @@ -0,0 +1,105 @@ +// Copyright (c) homuler and Vignette +// This file is part of MediaPipe.NET. +// MediaPipe.NET is licensed under the MIT License. See LICENSE for details. + +using System; +using Mediapipe.Net.Native; + +namespace Mediapipe.Net.External +{ + public static class Glog + { + public enum Severity : int + { + Info = 0, + Warning = 1, + Error = 2, + Fatal = 3, + } + + private static bool logToStderr = false; + public static bool LogToStderr + { + get => logToStderr; + set + { + UnsafeNativeMethods.glog_FLAGS_logtostderr(value); + logToStderr = value; + } + } + + private static int stderrThreshold = 2; + public static int StderrThreshold + { + get => stderrThreshold; + set + { + UnsafeNativeMethods.glog_FLAGS_stderrthreshold(value); + stderrThreshold = value; + } + } + + private static int minLogLevel = 0; + public static int MinLogLevel + { + get => minLogLevel; + set + { + UnsafeNativeMethods.glog_FLAGS_minloglevel(value); + minLogLevel = value; + } + } + + private static string? logDir; + public static string? LogDir + { + get => logDir; + set + { + UnsafeNativeMethods.glog_FLAGS_log_dir(value ?? ""); + logDir = value; + } + } + + private static int v = 0; + public static int V + { + get => v; + set + { + UnsafeNativeMethods.glog_FLAGS_v(value); + v = value; + } + } + + public static void Initialize(string name) + => UnsafeNativeMethods.google_InitGoogleLogging__PKc(name).Assert(); + + public static void Shutdown() + => UnsafeNativeMethods.google_ShutdownGoogleLogging().Assert(); + + public static void Log(Severity severity, string str) + { + switch (severity) + { + case Severity.Info: + UnsafeNativeMethods.glog_LOG_INFO__PKc(str); + break; + case Severity.Warning: + UnsafeNativeMethods.glog_LOG_WARNING__PKc(str); + break; + case Severity.Error: + UnsafeNativeMethods.glog_LOG_ERROR__PKc(str); + break; + case Severity.Fatal: + UnsafeNativeMethods.glog_LOG_FATAL__PKc(str); + break; + default: + throw new ArgumentException($"Unknown Severity: {severity}"); + } + } + + public static void FlushLogFiles(Severity severity = Severity.Info) + => UnsafeNativeMethods.google_FlushLogFiles(severity); + } +} diff --git a/Mediapipe.Net/External/Protobuf.cs b/Mediapipe.Net/External/Protobuf.cs new file mode 100644 index 0000000..6e9d1b9 --- /dev/null +++ b/Mediapipe.Net/External/Protobuf.cs @@ -0,0 +1,12 @@ +// Copyright (c) homuler and Vignette +// This file is part of MediaPipe.NET. +// MediaPipe.NET is licensed under the MIT License. See LICENSE for details. + +namespace Mediapipe.Net.External +{ + internal static class Protobuf + { + public delegate void ProtobufLogHandler(int level, string filename, int line, string message); + // TODO: (from homuler) Overwrite protobuf logger to show logs in Console Window. + } +} diff --git a/Mediapipe.Net/External/SerializedProtoVector.cs b/Mediapipe.Net/External/SerializedProtoVector.cs new file mode 100644 index 0000000..3c35ac5 --- /dev/null +++ b/Mediapipe.Net/External/SerializedProtoVector.cs @@ -0,0 +1,41 @@ +// Copyright (c) homuler and Vignette +// This file is part of MediaPipe.NET. +// MediaPipe.NET is licensed under the MIT License. See LICENSE for details. + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using Mediapipe.Net.Native; +using Google.Protobuf; + +namespace Mediapipe.Net.External +{ + [StructLayout(LayoutKind.Sequential)] + internal struct SerializedProtoVector + { + public IntPtr Data; + public int Size; + + // TODO: This is looking just as sus as SerializedProto.Dispose(). + // Should be investigated in the same way. + public void Dispose() => UnsafeNativeMethods.mp_api_SerializedProtoArray__delete(Data); + + public List Deserialize(MessageParser parser) where T : IMessage + { + var protos = new List(Size); + + unsafe + { + var protoPtr = (SerializedProto*)Data; + + for (var i = 0; i < Size; i++) + { + var serializedProto = Marshal.PtrToStructure((IntPtr)protoPtr++); + protos.Add(serializedProto.Deserialize(parser)); + } + } + + return protos; + } + } +} diff --git a/Mediapipe.Net/External/StdString.cs b/Mediapipe.Net/External/StdString.cs new file mode 100644 index 0000000..b5098c4 --- /dev/null +++ b/Mediapipe.Net/External/StdString.cs @@ -0,0 +1,29 @@ +// Copyright (c) homuler and Vignette +// This file is part of MediaPipe.NET. +// MediaPipe.NET is licensed under the MIT License. See LICENSE for details. + +using System; +using Mediapipe.Net.Core; +using Mediapipe.Net.Native; + +namespace Mediapipe.Net.External +{ + public class StdString : MpResourceHandle + { + public StdString(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { } + + public StdString(byte[] bytes) : base() + { + UnsafeNativeMethods.std_string__PKc_i(bytes, bytes.Length, out var ptr).Assert(); + Ptr = ptr; + } + + protected override void DeleteMpPtr() => UnsafeNativeMethods.std_string__delete(Ptr); + + public void Swap(StdString str) + { + UnsafeNativeMethods.std_string__swap__Rstr(MpPtr, str.MpPtr); + GC.KeepAlive(this); + } + } +}