Exemplo n.º 1
0
        public static List <SaveType> ReadTypes()
        {
            using (var stream = new FileStream("saveInfo.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var reader = new StreamReader(stream))
                {
                    IEnumerable <SaveField> csv = new CsvReader(reader).GetRecords <SaveField>();
                    var       types             = new List <SaveType>();
                    SaveType  type     = null;
                    SaveField bitfield = null;

                    foreach (var field in csv)
                    {
                        switch (field.Type)
                        {
                        case "Class":
                            type = new SaveType {
                                Name = field.Name
                            };
                            types.Add(type);
                            break;

                        case "Bitfield":
                            if (type == null)
                            {
                                throw new InvalidDataException("Encountered a Bitfield without a preceding Class");
                            }
                            bitfield       = field;
                            field.Bitfield = new List <SaveField>();
                            type.Fields.Add(field);
                            break;

                        case "BitfieldValue":
                            if (bitfield == null)
                            {
                                throw new InvalidDataException("Encountered a BitfieldValue without a preceding Bitfield");
                            }
                            bitfield.Bitfield.Add(field);
                            break;

                        default:
                            if (type == null)
                            {
                                throw new InvalidDataException($"Encountered a {field.Type} without a preceding Class");
                            }
                            type.Fields.Add(field);
                            break;
                        }
                    }

                    return(types);
                }
        }
Exemplo n.º 2
0
        public static List <SaveType> ReadTypes()
        {
            using (var stream = new FileStream("saveInfo.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var reader = new StreamReader(stream))
                {
                    IEnumerable <SaveField> csv = new CsvReader(reader).GetRecords <SaveField>();
                    var       types             = new List <SaveType>();
                    SaveType  type     = null;
                    SaveField bitfield = null;

                    foreach (var field in csv)
                    {
                        switch (field.Type)
                        {
                        case "Class":
                            type = new SaveType {
                                Name = field.Name
                            };
                            types.Add(type);
                            break;

                        case "Bitfield":
                            bitfield       = field;
                            field.Bitfield = new List <SaveField>();
                            Debug.Assert(type != null, nameof(type) + " != null");
                            type.Fields.Add(field);
                            break;

                        case "BitfieldValue":
                            Debug.Assert(bitfield != null, nameof(bitfield) + " != null");
                            bitfield.Bitfield.Add(field);
                            break;

                        default:
                            Debug.Assert(type != null, nameof(type) + " != null");
                            type.Fields.Add(field);
                            break;
                        }
                    }

                    return(types);
                }
        }
Exemplo n.º 3
0
        private static void CreateReadFunction(SaveType type, Indenter sb)
        {
            sb.AppendLine();
            sb.AppendLine($"public {type.Name}(DataBuffer save)");
            sb.AppendLineAndIncrease("{");
            bool firstField   = true;
            bool prevWasArray = false;

            foreach (var field in type.Fields)
            {
                switch (field.Type)
                {
                case "Scalar":
                    if (prevWasArray)
                    {
                        sb.AppendLine();
                    }
                    if (field.DataType == "string")
                    {
                        sb.AppendLine(GetReadValue(field));
                    }
                    else
                    {
                        sb.AppendLine($"{field.Name} = {GetReadValue(field)}");
                    }

                    prevWasArray = false;

                    break;

                case "Array":
                    if (!firstField)
                    {
                        sb.AppendLine();
                    }
                    sb.AppendLine($"for (int i = 0; i < {field.Name}.Length; i++)");
                    sb.AppendLineAndIncrease("{");
                    sb.AppendLine($"{field.Name}[i] = {GetReadValue(field)}");

                    sb.DecreaseAndAppendLine("}");
                    prevWasArray = true;
                    break;

                case "Bitfield":
                    sb.AppendLine($"{field.DataType} {field.Name} = {GetReadValue(field)}");
                    int bit = 0;
                    foreach (var value in field.Bitfield)
                    {
                        sb.AppendLine(
                            $"{value.Name} = ({field.DataType})({field.Name} >> {bit} & ((1u << {value.Size}) - 1));");
                        bit += int.Parse(value.Size);
                    }
                    break;

                case "BitfieldArray":
                    sb.AppendLine($"Read.ReadBitfieldArray(save, {field.Name}, {field.Length}, {field.Size});");
                    break;

                case "Padding":
                    sb.AppendLine($"save.Position += {field.Length};");
                    break;
                }

                firstField = false;
            }

            sb.DecreaseAndAppendLine("}");
        }