コード例 #1
0
        private static bool TryReadFile(string filePath, out Constants constants)
        {
            constants = null;

            try
            {
                var    encoding = new UTF8Encoding(true);
                string serialized;

                using (var file = File.OpenRead(filePath))
                {
                    serialized = encoding.GetString(file.ReadAll());
                }

                constants = new DeserializerBuilder()
                            .WithNamingConvention(NullNamingConvention.Instance)
                            .WithTypeResolver(new DynamicTypeResolver())
                            .Build()
                            .Deserialize <Constants>(serialized);

                Console.WriteLine($"\nUSED PHYSICAL PARAMETERS FOR: {Path.GetFileNameWithoutExtension(filePath)}");
                foreach (var prop in constants.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    Console.WriteLine($"{prop.Name}: {prop.GetValue(constants)}");
                }

                return(true);
            }
            catch (YamlDotNet.Core.YamlException)
            {
                Console.WriteLine($"Config file wrong. Please check '{Path.GetFileNameWithoutExtension(filePath)}.yaml'");

                return(false);
            }
        }
コード例 #2
0
        static ManagedGameObjectIO()
        {
            var serializer_ = new SerializerBuilder();

            var typeInspectorFactories_ = serializer_.GetType().GetField("typeInspectorFactories", BindingFlags.NonPublic | BindingFlags.Instance);
            var typeInspectorFactories  = typeInspectorFactories_.GetValue(serializer_);


            var Add = typeInspectorFactories.GetType().GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);

            Add.Invoke(typeInspectorFactories, new object[] {
                typeof(YamlAttributesTypeInspector),
                new Func <ITypeInspector, ITypeInspector>((ITypeInspector inner) => new YamlAttributesTypeInspector(inner))
            });

            var Remove = typeInspectorFactories.GetType().GetMethod("Remove", BindingFlags.Public | BindingFlags.Instance);

            Remove.Invoke(typeInspectorFactories, new object[] {
                typeof(YamlDotNet.Serialization.YamlAttributesTypeInspector)
            });

#if DEBUG
            var _namingConvention = serializer_.GetType().GetField("namingConvention", BindingFlags.NonPublic | BindingFlags.Instance);
            var namingConvention  = _namingConvention.GetValue(serializer_) as INamingConvention;
            var objectGraphTraversalStrategyFactory = (ObjectGraphTraversalStrategyFactory)((ITypeInspector typeInspector, ITypeResolver typeResolver, IEnumerable <IYamlTypeConverter> typeConverters, int maximumRecursion) => new Debug_TraversalStrategy(typeInspector, typeResolver, maximumRecursion, namingConvention));
            serializer_.WithObjectGraphTraversalStrategyFactory(objectGraphTraversalStrategyFactory);
#endif

            m_Serializer = serializer_.Build();

            var deserializer_      = new DeserializerBuilder();
            var BuildTypeInspector = deserializer_.GetType().GetMethod("BuildTypeInspector", BindingFlags.NonPublic | BindingFlags.Instance);
            deserializer_.WithNodeDeserializer(new ManagedGameObject_Deserializer(BuildTypeInspector.Invoke(deserializer_, new object[] { }) as ITypeInspector));
            m_Deserializer = deserializer_.Build();
        }