/// <summary>Loads a player deck.</summary> /// <param name="id">An array containing the loaded CardIdentity ids.</param> /// <param name="type">An array containing the corresponding CardModel guids (encrypted).</param> /// <param name="group">An array indicating the group the cards must be loaded into.</param> public void LoadDeck(int[] id, Guid[] type, Group[] group, string[] size, string sleeve, bool limited) { if (id.Length != type.Length || id.Length != group.Length) { Program.GameMess.Warning("[LoadDeck] Protocol violation: inconsistent arrays sizes."); return; } if (id.Length == 0) { return; // Loading an empty deck --> do nothing } var who = Player.Find((byte)(id[0] >> 16)); if (who == null) { Program.GameMess.Warning("[LoadDeck] Player not found."); return; } WriteReplayAction(who.Id); if (limited) { Program.GameMess.System("{0} loads a limited deck.", who); } else { Program.GameMess.System("{0} loads a deck.", who); } if (!IsLocalPlayer(who)) { try { var sleeveObj = Sleeve.FromString(sleeve); who.SetSleeve(sleeveObj); } catch (SleeveException ex) { Log.Warn(ex.Message, ex); Program.GameMess.Warning($"There was an error loading {0}'s deck sleeve: " + ex.Message, who); } catch (Exception ex) { Log.Warn(ex.Message, ex); Program.GameMess.Warning($"There was an unknown error loading {0}'s deck sleeve.", who); } } CreateCard(id, type, group, size); Log.Info("LoadDeck Starting Task to Fire Event"); Program.GameEngine.EventProxy.OnLoadDeck_3_1_0_0(who, @group.Distinct().ToArray()); Program.GameEngine.EventProxy.OnLoadDeck_3_1_0_1(who, @group.Distinct().ToArray()); Program.GameEngine.EventProxy.OnDeckLoaded_3_1_0_2(who, @group.Distinct().ToArray()); }
public static IDeck Load(this IDeck deck, Game game, string path, bool cloneCards = true) { var ret = new Deck(); ret.Sections = new List <ISection>(); try { var cards = game.Sets().SelectMany(x => x.Cards).ToArray(); using (var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { var doc = XDocument.Load(fs); var gameId = Guid.Parse(doc.Descendants("deck").First().Attribute("game").Value); var shared = doc.Descendants("deck").First().Attr <bool>("shared"); foreach (var sectionelem in doc.Descendants("section")) { var section = new Section(); section.Cards = new List <IMultiCard>(); section.Name = sectionelem.Attribute("name").Value; section.Shared = sectionelem.Attr <bool>("shared"); // On old style decks, if it's shared, then all subsequent sections are shared if (shared) { section.Shared = true; } foreach (var cardelem in sectionelem.Descendants("card")) { var cardId = Guid.Parse(cardelem.Attribute("id").Value); var cardq = Int32.Parse(cardelem.Attribute("qty").Value); var card = cards.FirstOrDefault(x => x.Id == cardId); if (card == null) { var cardN = cardelem.Value; card = cards.FirstOrDefault(x => x.Name.Equals(cardN, StringComparison.CurrentCultureIgnoreCase)); if (card == null) { throw new UserMessageException(L.D.Exception__CanNotLoadDeckCardNotInstalled_Format, path, cardId, cardN); } } (section.Cards as IList <IMultiCard>).Add(card.ToMultiCard(cardq, cloneCards)); } if (section.Cards.Any()) { (ret.Sections as List <ISection>).Add(section); } } // Add deck notes var notesElem = doc.Descendants("notes").FirstOrDefault(); if (notesElem != null) { var cd = (notesElem.FirstNode as XCData); if (cd != null) { ret.Notes = cd.Value.Clone() as string; } } if (ret.Notes == null) { ret.Notes = ""; } // Add all missing sections so that the game doesn't get pissed off { var combinedList = game.DeckSections.Select(x => x.Value).Concat(game.SharedDeckSections.Select(y => y.Value)); foreach (var section in combinedList) { if (ret.Sections.Any(x => x.Name.Equals(section.Name, StringComparison.InvariantCultureIgnoreCase) && x.Shared == section.Shared) == false) { // Section not defined in the deck, so add an empty version of it. (ret.Sections as List <ISection>).Add( new Section { Name = section.Name.Clone() as string, Cards = new List <IMultiCard>(), Shared = section.Shared }); } } } // Add card sleeve { var sleeveElem = doc.Descendants("sleeve").FirstOrDefault(); if (sleeveElem != null) { var sleeveString = sleeveElem.Value; if (!string.IsNullOrWhiteSpace(sleeveString)) { try { ret.Sleeve = Sleeve.FromString(sleeveString); } catch (SleeveException ex) { throw new UserMessageException(ex.Message, ex); } } } } ret.GameId = gameId; ret.IsShared = shared; } // This is an old style shared deck file, we need to convert it now, for posterity sake. if (ret.IsShared) { ret.IsShared = false; ret.Save(game, path); } deck = ret; return(deck); } catch (UserMessageException) { throw; } catch (FormatException e) { Log.Error(String.Format("Problem loading deck from path {0}", path), e); throw new UserMessageException(L.D.Exception__CanNotLoadDeckCorrupt_Format, path); } catch (NullReferenceException e) { Log.Error(String.Format("Problem loading deck from path {0}", path), e); throw new UserMessageException(L.D.Exception__CanNotLoadDeckCorrupt_Format, path); } catch (XmlException e) { Log.Error(String.Format("Problem loading deck from path {0}", path), e); throw new UserMessageException(L.D.Exception__CanNotLoadDeckCorrupt_Format, path); } catch (FileNotFoundException) { throw new UserMessageException(L.D.Exception__CanNotLoadDeckFileNotFound_Format, path); } catch (IOException e) { Log.Error(String.Format("Problem loading deck from path {0}", path), e); throw new UserMessageException(L.D.Exception__CanNotLoadDeckIOError_Format, path, e.Message); } catch (Exception e) { Log.Error(String.Format("Problem loading deck from path {0}", path), e); throw new UserMessageException(L.D.Exception__CanNotLoadDeckUnspecified_Format, path); } }