From c62661b575491ea420d7be275d582bf3545e8476 Mon Sep 17 00:00:00 2001 From: Speykious Date: Sat, 15 Jan 2022 04:39:47 +0100 Subject: [PATCH] Port `SerializedProto` --- Mediapipe.Net/External/SerializedProto.cs | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Mediapipe.Net/External/SerializedProto.cs diff --git a/Mediapipe.Net/External/SerializedProto.cs b/Mediapipe.Net/External/SerializedProto.cs new file mode 100644 index 0000000..63b07fa --- /dev/null +++ b/Mediapipe.Net/External/SerializedProto.cs @@ -0,0 +1,28 @@ +// 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.Runtime.InteropServices; +using Google.Protobuf; +using Mediapipe.Net.Native; + +namespace Mediapipe.Net.External; + +[StructLayout(LayoutKind.Sequential)] +internal struct SerializedProto +{ + public IntPtr StrPtr; + public int Length; + + // TODO: That Dispose() method is looking very sus... + // Might wanna investigate if it's better as a child of Disposable. + public void Dispose() => UnsafeNativeMethods.delete_array__PKc(StrPtr); + + public T Deserialize(MessageParser parser) where T : IMessage + { + byte[] bytes = new byte[Length]; + Marshal.Copy(StrPtr, bytes, 0, bytes.Length); + return parser.ParseFrom(bytes); + } +}