简体   繁体   中英

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

First of all, I only found examples on google and here.json to object or.xml to object. But I have a pre-defined code that I want to read in, edit and save exactly or only changed things.

For the context, it's about skin mods, ie paths to images and changing their properties

The File

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
 }
}

actually i only need the value of assigned_vehicle and then the part as an object, edited and then only write the part back. Of course, it would be nicer to read everything in and save it completely again. So basically my own serializer and deserializer for this format.

so what do you mean? Read in file line by line and create and fill objects manually or is there a better solution than creating umpteen objects and filling them by hand?

Can something like that work?

I don't know what a parser / serializer / deserializer should look like. Or not even what my class is anyway

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
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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