예제 #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);
    }