Inheritance: ICloneable
Exemple #1
0
 public bool Equals(Marker other)
 {
     if (other == null) return false;
     if (other.Id != Id) return false;
     if (other.Name == null) return false;
     if (other.Name.Equals(Name, StringComparison.InvariantCultureIgnoreCase) == false) return false;
     return true;
 }
Exemple #2
0
 public object Clone()
 {
     var ret = new Marker();
     ret.Id = this.Id;
     ret.Name = this.Name.Clone() as string;
     ret.IconUri = IconUri;
     return ret;
 }
Exemple #3
0
        internal void AddRecentMarker(DataNew.Entities.Marker marker)
        {
            int idx = _recentMarkers.IndexOf(marker);

            if (idx == 0)
            {
                return;
            }
            if (idx > 0)
            {
                _recentMarkers.RemoveAt(idx);
                _recentMarkers.Insert(0, marker);
                return;
            }

            if (_recentMarkers.Count == MaxRecentMarkers)
            {
                _recentMarkers.RemoveAt(MaxRecentMarkers - 1);
            }
            _recentMarkers.Insert(0, marker);
        }
Exemple #4
0
        public object Deserialize(string fileName)
        {
            //var timer = new Stopwatch();
            //timer.Start();
            var ret = new Set();
            var directory = new FileInfo(fileName).Directory.FullName;
            using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var settings = new XmlReaderSettings();
                settings.Schemas.Add(SetSchema);
                var doc = XDocument.Load(fs);
                doc.Validate(settings.Schemas, (a, b) =>
                    {
                        Console.WriteLine(b.Exception);
                    });
                ret.Cards = new List<Card>();
                var root = doc.Element("set");
                ret.Id = new Guid(root.Attribute("id").Value);
                ret.Name = root.Attribute("name").Value;
                ret.Filename = fileName;
                ret.GameId = new Guid(root.Attribute("gameId").Value);
                ret.Cards = new List<Card>();
                ret.GameVersion = new Version(root.Attribute("gameVersion").Value);
                ret.Markers = new List<Marker>();
                ret.Packs = new List<Pack>();
                ret.Version = new Version(root.Attribute("version").Value);
                ret.PackageName = "";
                ret.InstallPath = directory;
                ret.DeckPath = Path.Combine(ret.InstallPath, "Decks");
                ret.PackUri = Path.Combine(ret.InstallPath, "Cards");
                var gameImageInstallPath = Path.Combine(Config.Instance.Paths.ImageDatabasePath, ret.GameId.ToString());
                ret.ImageInstallPath = Path.Combine(gameImageInstallPath, "Sets", ret.Id.ToString());
                ret.ImagePackUri = Path.Combine(ret.ImageInstallPath, "Cards");
                ret.ProxyPackUri = Path.Combine(ret.ImagePackUri, "Proxies");

                if (!Directory.Exists(ret.PackUri)) Directory.CreateDirectory(ret.PackUri);
                if (!Directory.Exists(ret.ImagePackUri)) Directory.CreateDirectory(ret.ImagePackUri);
                if (!Directory.Exists(ret.ProxyPackUri)) Directory.CreateDirectory(ret.ProxyPackUri);
                var game = DbContext.Get().Games.First(x => x.Id == ret.GameId);
                foreach (var c in doc.Document.Descendants("card"))
                {
					var card = new Card(new Guid(c.Attribute("id").Value), ret.Id, c.Attribute("name").Value,c.Attribute("id").Value, "",game.CardSizes["Default"],new Dictionary<string, CardPropertySet>());
                    //var card = new Card
                    //               {
                    //                   Id = new Guid(c.Attribute("id").Value),
                    //                   Name = c.Attribute("name").Value,
                    //                   SetId = ret.Id,
                    //                   Properties = new Dictionary<string, CardPropertySet>(),
                    //                   ImageUri = c.Attribute("id").Value,
                    //                   Alternate = "",
                    //                   Size = game.CardSizes["Default"]
                    //               };

                    var cs = c.Attribute("size");
                    if (cs != null)
                    {
						if(game.CardSizes.ContainsKey(cs.Value) == false)
                            throw new UserMessageException(Octgn.Library.Localization.L.D.Exception__BrokenGameContactDev_Format, game.Name);

                        card.Size = game.CardSizes[cs.Value];
                    }

                    var defaultProperties = new CardPropertySet();
                    defaultProperties.Type = "";
                    defaultProperties.Properties = new Dictionary<PropertyDef, object>();
                    foreach (var p in c.Descendants("property").Where(x => x.Parent.Name == "card"))
                    {
                        var pd = game.CustomProperties.FirstOrDefault(x => x.Name == p.Attribute("name").Value);
                        if (pd == null)
                        {
                            throw new UserMessageException(Octgn.Library.Localization.L.D.Exception__BrokenGameContactDev_Format, game.Name);
                        }
                        var newpd = pd.Clone() as PropertyDef;
                        defaultProperties.Properties.Add(newpd, p.Attribute("value").Value);
                    }
                    foreach (var cp in game.CustomProperties)
                    {
                        if (!defaultProperties.Properties.ContainsKey(cp))
                        {
                            var cpnew = cp.Clone() as PropertyDef;
                            cpnew.IsUndefined = true;
                            defaultProperties.Properties.Add(cpnew, "");
                        }
                    }
                    var np = new PropertyDef()
                                 {
                                     Hidden = false,
                                     Name = "Name",
                                     Type = PropertyType.String,
                                     TextKind = PropertyTextKind.FreeText,
                                     IgnoreText = false,
                                     IsUndefined = false
                                 };
                    if (defaultProperties.Properties.ContainsKey(np))
                        defaultProperties.Properties.Remove(np);
                    defaultProperties.Properties.Add(np, card.Name);
                    card.Properties.Add("", defaultProperties);

                    // Add all of the other property sets
                    foreach (var a in c.Descendants("alternate"))
                    {
                        var propset = new CardPropertySet();
                        propset.Properties = new Dictionary<PropertyDef, object>();
                        propset.Type = a.Attribute("type").Value;
                        var thisName = a.Attribute("name").Value;
                        foreach (var p in a.Descendants("property"))
                        {
                            var pd = game.CustomProperties.First(x => x.Name.Equals(p.Attribute("name").Value, StringComparison.InvariantCultureIgnoreCase));
                            var newprop = pd.Clone() as PropertyDef;
                            var val = p.Attribute("value").Value;
                            propset.Properties.Add(newprop, val);
                        }
                        foreach (var cp in game.CustomProperties)
                        {
                            if (!propset.Properties.ContainsKey(cp))
                            {
                                var cpnew = cp.Clone() as PropertyDef;
                                cpnew.IsUndefined = true;
                                propset.Properties.Add(cpnew, "");
                            }
                        }
                        var np2 = new PropertyDef()
                        {
                            Hidden = false,
                            Name = "Name",
                            Type = PropertyType.String,
                            TextKind = PropertyTextKind.FreeText,
                            IgnoreText = false,
                            IsUndefined = false
                        };
                        if (propset.Properties.ContainsKey(np2))
                            propset.Properties.Remove(np2);
                        propset.Properties.Add(np2, thisName);
                        card.Properties.Add(propset.Type, propset);
                    }

                    (ret.Cards as List<Card>).Add(card);
                }
                foreach (var p in doc.Document.Descendants("pack"))
                {
                    var pack = new Pack();
                    pack.Id = new Guid(p.Attribute("id").Value);
                    pack.Name = p.Attribute("name").Value;
                    pack.Definition = DeserializePack(p.Elements());
                    pack.SetId = ret.Id;
                    (ret.Packs as List<Pack>).Add(pack);
                }
                foreach (var m in doc.Document.Descendants("marker"))
                {
                    var marker = new Marker();
                    marker.Id = new Guid(m.Attribute("id").Value);
                    marker.Name = m.Attribute("name").Value;
                    var mpathd = new DirectoryInfo(Path.Combine(directory, "Markers"));
                    var mpath = mpathd.Exists == false ? null : mpathd.GetFiles(marker.Id.ToString() + ".*", SearchOption.TopDirectoryOnly).First();
                    marker.IconUri = mpath == null ? null : Path.Combine(directory, "Markers", mpath.FullName);
                    (ret.Markers as List<Marker>).Add(marker);
                }
            }

            if (ret.Cards == null) ret.Cards = new Card[0];
            if (ret.Markers == null) ret.Markers = new Marker[0];
            if (ret.Packs == null) ret.Packs = new Pack[0];
            //Console.WriteLine(timer.ElapsedMilliseconds);
            return ret;
        }
