示例#1
0
        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(
                                        "Problem loading deck {0}. The card with id: {1} and name: {2} is not installed.", 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
                                    });
                            }
                        }
                    }
                    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("The deck {0} is corrupt.", path);
            }
            catch (NullReferenceException e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("The deck {0} is corrupt.", path);
            }
            catch (XmlException e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("The deck {0} is corrupt.", path);
            }
            catch (FileNotFoundException)
            {
                throw new UserMessageException("Could not save deck to {0}, could not file the file.", path);
            }
            catch (IOException e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("Could not load deck from {0}, {1}", path, e.Message);
            }
            catch (Exception e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("Could not load deck from {0}, there was an unspecified problem.", path);
            }
            return ret;
        }