Пример #1
0
        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();
            }
        }
Пример #2
0
 public static SimpleDataTableCache GetInstance()
 {
     if (_instance == null)
     {
         _instance = new SimpleDataTableCache();
     }
     return (_instance);
 }
Пример #3
0
 public static SimpleDataTableCache GetInstance()
 {
     if (_instance == null)
     {
         _instance = new SimpleDataTableCache();
     }
     return(_instance);
 }
Пример #4
0
        public void Apply(GamesRepository repository, bool patchInstalledSets, string patchFolder)
        {
            if (!patchInstalledSets && patchFolder == null)
            {
                return;
            }

            using (Package package = Package.Open(_filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                ReadPackageDescription(package);

                // Get the list of sets to potentially patch
                _game = repository.Games.FirstOrDefault(g => g.Id == _gameId);
                if (_game == null)
                {
                    return;
                }
                List <string> installedSets = _game.Sets.Select(s => s.PackageName).ToList();
                List <string> uninstalledSets;

                if (patchFolder != null)
                {
                    string[] files = Directory.GetFiles(patchFolder, "*.o8s");
                    uninstalledSets = files.Except(installedSets).ToList();
                    if (!patchInstalledSets)
                    {
                        installedSets = files.Intersect(installedSets).ToList();
                    }
                }
                else
                {
                    uninstalledSets = new List <string>(0);
                }

                _current = 0;
                _max     = installedSets.Count + uninstalledSets.Count;
                OnProgress();

                foreach (string set in installedSets)
                {
                    SafeApply(package, set, true);
                }

                foreach (string set in uninstalledSets)
                {
                    SafeApply(package, set, false);
                }

                if (SimpleDataTableCache.CacheExists())
                {
                    SimpleDataTableCache.ClearCache();
                }
            }
        }
Пример #5
0
 public void DeleteSet(Set set)
 {
     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();
     }
 }
Пример #6
0
        private static void InsertPack(Pack pack, string xml, Guid setId)
        {
            using (SQLiteCommand com = GamesRepository.DatabaseConnection.CreateCommand())
            {
                //Build Query
                var sb = new StringBuilder();
                sb.Append("INSERT OR REPLACE INTO [packs](");
                sb.Append("[id],[set_real_id],[name],[xml]");
                sb.Append(") VALUES(");
                sb.Append("@id,(SELECT real_id FROM sets WHERE id = @set_id LIMIT 1),@name,@xml");
                sb.Append(");\n");
                com.CommandText = sb.ToString();

                com.Parameters.AddWithValue("@id", pack.Id.ToString());
                com.Parameters.AddWithValue("@set_id", setId.ToString());
                com.Parameters.AddWithValue("@name", pack.Name);
                com.Parameters.AddWithValue("@xml", xml);
                com.ExecuteNonQuery();
            }
            if (SimpleDataTableCache.CacheExists())
            {
                SimpleDataTableCache.ClearCache();
            }
        }
