Rust Plugin Not Loading Data File (always creating a new one)Solved

Hello Oxide!  I can't seem to find the solution on google, so I'm here posting - hoping someone can find something in my code.  The symptom is that  whenever I reload my plugin, it always creates a new datafile rather than loading the data within it.  The plugin and its IO behavior works as intended during gameplay.

Loading data through this method, which init() calls with LoadData();

private void LoadData()
        {
            try
            {
                Puts("loading datafile..{0}", Name.ToString());
                storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>(Name);
                Puts("Datafile loaded");
            }
            catch
            {
                storedData = null;
            }
            if (storedData == null)
            {
                Puts("-- no datafile, creating new..");
                ClearData();
            }
        }


StoredData class;
 private class StoredData
        {
            [JsonProperty("Monument Sesssions")]
            public List<MSession> m_sessions  { get; set; } = new List<MSession>();
           
  

            public StoredData()
            {

            }
                      
        }​


MSession class;
private class MSession
        {           
            [JsonProperty("Player Id")]
            public string player_id;

            [JsonProperty("Locked by admin?")]
            private bool locked_by_admin {get; set;} = false;

            [JsonProperty("Team ID")]
            private string team_id;

            [JsonProperty("Monument")]
            public Monument SessionMonument = new Monument();

            [JsonProperty("Session started_at")]
            private string started_at;

            [JsonProperty("Session updated_at")]
            private string updated_at;

            [JsonProperty("Session ended_at")]
            private string ended_at;

            [JsonProperty("Session duration(sec)")]
            private double duration;


            public MSession(string _player_id, Monument _monument, bool is_admin=false)
            {
                player_id = _player_id;
                team_id = GetPlayer(_player_id).currentTeam.ToString();
                SessionMonument = _monument;
                locked_by_admin = is_admin;
                started_at = DateTime.Now.ToString();
                updated_at = DateTime.Now.ToString();
                duration = 0;
            }



        }​


When my datafile looks like this, oxide loads it as intended (instead of creating a new one);
{
  "Monument Sesssions": []
}​


When my datafile looks like this, oxide creates a new file (instead of loading it);
{
  "Monument Sesssions": [
    {
      "Player Id": "76561############",
      "Team ID": "0",
      "Monument": {
        "Enabled: Is Monument enabled?": true,
        "Lock Looting: When a player enters this monument while locked, do we lock their looting abilities?": true,
        "Cancel Cards: When a player enters this monument while locked, do we lock their card swiping abilities?": true,
        "Lock Crate: When a player enters this monument while locked, do we lock their hacking on locked crates?": true,
        "Broadcasting: Do we want this plugin to broadcast to the server when this monument is locked?": true,
        "Monument Display Name": "Large Harbor",
        "Monument Prefab Name": "assets/bundled/prefabs/autospawn/monument/harbor/harbor_1.prefab",
        "Monument Short Name": "harbor_1",
        "Monument Grid Position": "B10",
        "Lock Attack: When a player enters a locked monument, do we lock their attacking abilities?": true,
        "Lock timer (600 = 10 min)?": 600
      },
      "Session started_at": "03/04/2023 16:27:48",
      "Session updated_at": "03/04/2023 16:27:48",
      "Session ended_at": null,
      "Session duration(sec)": 0.0,
      "Locked by admin?": false
    }
  ]
}​

just bumping this in case any fresh eyes have any idea.  Thanks

rustonauts

just bumping this in case any fresh eyes have any idea.  Thanks

How r you saving that Data?

thanks.

Interface.Oxide.DataFileSystem.WriteObject(Name, storedData);
MyData storedData;
private DynamicConfigFile MyDATA;

        private void OnServerInitialized()
        {
            MyDATA = Interface.Oxide.DataFileSystem.GetFile($"{Name}/MyFileName");
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                storedData = Interface.GetMod().DataFileSystem.ReadObject<MyData>($"{Name}/MyFileName");
            }
            catch
            {
                PrintWarning("Couldn't load cooldowns data, creating new data file");
                storedData= new MyData();
            }
        }

        public class MyData
        {
           public List<MSession> m_sessions = new List<MSession>();
        }

        public void SaveData()
        {
           MyDATA.WriteObject(storedData);
        }


Then ya should be able to do MyData.m_sessions

thanks.  Doing the same thing.  It just always wants to create a new datafile when I reload the plugin.  It even acknowledges that the file exists, but it just won't load the datafile.

Merged post

ok... here are more specifics.  If the datafile exists, but is empty, it loads it.  If the datafile exists, but is filled w/ data, then it doesn't load and creates a brand new datafile.

Empty datafile, LoadData() loads it just fine.

{
  "audits": []
}​
{
  "audits": [
    {
      "Date": "10/30/2024 17:51:58",
      "Player Steam ID": 76561############,
      "Player Name": "unohuim",
      "Team ID": 0,
      "Clan ID": 0,
      "Net Worth": 116,
      "Scrap": 86,
      "Cloth": 12,
      "Metal Frag": 5,
      "HQ Metal": 7,
      "Crude Oil": 0,
      "Stone": 0,
      "Wood": 945,
      "Fat": 12,
      "Diesel": 0,
      "Corn": 0,
      "Fertilizer": 0,
      "Trout": 0,
      "Perch": 0,
      "Shark": 0,
      "Salmon": 0,
      "OrangeRoughy": 0,
      "CatFish": 0
    }
  ]
}

but once it's filled w/ data, it doesn't load and creates a brand new datafile.



Merged post

the actual error msg = error: System.NullReferenceException: Object reference not set to an instance of an object.
rustonauts

thanks.  Doing the same thing.  It just always wants to create a new datafile when I reload the plugin.  It even acknowledges that the file exists, but it just won't load the datafile.

Merged post

ok... here are more specifics.  If the datafile exists, but is empty, it loads it.  If the datafile exists, but is filled w/ data, then it doesn't load and creates a brand new datafile.

Empty datafile, LoadData() loads it just fine.

{
  "audits": []
}​
{
  "audits": [
    {
      "Date": "10/30/2024 17:51:58",
      "Player Steam ID": 76561############,
      "Player Name": "unohuim",
      "Team ID": 0,
      "Clan ID": 0,
      "Net Worth": 116,
      "Scrap": 86,
      "Cloth": 12,
      "Metal Frag": 5,
      "HQ Metal": 7,
      "Crude Oil": 0,
      "Stone": 0,
      "Wood": 945,
      "Fat": 12,
      "Diesel": 0,
      "Corn": 0,
      "Fertilizer": 0,
      "Trout": 0,
      "Perch": 0,
      "Shark": 0,
      "Salmon": 0,
      "OrangeRoughy": 0,
      "CatFish": 0
    }
  ]
}

but once it's filled w/ data, it doesn't load and creates a brand new datafile.



Merged post

the actual error msg = error: System.NullReferenceException: Object reference not set to an instance of an object.


Looking its probly

 public Monument SessionMonument = new Monument();

remove saving the monument bet is saves and loads

omg.. problem solved!!

For some reason, the issue was in my class constructor.  I had to include a null check in the constructor, which surprises me.

if(!_player==null) {                
                    player_id = _player_id;
                    player_name = _player.displayName;
                    team_id = _player.currentTeam;
                    clan_id = _player.clanId;
                    _player = null;

                    NetWorth();                                
                }​

Thanks for your response and thought.  Case closed!

Locked automatically