public static XmlDocument getXmlDocFromFile(string path) { XmlDocument xmlDoc = new XmlDocument(); try { //load #if !USE_BINARY_FILES xmlDoc.Load(path); #else StreamReader streamReader = new StreamReader(path); string decodedString = streamReader.ReadToEnd(); streamReader.Close(); xmlDoc.LoadXml(TEncryptor.decryptAndDecodeFromBase64(decodedString, encriptionKey)); #endif } catch (Exception e) { if (e != null) { } //keep it here! return(null); } return(xmlDoc); }
public static void newSaveGame(int slot) { #if WRITE_SAVEGAMES createDirectoryIfNotExist(); string path = getSaveGameSlotFilePath(slot); XmlDocument doc = new XmlDocument(); XmlNode data = doc.AppendChild(doc.CreateElement("SaveGame")); //create the basic nodes XmlNode stats = doc.CreateElement("Stats"); XmlNode skills = doc.CreateElement("Skills"); XmlNode hud = doc.CreateElement("Hud"); XmlNode townHud = doc.CreateElement("TownHud"); XmlNode inventory = doc.CreateElement("Inventory"); XmlNode equipment = doc.CreateElement("Equipment"); XmlNode quests = doc.CreateElement("Quests"); XmlNode navigation = doc.CreateElement("Navigation"); XmlNode tutorials = doc.CreateElement("Tutorials"); //fill the initial stats stats.Attributes.Append(doc.CreateAttribute("level")).Value = Game.initialLevel.ToString(); stats.Attributes.Append(doc.CreateAttribute("experience")).Value = Game.initialExperience.ToString(); stats.Attributes.Append(doc.CreateAttribute("healthPoints")).Value = 0.ToString(); stats.Attributes.Append(doc.CreateAttribute("magicPoints")).Value = 0.ToString(); stats.Attributes.Append(doc.CreateAttribute("strengthPoints")).Value = 0.ToString(); stats.Attributes.Append(doc.CreateAttribute("agilityPoints")).Value = 0.ToString(); stats.Attributes.Append(doc.CreateAttribute("health")).Value = Game.initialHealth.ToString(); stats.Attributes.Append(doc.CreateAttribute("magic")).Value = Game.initialMagic.ToString(); stats.Attributes.Append(doc.CreateAttribute("strength")).Value = Game.initialStrength.ToString(); stats.Attributes.Append(doc.CreateAttribute("agility")).Value = Game.initialAgility.ToString(); stats.Attributes.Append(doc.CreateAttribute("coins")).Value = Game.initialCoins.ToString(); stats.Attributes.Append(doc.CreateAttribute("gems")).Value = Game.initialGems.ToString(); stats.Attributes.Append(doc.CreateAttribute("pointsLeft")).Value = 0.ToString(); stats.Attributes.Append(doc.CreateAttribute("easterEgg")).Value = 0.ToString(); stats.Attributes.Append(doc.CreateAttribute("tapjoyMask")).Value = 1.ToString(); stats.Attributes.Append(doc.CreateAttribute("rateUsShown")).Value = 0.ToString(); //write the initial skills data skills.Attributes.Append(doc.CreateAttribute("unlockBitMask")).Value = 0.ToString(); skills.Attributes.Append(doc.CreateAttribute("skill0")).Value = (-1).ToString(); skills.Attributes.Append(doc.CreateAttribute("skill1")).Value = (-1).ToString(); skills.Attributes.Append(doc.CreateAttribute("skill2")).Value = (-1).ToString(); skills.Attributes.Append(doc.CreateAttribute("skill3")).Value = (-1).ToString(); //write the initial hud data hud.Attributes.Append(doc.CreateAttribute("inventoryEnabled")).Value = 1.ToString(); hud.Attributes.Append(doc.CreateAttribute("healingPotionEnabled")).Value = 1.ToString(); hud.Attributes.Append(doc.CreateAttribute("magicPotionEnabled")).Value = 0.ToString(); hud.Attributes.Append(doc.CreateAttribute("quickChestEnabled")).Value = 0.ToString(); hud.Attributes.Append(doc.CreateAttribute("worldMapEnabled")).Value = 0.ToString(); hud.Attributes.Append(doc.CreateAttribute("enabledTownMask")).Value = 1.ToString(); hud.Attributes.Append(doc.CreateAttribute("inventoryTabMask")).Value = (1 << (int)Game.TabInventory.GAME_OPTION).ToString(); //write the initial Town Hud data townHud.Attributes.Append(doc.CreateAttribute("enabledTownHudMask")).Value = Game.shopkeeperDefaultDialogMask.ToString(); //write the initial inventory Dictionary <Item, int> initialItems = Game.getInitialItems(); foreach (KeyValuePair <Item, int> ItemToAdd in initialItems) { //get the item path Item item = ItemToAdd.Key; string itemPath = item.getPath(); //create a new item node and set their attributes XmlNode itemNode = doc.CreateElement("Item"); itemNode.Attributes.Append(doc.CreateAttribute("name")).Value = itemPath; itemNode.Attributes.Append(doc.CreateAttribute("ammount")).Value = ItemToAdd.Value.ToString(); //attach the new item to the list inventory.AppendChild(itemNode); } //write the initial equipment List <Item> initialEquipment = Game.getInitialEquipment(); foreach (Item item in initialEquipment) { //get the item path string itemPath = item.getPath(); //create a new item node and set their attributes XmlNode itemNode = doc.CreateElement("Item"); itemNode.Attributes.Append(doc.CreateAttribute("name")).Value = itemPath; //attach the new item to the list equipment.AppendChild(itemNode); } //write the initial navigation data navigation.Attributes.Append(doc.CreateAttribute("town")).Value = Game.townList[0]; //write the initial tutorial data foreach (Tutorial t in Tutorial.tutorialList) { XmlNode tutorial = doc.CreateElement("Tutorial"); tutorial.Attributes.Append(doc.CreateAttribute("name")).Value = t.GetType().ToString(); tutorial.Attributes.Append(doc.CreateAttribute("completed")).Value = t.completed?"1":"0"; tutorials.AppendChild(tutorial); } //attach the basic nodes to the document data.AppendChild(stats); data.AppendChild(skills); data.AppendChild(hud); data.AppendChild(townHud); data.AppendChild(inventory); data.AppendChild(equipment); data.AppendChild(quests); data.AppendChild(navigation); data.AppendChild(tutorials); Debug.Log("writing file:" + path); //save #if !USE_BINARY_FILES doc.Save(path); #else File.WriteAllText(path, TEncryptor.encryptAndEncodeToBase64(dumpXml(doc), encriptionKey)); #endif #endif }
public static void writeSaveGame(int slot) { #if WRITE_SAVEGAMES Game game = Game.game; createDirectoryIfNotExist(); string path = getSaveGameSlotFilePath(slot); XmlDocument doc = new XmlDocument(); XmlNode data = doc.AppendChild(doc.CreateElement("SaveGame")); //create the basic nodes XmlNode stats = doc.CreateElement("Stats"); XmlNode skills = doc.CreateElement("Skills"); XmlNode hud = doc.CreateElement("Hud"); XmlNode townHud = doc.CreateElement("TownHud"); XmlNode inventory = doc.CreateElement("Inventory"); XmlNode equipment = doc.CreateElement("Equipment"); XmlNode quests = doc.CreateElement("Quests"); XmlNode navigation = doc.CreateElement("Navigation"); XmlNode tutorials = doc.CreateElement("Tutorials"); CharacterStats charStats = game.gameStats; //fill the current stats stats.Attributes.Append(doc.CreateAttribute("level")).Value = charStats.level.ToString(); stats.Attributes.Append(doc.CreateAttribute("experience")).Value = charStats.experience.ToString(); stats.Attributes.Append(doc.CreateAttribute("healthPoints")).Value = charStats.healthPoints.ToString(); stats.Attributes.Append(doc.CreateAttribute("magicPoints")).Value = charStats.magicPoints.ToString(); stats.Attributes.Append(doc.CreateAttribute("strengthPoints")).Value = charStats.strengthPoints.ToString(); stats.Attributes.Append(doc.CreateAttribute("agilityPoints")).Value = charStats.agilityPoints.ToString(); stats.Attributes.Append(doc.CreateAttribute("health")).Value = charStats.health.ToString(); stats.Attributes.Append(doc.CreateAttribute("magic")).Value = charStats.magic.ToString(); stats.Attributes.Append(doc.CreateAttribute("strength")).Value = charStats.strength.ToString(); stats.Attributes.Append(doc.CreateAttribute("agility")).Value = charStats.agility.ToString(); stats.Attributes.Append(doc.CreateAttribute("coins")).Value = charStats.coins.ToString(); stats.Attributes.Append(doc.CreateAttribute("gems")).Value = charStats.gems.ToString(); stats.Attributes.Append(doc.CreateAttribute("pointsLeft")).Value = charStats.pointsLeft.ToString(); stats.Attributes.Append(doc.CreateAttribute("easterEgg")).Value = ((Game.game.easterEggDone)?1:0).ToString(); stats.Attributes.Append(doc.CreateAttribute("tapjoyMask")).Value = Game.game.tapjoyPromotionMask.ToString(); stats.Attributes.Append(doc.CreateAttribute("rateUsShown")).Value = ((Game.game.hasShownRateUsWindow)?1:0).ToString(); //write the initial skills data int unlockMask = 0; for (int i = 0; i < Game.game.unlockedSkills.Length; i++) { if (Game.game.unlockedSkills[i]) { unlockMask |= 1 << i; } } skills.Attributes.Append(doc.CreateAttribute("unlockBitMask")).Value = unlockMask.ToString(); skills.Attributes.Append(doc.CreateAttribute("skill0")).Value = ((int)Game.game.currentSkills[0]).ToString(); skills.Attributes.Append(doc.CreateAttribute("skill1")).Value = ((int)Game.game.currentSkills[1]).ToString(); skills.Attributes.Append(doc.CreateAttribute("skill2")).Value = ((int)Game.game.currentSkills[2]).ToString(); skills.Attributes.Append(doc.CreateAttribute("skill3")).Value = ((int)Game.game.currentSkills[3]).ToString(); //write the initial hud data hud.Attributes.Append(doc.CreateAttribute("inventoryEnabled")).Value = Game.game.inventoryEnabled?"1":"0"; hud.Attributes.Append(doc.CreateAttribute("healingPotionEnabled")).Value = Game.game.healingPotionEnabled?"1":"0"; hud.Attributes.Append(doc.CreateAttribute("magicPotionEnabled")).Value = Game.game.magicPotionEnabled?"1":"0"; hud.Attributes.Append(doc.CreateAttribute("quickChestEnabled")).Value = Game.game.quickChestEnabled?"1":"0"; hud.Attributes.Append(doc.CreateAttribute("worldMapEnabled")).Value = Game.game.worldMapEnabled?"1":"0"; hud.Attributes.Append(doc.CreateAttribute("enabledTownMask")).Value = Game.game.enabledTownMask.ToString(); hud.Attributes.Append(doc.CreateAttribute("inventoryTabMask")).Value = Game.game.enabledInventoryTabMask.ToString(); //write the initial Town Hud data townHud.Attributes.Append(doc.CreateAttribute("enabledTownHudMask")).Value = Game.game.shopkeeperDialogMask.ToString(); //write the current inventory foreach (KeyValuePair <Item, int> ItemToAdd in Inventory.inventory.itemList) { //get the item path Item item = ItemToAdd.Key; string itemPath = item.getPath(); //create a new item node and set their attributes XmlNode itemNode = doc.CreateElement("Item"); itemNode.Attributes.Append(doc.CreateAttribute("name")).Value = itemPath; itemNode.Attributes.Append(doc.CreateAttribute("ammount")).Value = ItemToAdd.Value.ToString(); //attach the new item to the list inventory.AppendChild(itemNode); } //write the current equipment foreach (KeyValuePair <Item.Type, Item> ItemToAdd in Equipment.equipment.items) { if (ItemToAdd.Value != null) { //get the item path Item item = ItemToAdd.Value; string itemPath = item.getPath(); //create a new item node and set their attributes XmlNode itemNode = doc.CreateElement("Item"); itemNode.Attributes.Append(doc.CreateAttribute("name")).Value = itemPath; //attach the new item to the list equipment.AppendChild(itemNode); } } //write quest data List <int> completedQuest = QuestManager.manager.getCompletedQuestList(); if (completedQuest != null && completedQuest.Count > 0) { foreach (int questIndex in completedQuest) { string qName = QuestManager.manager.questList[questIndex].name.Substring(QuestManager.manager.questList[questIndex].name.LastIndexOf('/') + 1).ToLower(); XmlNode questNode = doc.CreateElement("Quest"); questNode.Attributes.Append(doc.CreateAttribute("name")).Value = qName; quests.AppendChild(questNode); } } //write the navigation data navigation.Attributes.Append(doc.CreateAttribute("town")).Value = Game.townList[game.currentTown]; //write the tutorial data foreach (Tutorial t in Tutorial.tutorialList) { XmlNode tutorial = doc.CreateElement("Tutorial"); tutorial.Attributes.Append(doc.CreateAttribute("name")).Value = t.GetType().ToString(); tutorial.Attributes.Append(doc.CreateAttribute("completed")).Value = t.completed?"1":"0"; tutorials.AppendChild(tutorial); } //attach the basic nodes to the document data.AppendChild(stats); data.AppendChild(skills); data.AppendChild(hud); data.AppendChild(townHud); data.AppendChild(inventory); data.AppendChild(equipment); data.AppendChild(quests); data.AppendChild(navigation); data.AppendChild(tutorials); Debug.Log("writing file:" + path); //save #if !USE_BINARY_FILES doc.Save(path); #else File.WriteAllText(path, TEncryptor.encryptAndEncodeToBase64(dumpXml(doc), encriptionKey)); #endif #endif }
public static void CheckPlayingDates() { #if WRITE_SAVEGAMES createDirectoryIfNotExist(); string path = getSaveFileSlotPath(); //if save file doesn't exist it means is the first time he plays the game or he delete the savefile and we are screw if (!saveFileExist()) { XmlDocument doc = new XmlDocument(); XmlNode data = doc.AppendChild(doc.CreateElement("Data")); DateTime now = DateTime.Now; now = now.AddSeconds(-now.Second).AddMinutes(-now.Minute).AddHours(-now.Hour); data.AppendChild(doc.CreateElement("Day0")).InnerXml = now.ToString(); data.AppendChild(doc.CreateElement("Day1")).InnerXml = "-1"; data.AppendChild(doc.CreateElement("Day2")).InnerXml = "-1"; data.AppendChild(doc.CreateElement("Day3")).InnerXml = "-1"; //save #if !USE_BINARY_FILES doc.Save(path); #else File.WriteAllText(path, TEncryptor.encryptAndEncodeToBase64(dumpXml(doc), encriptionKey)); #endif } else { XmlDocument doc = getXmlDocFromFile(path); if (doc == null) { return; } DateTime Day0 = DateTime.Parse(doc.GetElementsByTagName("Day0").Item(0).InnerXml); bool day1isValid = doc.GetElementsByTagName("Day1").Item(0).InnerXml != "-1"; bool day2isValid = doc.GetElementsByTagName("Day2").Item(0).InnerXml != "-1"; bool day3isValid = doc.GetElementsByTagName("Day3").Item(0).InnerXml != "-1"; DateTime now = DateTime.Now; now = now.AddSeconds(-now.Second).AddMinutes(-now.Minute).AddHours(-now.Hour); TimeSpan span = now - Day0; //Play again on the next day if (!day1isValid && span.Days == 1) { doc.GetElementsByTagName("Day1").Item(0).InnerXml = now.ToString(); #if UNITY_ANDROID Game.CompleteTapjoyAction("1a3e7829-9cbc-498c-87cf-70e30e26799d"); #elif UNITY_IPHONE Game.CompleteTapjoyAction("7c3f6acd-30a4-4999-a245-75bba0d4f13a"); #endif } //Play again on the 3rd day if (!day2isValid && span.Days == 2) { doc.GetElementsByTagName("Day2").Item(0).InnerXml = now.ToString(); #if UNITY_ANDROID Game.CompleteTapjoyAction("e2e6690c-b2c5-4843-95be-661749d6cb4b"); #elif UNITY_IPHONE Game.CompleteTapjoyAction("a200b23f-1313-4136-b363-3ed2c670695e"); #endif } //cPlay again on the 4th day if (!day3isValid && span.Days == 3) { doc.GetElementsByTagName("Day3").Item(0).InnerXml = now.ToString(); #if UNITY_ANDROID Game.CompleteTapjoyAction("8a4d018b-526e-4147-ba84-9a881d4bccd2"); #elif UNITY_IPHONE Game.CompleteTapjoyAction("1d4d18fb-5700-42f4-93eb-d424de380823"); #endif } //save #if !USE_BINARY_FILES doc.Save(path); #else File.WriteAllText(path, TEncryptor.encryptAndEncodeToBase64(dumpXml(doc), encriptionKey)); #endif } #endif }