Hey guys,
I'm getting desperate, I don't know why this happens, but I cant cast back from the datafile.
This is the class that I'm trying to load back from file:
And this is the code to do that:Code:namespace Hunt { public class RPGInfo { public RPGInfo(string steamId) { SteamId = steamId; Level = 1; Experience = 0; SkillPoints = new List<SkillPoints>(); LastSaved = DateTime.UtcNow; } public bool AddExperience(float xp) { Experience += xp; if (Experience >= RequiredExperience()) { Level++; return true; } return false; } public float RequiredExperience() { return (float)(Math.Log10(Level + 1) * 1000); } public override String ToString() { StringBuilder sb = new StringBuilder(); sb.AppendLine("======HUNT RPG======"); sb.AppendLine(String.Format("SteamId: {0}", SteamId)); sb.AppendLine(String.Format("Level: {0}", Level)); sb.AppendLine(String.Format("XP: {0}/{1}", Experience, RequiredExperience())); sb.AppendLine("===================="); return sb.ToString(); } public string SteamId { get; set; } public int Level { get; set; } public float Experience { get; set; } public List<SkillPoints> SkillPoints { get; set; } public DateTime LastSaved { get; set; } } public class SkillPoints { public Skill Skill { get; set; } public int points { get; set; } } public class Skill { public string Name { get; set; } public string Description { get; set; } }}
I know is a lot of code to read, but I've tried to use this method (Reneb - Kits) and the (Factions - LFG) method as well. Not sure why that happens.Code:namespace Oxide.Plugins { [Info("HuntRPG Plugin", "SW", 0.1)] class HuntRPG : RustPlugin { private DynamicConfigFile RPGConfig; private bool Changed; void OnServerInitialized() { LoadRPG(); } private void LoadRPG() { RPGConfig = Interface.GetMod().DataFileSystem.GetDatafile("Hunt_Data"); } private void SaveRPG() { Interface.GetMod().DataFileSystem.SaveDatafile("Hunt_Data"); } private RPGInfo RPGInfo(BasePlayer player) { string steamId = SteamId(player); var rpg_info = new RPGInfo(steamId); if (RPGConfig[steamId] == null) { RPGConfig[steamId] = rpg_info; SaveRPG(); } else { rpg_info = (RPGConfig[steamId]) as RPGInfo; if (rpg_info == null) { SendReply(player, "Oh shit, really?"); rpg_info = new RPGInfo(steamId); } } return rpg_info; } private string SteamId(BasePlayer player) { return player.userID.ToString(); } [ChatCommand("ficha")] private void cmdFicha(BasePlayer player, string command, string[] args) { SendReply(player, RPGInfo(player).ToString()); } }
Oh, just so you know this code works if the server is not restarted, I mean, it saves and loads from the file. But if I restart the server, it just cant load again from the file. =[
Any ideas?
Thank You
[DOUBLEPOST=1425564034,1425529620][/DOUBLEPOST]Ok, so I didn't found out the reason, but I managed to get it working using the Factions method.
Here is the code:
Code:private const string DataFileName = "Hunt_Data"; private Dictionary<string, RPGInfo> RPGConfig; void OnServerInitialized() { Interface.GetMod().DataFileSystem.GetDatafile(DataFileName); Interface.GetMod().DataFileSystem.SaveDatafile(DataFileName); } void Init() { RPGConfig = new Dictionary<string, RPGInfo>(); } void Loaded() { LoadRPG(); } private void LoadRPG() { RPGConfig = Interface.GetMod().DataFileSystem.ReadObject<Dictionary<string,RPGInfo>>(DataFileName); } private void SaveRPG() { Interface.GetMod().DataFileSystem.WriteObject<Dictionary<string, RPGInfo>>(DataFileName, RPGConfig); }
Solved DataFile cast problem?
Discussion in 'Rust Development' started by PedraozauM, Mar 5, 2015.
-
ah you got it already, gave it a short try
Code:// Reference: Oxide.Ext.Rustusing System; using System.Collections.Generic; using System.Text; using Oxide.Core; using Oxide.Core.Configuration;namespace Oxide.Plugins { [Info("HuntRPG Plugin", "SW", 0.1)] internal class HuntRPG : RustPlugin { private Dictionary<string, RPGInfo> RPGInfos; private bool Changed; private void OnServerInitialized() { LoadRPG(); } private void LoadRPG() { try { RPGInfos = Interface.GetMod().DataFileSystem.ReadObject<Dictionary<string, RPGInfo>>("Hunt_Data"); } catch (Exception) { RPGInfos = new Dictionary<string, RPGInfo>(); } } private void SaveRPG() { Interface.GetMod().DataFileSystem.WriteObject("Hunt_Data", RPGInfos); } private RPGInfo GetRPGInfo(BasePlayer player) { string steamId = SteamId(player); var rpg_info = new RPGInfo(steamId); if (!RPGInfos.ContainsKey(steamId) || RPGInfos[steamId] == null) { RPGInfos[steamId] = rpg_info; SaveRPG(); } else { rpg_info = RPGInfos[steamId]; if (rpg_info == null) { SendReply(player, "Oh shit, really?"); rpg_info = new RPGInfo(steamId); } } return rpg_info; } private string SteamId(BasePlayer player) { return player.userID.ToString(); } [ChatCommand("ficha")] private void cmdFicha(BasePlayer player, string command, string[] args) { SendReply(player, GetRPGInfo(player).ToString()); } class RPGInfo { public RPGInfo(string steamId) { SteamId = steamId; Level = 1; Experience = 0; SkillPoints = new List<SkillPoints>(); LastSaved = DateTime.UtcNow; } public bool AddExperience(float xp) { Experience += xp; if (Experience >= RequiredExperience()) { Level++; return true; } return false; } public float RequiredExperience() { return (float)(Math.Log10(Level + 1) * 1000); } public override String ToString() { StringBuilder sb = new StringBuilder(); sb.AppendLine("======HUNT RPG======"); sb.AppendLine(String.Format("SteamId: {0}", SteamId)); sb.AppendLine(String.Format("Level: {0}", Level)); sb.AppendLine(String.Format("XP: {0}/{1}", Experience, RequiredExperience())); sb.AppendLine("===================="); return sb.ToString(); } public string SteamId { get; set; } public int Level { get; set; } public float Experience { get; set; } public List<SkillPoints> SkillPoints { get; set; } public DateTime LastSaved { get; set; } } class SkillPoints { public Skill Skill { get; set; } public int points { get; set; } } class Skill { public string Name { get; set; } public string Description { get; set; } } } }
-
Now I get it, for some reason the JSON reader cant read properties with private set (o.0).
Im gonna make a suggestion to the oxider devs to make a JSON Hanlder that uses replections to get private properties and stuff, and besides that to add an Attribute to mark properties that doesnt need to be serialized.