Skip to content

Commit

Permalink
Increase flexability of version.txt handling
Browse files Browse the repository at this point in the history
  • Loading branch information
oblivioncth committed Jun 30, 2024
1 parent 7fb8242 commit f8eefdd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 48 deletions.
66 changes: 53 additions & 13 deletions lib/include/fp/fp-install.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ inline const QString NAME = u"Flashpoint"_s;
class QX_ERROR_TYPE(InstallError, "Fp::InstallError", 1100)
{
friend class Install;
//-Class Enums-------------------------------------------------------------
//-Class Enums-------------------------------------------------------------
public:
enum Type
{
Expand All @@ -43,7 +43,7 @@ class QX_ERROR_TYPE(InstallError, "Fp::InstallError", 1100)
DatapackSourceMissing
};

//-Class Variables-------------------------------------------------------------
//-Class Variables-------------------------------------------------------------
private:
static inline const QHash<Type, QString> ERR_STRINGS{
{NoError, u""_s},
Expand All @@ -52,16 +52,16 @@ class QX_ERROR_TYPE(InstallError, "Fp::InstallError", 1100)
{DatapackSourceMissing, u"Expected datapack source missing."_s}
};

//-Instance Variables-------------------------------------------------------------
//-Instance Variables-------------------------------------------------------------
private:
Type mType;
QString mSpecific;

//-Constructor-------------------------------------------------------------
//-Constructor-------------------------------------------------------------
private:
InstallError(Type t = NoError, const QString& s = {});

//-Instance Functions-------------------------------------------------------------
//-Instance Functions-------------------------------------------------------------
public:
bool isValid() const;
Type type() const;
Expand All @@ -77,9 +77,10 @@ class QX_ERROR_TYPE(InstallError, "Fp::InstallError", 1100)
class FP_FP_EXPORT Install
{
friend class Toolkit;
//-Class Enums---------------------------------------------------------------------------------------------------

//-Inner Class---------------------------------------------------------------------------------------------------
public:
enum class Edition {Ultimate, Infinity, Core};
class VersionInfo;

//-Class Variables-----------------------------------------------------------------------------------------------
public: // Ugh
Expand Down Expand Up @@ -111,9 +112,6 @@ class FP_FP_EXPORT Install
// Main datapack source
static inline const QString MAIN_DATAPACK_SOURCE = u"Flashpoint Project"_s;

// Regex
static inline const QRegularExpression VERSION_NUMBER_REGEX = QRegularExpression(u"[fF]lashpoint (?<version>.*?) "_s);

//-Instance Variables-----------------------------------------------------------------------------------------------
private:
// Validity
Expand All @@ -135,6 +133,9 @@ class FP_FP_EXPORT Install
std::shared_ptr<QFile> mExecsJsonFile;
std::unique_ptr<QFile> mVersionFile;

// Info
std::shared_ptr<VersionInfo> mVersionInfo;

// Settings
Config mConfig;
Preferences mPreferences;
Expand Down Expand Up @@ -167,9 +168,7 @@ class FP_FP_EXPORT Install
Qx::Error error() const;

// General information
Edition edition() const;
QString nameVersionString() const;
Qx::VersionNumber version() const;
std::shared_ptr<VersionInfo> versionInfo() const;
QString launcherChecksum() const;

// Facilities
Expand All @@ -194,6 +193,47 @@ class FP_FP_EXPORT Install
QDir platformLogosDirectory() const;
};

class Install::VersionInfo
{
//-Class Enums---------------------------------------------------------------------------------------------------
public:
enum Edition {Ultimate, Infinity, Core};

//-Class Variables
private:
// Regex
static inline const QString VER_TXT_GRP_EDITIONA = "e1";
static inline const QString VER_TXT_GRP_EDITIONB = "e2";
static inline const QString VER_TXT_GRP_VERSION = "v";
static inline const QString VER_TXT_GRP_NICK = "n";
static inline const QRegularExpression VER_TXT_REGEX = QRegularExpression(
uR"([fF]lashpoint\s+(?<)"_s + VER_TXT_GRP_EDITIONA +
uR"(_s>[a-zA-Z]+)?\s*(?<)"_s + VER_TXT_GRP_VERSION +
uR"(>[0-9]+(?:\.[0-9]+)?)?\s*(?<)"_s + VER_TXT_GRP_EDITIONB +
uR"(>[a-zA-Z]+)?\s+-\s+(?<)"_s + VER_TXT_GRP_NICK +
uR"(>.*)))"_s
);

//-Instance Variables-----------------------------------------------------------------------------------------------
private:
QString mFullString;
Edition mEdition;
Qx::VersionNumber mVersion;
QString mNickname;

//-Constructor-------------------------------------------------------------------------------------------------
public:
VersionInfo(const QString& verTxtStr);

//-Instance Functions------------------------------------------------------------------------------------------------------
public:
bool isNull() const;
QString fullString() const;
Edition edition() const;
Qx::VersionNumber version() const;
QString nickname() const;
};

}


