From e58408e142e8aec45dd90351bd021e42c54204a5 Mon Sep 17 00:00:00 2001 From: Junrou Nishida Date: Sun, 17 Apr 2022 17:54:16 +0900 Subject: [PATCH] fix: constructor should throw if it fails to initialize (#533) --- .../Scripts/Framework/CalculatorGraph.cs | 10 +++----- .../CalculatorGraphConfigExtension.cs | 24 +++++++++++++++++++ .../CalculatorGraphConfigExtension.cs.meta | 11 +++++++++ .../Framework/CalculatorGraph_Unsafe.cs | 3 --- .../CalculatorGraphConfigExtension.cs | 11 --------- mediapipe_api/framework/calculator_graph.cc | 9 ------- mediapipe_api/framework/calculator_graph.h | 1 - 7 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs.meta diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraph.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraph.cs index 6c94879e9..7d26bee12 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraph.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraph.cs @@ -22,13 +22,7 @@ public CalculatorGraph() : base() this.ptr = ptr; } - public CalculatorGraph(string textFormatConfig) : base() - { - UnsafeNativeMethods.mp_CalculatorGraph__PKc(textFormatConfig, out var ptr).Assert(); - this.ptr = ptr; - } - - public CalculatorGraph(byte[] serializedConfig) : base() + private CalculatorGraph(byte[] serializedConfig) : base() { UnsafeNativeMethods.mp_CalculatorGraph__PKc_i(serializedConfig, serializedConfig.Length, out var ptr).Assert(); this.ptr = ptr; @@ -36,6 +30,8 @@ public CalculatorGraph(byte[] serializedConfig) : base() public CalculatorGraph(CalculatorGraphConfig config) : this(config.ToByteArray()) { } + public CalculatorGraph(string textFormatConfig) : this(CalculatorGraphConfig.Parser.ParseFromTextFormat(textFormatConfig)) { } + protected override void DeleteMpPtr() { UnsafeNativeMethods.mp_CalculatorGraph__delete(ptr); diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs new file mode 100644 index 000000000..728ba0549 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs @@ -0,0 +1,24 @@ +// Copyright (c) 2021 homuler +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. + +using pb = Google.Protobuf; + +namespace Mediapipe +{ + public static class CalculatorGraphConfigExtension + { + public static CalculatorGraphConfig ParseFromTextFormat(this pb::MessageParser _, string configText) + { + if (UnsafeNativeMethods.mp_api__ConvertFromCalculatorGraphConfigTextFormat(configText, out var serializedProto)) + { + var config = serializedProto.Deserialize(CalculatorGraphConfig.Parser); + serializedProto.Dispose(); + return config; + } + throw new MediaPipeException("Failed to parse config text. See error logs for more details"); + } + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs.meta new file mode 100644 index 000000000..b478d0494 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/CalculatorGraphConfigExtension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5d17ea3a25da1d9e79b6121a228e2a99 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs index 0351009ec..3e43fbfce 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs @@ -14,9 +14,6 @@ internal static partial class UnsafeNativeMethods [DllImport(MediaPipeLibrary, ExactSpelling = true)] public static extern MpReturnCode mp_CalculatorGraph__(out IntPtr graph); - [DllImport(MediaPipeLibrary, ExactSpelling = true)] - public static extern MpReturnCode mp_CalculatorGraph__PKc(string textFormatConfig, out IntPtr graph); - [DllImport(MediaPipeLibrary, ExactSpelling = true)] public static extern MpReturnCode mp_CalculatorGraph__PKc_i(byte[] serializedConfig, int size, out IntPtr graph); diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Extension/CalculatorGraphConfigExtension.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Extension/CalculatorGraphConfigExtension.cs index 7729b4f8f..39f701669 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Extension/CalculatorGraphConfigExtension.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Extension/CalculatorGraphConfigExtension.cs @@ -10,17 +10,6 @@ namespace Mediapipe.Unity { public static class CalculatorGraphConfigExtension { - public static CalculatorGraphConfig ParseFromTextFormat(this pb::MessageParser _, string configText) - { - if (UnsafeNativeMethods.mp_api__ConvertFromCalculatorGraphConfigTextFormat(configText, out var serializedProto)) - { - var config = serializedProto.Deserialize(CalculatorGraphConfig.Parser); - serializedProto.Dispose(); - return config; - } - throw new MediaPipeException("Failed to parse config text. See error logs for more details"); - } - public static string AddPacketPresenceCalculator(this CalculatorGraphConfig config, string outputStreamName) { var presenceStreamName = Tool.GetUnusedStreamName(config, $"{outputStreamName}_presence"); diff --git a/mediapipe_api/framework/calculator_graph.cc b/mediapipe_api/framework/calculator_graph.cc index 1f888fd9a..93a6e737e 100644 --- a/mediapipe_api/framework/calculator_graph.cc +++ b/mediapipe_api/framework/calculator_graph.cc @@ -24,15 +24,6 @@ MpReturnCode mp_CalculatorGraph__(mediapipe::CalculatorGraph** graph_out) { void mp_CalculatorGraph__delete(mediapipe::CalculatorGraph* graph) { delete graph; } -MpReturnCode mp_CalculatorGraph__PKc(const char* text_format_config, mediapipe::CalculatorGraph** graph_out) { - TRY_ALL - mediapipe::CalculatorGraphConfig config; - auto result = google::protobuf::TextFormat::ParseFromString(text_format_config, &config); - *graph_out = result ? new mediapipe::CalculatorGraph(config) : nullptr; - RETURN_CODE(MpReturnCode::Success); - CATCH_ALL -} - MpReturnCode mp_CalculatorGraph__PKc_i(const char* serialized_config, int size, mediapipe::CalculatorGraph** graph_out) { TRY_ALL auto config = ParseFromStringAsCalculatorGraphConfig(serialized_config, size); diff --git a/mediapipe_api/framework/calculator_graph.h b/mediapipe_api/framework/calculator_graph.h index cea5cf957..7e9273cb1 100644 --- a/mediapipe_api/framework/calculator_graph.h +++ b/mediapipe_api/framework/calculator_graph.h @@ -29,7 +29,6 @@ typedef std::map SidePackets; typedef absl::Status* NativePacketCallback(mediapipe::CalculatorGraph* graph, int stream_id, const mediapipe::Packet&); MP_CAPI(MpReturnCode) mp_CalculatorGraph__(mediapipe::CalculatorGraph** graph_out); -MP_CAPI(MpReturnCode) mp_CalculatorGraph__PKc(const char* text_format_config, mediapipe::CalculatorGraph** graph_out); MP_CAPI(MpReturnCode) mp_CalculatorGraph__PKc_i(const char* serialized_config, int size, mediapipe::CalculatorGraph** graph_out); MP_CAPI(void) mp_CalculatorGraph__delete(mediapipe::CalculatorGraph* graph);