mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 18:55:01 +02:00
Merge pull request #8664 from huffman/fix/script-resolve-path
Fix ScriptEngine not properly resolving paths
This commit is contained in:
commit
14f8fb7c90
1 changed files with 31 additions and 6 deletions
|
@ -25,6 +25,7 @@
|
|||
#include <QtNetwork/QNetworkRequest>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
|
||||
#include <QtScript/QScriptContextInfo>
|
||||
#include <QtScript/QScriptValue>
|
||||
#include <QtScript/QScriptValueIterator>
|
||||
|
||||
|
@ -106,6 +107,25 @@ void inputControllerFromScriptValue(const QScriptValue &object, controller::Inpu
|
|||
out = qobject_cast<controller::InputController*>(object.toQObject());
|
||||
}
|
||||
|
||||
// The purpose of the following two function is to embed entity ids into entity script filenames
|
||||
// so that they show up in stacktraces
|
||||
//
|
||||
// Extract the url portion of a url that has been encoded with encodeEntityIdIntoEntityUrl(...)
|
||||
QString extractUrlFromEntityUrl(const QString& url) {
|
||||
auto parts = url.split(' ', QString::SkipEmptyParts);
|
||||
if (parts.length() > 0) {
|
||||
return parts[0];
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// Encode an entity id into an entity url
|
||||
// Example: http://www.example.com/some/path.js [EntityID:{9fdd355f-d226-4887-9484-44432d29520e}]
|
||||
QString encodeEntityIdIntoEntityUrl(const QString& url, const QString& entityID) {
|
||||
return url + " [EntityID:" + entityID + "]";
|
||||
}
|
||||
|
||||
static bool hasCorrectSyntax(const QScriptProgram& program) {
|
||||
const auto syntaxCheck = QScriptEngine::checkSyntax(program.sourceCode());
|
||||
if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) {
|
||||
|
@ -1091,14 +1111,19 @@ QUrl ScriptEngine::resolvePath(const QString& include) const {
|
|||
return expandScriptUrl(url);
|
||||
}
|
||||
|
||||
QScriptContextInfo contextInfo { currentContext()->parentContext() };
|
||||
|
||||
// we apparently weren't a fully qualified url, so, let's assume we're relative
|
||||
// to the original URL of our script
|
||||
QUrl parentURL;
|
||||
if (_parentURL.isEmpty()) {
|
||||
parentURL = QUrl(_fileNameString);
|
||||
} else {
|
||||
parentURL = QUrl(_parentURL);
|
||||
QUrl parentURL = extractUrlFromEntityUrl(contextInfo.fileName());
|
||||
if (parentURL.isEmpty()) {
|
||||
if (_parentURL.isEmpty()) {
|
||||
parentURL = QUrl(_fileNameString);
|
||||
} else {
|
||||
parentURL = QUrl(_parentURL);
|
||||
}
|
||||
}
|
||||
|
||||
// if the parent URL's scheme is empty, then this is probably a local file...
|
||||
if (parentURL.scheme().isEmpty()) {
|
||||
parentURL = QUrl::fromLocalFile(_fileNameString);
|
||||
|
@ -1323,7 +1348,7 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co
|
|||
|
||||
auto scriptCache = DependencyManager::get<ScriptCache>();
|
||||
bool isFileUrl = isURL && scriptOrURL.startsWith("file://");
|
||||
auto fileName = QString("(EntityID:%1, %2)").arg(entityID.toString(), isURL ? scriptOrURL : "EmbededEntityScript");
|
||||
auto fileName = isURL ? encodeEntityIdIntoEntityUrl(scriptOrURL, entityID.toString()) : "EmbeddedEntityScript";
|
||||
|
||||
QScriptProgram program(contents, fileName);
|
||||
if (!hasCorrectSyntax(program)) {
|
||||
|
|
Loading…
Reference in a new issue