<commit_before>/*************************************************************************/
/*  class_db.cpp                                                         */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2018 <NAME>, <NAME>.                 */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* 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.                */
/*************************************************************************/

#include "class_db.h"

#include "os/mutex.h"
#include "version.h"

#define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
#define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);

#ifdef DEBUG_METHODS_ENABLED

MethodDefinition D_METHOD(const char *p_name) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.push_back(StaticCString::create(p_arg1));
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(2);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(3);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(4);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(5);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(6);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(7);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(8);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(9);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(10);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(11);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	md.args.write[10] = StaticCString::create(p_arg11);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(12);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	md.args.write[10] = StaticCString::create(p_arg11);
	md.args.write[11] = StaticCString::create(p_arg12);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12, const char *p_arg13) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(13);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	md.args.write[10] = StaticCString::create(p_arg11);
	md.args.write[11] = StaticCString::create(p_arg12);
	md.args.write[12] = StaticCString::create(p_arg13);
	return md;
}

#endif

ClassDB::APIType ClassDB::current_api = API_CORE;

void ClassDB::set_current_api(APIType p_api) {

	current_api = p_api;
}

HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
HashMap<StringName, StringName> ClassDB::resource_base_extensions;
HashMap<StringName, StringName> ClassDB::compat_classes;

ClassDB::ClassInfo::ClassInfo() {

	api = API_NONE;
	creation_func = NULL;
	inherits_ptr = NULL;
	disabled = false;
	exposed = false;
}

ClassDB::ClassInfo::~ClassInfo() {
}

bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {

	OBJTYPE_RLOCK;

	StringName inherits = p_class;

	while (inherits.operator String().length()) {

		if (inherits == p_inherits)
			return true;
		inherits = get_parent_class(inherits);
	}

	return false;
}
void ClassDB::get_class_list(List<StringName> *p_classes) {

	OBJTYPE_RLOCK;

	const StringName *k = NULL;

	while ((k = classes.next(k))) {

		p_classes->push_back(*k);
	}

	p_classes->sort();
}

void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {

	OBJTYPE_RLOCK;

	const StringName *k = NULL;

	while ((k = classes.next(k))) {

		if (*k != p_class && is_parent_class(*k, p_class))
			p_classes->push_back(*k);
	}
}

StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);
	if (!ti)
		return StringName();
	return ti->inherits;
}

StringName ClassDB::get_parent_class(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);
	ERR_FAIL_COND_V(!ti, StringName());
	return ti->inherits;
}

ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);

	ERR_FAIL_COND_V(!ti, API_NONE);
	return ti->api;
}

uint64_t ClassDB::get_api_hash(APIType p_api) {

	OBJTYPE_RLOCK;
#ifdef DEBUG_METHODS_ENABLED

	uint64_t hash = hash_djb2_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));

	List<StringName> names;

	const StringName *k = NULL;

	while ((k = classes.next(k))) {

		names.push_back(*k);
	}
	//must be alphabetically sorted for hash to compute
	names.sort_custom<StringName::AlphCompare>();

	for (List<StringName>::Element *E = names.front(); E; E = E->next()) {

		ClassInfo *t = classes.getptr(E->get());
		ERR_FAIL_COND_V(!t, 0);
		if (t->api != p_api || !t->exposed)
			continue;
		hash = hash_djb2_one_64(t->name.hash(), hash);
		hash = hash_djb2_one_64(t->inherits.hash(), hash);

		{ //methods

			List<StringName> snames;

			k = NULL;

			while ((k = t->method_map.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				MethodBind *mb = t->method_map[F->get()];
				hash = hash_djb2_one_64(mb->get_name().hash(), hash);
				hash = hash_djb2_one_64(mb->get_argument_count(), hash);
				hash = hash_djb2_one_64(mb->get_argument_type(-1), hash); //return

				for (int i = 0; i < mb->get_argument_count(); i++) {
					const PropertyInfo info = mb->get_argument_info(i);
					hash = hash_djb2_one_64(info.type, hash);
					hash = hash_djb2_one_64(info.name.hash(), hash);
					hash = hash_djb2_one_64(info.hint, hash);
					hash = hash_djb2_one_64(info.hint_string.hash(), hash);
				}

				hash = hash_djb2_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_djb2_one_64(da.hash(), hash);
				}

				hash = hash_djb2_one_64(mb->get_hint_flags(), hash);
			}
		}

		{ //constants

			List<StringName> snames;

			k = NULL;

			while ((k = t->constant_map.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				hash = hash_djb2_one_64(F->get().hash(), hash);
				hash = hash_djb2_one_64(t->constant_map[F->get()], hash);
			}
		}

		{ //signals

			List<StringName> snames;

			k = NULL;

			while ((k = t->signal_map.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				MethodInfo &mi = t->signal_map[F->get()];
				hash = hash_djb2_one_64(F->get().hash(), hash);
				for (int i = 0; i < mi.arguments.size(); i++) {
					hash = hash_djb2_one_64(mi.arguments[i].type, hash);
				}
			}
		}

		{ //properties

			List<StringName> snames;

			k = NULL;

			while ((k = t->property_setget.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				PropertySetGet *psg = t->property_setget.getptr(F->get());

				hash = hash_djb2_one_64(F->get().hash(), hash);
				hash = hash_djb2_one_64(psg->setter.hash(), hash);
				hash = hash_djb2_one_64(psg->getter.hash(), hash);
			}
		}

		//property list
		for (List<PropertyInfo>::Element *F = t->property_list.front(); F; F = F->next()) {

			hash = hash_djb2_one_64(F->get().name.hash(), hash);
			hash = hash_djb2_one_64(F->get().type, hash);
			hash = hash_djb2_one_64(F->get().hint, hash);
			hash = hash_djb2_one_64(F->get().hint_string.hash(), hash);
			hash = hash_djb2_one_64(F->get().usage, hash);
		}
	}

	return hash;
#else
	return 0;
#endif
}

bool ClassDB::class_exists(const StringName &p_class) {

	OBJTYPE_RLOCK;
	return classes.has(p_class);
}

void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {

	OBJTYPE_WLOCK;
	compat_classes[p_class] = p_fallback;
}

Object *ClassDB::instance(const StringName &p_class) {

	ClassInfo *ti;
	{
		OBJTYPE_RLOCK;
		ti = classes.getptr(p_class);
		if (!ti || ti->disabled || !ti->creation_func) {
			if (compat_classes.has(p_class)) {
				ti = classes.getptr(compat_classes[p_class]);
			}
		}
		ERR_FAIL_COND_V(!ti, NULL);
		ERR_FAIL_COND_V(ti->disabled, NULL);
		ERR_FAIL_COND_V(!ti->creation_func, NULL);
	}

	return ti->creation_func();
}
bool ClassDB::can_instance(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);
	ERR_FAIL_COND_V(!ti, false);
	return (!ti->disabled && ti->creation_func != NULL);<commit_msg>Print an error if you try to instantiate an editor class outside the editor, fixes #17624<commit_after>/*************************************************************************/
/*  class_db.cpp                                                         */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2018 <NAME>, <NAME>.                 */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* 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.                */
/*************************************************************************/

#include "class_db.h"

#include "engine.h"
#include "os/mutex.h"
#include "version.h"

#define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
#define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);

#ifdef DEBUG_METHODS_ENABLED

MethodDefinition D_METHOD(const char *p_name) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.push_back(StaticCString::create(p_arg1));
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(2);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(3);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(4);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(5);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(6);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(7);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(8);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(9);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(10);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(11);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	md.args.write[10] = StaticCString::create(p_arg11);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(12);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	md.args.write[10] = StaticCString::create(p_arg11);
	md.args.write[11] = StaticCString::create(p_arg12);
	return md;
}

MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12, const char *p_arg13) {

	MethodDefinition md;
	md.name = StaticCString::create(p_name);
	md.args.resize(13);
	md.args.write[0] = StaticCString::create(p_arg1);
	md.args.write[1] = StaticCString::create(p_arg2);
	md.args.write[2] = StaticCString::create(p_arg3);
	md.args.write[3] = StaticCString::create(p_arg4);
	md.args.write[4] = StaticCString::create(p_arg5);
	md.args.write[5] = StaticCString::create(p_arg6);
	md.args.write[6] = StaticCString::create(p_arg7);
	md.args.write[7] = StaticCString::create(p_arg8);
	md.args.write[8] = StaticCString::create(p_arg9);
	md.args.write[9] = StaticCString::create(p_arg10);
	md.args.write[10] = StaticCString::create(p_arg11);
	md.args.write[11] = StaticCString::create(p_arg12);
	md.args.write[12] = StaticCString::create(p_arg13);
	return md;
}

#endif

ClassDB::APIType ClassDB::current_api = API_CORE;

void ClassDB::set_current_api(APIType p_api) {

	current_api = p_api;
}

HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
HashMap<StringName, StringName> ClassDB::resource_base_extensions;
HashMap<StringName, StringName> ClassDB::compat_classes;

ClassDB::ClassInfo::ClassInfo() {

	api = API_NONE;
	creation_func = NULL;
	inherits_ptr = NULL;
	disabled = false;
	exposed = false;
}

ClassDB::ClassInfo::~ClassInfo() {
}

bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {

	OBJTYPE_RLOCK;

	StringName inherits = p_class;

	while (inherits.operator String().length()) {

		if (inherits == p_inherits)
			return true;
		inherits = get_parent_class(inherits);
	}

	return false;
}
void ClassDB::get_class_list(List<StringName> *p_classes) {

	OBJTYPE_RLOCK;

	const StringName *k = NULL;

	while ((k = classes.next(k))) {

		p_classes->push_back(*k);
	}

	p_classes->sort();
}

void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {

	OBJTYPE_RLOCK;

	const StringName *k = NULL;

	while ((k = classes.next(k))) {

		if (*k != p_class && is_parent_class(*k, p_class))
			p_classes->push_back(*k);
	}
}

StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);
	if (!ti)
		return StringName();
	return ti->inherits;
}

