Solved Appending data to a file?

Discussion in 'Rust Development' started by modonga, Jun 4, 2016.

  1. hi dears, exist the function to append data to a file?

    like:
    Interface.GetMod().DataFileSystem.AppendFile(myFile)

    Regards
     
  2. Wulf

    Wulf Community Admin

    The Datafile API is only for JSON files, you'd simply update the file by adding new information. There aren't any other options for plugins to create other file types currently.
     
  3. i know that other files are restricted, so no append function to JSON file?

    so i need to read add new info and save again?
     
  4. Yeah, only through storing the entire content again.
    A data/config file is always a single JSON value, meaning there's no point in "appending" something to the file (there can be no two JSON values in a single file). If it was any different, you wouldn't be able to serialize/deserialize JSON into a composite type.
    If you're looking to create files and append to them (not read them!), you may use ConVar.Server.Log.
     
  5. Wulf

    Wulf Community Admin

    Just keep in mind that will add timestamps, not just a straight file with the data you want.
     
  6. excelent! thanks solve issue
     
  7. this is my solution. Regards

    string sendFile = string.Format("DataPTS");
    Core.Configuration.DynamicConfigFile receiveData = Interface.GetMod().DataFileSystem.GetDatafile(sendFile);
    (receiveData[section] as List<object>).Add(data); <---- THIS WORK GREAT
    Interface.GetMod().DataFileSystem.SaveDatafile(sendFile);
     
  8. This doesn't actually append to the file, it deserializes the full file, appends an element to a list (which is different from appending to the file) and then reserializes the entire thing.
    This is a lot more expensive than appending to a file.
     
  9. you say "A data/config file is always a single JSON value, meaning there's no point in "appending" something to the file" so this is my solution. my code dont append, only say that this is my solution to add data.
     
  10. For Oxide, if you're looking for a persistent storage:
    • that is often written to and read from, use an SQLite database.
    • that should persist data between multiple servers, use a MySQL database.
    • that is rarely written to and read from, use a JSON data file.
    • that is used for logging, use a log file.