Exemple #5
0
        public void InstallSet(string filename)
        {
            try
            {
                using (Package package = Package.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    PackageRelationship defRelationship = package.GetRelationshipsByType("http://schemas.octgn.org/set/definition").First();
                    PackagePart definition = package.GetPart(defRelationship.TargetUri);

                    var settings = new XmlReaderSettings { ValidationType = ValidationType.Schema, IgnoreWhitespace = true };
                    using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Paths),"Schema/CardSet.xsd"))
                        if (s != null)
                            using (XmlReader reader = XmlReader.Create(s))
                                settings.Schemas.Add(null, reader);

                    // Read the cards
                    using (XmlReader reader = XmlReader.Create(definition.GetStream(), settings))
                    {
                        reader.ReadToFollowing("set"); // <?xml ... ?>
                        var thisset = this.FromXmlReader(filename, reader);
                        var game = GameManager.Get().GetById(thisset.GameId);
                        if (thisset.GameVersion.Major != game.Version.Major || thisset.GameVersion.Minor != game.Version.Minor)
                            throw new ApplicationException(
                                string.Format(
                                    "The set '{0}' is incompatible with the installed game version.\nGame version: \nSet made for version: {1:2}.",
                                    thisset.Name, thisset.GameVersion));

                        if (reader.IsStartElement("packaging"))
                        {
                            reader.ReadStartElement(); // <packaging>
                            while (reader.IsStartElement("pack"))
                            {
                                string xml = reader.ReadOuterXml();
                                var pack = PackManager.Get().FromXml(thisset, xml);
                                var temp = thisset.Packs.ToList();
                                temp.Add(pack);
                                thisset.Packs = temp;
                            }
                            reader.ReadEndElement(); // </packaging>
                        }

                        if (reader.IsStartElement("markers"))
                        {
                            reader.ReadStartElement(); // <markers>
                            while (reader.IsStartElement("marker"))
                            {
                                var marker = new Marker();
                                reader.MoveToAttribute("name");
                                marker.Name = reader.Value;
                                reader.MoveToAttribute("id");
                                marker.Id = new Guid(reader.Value);
                                Uri markerImageUri = definition.GetRelationship("M" + marker.Id.ToString("N")).TargetUri;
                                marker.IconUri = markerImageUri.OriginalString;
                                if (!package.PartExists(markerImageUri))
                                    throw new ApplicationException(
                                        string.Format(
                                            "Image for marker '{0}', with URI '{1}' was not found in the package.",
                                            marker.Name, marker.IconUri));
                                reader.Read(); // <marker />
                                var temp = thisset.Markers.ToList();
                                temp.Add(marker);
                                thisset.Markers = temp;
                            }
                            reader.ReadEndElement(); // </markers>
                        }

                        var cardList = thisset.Cards().ToList();
                        if (reader.IsStartElement("cards"))
                        {
                            reader.ReadStartElement(); // <cards>
                            while (reader.IsStartElement("card"))
                            {
                                cardList.Add(CardManager.Get().FromXmlReader(reader,game,thisset,definition,package));
                            }
                            // cards are parsed through the CardModel constructor, which are then inserted individually into the database
                            reader.ReadEndElement(); // </cards>
                        }

                        reader.ReadEndElement();
                        DbContext.Get().Save(thisset);
                        thisset.AddCard(cardList.ToArray());
                    }
                    string path = Path.Combine(Paths.DataDirectory, "Decks");
                    PackageRelationshipCollection decks = package.GetRelationshipsByType("http://schemas.octgn.org/set/deck");
                    var buffer = new byte[0x1000];
                    foreach (PackageRelationship deckRel in decks)
                    {
                        PackagePart deck = package.GetPart(deckRel.TargetUri);
                        string deckuri = Path.GetFileName(deck.Uri.ToString());
                        if (deckuri == null) continue;
                        string deckFilename = Path.Combine(path, deckuri);
                        using (Stream deckStream = deck.GetStream(FileMode.Open, FileAccess.Read))
                        using (
                            FileStream targetStream = File.Open(deckFilename, FileMode.Create, FileAccess.Write, FileShare.Read)
                            )
                        {
                            while (true)
                            {
                                int read = deckStream.Read(buffer, 0, buffer.Length);
                                if (read == 0) break;
                                targetStream.Write(buffer, 0, read);
                            }
                        }
                    }
                    package.Close();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }