// This file is part of c-evo-evo. // // This software is governed by the CeCILL license under French law and // abiding by the rules of distribution of free software. You can use, // modify and/or redistribute the software under the terms of the CeCILL // license as circulated by CEA, CNRS and INRIA at the following URL // "http://www.cecill.info". // // As a counterpart to the access to the source code and rights to copy, // modify and redistribute granted by the license, users are provided only // with a limited warranty and the software's author, the holder of the // economic rights, and the successive licensors have only limited // liability. // // In this respect, the user's attention is drawn to the risks associated // with loading, using, modifying and/or developing or reproducing the // software by the user in light of its specific status of free software, // that may mean that it is complicated to manipulate, and that also // therefore means that it is reserved for developers and experienced // professionals having in-depth computer knowledge. Users are therefore // encouraged to load and test the software's suitability as regards their // requirements in conditions enabling the security of their systems and/or // data to be ensured and, more generally, to use and operate it in the // same conditions as regards security. // // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. #include "Global.h" #include "Cevo/CevoGraphicSet.h" #include "Cevo/CevoScreenTools.h" #include "Cevo/CevoTexture.h" #include #include #include #include #include #include #include namespace Cevo { bool fullScreen = false; float musicVolume = 0.5f; QRect termBounds(0, 0, 800, 480); bool musicEnabled = true; bool generateNames = true; namespace { // The CEVO_RESOURCE_DIR environment variable overrides the compile-time // install location, to run a build-tree binary against the source resources/. QString resourceDir() { const QByteArray fromEnvironment = qgetenv("CEVO_RESOURCE_DIR"); if (!fromEnvironment.isEmpty()) return QString::fromLocal8Bit(fromEnvironment); #ifdef CEVO_RESOURCE_DIR return QStringLiteral(CEVO_RESOURCE_DIR); #else return QString(); #endif } } // namespace QString getGraphicsDir() { const QString dir = resourceDir(); return dir.isEmpty() ? QString() : dir + QStringLiteral("/Graphics"); } QString getMusicDir() { const QString dir = resourceDir(); return dir.isEmpty() ? QString() : dir + QStringLiteral("/Music"); } QString getAppSharePath(const QString &path) { const QString dir = resourceDir(); return dir.isEmpty() ? path : dir + QLatin1Char('/') + path; } QString localeCode; QString localeCodeAuto() { const QLocale locale = QLocale::system(); if (locale.language() == QLocale::Chinese) { return locale.script() == QLocale::TraditionalChineseScript ? QStringLiteral("zh-Hant") : QStringLiteral("zh-Hans"); } return locale.name().section(QLatin1Char('_'), 0, 0); } QString localizedFilePath(const QString &path) { if (localeCode != QLatin1String("en")) { const QString code = localeCode.isEmpty() ? localeCodeAuto() : localeCode; const QString localized = getAppSharePath(QStringLiteral("Localization/") + code + QLatin1Char('/') + path); if (QFileInfo::exists(localized)) return localized; } return getAppSharePath(path); } namespace { QString dataDirectory; } void initDirectories(const QStringList &args) { dataDirectory = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); if (dataDirectory.isEmpty()) dataDirectory = getAppSharePath(QString()); // Override by parameter for (int i = 1; i < args.size() - 1; ++i) { if (args.at(i) == QLatin1String("-d") || args.at(i) == QLatin1String("--data-dir")) dataDirectory = args.at(i + 1); } QDir().mkpath(dataDirectory); } QString getSavedDir(bool share) { return share ? getAppSharePath(QStringLiteral("Saved")) : dataDirectory + QStringLiteral("/Saved"); } QString getMapsDir(bool share) { return share ? getAppSharePath(QStringLiteral("Maps")) : dataDirectory + QStringLiteral("/Maps"); } StringTable phrases; StringTable phrases2; bool phrases2FallenBackToEnglish = false; void loadPhrases() { phrases2FallenBackToEnglish = false; const QString languageFile = localizedFilePath(QStringLiteral("Language.txt")); if (QFileInfo::exists(languageFile)) { phrases.loadFromFile(languageFile); const QString language2File = localizedFilePath(QStringLiteral("Language2.txt")); if (QFileInfo::exists(language2File)) { phrases2.loadFromFile(language2File); } else { phrases2.loadFromFile(getAppSharePath(QStringLiteral("Language2.txt"))); phrases2FallenBackToEnglish = true; } } else { phrases.loadFromFile(getAppSharePath(QStringLiteral("Language.txt"))); phrases2.loadFromFile(getAppSharePath(QStringLiteral("Language2.txt"))); } if (phrases.getHandle(QStringLiteral("BTN_OK")) < 0) qWarning("Language.txt not found in %s", qPrintable(getAppSharePath(QString()))); } QString formatPhrase(const QString &key, const QStringList &args) { QString result = phraseToQtFormat(phrases.lookup(key)); for (const QString &arg : args) result = result.arg(arg); return result; } void loadConfig() { QSettings settings; localeCode = settings.value(QStringLiteral("Locale")).toString(); fullScreen = settings.value(QStringLiteral("ScreenMode"), 1).toInt() > 0; musicEnabled = settings.value(QStringLiteral("MusicEnabled"), true).toBool(); musicVolume = settings.value(QStringLiteral("MusicVolume"), 0.5).toFloat(); termBounds = QRect(settings.value(QStringLiteral("TermLeft"), 0).toInt(), settings.value(QStringLiteral("TermTop"), 0).toInt(), settings.value(QStringLiteral("TermWidth"), 800).toInt(), settings.value(QStringLiteral("TermHeight"), 480).toInt()); gamma = settings.value(QStringLiteral("Gamma"), 100).toInt(); } void saveConfig() { QSettings settings; settings.setValue(QStringLiteral("Locale"), localeCode); settings.setValue(QStringLiteral("ScreenMode"), fullScreen ? 1 : 0); settings.setValue(QStringLiteral("MusicEnabled"), musicEnabled); settings.setValue(QStringLiteral("MusicVolume"), musicVolume); settings.setValue(QStringLiteral("TermLeft"), termBounds.left()); settings.setValue(QStringLiteral("TermTop"), termBounds.top()); settings.setValue(QStringLiteral("TermWidth"), termBounds.width()); settings.setValue(QStringLiteral("TermHeight"), termBounds.height()); settings.setValue(QStringLiteral("Gamma"), gamma); } void loadAssets() { graphicsDir = getGraphicsDir(); loadPhrases(); loadFonts(localizedFilePath(QStringLiteral("Fonts.txt"))); Templates = loadGraphicSet(QStringLiteral("Templates") + QLatin1String(PngExt), false); HGrSystem = loadGraphicSet(QStringLiteral("System") + QLatin1String(PngExt)); HGrSystem2 = loadGraphicSet(QStringLiteral("System2") + QLatin1String(PngExt)); static Dpi::Bitmap colorsBitmap; static Dpi::Bitmap bigImpBitmap; static Texture mainTexture; colorsBitmap.loadFromFile(graphicsDir + QStringLiteral("/Colors") + QLatin1String(PngExt)); paper.loadFromFile(graphicsDir + QStringLiteral("/Paper") + QLatin1String(JpgExt)); bigImpBitmap.loadFromFile(graphicsDir + QStringLiteral("/Icons") + QLatin1String(PngExt)); Colors = &colorsBitmap; bigImp = &bigImpBitmap; MainTexture = &mainTexture; } QString phraseToQtFormat(const QString &text) { QString converted; int argNumber = 0; for (int i = 0; i < text.length(); ++i) { if (text.at(i) == QLatin1Char('%') && i + 1 < text.length()) { const QChar next = text.at(i + 1); if (next == QLatin1Char('d') || next == QLatin1Char('s')) { converted += QLatin1Char('%') + QString::number(++argNumber); ++i; continue; } if (next == QLatin1Char('%')) { converted += QLatin1Char('%'); ++i; continue; } } converted += text.at(i); } return converted; } } // namespace Cevo