Expand Down
81 changes: 46 additions & 35 deletions lib/src/fp-install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ Install::Install(QString installPath, bool preloadPlaylists) :
return;
}

// Read version info (assumes file exists from earlier, only read first line in case there is a trailing newline)
QString verTxtStr;
Qx::readTextFromFile(verTxtStr, *mVersionFile, Qx::Start, Qx::TextPos(Qx::First, Qx::Last));
mVersionInfo = std::make_shared<VersionInfo>(verTxtStr);

// Ensure expected datapack source exists on Infinity
if(edition() == Edition::Infinity && (!mPreferences.gameDataSources || !mPreferences.gameDataSources->contains(MAIN_DATAPACK_SOURCE)))
if(!mVersionInfo->isNull() && mVersionInfo->edition() == VersionInfo::Infinity && (!mPreferences.gameDataSources || !mPreferences.gameDataSources->contains(MAIN_DATAPACK_SOURCE)))
{
mError = InstallError(InstallError::DatapackSourceMissing, MAIN_DATAPACK_SOURCE);
return;
Expand Down Expand Up @@ -197,6 +202,7 @@ void Install::nullify()
mPreferencesJsonFile.reset();
mServicesJsonFile.reset();
mVersionFile.reset();
mVersionInfo.reset();
if(mMacroResolver)
qxDelete(mMacroResolver);
if(mDatabase)
Expand All @@ -216,40 +222,7 @@ void Install::nullify()
bool Install::isValid() const { return mValid; }
Qx::Error Install::error() const { return mError; }

Install::Edition Install::edition() const
{
QString nameVer = nameVersionString();

return nameVer.contains(u"ultimate"_s, Qt::CaseInsensitive) ? Edition::Ultimate :
nameVer.contains(u"infinity"_s, Qt::CaseInsensitive) ? Edition::Infinity :
Edition::Core;
}

QString Install::nameVersionString() const
{
// Check version file (only read first line in case there is a trailing newline character)
QString readVersion = QString();
if(mVersionFile->exists())
Qx::readTextFromFile(readVersion, *mVersionFile, Qx::Start, Qx::TextPos(Qx::First, Qx::Last));

return readVersion;
}

Qx::VersionNumber Install::version() const
{
QString nameVer = nameVersionString();
QRegularExpressionMatch versionMatch = VERSION_NUMBER_REGEX.match(nameVer);

if(versionMatch.hasMatch())
{
Qx::VersionNumber fpVersion = Qx::VersionNumber::fromString(versionMatch.captured(u"version"_s));
if(!fpVersion.isNull())
return fpVersion;
}

qWarning("Could not determine flashpoint version number!");
return Qx::VersionNumber();
}
std::shared_ptr<Install::VersionInfo> Install::versionInfo() const { return mVersionInfo; }

QString Install::launcherChecksum() const
{
Expand All @@ -276,4 +249,42 @@ QDir Install::entryScreenshotsDirectory() const { return mEntryScreenshotsDirect
QDir Install::extrasDirectory() const { return mExtrasDirectory; }
QDir Install::platformLogosDirectory() const { return mPlatformLogosDirectory; }

//===============================================================================================================
// Install::VersionInfo
//===============================================================================================================


//-Constructor------------------------------------------------------------------------------------------------
//Public:
Install::VersionInfo::VersionInfo(const QString& verTxtStr) :
mEdition(Edition::Core)
{
QRegularExpressionMatch rem = VER_TXT_REGEX.match(verTxtStr);
if(!rem.hasMatch() ||
!(rem.hasCaptured(VER_TXT_GRP_EDITIONA) || rem.hasCaptured(VER_TXT_GRP_EDITIONB)) ||
!rem.hasCaptured(VER_TXT_GRP_VERSION) ||
!rem.hasCaptured(VER_TXT_GRP_NICK))
{
qWarning("Unknown version.txt format!");
return;
}

mFullString = verTxtStr;
QString edStr = rem.hasCaptured(VER_TXT_GRP_EDITIONA) ? rem.captured(VER_TXT_GRP_EDITIONA) : rem.captured(VER_TXT_GRP_EDITIONB);
mEdition = edStr.contains(u"ultimate"_s, Qt::CaseInsensitive) ? Edition::Ultimate :
edStr.contains(u"infinity"_s, Qt::CaseInsensitive) ? Edition::Infinity :
Edition::Core;
mVersion = Qx::VersionNumber::fromString(rem.captured(VER_TXT_GRP_VERSION));
Q_ASSERT(!mVersion.isNull()); // Regex should fail before this
mNickname = rem.captured(VER_TXT_GRP_NICK);
}

//-Instance Functions------------------------------------------------------------------------------------------------------
//Public:
bool Install::VersionInfo::isNull() const { return mFullString.isEmpty(); }
QString Install::VersionInfo::fullString() const { return mFullString; }
Install::VersionInfo::Edition Install::VersionInfo::edition() const { return mEdition; }
Qx::VersionNumber Install::VersionInfo::version() const { return mVersion; }
QString Install::VersionInfo::nickname() const { return mNickname; }

}

0 comments on commit f8eefdd

Please sign in to comment.