Пример #1
0
    private bool TryReadProperty(string streamName, string dataName, Property property, List <Property> properties, CodeBuilder builder, SyntaxProvider syntaxProvider, Method readMethod = null)
    {
        if (readMethod is null)
        {
            syntaxProvider.Methods.TryGetReadMethod(property, out readMethod);
        }

        var context = new MethodBuildingContext(streamName, dataName, property, properties, builder, readMethod, syntaxProvider.Methods, syntaxProvider.Context);

        if (property.Reading.Execute(context))
        {
            return(true);
        }

        // Attributes behavior
        for (int i = 0; i < property.Attributes.Length; i++)
        {
            if (property.Attributes[i].ModifyDeserialization(context))
            {
                property.Read.Execute(context);
                return(true);
            }
        }

        // Default behavior
        if (readMethod is null)
        {
            ReportMissingMethod(syntaxProvider, property, "deserialization");
            return(false);
        }
        builder.Line($"{dataName} = {streamName}.{readMethod}();");

        property.Read.Execute(context);
        return(true);
    }
Пример #2
0
    private bool TrySerializeProperty(string streamName, Property property, List <Property> properties, CodeBuilder builder, SyntaxProvider syntaxProvider, Method writeMethod = null)
    {
        if (writeMethod is null)
        {
            syntaxProvider.Methods.TryGetWriteMethod(property, out writeMethod);
        }

        var context = new MethodBuildingContext(streamName, property.Name, property, properties, builder, writeMethod, syntaxProvider.Methods, syntaxProvider.Context);

        if (property.Writing.Execute(context))
        {
            return(true);
        }

        // Attributes behavior
        for (int i = 0; i < property.Attributes.Length; i++)
        {
            if (property.Attributes[i].ModifySerialization(context))
            {
                property.Written.Execute(context);
                return(true);
            }
        }

        // Default behavior
        if (writeMethod is null)
        {
            ReportMissingMethod(syntaxProvider, property, "serialization");
            return(false);
        }
        builder.Line($"{streamName}.{writeMethod}({property});");

        property.Written.Execute(context);
        return(true);
    }
Пример #3
0
        public override bool ModifyCollectionPrefixSerialization(MethodBuildingContext context)
        {
            if (!context.MethodsRegistry.TryGetWriteMethod(context.Property.CloneWithType(Type), out Method writeMethod))
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "There is no write method for this type, attribute will be ignored.", syntax);
                return(false);
            }

            context.CodeBuilder.Line($"{context.StreamName}.{writeMethod}(({Type}){context.DataName}.{context.Property.Length});");
            return(true);
        }
Пример #4
0
        public override bool ModifyCollectionPrefixDeserialization(MethodBuildingContext context)
        {
            if (Length < 0)
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "Length must be a non-negative number. Attribute will be ignored.", syntax);
                return(false);
            }

            context.CodeBuilder.Line($"{context.DataName} = {context.Property.NewCollection(Length.ToString())}");
            return(true);
        }
Пример #5
0
        public override bool ModifyCollectionPrefixSerialization(MethodBuildingContext context)
        {
            if (Length < 0)
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "Length must be a non-negative number. Attribute will be ignored.", syntax);
                return(false);
            }

            // Doesn't write length prefix
            return(true);
        }
