public override void GenerateMaskToString(FileGeneration fg, TypeGeneration field, Accessor accessor, bool topLevel, bool printMask)
        {
            if (!field.IntegrateField)
            {
                return;
            }
            bool doIf;

            using (var args = new IfWrapper(fg, ANDs: true))
            {
                if (field.Nullable)
                {
                    args.Add($"{accessor} != null");
                }
                if (printMask)
                {
                    args.Add($"({GenerateBoolMaskCheck(field, "printMask")})");
                }
                doIf = !args.Empty;
            }
            using (new BraceWrapper(fg, doIf))
            {
                fg.AppendLine($"fg.{nameof(FileGeneration.AppendLine)}($\"{field.Name} => {{{accessor}}}\");");
            }
        }
Exemple #2
0
        public override void GenerateWriteToNode(ObjectGeneration obj, FileGeneration fg)
        {
            using (var args = new FunctionWrapper(fg,
                                                  $"public static void WriteToNode{ModuleNickname}{obj.GetGenericTypes(MaskType.Normal)}"))
            {
                args.Wheres.AddRange(obj.GenerateWhereClauses(LoquiInterfaceType.IGetter, defs: obj.Generics));
                args.Add($"{obj.Interface(internalInterface: true, getter: true)} item");
                args.Add($"XElement {XmlTranslationModule.XElementLine.GetParameterName(obj)}");
                args.Add($"ErrorMaskBuilder? errorMask");
                args.Add($"{nameof(TranslationCrystal)}? translationMask");
            }
            using (new BraceWrapper(fg))
            {
                if (obj.HasLoquiBaseObject)
                {
                    using (var args = new ArgsWrapper(fg,
                                                      $"{this.TranslationWriteClass(obj.BaseClass)}.WriteToNode{ModuleNickname}"))
                    {
                        args.Add($"item: item");
                        args.Add($"{XmlTranslationModule.XElementLine.GetParameterName(obj)}: {XmlTranslationModule.XElementLine.GetParameterName(obj)}");
                        args.Add($"errorMask: errorMask");
                        args.Add($"translationMask: translationMask");
                    }
                }

                void generateNormal(XmlTranslationGeneration generator, TypeGeneration field)
                {
                    if (!generator.ShouldGenerateWrite(field))
                    {
                        return;
                    }

                    List <string> conditions = new List <string>();

                    if (field.HasBeenSet)
                    {
                        conditions.Add($"{field.HasBeenSetAccessor(getter: true, accessor: Accessor.FromType(field, "item"))}");
                    }
                    if (this.TranslationMaskParameter)
                    {
                        conditions.Add(field.GetTranslationIfAccessor("translationMask"));
                    }
                    if (conditions.Count > 0)
                    {
                        using (var args = new IfWrapper(fg, ANDs: true))
                        {
                            foreach (var item in conditions)
                            {
                                args.Add(item);
                            }
                        }
                    }
                    using (new BraceWrapper(fg, doIt: conditions.Count > 0))
                    {
                        var maskType = this.Gen.MaskModule.GetMaskModule(field.GetType()).GetErrorMaskTypeStr(field);
                        generator.GenerateWrite(
                            fg: fg,
                            objGen: obj,
                            typeGen: field,
                            writerAccessor: $"{XmlTranslationModule.XElementLine.GetParameterName(obj)}",
                            itemAccessor: Accessor.FromType(field, "item"),
                            errorMaskAccessor: $"errorMask",
                            translationMaskAccessor: "translationMask",
                            nameAccessor: $"nameof(item.{field.Name})");
                    }
                }

                foreach (var field in obj.IterateFields(expandSets: SetMarkerType.ExpandSets.FalseAndInclude, nonIntegrated: true))
                {
                    if (!this.TryGetTypeGeneration(field.GetType(), out var generator))
                    {
                        if (!field.IntegrateField)
                        {
                            continue;
                        }
                        throw new ArgumentException("Unsupported type generator: " + field);
                    }

                    if (field is DataType dataType)
                    {
                        if (dataType.HasBeenSet)
                        {
                            fg.AppendLine($"if (item.{dataType.StateName}.HasFlag({obj.Name}.{dataType.EnumName}.Has))");
                        }
                        using (new BraceWrapper(fg, doIt: dataType.HasBeenSet))
                        {
                            bool isInRange             = false;
                            int  encounteredBreakIndex = 0;
                            foreach (var subField in dataType.IterateFieldsWithMeta())
                            {
                                if (!this.TryGetTypeGeneration(subField.Field.GetType(), out var subGenerator))
                                {
                                    throw new ArgumentException("Unsupported type generator: " + subField.Field);
                                }

                                var subData = subField.Field.GetFieldData();
                                if (!subGenerator.ShouldGenerateCopyIn(subField.Field))
                                {
                                    continue;
                                }
                                if (subField.BreakIndex != -1)
                                {
                                    fg.AppendLine($"if (!item.{dataType.StateName}.HasFlag({obj.Name}.{dataType.EnumName}.Break{subField.BreakIndex}))");
                                    fg.AppendLine("{");
                                    fg.Depth++;
                                    encounteredBreakIndex++;
                                }
                                if (subField.Range != null && !isInRange)
                                {
                                    isInRange = true;
                                    fg.AppendLine($"if (item.{dataType.StateName}.HasFlag({obj.Name}.{dataType.EnumName}.Range{subField.RangeIndex}))");
                                    fg.AppendLine("{");
                                    fg.Depth++;
                                }
                                if (subField.Range == null && isInRange)
                                {
                                    isInRange = false;
                                    fg.Depth--;
                                    fg.AppendLine("}");
                                }
                                generateNormal(subGenerator, subField.Field);
                            }
                            for (int i = 0; i < encounteredBreakIndex; i++)
                            {
                                fg.Depth--;
                                fg.AppendLine("}");
                                if (i == encounteredBreakIndex - 1)
                                {
                                    fg.AppendLine("else");
                                    using (new BraceWrapper(fg))
                                    {
                                        fg.AppendLine($"node.Add(new XElement(\"Has{dataType.EnumName}\"));");
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        generateNormal(generator, field);
                    }
                }
            }
            fg.AppendLine();
        }
        public override void GenerateWrite(
            FileGeneration fg,
            ObjectGeneration obj,
            TypeGeneration typeGen,
            Accessor writerAccessor,
            Accessor itemAccessor,
            Accessor errorMaskAccessor,
            Accessor nameAccessor,
            Accessor translationMaskAccessor)
        {
            var  dataType  = typeGen as DataType;
            bool isInRange = false;
            var  origDepth = fg.Depth;

            foreach (var subField in dataType.IterateFieldsWithMeta())
            {
                if (!this.XmlMod.TryGetTypeGeneration(subField.Field.GetType(), out var subGenerator))
                {
                    throw new ArgumentException("Unsupported type generator: " + subField.Field);
                }

                var subData = subField.Field.GetFieldData();
                if (!subGenerator.ShouldGenerateWrite(subField.Field))
                {
                    continue;
                }
                if (subField.BreakIndex != -1)
                {
                    fg.AppendLine($"if (!item.{dataType.StateName}.HasFlag({obj.Name}.{dataType.EnumName}.Break{subField.BreakIndex}))");
                    fg.AppendLine("{");
                    fg.Depth++;
                }
                if (subField.Range != null && !isInRange)
                {
                    isInRange = true;
                    fg.AppendLine($"if (item.{dataType.StateName}.HasFlag({obj.Name}.{dataType.EnumName}.Range{subField.RangeIndex}))");
                    fg.AppendLine("{");
                    fg.Depth++;
                }
                if (subField.Range == null && isInRange)
                {
                    isInRange = false;
                    fg.Depth--;
                    fg.AppendLine("}");
                }

                List <string> conditions = new List <string>();
                if (this.XmlMod.TranslationMaskParameter &&
                    subField.Field.IntegrateField)
                {
                    conditions.Add(subField.Field.GetTranslationIfAccessor("translationMask"));
                }
                if (conditions.Count > 0)
                {
                    using (var args = new IfWrapper(fg, ANDs: true))
                    {
                        foreach (var item in conditions)
                        {
                            args.Add(item);
                        }
                    }
                }
                using (new BraceWrapper(fg, doIt: conditions.Count > 0))
                {
                    subGenerator.GenerateWrite(
                        fg: fg,
                        objGen: obj,
                        nameAccessor: $"nameof(item.{subField.Field.Name})",
                        typeGen: subField.Field,
                        writerAccessor: writerAccessor,
                        translationMaskAccessor: "translationMask",
                        itemAccessor: Accessor.FromType(subField.Field, "item"),
                        errorMaskAccessor: $"errorMask");
                }
            }
            for (int i = fg.Depth - origDepth; i > 0; i--)
            {
                fg.Depth--;
                fg.AppendLine("}");
            }
        }