diff --git a/src/client/spark_core/Protobuf/ProtobufSerializationHelper.cs b/src/client/spark_core/Protobuf/ProtobufSerializationHelper.cs index a33a8636d..987696e49 100644 --- a/src/client/spark_core/Protobuf/ProtobufSerializationHelper.cs +++ b/src/client/spark_core/Protobuf/ProtobufSerializationHelper.cs @@ -11,6 +11,12 @@ namespace Spark.Core.Protobuf { using System.IO; public static class ProtobufSerializationHelper { + public static T deserialize_proto3(byte[] bytes) where T : IMessage, new() { + var rv = new T(); + rv.MergeFrom(bytes); + return rv; + } + public static bool try_deserialize(Stream stream, [NotNullWhen(true)] out T? result, bool log_exception = true) { try { result = deserialize(stream); @@ -41,6 +47,20 @@ namespace Spark.Core.Protobuf { return try_deserialize(ms, out result); } + public static bool try_deserialize_proto3( + byte[] bytes, [NotNullWhen(true)] out T? result, bool log_exception = true) where T : IMessage, new() { + try { + result = deserialize_proto3(bytes); + return true; + } catch (Exception ex) { + if (log_exception) { + Log.Gen.warn($"Failed to deserialize protobuf of type {typeof(T)}: {ex}"); + } + } + result = default; + return false; + } + public static void serialize(Stream stream, T t) => Serializer.SerializeWithLengthPrefix(stream, t, PrefixStyle.Fixed32); diff --git a/src/client/spark_core/config/Configuration.cs b/src/client/spark_core/config/Configuration.cs index bb7653a2f..a0a4d2f64 100644 --- a/src/client/spark_core/config/Configuration.cs +++ b/src/client/spark_core/config/Configuration.cs @@ -2,7 +2,7 @@ namespace mbts.config { using bts; - using Spark.Core.Protobuf; + using Google.Protobuf; using System; using System.Diagnostics.Contracts; @@ -34,11 +34,20 @@ namespace mbts.config { using var ms = new MemoryStream(buffer); try { - return ProtoBuf.Serializer.Deserialize(ms); + return typeof(IMessage).IsAssignableFrom(typeof(T)) ? proto3_impl() : proto2_impl(); } catch (Exception ex) { Log.assert(false, _fmt_deserialization_error(typeof(T), key, ex)); } return new(); + + T proto2_impl() => + ProtoBuf.Serializer.Deserialize(ms); + + T proto3_impl() { + var rv = new T(); + ((IMessage)rv).MergeFrom(ms); + return rv; + } } public static void initialize() { @@ -75,7 +84,7 @@ namespace mbts.config { return true; } - [DllImport("libspark", CharSet = CharSet.Unicode)] + [DllImport("libspark")] internal static extern void configcheck(int count, string[] args); [Pure] @@ -105,13 +114,13 @@ namespace mbts.config { } } - [DllImport("libspark", EntryPoint = "get_buffer", CharSet = CharSet.Unicode)] + [DllImport("libspark", EntryPoint = "get_buffer")] static extern IntPtr _raw_get_buffer(string key_name, ref int length); - [DllImport("libspark", EntryPoint = "initialize_nothrow", CharSet = CharSet.Unicode)] + [DllImport("libspark", EntryPoint = "initialize_nothrow")] static extern bool _initialize_nothrow(StringBuilder buffer, int length); - [DllImport("libspark", EntryPoint = "initialize_for_test", CharSet = CharSet.Unicode)] + [DllImport("libspark", EntryPoint = "initialize_for_test")] static extern void _raw_initialize_for_test(string buffer); [DllImport("libspark", EntryPoint = "reset")]