繁体   English   中英

寻找最佳实践 - C# - 将文件读取到 Object,编辑并保存回 - 不是 XML、Json

[英]looking for a best practice - C# - Read file to Object, edit and save back to - Not XML, Json

首先,我只在谷歌和这里找到了示例。json 到 object 或 .xml 到 object。但是我有一个预定义的代码,我想读入、编辑和保存完全或仅更改的内容。

对于上下文,它是关于皮肤模型的,即图像路径和更改它们的属性

文件

Unit
{
 economy : _.abcd.1234 {
  player : _.defg.5678
  vehicles: 10
  vehicle[0] : _.hijk.9012
  vehicle[1] : 
  ... and so on
  vehicle[9] : _.lmno.3456
  assigned_vehicle: _.hijk.9012
  ...
 }
 ... other things
 vehicle : _.hijk.9012 {
  license_plate: "M XX 69"
  accessories: 41
  accessories[0]: _.af25.1780
  ...
  accessories[40]: _.6e68.a620
  data_path: "/def/vehicle/mercedes/data.txt"
 }
 ... other vehicles
 ... other accessories
 accessory : _.af25.1780 {
  offset: 4
  paint_color: (1, 1, 1)
  wear: 0
  data_path: "/def/vehicle/t_wheel/single_385_55_steel.sii"
 }
 ... other accessories
 ... more things with format
 object : name {
  property : value
  property : object
  property : count
  property[x] : value
 }
}

实际上我只需要 assigned_vehicle 的值,然后是 object 的部分,编辑后只写回部分。 当然,如果全部读进去再完整保存就更好了。 所以基本上我自己的这种格式的序列化器和反序列化器。

所以你是什么意思? 逐行读取文件并手动创建和填充对象,或者有比创建无数对象并手动填充它们更好的解决方案吗?

这样的东西可以工作吗?

我不知道解析器/序列化器/反序列化器应该是什么样子。 或者甚至不是我的 class 是什么

public static void ParseLocalSaveGame(SaveGame saveGame)
{
    using (StreamReader sr = new StreamReader(saveGame.Path))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            if (line.Contains("Unit"))
            {
                // beginning of file
                // jump to the first "usable" line
                sr.ReadLine();
                sr.ReadLine();
                continue;
            }

            if (line.Contains(""))
            {
                // skip empty line
                sr.ReadLine(); 
                continue;
            }
            else if (line.Contains("economy"))
            {
                saveGame.Economy = new Economy(line.Replace("economy :", "").Replace(" ", "").Replace("{", ""));
                ParseSaveGameEconomy(sr);
                continue;
            }
            else if (line.Contains("vehicle"))
            {
                Vehicle vehicle = new Vehicle(line.Replace("vehicle :", "").Replace(" ", "").Replace("{", ""));
                ParseSaveGameVehicle(sr, vehicle);
                saveGame.Vehicles.Add(vehicle);
                continue;
            }
            else if (line.Contains("accessory"))
            {
                Accessory accessory = new Accessory(line.Replace("accessory :", "").Replace(" ", "").Replace("{", ""));
                ParseSaveGameAccessory(sr, accessory);
                saveGame.Accessories.Add(accessory);
                continue;
            }
            else if (line.Contains("}"))
            {
                //This should be the last line
                continue;
            }
            else
            {
                // This should not happen!
                // TODO: Write Error
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM