public MarkerModel(Guid id, string name, string icon, Set set) { _set = set; _iconUri = icon; this.name = name; Id = id; }
public MarkerModel(XmlReader reader, Set set) { reader.MoveToAttribute("name"); name = reader.Value; reader.MoveToAttribute("id"); id = new Guid(reader.Value); reader.Read(); // <marker /> this.set = set; }
private void Apply(Package package, string filename, bool installed) { using (var setPkg = Package.Open(filename, FileMode.Open, FileAccess.ReadWrite)) { // Extract information about the target set var defRelationship = setPkg.GetRelationshipsByType("http://schemas.octgn.org/set/definition").First(); var definition = setPkg.GetPart(defRelationship.TargetUri); Set set; using (var reader = XmlReader.Create(definition.GetStream(), xmlSettings)) { reader.ReadToFollowing("set"); // <?xml ... ?> set = new Set(filename, reader, game.repository); // Check if the set game matches the patch if (set.Game != game) return; } // Check if there is a patch for this set string relationId = "S" + set.Id.ToString("N"); if (!package.RelationshipExists(relationId)) return; var patchPart = package.GetPart(package.GetRelationship(relationId).TargetUri); XDocument patchDoc; using (var stream = patchPart.GetStream()) patchDoc = XDocument.Load(stream); // Check if the set is at least the required version for patching if (set.Version < patchDoc.Root.Attr<Version>("minVersion")) return; if (set.Version > patchDoc.Root.Attr<Version>("maxVersion")) return; if (installed) game.DeleteSet(game.Sets.Single(s => s.packageName == filename)); // Process the set foreach (XElement action in patchDoc.Root.Elements()) switch (action.Name.LocalName) { case "new": { Uri targetUri = new Uri(action.Attr<string>("targetUri"), UriKind.Relative); string relationshipId = action.Attr<string>("relationshipId"); string contentType = action.Attr<string>("contentType"); PackagePart part = setPkg.PartExists(targetUri) ? setPkg.GetPart(targetUri) : setPkg.CreatePart(targetUri, contentType, CompressionOption.Normal); using (var targetStream = part.GetStream(FileMode.Create, FileAccess.Write)) using (var srcStream = package.GetPart(patchPart.GetRelationship(relationshipId).TargetUri).GetStream()) srcStream.CopyTo(targetStream); break; } case "newrel": { Uri partUri = new Uri(action.Attr<string>("partUri"), UriKind.Relative); string relationshipId = action.Attr<string>("relationshipId"); Uri targetUri = new Uri(action.Attr<string>("targetUri"), UriKind.Relative); string relationshipType = action.Attr<string>("relationshipType"); PackagePart part = setPkg.GetPart(partUri); if (part.RelationshipExists(relationshipId)) part.DeleteRelationship(relationshipId); part.CreateRelationship(targetUri, TargetMode.Internal, relationshipType, relationshipId); break; } default: throw new InvalidFileFormatException("Unknown patch action: " + action.Name); } } OnProgress(string.Format("{0} patched.", System.IO.Path.GetFileName(filename))); if (installed) try { game.InstallSet(filename); } catch (Exception ex) { OnProgress(string.Format("{0} can't be re-installed.\nDetails: {1}", filename, ex.Message), true); } }
private void InsertSet(Set set) { using(var setTable = db.OpenTable("Set", false, false)) { if (setTable.Find("id:'" + set.Id + "'", "SetPK", false, false)) setTable.Delete(); setTable.Insert(); setTable.PutGuid("id", set.Id); setTable.PutString("name", set.Name); setTable.PutString("gameVersion", set.GameVersion.ToString()); setTable.PutString("version", set.Version.ToString()); setTable.PutString("package", set.PackageName); setTable.Post(); } cachedSets = null; }
public void InstallSet(string filename) { OpenDatabase(false); try { db.BeginTransaction(); using(var package = Package.Open(filename, FileMode.Open, FileAccess.Read)) { var defRelationship = package.GetRelationshipsByType("http://schemas.octgn.org/set/definition").First(); var definition = package.GetPart(defRelationship.TargetUri); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.IgnoreWhitespace = true; using(Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(GamesRepository), "CardSet.xsd")) using(XmlReader reader = XmlReader.Create(s)) settings.Schemas.Add(null, reader); Set set; // Read the cards using(var reader = XmlReader.Create(definition.GetStream(), settings)) { reader.ReadToFollowing("set"); // <?xml ... ?> set = new Set(filename, reader, repository); if(set.Game != this) throw new ApplicationException(string.Format("The set '{0}' is not built for the game '{1}'.", set.Name, Name)); if(set.GameVersion.Major != Version.Major || set.GameVersion.Minor != Version.Minor) throw new ApplicationException(string.Format("The set '{0}' is incompatible with the installed game version.\nGame version: {1:2}\nSet made for version: {2:2}.", set.Name, Version, set.GameVersion)); InsertSet(set); if(reader.IsStartElement("packaging")) { reader.ReadStartElement(); // <packaging> while(reader.IsStartElement("pack")) { string xml = reader.ReadOuterXml(); var pack = new Pack(set, xml); InsertPack(pack, xml, set.Id); } reader.ReadEndElement(); // </packaging> } if(reader.IsStartElement("markers")) { reader.ReadStartElement(); // <markers> while(reader.IsStartElement("marker")) { reader.MoveToAttribute("name"); var markerName = reader.Value; reader.MoveToAttribute("id"); var markerId = new Guid(reader.Value); var markerImageUri = definition.GetRelationship("M" + markerId.ToString("N")).TargetUri; var markerUri = markerImageUri.OriginalString; if(!package.PartExists(markerImageUri)) throw new ApplicationException(string.Format("Image for marker '{0}', with URI '{1}' was not found in the package.", markerName, markerUri)); reader.Read(); // <marker /> InsertMarker(markerId, markerName, markerUri, set.Id); } reader.ReadEndElement(); // </markers> } if(reader.IsStartElement("cards")) { reader.ReadStartElement(); // <cards> while(reader.IsStartElement("card")) InsertCard(new CardModel(reader, this, set, definition, package)); reader.ReadEndElement(); // </cards> } reader.ReadEndElement(); } CopyDecks(package); package.Close(); // Commit the changes db.CommitTransaction(); if(cachedSets != null) cachedSets.Add(set); } } catch { db.RollbackTransaction(); throw; } finally { CloseDatabase(); } }
public void DeleteSet(Set set) { OpenDatabase(false); try { using(var setTable = db.OpenTable("Set", false, false)) { if(setTable.Find("id:'" + set.Id + "'", "SetPK", false, false)) { db.BeginTransaction(); try { setTable.Delete(); db.CommitTransaction(); } catch { db.RollbackTransaction(); throw; } if(cachedSets != null) cachedSets.Remove(set); } } } finally { CloseDatabase(); } }
private ObservableCollection<Set> GetAllSets() { var result = new ObservableCollection<Set>(); using (SQLiteCommand com = GamesRepository.DatabaseConnection.CreateCommand()) { com.CommandText = "SElECT * FROM [sets] WHERE [game_real_id]=(SELECT real_id FROM games WHERE id = @game_id LIMIT 1);"; com.Parameters.AddWithValue("@game_id", Id.ToString()); using (SQLiteDataReader dr = com.ExecuteReader()) { while (dr.Read()) { var guid = dr["id"] as string; if (guid == null) continue; var s = new Set { Id = Guid.Parse(guid), Name = (string) dr["name"], Game = this, GameVersion = new Version((string) dr["game_version"]), Version = new Version((string) dr["version"]), PackageName = (string) dr["package"] }; result.Add(s); } } } return result; }
private static void InsertSet(Set set) { using (SQLiteCommand com = GamesRepository.DatabaseConnection.CreateCommand()) { //Build Query var sb = new StringBuilder(); sb.Append("INSERT OR REPLACE INTO [sets]("); sb.Append("[id],[name],[game_real_id],[game_version],[version],[package]"); sb.Append(") VALUES("); sb.Append( "@id,@name,(SELECT real_id FROM games WHERE id = @game_id LIMIT 1),@game_version,@version,@package"); sb.Append(");\n"); com.CommandText = sb.ToString(); com.Parameters.AddWithValue("@id", set.Id.ToString()); com.Parameters.AddWithValue("@name", set.Name); com.Parameters.AddWithValue("@game_id", set.Game.Id.ToString()); com.Parameters.AddWithValue("@game_version", set.GameVersion.ToString()); com.Parameters.AddWithValue("@version", set.Version.ToString()); com.Parameters.AddWithValue("@package", set.PackageName); com.ExecuteNonQuery(); } if (SimpleDataTableCache.CacheExists()) { SimpleDataTableCache.ClearCache(); } }
public void InstallSet(string filename) { SQLiteTransaction trans = GamesRepository.DatabaseConnection.BeginTransaction(); 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 (GamesRepository), "CardSet.xsd")) //CardSet.xsd determines the "attributes" of a card (name, guid, alternate, dependent) 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 set = new Set(filename, reader, Repository); if (set.Game != this) throw new ApplicationException( string.Format("The set '{0}' is not built for the game '{1}'.", set.Name, Name)); if (set.GameVersion.Major != Version.Major || set.GameVersion.Minor != 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}.", set.Name, set.GameVersion)); InsertSet(set); if (reader.IsStartElement("packaging")) { reader.ReadStartElement(); // <packaging> while (reader.IsStartElement("pack")) { string xml = reader.ReadOuterXml(); var pack = new Pack(set, xml); InsertPack(pack, xml, set.Id); } reader.ReadEndElement(); // </packaging> } if (reader.IsStartElement("markers")) { reader.ReadStartElement(); // <markers> while (reader.IsStartElement("marker")) { reader.MoveToAttribute("name"); string markerName = reader.Value; reader.MoveToAttribute("id"); var markerId = new Guid(reader.Value); Uri markerImageUri = definition.GetRelationship("M" + markerId.ToString("N")).TargetUri; string markerUri = markerImageUri.OriginalString; if (!package.PartExists(markerImageUri)) throw new ApplicationException( string.Format( "Image for marker '{0}', with URI '{1}' was not found in the package.", markerName, markerUri)); reader.Read(); // <marker /> InsertMarker(markerId, markerName, markerUri, set.Id); } reader.ReadEndElement(); // </markers> } if (reader.IsStartElement("cards")) { reader.ReadStartElement(); // <cards> while (reader.IsStartElement("card")) InsertCard(new CardModel(reader, this, set, definition, package)); // cards are parsed through the CardModel constructor, which are then inserted individually into the database reader.ReadEndElement(); // </cards> } reader.ReadEndElement(); } CopyDecks(package); package.Close(); // Commit the changes trans.Commit(); } } catch (Exception e) { trans.Rollback(); throw e; } if (SimpleDataTableCache.CacheExists()) { SimpleDataTableCache.ClearCache(); } }
public Set GetSetByRealId(long id) { using (SQLiteCommand com = GamesRepository.DatabaseConnection.CreateCommand()) { com.CommandText = "SElECT id, name, game_version, version, package FROM [sets] WHERE [real_id]=@id;"; com.Parameters.AddWithValue("@id", id.ToString(CultureInfo.InvariantCulture)); using (SQLiteDataReader dr = com.ExecuteReader()) { if (dr.Read()) { var did = dr["id"] as string; if (did != null) { var s = new Set { Id = Guid.Parse(did), Name = (string)dr["name"], Game = this, GameVersion = new Version((string)dr["game_version"]), Version = new Version((string)dr["version"]), PackageName = (string)dr["package"] }; return s; } } } } return null; }
public void DeleteSet(Set set) { if (set == null) return; using (SQLiteCommand com = GamesRepository.DatabaseConnection.CreateCommand()) { com.CommandText = "DELETE FROM [sets] WHERE [id]=@id;"; com.Parameters.AddWithValue("@id", set.Id.ToString()); com.ExecuteNonQuery(); } if (SimpleDataTableCache.CacheExists()) { SimpleDataTableCache.ClearCache(); } }
private void InsertSet(Set set) { using (var setTable = db.OpenTable("Set", false, false)) { if (setTable.Find("id:'" + set.Id + "'", "SetPK", false, false)) throw new ApplicationException(string.Format("The set '{0}' is already installed.", set.Id)); setTable.Insert(); setTable.PutGuid("id", set.Id); setTable.PutString("name", set.Name); setTable.PutString("gameVersion", set.GameVersion.ToString()); setTable.PutString("version", set.Version.ToString()); setTable.PutString("package", set.packageName); setTable.Post(); } }