From ada13f6578533485739354fe6efb2369d8ee696d Mon Sep 17 00:00:00 2001 From: ctrlaltca Date: Fri, 31 May 2019 17:48:30 +0200 Subject: [PATCH] Card Database converter (#3694) * Database converter * Fix win compilation and NSIS installer * Maybe fix windows again * Re-fix windows --- CMakeLists.txt | 7 +++ cmake/NSIS.template.in | 1 + cockatrice/src/carddatabase.h | 2 +- dbconverter/CMakeLists.txt | 102 ++++++++++++++++++++++++++++++++++ dbconverter/src/main.cpp | 67 ++++++++++++++++++++++ dbconverter/src/main.h | 20 +++++++ dbconverter/src/mocks.cpp | 53 ++++++++++++++++++ dbconverter/src/mocks.h | 50 +++++++++++++++++ 8 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 dbconverter/CMakeLists.txt create mode 100644 dbconverter/src/main.cpp create mode 100644 dbconverter/src/main.h create mode 100644 dbconverter/src/mocks.cpp create mode 100644 dbconverter/src/mocks.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5029a3f4..dd717a6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -255,6 +255,13 @@ if(WITH_ORACLE) SET(CPACK_INSTALL_CMAKE_PROJECTS "Oracle;Oracle;ALL;/" ${CPACK_INSTALL_CMAKE_PROJECTS}) endif() +# Compile dbconverter (default on) +option(WITH_DBCONVERTER "build oracle" ON) +if(WITH_DBCONVERTER) + add_subdirectory(dbconverter) + SET(CPACK_INSTALL_CMAKE_PROJECTS "Dbconverter;Dbconverter;ALL;/" ${CPACK_INSTALL_CMAKE_PROJECTS}) +endif() + # Compile tests (default off) option(TEST "build tests" OFF) if(TEST) diff --git a/cmake/NSIS.template.in b/cmake/NSIS.template.in index fdc01520..96b0d79e 100644 --- a/cmake/NSIS.template.in +++ b/cmake/NSIS.template.in @@ -213,6 +213,7 @@ ${AndIf} ${FileExists} "$INSTDIR\portable.dat" Delete "$INSTDIR\uninstall.exe" Delete "$INSTDIR\cockatrice.exe" Delete "$INSTDIR\oracle.exe" + Delete "$INSTDIR\dbconverter.exe" Delete "$INSTDIR\servatrice.exe" Delete "$INSTDIR\Qt*.dll" Delete "$INSTDIR\libmysql.dll" diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index 0390973e..b1890914 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -437,7 +437,7 @@ public slots: LoadStatus loadCardDatabases(); void addCard(CardInfoPtr card); void addSet(CardSetPtr set); -private slots: +protected slots: LoadStatus loadCardDatabase(const QString &path); signals: void cardDatabaseLoadingFailed(); diff --git a/dbconverter/CMakeLists.txt b/dbconverter/CMakeLists.txt new file mode 100644 index 00000000..ca3167b0 --- /dev/null +++ b/dbconverter/CMakeLists.txt @@ -0,0 +1,102 @@ +# CMakeLists for dbconverter directory + +PROJECT(Dbconverter VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") + +SET(dbconverter_SOURCES + src/main.cpp + src/mocks.cpp + ../cockatrice/src/carddatabase.cpp + ../cockatrice/src/carddbparser/carddatabaseparser.cpp + ../cockatrice/src/carddbparser/cockatricexml3.cpp + ../cockatrice/src/carddbparser/cockatricexml4.cpp + ${VERSION_STRING_CPP} +) + +# Qt5 +find_package(Qt5 COMPONENTS Network Widgets REQUIRED) +set(dbconverter_QT_MODULES Qt5::Core Qt5::Network Qt5::Widgets) +SET(QT_DONT_USE_QTGUI TRUE) + +# Build servatrice binary and link it +ADD_EXECUTABLE(dbconverter MACOSX_BUNDLE ${dbconverter_SOURCES} ${dbconverter_MOC_SRCS}) + +if(MSVC) + TARGET_LINK_LIBRARIES(dbconverter ${dbconverter_QT_MODULES} Qt5::WinMain) +else() + TARGET_LINK_LIBRARIES(dbconverter ${dbconverter_QT_MODULES}) +endif() + +# install rules +if(UNIX) + if(APPLE) + set(MACOSX_BUNDLE_INFO_STRING "${PROJECT_NAME}") + set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.cockatrice.${PROJECT_NAME}") + set(MACOSX_BUNDLE_LONG_VERSION_STRING "${PROJECT_NAME}-${PROJECT_VERSION}") + set(MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME}) + set(MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION}) + set(MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}) + + INSTALL(TARGETS dbconverter BUNDLE DESTINATION ./) + else() + # Assume linux + INSTALL(TARGETS dbconverter RUNTIME DESTINATION bin/) + endif() +elseif(WIN32) + INSTALL(TARGETS dbconverter RUNTIME DESTINATION ./) +endif() + +if(APPLE) + # these needs to be relative to CMAKE_INSTALL_PREFIX + set(plugin_dest_dir dbconverter.app/Contents/Plugins) + set(qtconf_dest_dir dbconverter.app/Contents/Resources) + get_filename_component(QT_LIBRARY_DIR "${QT_LIBRARY_DIR}/.." ABSOLUTE) + + # qt5 plugins: platforms, sqldrivers/mysql + install(DIRECTORY "${QT_PLUGINS_DIR}/" DESTINATION ${plugin_dest_dir} COMPONENT Runtime + FILES_MATCHING + PATTERN "*.dSYM" EXCLUDE + PATTERN "*_debug.dylib" EXCLUDE + PATTERN "platforms/*.dylib" + ) + + install(CODE " + file(WRITE \"\${CMAKE_INSTALL_PREFIX}/${qtconf_dest_dir}/qt.conf\" \"[Paths] +Plugins = Plugins +Translations = Resources/translations\") + " COMPONENT Runtime) + + install(CODE " + file(GLOB_RECURSE QTPLUGINS + \"\${CMAKE_INSTALL_PREFIX}/${plugin_dest_dir}/*.dylib\") + set(BU_CHMOD_BUNDLE_ITEMS ON) + include(BundleUtilities) + fixup_bundle(\"\${CMAKE_INSTALL_PREFIX}/dbconverter.app\" \"\${QTPLUGINS}\" \"${QT_LIBRARY_DIR};${MYSQLCLIENT_LIBRARY_DIR}\") + " COMPONENT Runtime) +endif() + +if(WIN32) + # these needs to be relative to CMAKE_INSTALL_PREFIX + set(plugin_dest_dir Plugins) + set(qtconf_dest_dir .) + + install(DIRECTORY "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/${CMAKE_BUILD_TYPE}/" DESTINATION ./ FILES_MATCHING PATTERN "*.dll") + + # qt5 plugins: platforms, sqldrivers/mysql + install(DIRECTORY "${QT_PLUGINS_DIR}/" DESTINATION ${plugin_dest_dir} COMPONENT Runtime + FILES_MATCHING REGEX "(platforms/.*)\\.dll" + REGEX ".*d\\.dll" EXCLUDE) + + install(CODE " + file(WRITE \"\${CMAKE_INSTALL_PREFIX}/${qtconf_dest_dir}/qt.conf\" \"[Paths] +Plugins = Plugins +Translations = Resources/translations\") + " COMPONENT Runtime) + + install(CODE " + file(GLOB_RECURSE QTPLUGINS + \"\${CMAKE_INSTALL_PREFIX}/${plugin_dest_dir}/*.dll\") + set(BU_CHMOD_BUNDLE_ITEMS ON) + include(BundleUtilities) + fixup_bundle(\"\${CMAKE_INSTALL_PREFIX}/dbconverter.exe\" \"\${QTPLUGINS}\" \"${QT_LIBRARY_DIR}\") + " COMPONENT Runtime) +endif() diff --git a/dbconverter/src/main.cpp b/dbconverter/src/main.cpp new file mode 100644 index 00000000..17f6e974 --- /dev/null +++ b/dbconverter/src/main.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2019 by Fabio Bas * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include +#include +#include +#include + +#include "version_string.h" +#include "mocks.h" +#include "main.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + app.setOrganizationName("Cockatrice"); + app.setApplicationName("Dbconverter"); + app.setApplicationVersion(VERSION_STRING); + + QCommandLineParser parser; + parser.addPositionalArgument("olddb", "Read existing card database from "); + parser.addPositionalArgument("newdb", "Write new card database to "); + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + + QString oldDbPath; + QString newDbPath; + QStringList args = parser.positionalArguments(); + if (args.count() == 2) + { + oldDbPath = QFileInfo(args.at(0)).absoluteFilePath(); + newDbPath = QFileInfo(args.at(1)).absoluteFilePath(); + } else { + qCritical() << "Usage: dbconverter "; + parser.showHelp(1); + exit(0); + } + + settingsCache = new SettingsCache; + CardDatabaseConverter *db = new CardDatabaseConverter; + + qInfo() << "---------------------------------------------"; + qInfo() << "Loading cards from" << oldDbPath; + db->loadCardDatabase(oldDbPath); + qInfo() << "---------------------------------------------"; + qInfo() << "Saving cards to" << newDbPath; + db->saveCardDatabase(newDbPath); + qInfo() << "---------------------------------------------"; +} diff --git a/dbconverter/src/main.h b/dbconverter/src/main.h new file mode 100644 index 00000000..31bdb575 --- /dev/null +++ b/dbconverter/src/main.h @@ -0,0 +1,20 @@ +#ifndef MAIN_H +#define MAIN_H + +#include "../../cockatrice/src/carddatabase.h" +#include "../../cockatrice/src/carddbparser/cockatricexml4.h" + +class CardDatabaseConverter : public CardDatabase +{ +public: + LoadStatus loadCardDatabase(const QString &path) { + return CardDatabase::loadCardDatabase(path); + } + + bool saveCardDatabase(const QString &fileName) { + CockatriceXml4Parser parser; + return parser.saveToFile(sets, cards, fileName); + } +}; + +#endif diff --git a/dbconverter/src/mocks.cpp b/dbconverter/src/mocks.cpp new file mode 100644 index 00000000..dc4b5a78 --- /dev/null +++ b/dbconverter/src/mocks.cpp @@ -0,0 +1,53 @@ + +#include "mocks.h" + +void CardDatabaseSettings::setSortKey(QString /* shortName */, unsigned int /* sortKey */){}; +void CardDatabaseSettings::setEnabled(QString /* shortName */, bool /* enabled */){}; +void CardDatabaseSettings::setIsKnown(QString /* shortName */, bool /* isknown */){}; +unsigned int CardDatabaseSettings::getSortKey(QString /* shortName */) +{ + return 0; +}; +bool CardDatabaseSettings::isEnabled(QString /* shortName */) +{ + return true; +}; +bool CardDatabaseSettings::isKnown(QString /* shortName */) +{ + return true; +}; + +SettingsCache::SettingsCache() +{ + cardDatabaseSettings = new CardDatabaseSettings(); +}; +SettingsCache::~SettingsCache() +{ + delete cardDatabaseSettings; +}; +QString SettingsCache::getCustomCardDatabasePath() const +{ + return ""; +} +QString SettingsCache::getCardDatabasePath() const +{ + return ""; +} +QString SettingsCache::getTokenDatabasePath() const +{ + return ""; +} +QString SettingsCache::getSpoilerCardDatabasePath() const +{ + return ""; +} +CardDatabaseSettings &SettingsCache::cardDatabase() const +{ + return *cardDatabaseSettings; +} + +void PictureLoader::clearPixmapCache(CardInfoPtr /* card */) +{ +} + +SettingsCache *settingsCache; diff --git a/dbconverter/src/mocks.h b/dbconverter/src/mocks.h new file mode 100644 index 00000000..a7452106 --- /dev/null +++ b/dbconverter/src/mocks.h @@ -0,0 +1,50 @@ +/* + * Beware of this preprocessor hack used to redefine the settingCache class + * instead of including it and all of its dependencies. + */ + +#include +#include + +#define SETTINGSCACHE_H +#define PICTURELOADER_H + +#include "../../cockatrice/src/carddatabase.h" + +class CardDatabaseSettings +{ +public: + void setSortKey(QString shortName, unsigned int sortKey); + void setEnabled(QString shortName, bool enabled); + void setIsKnown(QString shortName, bool isknown); + + unsigned int getSortKey(QString shortName); + bool isEnabled(QString shortName); + bool isKnown(QString shortName); +}; + +class SettingsCache : public QObject +{ + Q_OBJECT +private: + CardDatabaseSettings *cardDatabaseSettings; + +public: + SettingsCache(); + ~SettingsCache(); + QString getCustomCardDatabasePath() const; + QString getCardDatabasePath() const; + QString getTokenDatabasePath() const; + QString getSpoilerCardDatabasePath() const; + CardDatabaseSettings &cardDatabase() const; +signals: + void cardDatabasePathChanged(); +}; + +extern SettingsCache *settingsCache; + +class PictureLoader +{ +public: + static void clearPixmapCache(CardInfoPtr card); +};