瀏覽代碼

Show error message if vcmi unable to access data directory instead of
silent crash

Ivan Savenko 1 年之前
父節點
當前提交
2b7131cfea
共有 3 個文件被更改,包括 20 次插入3 次删除
  1. 8 1
      client/CMT.cpp
  2. 3 1
      lib/filesystem/CFileInputStream.cpp
  3. 9 1
      lib/filesystem/CFilesystemLoader.cpp

+ 8 - 1
client/CMT.cpp

@@ -208,7 +208,14 @@ int main(int argc, char * argv[])
 	logGlobal->info("The log file will be saved to %s", logPath);
 
 	// Init filesystem and settings
-	preinitDLL(::console, false);
+	try
+	{
+		preinitDLL(::console, false);
+	}
+	catch (const DataLoadingException & e)
+	{
+		handleFatalError(e.what(), true);
+	}
 
 	Settings session = settings.write["session"];
 	auto setSettingBool = [&](std::string key, std::string arg) {

+ 3 - 1
lib/filesystem/CFileInputStream.cpp

@@ -10,6 +10,8 @@
 #include "StdInc.h"
 #include "CFileInputStream.h"
 
+#include "../ExceptionsCommon.h"
+
 VCMI_LIB_NAMESPACE_BEGIN
 
 CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size)
@@ -18,7 +20,7 @@ CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 st
 	fileStream{file.c_str(), std::ios::in | std::ios::binary}
 {
 	if (fileStream.fail())
-		throw std::runtime_error("File " + file.string() + " isn't available.");
+		throw DataLoadingException("Failed to open file '" + file.string() + "'. Reason: " + strerror(errno) );
 
 	if (dataSize == 0)
 	{

+ 9 - 1
lib/filesystem/CFilesystemLoader.cpp

@@ -12,14 +12,22 @@
 
 #include "CFileInputStream.h"
 
+#include "../ExceptionsCommon.h"
+
 VCMI_LIB_NAMESPACE_BEGIN
 
 CFilesystemLoader::CFilesystemLoader(std::string _mountPoint, boost::filesystem::path baseDirectory, size_t depth, bool initial):
 	baseDirectory(std::move(baseDirectory)),
 	mountPoint(std::move(_mountPoint)),
-	fileList(listFiles(mountPoint, depth, initial)),
 	recursiveDepth(depth)
 {
+	try {
+		fileList = listFiles(mountPoint, depth, initial);
+	}
+	catch (const boost::filesystem::filesystem_error & e) {
+		throw DataLoadingException("Failed to load content of '" + baseDirectory.string() + "'. Reason: " + e.what());
+	}
+
 	logGlobal->trace("File system loaded, %d files found", fileList.size());
 }