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); } }
public static string GetWriteValue(SaveField field, string append = null) { string result = ""; string dataType = field.DataType; string enumCast = string.Empty; if (!string.IsNullOrWhiteSpace(field.Enum)) { dataType = SizeToType[field.Size]; enumCast = $"({dataType})"; } switch (dataType) { case "sbyte": case "byte": case "short": case "ushort": case "int": case "uint": case "long": case "ulong": case "float": var writeFunc = WriteFunctions[dataType]; result += $"save.{writeFunc}({enumCast}{field.Name}{append});"; break; case "bool": var writeFuncBool = WriteFunctions[SizeToType[field.Length]]; result += $"save.{writeFuncBool}(({SizeToType[field.Length]})({field.Name} ? 1 : 0));"; break; case "string" when !string.IsNullOrWhiteSpace(field.Size): result += $"save.WriteSizedUTF8({field.Name}, {field.Length});"; break; case "string": result += $"save.WriteUTF8({field.Name}{append});"; break; default: result += $"{field.Name}{append}.WriteSave(save);"; break; } return(result); }
public static string GetReadValue(SaveField field) { string result = ""; string dataType = field.DataType; if (!string.IsNullOrWhiteSpace(field.Enum)) { result += $"({field.DataType})"; dataType = SizeToType[field.Size]; } switch (dataType) { case "sbyte": case "byte": case "short": case "ushort": case "int": case "uint": case "long": case "ulong": case "float": var readFunc = ReadFunctions[dataType]; result += $"save.{readFunc}();"; break; case "bool": var readFuncBool = ReadFunctions[SizeToType[field.Length]]; result += $"save.{readFuncBool}() != 0;"; break; case "string" when !string.IsNullOrWhiteSpace(field.Size): result += $"{field.Name} = Read.ReadSizedUTF8(save, {field.Length});"; break; case "string": result += $"{field.Name} = save.ReadUTF8({field.Length});"; break; default: result += $"new {dataType}(save);"; break; } return(result); }
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); } }