diff --git a/src/client/spark/core/Utility/ProtoHelper.cs b/src/client/spark/core/Utility/ProtoHelper.cs index a4bb45107..31d54f208 100644 --- a/src/client/spark/core/Utility/ProtoHelper.cs +++ b/src/client/spark/core/Utility/ProtoHelper.cs @@ -1,115 +1,134 @@ -using mbts; -using Google.Protobuf; -using Google.Protobuf.Reflection; -using mbts.ui.utility; -using System; -using System.Collections.Generic; -using System.Linq; -using mbts.utils; -using bts.api; -using bts.api.orderman; -using System.Reflection; - -namespace bts.utils { - static class ProtoHelper { - const string - _case_suffix = "Case", - _oneof_case_suffix = "OneofCase"; - - public static MessageDescriptor Descriptor() where T : IMessage, new() => - _DescriptorImpl.value; - - public static OneofDescriptor get_oneof_descriptor(string prop_name) where T : IMessage, new() { - prop_name = prop_name.iremove_suffix(_case_suffix); - return Descriptor().Oneofs.FirstOrDefault(d => d.Name.iequals(prop_name)); - } - - public static IReadOnlyDictionary get_oneof_descriptor_index(string prop_name) - where T : IMessage, new() - where TOneofCase : struct => - _DescriptorImpl.OneofImpl.get_descriptor_case_index(prop_name); - - public static FieldDescriptor get_oneof_field_descriptor(TOneofCase oneof_case) where T : IMessage, new() where TOneofCase : struct { - string oneof_name = oneof_case.ToString(); - return Descriptor().Fields.InFieldNumberOrder().FirstOrDefault(fd => fd.Name.Replace("_", "").iequals(oneof_name)); - } - - public static TImmutable immutable_factory(TMutable m) where TMutable : IMessage, new() where TImmutable : IImmutableMessage => - (TImmutable)Activator.CreateInstance(typeof(TImmutable), m); - - public static IMessage mutable_factory(Type type) => - MutableFactoryStore.get(type)(); - - static class MutableFactoryStore { - [ThreadStatic] - static readonly Dictionary> _factories = new Dictionary>(); - - public static Func get(Type type) => - _factories.GetOrAdd(type, - t => typeof(MutableFactory<>).MakeGenericType(t) - .GetField(nameof(MutableFactory.anonymous_factory), BindingFlags.Static | BindingFlags.Public) - .GetValue(null) as Func); - } - - static class MutableFactory where T : IMessage, new() { - public static readonly Func anonymous_factory = () => new T(); - } - - public static Type get_field_type(FieldDescriptor field_descriptor) { - switch (field_descriptor.FieldType) { - case FieldType.Bool: return typeof(bool); - case FieldType.Bytes: return typeof(ByteString); - case FieldType.Double: return typeof(double); - case FieldType.Float: return typeof(float); - case FieldType.UInt32: return typeof(uint); - case FieldType.UInt64: return typeof(ulong); - // int - case FieldType.SFixed32: - case FieldType.Fixed32: - case FieldType.SInt32: - case FieldType.Int32: return typeof(int); - // long - case FieldType.SFixed64: - case FieldType.Fixed64: - case FieldType.SInt64: - case FieldType.Int64: return typeof(long); - // - case FieldType.String: return typeof(string); - case FieldType.Enum: return field_descriptor.EnumType.ClrType; - case FieldType.Message: return field_descriptor.MessageType.ClrType; - default: throw ExceptionBuilder.Argument.unexpected_case(field_descriptor.FieldType, nameof(field_descriptor)); - } - } - - public static IReadOnlyDictionary get_oneof_descriptor_index(OneofDescriptor descriptor) where TOneofCase : struct => - descriptor.Fields.ToDictionary(fd => EnumHelper.parse(fd.Name.Replace("_", ""))); - - static class _DescriptorImpl where T : IMessage, new() { - public static readonly MessageDescriptor value = new T().Descriptor; - - public static class OneofImpl where TOneofCase : struct { - static Dictionary> _descriptor_case_indices = - new Dictionary>(); - - public static IReadOnlyDictionary get_descriptor_case_index(string prop_name) { - /// Use local copy because is updated atomically. - var dci = _descriptor_case_indices; - if (!dci.TryGetValue(prop_name, out IReadOnlyDictionary rv)) { - /// Update atomically (lock-free). - /// 1) clone the local copy. - /// 2) add the new KeyValue to the clone. - /// 3) assign the clone back to . - rv = get_oneof_descriptor_index(get_oneof_descriptor(prop_name)) - .ToDictionary(kv => kv.Key, kv => (kv.Value, get_field_type(kv.Value))); - dci = dci.ToDictionary(); - dci.Add(prop_name, rv); - /// It is possible for add ops to be overwritten. - /// This is perfectly safe, just means that the 'clone-add-assign' will be repeated the next time a caller requests this key. - _descriptor_case_indices = dci; - } - return rv; - } - } - } - } -} +using bts.api; +using bts.api.orderman; +using Google.Protobuf; +using Google.Protobuf.Reflection; +using mbts; +using mbts.ui.utilities; +using mbts.ui.utility; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace bts.utils { + static class ProtoHelper { + const string + _case_suffix = "Case", + _oneof_case_suffix = "OneofCase"; + + public static MessageDescriptor Descriptor() where T : IMessage, new() => + _DescriptorImpl.value; + + public static OneofDescriptor get_oneof_descriptor(string prop_name) where T : IMessage, new() { + prop_name = prop_name.iremove_suffix(_case_suffix); + return Descriptor().Oneofs.FirstOrDefault(d => d.Name.iequals(prop_name)); + } + + public static IReadOnlyDictionary get_oneof_descriptor_index(string prop_name) + where T : IMessage, new() + where TOneofCase : struct => + _DescriptorImpl.OneofImpl.get_descriptor_case_index(prop_name); + + public static FieldDescriptor get_oneof_field_descriptor(TOneofCase oneof_case) where T : IMessage, new() where TOneofCase : struct { + string oneof_name = oneof_case.ToString(); + return Descriptor().Fields.InFieldNumberOrder().FirstOrDefault(fd => fd.Name.Replace("_", "").iequals(oneof_name)); + } + + public static TOneofCase get_oneof_case(OneofDescriptor oneof_descriptor, IMessage value) where TOneofCase : struct { + var fd = oneof_descriptor.Accessor.GetCaseFieldDescriptor(value); + if (fd == null) + return default; + string enum_type_name = DisplayNameHelper.snake_to_title_case(oneof_descriptor.Name) + "OneofCase"; + var enum_type = oneof_descriptor.ContainingType.ClrType.GetNestedType(enum_type_name); + if (enum_type == null) { + var all_types = oneof_descriptor.ContainingType.ClrType.GetNestedTypes(); + enum_type = all_types.FirstOrDefault(t => t.Name == enum_type_name); + if (enum_type == null) + return default; + } + string adj_name = fd.Name.Replace("_", ""); + var result = Enum.Parse(enum_type, adj_name, true); + var converted = ConvertUtils.try_convert(result); + return converted; + } + + public static TImmutable immutable_factory(TMutable m) where TMutable : IMessage, new() where TImmutable : IImmutableMessage => + (TImmutable)Activator.CreateInstance(typeof(TImmutable), m); + + public static IMessage mutable_factory(Type type) => + MutableFactoryStore.get(type)(); + + static class MutableFactoryStore { + [ThreadStatic] + static readonly Dictionary> _factories = new Dictionary>(); + + public static Func get(Type type) => + _factories.GetOrAdd(type, + t => typeof(MutableFactory<>).MakeGenericType(t) + .GetField(nameof(MutableFactory.anonymous_factory), BindingFlags.Static | BindingFlags.Public) + .GetValue(null) as Func); + } + + static class MutableFactory where T : IMessage, new() { + public static readonly Func anonymous_factory = () => new T(); + } + + public static Type get_field_type(FieldDescriptor field_descriptor) { + switch (field_descriptor.FieldType) { + case FieldType.Bool: return typeof(bool); + case FieldType.Bytes: return typeof(ByteString); + case FieldType.Double: return typeof(double); + case FieldType.Float: return typeof(float); + case FieldType.UInt32: return typeof(uint); + case FieldType.UInt64: return typeof(ulong); + // int + case FieldType.SFixed32: + case FieldType.Fixed32: + case FieldType.SInt32: + case FieldType.Int32: return typeof(int); + // long + case FieldType.SFixed64: + case FieldType.Fixed64: + case FieldType.SInt64: + case FieldType.Int64: return typeof(long); + // + case FieldType.String: return typeof(string); + case FieldType.Enum: return field_descriptor.EnumType.ClrType; + case FieldType.Message: return field_descriptor.MessageType.ClrType; + default: throw ExceptionBuilder.Argument.unexpected_case(field_descriptor.FieldType, nameof(field_descriptor)); + } + } + + public static IReadOnlyDictionary get_oneof_descriptor_index(OneofDescriptor descriptor) where TOneofCase : struct => + descriptor.Fields.ToDictionary(fd => EnumHelper.parse(fd.Name.Replace("_", ""))); + + static class _DescriptorImpl where T : IMessage, new() { + public static readonly MessageDescriptor value = new T().Descriptor; + + public static class OneofImpl where TOneofCase : struct { + static Dictionary> _descriptor_case_indices = + new Dictionary>(); + + public static IReadOnlyDictionary get_descriptor_case_index(string prop_name) { + /// Use local copy because is updated atomically. + var dci = _descriptor_case_indices; + if (!dci.TryGetValue(prop_name, out IReadOnlyDictionary rv)) { + /// Update atomically (lock-free). + /// 1) clone the local copy. + /// 2) add the new KeyValue to the clone. + /// 3) assign the clone back to . + rv = get_oneof_descriptor_index(get_oneof_descriptor(prop_name)) + .ToDictionary(kv => kv.Key, kv => (kv.Value, get_field_type(kv.Value))); + dci = dci.ToDictionary(); + dci.Add(prop_name, rv); + /// It is possible for add ops to be overwritten. + /// This is perfectly safe, just means that the 'clone-add-assign' will be repeated the next time a caller requests this key. + _descriptor_case_indices = dci; + } + return rv; + } + } + } + } +}