Пример #6
0
        public override bool ModifyCollectionPrefixDeserialization(MethodBuildingContext context)
        {
            if (!context.MethodsRegistry.TryGetReadMethod(context.Property.CloneWithType(Type), out Method readMethod))
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "There is no read method for this type, attribute will be ignored.", syntax);
                return(false);
            }

            string getLength = $"{context.StreamName}.{readMethod}()";

            context.CodeBuilder.Line($"{context.DataName} = {context.Property.NewCollection(getLength)};");
            return(true);
        }
        public static bool Execute(this PreactionCallback callback, MethodBuildingContext context)
        {
            if (callback is null)
            {
                return(false);
            }

            foreach (PreactionCallback subcallback in callback.GetInvocationList())
            {
                if (subcallback(context))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #8
0
    public override bool ModifyDeserialization(MethodBuildingContext context)
    {
        if (!context.MethodsRegistry.TryGetReadMethod(context.Property.CloneWithType(Type), out Method readMethod))
        {
            DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "There is no read method for this type, attribute will be ignored.", syntax);
            return(false);
        }

        if (context.Property.IsGeneric)
        {
            context.CodeBuilder.Line($"var temp = {context.StreamName}.{readMethod}();");
            context.CodeBuilder.Line($"{context.DataName} = Unsafe.As<{readMethod.Type}, {context.Property.Type}>(ref temp);");
        }
        else
        {
            context.CodeBuilder.Line($"{context.DataName} = ({context.Property.Type}){context.StreamName}.{readMethod}();");
        }
        return(true);
    }
 public static void Execute(this PostactionCallback callback, MethodBuildingContext context)
 {
     callback?.Invoke(context);
 }
Пример #10
0
 public virtual bool ModifyCollectionPrefixDeserialization(MethodBuildingContext context)
 {
     return(false);
 }
Пример #11
0
 public virtual bool ModifySerialization(MethodBuildingContext context)
 {
     return(false);
 }
Пример #12
0
    private bool TryReadPropertyCollection(string streamName, string dataName, Property property, List <Property> properties, CodeBuilder builder, SyntaxProvider syntaxProvider)
    {
        // If there is a method for writing the whole collection, use it instead
        if (syntaxProvider.Methods.TryGetReadMethod(property, collection: true, out Method collectionReadMethod))
        {
            return(TryReadProperty(streamName, dataName, property, properties, builder, syntaxProvider, collectionReadMethod));
        }

        syntaxProvider.Methods.TryGetReadMethod(varInt, out Method readMethod);
        var context = new MethodBuildingContext(streamName, dataName, property, properties, builder, readMethod, syntaxProvider.Methods, syntaxProvider.Context);

        if (property.Reading.Execute(context))
        {
            return(true);
        }

        // Attributes behavior
        for (int i = 0; i < property.Attributes.Length; i++)
        {
            if (property.Attributes[i].ModifyCollectionPrefixDeserialization(context))
            {
                goto LOOP;
            }
        }

        // Default behavior
        if (readMethod is null)
        {
            ReportMissingMethod(syntaxProvider, property, "deserialization");
            return(false);
        }
        string getLength = $"{streamName}.{readMethod}()";

        builder.Line($"{dataName} = {property.NewCollection(getLength)};");

LOOP:
        builder.Statement($"for (int i = 0; i < {dataName}.{property.Length}; i++)");

        context = new MethodBuildingContext(streamName, dataName + "[i]", property, properties, builder, readMethod, syntaxProvider.Methods, syntaxProvider.Context);

        // Attributes behavior
        for (int i = 0; i < property.Attributes.Length; i++)
        {
            if (property.Attributes[i].ModifyDeserialization(context))
            {
                goto END_LOOP;
            }
        }

        // Default behavior
        if (readMethod is null)
        {
            ReportMissingMethod(syntaxProvider, property, "deserialization");
            return(false);
        }
        builder.Line($"{dataName}[i] = {streamName}.{readMethod}();");

END_LOOP:
        builder.EndScope();

        property.Read.Execute(context);
        return(true);
    }
Пример #13
0
    private bool TrySerializePropertyCollection(string streamName, Property property, List <Property> properties, CodeBuilder builder, SyntaxProvider syntaxProvider)
    {
        // If there is a method for writing the whole collection, use it instead
        if (syntaxProvider.Methods.TryGetWriteMethod(property, collection: true, out Method collectionWriteMethod))
        {
            return(TrySerializeProperty(streamName, property, properties, builder, syntaxProvider, collectionWriteMethod));
        }

        syntaxProvider.Methods.TryGetWriteMethod(varInt, out Method writeMethod);

        var context = new MethodBuildingContext(streamName, property.Name, property, properties, builder, writeMethod, syntaxProvider.Methods, syntaxProvider.Context);

        if (property.Writing.Execute(context))
        {
            return(true);
        }

        // Attributes behavior
        for (int i = 0; i < property.Attributes.Length; i++)
        {
            if (property.Attributes[i].ModifyCollectionPrefixSerialization(context))
            {
                goto LOOP;
            }
        }

        // Default behavior
        if (writeMethod is null)
        {
            ReportMissingMethod(syntaxProvider, property, "serialization");
            return(false);
        }
        builder.Line($"{streamName}.{writeMethod}({property}.{property.Length});");

LOOP:
        property.Written.Execute(context);

        // Begin the for loop
        builder.Statement($"for (int i = 0; i < {property}.{property.Length}; i++)");
        syntaxProvider.Methods.TryGetWriteMethod(property, out writeMethod);
        context = new MethodBuildingContext(streamName, property.Name + "[i]", property, properties, builder, writeMethod, syntaxProvider.Methods, syntaxProvider.Context);

        // Attributes behavior
        for (int i = 0; i < property.Attributes.Length; i++)
        {
            if (property.Attributes[i].ModifySerialization(context))
            {
                goto END_LOOP;
            }
        }

        // Default behavior
        if (writeMethod is null)
        {
            ReportMissingMethod(syntaxProvider, property, "serialization");
            return(false);
        }
        builder.Line($"{streamName}.{writeMethod}({property}[i]);");

END_LOOP:
        builder.EndScope();

        property.Written.Execute(context);
        return(true);
    }