Fix method hashes with default arguments

This commit is contained in:
David Snopek
2023-09-10 12:36:44 -05:00
parent fc99492d30
commit 0d13727c97
7 changed files with 937 additions and 8 deletions

View File

@@ -32,6 +32,7 @@
#include "core/config/engine.h"
#include "core/core_constants.h"
#include "core/extension/gdextension_compat_hashes.h"
#include "core/io/file_access.h"
#include "core/io/json.h"
#include "core/templates/pair.h"
@@ -884,11 +885,18 @@ Dictionary GDExtensionAPIDump::generate_extension_api() {
d2["hash"] = method->get_hash();
Vector<uint32_t> compat_hashes = ClassDB::get_method_compatibility_hashes(class_name, method_name);
Array compatibility;
if (compat_hashes.size()) {
Array compatibility;
for (int i = 0; i < compat_hashes.size(); i++) {
compatibility.push_back(compat_hashes[i]);
}
}
#ifndef DISABLE_DEPRECATED
GDExtensionCompatHashes::get_legacy_hashes(class_name, method_name, compatibility);
#endif
if (compatibility.size() > 0) {
d2["hash_compatibility"] = compatibility;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,57 @@
/**************************************************************************/
/* gdextension_compat_hashes.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GDEXTENSION_COMPAT_HASHES_H
#define GDEXTENSION_COMPAT_HASHES_H
#ifndef DISABLE_DEPRECATED
#include "core/string/string_name.h"
#include "core/templates/hash_map.h"
#include "core/templates/local_vector.h"
class GDExtensionCompatHashes {
struct Mapping {
StringName method;
uint32_t legacy_hash;
uint32_t current_hash;
};
static HashMap<StringName, LocalVector<Mapping>> mappings;
public:
static void initialize();
static bool lookup_current_hash(const StringName &p_class, const StringName &p_method, uint32_t p_legacy_hash, uint32_t *r_current_hash);
static bool get_legacy_hashes(const StringName &p_class, const StringName &p_method, Array &r_hashes);
};
#endif // DISABLE_DEPRECATED
#endif // GDEXTENSION_COMPAT_HASHES_H

View File

@@ -32,6 +32,7 @@
#include "core/config/engine.h"
#include "core/extension/gdextension.h"
#include "core/extension/gdextension_compat_hashes.h"
#include "core/io/file_access.h"
#include "core/io/xml_parser.h"
#include "core/object/class_db.h"
@@ -1144,6 +1145,17 @@ static GDExtensionMethodBindPtr gdextension_classdb_get_method_bind(GDExtensionC
const StringName methodname = *reinterpret_cast<const StringName *>(p_methodname);
bool exists = false;
MethodBind *mb = ClassDB::get_method_with_compatibility(classname, methodname, p_hash, &exists);
#ifndef DISABLE_DEPRECATED
// If lookup failed, see if this is one of the broken hashes from issue #81386.
if (!mb && exists) {
uint32_t mapped_hash;
if (GDExtensionCompatHashes::lookup_current_hash(classname, methodname, p_hash, &mapped_hash)) {
mb = ClassDB::get_method_with_compatibility(classname, methodname, mapped_hash, &exists);
}
}
#endif
if (!mb && exists) {
ERR_PRINT("Method '" + classname + "." + methodname + "' has changed and no compatibility fallback has been provided. Please open an issue.");
return nullptr;

View File

@@ -29,6 +29,8 @@
/**************************************************************************/
#include "gdextension_manager.h"
#include "core/extension/gdextension_compat_hashes.h"
#include "core/io/file_access.h"
GDExtensionManager::LoadStatus GDExtensionManager::load_extension(const String &p_path) {
@@ -171,4 +173,8 @@ GDExtensionManager *GDExtensionManager::singleton = nullptr;
GDExtensionManager::GDExtensionManager() {
ERR_FAIL_COND(singleton != nullptr);
singleton = this;
#ifndef DISABLE_DEPRECATED
GDExtensionCompatHashes::initialize();
#endif
}

View File

@@ -221,10 +221,11 @@ uint32_t ClassDB::get_api_hash(APIType p_api) {
hash = hash_murmur3_one_64(mb->get_default_argument_count(), hash);
for (int i = 0; i < mb->get_default_argument_count(); i++) {
//hash should not change, i hope for tis
Variant da = mb->get_default_argument(i);
hash = hash_murmur3_one_64(da.hash(), hash);
for (int i = 0; i < mb->get_argument_count(); i++) {
if (mb->has_default_argument(i)) {
Variant da = mb->get_default_argument(i);
hash = hash_murmur3_one_64(da.hash(), hash);
}
}
hash = hash_murmur3_one_64(mb->get_hint_flags(), hash);

View File

@@ -47,9 +47,11 @@ uint32_t MethodBind::get_hash() const {
}
hash = hash_murmur3_one_32(get_default_argument_count(), hash);
for (int i = 0; i < get_default_argument_count(); i++) {
Variant v = get_default_argument(i);
hash = hash_murmur3_one_32(v.hash(), hash);
for (int i = 0; i < get_argument_count(); i++) {
if (has_default_argument(i)) {
Variant v = get_default_argument(i);
hash = hash_murmur3_one_32(v.hash(), hash);
}
}
hash = hash_murmur3_one_32(is_const(), hash);