Port the whole Mediapipe.Net.External

This commit is contained in:
Speykious
2022-01-15 07:19:22 +01:00
parent d63938ab94
commit 13ba342293
4 changed files with 187 additions and 0 deletions

105
Mediapipe.Net/External/Glog.cs vendored Normal file
View File

@ -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);
}
}

12
Mediapipe.Net/External/Protobuf.cs vendored Normal file
View File

@ -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.
}
}

View File

@ -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<T> Deserialize<T>(MessageParser<T> parser) where T : IMessage<T>
{
var protos = new List<T>(Size);
unsafe
{
var protoPtr = (SerializedProto*)Data;
for (var i = 0; i < Size; i++)
{
var serializedProto = Marshal.PtrToStructure<SerializedProto>((IntPtr)protoPtr++);
protos.Add(serializedProto.Deserialize(parser));
}
}
return protos;
}
}
}

29
Mediapipe.Net/External/StdString.cs vendored Normal file
View File

@ -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);
}
}
}