Пример #7
0
        public DataTable SelectCards(string[] conditions)
        {
            // Make null-safe
            if (conditions == null)
            {
                conditions = new String[0];
            }

            // This provides multiple 'and' conditions but only one 'or' condition which
            // should be OK for now, but will not accomodate multiple 'or' conditions
            // without a re-write.
            IList <String> consAnd = new List <String>();
            IList <String> consOr  = new List <String>();

            foreach (string c in conditions)
            {
                if (c.Contains("set_id"))
                {
                    consOr.Add(c);
                }
                else
                {
                    consAnd.Add(c);
                }
            }

            StringBuilder sb;
            var           cards = new DataTable();

            if (SimpleDataTableCache.GetInstance().IsCached(conditions))
            {
                cards = SimpleDataTableCache.GetInstance().GetCache(conditions);
            }
            else
            {
                var          customProperties = new DataTable();
                IList <Guid> sets             = GUID_EMPTY;
                if (conditions != null)
                {
                    sets = conditions
                           .Where(c => c.Contains("set_id"))
                           .SelectMany(c => ExtractGuids(c))
                           .ToList();
                }


                // We need 2 different queries here because it is possible for...
                //   [cards].[game_id] <> [games].[id]
                // ...or it is possible that there are cards not attached to a set
                // in which case we need...
                //   LEFT OUTER JOIN [sets]
                // ...instead of...
                //   INNER JOIN [sets]
                // ...which is possible in theory because the columns [sets].[game_real_id]
                // and [cards].[set_real_id] are both NULLABLE. This SQL implementation should
                // produce the same query results as before.
                sb = new StringBuilder();
                if (sets.Count > 0)
                {
                    sb.Append(" SELECT *, [sets].[id] AS [set_id] ");
                    sb.Append(" FROM      [cards] ");
                    sb.Append(" INNER JOIN	[sets]	ON	[sets].[real_id]  = [cards].[set_real_id] ");
                    sb.Append(" INNER JOIN	[games]	ON	[games].[real_id] = [sets].[game_real_id] ");
                    sb.Append(" WHERE [games].[id] = @game_id ");
                    sb.Append(" AND ( ");
                    for (int i = 0; i < sets.Count; i++)
                    {
                        if (i > 0)
                        {
                            sb.Append(" OR ");
                        }
                        sb.Append(" [sets].[set_id] = @set_id" + i);
                    }
                    sb.Append(" ) ");
                }
                else
                {
                    sb.Append(" SELECT [cards].*, [sets].[id] AS [set_id] ");
                    sb.Append(" FROM 			 [cards] ");
                    sb.Append(" LEFT OUTER JOIN [sets]	ON	[sets].[real_id] = [cards].[set_real_id] ");
                    sb.Append(" WHERE [cards].[game_id] = @game_id ");
                }

                String sql = CleanupSql(sb);

#if (DEBUG)
                Debug.Write(sql);
#endif

                using (SQLiteCommand com = GamesRepository.DatabaseConnection.CreateCommand())
                {
                    // Cards query
                    com.CommandText = sql;
                    com.Parameters.AddWithValue("@game_id", Id.ToString());
                    for (int i = 0; i < sets.Count; i++)
                    {
                        com.Parameters.AddWithValue("@set_id" + i, sets[i]);
                    }
                    cards.Load(com.ExecuteReader());

                    // Custom Properties query
                    com.CommandText = "SELECT * FROM [custom_properties] WHERE [game_id] = @game_id";
                    com.Parameters.AddWithValue("@game_id", Id.ToString());
                    customProperties.Load(com.ExecuteReader());
                }

                // Update our cache
                SimpleDataTableCache.GetInstance().AddToCache(conditions, cards);
            }

            // Appears to remove the game ID guid from the column names
            string gameIdWithoutDashes = Id.ToString().Replace("-", "");
            for (int i = 0; i < cards.Columns.Count; i++)
            {
                cards.Columns[i].ColumnName = cards.Columns[i].ColumnName.Replace(gameIdWithoutDashes, "");
            }

            //Now apply the search query to it
            sb = new StringBuilder();
            for (int i = 0; i < consAnd.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append("(" + consAnd[i] + ")");
            }

            if (consOr.Count > 0)
            {
                // Do we already have some WHERE clause stuff, if so
                // we append the 'AND' on to the end of it.
                if (consAnd.Count > 0)
                {
                    sb.Append(" AND ");
                }

                sb.Append("(");
                for (int i = 0; i < consOr.Count; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(" OR ");
                    }
                    sb.Append("(" + consOr[i] + ")");
                }
                sb.Append(")");
            }

            String where = CleanupSql(sb);

            // Did we actually create a 'WHERE' condition? If so,
            // apply it to our data table.
            if (!String.IsNullOrEmpty(where))
            {
                cards.CaseSensitive = false;
                DataTable dtnw = cards.Clone();
                dtnw.Rows.Clear();

                DataRow[] rows = cards.Select(where);

                foreach (DataRow r in rows)
                {
                    dtnw.ImportRow(r);
                }

                // This isn't needed since the cards should be GC'd
                //cards.Rows.Clear();
                cards = dtnw;
            }

            return(cards);
        }
Пример #8
0
        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();
            }
        }