From 51419d70928124f8a5d05e0cce01c1ca5185ee5b Mon Sep 17 00:00:00 2001 From: elexis Date: Sun, 21 Jul 2019 01:35:39 +0000 Subject: [PATCH] Correct uncaught MapReader and MapGenerator throw statements following 2b138f47db and 2180862d40. Differential Revision: https://code.wildfiregames.com/D2089 Reviewed By: historic_bruno This was SVN commit r22521. --- .../mods/public/gui/gamesetup/gamesetup.js | 2 +- source/graphics/MapGenerator.cpp | 21 ++++++++++++++----- source/graphics/MapReader.cpp | 6 +++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup.js b/binaries/data/mods/public/gui/gamesetup/gamesetup.js index c02c097517..2b3d503133 100644 --- a/binaries/data/mods/public/gui/gamesetup/gamesetup.js +++ b/binaries/data/mods/public/gui/gamesetup/gamesetup.js @@ -1713,7 +1713,7 @@ function getFilteredMaps(filterFunc) let file = g_GameAttributes.mapPath + mapFile; let mapData = loadMapData(file); - if (!mapData.settings || filterFunc && !filterFunc(mapData.settings.Keywords || [])) + if (!mapData || !mapData.settings || filterFunc && !filterFunc(mapData.settings.Keywords || [])) continue; maps.push({ diff --git a/source/graphics/MapGenerator.cpp b/source/graphics/MapGenerator.cpp index b033ac55db..a1dfb71d7e 100644 --- a/source/graphics/MapGenerator.cpp +++ b/source/graphics/MapGenerator.cpp @@ -334,14 +334,28 @@ JS::Value CMapGeneratorWorker::LoadHeightmap(ScriptInterface::CxPrivate* pCxPriv // See CMapReader::UnpackTerrain, CMapReader::ParseTerrain for the reordering JS::Value CMapGeneratorWorker::LoadMapTerrain(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& filename) { + CMapGeneratorWorker* self = static_cast(pCxPrivate->pCBData); + JSContext* cx = self->m_ScriptInterface->GetContext(); + JSAutoRequest rq(cx); + if (!VfsFileExists(filename)) - throw PSERROR_File_OpenFailed(); + { + self->m_ScriptInterface->ReportError( + ("Terrain file \"" + filename.string8() + "\" does not exist!").c_str()); + + return JS::UndefinedValue(); + } CFileUnpacker unpacker; unpacker.Read(filename, "PSMP"); if (unpacker.GetVersion() < CMapIO::FILE_READ_VERSION) - throw PSERROR_File_InvalidVersion(); + { + self->m_ScriptInterface->ReportError( + ("Could not load terrain file \"" + filename.string8() + "\" too old version!").c_str()); + + return JS::UndefinedValue(); + } // unpack size ssize_t patchesPerSide = (ssize_t)unpacker.UnpackSize(); @@ -384,9 +398,6 @@ JS::Value CMapGeneratorWorker::LoadMapTerrain(ScriptInterface::CxPrivate* pCxPri } } - CMapGeneratorWorker* self = static_cast(pCxPrivate->pCBData); - JSContext* cx = self->m_ScriptInterface->GetContext(); - JSAutoRequest rq(cx); JS::RootedValue returnValue(cx, JS::ObjectValue(*JS_NewPlainObject(cx))); self->m_ScriptInterface->SetProperty(returnValue, "height", heightmap); self->m_ScriptInterface->SetProperty(returnValue, "textureNames", textureNames); diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index d3b69c5501..f28dae2a6c 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Wildfire Games. +/* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -102,7 +102,7 @@ void CMapReader::LoadMap(const VfsPath& pathname, JSRuntime* rt, JS::HandleValu // check oldest supported version if (file_format_version < FILE_READ_VERSION) - throw PSERROR_File_InvalidVersion(); + throw PSERROR_Game_World_MapLoadFailed("Could not load terrain file - too old version!"); // delete all existing entities if (pSimulation2) @@ -451,7 +451,7 @@ void CXMLReader::Init(const VfsPath& xml_filename) node_idx = entity_idx = 0; if (xmb_file.Load(g_VFS, xml_filename, "scenario") != PSRETURN_OK) - throw PSERROR_File_ReadFailed(); + throw PSERROR_Game_World_MapLoadFailed("Could not read map XML file!"); // define the elements and attributes that are frequently used in the XML file, // so we don't need to do lots of string construction and comparison when