Guys,
I'm hopping to create a JSON List with categories and its subs using this:
Code:public class configObj { public List<configMessages> Messages { set; get; } public configObj() { Messages = new List<configMessages>(); } } public class configMessages { public string Msg1 = "foo"; public string Msg2 = "bar"; }
Anyone knows how to implement this code?
Setting Config categories with public List<catList> Categ { set; get; } in C#
Discussion in 'Rust Development' started by TheRotAG, Feb 9, 2015.
-
gimme a second mate
Code:public class Info { public int aa{ set; get; } public int bb { set; get; } public string ss { set; get; } public Info(int i, int r, string n) { aa = i; bb = r; ss = n; } }; private Dictionary<int, Info> Infoz;public Core.Configuration.DynamicConfigFile InfoData;void Loaded() { InfoData = Interface.GetMod().DataFileSystem.GetDatafile("MyDataFile"); if (InfoData["Info"] != null) Infoz = JsonConvert.DeserializeObject<Dictionary<int, Info>>((string)JsonConvert.SerializeObject(InfoData["Info"]).ToString()); else Infoz= new Dictionary<int, Info>(); }
and too save it
Code:InfoData["Info"] = Infoz; Interface.GetMod().DataFileSystem.SaveDatafile("MyDataFile");
Last edited by a moderator: Feb 10, 2015 -
You can use Interface.GetMod().DataFileSystem.
ReadObject<T>(name) and WriteObject<T>(name, T Object) for custom objects in C# plugins
For exmp.
Code:try { List<myclass> myobj = Interface.GetMod().DataFileSystem.ReadObject<List<myclass>>("MyData"); } catch { List<myclass> myobj = new List<myclass>(); }
Interface.GetMod().DataFileSystem.WriteObject<List<myclass>>("MyData",myobj);Last edited by a moderator: Feb 10, 2015 -
Tnx bros' gonna test.
-
i suggest using my method cause u can simply do
if(Infoz.ContainsKey((int)player.userID))
Infoz[(int)player.userID].aa = 2323;
else
Infoz.Add((int)target.userID, new Info(42, 2424, "Yay"));
efficient for loading and saving due to useing steamid as a index dictionary method is pro cause indexing stuff is superior -
You are basicly serializing and destializing single file for 3 times its time/resource consuming you can still use dictionaries dirrctly with ReadObject
Dictionary<string,info> = Interface.GetMod().DataFileSystem.ReadObject<Dictionary<string,info>>("MyData"); ofc cast it with try to catch exceptations -
-
@ColonelAngus
yes but im talking about dictionary compared to list, not the serializing way lol
@Feramor
its best to avoid using try and catch methods, you should simply have proper checks in places so you dont cause a exception in the first place
@Feramor | @ColonelAngus
thanks for the hint about the serializing im new to oxide but in no way new to c# i have around 10 years experience -
Only exceptions for ReadObject is File not found and Deserialization error
-
shame we cant use io we could check if file existed
-
https://github.com/OxideMod/Oxide -
im lazy lol
-
-
This has to be like this Plugin developers should cast it with try structure since we can't construct custom classes with < T > since they may have custom constructers.
-
a simple method in that file to create it with user defined arguments would solve that, without requiring a thrown exception.
Doesn't have to be like that at all