public static object GetMessage(FunMessage msg, MessageType msg_type) { object _msg = null; bool success = Extensible.TryGetValue(serializer_, MessageTable.GetType(msg_type), msg, (int)msg_type, DataFormat.Default, true, out _msg); if (!success) { FunDebug.Log("Failed to decode {0} {1}", MessageTable.GetType(msg_type), (int)msg_type); return(null); } return(_msg); }
static void TestReadExt <T>() where T : IExtTest, IExtensible, new() { BiggerObject obj = GetBigObject(); T small = Serializer.ChangeType <BiggerObject, T>(obj); _ = GetExtensionBytes(small); bool hasValue = Extensible.TryGetValue <float>(small, 3, out var val); Assert.True(hasValue); //, "has value"); Assert.Equal(obj.SomeFloat, val); //, "float value"); hasValue = Extensible.TryGetValue <float>(small, 1000, out val); Assert.False(hasValue); //, "no value"); Assert.Equal(default, val);
private void DumpOptionsFieldRecursive(FieldDescriptorProto field, IExtensible options, IDictionary <string, string> optionsKv, string path) { var key = string.IsNullOrEmpty(path) ? $"({field.name})" : $"{path}.{field.name}"; if (IsNamedType(field.type) && !string.IsNullOrEmpty(field.type_name)) { var fieldData = _protobufTypeMap[field.type_name].Source; switch (fieldData) { case EnumDescriptorProto enumProto: { if (Extensible.TryGetValue(options, field.number, out int idx)) { var value = enumProto.value.Find(x => x.number == idx); optionsKv.Add(key, value.name); } break; } case DescriptorProto messageProto: { var extension = Extensible.GetValue <ExtensionPlaceholder>(options, field.number); if (extension != null) { foreach (var subField in messageProto.field) { DumpOptionsFieldRecursive(subField, extension, optionsKv, key); } } break; } } } else { if (ExtractType(options, field, out var value)) { optionsKv.Add(key, value); } } }
public void TestDeserializeUndefinedEnumWithoutDefault() { // check we can deserialize with the extra data var see = Program.Build <SomeEnumEntityWithoutDefault>(0x10, 0x09); // shouldn't set the property Assert.IsFalse(see.Enum.HasValue); // data should be available via extension API int val; Assert.IsTrue(Extensible.TryGetValue <int>(see, 2, DataFormat.Default, true, out val)); Assert.AreEqual(9, val); // and check it re-serializes OK Program.CheckBytes(see, 0x10, 0x09); }
public static void DebugString(FunDedicatedServerRpcMessage msg, StringBuilder log) { foreach (MessageType msg_type in Enum.GetValues(typeof(MessageType))) { object _msg = null; Type type = MessageTable.GetType(msg_type); bool succeed = Extensible.TryGetValue(serializer_, type, msg, (int)msg_type, DataFormat.Default, true, out _msg); if (succeed) { var json = JObject.FromObject(_msg); log.Append(prettyJsonString(json)); debugString((IExtensible)_msg, type, log); } } }
public void CanParseCustomOptionsFromExternalSchema() { var set = new FileDescriptorSet(); set.AddImportPath("./Schemas"); Assert.True(set.Add("nanopb_test.proto")); set.Process(); Assert.Empty(set.GetErrors()); var bar = set.Files.Single(x => x.Name == "nanopb_test.proto") .MessageTypes.Single() .Fields.Single(x => x.Number == 3); // normally you'd just use an "if (Extensible.TryGetValue(...)" here; I'm proving it for the test Assert.True(Extensible.TryGetValue <NanoPBOptions>(RuntimeTypeModel.Default, bar.Options, 1010, out var options)); Assert.True(options.ShouldSerializeMaxSize()); // this is "actively set" vs "set via the default" etc Assert.Equal(42, options.MaxSize); }
public static T GetMessage <T> (FunDedicatedServerRpcMessage msg, MessageType msg_type) { try { object _msg = null; Extensible.TryGetValue(serializer_, MessageTable.GetType(msg_type), msg, (int)msg_type, DataFormat.Default, true, out _msg); FunDebug.Assert(_msg != null, "TryGetValue() failed. Please check the message type."); return((T)_msg); } catch (Exception e) { Type type = MessageTable.GetType(msg_type); FunDebug.LogError("FunapiDSRpcMessage.GetMessage - Failed to decode '{0}' ({1})\n{2}", type, msg_type, e.ToString()); } return(default(T)); }
static void TestReadExt <T>() where T : IExtTest, IExtensible, new() { BiggerObject obj = GetBigObject(); T small = Serializer.ChangeType <BiggerObject, T>(obj); var tm = TypeModel.Create(false, ProtoCompatibilitySettingsValue.FullCompatibility); small = tm.ChangeType <BiggerObject, T>(obj); byte[] raw = GetExtensionBytes(small); float val; bool hasValue = Extensible.TryGetValue <float>(tm, small, 3, out val); Assert.IsTrue(hasValue, "has value"); Assert.AreEqual(obj.SomeFloat, val, "float value"); hasValue = Extensible.TryGetValue <float>(tm, small, 1000, out val); Assert.IsFalse(hasValue, "no value"); Assert.AreEqual(default(float), val); }
static void debugString(IExtensible msg, Type type, StringBuilder log) { MethodInfo method = typeof(MessageTable).GetMethod("GetExtensions"); if (method != null) { Dictionary <int, Type> extensions = method.Invoke(null, new object[] { type }) as Dictionary <int, Type>; foreach (KeyValuePair <int, Type> pair in extensions) { object _msg = null; bool succeed = Extensible.TryGetValue(serializer_, pair.Value, msg, pair.Key, DataFormat.Default, true, out _msg); if (succeed) { var json = JObject.FromObject(_msg); log.Append(prettyJsonString(json)); debugString((IExtensible)_msg, pair.Value, log); } } } }
private static bool ExtractType(IExtensible data, FieldDescriptorProto field, out string value) { switch (field.type) { case FieldDescriptorProto.Type.TYPE_INT32: case FieldDescriptorProto.Type.TYPE_UINT32: case FieldDescriptorProto.Type.TYPE_FIXED32: if (Extensible.TryGetValue(data, field.number, out uint int32)) { value = Convert.ToString(int32); return(true); } break; case FieldDescriptorProto.Type.TYPE_INT64: case FieldDescriptorProto.Type.TYPE_UINT64: case FieldDescriptorProto.Type.TYPE_FIXED64: if (Extensible.TryGetValue(data, field.number, out ulong int64)) { value = Convert.ToString(int64); return(true); } break; case FieldDescriptorProto.Type.TYPE_SINT32: case FieldDescriptorProto.Type.TYPE_SFIXED32: if (Extensible.TryGetValue(data, field.number, out int sint32)) { value = Convert.ToString(sint32); return(true); } break; case FieldDescriptorProto.Type.TYPE_SINT64: case FieldDescriptorProto.Type.TYPE_SFIXED64: if (Extensible.TryGetValue(data, field.number, out long sint64)) { value = Convert.ToString(sint64); return(true); } break; case FieldDescriptorProto.Type.TYPE_STRING: if (Extensible.TryGetValue(data, field.number, out string str)) { value = $"\"{str}\""; return(true); } break; case FieldDescriptorProto.Type.TYPE_BOOL: if (Extensible.TryGetValue(data, field.number, out bool boolean)) { value = boolean ? "true" : "false"; return(true); } break; case FieldDescriptorProto.Type.TYPE_BYTES: if (Extensible.TryGetValue(data, field.number, out byte[] bytes))