StringName ClassDB::get_parent_class(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);
	ERR_FAIL_COND_V(!ti, StringName());
	return ti->inherits;
}

ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);

	ERR_FAIL_COND_V(!ti, API_NONE);
	return ti->api;
}

uint64_t ClassDB::get_api_hash(APIType p_api) {

	OBJTYPE_RLOCK;
#ifdef DEBUG_METHODS_ENABLED

	uint64_t hash = hash_djb2_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));

	List<StringName> names;

	const StringName *k = NULL;

	while ((k = classes.next(k))) {

		names.push_back(*k);
	}
	//must be alphabetically sorted for hash to compute
	names.sort_custom<StringName::AlphCompare>();

	for (List<StringName>::Element *E = names.front(); E; E = E->next()) {

		ClassInfo *t = classes.getptr(E->get());
		ERR_FAIL_COND_V(!t, 0);
		if (t->api != p_api || !t->exposed)
			continue;
		hash = hash_djb2_one_64(t->name.hash(), hash);
		hash = hash_djb2_one_64(t->inherits.hash(), hash);

		{ //methods

			List<StringName> snames;

			k = NULL;

			while ((k = t->method_map.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				MethodBind *mb = t->method_map[F->get()];
				hash = hash_djb2_one_64(mb->get_name().hash(), hash);
				hash = hash_djb2_one_64(mb->get_argument_count(), hash);
				hash = hash_djb2_one_64(mb->get_argument_type(-1), hash); //return

				for (int i = 0; i < mb->get_argument_count(); i++) {
					const PropertyInfo info = mb->get_argument_info(i);
					hash = hash_djb2_one_64(info.type, hash);
					hash = hash_djb2_one_64(info.name.hash(), hash);
					hash = hash_djb2_one_64(info.hint, hash);
					hash = hash_djb2_one_64(info.hint_string.hash(), hash);
				}

				hash = hash_djb2_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_djb2_one_64(da.hash(), hash);
				}

				hash = hash_djb2_one_64(mb->get_hint_flags(), hash);
			}
		}

		{ //constants

			List<StringName> snames;

			k = NULL;

			while ((k = t->constant_map.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				hash = hash_djb2_one_64(F->get().hash(), hash);
				hash = hash_djb2_one_64(t->constant_map[F->get()], hash);
			}
		}

		{ //signals

			List<StringName> snames;

			k = NULL;

			while ((k = t->signal_map.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				MethodInfo &mi = t->signal_map[F->get()];
				hash = hash_djb2_one_64(F->get().hash(), hash);
				for (int i = 0; i < mi.arguments.size(); i++) {
					hash = hash_djb2_one_64(mi.arguments[i].type, hash);
				}
			}
		}

		{ //properties

			List<StringName> snames;

			k = NULL;

			while ((k = t->property_setget.next(k))) {

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {

				PropertySetGet *psg = t->property_setget.getptr(F->get());

				hash = hash_djb2_one_64(F->get().hash(), hash);
				hash = hash_djb2_one_64(psg->setter.hash(), hash);
				hash = hash_djb2_one_64(psg->getter.hash(), hash);
			}
		}

		//property list
		for (List<PropertyInfo>::Element *F = t->property_list.front(); F; F = F->next()) {

			hash = hash_djb2_one_64(F->get().name.hash(), hash);
			hash = hash_djb2_one_64(F->get().type, hash);
			hash = hash_djb2_one_64(F->get().hint, hash);
			hash = hash_djb2_one_64(F->get().hint_string.hash(), hash);
			hash = hash_djb2_one_64(F->get().usage, hash);
		}
	}

	return hash;
#else
	return 0;
#endif
}

bool ClassDB::class_exists(const StringName &p_class) {

	OBJTYPE_RLOCK;
	return classes.has(p_class);
}

void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {

	OBJTYPE_WLOCK;
	compat_classes[p_class] = p_fallback;
}

Object *ClassDB::instance(const StringName &p_class) {

	ClassInfo *ti;
	{
		OBJTYPE_RLOCK;
		ti = classes.getptr(p_class);
		if (!ti || ti->disabled || !ti->creation_func) {
			if (compat_classes.has(p_class)) {
				ti = classes.getptr(compat_classes[p_class]);
			}
		}
		ERR_FAIL_COND_V(!ti, NULL);
		ERR_FAIL_COND_V(ti->disabled, NULL);
		ERR_FAIL_COND_V(!ti->creation_func, NULL);
	}
#ifdef TOOLS_ENABLED
	if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
		ERR_PRINTS("Class '" + String(p_class) + "' can only be instantiated by editor.");
		return NULL;
	}
#endif
	return ti->creation_func();
}
bool ClassDB::can_instance(const StringName &p_class) {

	OBJTYPE_RLOCK;

	ClassInfo *ti = classes.getptr(p_class);
	ERR_FAIL_COND_V(!ti, false);
	return (!ti->disabled && ti->creation_func != NULL);
{'fraction_non_alphanumeric': 0.1653868194842407, 'fraction_numerical': 0.023925501432664757, 'mean_word_length': 4.431216931216931, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 163, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2309516', 'n_tokens_mistral': 13980, 'n_tokens_neox': 12885, 'n_words': 2499}