Skip to content

Commit

Permalink
Merge pull request #4971 from myk002/myk_version_mismatch_error
Browse files Browse the repository at this point in the history
[Core] make version mismatch error message more informative
  • Loading branch information
myk002 authored Sep 30, 2024
2 parents 28cc339 + d77f0c5 commit 2345bc8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Template for new versions:

## Documentation
- Dreamfort: add link to Dreamfort tutorial youtube series: https://www.youtube.com/playlist?list=PLzXx9JcB9oXxmrtkO1y8ZXzBCFEZrKxve
- The error message that comes up if there is a version mismatch between DF and the installed DFHack now informs you which DF versions are supported by the installed version of DFHack

## API

Expand Down
22 changes: 21 additions & 1 deletion library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ using namespace DFHack;
using namespace df::enums;
using df::global::init;
using df::global::world;
using std::string;

// FIXME: A lot of code in one file, all doing different things... there's something fishy about it.

Expand Down Expand Up @@ -1611,7 +1612,26 @@ bool Core::InitMainThread() {
}
else
{
fatal("Not a known DF version.\n");
std::stringstream msg;
msg << "Not a known DF version.\n"
"\n"
"Please make sure that you have a version\n"
"of DFHack installed that matches the version\n"
"of Dwarf Fortress.\n"
"\n";
auto supported_versions = vif->getVersionInfosForCurOs();
if (supported_versions.size()) {
msg << "DF releases supported by this version of DFHack:\n\n";
for (auto & sv : supported_versions) {
string ver = sv->getVersion();
if (ver.starts_with("v0.")) { // translate "v0.50" to the standard format: "v50"
ver = "v" + ver.substr(3);
}
msg << " " << ver << "\n";
}
msg << "\n";
}
fatal(msg.str());
}
errorstate = true;
return false;
Expand Down
21 changes: 19 additions & 2 deletions library/VersionInfoFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ distribution.
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;

#include "VersionInfoFactory.h"
#include "VersionInfo.h"
#include "Error.h"
#include "Memory.h"
#include "MemAccess.h"
#include "PluginManager.h"
using namespace DFHack;

#include <tinyxml.h>

using namespace DFHack;
using std::cerr;
using std::endl;
using std::string;
using std::vector;

VersionInfoFactory::VersionInfoFactory()
{
error = false;
Expand Down Expand Up @@ -77,6 +81,19 @@ std::shared_ptr<const VersionInfo> VersionInfoFactory::getVersionInfoByPETimesta
return nullptr;
}

std::vector<std::shared_ptr<const VersionInfo>> VersionInfoFactory::getVersionInfosForCurOs() const {
static const OSType expected = VersionInfo::getCurOS();

std::vector<std::shared_ptr<const VersionInfo>> ret;

for (const auto& version : versions) {
if (version->getOS() == expected && version->getVersion().find("LOCAL") == std::string::npos)
ret.emplace_back(version);
}

return ret;
}

static uintptr_t to_addr(const char * cstr) {
if (sizeof(uintptr_t) == sizeof(unsigned long))
return strtoul(cstr, 0, 0);
Expand Down
20 changes: 13 additions & 7 deletions library/include/VersionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ namespace DFHack
OS = rhs.OS;
};

static OSType getCurOS() {
#if defined(_WIN32)
const OSType expected = OS_WINDOWS;
#elif defined(_DARWIN)
const OSType expected = OS_APPLE;
#else
const OSType expected = OS_LINUX;
#endif
return expected;
}

uintptr_t getBase () const { return base; };
intptr_t getRebaseDelta() const { return rebase_delta; }
void setBase (const uintptr_t _base) { base = _base; };
Expand Down Expand Up @@ -171,13 +182,8 @@ namespace DFHack
};

void ValidateOS() {
#if defined(_WIN32)
const OSType expected = OS_WINDOWS;
#elif defined(_DARWIN)
const OSType expected = OS_APPLE;
#else
const OSType expected = OS_LINUX;
#endif
static const OSType expected = getCurOS();

if (expected != getOS()) {
std::cerr << "OS mismatch; resetting to " << int(expected) << std::endl;
setOS(expected);
Expand Down
1 change: 1 addition & 0 deletions library/include/VersionInfoFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace DFHack
bool isInErrorState() const {return error;};
std::shared_ptr<const VersionInfo> getVersionInfoByMD5(std::string md5string) const;
std::shared_ptr<const VersionInfo> getVersionInfoByPETimestamp(uintptr_t timestamp) const;
std::vector<std::shared_ptr<const VersionInfo>> getVersionInfosForCurOs() const;
// trash existing list
void clear();
private:
Expand Down

0 comments on commit 2345bc8

Please sign in to comment.