Hey guys, I am trying to learn c# to do my plugin in rather then LUA. So forgive me if this is an easy answer and I'm missing it.
In LUA we can just load the info directly from the datafile without extra handling of the data, it doesn't seem to be the same for c#.
I'm saving the dataFile by using:
(where whitelist is Dictionary<string, List<string>>)Code:private void SaveData(){ var dataFile = Interface.GetMod ().DataFileSystem.GetDatafile (this.Title); dataFile["whitelist"]= whitelist; Interface.GetMod ().DataFileSystem.SaveDatafile (this.Title); }
The problem is I don't know how to use what I get from:
when trying to load it back into my "whitelist" variable.Code:var dataFile = Interface.GetMod ().DataFileSystem.GetDatafile (this.Title);
Can someone help point me in the right direction?
EDIT: Just playing around with it and I managed to get this working:
Is it the best way to do this? Obviously instead of Puts() I would be adding this to my variable.Code:private void LoadData() { var dataFile = Interface.GetMod ().DataFileSystem.GetDatafile (this.Title); var whitelistData = (Dictionary<string, object>)Convert.ChangeType(dataFile["whitelist"], typeof(Dictionary<string, object>)); foreach (KeyValuePair<string, object> wld in whitelistData) { var wldValue = (List<object>)Convert.ChangeType(wld.Value, typeof(List<object>)); Puts("Player: {0}", wld.Key); for (int i = 0; i < wldValue.Count; i++) { Puts("Allowed: {0}", wldValue[i]); } } Interface.GetMod ().DataFileSystem.SaveDatafile (this.Title); }
New to C#, some issues with datafiles
Discussion in 'Rust Development' started by Capadillo, Mar 3, 2016.
-
Code:
class SomeData { ... }SomeData data = new SomeData();Interface.Oxide.DataFileSystem.WriteObject(filename, data); data = Interface.Oxide.DataFileSystem.ReadObject<SomeData>(filename); -
These are some simplified methods I've written. Just put them into your code.
Example:Code:void LoadData<T>(ref T data, string filename = "?") => data = Core.Interface.Oxide.DataFileSystem.ReadObject<T>(filename == "?" ? this.Title : $"{this.Title}/{filename}");void SaveData<T>(ref T data, string filename = "?") => Core.Interface.Oxide.DataFileSystem.WriteObject(filename == "?" ? this.Title : $"{this.Title}/{filename}", data);
Code:List<ulong> steamIDs = new List<ulong>();void Loaded() => LoadData(ref steamIDs);void OnPlayerInit(BasePlayer player) { if(!steamIDs.Contains(player.userID)) { steamIDs.Add(player.userID); SaveData(ref steamIDs); } } -
Just a couple of quick questions LaserHydra:
Does the => (for LoadData()) work in the same principal as putting what follows in {}?
And the else clause of the inline read/write object; is that creating a folder in the data directory under the name of the plugin title?
Lastly, this bit is what throws me a little as I still don't have a full grasp of c#. In your example if I change the steamIDs to something like:
will that still work/save as i would expect it to?Code:Dictionary<ulong, List<ulong>> steamIDs = new Dictionary<ulong, List<ulong>>();
In either case I love that you gave an example of how it could be used in practice. It makes it so much easier to understand for those just starting with c#. Thank you!Last edited by a moderator: Mar 7, 2016 -
In this case "=>" is used as part of a "Lambda Expression". You can read up on it here: Lambda Expressions (C# Programming Guide)
Yes it will!
It depends on what actually you want to do, but if the data contained in the data file can be converted into the Dictionary<ulong, List<ulong>> type then it should work. -
I would redo part of my plugin so it reflected that type when it saves. I was initially using strings but ulong wojld likely be a little more beneficial.
Thank you for the response, and patience, Tuntenfisch!
-
One thing I noticed is that if you save a ulong value to the configuration file and then later try to read it you might get an InvalidCastException because for some reason the ulong get's interpreted as a string and you can't just convert a string to a ulong. It might be the case that I'm doing something wrong but I just wanted to give a heads-up for you.
-
We were talking about datafiles and not config files
Datafiles should work fine. -
I'm aware of that, just wanted to give him a heads-up.
-
Thanks for the heads up anyway! Though the only thing I'll likely use the config for is to set a variable slash command.
