//This method deserializes a string and puts the information into the instance itself. public override void Deserialize(string info) { base.Deserialize(info); Points = Convert.ToInt32(JsonUtils.ExtractValue(info, "points")); foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(info, "orblist"))) { Orbs.Add(Convert.ToInt32(s)); } GamePowerup = new Powerup(); var strs = JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(info, "powerups")); for (int i = 0; i < Math.Min(strs.Count, 3); ++i) { switch (strs[i]) { case "ghost": GamePowerup.CurrentPowerups.Add(Powerup.powerups.ghost); GamePowerup.CarryingGhost = true; break; case "destabilize": GamePowerup.CurrentPowerups.Add(Powerup.powerups.destabilize); GamePowerup.CarryingDestabilize = true; break; case "neutralize": GamePowerup.CurrentPowerups.Add(Powerup.powerups.neutralize); GamePowerup.CarryingNeutralize = true; break; } } }
public void Test_FromJsonArray() { string s = @"[ 1, {2:[]}, ""3""]"; var l = JsonUtils.GetObjectsInArray(s); Assert.AreEqual(l[0], "1"); Assert.AreEqual(l[1], "{2:[]}"); Assert.AreEqual(l[2], "\"3\""); }
public void Test_ToJsonArray() { var orbs = new Game(false).Orbs; string st = JsonUtils.ToJsonList(orbs); var s = JsonUtils.GetObjectsInArray(st); Assert.AreEqual(s.Count, orbs.Count); for (int i = 0; i < orbs.Count; i++) { Orb other = GameObject.FromJsonFactory <Orb>(s[i]); Assert.AreEqual(other.Xcoor, orbs[i].Xcoor, 0.5); Assert.AreEqual((int)other.Ycoor, (int)orbs[i].Ycoor, 0.5); Assert.AreEqual(other.Color, orbs[i].Color); } }
//This method reads a file, deserializes it into a Game, and returns the Game object. public static Game Load(string filename, bool isCheatMode) { string json; try { using (StreamReader sr = new StreamReader(File.OpenRead(filename))) { json = sr.ReadToEnd(); } } catch { throw new ArgumentException("The file does not exist"); } string version = JsonUtils.ExtractValue(json, "version"); if (version != '"' + Version + '"') { throw new FormatException("Wrong version of saved game file: " + version); } var game = new Game(isCheatMode); game.StableWells = new List <Well>(); game.UnstableWells = new List <Well>(); game.Orbs = new List <Orb>(); game.Username = JsonUtils.ExtractValue(json, "username"); game.Ticks = Convert.ToInt32(JsonUtils.ExtractValue(json, "ticks")); game.Player = GameObject.FromJsonFactory <Ship>(JsonUtils.ExtractValue(json, "humanplayer")); game.Points = game.Player.Points; game.Player.ParentGame = game; game.Player.GamePowerup.ParentGame = game; foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "stablegravitywells"))) { game.StableWells.Add(GameObject.FromJsonFactory <Well>(s)); } foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "unstablegravitywells"))) { Well w = GameObject.FromJsonFactory <Well>(s); w.ShockWave.ParentGame = game; game.UnstableWells.Add(w); } foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "orbs"))) { game.Orbs.Add(GameObject.FromJsonFactory <Orb>(s)); } foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "aiplayers"))) { AIShip aIShip = GameObject.FromJsonFactory <AIShip>(s); aIShip.ParentGame = game; game.AIShips.Add(aIShip); aIShip.GamePowerup.ParentGame = game; aIShip.InitializeTargets(); } game.GameObjects.AddRange(game.Orbs); game.GameObjects.AddRange(game.UnstableWells); game.GameObjects.AddRange(game.StableWells); game.GameObjects.Add(game.Player); return(game); }