diff options
Diffstat (limited to 'main')
36 files changed, 880 insertions, 7490 deletions
diff --git a/main/SCsub b/main/SCsub index 7a301b82bc..87d64e48f9 100644 --- a/main/SCsub +++ b/main/SCsub @@ -2,27 +2,37 @@ Import("env") -from platform_methods import run_in_subprocess import main_builders env.main_sources = [] -env.add_source_files(env.main_sources, "*.cpp") +env_main = env.Clone() +env_main.add_source_files(env.main_sources, "*.cpp") -env.Depends("#main/splash.gen.h", "#main/splash.png") -env.CommandNoCache("#main/splash.gen.h", "#main/splash.png", run_in_subprocess(main_builders.make_splash)) +if env["tests"]: + env_main.Append(CPPDEFINES=["TESTS_ENABLED"]) -env.Depends("#main/splash_editor.gen.h", "#main/splash_editor.png") -env.CommandNoCache( - "#main/splash_editor.gen.h", "#main/splash_editor.png", run_in_subprocess(main_builders.make_splash_editor) +env_main.Depends("#main/splash.gen.h", "#main/splash.png") +env_main.CommandNoCache( + "#main/splash.gen.h", + "#main/splash.png", + env.Run(main_builders.make_splash, "Building splash screen header."), ) -env.Depends("#main/app_icon.gen.h", "#main/app_icon.png") -env.CommandNoCache("#main/app_icon.gen.h", "#main/app_icon.png", run_in_subprocess(main_builders.make_app_icon)) +env_main.Depends("#main/splash_editor.gen.h", "#main/splash_editor.png") +env_main.CommandNoCache( + "#main/splash_editor.gen.h", + "#main/splash_editor.png", + env.Run(main_builders.make_splash_editor, "Building editor splash screen header."), +) -if env["tools"]: - SConscript("tests/SCsub") +env_main.Depends("#main/app_icon.gen.h", "#main/app_icon.png") +env_main.CommandNoCache( + "#main/app_icon.gen.h", + "#main/app_icon.png", + env.Run(main_builders.make_app_icon, "Building application icon."), +) -lib = env.add_library("main", env.main_sources) +lib = env_main.add_library("main", env.main_sources) env.Prepend(LIBS=[lib]) diff --git a/main/main.cpp b/main/main.cpp index 68016a5cef..71dd3cf1ad 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 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 */ @@ -30,6 +30,8 @@ #include "main.h" +#include "core/config/project_settings.h" +#include "core/core_string_names.h" #include "core/crypto/crypto.h" #include "core/debugger/engine_debugger.h" #include "core/input/input.h" @@ -40,12 +42,11 @@ #include "core/io/image_loader.h" #include "core/io/ip.h" #include "core/io/resource_loader.h" -#include "core/message_queue.h" +#include "core/object/message_queue.h" #include "core/os/dir_access.h" #include "core/os/os.h" -#include "core/project_settings.h" #include "core/register_core_types.h" -#include "core/translation.h" +#include "core/string/translation.h" #include "core/version.h" #include "core/version_hash.gen.h" #include "drivers/register_driver_types.h" @@ -54,7 +55,6 @@ #include "main/performance.h" #include "main/splash.gen.h" #include "main/splash_editor.gen.h" -#include "main/tests/test_main.h" #include "modules/modules_enabled.gen.h" #include "modules/register_module_types.h" #include "platform/register_platform_apis.h" @@ -70,17 +70,23 @@ #include "servers/physics_server_2d.h" #include "servers/physics_server_3d.h" #include "servers/register_server_types.h" -#include "servers/rendering/rendering_server_raster.h" -#include "servers/rendering/rendering_server_wrap_mt.h" +#include "servers/rendering/rendering_server_default.h" +#include "servers/text_server.h" #include "servers/xr_server.h" +#ifdef TESTS_ENABLED +#include "tests/test_main.h" +#endif + #ifdef TOOLS_ENABLED -#include "editor/doc_data.h" + #include "editor/doc_data_class_path.gen.h" +#include "editor/doc_tools.h" #include "editor/editor_node.h" #include "editor/editor_settings.h" #include "editor/progress_dialog.h" #include "editor/project_manager.h" + #endif /* Static members */ @@ -107,6 +113,7 @@ static DisplayServer *display_server = nullptr; static RenderingServer *rendering_server = nullptr; static CameraServer *camera_server = nullptr; static XRServer *xr_server = nullptr; +static TextServerManager *tsman = nullptr; static PhysicsServer3D *physics_server = nullptr; static PhysicsServer2D *physics_2d_server = nullptr; static NavigationServer3D *navigation_server = nullptr; @@ -116,6 +123,10 @@ static bool _start_success = false; // Drivers +String tablet_driver = ""; +String text_driver = ""; + +static int text_driver_idx = -1; static int display_driver_idx = -1; static int audio_driver_idx = -1; @@ -161,6 +172,8 @@ static bool disable_render_loop = false; static int fixed_fps = -1; static bool print_fps = false; +bool profile_gpu = false; + /* Helper methods */ // Used by Mono module, should likely be registered in Engine singleton instead @@ -186,7 +199,8 @@ static String get_full_version_string() { // to have less code in main.cpp. void initialize_physics() { /// 3D Physics Server - physics_server = PhysicsServer3DManager::new_server(ProjectSettings::get_singleton()->get(PhysicsServer3DManager::setting_property_name)); + physics_server = PhysicsServer3DManager::new_server( + ProjectSettings::get_singleton()->get(PhysicsServer3DManager::setting_property_name)); if (!physics_server) { // Physics server not found, Use the default physics physics_server = PhysicsServer3DManager::new_default_server(); @@ -195,7 +209,8 @@ void initialize_physics() { physics_server->init(); /// 2D Physics server - physics_2d_server = PhysicsServer2DManager::new_server(ProjectSettings::get_singleton()->get(PhysicsServer2DManager::setting_property_name)); + physics_2d_server = PhysicsServer2DManager::new_server( + ProjectSettings::get_singleton()->get(PhysicsServer2DManager::setting_property_name)); if (!physics_2d_server) { // Physics server not found, Use the default physics physics_2d_server = PhysicsServer2DManager::new_default_server(); @@ -244,34 +259,34 @@ void finalize_navigation_server() { void Main::print_help(const char *p_binary) { print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE)); OS::get_singleton()->print("Free and open source software under the terms of the MIT license.\n"); - OS::get_singleton()->print("(c) 2007-2020 Juan Linietsky, Ariel Manzur.\n"); - OS::get_singleton()->print("(c) 2014-2020 Godot Engine contributors.\n"); + OS::get_singleton()->print("(c) 2007-2021 Juan Linietsky, Ariel Manzur.\n"); + OS::get_singleton()->print("(c) 2014-2021 Godot Engine contributors.\n"); OS::get_singleton()->print("\n"); OS::get_singleton()->print("Usage: %s [options] [path to scene or 'project.godot' file]\n", p_binary); OS::get_singleton()->print("\n"); OS::get_singleton()->print("General options:\n"); - OS::get_singleton()->print(" -h, --help Display this help message.\n"); - OS::get_singleton()->print(" --version Display the version string.\n"); - OS::get_singleton()->print(" -v, --verbose Use verbose stdout mode.\n"); - OS::get_singleton()->print(" --quiet Quiet mode, silences stdout messages. Errors are still displayed.\n"); + OS::get_singleton()->print(" -h, --help Display this help message.\n"); + OS::get_singleton()->print(" --version Display the version string.\n"); + OS::get_singleton()->print(" -v, --verbose Use verbose stdout mode.\n"); + OS::get_singleton()->print(" --quiet Quiet mode, silences stdout messages. Errors are still displayed.\n"); OS::get_singleton()->print("\n"); OS::get_singleton()->print("Run options:\n"); #ifdef TOOLS_ENABLED - OS::get_singleton()->print(" -e, --editor Start the editor instead of running the scene.\n"); - OS::get_singleton()->print(" -p, --project-manager Start the project manager, even if a project is auto-detected.\n"); + OS::get_singleton()->print(" -e, --editor Start the editor instead of running the scene.\n"); + OS::get_singleton()->print(" -p, --project-manager Start the project manager, even if a project is auto-detected.\n"); #endif - OS::get_singleton()->print(" -q, --quit Quit after the first iteration.\n"); - OS::get_singleton()->print(" -l, --language <locale> Use a specific locale (<locale> being a two-letter code).\n"); - OS::get_singleton()->print(" --path <directory> Path to a project (<directory> must contain a 'project.godot' file).\n"); - OS::get_singleton()->print(" -u, --upwards Scan folders upwards for project.godot file.\n"); - OS::get_singleton()->print(" --main-pack <file> Path to a pack (.pck) file to load.\n"); - OS::get_singleton()->print(" --render-thread <mode> Render thread mode ('unsafe', 'safe', 'separate').\n"); - OS::get_singleton()->print(" --remote-fs <address> Remote filesystem (<host/IP>[:<port>] address).\n"); - OS::get_singleton()->print(" --remote-fs-password <password> Password for remote filesystem.\n"); - - OS::get_singleton()->print(" --audio-driver <driver> Audio driver ["); + OS::get_singleton()->print(" -q, --quit Quit after the first iteration.\n"); + OS::get_singleton()->print(" -l, --language <locale> Use a specific locale (<locale> being a two-letter code).\n"); + OS::get_singleton()->print(" --path <directory> Path to a project (<directory> must contain a 'project.godot' file).\n"); + OS::get_singleton()->print(" -u, --upwards Scan folders upwards for project.godot file.\n"); + OS::get_singleton()->print(" --main-pack <file> Path to a pack (.pck) file to load.\n"); + OS::get_singleton()->print(" --render-thread <mode> Render thread mode ('unsafe', 'safe', 'separate').\n"); + OS::get_singleton()->print(" --remote-fs <address> Remote filesystem (<host/IP>[:<port>] address).\n"); + OS::get_singleton()->print(" --remote-fs-password <password> Password for remote filesystem.\n"); + + OS::get_singleton()->print(" --audio-driver <driver> Audio driver ["); for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) { if (i > 0) { OS::get_singleton()->print(", "); @@ -280,7 +295,7 @@ void Main::print_help(const char *p_binary) { } OS::get_singleton()->print("].\n"); - OS::get_singleton()->print(" --display-driver <driver> Display driver (and rendering driver) ["); + OS::get_singleton()->print(" --display-driver <driver> Display driver (and rendering driver) ["); for (int i = 0; i < DisplayServer::get_create_function_count(); i++) { if (i > 0) { OS::get_singleton()->print(", "); @@ -296,74 +311,181 @@ void Main::print_help(const char *p_binary) { OS::get_singleton()->print(")"); } OS::get_singleton()->print("].\n"); - OS::get_singleton()->print(" --rendering-driver <driver> Rendering driver (depends on display driver).\n"); + + OS::get_singleton()->print(" --rendering-driver <driver> Rendering driver (depends on display driver).\n"); + + OS::get_singleton()->print(" --text-driver <driver> Text driver (Fonts, BiDi, shaping)\n"); + OS::get_singleton()->print("\n"); #ifndef SERVER_ENABLED OS::get_singleton()->print("Display options:\n"); - OS::get_singleton()->print(" -f, --fullscreen Request fullscreen mode.\n"); - OS::get_singleton()->print(" -m, --maximized Request a maximized window.\n"); - OS::get_singleton()->print(" -w, --windowed Request windowed mode.\n"); - OS::get_singleton()->print(" -t, --always-on-top Request an always-on-top window.\n"); - OS::get_singleton()->print(" --resolution <W>x<H> Request window resolution.\n"); - OS::get_singleton()->print(" --position <X>,<Y> Request window position.\n"); - OS::get_singleton()->print(" --low-dpi Force low-DPI mode (macOS and Windows only).\n"); - OS::get_singleton()->print(" --no-window Disable window creation (Windows only). Useful together with --script.\n"); - OS::get_singleton()->print(" --enable-vsync-via-compositor When vsync is enabled, vsync via the OS' window compositor (Windows only).\n"); - OS::get_singleton()->print(" --disable-vsync-via-compositor Disable vsync via the OS' window compositor (Windows only).\n"); - OS::get_singleton()->print(" --single-window Use a single window (no separate subwindows).\n"); - OS::get_singleton()->print(" --tablet-driver Tablet input driver ("); - for (int i = 0; i < OS::get_singleton()->get_tablet_driver_count(); i++) { - if (i != 0) - OS::get_singleton()->print(", "); - OS::get_singleton()->print("'%s'", OS::get_singleton()->get_tablet_driver_name(i).utf8().get_data()); - } - OS::get_singleton()->print(") (Windows only).\n"); + OS::get_singleton()->print(" -f, --fullscreen Request fullscreen mode.\n"); + OS::get_singleton()->print(" -m, --maximized Request a maximized window.\n"); + OS::get_singleton()->print(" -w, --windowed Request windowed mode.\n"); + OS::get_singleton()->print(" -t, --always-on-top Request an always-on-top window.\n"); + OS::get_singleton()->print(" --resolution <W>x<H> Request window resolution.\n"); + OS::get_singleton()->print(" --position <X>,<Y> Request window position.\n"); + OS::get_singleton()->print(" --low-dpi Force low-DPI mode (macOS and Windows only).\n"); + OS::get_singleton()->print(" --no-window Disable window creation (Windows only). Useful together with --script.\n"); + OS::get_singleton()->print(" --enable-vsync-via-compositor When vsync is enabled, vsync via the OS' window compositor (Windows only).\n"); + OS::get_singleton()->print(" --disable-vsync-via-compositor Disable vsync via the OS' window compositor (Windows only).\n"); + OS::get_singleton()->print(" --single-window Use a single window (no separate subwindows).\n"); + OS::get_singleton()->print(" --tablet-driver Pen tablet input driver.\n"); OS::get_singleton()->print("\n"); #endif OS::get_singleton()->print("Debug options:\n"); - OS::get_singleton()->print(" -d, --debug Debug (local stdout debugger).\n"); - OS::get_singleton()->print(" -b, --breakpoints Breakpoint list as source::line comma-separated pairs, no spaces (use %%20 instead).\n"); - OS::get_singleton()->print(" --profiling Enable profiling in the script debugger.\n"); - OS::get_singleton()->print(" --gpu-abort Abort on GPU errors (usually validation layer errors), may help see the problem if your system freezes.\n"); - OS::get_singleton()->print(" --remote-debug <uri> Remote debug (<protocol>://<host/IP>[:<port>], e.g. tcp://127.0.0.1:6007).\n"); + OS::get_singleton()->print(" -d, --debug Debug (local stdout debugger).\n"); + OS::get_singleton()->print(" -b, --breakpoints Breakpoint list as source::line comma-separated pairs, no spaces (use %%20 instead).\n"); + OS::get_singleton()->print(" --profiling Enable profiling in the script debugger.\n"); + OS::get_singleton()->print(" --vk-layers Enable Vulkan Validation layers for debugging.\n"); +#if DEBUG_ENABLED + OS::get_singleton()->print(" --gpu-abort Abort on GPU errors (usually validation layer errors), may help see the problem if your system freezes.\n"); +#endif + OS::get_singleton()->print(" --remote-debug <uri> Remote debug (<protocol>://<host/IP>[:<port>], e.g. tcp://127.0.0.1:6007).\n"); #if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED) - OS::get_singleton()->print(" --debug-collisions Show collision shapes when running the scene.\n"); - OS::get_singleton()->print(" --debug-navigation Show navigation polygons when running the scene.\n"); + OS::get_singleton()->print(" --debug-collisions Show collision shapes when running the scene.\n"); + OS::get_singleton()->print(" --debug-navigation Show navigation polygons when running the scene.\n"); #endif - OS::get_singleton()->print(" --frame-delay <ms> Simulate high CPU load (delay each frame by <ms> milliseconds).\n"); - OS::get_singleton()->print(" --time-scale <scale> Force time scale (higher values are faster, 1.0 is normal speed).\n"); - OS::get_singleton()->print(" --disable-render-loop Disable render loop so rendering only occurs when called explicitly from script.\n"); - OS::get_singleton()->print(" --disable-crash-handler Disable crash handler when supported by the platform code.\n"); - OS::get_singleton()->print(" --fixed-fps <fps> Force a fixed number of frames per second. This setting disables real-time synchronization.\n"); - OS::get_singleton()->print(" --print-fps Print the frames per second to the stdout.\n"); + OS::get_singleton()->print(" --frame-delay <ms> Simulate high CPU load (delay each frame by <ms> milliseconds).\n"); + OS::get_singleton()->print(" --time-scale <scale> Force time scale (higher values are faster, 1.0 is normal speed).\n"); + OS::get_singleton()->print(" --disable-render-loop Disable render loop so rendering only occurs when called explicitly from script.\n"); + OS::get_singleton()->print(" --disable-crash-handler Disable crash handler when supported by the platform code.\n"); + OS::get_singleton()->print(" --fixed-fps <fps> Force a fixed number of frames per second. This setting disables real-time synchronization.\n"); + OS::get_singleton()->print(" --print-fps Print the frames per second to the stdout.\n"); + OS::get_singleton()->print(" --profile-gpu Show a simple profile of the tasks that took more time during frame rendering.\n"); OS::get_singleton()->print("\n"); OS::get_singleton()->print("Standalone tools:\n"); - OS::get_singleton()->print(" -s, --script <script> Run a script.\n"); - OS::get_singleton()->print(" --check-only Only parse for errors and quit (use with --script).\n"); + OS::get_singleton()->print(" -s, --script <script> Run a script.\n"); + OS::get_singleton()->print(" --check-only Only parse for errors and quit (use with --script).\n"); #ifdef TOOLS_ENABLED - OS::get_singleton()->print(" --export <preset> <path> Export the project using the given preset and matching release template. The preset name should match one defined in export_presets.cfg.\n"); - OS::get_singleton()->print(" <path> should be absolute or relative to the project directory, and include the filename for the binary (e.g. 'builds/game.exe'). The target directory should exist.\n"); - OS::get_singleton()->print(" --export-debug <preset> <path> Same as --export, but using the debug template.\n"); - OS::get_singleton()->print(" --export-pack <preset> <path> Same as --export, but only export the game pack for the given preset. The <path> extension determines whether it will be in PCK or ZIP format.\n"); - OS::get_singleton()->print(" --doctool <path> Dump the engine API reference to the given <path> in XML format, merging if existing files are found.\n"); - OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n"); - OS::get_singleton()->print(" --build-solutions Build the scripting solutions (e.g. for C# projects). Implies --editor and requires a valid project to edit.\n"); + OS::get_singleton()->print(" --export <preset> <path> Export the project using the given preset and matching release template. The preset name should match one defined in export_presets.cfg.\n"); + OS::get_singleton()->print(" <path> should be absolute or relative to the project directory, and include the filename for the binary (e.g. 'builds/game.exe'). The target directory should exist.\n"); + OS::get_singleton()->print(" --export-debug <preset> <path> Same as --export, but using the debug template.\n"); + OS::get_singleton()->print(" --export-pack <preset> <path> Same as --export, but only export the game pack for the given preset. The <path> extension determines whether it will be in PCK or ZIP format.\n"); + OS::get_singleton()->print(" --doctool <path> Dump the engine API reference to the given <path> in XML format, merging if existing files are found.\n"); + OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n"); + OS::get_singleton()->print(" --build-solutions Build the scripting solutions (e.g. for C# projects). Implies --editor and requires a valid project to edit.\n"); #ifdef DEBUG_METHODS_ENABLED - OS::get_singleton()->print(" --gdnative-generate-json-api Generate JSON dump of the Godot API for GDNative bindings.\n"); + OS::get_singleton()->print(" --gdnative-generate-json-api <path> Generate JSON dump of the Godot API for GDNative bindings and save it on the file specified in <path>.\n"); + OS::get_singleton()->print(" --gdnative-generate-json-builtin-api <path> Generate JSON dump of the Godot API of the builtin Variant types and utility functions for GDNative bindings and save it on the file specified in <path>.\n"); +#endif +#ifdef TESTS_ENABLED + OS::get_singleton()->print(" --test [--help] Run unit tests. Use --test --help for more information.\n"); #endif - OS::get_singleton()->print(" --test <test> Run a unit test ["); - const char **test_names = tests_get_names(); - const char *comma = ""; - while (*test_names) { - OS::get_singleton()->print("%s'%s'", comma, *test_names); - test_names++; - comma = ", "; + OS::get_singleton()->print("\n"); +#endif +} + +#ifdef TESTS_ENABLED +// The order is the same as in `Main::setup()`, only core and some editor types +// are initialized here. This also combines `Main::setup2()` initialization. +Error Main::test_setup() { + OS::get_singleton()->initialize(); + + engine = memnew(Engine); + + register_core_types(); + register_core_driver_types(); + + globals = memnew(ProjectSettings); + + GLOBAL_DEF("debug/settings/crash_handler/message", + String("Please include this when reporting the bug on https://github.com/godotengine/godot/issues")); + + translation_server = memnew(TranslationServer); + + // From `Main::setup2()`. + preregister_module_types(); + preregister_server_types(); + + register_core_singletons(); + + register_server_types(); + + translation_server->setup(); //register translations, load them, etc. + if (locale != "") { + translation_server->set_locale(locale); + } + translation_server->load_translations(); + ResourceLoader::load_translation_remaps(); //load remaps for resources + + ResourceLoader::load_path_remaps(); + + register_scene_types(); + +#ifdef TOOLS_ENABLED + ClassDB::set_current_api(ClassDB::API_EDITOR); + EditorNode::register_editor_types(); + + ClassDB::set_current_api(ClassDB::API_CORE); +#endif + register_platform_apis(); + + register_module_types(); + register_driver_types(); + + ClassDB::set_current_api(ClassDB::API_NONE); + + _start_success = true; + + return OK; +} +// The order is the same as in `Main::cleanup()`. +void Main::test_cleanup() { + ERR_FAIL_COND(!_start_success); + + EngineDebugger::deinitialize(); + + ResourceLoader::remove_custom_loaders(); + ResourceSaver::remove_custom_savers(); + +#ifdef TOOLS_ENABLED + EditorNode::unregister_editor_types(); +#endif + unregister_driver_types(); + unregister_module_types(); + unregister_platform_apis(); + unregister_scene_types(); + unregister_server_types(); + + OS::get_singleton()->finalize(); + + if (translation_server) { + memdelete(translation_server); + } + if (globals) { + memdelete(globals); + } + if (engine) { + memdelete(engine); + } + + unregister_core_driver_types(); + unregister_core_types(); + + OS::get_singleton()->finalize_core(); +} +#endif + +int Main::test_entrypoint(int argc, char *argv[], bool &tests_need_run) { +#ifdef TESTS_ENABLED + for (int x = 0; x < argc; x++) { + if ((strncmp(argv[x], "--test", 6) == 0) && (strlen(argv[x]) == 6)) { + tests_need_run = true; + // TODO: need to come up with different test contexts. + // Not every test requires high-level functionality like `ClassDB`. + test_setup(); + int status = test_main(argc, argv); + test_cleanup(); + return status; + } } - OS::get_singleton()->print("].\n"); #endif + tests_need_run = false; + return 0; } /* Engine initialization @@ -397,8 +519,6 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph engine = memnew(Engine); - ClassDB::init(); - MAIN_PRINT("Main: Initialize CORE"); register_core_types(); @@ -406,10 +526,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph MAIN_PRINT("Main: Initialize Globals"); - Thread::_main_thread_id = Thread::get_caller_id(); - - globals = memnew(ProjectSettings); input_map = memnew(InputMap); + globals = memnew(ProjectSettings); register_core_settings(); //here globals is present @@ -418,7 +536,13 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph ClassDB::register_class<Performance>(); engine->add_singleton(Engine::Singleton("Performance", performance)); - GLOBAL_DEF("debug/settings/crash_handler/message", String("Please include this when reporting the bug on https://github.com/godotengine/godot/issues")); + // Only flush stdout in debug builds by default, as spamming `print()` will + // decrease performance if this is enabled. + GLOBAL_DEF("application/run/flush_stdout_on_print", false); + GLOBAL_DEF("application/run/flush_stdout_on_print.debug", true); + + GLOBAL_DEF("debug/settings/crash_handler/message", + String("Please include this when reporting the bug on https://github.com/godotengine/godot/issues")); MAIN_PRINT("Main: Parse CMDLine"); @@ -443,7 +567,6 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph String display_driver = ""; String audio_driver = ""; - String tablet_driver = ""; String project_path = "."; bool upwards = false; String debug_uri = ""; @@ -523,7 +646,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } if (!found) { - OS::get_singleton()->print("Unknown audio driver '%s', aborting.\nValid options are ", audio_driver.utf8().get_data()); + OS::get_singleton()->print("Unknown audio driver '%s', aborting.\nValid options are ", + audio_driver.utf8().get_data()); for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) { if (i == AudioDriverManager::get_driver_count() - 1) { @@ -545,6 +669,14 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph OS::get_singleton()->print("Missing audio driver argument, aborting.\n"); goto error; } + } else if (I->get() == "--text-driver") { + if (I->next()) { + text_driver = I->next()->get(); + N = I->next()->next(); + } else { + OS::get_singleton()->print("Missing text driver argument, aborting.\n"); + goto error; + } } else if (I->get() == "--display-driver") { // force video driver @@ -559,7 +691,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } if (!found) { - OS::get_singleton()->print("Unknown display driver '%s', aborting.\nValid options are ", display_driver.utf8().get_data()); + OS::get_singleton()->print("Unknown display driver '%s', aborting.\nValid options are ", + display_driver.utf8().get_data()); for (int i = 0; i < DisplayServer::get_create_function_count(); i++) { if (i == DisplayServer::get_create_function_count() - 1) { @@ -593,24 +726,15 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else if (I->get() == "-w" || I->get() == "--windowed") { // force windowed window init_windowed = true; - } else if (I->get() == "--gpu-abort") { // force windowed window - + } else if (I->get() == "--vk-layers") { + Engine::singleton->use_validation_layers = true; +#ifdef DEBUG_ENABLED + } else if (I->get() == "--gpu-abort") { Engine::singleton->abort_on_gpu_errors = true; +#endif } else if (I->get() == "--tablet-driver") { if (I->next()) { tablet_driver = I->next()->get(); - bool found = false; - for (int i = 0; i < OS::get_singleton()->get_tablet_driver_count(); i++) { - if (tablet_driver == OS::get_singleton()->get_tablet_driver_name(i)) { - found = true; - } - } - - if (!found) { - OS::get_singleton()->print("Unknown tablet driver '%s', aborting.\n", tablet_driver.utf8().get_data()); - goto error; - } - N = I->next()->next(); } else { OS::get_singleton()->print("Missing tablet driver argument, aborting.\n"); @@ -629,7 +753,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph if (vm.find("x") == -1) { // invalid parameter format - OS::get_singleton()->print("Invalid resolution '%s', it should be e.g. '1280x720'.\n", vm.utf8().get_data()); + OS::get_singleton()->print("Invalid resolution '%s', it should be e.g. '1280x720'.\n", + vm.utf8().get_data()); goto error; } @@ -637,7 +762,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph int h = vm.get_slice("x", 1).to_int(); if (w <= 0 || h <= 0) { - OS::get_singleton()->print("Invalid resolution '%s', width and height must be above 0.\n", vm.utf8().get_data()); + OS::get_singleton()->print("Invalid resolution '%s', width and height must be above 0.\n", + vm.utf8().get_data()); goto error; } @@ -658,7 +784,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph if (vm.find(",") == -1) { // invalid parameter format - OS::get_singleton()->print("Invalid position '%s', it should be e.g. '80,128'.\n", vm.utf8().get_data()); + OS::get_singleton()->print("Invalid position '%s', it should be e.g. '80,128'.\n", + vm.utf8().get_data()); goto error; } @@ -747,14 +874,15 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph auto_build_solutions = true; editor = true; #ifdef DEBUG_METHODS_ENABLED - } else if (I->get() == "--gdnative-generate-json-api") { + } else if (I->get() == "--gdnative-generate-json-api" || I->get() == "--gdnative-generate-json-builtin-api") { // Register as an editor instance to use low-end fallback if relevant. editor = true; // We still pass it to the main arguments since the argument handling itself is not done in this function main_args.push_back(I->get()); #endif - } else if (I->get() == "--export" || I->get() == "--export-debug" || I->get() == "--export-pack") { // Export project + } else if (I->get() == "--export" || I->get() == "--export-debug" || + I->get() == "--export-pack") { // Export project editor = true; main_args.push_back(I->get()); @@ -780,7 +908,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else if (I->get().ends_with("project.godot")) { String path; String file = I->get(); - int sep = MAX(file.find_last("/"), file.find_last("\\")); + int sep = MAX(file.rfind("/"), file.rfind("\\")); if (sep == -1) { path = "."; } else { @@ -818,7 +946,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else if (I->get() == "--time-scale") { // force time scale if (I->next()) { - Engine::get_singleton()->set_time_scale(I->next()->get().to_double()); + Engine::get_singleton()->set_time_scale(I->next()->get().to_float()); N = I->next()->next(); } else { OS::get_singleton()->print("Missing time scale argument, aborting.\n"); @@ -836,6 +964,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else if (I->get() == "-d" || I->get() == "--debug") { debug_uri = "local://"; + OS::get_singleton()->_debug_stdout = true; #if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED) } else if (I->get() == "--debug-collisions") { debug_collisions = true; @@ -846,7 +975,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph if (I->next()) { debug_uri = I->next()->get(); if (debug_uri.find("://") == -1) { // wrong address - OS::get_singleton()->print("Invalid debug host address, it should be of the form <protocol>://<host/IP>:<port>.\n"); + OS::get_singleton()->print( + "Invalid debug host address, it should be of the form <protocol>://<host/IP>:<port>.\n"); goto error; } N = I->next()->next(); @@ -856,7 +986,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } } else if (I->get() == "--allow_focus_steal_pid") { // not exposed to user if (I->next()) { - allow_focus_steal_pid = I->next()->get().to_int64(); + allow_focus_steal_pid = I->next()->get().to_int(); N = I->next()->next(); } else { OS::get_singleton()->print("Missing editor PID argument, aborting.\n"); @@ -874,6 +1004,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } } else if (I->get() == "--print-fps") { print_fps = true; + } else if (I->get() == "--profile-gpu") { + profile_gpu = true; } else if (I->get() == "--disable-crash-handler") { OS::get_singleton()->disable_crash_handler(); } else if (I->get() == "--skip-breakpoints") { @@ -887,7 +1019,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph #ifdef TOOLS_ENABLED if (editor && project_manager) { - OS::get_singleton()->print("Error: Command line arguments implied opening both editor and project manager, which is not possible. Aborting.\n"); + OS::get_singleton()->print( + "Error: Command line arguments implied opening both editor and project manager, which is not possible. Aborting.\n"); goto error; } #endif @@ -930,16 +1063,39 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph #endif } + // Initialize user data dir. + OS::get_singleton()->ensure_user_data_dir(); + GLOBAL_DEF("memory/limits/multithreaded_server/rid_pool_prealloc", 60); - ProjectSettings::get_singleton()->set_custom_property_info("memory/limits/multithreaded_server/rid_pool_prealloc", PropertyInfo(Variant::INT, "memory/limits/multithreaded_server/rid_pool_prealloc", PROPERTY_HINT_RANGE, "0,500,1")); // No negative and limit to 500 due to crashes + ProjectSettings::get_singleton()->set_custom_property_info("memory/limits/multithreaded_server/rid_pool_prealloc", + PropertyInfo(Variant::INT, + "memory/limits/multithreaded_server/rid_pool_prealloc", + PROPERTY_HINT_RANGE, + "0,500,1")); // No negative and limit to 500 due to crashes GLOBAL_DEF("network/limits/debugger/max_chars_per_second", 32768); - ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_chars_per_second", PropertyInfo(Variant::INT, "network/limits/debugger/max_chars_per_second", PROPERTY_HINT_RANGE, "0, 4096, 1, or_greater")); + ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_chars_per_second", + PropertyInfo(Variant::INT, + "network/limits/debugger/max_chars_per_second", + PROPERTY_HINT_RANGE, + "0, 4096, 1, or_greater")); GLOBAL_DEF("network/limits/debugger/max_queued_messages", 2048); - ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_queued_messages", PropertyInfo(Variant::INT, "network/limits/debugger/max_queued_messages", PROPERTY_HINT_RANGE, "0, 8192, 1, or_greater")); + ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_queued_messages", + PropertyInfo(Variant::INT, + "network/limits/debugger/max_queued_messages", + PROPERTY_HINT_RANGE, + "0, 8192, 1, or_greater")); GLOBAL_DEF("network/limits/debugger/max_errors_per_second", 400); - ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_errors_per_second", PropertyInfo(Variant::INT, "network/limits/debugger/max_errors_per_second", PROPERTY_HINT_RANGE, "0, 200, 1, or_greater")); + ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_errors_per_second", + PropertyInfo(Variant::INT, + "network/limits/debugger/max_errors_per_second", + PROPERTY_HINT_RANGE, + "0, 200, 1, or_greater")); GLOBAL_DEF("network/limits/debugger/max_warnings_per_second", 400); - ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_warnings_per_second", PropertyInfo(Variant::INT, "network/limits/debugger/max_warnings_per_second", PROPERTY_HINT_RANGE, "0, 200, 1, or_greater")); + ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_warnings_per_second", + PropertyInfo(Variant::INT, + "network/limits/debugger/max_warnings_per_second", + PROPERTY_HINT_RANGE, + "0, 200, 1, or_greater")); EngineDebugger::initialize(debug_uri, skip_breakpoints, breakpoints); @@ -948,19 +1104,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph packed_data->set_disabled(true); globals->set_disable_feature_overrides(true); } - #endif - GLOBAL_DEF("logging/file_logging/enable_file_logging", false); - GLOBAL_DEF("logging/file_logging/log_path", "user://logs/log.txt"); - GLOBAL_DEF("logging/file_logging/max_log_files", 10); - ProjectSettings::get_singleton()->set_custom_property_info("logging/file_logging/max_log_files", PropertyInfo(Variant::INT, "logging/file_logging/max_log_files", PROPERTY_HINT_RANGE, "0,20,1,or_greater")); //no negative numbers - if (FileAccess::get_create_func(FileAccess::ACCESS_USERDATA) && GLOBAL_GET("logging/file_logging/enable_file_logging")) { - String base_path = GLOBAL_GET("logging/file_logging/log_path"); - int max_files = GLOBAL_GET("logging/file_logging/max_log_files"); - OS::get_singleton()->add_logger(memnew(RotatedFileLogger(base_path, max_files))); - } - #ifdef TOOLS_ENABLED if (editor) { Engine::get_singleton()->set_editor_hint(true); @@ -977,7 +1122,29 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } #endif - if (main_args.size() == 0 && String(GLOBAL_DEF("application/run/main_scene", "")) == "") { + GLOBAL_DEF("debug/file_logging/enable_file_logging", false); + // Only file logging by default on desktop platforms as logs can't be + // accessed easily on mobile/Web platforms (if at all). + // This also prevents logs from being created for the editor instance, as feature tags + // are disabled while in the editor (even if they should logically apply). + GLOBAL_DEF("debug/file_logging/enable_file_logging.pc", true); + GLOBAL_DEF("debug/file_logging/log_path", "user://logs/godot.log"); + GLOBAL_DEF("debug/file_logging/max_log_files", 5); + ProjectSettings::get_singleton()->set_custom_property_info("debug/file_logging/max_log_files", + PropertyInfo(Variant::INT, + "debug/file_logging/max_log_files", + PROPERTY_HINT_RANGE, + "0,20,1,or_greater")); //no negative numbers + if (!project_manager && !editor && FileAccess::get_create_func(FileAccess::ACCESS_USERDATA) && + GLOBAL_GET("debug/file_logging/enable_file_logging")) { + // Don't create logs for the project manager as they would be written to + // the current working directory, which is inconvenient. + String base_path = GLOBAL_GET("debug/file_logging/log_path"); + int max_files = GLOBAL_GET("debug/file_logging/max_log_files"); + OS::get_singleton()->add_logger(memnew(RotatedFileLogger(base_path, max_files))); + } + + if (main_args.size() == 0 && String(GLOBAL_GET("application/run/main_scene")) == "") { #ifdef TOOLS_ENABLED if (!editor && !project_manager) { #endif @@ -993,7 +1160,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph use_custom_res = false; input_map->load_default(); //keys for editor } else { - input_map->load_from_globals(); //keys for game + input_map->load_from_project_settings(); //keys for game } if (bool(ProjectSettings::get_singleton()->get("application/run/disable_stdout"))) { @@ -1009,34 +1176,49 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph OS::get_singleton()->set_cmdline(execpath, main_args); - GLOBAL_DEF("rendering/quality/driver/driver_name", "Vulkan"); - ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/driver/driver_name", PropertyInfo(Variant::STRING, "rendering/quality/driver/driver_name", PROPERTY_HINT_ENUM, "Vulkan,GLES2")); + GLOBAL_DEF("rendering/driver/driver_name", "Vulkan"); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/driver/driver_name", + PropertyInfo(Variant::STRING, + "rendering/driver/driver_name", + PROPERTY_HINT_ENUM, "Vulkan")); if (display_driver == "") { - display_driver = GLOBAL_GET("rendering/quality/driver/driver_name"); - } - - // Assigning here even though it's GLES2-specific, to be sure that it appears in docs - GLOBAL_DEF("rendering/quality/2d/gles2_use_nvidia_rect_flicker_workaround", false); - - GLOBAL_DEF("display/window/size/width", 1024); - ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/width", PropertyInfo(Variant::INT, "display/window/size/width", PROPERTY_HINT_RANGE, "0,7680,or_greater")); // 8K resolution - GLOBAL_DEF("display/window/size/height", 600); - ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/height", PropertyInfo(Variant::INT, "display/window/size/height", PROPERTY_HINT_RANGE, "0,4320,or_greater")); // 8K resolution - GLOBAL_DEF("display/window/size/resizable", true); - GLOBAL_DEF("display/window/size/borderless", false); - GLOBAL_DEF("display/window/size/fullscreen", false); + display_driver = GLOBAL_GET("rendering/driver/driver_name"); + } + + GLOBAL_DEF_BASIC("display/window/size/width", 1024); + ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/width", + PropertyInfo(Variant::INT, "display/window/size/width", + PROPERTY_HINT_RANGE, + "0,7680,or_greater")); // 8K resolution + GLOBAL_DEF_BASIC("display/window/size/height", 600); + ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/height", + PropertyInfo(Variant::INT, "display/window/size/height", + PROPERTY_HINT_RANGE, + "0,4320,or_greater")); // 8K resolution + GLOBAL_DEF_BASIC("display/window/size/resizable", true); + GLOBAL_DEF_BASIC("display/window/size/borderless", false); + GLOBAL_DEF_BASIC("display/window/size/fullscreen", false); GLOBAL_DEF("display/window/size/always_on_top", false); GLOBAL_DEF("display/window/size/test_width", 0); - ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/test_width", PropertyInfo(Variant::INT, "display/window/size/test_width", PROPERTY_HINT_RANGE, "0,7680,or_greater")); // 8K resolution + ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/test_width", + PropertyInfo(Variant::INT, + "display/window/size/test_width", + PROPERTY_HINT_RANGE, + "0,7680,or_greater")); // 8K resolution GLOBAL_DEF("display/window/size/test_height", 0); - ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/test_height", PropertyInfo(Variant::INT, "display/window/size/test_height", PROPERTY_HINT_RANGE, "0,4320,or_greater")); // 8K resolution + ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/test_height", + PropertyInfo(Variant::INT, + "display/window/size/test_height", + PROPERTY_HINT_RANGE, + "0,4320,or_greater")); // 8K resolution if (use_custom_res) { if (!force_res) { window_size.width = GLOBAL_GET("display/window/size/width"); window_size.height = GLOBAL_GET("display/window/size/height"); - if (globals->has_setting("display/window/size/test_width") && globals->has_setting("display/window/size/test_height")) { + if (globals->has_setting("display/window/size/test_width") && + globals->has_setting("display/window/size/test_height")) { int tw = globals->get("display/window/size/test_width"); if (tw > 0) { window_size.width = tw; @@ -1059,10 +1241,12 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } if (bool(GLOBAL_GET("display/window/size/always_on_top"))) { - window_flags |= DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP; + window_flags |= DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP_BIT; } } + GLOBAL_DEF("internationalization/rendering/force_right_to_left_layout_direction", false); + if (!force_lowdpi) { OS::get_singleton()->_allow_hidpi = GLOBAL_DEF("display/window/dpi/allow_hidpi", false); } @@ -1080,44 +1264,29 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph OS::get_singleton()->_vsync_via_compositor = window_vsync_via_compositor; - if (tablet_driver == "") { // specified in project.godot - tablet_driver = GLOBAL_DEF_RST("display/window/tablet_driver", OS::get_singleton()->get_tablet_driver_name(0)); - } - - for (int i = 0; i < OS::get_singleton()->get_tablet_driver_count(); i++) { - if (tablet_driver == OS::get_singleton()->get_tablet_driver_name(i)) { - OS::get_singleton()->set_current_tablet_driver(OS::get_singleton()->get_tablet_driver_name(i)); - break; - } - } - - if (tablet_driver == "") { - OS::get_singleton()->set_current_tablet_driver(OS::get_singleton()->get_tablet_driver_name(0)); - } - /* todo restore - OS::get_singleton()->_allow_layered = GLOBAL_DEF("display/window/per_pixel_transparency/allowed", false); - video_mode.layered = GLOBAL_DEF("display/window/per_pixel_transparency/enabled", false); + OS::get_singleton()->_allow_layered = GLOBAL_DEF("display/window/per_pixel_transparency/allowed", false); + video_mode.layered = GLOBAL_DEF("display/window/per_pixel_transparency/enabled", false); */ - GLOBAL_DEF("rendering/quality/intended_usage/framebuffer_allocation", 2); - GLOBAL_DEF("rendering/quality/intended_usage/framebuffer_allocation.mobile", 3); - if (editor || project_manager) { // The editor and project manager always detect and use hiDPI if needed OS::get_singleton()->_allow_hidpi = true; OS::get_singleton()->_allow_layered = false; } - Engine::get_singleton()->_pixel_snap = GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false); OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true); if (rtm == -1) { - rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE); + rtm = GLOBAL_DEF("rendering/driver/threads/thread_model", OS::RENDER_THREAD_SAFE); } if (rtm >= 0 && rtm < 3) { +#ifdef NO_THREADS + rtm = OS::RENDER_THREAD_UNSAFE; // No threads available on this platform. +#else if (editor) { rtm = OS::RENDER_THREAD_SAFE; } +#endif OS::get_singleton()->_render_thread_mode = OS::RenderThreadMode(rtm); } @@ -1135,7 +1304,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } if (audio_driver == "") { // specified in project.godot - audio_driver = GLOBAL_DEF_RST("audio/driver", AudioDriverManager::get_driver(0)->get_name()); + audio_driver = GLOBAL_DEF_RST_NOVAL("audio/driver/driver", AudioDriverManager::get_driver(0)->get_name()); } for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) { @@ -1169,11 +1338,16 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } } - Engine::get_singleton()->set_iterations_per_second(GLOBAL_DEF("physics/common/physics_fps", 60)); - ProjectSettings::get_singleton()->set_custom_property_info("physics/common/physics_fps", PropertyInfo(Variant::INT, "physics/common/physics_fps", PROPERTY_HINT_RANGE, "1,120,1,or_greater")); + Engine::get_singleton()->set_iterations_per_second(GLOBAL_DEF_BASIC("physics/common/physics_fps", 60)); + ProjectSettings::get_singleton()->set_custom_property_info("physics/common/physics_fps", + PropertyInfo(Variant::INT, "physics/common/physics_fps", + PROPERTY_HINT_RANGE, "1,120,1,or_greater")); Engine::get_singleton()->set_physics_jitter_fix(GLOBAL_DEF("physics/common/physics_jitter_fix", 0.5)); Engine::get_singleton()->set_target_fps(GLOBAL_DEF("debug/settings/fps/force_fps", 0)); - ProjectSettings::get_singleton()->set_custom_property_info("debug/settings/fps/force_fps", PropertyInfo(Variant::INT, "debug/settings/fps/force_fps", PROPERTY_HINT_RANGE, "0,120,1,or_greater")); + ProjectSettings::get_singleton()->set_custom_property_info("debug/settings/fps/force_fps", + PropertyInfo(Variant::INT, + "debug/settings/fps/force_fps", + PROPERTY_HINT_RANGE, "0,120,1,or_greater")); GLOBAL_DEF("debug/settings/stdout/print_fps", false); GLOBAL_DEF("debug/settings/stdout/verbose_stdout", false); @@ -1184,14 +1358,24 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph if (frame_delay == 0) { frame_delay = GLOBAL_DEF("application/run/frame_delay_msec", 0); - ProjectSettings::get_singleton()->set_custom_property_info("application/run/frame_delay_msec", PropertyInfo(Variant::INT, "application/run/frame_delay_msec", PROPERTY_HINT_RANGE, "0,100,1,or_greater")); // No negative numbers + ProjectSettings::get_singleton()->set_custom_property_info("application/run/frame_delay_msec", + PropertyInfo(Variant::INT, + "application/run/frame_delay_msec", + PROPERTY_HINT_RANGE, + "0,100,1,or_greater")); // No negative numbers } OS::get_singleton()->set_low_processor_usage_mode(GLOBAL_DEF("application/run/low_processor_mode", false)); - OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(GLOBAL_DEF("application/run/low_processor_mode_sleep_usec", 6900)); // Roughly 144 FPS - ProjectSettings::get_singleton()->set_custom_property_info("application/run/low_processor_mode_sleep_usec", PropertyInfo(Variant::INT, "application/run/low_processor_mode_sleep_usec", PROPERTY_HINT_RANGE, "0,33200,1,or_greater")); // No negative numbers + OS::get_singleton()->set_low_processor_usage_mode_sleep_usec( + GLOBAL_DEF("application/run/low_processor_mode_sleep_usec", 6900)); // Roughly 144 FPS + ProjectSettings::get_singleton()->set_custom_property_info("application/run/low_processor_mode_sleep_usec", + PropertyInfo(Variant::INT, + "application/run/low_processor_mode_sleep_usec", + PROPERTY_HINT_RANGE, + "0,33200,1,or_greater")); // No negative numbers GLOBAL_DEF("display/window/ios/hide_home_indicator", true); + GLOBAL_DEF("input_devices/pointing/ios/touch_delay", 0.150); Engine::get_singleton()->set_frame_delay(frame_delay); @@ -1205,6 +1389,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph error: + text_driver = ""; display_driver = ""; audio_driver = ""; tablet_driver = ""; @@ -1262,13 +1447,69 @@ Error Main::setup2(Thread::ID p_main_tid_override) { // Print engine name and version print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE)); +#if !defined(NO_THREADS) if (p_main_tid_override) { - Thread::_main_thread_id = p_main_tid_override; + Thread::main_thread_id = p_main_tid_override; } +#endif - /* Initialize user data dir */ + /* Determine text driver */ - OS::get_singleton()->ensure_user_data_dir(); + if (text_driver == "") { + text_driver = GLOBAL_GET("internationalization/rendering/text_driver"); + } + + if (text_driver != "") { + /* Load user selected text server. */ + for (int i = 0; i < TextServerManager::get_interface_count(); i++) { + if (text_driver == TextServerManager::get_interface_name(i)) { + text_driver_idx = i; + break; + } + } + } + + if (text_driver_idx < 0) { + /* If not selected, use one with the most features available. */ + int max_features = 0; + for (int i = 0; i < TextServerManager::get_interface_count(); i++) { + uint32_t ftrs = TextServerManager::get_interface_features(i); + int features = 0; + while (ftrs) { + features += ftrs & 1; + ftrs >>= 1; + } + if (features >= max_features) { + max_features = features; + text_driver_idx = i; + } + } + } + print_verbose("Using \"" + TextServerManager::get_interface_name(text_driver_idx) + "\" text server..."); + + /* Initialize Text Server */ + + { + tsman = memnew(TextServerManager); + Error err; + TextServer *text_server = TextServerManager::initialize(text_driver_idx, err); + if (err != OK || text_server == nullptr) { + for (int i = 0; i < TextServerManager::get_interface_count(); i++) { + if (i == text_driver_idx) { + continue; //don't try the same twice + } + text_server = TextServerManager::initialize(i, err); + if (err == OK && text_server != nullptr) { + break; + } + } + } + + if (err != OK || text_server == nullptr) { + ERR_PRINT("Unable to create TextServer, all text drivers failed."); + return err; + } + } /* Initialize Input */ @@ -1281,20 +1522,20 @@ Error Main::setup2(Thread::ID p_main_tid_override) { Error err; display_server = DisplayServer::create(display_driver_idx, rendering_driver, window_mode, window_flags, window_size, err); - if (err != OK) { + if (err != OK || display_server == nullptr) { //ok i guess we can't use this display server, try other ones for (int i = 0; i < DisplayServer::get_create_function_count(); i++) { if (i == display_driver_idx) { continue; //don't try the same twice } - display_server = DisplayServer::create(display_driver_idx, rendering_driver, window_mode, window_flags, window_size, err); - if (err == OK) { + display_server = DisplayServer::create(i, rendering_driver, window_mode, window_flags, window_size, err); + if (err == OK && display_server != nullptr) { break; } } } - if (!display_server) { + if (err != OK || display_server == nullptr) { ERR_PRINT("Unable to create DisplayServer, all display drivers failed."); return err; } @@ -1304,14 +1545,44 @@ Error Main::setup2(Thread::ID p_main_tid_override) { display_server->screen_set_orientation(window_orientation); } - /* Initialize Visual Server */ + /* Initialize Pen Table Driver */ + + { + GLOBAL_DEF_RST_NOVAL("input_devices/pen_tablet/driver", ""); + GLOBAL_DEF_RST_NOVAL("input_devices/pen_tablet/driver.windows", ""); + ProjectSettings::get_singleton()->set_custom_property_info("input_devices/pen_tablet/driver.windows", PropertyInfo(Variant::STRING, "input_devices/pen_tablet/driver.windows", PROPERTY_HINT_ENUM, "wintab,winink")); + } + + if (tablet_driver == "") { // specified in project.godot + tablet_driver = GLOBAL_GET("input_devices/pen_tablet/driver"); + if (tablet_driver == "") { + tablet_driver = DisplayServer::get_singleton()->tablet_get_driver_name(0); + } + } + + for (int i = 0; i < DisplayServer::get_singleton()->tablet_get_driver_count(); i++) { + if (tablet_driver == DisplayServer::get_singleton()->tablet_get_driver_name(i)) { + DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(i)); + break; + } + } - rendering_server = memnew(RenderingServerRaster); - if (OS::get_singleton()->get_render_thread_mode() != OS::RENDER_THREAD_UNSAFE) { - rendering_server = memnew(RenderingServerWrapMT(rendering_server, OS::get_singleton()->get_render_thread_mode() == OS::RENDER_SEPARATE_THREAD)); + if (DisplayServer::get_singleton()->tablet_get_current_driver() == "") { + DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(0)); } + print_verbose("Using \"" + tablet_driver + "\" pen tablet driver..."); + + /* Initialize Visual Server */ + + rendering_server = memnew(RenderingServerDefault(OS::get_singleton()->get_render_thread_mode() == OS::RENDER_SEPARATE_THREAD)); + rendering_server->init(); + rendering_server->set_render_loop_enabled(!disable_render_loop); + + if (profile_gpu) { + rendering_server->set_print_gpu_profile(true); + } OS::get_singleton()->initialize_joypads(); @@ -1337,7 +1608,7 @@ Error Main::setup2(Thread::ID p_main_tid_override) { MAIN_PRINT("Main: Setup Logo"); -#ifdef JAVASCRIPT_ENABLED +#if defined(JAVASCRIPT_ENABLED) || defined(ANDROID_ENABLED) bool show_logo = false; #else bool show_logo = true; @@ -1363,16 +1634,19 @@ Error Main::setup2(Thread::ID p_main_tid_override) { register_server_types(); - MAIN_PRINT("Main: Load Remaps"); + MAIN_PRINT("Main: Load Boot Image"); - Color clear = GLOBAL_DEF("rendering/environment/default_clear_color", Color(0.3, 0.3, 0.3)); + Color clear = GLOBAL_DEF("rendering/environment/defaults/default_clear_color", Color(0.3, 0.3, 0.3)); RenderingServer::get_singleton()->set_default_clear_color(clear); if (show_logo) { //boot logo! String boot_logo_path = GLOBAL_DEF("application/boot_splash/image", String()); bool boot_logo_scale = GLOBAL_DEF("application/boot_splash/fullsize", true); bool boot_logo_filter = GLOBAL_DEF("application/boot_splash/use_filter", true); - ProjectSettings::get_singleton()->set_custom_property_info("application/boot_splash/image", PropertyInfo(Variant::STRING, "application/boot_splash/image", PROPERTY_HINT_FILE, "*.png")); + ProjectSettings::get_singleton()->set_custom_property_info("application/boot_splash/image", + PropertyInfo(Variant::STRING, + "application/boot_splash/image", + PROPERTY_HINT_FILE, "*.png")); Ref<Image> boot_logo; @@ -1388,8 +1662,8 @@ Error Main::setup2(Thread::ID p_main_tid_override) { Color boot_bg_color = GLOBAL_DEF("application/boot_splash/bg_color", boot_splash_bg_color); if (boot_logo.is_valid()) { - OS::get_singleton()->_msec_splash = OS::get_singleton()->get_ticks_msec(); - RenderingServer::get_singleton()->set_boot_image(boot_logo, boot_bg_color, boot_logo_scale, boot_logo_filter); + RenderingServer::get_singleton()->set_boot_image(boot_logo, boot_bg_color, boot_logo_scale, + boot_logo_filter); } else { #ifndef NO_DEFAULT_BOOT_LOGO @@ -1414,21 +1688,30 @@ Error Main::setup2(Thread::ID p_main_tid_override) { } MAIN_PRINT("Main: DCC"); - RenderingServer::get_singleton()->set_default_clear_color(GLOBAL_DEF("rendering/environment/default_clear_color", Color(0.3, 0.3, 0.3))); - MAIN_PRINT("Main: END"); + RenderingServer::get_singleton()->set_default_clear_color( + GLOBAL_DEF("rendering/environment/defaults/default_clear_color", Color(0.3, 0.3, 0.3))); GLOBAL_DEF("application/config/icon", String()); - ProjectSettings::get_singleton()->set_custom_property_info("application/config/icon", PropertyInfo(Variant::STRING, "application/config/icon", PROPERTY_HINT_FILE, "*.png,*.webp")); + ProjectSettings::get_singleton()->set_custom_property_info("application/config/icon", + PropertyInfo(Variant::STRING, "application/config/icon", + PROPERTY_HINT_FILE, "*.png,*.webp,*.svg,*.svgz")); GLOBAL_DEF("application/config/macos_native_icon", String()); - ProjectSettings::get_singleton()->set_custom_property_info("application/config/macos_native_icon", PropertyInfo(Variant::STRING, "application/config/macos_native_icon", PROPERTY_HINT_FILE, "*.icns")); + ProjectSettings::get_singleton()->set_custom_property_info("application/config/macos_native_icon", + PropertyInfo(Variant::STRING, + "application/config/macos_native_icon", + PROPERTY_HINT_FILE, "*.icns")); GLOBAL_DEF("application/config/windows_native_icon", String()); - ProjectSettings::get_singleton()->set_custom_property_info("application/config/windows_native_icon", PropertyInfo(Variant::STRING, "application/config/windows_native_icon", PROPERTY_HINT_FILE, "*.ico")); + ProjectSettings::get_singleton()->set_custom_property_info("application/config/windows_native_icon", + PropertyInfo(Variant::STRING, + "application/config/windows_native_icon", + PROPERTY_HINT_FILE, "*.ico")); Input *id = Input::get_singleton(); if (id) { - if (bool(GLOBAL_DEF("input_devices/pointing/emulate_touch_from_mouse", false)) && !(editor || project_manager)) { + if (bool(GLOBAL_DEF("input_devices/pointing/emulate_touch_from_mouse", false)) && + !(editor || project_manager)) { bool found_touchscreen = false; for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) { if (DisplayServer::get_singleton()->screen_is_touchscreen(i)) { @@ -1444,7 +1727,16 @@ Error Main::setup2(Thread::ID p_main_tid_override) { id->set_emulate_mouse_from_touch(bool(GLOBAL_DEF("input_devices/pointing/emulate_mouse_from_touch", true))); } - MAIN_PRINT("Main: Load Remaps"); + MAIN_PRINT("Main: Load Translations and Remaps"); + + translation_server->setup(); //register translations, load them, etc. + if (locale != "") { + translation_server->set_locale(locale); + } + translation_server->load_translations(); + ResourceLoader::load_translation_remaps(); //load remaps for resources + + ResourceLoader::load_path_remaps(); MAIN_PRINT("Main: Load Scene Types"); @@ -1453,10 +1745,14 @@ Error Main::setup2(Thread::ID p_main_tid_override) { GLOBAL_DEF("display/mouse_cursor/custom_image", String()); GLOBAL_DEF("display/mouse_cursor/custom_image_hotspot", Vector2()); GLOBAL_DEF("display/mouse_cursor/tooltip_position_offset", Point2(10, 10)); - ProjectSettings::get_singleton()->set_custom_property_info("display/mouse_cursor/custom_image", PropertyInfo(Variant::STRING, "display/mouse_cursor/custom_image", PROPERTY_HINT_FILE, "*.png,*.webp")); + ProjectSettings::get_singleton()->set_custom_property_info("display/mouse_cursor/custom_image", + PropertyInfo(Variant::STRING, + "display/mouse_cursor/custom_image", + PROPERTY_HINT_FILE, "*.png,*.webp")); if (String(ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image")) != String()) { - Ref<Texture2D> cursor = ResourceLoader::load(ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image")); + Ref<Texture2D> cursor = ResourceLoader::load( + ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image")); if (cursor.is_valid()) { Vector2 hotspot = ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image_hotspot"); Input::get_singleton()->set_custom_mouse_cursor(cursor, Input::CURSOR_ARROW, hotspot); @@ -1486,17 +1782,6 @@ Error Main::setup2(Thread::ID p_main_tid_override) { // This loads global classes, so it must happen before custom loaders and savers are registered ScriptServer::init_languages(); - MAIN_PRINT("Main: Load Translations"); - - translation_server->setup(); //register translations, load them, etc. - if (locale != "") { - translation_server->set_locale(locale); - } - translation_server->load_translations(); - ResourceLoader::load_translation_remaps(); //load remaps for resources - - ResourceLoader::load_path_remaps(); - audio_server->load_default_bus_layout(); if (use_debug_profiler && EngineDebugger::is_active()) { @@ -1537,7 +1822,6 @@ bool Main::start() { String positional_arg; String game_path; String script; - String test; bool check_only = false; #ifdef TOOLS_ENABLED @@ -1548,10 +1832,12 @@ bool Main::start() { #endif main_timer_sync.init(OS::get_singleton()->get_ticks_usec()); - List<String> args = OS::get_singleton()->get_cmdline_args(); + + // parameters that do not have an argument to the right for (int i = 0; i < args.size(); i++) { - //parameters that do not have an argument to the right + // Doctest Unit Testing Handler + // Designed to override and pass arguments to the unit test handler. if (args[i] == "--check-only") { check_only = true; #ifdef TOOLS_ENABLED @@ -1584,8 +1870,6 @@ bool Main::start() { bool parsed_pair = true; if (args[i] == "-s" || args[i] == "--script") { script = args[i + 1]; - } else if (args[i] == "--test") { - test = args[i + 1]; #ifdef TOOLS_ENABLED } else if (args[i] == "--doctool") { doc_tool = args[i + 1]; @@ -1614,14 +1898,14 @@ bool Main::start() { } } - String main_loop_type; #ifdef TOOLS_ENABLED if (doc_tool != "") { - Engine::get_singleton()->set_editor_hint(true); // Needed to instance editor-only classes for their default values + Engine::get_singleton()->set_editor_hint( + true); // Needed to instance editor-only classes for their default values { DirAccessRef da = DirAccess::open(doc_tool); - ERR_FAIL_COND_V_MSG(!da, false, "Argument supplied to --doctool must be a base Godot build directory."); + ERR_FAIL_COND_V_MSG(!da, false, "Argument supplied to --doctool must be a valid directory path."); } #ifndef MODULE_MONO_ENABLED @@ -1634,18 +1918,24 @@ bool Main::start() { GLOBAL_DEF("mono/profiler/args", "log:calls,alloc,sample,output=output.mlpd"); GLOBAL_DEF("mono/profiler/enabled", false); GLOBAL_DEF("mono/unhandled_exception_policy", 0); + // From editor/csharp_project.cpp. + GLOBAL_DEF("mono/project/auto_update_project", true); #endif - DocData doc; + DocTools doc; doc.generate(doc_base); - DocData docsrc; + DocTools docsrc; Map<String, String> doc_data_classes; Set<String> checked_paths; print_line("Loading docs..."); for (int i = 0; i < _doc_data_class_path_count; i++) { - String path = doc_tool.plus_file(_doc_data_class_paths[i].path); + // Custom modules are always located by absolute path. + String path = _doc_data_class_paths[i].path; + if (path.is_rel_path()) { + path = doc_tool.plus_file(path); + } String name = _doc_data_class_paths[i].name; doc_data_classes[name] = path; if (!checked_paths.has(path)) { @@ -1675,7 +1965,7 @@ bool Main::start() { doc.merge_from(docsrc); for (Set<String>::Element *E = checked_paths.front(); E; E = E->next()) { print_line("Erasing old docs at: " + E->get()); - DocData::erase_classes(E->get()); + DocTools::erase_classes(E->get()); } print_line("Generating new docs..."); @@ -1684,35 +1974,19 @@ bool Main::start() { return false; } - if (_export_preset != "") { - if (positional_arg == "") { - String err = "Command line includes export parameter option, but no destination path was given.\n"; - err += "Please specify the binary's file path to export to. Aborting export."; - ERR_PRINT(err); - return false; - } - } #endif - if (script == "" && game_path == "" && String(GLOBAL_DEF("application/run/main_scene", "")) != "") { - game_path = GLOBAL_DEF("application/run/main_scene", ""); + if (script == "" && game_path == "" && String(GLOBAL_GET("application/run/main_scene")) != "") { + game_path = GLOBAL_GET("application/run/main_scene"); } MainLoop *main_loop = nullptr; if (editor) { main_loop = memnew(SceneTree); }; + String main_loop_type = GLOBAL_DEF("application/run/main_loop_type", "SceneTree"); - if (test != "") { -#ifdef TOOLS_ENABLED - main_loop = test_main(test, args); - - if (!main_loop) { - return false; - } -#endif - - } else if (script != "") { + if (script != "") { Ref<Script> script_res = ResourceLoader::load(script); ERR_FAIL_COND_V_MSG(script_res.is_null(), false, "Can't load script: " + script); @@ -1731,17 +2005,33 @@ bool Main::start() { if (obj) { memdelete(obj); } - ERR_FAIL_V_MSG(false, vformat("Can't load the script \"%s\" as it doesn't inherit from SceneTree or MainLoop.", script)); + ERR_FAIL_V_MSG(false, + vformat("Can't load the script \"%s\" as it doesn't inherit from SceneTree or MainLoop.", + script)); } - script_loop->set_init_script(script_res); + script_loop->set_initialize_script(script_res); main_loop = script_loop; } else { return false; } - - } else { - main_loop_type = GLOBAL_DEF("application/run/main_loop_type", ""); + } else { // Not based on script path. + if (!editor && !ClassDB::class_exists(main_loop_type) && ScriptServer::is_global_class(main_loop_type)) { + String script_path = ScriptServer::get_global_class_path(main_loop_type); + Ref<Script> script_res = ResourceLoader::load(script_path); + StringName script_base = ScriptServer::get_global_class_native_base(main_loop_type); + Object *obj = ClassDB::instance(script_base); + MainLoop *script_loop = Object::cast_to<MainLoop>(obj); + if (!script_loop) { + if (obj) { + memdelete(obj); + } + DisplayServer::get_singleton()->alert("Error: Invalid MainLoop script base type: " + script_base); + ERR_FAIL_V_MSG(false, vformat("The global class %s does not inherit from SceneTree or MainLoop.", main_loop_type)); + } + script_loop->set_initialize_script(script_res); + main_loop = script_loop; + } } if (!main_loop && main_loop_type == "") { @@ -1787,46 +2077,26 @@ bool Main::start() { if (!project_manager && !editor) { // game if (game_path != "" || script != "") { //autoload - List<PropertyInfo> props; - ProjectSettings::get_singleton()->get_property_list(&props); + Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list(); //first pass, add the constants so they exist before any script is loaded - for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) { - String s = E->get().name; - if (!s.begins_with("autoload/")) { - continue; - } - String name = s.get_slicec('/', 1); - String path = ProjectSettings::get_singleton()->get(s); - bool global_var = false; - if (path.begins_with("*")) { - global_var = true; - } + for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) { + const ProjectSettings::AutoloadInfo &info = E->get(); - if (global_var) { + if (info.is_singleton) { for (int i = 0; i < ScriptServer::get_language_count(); i++) { - ScriptServer::get_language(i)->add_global_constant(name, Variant()); + ScriptServer::get_language(i)->add_global_constant(info.name, Variant()); } } } //second pass, load into global constants List<Node *> to_add; - for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) { - String s = E->get().name; - if (!s.begins_with("autoload/")) { - continue; - } - String name = s.get_slicec('/', 1); - String path = ProjectSettings::get_singleton()->get(s); - bool global_var = false; - if (path.begins_with("*")) { - global_var = true; - path = path.substr(1, path.length() - 1); - } + for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) { + const ProjectSettings::AutoloadInfo &info = E->get(); - RES res = ResourceLoader::load(path); - ERR_CONTINUE_MSG(res.is_null(), "Can't autoload: " + path); + RES res = ResourceLoader::load(info.path); + ERR_CONTINUE_MSG(res.is_null(), "Can't autoload: " + info.path); Node *n = nullptr; if (res->is_class("PackedScene")) { Ref<PackedScene> ps = res; @@ -1835,25 +2105,27 @@ bool Main::start() { Ref<Script> script_res = res; StringName ibt = script_res->get_instance_base_type(); bool valid_type = ClassDB::is_parent_class(ibt, "Node"); - ERR_CONTINUE_MSG(!valid_type, "Script does not inherit a Node: " + path); + ERR_CONTINUE_MSG(!valid_type, "Script does not inherit a Node: " + info.path); Object *obj = ClassDB::instance(ibt); - ERR_CONTINUE_MSG(obj == nullptr, "Cannot instance script for autoload, expected 'Node' inheritance, got: " + String(ibt)); + ERR_CONTINUE_MSG(obj == nullptr, + "Cannot instance script for autoload, expected 'Node' inheritance, got: " + + String(ibt)); n = Object::cast_to<Node>(obj); n->set_script(script_res); } - ERR_CONTINUE_MSG(!n, "Path in autoload not a node or script: " + path); - n->set_name(name); + ERR_CONTINUE_MSG(!n, "Path in autoload not a node or script: " + info.path); + n->set_name(info.name); //defer so references are all valid on _ready() to_add.push_back(n); - if (global_var) { + if (info.is_singleton) { for (int i = 0; i < ScriptServer::get_language_count(); i++) { - ScriptServer::get_language(i)->add_global_constant(name, n); + ScriptServer::get_language(i)->add_global_constant(info.name, n); } } } @@ -1877,23 +2149,19 @@ bool Main::start() { } #endif - { - int directional_atlas_size = GLOBAL_GET("rendering/quality/directional_shadow/size"); - RenderingServer::get_singleton()->directional_shadow_atlas_set_size(directional_atlas_size); - } - if (!editor && !project_manager) { //standard helpers that can be changed from main config - String stretch_mode = GLOBAL_DEF("display/window/stretch/mode", "disabled"); - String stretch_aspect = GLOBAL_DEF("display/window/stretch/aspect", "ignore"); - Size2i stretch_size = Size2(GLOBAL_DEF("display/window/size/width", 0), GLOBAL_DEF("display/window/size/height", 0)); + String stretch_mode = GLOBAL_DEF_BASIC("display/window/stretch/mode", "disabled"); + String stretch_aspect = GLOBAL_DEF_BASIC("display/window/stretch/aspect", "ignore"); + Size2i stretch_size = Size2i(GLOBAL_DEF_BASIC("display/window/size/width", 0), + GLOBAL_DEF_BASIC("display/window/size/height", 0)); Window::ContentScaleMode cs_sm = Window::CONTENT_SCALE_MODE_DISABLED; - if (stretch_mode == "objects") { - cs_sm = Window::CONTENT_SCALE_MODE_OBJECTS; - } else if (stretch_mode == "pixels") { - cs_sm = Window::CONTENT_SCALE_MODE_PIXELS; + if (stretch_mode == "canvas_items") { + cs_sm = Window::CONTENT_SCALE_MODE_CANVAS_ITEMS; + } else if (stretch_mode == "viewport") { + cs_sm = Window::CONTENT_SCALE_MODE_VIEWPORT; } Window::ContentScaleAspect cs_aspect = Window::CONTENT_SCALE_ASPECT_IGNORE; @@ -1915,62 +2183,89 @@ bool Main::start() { sml->set_quit_on_go_back(GLOBAL_DEF("application/config/quit_on_go_back", true)); String appname = ProjectSettings::get_singleton()->get("application/config/name"); appname = TranslationServer::get_singleton()->translate(appname); +#ifdef DEBUG_ENABLED + // Append a suffix to the window title to denote that the project is running + // from a debug build (including the editor). Since this results in lower performance, + // this should be clearly presented to the user. + DisplayServer::get_singleton()->window_set_title(vformat("%s (DEBUG)", appname)); +#else DisplayServer::get_singleton()->window_set_title(appname); - - int shadow_atlas_size = GLOBAL_GET("rendering/quality/shadow_atlas/size"); - int shadow_atlas_q0_subdiv = GLOBAL_GET("rendering/quality/shadow_atlas/quadrant_0_subdiv"); - int shadow_atlas_q1_subdiv = GLOBAL_GET("rendering/quality/shadow_atlas/quadrant_1_subdiv"); - int shadow_atlas_q2_subdiv = GLOBAL_GET("rendering/quality/shadow_atlas/quadrant_2_subdiv"); - int shadow_atlas_q3_subdiv = GLOBAL_GET("rendering/quality/shadow_atlas/quadrant_3_subdiv"); - - sml->get_root()->set_shadow_atlas_size(shadow_atlas_size); - sml->get_root()->set_shadow_atlas_quadrant_subdiv(0, Viewport::ShadowAtlasQuadrantSubdiv(shadow_atlas_q0_subdiv)); - sml->get_root()->set_shadow_atlas_quadrant_subdiv(1, Viewport::ShadowAtlasQuadrantSubdiv(shadow_atlas_q1_subdiv)); - sml->get_root()->set_shadow_atlas_quadrant_subdiv(2, Viewport::ShadowAtlasQuadrantSubdiv(shadow_atlas_q2_subdiv)); - sml->get_root()->set_shadow_atlas_quadrant_subdiv(3, Viewport::ShadowAtlasQuadrantSubdiv(shadow_atlas_q3_subdiv)); +#endif bool snap_controls = GLOBAL_DEF("gui/common/snap_controls_to_pixels", true); sml->get_root()->set_snap_controls_to_pixels(snap_controls); - bool font_oversampling = GLOBAL_DEF("rendering/quality/dynamic_fonts/use_oversampling", true); + bool font_oversampling = GLOBAL_DEF("gui/fonts/dynamic_fonts/use_oversampling", true); sml->get_root()->set_use_font_oversampling(font_oversampling); - int texture_filter = GLOBAL_DEF("rendering/canvas_textures/default_texture_filter", 1); - int texture_repeat = GLOBAL_DEF("rendering/canvas_textures/default_texture_repeat", 0); - sml->get_root()->set_default_canvas_item_texture_filter(Viewport::DefaultCanvasItemTextureFilter(texture_filter)); - sml->get_root()->set_default_canvas_item_texture_repeat(Viewport::DefaultCanvasItemTextureRepeat(texture_repeat)); + int texture_filter = GLOBAL_DEF("rendering/textures/canvas_textures/default_texture_filter", 1); + int texture_repeat = GLOBAL_DEF("rendering/textures/canvas_textures/default_texture_repeat", 0); + sml->get_root()->set_default_canvas_item_texture_filter( + Viewport::DefaultCanvasItemTextureFilter(texture_filter)); + sml->get_root()->set_default_canvas_item_texture_repeat( + Viewport::DefaultCanvasItemTextureRepeat(texture_repeat)); } else { - GLOBAL_DEF("display/window/stretch/mode", "disabled"); - ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/mode", PropertyInfo(Variant::STRING, "display/window/stretch/mode", PROPERTY_HINT_ENUM, "disabled,2d,viewport")); - GLOBAL_DEF("display/window/stretch/aspect", "ignore"); - ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/aspect", PropertyInfo(Variant::STRING, "display/window/stretch/aspect", PROPERTY_HINT_ENUM, "ignore,keep,keep_width,keep_height,expand")); - GLOBAL_DEF("display/window/stretch/shrink", 1.0); - ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/shrink", PropertyInfo(Variant::FLOAT, "display/window/stretch/shrink", PROPERTY_HINT_RANGE, "1.0,8.0,0.1")); + GLOBAL_DEF_BASIC("display/window/stretch/mode", "disabled"); + ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/mode", + PropertyInfo(Variant::STRING, + "display/window/stretch/mode", + PROPERTY_HINT_ENUM, + "disabled,canvas_items,viewport")); + GLOBAL_DEF_BASIC("display/window/stretch/aspect", "ignore"); + ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/aspect", + PropertyInfo(Variant::STRING, + "display/window/stretch/aspect", + PROPERTY_HINT_ENUM, + "ignore,keep,keep_width,keep_height,expand")); + GLOBAL_DEF_BASIC("display/window/stretch/shrink", 1.0); + ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/shrink", + PropertyInfo(Variant::FLOAT, + "display/window/stretch/shrink", + PROPERTY_HINT_RANGE, + "1.0,8.0,0.1")); sml->set_auto_accept_quit(GLOBAL_DEF("application/config/auto_accept_quit", true)); sml->set_quit_on_go_back(GLOBAL_DEF("application/config/quit_on_go_back", true)); - GLOBAL_DEF("gui/common/snap_controls_to_pixels", true); - GLOBAL_DEF("rendering/quality/dynamic_fonts/use_oversampling", true); + GLOBAL_DEF_BASIC("gui/common/snap_controls_to_pixels", true); + GLOBAL_DEF_BASIC("gui/fonts/dynamic_fonts/use_oversampling", true); + + GLOBAL_DEF_BASIC("rendering/textures/canvas_textures/default_texture_filter", 1); + ProjectSettings::get_singleton()->set_custom_property_info( + "rendering/textures/canvas_textures/default_texture_filter", + PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_filter", PROPERTY_HINT_ENUM, + "Nearest,Linear,MipmapLinear,MipmapNearest")); + GLOBAL_DEF_BASIC("rendering/textures/canvas_textures/default_texture_repeat", 0); + ProjectSettings::get_singleton()->set_custom_property_info( + "rendering/textures/canvas_textures/default_texture_repeat", + PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_repeat", PROPERTY_HINT_ENUM, + "Disable,Enable,Mirror")); + } + +#ifdef TOOLS_ENABLED + if (editor) { + bool editor_embed_subwindows = EditorSettings::get_singleton()->get_setting( + "interface/editor/single_window_mode"); - GLOBAL_DEF("rendering/canvas_textures/default_texture_filter", 1); - ProjectSettings::get_singleton()->set_custom_property_info("rendering/canvas_textures/default_texture_filter", PropertyInfo(Variant::INT, "rendering/canvas_textures/default_texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,MipmapLinear,MipmapNearest")); - GLOBAL_DEF("rendering/canvas_textures/default_texture_repeat", 0); - ProjectSettings::get_singleton()->set_custom_property_info("rendering/canvas_textures/default_texture_repeat", PropertyInfo(Variant::INT, "rendering/canvas_textures/default_texture_repeat", PROPERTY_HINT_ENUM, "Disable,Enable,Mirror")); + if (editor_embed_subwindows) { + sml->get_root()->set_embed_subwindows_hint(true); + } } +#endif String local_game_path; if (game_path != "" && !project_manager) { local_game_path = game_path.replace("\\", "/"); if (!local_game_path.begins_with("res://")) { - bool absolute = (local_game_path.size() > 1) && (local_game_path[0] == '/' || local_game_path[1] == ':'); + bool absolute = + (local_game_path.size() > 1) && (local_game_path[0] == '/' || local_game_path[1] == ':'); if (!absolute) { if (ProjectSettings::get_singleton()->is_using_datapack()) { local_game_path = "res://" + local_game_path; } else { - int sep = local_game_path.find_last("/"); + int sep = local_game_path.rfind("/"); if (sep == -1) { DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); @@ -1979,7 +2274,8 @@ bool Main::start() { } else { DirAccess *da = DirAccess::open(local_game_path.substr(0, sep)); if (da) { - local_game_path = da->get_current_dir().plus_file(local_game_path.substr(sep + 1, local_game_path.length())); + local_game_path = da->get_current_dir().plus_file( + local_game_path.substr(sep + 1, local_game_path.length())); memdelete(da); } } @@ -1991,13 +2287,7 @@ bool Main::start() { #ifdef TOOLS_ENABLED if (editor) { - bool editor_embed_subwindows = EditorSettings::get_singleton()->get_setting("interface/editor/single_window_mode"); - - if (editor_embed_subwindows) { - sml->get_root()->set_embed_subwindows_hint(true); - } - - if (game_path != GLOBAL_GET("application/run/main_scene") || !editor_node->has_scenes_in_session()) { + if (game_path != String(GLOBAL_GET("application/run/main_scene")) || !editor_node->has_scenes_in_session()) { Error serr = editor_node->load_scene(local_game_path); if (serr != OK) { ERR_PRINT("Failed to load scene"); @@ -2055,7 +2345,7 @@ bool Main::start() { } #ifdef TOOLS_ENABLED - if (project_manager || (script == "" && test == "" && game_path == "" && !editor)) { + if (project_manager || (script == "" && game_path == "" && !editor)) { Engine::get_singleton()->set_editor_hint(true); ProjectManager *pmanager = memnew(ProjectManager); ProgressDialog *progress_dialog = memnew(ProgressDialog); @@ -2068,12 +2358,16 @@ bool Main::start() { if (project_manager || editor) { if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CONSOLE_WINDOW)) { // Hide console window if requested (Windows-only). - bool hide_console = EditorSettings::get_singleton()->get_setting("interface/editor/hide_console_window"); + bool hide_console = EditorSettings::get_singleton()->get_setting( + "interface/editor/hide_console_window"); DisplayServer::get_singleton()->console_set_visible(!hide_console); } // Load SSL Certificates from Editor Settings (or builtin) - Crypto::load_default_certificates(EditorSettings::get_singleton()->get_setting("network/ssl/editor_ssl_certificates").operator String()); + Crypto::load_default_certificates(EditorSettings::get_singleton()->get_setting( + "network/ssl/editor_ssl_certificates") + . + operator String()); } #endif } @@ -2099,18 +2393,18 @@ bool Main::start() { */ uint64_t Main::last_ticks = 0; -uint64_t Main::target_ticks = 0; uint32_t Main::frames = 0; uint32_t Main::frame = 0; bool Main::force_redraw_requested = false; int Main::iterating = 0; + bool Main::is_iterating() { return iterating > 0; } // For performance metrics. static uint64_t physics_process_max = 0; -static uint64_t idle_process_max = 0; +static uint64_t process_max = 0; bool Main::iteration() { //for now do not error on this @@ -2126,19 +2420,19 @@ bool Main::iteration() { uint64_t ticks_elapsed = ticks - last_ticks; int physics_fps = Engine::get_singleton()->get_iterations_per_second(); - float frame_slice = 1.0 / physics_fps; + float physics_step = 1.0 / physics_fps; float time_scale = Engine::get_singleton()->get_time_scale(); - MainFrameTime advance = main_timer_sync.advance(frame_slice, physics_fps); - double step = advance.idle_step; - double scaled_step = step * time_scale; + MainFrameTime advance = main_timer_sync.advance(physics_step, physics_fps); + double process_step = advance.process_step; + double scaled_step = process_step * time_scale; - Engine::get_singleton()->_frame_step = step; + Engine::get_singleton()->_process_step = process_step; Engine::get_singleton()->_physics_interpolation_fraction = advance.interpolation_fraction; uint64_t physics_process_ticks = 0; - uint64_t idle_process_ticks = 0; + uint64_t process_ticks = 0; frame += ticks_elapsed; @@ -2146,7 +2440,7 @@ bool Main::iteration() { static const int max_physics_steps = 8; if (fixed_fps == -1 && advance.physics_steps > max_physics_steps) { - step -= (advance.physics_steps - max_physics_steps) * frame_slice; + process_step -= (advance.physics_steps - max_physics_steps) * physics_step; advance.physics_steps = max_physics_steps; } @@ -2163,19 +2457,20 @@ bool Main::iteration() { PhysicsServer2D::get_singleton()->sync(); PhysicsServer2D::get_singleton()->flush_queries(); - if (OS::get_singleton()->get_main_loop()->iteration(frame_slice * time_scale)) { + if (OS::get_singleton()->get_main_loop()->physics_process(physics_step * time_scale)) { exit = true; break; } - NavigationServer3D::get_singleton_mut()->process(frame_slice * time_scale); + NavigationServer3D::get_singleton_mut()->process(physics_step * time_scale); message_queue->flush(); - PhysicsServer3D::get_singleton()->step(frame_slice * time_scale); + PhysicsServer3D::get_singleton()->end_sync(); + PhysicsServer3D::get_singleton()->step(physics_step * time_scale); PhysicsServer2D::get_singleton()->end_sync(); - PhysicsServer2D::get_singleton()->step(frame_slice * time_scale); + PhysicsServer2D::get_singleton()->step(physics_step * time_scale); message_queue->flush(); @@ -2186,16 +2481,17 @@ bool Main::iteration() { Engine::get_singleton()->_in_physics = false; - uint64_t idle_begin = OS::get_singleton()->get_ticks_usec(); + uint64_t process_begin = OS::get_singleton()->get_ticks_usec(); - if (OS::get_singleton()->get_main_loop()->idle(step * time_scale)) { + if (OS::get_singleton()->get_main_loop()->process(process_step * time_scale)) { exit = true; } message_queue->flush(); RenderingServer::get_singleton()->sync(); //sync if still drawing from previous frames. - if (DisplayServer::get_singleton()->can_any_window_draw() && !disable_render_loop) { + if (DisplayServer::get_singleton()->can_any_window_draw() && + RenderingServer::get_singleton()->is_render_loop_enabled()) { if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) { if (RenderingServer::get_singleton()->has_changed()) { RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands @@ -2208,8 +2504,8 @@ bool Main::iteration() { } } - idle_process_ticks = OS::get_singleton()->get_ticks_usec() - idle_begin; - idle_process_max = MAX(idle_process_ticks, idle_process_max); + process_ticks = OS::get_singleton()->get_ticks_usec() - process_begin; + process_max = MAX(process_ticks, process_max); uint64_t frame_time = OS::get_singleton()->get_ticks_usec() - ticks; for (int i = 0; i < ScriptServer::get_language_count(); i++) { @@ -2219,11 +2515,11 @@ bool Main::iteration() { AudioServer::get_singleton()->update(); if (EngineDebugger::is_active()) { - EngineDebugger::get_singleton()->iteration(frame_time, idle_process_ticks, physics_process_ticks, frame_slice); + EngineDebugger::get_singleton()->iteration(frame_time, process_ticks, physics_process_ticks, physics_step); } frames++; - Engine::get_singleton()->_idle_frames++; + Engine::get_singleton()->_process_frames++; if (frame > 1000000) { if (editor || project_manager) { @@ -2235,9 +2531,9 @@ bool Main::iteration() { } Engine::get_singleton()->_fps = frames; - performance->set_process_time(USEC_TO_SEC(idle_process_max)); + performance->set_process_time(USEC_TO_SEC(process_max)); performance->set_physics_process_time(USEC_TO_SEC(physics_process_max)); - idle_process_max = 0; + process_max = 0; physics_process_max = 0; frame %= 1000000; @@ -2250,36 +2546,19 @@ bool Main::iteration() { return exit; } - if (OS::get_singleton()->is_in_low_processor_usage_mode() || !DisplayServer::get_singleton()->can_any_window_draw()) { - OS::get_singleton()->delay_usec(OS::get_singleton()->get_low_processor_usage_mode_sleep_usec()); //apply some delay to force idle time - } else { - uint32_t frame_delay = Engine::get_singleton()->get_frame_delay(); - if (frame_delay) { - OS::get_singleton()->delay_usec(Engine::get_singleton()->get_frame_delay() * 1000); - } - } - - int target_fps = Engine::get_singleton()->get_target_fps(); - if (target_fps > 0 && !Engine::get_singleton()->is_editor_hint()) { - uint64_t time_step = 1000000L / target_fps; - target_ticks += time_step; - uint64_t current_ticks = OS::get_singleton()->get_ticks_usec(); - if (current_ticks < target_ticks) { - OS::get_singleton()->delay_usec(target_ticks - current_ticks); - } - current_ticks = OS::get_singleton()->get_ticks_usec(); - target_ticks = MIN(MAX(target_ticks, current_ticks - time_step), current_ticks + time_step); - } + OS::get_singleton()->add_frame_delay(DisplayServer::get_singleton()->window_can_draw()); #ifdef TOOLS_ENABLED if (auto_build_solutions) { auto_build_solutions = false; // Only relevant when running the editor. if (!editor) { - ERR_FAIL_V_MSG(true, "Command line option --build-solutions was passed, but no project is being edited. Aborting."); + ERR_FAIL_V_MSG(true, + "Command line option --build-solutions was passed, but no project is being edited. Aborting."); } if (!EditorNode::get_singleton()->call_build()) { - ERR_FAIL_V_MSG(true, "Command line option --build-solutions was passed, but the build callback failed. Aborting."); + ERR_FAIL_V_MSG(true, + "Command line option --build-solutions was passed, but the build callback failed. Aborting."); } } #endif @@ -2297,16 +2576,18 @@ void Main::force_redraw() { * so that the engine closes cleanly without leaking memory or crashing. * The order matters as some of those steps are linked with each other. */ -void Main::cleanup() { - ERR_FAIL_COND(!_start_success); +void Main::cleanup(bool p_force) { + if (!p_force) { + ERR_FAIL_COND(!_start_success); + } EngineDebugger::deinitialize(); ResourceLoader::remove_custom_loaders(); ResourceSaver::remove_custom_savers(); + // Flush before uninitializing the scene, but delete the MessageQueue as late as possible. message_queue->flush(); - memdelete(message_queue); OS::get_singleton()->delete_main_loop(); @@ -2357,6 +2638,10 @@ void Main::cleanup() { finalize_navigation_server(); finalize_display(); + if (tsman) { + memdelete(tsman); + } + if (input) { memdelete(input); } @@ -2387,11 +2672,14 @@ void Main::cleanup() { //attempt to restart with arguments String exec = OS::get_singleton()->get_executable_path(); List<String> args = OS::get_singleton()->get_restart_on_exit_arguments(); - OS::ProcessID pid = 0; - OS::get_singleton()->execute(exec, args, false, &pid); + OS::get_singleton()->create_process(exec, args); OS::get_singleton()->set_restart_on_exit(false, List<String>()); //clear list (uses memory) } + // Now should be safe to delete MessageQueue (famous last words). + message_queue->flush(); + memdelete(message_queue); + unregister_core_driver_types(); unregister_core_types(); diff --git a/main/main.h b/main/main.h index ab6917a65c..f4fff6b97e 100644 --- a/main/main.h +++ b/main/main.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 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 */ @@ -31,14 +31,13 @@ #ifndef MAIN_H #define MAIN_H -#include "core/error_list.h" +#include "core/error/error_list.h" #include "core/os/thread.h" #include "core/typedefs.h" class Main { static void print_help(const char *p_binary); static uint64_t last_ticks; - static uint64_t target_ticks; static uint32_t frames; static uint32_t frame; static bool force_redraw_requested; @@ -46,9 +45,13 @@ class Main { public: static bool is_project_manager(); - + static int test_entrypoint(int argc, char *argv[], bool &tests_need_run); static Error setup(const char *execpath, int argc, char *argv[], bool p_second_phase = true); static Error setup2(Thread::ID p_main_tid_override = 0); +#ifdef TESTS_ENABLED + static Error test_setup(); + static void test_cleanup(); +#endif static bool start(); static bool iteration(); @@ -56,7 +59,22 @@ public: static bool is_iterating(); - static void cleanup(); + static void cleanup(bool p_force = false); }; +// Test main override is for the testing behaviour. +#define TEST_MAIN_OVERRIDE \ + bool run_test = false; \ + int return_code = Main::test_entrypoint(argc, argv, run_test); \ + if (run_test) { \ + return return_code; \ + } + +#define TEST_MAIN_PARAM_OVERRIDE(argc, argv) \ + bool run_test = false; \ + int return_code = Main::test_entrypoint(argc, argv, run_test); \ + if (run_test) { \ + return return_code; \ + } + #endif // MAIN_H diff --git a/main/main_builders.py b/main/main_builders.py index 2ea774e3b4..aa91201c3e 100644 --- a/main/main_builders.py +++ b/main/main_builders.py @@ -4,7 +4,6 @@ All such functions are invoked in a subprocess on Windows to prevent build flaki """ from platform_methods import subprocess_main -from collections import OrderedDict def make_splash(target, source, env): diff --git a/main/main_timer_sync.cpp b/main/main_timer_sync.cpp index 5252ea005b..93448d0904 100644 --- a/main/main_timer_sync.cpp +++ b/main/main_timer_sync.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 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 */ @@ -30,17 +30,17 @@ #include "main_timer_sync.h" -void MainFrameTime::clamp_idle(float min_idle_step, float max_idle_step) { - if (idle_step < min_idle_step) { - idle_step = min_idle_step; - } else if (idle_step > max_idle_step) { - idle_step = max_idle_step; +void MainFrameTime::clamp_process_step(float min_process_step, float max_process_step) { + if (process_step < min_process_step) { + process_step = min_process_step; + } else if (process_step > max_process_step) { + process_step = max_process_step; } } ///////////////////////////////// -// returns the fraction of p_frame_slice required for the timer to overshoot +// returns the fraction of p_physics_step required for the timer to overshoot // before advance_core considers changing the physics_steps return from // the typical values as defined by typical_physics_steps float MainTimerSync::get_physics_jitter_fix() { @@ -72,15 +72,15 @@ int MainTimerSync::get_average_physics_steps(float &p_min, float &p_max) { return CONTROL_STEPS; } -// advance physics clock by p_idle_step, return appropriate number of steps to simulate -MainFrameTime MainTimerSync::advance_core(float p_frame_slice, int p_iterations_per_second, float p_idle_step) { +// advance physics clock by p_process_step, return appropriate number of steps to simulate +MainFrameTime MainTimerSync::advance_core(float p_physics_step, int p_physics_fps, float p_process_step) { MainFrameTime ret; - ret.idle_step = p_idle_step; + ret.process_step = p_process_step; // simple determination of number of physics iteration - time_accum += ret.idle_step; - ret.physics_steps = floor(time_accum * p_iterations_per_second); + time_accum += ret.process_step; + ret.physics_steps = floor(time_accum * p_physics_fps); int min_typical_steps = typical_physics_steps[0]; int max_typical_steps = min_typical_steps + 1; @@ -107,7 +107,7 @@ MainFrameTime MainTimerSync::advance_core(float p_frame_slice, int p_iterations_ // try to keep it consistent with previous iterations if (ret.physics_steps < min_typical_steps) { - const int max_possible_steps = floor((time_accum)*p_iterations_per_second + get_physics_jitter_fix()); + const int max_possible_steps = floor((time_accum)*p_physics_fps + get_physics_jitter_fix()); if (max_possible_steps < min_typical_steps) { ret.physics_steps = max_possible_steps; update_typical = true; @@ -115,7 +115,7 @@ MainFrameTime MainTimerSync::advance_core(float p_frame_slice, int p_iterations_ ret.physics_steps = min_typical_steps; } } else if (ret.physics_steps > max_typical_steps) { - const int min_possible_steps = floor((time_accum)*p_iterations_per_second - get_physics_jitter_fix()); + const int min_possible_steps = floor((time_accum)*p_physics_fps - get_physics_jitter_fix()); if (min_possible_steps > max_typical_steps) { ret.physics_steps = min_possible_steps; update_typical = true; @@ -124,7 +124,7 @@ MainFrameTime MainTimerSync::advance_core(float p_frame_slice, int p_iterations_ } } - time_accum -= ret.physics_steps * p_frame_slice; + time_accum -= ret.physics_steps * p_physics_step; // keep track of accumulated step counts for (int i = CONTROL_STEPS - 2; i >= 0; --i) { @@ -146,52 +146,52 @@ MainFrameTime MainTimerSync::advance_core(float p_frame_slice, int p_iterations_ } // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero -MainFrameTime MainTimerSync::advance_checked(float p_frame_slice, int p_iterations_per_second, float p_idle_step) { +MainFrameTime MainTimerSync::advance_checked(float p_physics_step, int p_physics_fps, float p_process_step) { if (fixed_fps != -1) { - p_idle_step = 1.0 / fixed_fps; + p_process_step = 1.0 / fixed_fps; } // compensate for last deficit - p_idle_step += time_deficit; + p_process_step += time_deficit; - MainFrameTime ret = advance_core(p_frame_slice, p_iterations_per_second, p_idle_step); + MainFrameTime ret = advance_core(p_physics_step, p_physics_fps, p_process_step); - // we will do some clamping on ret.idle_step and need to sync those changes to time_accum, + // we will do some clamping on ret.process_step and need to sync those changes to time_accum, // that's easiest if we just remember their fixed difference now - const double idle_minus_accum = ret.idle_step - time_accum; + const double process_minus_accum = ret.process_step - time_accum; - // first, least important clamping: keep ret.idle_step consistent with typical_physics_steps. - // this smoothes out the idle steps and culls small but quick variations. + // first, least important clamping: keep ret.process_step consistent with typical_physics_steps. + // this smoothes out the process steps and culls small but quick variations. { float min_average_physics_steps, max_average_physics_steps; int consistent_steps = get_average_physics_steps(min_average_physics_steps, max_average_physics_steps); if (consistent_steps > 3) { - ret.clamp_idle(min_average_physics_steps * p_frame_slice, max_average_physics_steps * p_frame_slice); + ret.clamp_process_step(min_average_physics_steps * p_physics_step, max_average_physics_steps * p_physics_step); } } // second clamping: keep abs(time_deficit) < jitter_fix * frame_slise - float max_clock_deviation = get_physics_jitter_fix() * p_frame_slice; - ret.clamp_idle(p_idle_step - max_clock_deviation, p_idle_step + max_clock_deviation); + float max_clock_deviation = get_physics_jitter_fix() * p_physics_step; + ret.clamp_process_step(p_process_step - max_clock_deviation, p_process_step + max_clock_deviation); - // last clamping: make sure time_accum is between 0 and p_frame_slice for consistency between physics and idle - ret.clamp_idle(idle_minus_accum, idle_minus_accum + p_frame_slice); + // last clamping: make sure time_accum is between 0 and p_physics_step for consistency between physics and process + ret.clamp_process_step(process_minus_accum, process_minus_accum + p_physics_step); // restore time_accum - time_accum = ret.idle_step - idle_minus_accum; + time_accum = ret.process_step - process_minus_accum; // track deficit - time_deficit = p_idle_step - ret.idle_step; + time_deficit = p_process_step - ret.process_step; - // p_frame_slice is 1.0 / iterations_per_sec + // p_physics_step is 1.0 / iterations_per_sec // i.e. the time in seconds taken by a physics tick - ret.interpolation_fraction = time_accum / p_frame_slice; + ret.interpolation_fraction = time_accum / p_physics_step; return ret; } // determine wall clock step since last iteration -float MainTimerSync::get_cpu_idle_step() { +float MainTimerSync::get_cpu_process_step() { uint64_t cpu_ticks_elapsed = current_cpu_ticks_usec - last_cpu_ticks_usec; last_cpu_ticks_usec = current_cpu_ticks_usec; @@ -219,9 +219,9 @@ void MainTimerSync::set_fixed_fps(int p_fixed_fps) { fixed_fps = p_fixed_fps; } -// advance one frame, return timesteps to take -MainFrameTime MainTimerSync::advance(float p_frame_slice, int p_iterations_per_second) { - float cpu_idle_step = get_cpu_idle_step(); +// advance one physics frame, return timesteps to take +MainFrameTime MainTimerSync::advance(float p_physics_step, int p_physics_fps) { + float cpu_process_step = get_cpu_process_step(); - return advance_checked(p_frame_slice, p_iterations_per_second, cpu_idle_step); + return advance_checked(p_physics_step, p_physics_fps, cpu_process_step); } diff --git a/main/main_timer_sync.h b/main/main_timer_sync.h index 2126381c7c..884978bf96 100644 --- a/main/main_timer_sync.h +++ b/main/main_timer_sync.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 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 */ @@ -31,14 +31,14 @@ #ifndef MAIN_TIMER_SYNC_H #define MAIN_TIMER_SYNC_H -#include "core/engine.h" +#include "core/config/engine.h" struct MainFrameTime { - float idle_step; // time to advance idles for (argument to process()) + float process_step; // delta time to advance during process() int physics_steps; // number of times to iterate the physics engine float interpolation_fraction; // fraction through the current physics tick - void clamp_idle(float min_idle_step, float max_idle_step); + void clamp_process_step(float min_process_step, float max_process_step); }; class MainTimerSync { @@ -49,7 +49,7 @@ class MainTimerSync { // logical game time since last physics timestep float time_accum = 0; - // current difference between wall clock time and reported sum of idle_steps + // current difference between wall clock time and reported sum of process_steps float time_deficit = 0; // number of frames back for keeping accumulated physics steps roughly constant. @@ -67,7 +67,7 @@ class MainTimerSync { int fixed_fps = 0; protected: - // returns the fraction of p_frame_slice required for the timer to overshoot + // returns the fraction of p_physics_step required for the timer to overshoot // before advance_core considers changing the physics_steps return from // the typical values as defined by typical_physics_steps float get_physics_jitter_fix(); @@ -76,14 +76,14 @@ protected: // return value: number of frames back this data is consistent int get_average_physics_steps(float &p_min, float &p_max); - // advance physics clock by p_idle_step, return appropriate number of steps to simulate - MainFrameTime advance_core(float p_frame_slice, int p_iterations_per_second, float p_idle_step); + // advance physics clock by p_process_step, return appropriate number of steps to simulate + MainFrameTime advance_core(float p_physics_step, int p_physics_fps, float p_process_step); // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero - MainFrameTime advance_checked(float p_frame_slice, int p_iterations_per_second, float p_idle_step); + MainFrameTime advance_checked(float p_physics_step, int p_physics_fps, float p_process_step); // determine wall clock step since last iteration - float get_cpu_idle_step(); + float get_cpu_process_step(); public: MainTimerSync(); @@ -96,7 +96,7 @@ public: void set_fixed_fps(int p_fixed_fps); // advance one frame, return timesteps to take - MainFrameTime advance(float p_frame_slice, int p_iterations_per_second); + MainFrameTime advance(float p_physics_step, int p_physics_fps); }; #endif // MAIN_TIMER_SYNC_H diff --git a/main/performance.cpp b/main/performance.cpp index 7e6b9fca64..a2e53f2ee2 100644 --- a/main/performance.cpp +++ b/main/performance.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 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 */ @@ -30,7 +30,7 @@ #include "performance.h" -#include "core/message_queue.h" +#include "core/object/message_queue.h" #include "core/os/os.h" #include "scene/main/node.h" #include "scene/main/scene_tree.h" @@ -43,6 +43,12 @@ Performance *Performance::singleton = nullptr; void Performance::_bind_methods() { ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor); + ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array())); + ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor); + ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor); + ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor); + ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time); + ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names); BIND_ENUM_CONSTANT(TIME_FPS); BIND_ENUM_CONSTANT(TIME_PROCESS); @@ -87,7 +93,6 @@ float Performance::_get_node_count() const { String Performance::get_monitor_name(Monitor p_monitor) const { ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String()); static const char *names[MONITOR_MAX] = { - "time/fps", "time/process", "time/physics_process", @@ -114,7 +119,7 @@ String Performance::get_monitor_name(Monitor p_monitor) const { "physics_3d/active_objects", "physics_3d/collision_pairs", "physics_3d/islands", - "audio/output_latency", + "audio/driver/output_latency", }; @@ -189,7 +194,6 @@ Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY); // ugly static const MonitorType types[MONITOR_MAX] = { - MONITOR_TYPE_QUANTITY, MONITOR_TYPE_TIME, MONITOR_TYPE_TIME, @@ -231,8 +235,78 @@ void Performance::set_physics_process_time(float p_pt) { _physics_process_time = p_pt; } +void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) { + ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists."); + _monitor_map.insert(p_id, MonitorCall(p_callable, p_args)); + _monitor_modification_time = OS::get_singleton()->get_ticks_usec(); +} + +void Performance::remove_custom_monitor(const StringName &p_id) { + ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exists."); + _monitor_map.erase(p_id); + _monitor_modification_time = OS::get_singleton()->get_ticks_usec(); +} + +bool Performance::has_custom_monitor(const StringName &p_id) { + return _monitor_map.has(p_id); +} + +Variant Performance::get_custom_monitor(const StringName &p_id) { + ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exists."); + bool error; + String error_message; + Variant return_value = _monitor_map[p_id].call(error, error_message); + ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message); + return return_value; +} + +Array Performance::get_custom_monitor_names() { + if (!_monitor_map.size()) { + return Array(); + } + Array return_array; + return_array.resize(_monitor_map.size()); + int index = 0; + for (OrderedHashMap<StringName, MonitorCall>::Element i = _monitor_map.front(); i; i = i.next()) { + return_array.set(index, i.key()); + index++; + } + return return_array; +} + +uint64_t Performance::get_monitor_modification_time() { + return _monitor_modification_time; +} + Performance::Performance() { _process_time = 0; _physics_process_time = 0; + _monitor_modification_time = 0; singleton = this; } + +Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) { + _callable = p_callable; + _arguments = p_arguments; +} + +Performance::MonitorCall::MonitorCall() { +} + +Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) { + Vector<const Variant *> arguments_mem; + arguments_mem.resize(_arguments.size()); + for (int i = 0; i < _arguments.size(); i++) { + arguments_mem.write[i] = &_arguments[i]; + } + const Variant **args = (const Variant **)arguments_mem.ptr(); + int argc = _arguments.size(); + Variant return_value; + Callable::CallError error; + _callable.call(args, argc, return_value, error); + r_error = (error.error != Callable::CallError::CALL_OK); + if (r_error) { + r_error_message = Variant::get_callable_error_text(_callable, args, argc, error); + } + return return_value; +} diff --git a/main/performance.h b/main/performance.h index ddbe45fa00..122e5a4f9a 100644 --- a/main/performance.h +++ b/main/performance.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 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 */ @@ -31,7 +31,8 @@ #ifndef PERFORMANCE_H #define PERFORMANCE_H -#include "core/object.h" +#include "core/object/class_db.h" +#include "core/templates/ordered_hash_map.h" #define PERF_WARN_OFFLINE_FUNCTION #define PERF_WARN_PROCESS_SYNC @@ -47,9 +48,21 @@ class Performance : public Object { float _process_time; float _physics_process_time; + class MonitorCall { + Callable _callable; + Vector<Variant> _arguments; + + public: + MonitorCall(Callable p_callable, Vector<Variant> p_arguments); + MonitorCall(); + Variant call(bool &r_error, String &r_error_message); + }; + + OrderedHashMap<StringName, MonitorCall> _monitor_map; + uint64_t _monitor_modification_time; + public: enum Monitor { - TIME_FPS, TIME_PROCESS, TIME_PHYSICS_PROCESS, @@ -95,6 +108,14 @@ public: void set_process_time(float p_pt); void set_physics_process_time(float p_pt); + void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args); + void remove_custom_monitor(const StringName &p_id); + bool has_custom_monitor(const StringName &p_id); + Variant get_custom_monitor(const StringName &p_id); + Array get_custom_monitor_names(); + + uint64_t get_monitor_modification_time(); + static Performance *get_singleton() { return singleton; } Performance(); diff --git a/main/splash_editor.png b/main/splash_editor.png Binary files differindex 29931a71db..49af9fde22 100644 --- a/main/splash_editor.png +++ b/main/splash_editor.png diff --git a/main/tests/SCsub b/main/tests/SCsub deleted file mode 100644 index cb1d35b12f..0000000000 --- a/main/tests/SCsub +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/python - -Import("env") - -env.tests_sources = [] -env.add_source_files(env.tests_sources, "*.cpp") - -lib = env.add_library("tests", env.tests_sources) -env.Prepend(LIBS=[lib]) diff --git a/main/tests/test_astar.cpp b/main/tests/test_astar.cpp deleted file mode 100644 index fe335589b0..0000000000 --- a/main/tests/test_astar.cpp +++ /dev/null @@ -1,409 +0,0 @@ -/*************************************************************************/ -/* test_astar.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_astar.h" - -#include "core/math/a_star.h" -#include "core/math/math_funcs.h" -#include "core/os/os.h" - -#include <math.h> -#include <stdio.h> - -namespace TestAStar { - -class ABCX : public AStar { -public: - enum { A, - B, - C, - X }; - - ABCX() { - add_point(A, Vector3(0, 0, 0)); - add_point(B, Vector3(1, 0, 0)); - add_point(C, Vector3(0, 1, 0)); - add_point(X, Vector3(0, 0, 1)); - connect_points(A, B); - connect_points(A, C); - connect_points(B, C); - connect_points(X, A); - } - - // Disable heuristic completely - float _compute_cost(int p_from, int p_to) { - if (p_from == A && p_to == C) { - return 1000; - } - return 100; - } -}; - -bool test_abc() { - ABCX abcx; - Vector<int> path = abcx.get_id_path(ABCX::A, ABCX::C); - bool ok = path.size() == 3; - int i = 0; - ok = ok && path[i++] == ABCX::A; - ok = ok && path[i++] == ABCX::B; - ok = ok && path[i++] == ABCX::C; - return ok; -} - -bool test_abcx() { - ABCX abcx; - Vector<int> path = abcx.get_id_path(ABCX::X, ABCX::C); - bool ok = path.size() == 4; - int i = 0; - ok = ok && path[i++] == ABCX::X; - ok = ok && path[i++] == ABCX::A; - ok = ok && path[i++] == ABCX::B; - ok = ok && path[i++] == ABCX::C; - return ok; -} - -bool test_add_remove() { - AStar a; - bool ok = true; - - // Manual tests - a.add_point(1, Vector3(0, 0, 0)); - a.add_point(2, Vector3(0, 1, 0)); - a.add_point(3, Vector3(1, 1, 0)); - a.add_point(4, Vector3(2, 0, 0)); - a.connect_points(1, 2, true); - a.connect_points(1, 3, true); - a.connect_points(1, 4, false); - - ok = ok && (a.are_points_connected(2, 1) == true); - ok = ok && (a.are_points_connected(4, 1) == true); - ok = ok && (a.are_points_connected(2, 1, false) == true); - ok = ok && (a.are_points_connected(4, 1, false) == false); - - a.disconnect_points(1, 2, true); - ok = ok && (a.get_point_connections(1).size() == 2); // 3, 4 - ok = ok && (a.get_point_connections(2).size() == 0); - - a.disconnect_points(4, 1, false); - ok = ok && (a.get_point_connections(1).size() == 2); // 3, 4 - ok = ok && (a.get_point_connections(4).size() == 0); - - a.disconnect_points(4, 1, true); - ok = ok && (a.get_point_connections(1).size() == 1); // 3 - ok = ok && (a.get_point_connections(4).size() == 0); - - a.connect_points(2, 3, false); - ok = ok && (a.get_point_connections(2).size() == 1); // 3 - ok = ok && (a.get_point_connections(3).size() == 1); // 1 - - a.connect_points(2, 3, true); - ok = ok && (a.get_point_connections(2).size() == 1); // 3 - ok = ok && (a.get_point_connections(3).size() == 2); // 1, 2 - - a.disconnect_points(2, 3, false); - ok = ok && (a.get_point_connections(2).size() == 0); - ok = ok && (a.get_point_connections(3).size() == 2); // 1, 2 - - a.connect_points(4, 3, true); - ok = ok && (a.get_point_connections(3).size() == 3); // 1, 2, 4 - ok = ok && (a.get_point_connections(4).size() == 1); // 3 - - a.disconnect_points(3, 4, false); - ok = ok && (a.get_point_connections(3).size() == 2); // 1, 2 - ok = ok && (a.get_point_connections(4).size() == 1); // 3 - - a.remove_point(3); - ok = ok && (a.get_point_connections(1).size() == 0); - ok = ok && (a.get_point_connections(2).size() == 0); - ok = ok && (a.get_point_connections(4).size() == 0); - - a.add_point(0, Vector3(0, -1, 0)); - a.add_point(3, Vector3(2, 1, 0)); - // 0: (0, -1) - // 1: (0, 0) - // 2: (0, 1) - // 3: (2, 1) - // 4: (2, 0) - - // Tests for get_closest_position_in_segment - a.connect_points(2, 3); - ok = ok && (a.get_closest_position_in_segment(Vector3(0.5, 0.5, 0)) == Vector3(0.5, 1, 0)); - - a.connect_points(3, 4); - a.connect_points(0, 3); - a.connect_points(1, 4); - a.disconnect_points(1, 4, false); - a.disconnect_points(4, 3, false); - a.disconnect_points(3, 4, false); - // Remaining edges: <2, 3>, <0, 3>, <1, 4> (directed) - ok = ok && (a.get_closest_position_in_segment(Vector3(2, 0.5, 0)) == Vector3(1.75, 0.75, 0)); - ok = ok && (a.get_closest_position_in_segment(Vector3(-1, 0.2, 0)) == Vector3(0, 0, 0)); - ok = ok && (a.get_closest_position_in_segment(Vector3(3, 2, 0)) == Vector3(2, 1, 0)); - - Math::seed(0); - - // Random tests for connectivity checks - for (int i = 0; i < 20000; i++) { - int u = Math::rand() % 5; - int v = Math::rand() % 4; - if (u == v) { - v = 4; - } - if (Math::rand() % 2 == 1) { - // Add a (possibly existing) directed edge and confirm connectivity - a.connect_points(u, v, false); - ok = ok && (a.are_points_connected(u, v, false) == true); - } else { - // Remove a (possibly nonexistent) directed edge and confirm disconnectivity - a.disconnect_points(u, v, false); - ok = ok && (a.are_points_connected(u, v, false) == false); - } - } - - // Random tests for point removal - for (int i = 0; i < 20000; i++) { - a.clear(); - for (int j = 0; j < 5; j++) { - a.add_point(j, Vector3(0, 0, 0)); - } - - // Add or remove random edges - for (int j = 0; j < 10; j++) { - int u = Math::rand() % 5; - int v = Math::rand() % 4; - if (u == v) { - v = 4; - } - if (Math::rand() % 2 == 1) { - a.connect_points(u, v, false); - } else { - a.disconnect_points(u, v, false); - } - } - - // Remove point 0 - a.remove_point(0); - // White box: this will check all edges remaining in the segments set - for (int j = 1; j < 5; j++) { - ok = ok && (a.are_points_connected(0, j, true) == false); - } - } - - // It's been great work, cheers \(^ ^)/ - return ok; -} - -bool test_solutions() { - // Random stress tests with Floyd-Warshall - - const int N = 30; - Math::seed(0); - - for (int test = 0; test < 1000; test++) { - AStar a; - Vector3 p[N]; - bool adj[N][N] = { { false } }; - - // Assign initial coordinates - for (int u = 0; u < N; u++) { - p[u].x = Math::rand() % 100; - p[u].y = Math::rand() % 100; - p[u].z = Math::rand() % 100; - a.add_point(u, p[u]); - } - - // Generate a random sequence of operations - for (int i = 0; i < 1000; i++) { - // Pick two different vertices - int u, v; - u = Math::rand() % N; - v = Math::rand() % (N - 1); - if (u == v) { - v = N - 1; - } - - // Pick a random operation - int op = Math::rand(); - switch (op % 9) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - // Add edge (u, v); possibly bidirectional - a.connect_points(u, v, op % 2); - adj[u][v] = true; - if (op % 2) { - adj[v][u] = true; - } - break; - case 6: - case 7: - // Remove edge (u, v); possibly bidirectional - a.disconnect_points(u, v, op % 2); - adj[u][v] = false; - if (op % 2) { - adj[v][u] = false; - } - break; - case 8: - // Remove point u and add it back; clears adjacent edges and changes coordinates - a.remove_point(u); - p[u].x = Math::rand() % 100; - p[u].y = Math::rand() % 100; - p[u].z = Math::rand() % 100; - a.add_point(u, p[u]); - for (v = 0; v < N; v++) { - adj[u][v] = adj[v][u] = false; - } - break; - } - } - - // Floyd-Warshall - float d[N][N]; - for (int u = 0; u < N; u++) { - for (int v = 0; v < N; v++) { - d[u][v] = (u == v || adj[u][v]) ? p[u].distance_to(p[v]) : INFINITY; - } - } - - for (int w = 0; w < N; w++) { - for (int u = 0; u < N; u++) { - for (int v = 0; v < N; v++) { - if (d[u][v] > d[u][w] + d[w][v]) { - d[u][v] = d[u][w] + d[w][v]; - } - } - } - } - - // Display statistics - int count = 0; - for (int u = 0; u < N; u++) { - for (int v = 0; v < N; v++) { - if (adj[u][v]) { - count++; - } - } - } - printf("Test #%4d: %3d edges, ", test + 1, count); - count = 0; - for (int u = 0; u < N; u++) { - for (int v = 0; v < N; v++) { - if (!Math::is_inf(d[u][v])) { - count++; - } - } - } - printf("%3d/%d pairs of reachable points\n", count - N, N * (N - 1)); - - // Check A*'s output - bool match = true; - for (int u = 0; u < N; u++) { - for (int v = 0; v < N; v++) { - if (u != v) { - Vector<int> route = a.get_id_path(u, v); - if (!Math::is_inf(d[u][v])) { - // Reachable - if (route.size() == 0) { - printf("From %d to %d: A* did not find a path\n", u, v); - match = false; - goto exit; - } - float astar_dist = 0; - for (int i = 1; i < route.size(); i++) { - if (!adj[route[i - 1]][route[i]]) { - printf("From %d to %d: edge (%d, %d) does not exist\n", - u, v, route[i - 1], route[i]); - match = false; - goto exit; - } - astar_dist += p[route[i - 1]].distance_to(p[route[i]]); - } - if (!Math::is_equal_approx(astar_dist, d[u][v])) { - printf("From %d to %d: Floyd-Warshall gives %.6f, A* gives %.6f\n", - u, v, d[u][v], astar_dist); - match = false; - goto exit; - } - } else { - // Unreachable - if (route.size() > 0) { - printf("From %d to %d: A* somehow found a nonexistent path\n", u, v); - match = false; - goto exit; - } - } - } - } - } - - exit: - if (!match) { - return false; - } - } - return true; -} - -typedef bool (*TestFunc)(); - -TestFunc test_funcs[] = { - test_abc, - test_abcx, - test_add_remove, - test_solutions, - nullptr -}; - -MainLoop *test() { - int count = 0; - int passed = 0; - - while (true) { - if (!test_funcs[count]) { - break; - } - bool pass = test_funcs[count](); - if (pass) { - passed++; - } - OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED"); - - count++; - } - OS::get_singleton()->print("\n"); - OS::get_singleton()->print("Passed %i of %i tests\n", passed, count); - return nullptr; -} - -} // namespace TestAStar diff --git a/main/tests/test_astar.h b/main/tests/test_astar.h deleted file mode 100644 index 0992812c18..0000000000 --- a/main/tests/test_astar.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_astar.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_ASTAR_H -#define TEST_ASTAR_H - -#include "core/os/main_loop.h" - -namespace TestAStar { - -MainLoop *test(); -} - -#endif diff --git a/main/tests/test_class_db.cpp b/main/tests/test_class_db.cpp deleted file mode 100644 index 4c00a09e9c..0000000000 --- a/main/tests/test_class_db.cpp +++ /dev/null @@ -1,882 +0,0 @@ -/*************************************************************************/ -/* test_class_db.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_class_db.h" - -#include "core/global_constants.h" -#include "core/ordered_hash_map.h" -#include "core/os/os.h" -#include "core/string_name.h" -#include "core/ustring.h" -#include "core/variant.h" - -namespace TestClassDB { - -enum class [[nodiscard]] TestResult{ - FAILED, - PASS -}; - -#define TEST_FAIL_COND_FATAL(m_cond, m_msg) \ - if (unlikely(m_cond)) { \ - ERR_PRINT(m_msg); \ - return TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_FAIL_COND(m_cond, m_msg) \ - if (unlikely(m_cond)) { \ - ERR_PRINT(m_msg); \ - __test_result__ = TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_CHECK_FATAL(m_test_expr) \ - if (unlikely((m_test_expr) == TestResult::FAILED)) { \ - return TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_CHECK(m_test_expr) \ - if (unlikely((m_test_expr) == TestResult::FAILED)) { \ - __test_result__ = TestResult::FAILED; \ - } else \ - ((void)0) - -#define TEST_START() \ - TestResult __test_result__ = TestResult::PASS; \ - ((void)0) - -#define TEST_END() return __test_result__; - -struct TypeReference { - StringName name; - bool is_enum = false; -}; - -struct ConstantData { - String name; - int value = 0; -}; - -struct EnumData { - StringName name; - List<ConstantData> constants; - - _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const { - return p_enum.name == name; - } -}; - -struct PropertyData { - StringName name; - int index = 0; - - StringName getter; - StringName setter; -}; - -struct ArgumentData { - TypeReference type; - String name; - bool has_defval = false; - Variant defval; -}; - -struct MethodData { - StringName name; - TypeReference return_type; - List<ArgumentData> arguments; - bool is_virtual = false; - bool is_vararg = false; -}; - -struct SignalData { - StringName name; - List<ArgumentData> arguments; -}; - -struct ExposedClass { - StringName name; - StringName base; - - bool is_singleton = false; - bool is_instantiable = false; - bool is_reference = false; - - ClassDB::APIType api_type; - - List<ConstantData> constants; - List<EnumData> enums; - List<PropertyData> properties; - List<MethodData> methods; - List<SignalData> signals_; - - const PropertyData *find_property_by_name(const StringName &p_name) const { - for (const List<PropertyData>::Element *E = properties.front(); E; E = E->next()) { - if (E->get().name == p_name) { - return &E->get(); - } - } - - return nullptr; - } - - const MethodData *find_method_by_name(const StringName &p_name) const { - for (const List<MethodData>::Element *E = methods.front(); E; E = E->next()) { - if (E->get().name == p_name) { - return &E->get(); - } - } - - return nullptr; - } -}; - -struct NamesCache { - StringName variant_type = StaticCString::create("Variant"); - StringName object_class = StaticCString::create("Object"); - StringName reference_class = StaticCString::create("Reference"); - StringName string_type = StaticCString::create("String"); - StringName string_name_type = StaticCString::create("StringName"); - StringName node_path_type = StaticCString::create("NodePath"); - StringName bool_type = StaticCString::create("bool"); - StringName int_type = StaticCString::create("int"); - StringName float_type = StaticCString::create("float"); - StringName void_type = StaticCString::create("void"); - StringName vararg_stub_type = StaticCString::create("@VarArg@"); - StringName vector2_type = StaticCString::create("Vector2"); - StringName rect2_type = StaticCString::create("Rect2"); - StringName vector3_type = StaticCString::create("Vector3"); - - // Object not included as it must be checked for all derived classes - static constexpr int nullable_types_count = 17; - StringName nullable_types[nullable_types_count] = { - string_type, - string_name_type, - node_path_type, - - StaticCString::create(_STR(Array)), - StaticCString::create(_STR(Dictionary)), - StaticCString::create(_STR(Callable)), - StaticCString::create(_STR(Signal)), - - StaticCString::create(_STR(PackedByteArray)), - StaticCString::create(_STR(PackedInt32Array)), - StaticCString::create(_STR(PackedInt64rray)), - StaticCString::create(_STR(PackedFloat32Array)), - StaticCString::create(_STR(PackedFloat64Array)), - StaticCString::create(_STR(PackedStringArray)), - StaticCString::create(_STR(PackedVector2Array)), - StaticCString::create(_STR(PackedVector3Array)), - StaticCString::create(_STR(PackedColorArray)), - }; - - bool is_nullable_type(const StringName &p_type) const { - for (int i = 0; i < nullable_types_count; i++) { - if (p_type == nullable_types[i]) { - return true; - } - } - - return false; - } -}; - -typedef OrderedHashMap<StringName, ExposedClass> ExposedClasses; - -struct Context { - Vector<StringName> enum_types; - Vector<StringName> builtin_types; - ExposedClasses exposed_classes; - List<EnumData> global_enums; - NamesCache names_cache; - - const ExposedClass *find_exposed_class(const StringName &p_name) const { - ExposedClasses::ConstElement elem = exposed_classes.find(p_name); - return elem ? &elem.value() : nullptr; - } - - const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const { - ExposedClasses::ConstElement elem = exposed_classes.find(p_type_ref.name); - return elem ? &elem.value() : nullptr; - } - - bool has_type(const Context &p_context, const TypeReference &p_type_ref) const { - if (p_context.builtin_types.find(p_type_ref.name) >= 0) { - return true; - } - - if (p_type_ref.is_enum) { - if (p_context.enum_types.find(p_type_ref.name) >= 0) { - return true; - } - - // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead. - return p_context.builtin_types.find(p_context.names_cache.int_type); - } - - return false; - } -}; - -bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type) { - if (p_arg_type.name == p_context.names_cache.variant_type) { - // Variant can take anything - return true; - } - - switch (p_val.get_type()) { - case Variant::NIL: - return p_context.find_exposed_class(p_arg_type) || - p_context.names_cache.is_nullable_type(p_arg_type.name); - case Variant::BOOL: - return p_arg_type.name == p_context.names_cache.bool_type; - case Variant::INT: - return p_arg_type.name == p_context.names_cache.int_type || - p_arg_type.name == p_context.names_cache.float_type || - p_arg_type.is_enum; - case Variant::FLOAT: - return p_arg_type.name == p_context.names_cache.float_type; - case Variant::STRING: - case Variant::STRING_NAME: - return p_arg_type.name == p_context.names_cache.string_type || - p_arg_type.name == p_context.names_cache.string_name_type || - p_arg_type.name == p_context.names_cache.node_path_type; - case Variant::NODE_PATH: - return p_arg_type.name == p_context.names_cache.node_path_type; - case Variant::TRANSFORM: - case Variant::TRANSFORM2D: - case Variant::BASIS: - case Variant::QUAT: - case Variant::PLANE: - case Variant::AABB: - case Variant::COLOR: - case Variant::VECTOR2: - case Variant::RECT2: - case Variant::VECTOR3: - case Variant::_RID: - case Variant::ARRAY: - case Variant::DICTIONARY: - case Variant::PACKED_BYTE_ARRAY: - case Variant::PACKED_INT32_ARRAY: - case Variant::PACKED_INT64_ARRAY: - case Variant::PACKED_FLOAT32_ARRAY: - case Variant::PACKED_FLOAT64_ARRAY: - case Variant::PACKED_STRING_ARRAY: - case Variant::PACKED_VECTOR2_ARRAY: - case Variant::PACKED_VECTOR3_ARRAY: - case Variant::PACKED_COLOR_ARRAY: - case Variant::CALLABLE: - case Variant::SIGNAL: - return p_arg_type.name == Variant::get_type_name(p_val.get_type()); - case Variant::OBJECT: - return p_context.find_exposed_class(p_arg_type); - case Variant::VECTOR2I: - return p_arg_type.name == p_context.names_cache.vector2_type || - p_arg_type.name == Variant::get_type_name(p_val.get_type()); - case Variant::RECT2I: - return p_arg_type.name == p_context.names_cache.rect2_type || - p_arg_type.name == Variant::get_type_name(p_val.get_type()); - case Variant::VECTOR3I: - return p_arg_type.name == p_context.names_cache.vector3_type || - p_arg_type.name == Variant::get_type_name(p_val.get_type()); - default: - ERR_PRINT("Unexpected Variant type: " + itos(p_val.get_type())); - break; - } - - return false; -} - -TestResult validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) { - TEST_START(); - - const MethodData *setter = p_class.find_method_by_name(p_prop.setter); - - // Search it in base classes too - const ExposedClass *top = &p_class; - while (!setter && top->base != StringName()) { - top = p_context.find_exposed_class(top->base); - TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'."); - setter = top->find_method_by_name(p_prop.setter); - } - - const MethodData *getter = p_class.find_method_by_name(p_prop.getter); - - // Search it in base classes too - top = &p_class; - while (!getter && top->base != StringName()) { - top = p_context.find_exposed_class(top->base); - TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'."); - getter = top->find_method_by_name(p_prop.getter); - } - - TEST_FAIL_COND(!setter && !getter, - "Couldn't find neither the setter nor the getter for property: '" + p_class.name + "." + String(p_prop.name) + "'."); - - if (setter) { - int setter_argc = p_prop.index != -1 ? 2 : 1; - TEST_FAIL_COND(setter->arguments.size() != setter_argc, - "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - - if (getter) { - int getter_argc = p_prop.index != -1 ? 1 : 0; - TEST_FAIL_COND(getter->arguments.size() != getter_argc, - "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - - if (getter && setter) { - const ArgumentData &setter_first_arg = setter->arguments.back()->get(); - if (getter->return_type.name != setter_first_arg.type.name) { - // Special case for Node::set_name - bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type && - setter_first_arg.type.name == p_context.names_cache.string_type; - - TEST_FAIL_COND(!whitelisted, - "Return type from getter doesn't match first argument of setter, for property: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - } - - const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type; - - const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref); - if (prop_class) { - TEST_FAIL_COND(prop_class->is_singleton, - "Property type is a singleton: '" + p_class.name + "." + String(p_prop.name) + "'."); - } else { - TEST_FAIL_COND(!p_context.has_type(p_context, prop_type_ref), - "Property type '" + prop_type_ref.name + "' not found: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - - if (getter) { - if (p_prop.index != -1) { - const ArgumentData &idx_arg = getter->arguments.front()->get(); - if (idx_arg.type.name != p_context.names_cache.int_type) { - // If not an int, it can be an enum - TEST_FAIL_COND(p_context.enum_types.find(idx_arg.type.name) < 0, - "Invalid type '" + idx_arg.type.name + "' for index argument of property getter: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - } - } - - if (setter) { - if (p_prop.index != -1) { - const ArgumentData &idx_arg = setter->arguments.front()->get(); - if (idx_arg.type.name != p_context.names_cache.int_type) { - // Assume the index parameter is an enum - // If not an int, it can be an enum - TEST_FAIL_COND(p_context.enum_types.find(idx_arg.type.name) < 0, - "Invalid type '" + idx_arg.type.name + "' for index argument of property setter: '" + p_class.name + "." + String(p_prop.name) + "'."); - } - } - } - - TEST_END(); -} - -TestResult validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) { - TEST_START(); - - const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type); - if (return_class) { - TEST_FAIL_COND(return_class->is_singleton, - "Method return type is a singleton: '" + p_class.name + "." + p_method.name + "'."); - } - - for (const List<ArgumentData>::Element *F = p_method.arguments.front(); F; F = F->next()) { - const ArgumentData &arg = F->get(); - - const ExposedClass *arg_class = p_context.find_exposed_class(arg.type); - if (arg_class) { - TEST_FAIL_COND(arg_class->is_singleton, - "Argument type is a singleton: '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'."); - } else { - TEST_FAIL_COND(!p_context.has_type(p_context, arg.type), - "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of method" + p_class.name + "." + p_method.name + "'."); - } - - if (arg.has_defval) { - TEST_FAIL_COND(!arg_default_value_is_assignable_to_type(p_context, arg.defval, arg.type), - "Invalid default value for parameter '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'."); - } - } - - TEST_END(); -} - -TestResult validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) { - TEST_START(); - - for (const List<ArgumentData>::Element *F = p_signal.arguments.front(); F; F = F->next()) { - const ArgumentData &arg = F->get(); - - const ExposedClass *arg_class = p_context.find_exposed_class(arg.type); - if (arg_class) { - TEST_FAIL_COND(arg_class->is_singleton, - "Argument class is a singleton: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'."); - } else { - TEST_FAIL_COND(!p_context.has_type(p_context, arg.type), - "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'."); - } - } - - TEST_END(); -} - -TestResult validate_class(const Context &p_context, const ExposedClass &p_exposed_class) { - TEST_START(); - - bool is_derived_type = p_exposed_class.base != StringName(); - - if (!is_derived_type) { - // Asserts about the base Object class - TEST_FAIL_COND_FATAL(p_exposed_class.name != p_context.names_cache.object_class, - "Class '" + p_exposed_class.name + "' has no base class."); - TEST_FAIL_COND_FATAL(!p_exposed_class.is_instantiable, - "Object class is not instantiable."); - TEST_FAIL_COND_FATAL(p_exposed_class.api_type != ClassDB::API_CORE, - "Object class is API is not API_CORE."); - TEST_FAIL_COND_FATAL(p_exposed_class.is_singleton, - "Object class is registered as a singleton."); - } - - CRASH_COND_MSG(p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class, - "Singleton base class '" + String(p_exposed_class.base) + "' is not Object, for class '" + p_exposed_class.name + "'."); - - CRASH_COND_MSG(is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base), - "Base type '" + p_exposed_class.base.operator String() + "' does not exist, for class '" + p_exposed_class.name + "'."); - - for (const List<PropertyData>::Element *F = p_exposed_class.properties.front(); F; F = F->next()) { - TEST_CHECK(validate_property(p_context, p_exposed_class, F->get())); - } - - for (const List<MethodData>::Element *F = p_exposed_class.methods.front(); F; F = F->next()) { - TEST_CHECK(validate_method(p_context, p_exposed_class, F->get())); - } - - for (const List<SignalData>::Element *F = p_exposed_class.signals_.front(); F; F = F->next()) { - TEST_CHECK(validate_signal(p_context, p_exposed_class, F->get())); - } - - TEST_END(); -} - -TestResult add_exposed_classes(Context &r_context) { - TEST_START(); - - List<StringName> class_list; - ClassDB::get_class_list(&class_list); - class_list.sort_custom<StringName::AlphCompare>(); - - while (class_list.size()) { - StringName class_name = class_list.front()->get(); - - ClassDB::APIType api_type = ClassDB::get_api_type(class_name); - - if (api_type == ClassDB::API_NONE) { - class_list.pop_front(); - continue; - } - - if (!ClassDB::is_class_exposed(class_name)) { - OS::get_singleton()->print("Ignoring class '%s' because it's not exposed\n", String(class_name).utf8().get_data()); - class_list.pop_front(); - continue; - } - - if (!ClassDB::is_class_enabled(class_name)) { - OS::get_singleton()->print("Ignoring class '%s' because it's not enabled\n", String(class_name).utf8().get_data()); - class_list.pop_front(); - continue; - } - - ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name); - - ExposedClass exposed_class; - exposed_class.name = class_name; - exposed_class.api_type = api_type; - exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name); - exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton; - exposed_class.is_reference = ClassDB::is_parent_class(class_name, "Reference"); - exposed_class.base = ClassDB::get_parent_class(class_name); - - // Add properties - - List<PropertyInfo> property_list; - ClassDB::get_property_list(class_name, &property_list, true); - - Map<StringName, StringName> accessor_methods; - - for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) { - const PropertyInfo &property = E->get(); - - if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY) { - continue; - } - - PropertyData prop; - prop.name = property.name; - prop.setter = ClassDB::get_property_setter(class_name, prop.name); - prop.getter = ClassDB::get_property_getter(class_name, prop.name); - - if (prop.setter != StringName()) { - accessor_methods[prop.setter] = prop.name; - } - if (prop.getter != StringName()) { - accessor_methods[prop.getter] = prop.name; - } - - bool valid = false; - prop.index = ClassDB::get_property_index(class_name, prop.name, &valid); - TEST_FAIL_COND(!valid, "Invalid property: '" + exposed_class.name + "." + String(prop.name) + "'."); - - exposed_class.properties.push_back(prop); - } - - // Add methods - - List<MethodInfo> virtual_method_list; - ClassDB::get_virtual_methods(class_name, &virtual_method_list, true); - - List<MethodInfo> method_list; - ClassDB::get_method_list(class_name, &method_list, true); - method_list.sort(); - - for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) { - const MethodInfo &method_info = E->get(); - - int argc = method_info.arguments.size(); - - if (method_info.name.empty()) { - continue; - } - - MethodData method; - method.name = method_info.name; - - if (method_info.flags & METHOD_FLAG_VIRTUAL) { - method.is_virtual = true; - } - - PropertyInfo return_info = method_info.return_val; - - MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name); - - method.is_vararg = m && m->is_vararg(); - - if (!m && !method.is_virtual) { - TEST_FAIL_COND(!virtual_method_list.find(method_info), - "Missing MethodBind for non-virtual method: '" + exposed_class.name + "." + method.name + "'."); - - // A virtual method without the virtual flag. This is a special case. - - // The method Object.free is registered as a virtual method, but without the virtual flag. - // This is because this method is not supposed to be overridden, but called. - // We assume the return type is void. - method.return_type.name = r_context.names_cache.void_type; - - // Actually, more methods like this may be added in the future, which could return - // something different. Let's put this check to notify us if that ever happens. - if (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free") { - WARN_PRINT("Notification: New unexpected virtual non-overridable method found." - " We only expected Object.free, but found '" + - exposed_class.name + "." + method.name + "'."); - } - } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - method.return_type.name = return_info.class_name; - method.return_type.is_enum = true; - } else if (return_info.class_name != StringName()) { - method.return_type.name = return_info.class_name; - - bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE && - ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.reference_class); - TEST_FAIL_COND(bad_reference_hint, String() + "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'." + - " Are you returning a reference type by pointer? Method: '" + - exposed_class.name + "." + method.name + "'."); - } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { - method.return_type.name = return_info.hint_string; - } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { - method.return_type.name = r_context.names_cache.variant_type; - } else if (return_info.type == Variant::NIL) { - method.return_type.name = r_context.names_cache.void_type; - } else { - // NOTE: We don't care about the size and sign of int and float in these tests - method.return_type.name = Variant::get_type_name(return_info.type); - } - - for (int i = 0; i < argc; i++) { - PropertyInfo arg_info = method_info.arguments[i]; - - String orig_arg_name = arg_info.name; - - ArgumentData arg; - arg.name = orig_arg_name; - - if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - arg.type.name = arg_info.class_name; - arg.type.is_enum = true; - } else if (arg_info.class_name != StringName()) { - arg.type.name = arg_info.class_name; - } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { - arg.type.name = arg_info.hint_string; - } else if (arg_info.type == Variant::NIL) { - arg.type.name = r_context.names_cache.variant_type; - } else { - // NOTE: We don't care about the size and sign of int and float in these tests - arg.type.name = Variant::get_type_name(arg_info.type); - } - - if (m && m->has_default_argument(i)) { - arg.has_defval = true; - arg.defval = m->get_default_argument(i); - } - - method.arguments.push_back(arg); - } - - if (method.is_vararg) { - ArgumentData vararg; - vararg.type.name = r_context.names_cache.vararg_stub_type; - vararg.name = "@varargs@"; - method.arguments.push_back(vararg); - } - - TEST_FAIL_COND(exposed_class.find_property_by_name(method.name), - "Method name conflicts with property: '" + String(class_name) + "." + String(method.name) + "'."); - - // Classes starting with an underscore are ignored unless they're used as a property setter or getter - if (!method.is_virtual && String(method.name)[0] == '_') { - for (const List<PropertyData>::Element *F = exposed_class.properties.front(); F; F = F->next()) { - const PropertyData &prop = F->get(); - - if (prop.setter == method.name || prop.getter == method.name) { - exposed_class.methods.push_back(method); - break; - } - } - } else { - exposed_class.methods.push_back(method); - } - } - - // Add signals - - const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map; - const StringName *k = nullptr; - - while ((k = signal_map.next(k))) { - SignalData signal; - - const MethodInfo &method_info = signal_map.get(*k); - - signal.name = method_info.name; - - int argc = method_info.arguments.size(); - - for (int i = 0; i < argc; i++) { - PropertyInfo arg_info = method_info.arguments[i]; - - String orig_arg_name = arg_info.name; - - ArgumentData arg; - arg.name = orig_arg_name; - - if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - arg.type.name = arg_info.class_name; - arg.type.is_enum = true; - } else if (arg_info.class_name != StringName()) { - arg.type.name = arg_info.class_name; - } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { - arg.type.name = arg_info.hint_string; - } else if (arg_info.type == Variant::NIL) { - arg.type.name = r_context.names_cache.variant_type; - } else { - // NOTE: We don't care about the size and sign of int and float in these tests - arg.type.name = Variant::get_type_name(arg_info.type); - } - - signal.arguments.push_back(arg); - } - - bool method_conflict = exposed_class.find_property_by_name(signal.name); - - if (method_conflict || exposed_class.find_method_by_name(signal.name)) { - // TODO: - // ClassDB allows signal names that conflict with method or property names. - // However registering a signal with a conflicting name is still considered wrong. - // Unfortunately there are some existing cases that are yet to be fixed. - // Until those are fixed we will print a warning instead of failing the test. - WARN_PRINT("Signal name conflicts with " + String(method_conflict ? "method" : "property") + - ": '" + String(class_name) + "." + String(signal.name) + "'."); - } - - exposed_class.signals_.push_back(signal); - } - - // Add enums and constants - - List<String> constants; - ClassDB::get_integer_constant_list(class_name, &constants, true); - - const HashMap<StringName, List<StringName>> &enum_map = class_info->enum_map; - k = nullptr; - - while ((k = enum_map.next(k))) { - EnumData enum_; - enum_.name = *k; - - const List<StringName> &enum_constants = enum_map.get(*k); - for (const List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) { - const StringName &constant_name = E->get(); - int *value = class_info->constant_map.getptr(constant_name); - TEST_FAIL_COND(!value, "Missing enum constant value: '" + - String(class_name) + "." + String(enum_.name) + "." + String(constant_name) + "'."); - constants.erase(constant_name); - - ConstantData constant; - constant.name = constant_name; - constant.value = *value; - - enum_.constants.push_back(constant); - } - - exposed_class.enums.push_back(enum_); - - r_context.enum_types.push_back(String(class_name) + "." + String(*k)); - } - - for (const List<String>::Element *E = constants.front(); E; E = E->next()) { - const String &constant_name = E->get(); - int *value = class_info->constant_map.getptr(StringName(E->get())); - TEST_FAIL_COND(!value, "Missing enum constant value: '" + String(class_name) + "." + String(constant_name) + "'."); - - ConstantData constant; - constant.name = constant_name; - constant.value = *value; - - exposed_class.constants.push_back(constant); - } - - r_context.exposed_classes.insert(class_name, exposed_class); - class_list.pop_front(); - } - - TEST_END(); -} - -void add_builtin_types(Context &r_context) { - // NOTE: We don't care about the size and sign of int and float in these tests - for (int i = 0; i < Variant::VARIANT_MAX; i++) { - r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i))); - } - - r_context.builtin_types.push_back(_STR(Variant)); - r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type); - r_context.builtin_types.push_back("void"); -} - -void add_global_enums(Context &r_context) { - int global_constants_count = GlobalConstants::get_global_constant_count(); - - if (global_constants_count > 0) { - for (int i = 0; i < global_constants_count; i++) { - StringName enum_name = GlobalConstants::get_global_constant_enum(i); - - if (enum_name != StringName()) { - ConstantData constant; - constant.name = GlobalConstants::get_global_constant_name(i); - constant.value = GlobalConstants::get_global_constant_value(i); - - EnumData enum_; - enum_.name = enum_name; - List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_); - if (enum_match) { - enum_match->get().constants.push_back(constant); - } else { - enum_.constants.push_back(constant); - r_context.global_enums.push_back(enum_); - } - } - } - - for (List<EnumData>::Element *E = r_context.global_enums.front(); E; E = E->next()) { - r_context.enum_types.push_back(E->get().name); - } - } - - // HARDCODED - List<StringName> hardcoded_enums; - hardcoded_enums.push_back("Vector2.Axis"); - hardcoded_enums.push_back("Vector2i.Axis"); - hardcoded_enums.push_back("Vector3.Axis"); - hardcoded_enums.push_back("Vector3i.Axis"); - for (List<StringName>::Element *E = hardcoded_enums.front(); E; E = E->next()) { - // These enums are not generated and must be written manually (e.g.: Vector3.Axis) - // Here, we assume core types do not begin with underscore - r_context.enum_types.push_back(E->get()); - } -} - -TestResult run_class_db_tests() { - TEST_START(); - - Context context; - - TEST_CHECK_FATAL(add_exposed_classes(context)); - add_builtin_types(context); - add_global_enums(context); - - const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class); - TEST_FAIL_COND_FATAL(!object_class, "Object class not found."); - TEST_FAIL_COND_FATAL(object_class->base != StringName(), - "Object class derives from another class: '" + object_class->base + "'."); - - for (ExposedClasses::Element E = context.exposed_classes.front(); E; E = E.next()) { - TEST_CHECK(validate_class(context, E.value())); - } - - TEST_END(); -} - -MainLoop *test() { - TestResult pass = run_class_db_tests(); - - OS::get_singleton()->print("ClassDB tests: %s\n", pass == TestResult::PASS ? "PASS" : "FAILED"); - - if (pass == TestResult::FAILED) { - OS::get_singleton()->set_exit_code(pass == TestResult::PASS ? 0 : 1); - } - - return nullptr; -} - -} // namespace TestClassDB diff --git a/main/tests/test_class_db.h b/main/tests/test_class_db.h deleted file mode 100644 index 1a31cfb01b..0000000000 --- a/main/tests/test_class_db.h +++ /dev/null @@ -1,42 +0,0 @@ -/*************************************************************************/ -/* test_class_db.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef GODOT_TEST_CLASS_DB_H -#define GODOT_TEST_CLASS_DB_H - -#include "core/os/main_loop.h" - -namespace TestClassDB { - -MainLoop *test(); - -} - -#endif //GODOT_TEST_CLASS_DB_H diff --git a/main/tests/test_gdscript.cpp b/main/tests/test_gdscript.cpp deleted file mode 100644 index 10586c6495..0000000000 --- a/main/tests/test_gdscript.cpp +++ /dev/null @@ -1,1035 +0,0 @@ -/*************************************************************************/ -/* test_gdscript.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_gdscript.h" - -#include "core/os/file_access.h" -#include "core/os/main_loop.h" -#include "core/os/os.h" - -#include "modules/modules_enabled.gen.h" -#ifdef MODULE_GDSCRIPT_ENABLED - -#include "modules/gdscript/gdscript.h" -#include "modules/gdscript/gdscript_compiler.h" -#include "modules/gdscript/gdscript_parser.h" -#include "modules/gdscript/gdscript_tokenizer.h" - -namespace TestGDScript { - -static void _print_indent(int p_ident, const String &p_text) { - String txt; - for (int i = 0; i < p_ident; i++) { - txt += '\t'; - } - - print_line(txt + p_text); -} - -static String _parser_extends(const GDScriptParser::ClassNode *p_class) { - String txt = "extends "; - if (String(p_class->extends_file) != "") { - txt += "\"" + p_class->extends_file + "\""; - if (p_class->extends_class.size()) { - txt += "."; - } - } - - for (int i = 0; i < p_class->extends_class.size(); i++) { - if (i != 0) { - txt += "."; - } - - txt += p_class->extends_class[i]; - } - - return txt; -} - -static String _parser_expr(const GDScriptParser::Node *p_expr) { - String txt; - switch (p_expr->type) { - case GDScriptParser::Node::TYPE_IDENTIFIER: { - const GDScriptParser::IdentifierNode *id_node = static_cast<const GDScriptParser::IdentifierNode *>(p_expr); - txt = id_node->name; - } break; - case GDScriptParser::Node::TYPE_CONSTANT: { - const GDScriptParser::ConstantNode *c_node = static_cast<const GDScriptParser::ConstantNode *>(p_expr); - if (c_node->value.get_type() == Variant::STRING) { - txt = "\"" + String(c_node->value) + "\""; - } else { - txt = c_node->value; - } - - } break; - case GDScriptParser::Node::TYPE_SELF: { - txt = "self"; - } break; - case GDScriptParser::Node::TYPE_ARRAY: { - const GDScriptParser::ArrayNode *arr_node = static_cast<const GDScriptParser::ArrayNode *>(p_expr); - txt += "["; - for (int i = 0; i < arr_node->elements.size(); i++) { - if (i > 0) { - txt += ", "; - } - txt += _parser_expr(arr_node->elements[i]); - } - txt += "]"; - } break; - case GDScriptParser::Node::TYPE_DICTIONARY: { - const GDScriptParser::DictionaryNode *dict_node = static_cast<const GDScriptParser::DictionaryNode *>(p_expr); - txt += "{"; - for (int i = 0; i < dict_node->elements.size(); i++) { - if (i > 0) { - txt += ", "; - } - - const GDScriptParser::DictionaryNode::Pair &p = dict_node->elements[i]; - txt += _parser_expr(p.key); - txt += ":"; - txt += _parser_expr(p.value); - } - txt += "}"; - } break; - case GDScriptParser::Node::TYPE_OPERATOR: { - const GDScriptParser::OperatorNode *c_node = static_cast<const GDScriptParser::OperatorNode *>(p_expr); - switch (c_node->op) { - case GDScriptParser::OperatorNode::OP_PARENT_CALL: - txt += "."; - [[fallthrough]]; - case GDScriptParser::OperatorNode::OP_CALL: { - ERR_FAIL_COND_V(c_node->arguments.size() < 1, ""); - String func_name; - const GDScriptParser::Node *nfunc = c_node->arguments[0]; - int arg_ofs = 0; - if (nfunc->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) { - const GDScriptParser::BuiltInFunctionNode *bif_node = static_cast<const GDScriptParser::BuiltInFunctionNode *>(nfunc); - func_name = GDScriptFunctions::get_func_name(bif_node->function); - arg_ofs = 1; - } else if (nfunc->type == GDScriptParser::Node::TYPE_TYPE) { - const GDScriptParser::TypeNode *t_node = static_cast<const GDScriptParser::TypeNode *>(nfunc); - func_name = Variant::get_type_name(t_node->vtype); - arg_ofs = 1; - } else { - ERR_FAIL_COND_V(c_node->arguments.size() < 2, ""); - nfunc = c_node->arguments[1]; - ERR_FAIL_COND_V(nfunc->type != GDScriptParser::Node::TYPE_IDENTIFIER, ""); - - if (c_node->arguments[0]->type != GDScriptParser::Node::TYPE_SELF) { - func_name = _parser_expr(c_node->arguments[0]) + "."; - } - - func_name += _parser_expr(nfunc); - arg_ofs = 2; - } - - txt += func_name + "("; - - for (int i = arg_ofs; i < c_node->arguments.size(); i++) { - const GDScriptParser::Node *arg = c_node->arguments[i]; - if (i > arg_ofs) { - txt += ", "; - } - txt += _parser_expr(arg); - } - - txt += ")"; - - } break; - case GDScriptParser::OperatorNode::OP_INDEX: { - ERR_FAIL_COND_V(c_node->arguments.size() != 2, ""); - - //index with [] - txt = _parser_expr(c_node->arguments[0]) + "[" + _parser_expr(c_node->arguments[1]) + "]"; - - } break; - case GDScriptParser::OperatorNode::OP_INDEX_NAMED: { - ERR_FAIL_COND_V(c_node->arguments.size() != 2, ""); - - txt = _parser_expr(c_node->arguments[0]) + "." + _parser_expr(c_node->arguments[1]); - - } break; - case GDScriptParser::OperatorNode::OP_NEG: { - txt = "-" + _parser_expr(c_node->arguments[0]); - } break; - case GDScriptParser::OperatorNode::OP_NOT: { - txt = "not " + _parser_expr(c_node->arguments[0]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_INVERT: { - txt = "~" + _parser_expr(c_node->arguments[0]); - } break; - case GDScriptParser::OperatorNode::OP_IN: { - txt = _parser_expr(c_node->arguments[0]) + " in " + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + "==" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_NOT_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + "!=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_LESS: { - txt = _parser_expr(c_node->arguments[0]) + "<" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_LESS_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + "<=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_GREATER: { - txt = _parser_expr(c_node->arguments[0]) + ">" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: { - txt = _parser_expr(c_node->arguments[0]) + ">=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_AND: { - txt = _parser_expr(c_node->arguments[0]) + " and " + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_OR: { - txt = _parser_expr(c_node->arguments[0]) + " or " + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ADD: { - txt = _parser_expr(c_node->arguments[0]) + "+" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_SUB: { - txt = _parser_expr(c_node->arguments[0]) + "-" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_MUL: { - txt = _parser_expr(c_node->arguments[0]) + "*" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_DIV: { - txt = _parser_expr(c_node->arguments[0]) + "/" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_MOD: { - txt = _parser_expr(c_node->arguments[0]) + "%" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: { - txt = _parser_expr(c_node->arguments[0]) + "<<" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: { - txt = _parser_expr(c_node->arguments[0]) + ">>" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN: { - txt = _parser_expr(c_node->arguments[0]) + "=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_ADD: { - txt = _parser_expr(c_node->arguments[0]) + "+=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SUB: { - txt = _parser_expr(c_node->arguments[0]) + "-=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_MUL: { - txt = _parser_expr(c_node->arguments[0]) + "*=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_DIV: { - txt = _parser_expr(c_node->arguments[0]) + "/=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_MOD: { - txt = _parser_expr(c_node->arguments[0]) + "%=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: { - txt = _parser_expr(c_node->arguments[0]) + "<<=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: { - txt = _parser_expr(c_node->arguments[0]) + ">>=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND: { - txt = _parser_expr(c_node->arguments[0]) + "&=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR: { - txt = _parser_expr(c_node->arguments[0]) + "|=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR: { - txt = _parser_expr(c_node->arguments[0]) + "^=" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_AND: { - txt = _parser_expr(c_node->arguments[0]) + "&" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_OR: { - txt = _parser_expr(c_node->arguments[0]) + "|" + _parser_expr(c_node->arguments[1]); - } break; - case GDScriptParser::OperatorNode::OP_BIT_XOR: { - txt = _parser_expr(c_node->arguments[0]) + "^" + _parser_expr(c_node->arguments[1]); - } break; - default: { - } - } - - } break; - case GDScriptParser::Node::TYPE_CAST: { - const GDScriptParser::CastNode *cast_node = static_cast<const GDScriptParser::CastNode *>(p_expr); - txt = _parser_expr(cast_node->source_node) + " as " + cast_node->cast_type.to_string(); - - } break; - case GDScriptParser::Node::TYPE_NEWLINE: { - //skippie - } break; - default: { - ERR_FAIL_V_MSG("", "Parser bug at " + itos(p_expr->line) + ", invalid expression type: " + itos(p_expr->type)); - } - } - - return txt; -} - -static void _parser_show_block(const GDScriptParser::BlockNode *p_block, int p_indent) { - for (int i = 0; i < p_block->statements.size(); i++) { - const GDScriptParser::Node *statement = p_block->statements[i]; - - switch (statement->type) { - case GDScriptParser::Node::TYPE_CONTROL_FLOW: { - const GDScriptParser::ControlFlowNode *cf_node = static_cast<const GDScriptParser::ControlFlowNode *>(statement); - switch (cf_node->cf_type) { - case GDScriptParser::ControlFlowNode::CF_IF: { - ERR_FAIL_COND(cf_node->arguments.size() != 1); - String txt; - txt += "if "; - txt += _parser_expr(cf_node->arguments[0]); - txt += ":"; - _print_indent(p_indent, txt); - ERR_FAIL_COND(!cf_node->body); - _parser_show_block(cf_node->body, p_indent + 1); - if (cf_node->body_else) { - _print_indent(p_indent, "else:"); - _parser_show_block(cf_node->body_else, p_indent + 1); - } - - } break; - case GDScriptParser::ControlFlowNode::CF_FOR: { - ERR_FAIL_COND(cf_node->arguments.size() != 2); - String txt; - txt += "for "; - txt += _parser_expr(cf_node->arguments[0]); - txt += " in "; - txt += _parser_expr(cf_node->arguments[1]); - txt += ":"; - _print_indent(p_indent, txt); - ERR_FAIL_COND(!cf_node->body); - _parser_show_block(cf_node->body, p_indent + 1); - - } break; - case GDScriptParser::ControlFlowNode::CF_WHILE: { - ERR_FAIL_COND(cf_node->arguments.size() != 1); - String txt; - txt += "while "; - txt += _parser_expr(cf_node->arguments[0]); - txt += ":"; - _print_indent(p_indent, txt); - ERR_FAIL_COND(!cf_node->body); - _parser_show_block(cf_node->body, p_indent + 1); - - } break; - case GDScriptParser::ControlFlowNode::CF_MATCH: { - // FIXME: Implement - } break; - case GDScriptParser::ControlFlowNode::CF_CONTINUE: { - _print_indent(p_indent, "continue"); - } break; - case GDScriptParser::ControlFlowNode::CF_BREAK: { - _print_indent(p_indent, "break"); - } break; - case GDScriptParser::ControlFlowNode::CF_RETURN: { - if (cf_node->arguments.size()) { - _print_indent(p_indent, "return " + _parser_expr(cf_node->arguments[0])); - } else { - _print_indent(p_indent, "return "); - } - } break; - } - - } break; - case GDScriptParser::Node::TYPE_LOCAL_VAR: { - const GDScriptParser::LocalVarNode *lv_node = static_cast<const GDScriptParser::LocalVarNode *>(statement); - _print_indent(p_indent, "var " + String(lv_node->name)); - } break; - default: { - //expression i guess - _print_indent(p_indent, _parser_expr(statement)); - } - } - } -} - -static void _parser_show_function(const GDScriptParser::FunctionNode *p_func, int p_indent, GDScriptParser::BlockNode *p_initializer = nullptr) { - String txt; - if (p_func->_static) { - txt = "static "; - } - txt += "func "; - if (p_func->name == "") { // initializer - txt += "[built-in-initializer]"; - } else { - txt += String(p_func->name); - } - txt += "("; - - for (int i = 0; i < p_func->arguments.size(); i++) { - if (i != 0) { - txt += ", "; - } - txt += "var " + String(p_func->arguments[i]); - if (i >= (p_func->arguments.size() - p_func->default_values.size())) { - int defarg = i - (p_func->arguments.size() - p_func->default_values.size()); - txt += "="; - txt += _parser_expr(p_func->default_values[defarg]); - } - } - - txt += ")"; - - //todo constructor check! - - txt += ":"; - - _print_indent(p_indent, txt); - if (p_initializer) { - _parser_show_block(p_initializer, p_indent + 1); - } - _parser_show_block(p_func->body, p_indent + 1); -} - -static void _parser_show_class(const GDScriptParser::ClassNode *p_class, int p_indent, const Vector<String> &p_code) { - if (p_indent == 0 && (String(p_class->extends_file) != "" || p_class->extends_class.size())) { - _print_indent(p_indent, _parser_extends(p_class)); - print_line("\n"); - } - - for (int i = 0; i < p_class->subclasses.size(); i++) { - const GDScriptParser::ClassNode *subclass = p_class->subclasses[i]; - String line = "class " + subclass->name; - if (String(subclass->extends_file) != "" || subclass->extends_class.size()) { - line += " " + _parser_extends(subclass); - } - line += ":"; - _print_indent(p_indent, line); - _parser_show_class(subclass, p_indent + 1, p_code); - print_line("\n"); - } - - for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { - const GDScriptParser::ClassNode::Constant &constant = E->get(); - _print_indent(p_indent, "const " + String(E->key()) + "=" + _parser_expr(constant.expression)); - } - - for (int i = 0; i < p_class->variables.size(); i++) { - const GDScriptParser::ClassNode::Member &m = p_class->variables[i]; - - _print_indent(p_indent, "var " + String(m.identifier)); - } - - print_line("\n"); - - for (int i = 0; i < p_class->static_functions.size(); i++) { - _parser_show_function(p_class->static_functions[i], p_indent); - print_line("\n"); - } - - for (int i = 0; i < p_class->functions.size(); i++) { - if (String(p_class->functions[i]->name) == "_init") { - _parser_show_function(p_class->functions[i], p_indent, p_class->initializer); - } else { - _parser_show_function(p_class->functions[i], p_indent); - } - print_line("\n"); - } - //_parser_show_function(p_class->initializer,p_indent); - print_line("\n"); -} - -static String _disassemble_addr(const Ref<GDScript> &p_script, const GDScriptFunction &func, int p_addr) { - int addr = p_addr & GDScriptFunction::ADDR_MASK; - - switch (p_addr >> GDScriptFunction::ADDR_BITS) { - case GDScriptFunction::ADDR_TYPE_SELF: { - return "self"; - } break; - case GDScriptFunction::ADDR_TYPE_CLASS: { - return "class"; - } break; - case GDScriptFunction::ADDR_TYPE_MEMBER: { - return "member(" + p_script->debug_get_member_by_index(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: { - return "class_const(" + func.get_global_name(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: { - Variant v = func.get_constant(addr); - String txt; - if (v.get_type() == Variant::STRING || v.get_type() == Variant::NODE_PATH) { - txt = "\"" + String(v) + "\""; - } else { - txt = v; - } - return "const(" + txt + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_STACK: { - return "stack(" + itos(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: { - return "var_stack(" + itos(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_GLOBAL: { - return "global(" + func.get_global_name(addr) + ")"; - } break; - case GDScriptFunction::ADDR_TYPE_NIL: { - return "nil"; - } break; - } - - return "<err>"; -} - -static void _disassemble_class(const Ref<GDScript> &p_class, const Vector<String> &p_code) { - const Map<StringName, GDScriptFunction *> &mf = p_class->debug_get_member_functions(); - - for (const Map<StringName, GDScriptFunction *>::Element *E = mf.front(); E; E = E->next()) { - const GDScriptFunction &func = *E->get(); - const int *code = func.get_code(); - int codelen = func.get_code_size(); - String defargs; - if (func.get_default_argument_count()) { - defargs = "defarg at: "; - for (int i = 0; i < func.get_default_argument_count(); i++) { - if (i > 0) { - defargs += ","; - } - defargs += itos(func.get_default_argument_addr(i)); - } - defargs += " "; - } - print_line("== function " + String(func.get_name()) + "() :: stack size: " + itos(func.get_max_stack_size()) + " " + defargs + "=="); - -#define DADDR(m_ip) (_disassemble_addr(p_class, func, code[ip + m_ip])) - - for (int ip = 0; ip < codelen;) { - int incr = 0; - String txt = itos(ip) + " "; - - switch (code[ip]) { - case GDScriptFunction::OPCODE_OPERATOR: { - int op = code[ip + 1]; - txt += " op "; - - String opname = Variant::get_operator_name(Variant::Operator(op)); - - txt += DADDR(4); - txt += " = "; - txt += DADDR(2); - txt += " " + opname + " "; - txt += DADDR(3); - incr += 5; - - } break; - case GDScriptFunction::OPCODE_SET: { - txt += "set "; - txt += DADDR(1); - txt += "["; - txt += DADDR(2); - txt += "]="; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_GET: { - txt += " get "; - txt += DADDR(3); - txt += "="; - txt += DADDR(1); - txt += "["; - txt += DADDR(2); - txt += "]"; - incr += 4; - - } break; - case GDScriptFunction::OPCODE_SET_NAMED: { - txt += " set_named "; - txt += DADDR(1); - txt += "[\""; - txt += func.get_global_name(code[ip + 2]); - txt += "\"]="; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_GET_NAMED: { - txt += " get_named "; - txt += DADDR(3); - txt += "="; - txt += DADDR(1); - txt += "[\""; - txt += func.get_global_name(code[ip + 2]); - txt += "\"]"; - incr += 4; - - } break; - case GDScriptFunction::OPCODE_SET_MEMBER: { - txt += " set_member "; - txt += "[\""; - txt += func.get_global_name(code[ip + 1]); - txt += "\"]="; - txt += DADDR(2); - incr += 3; - - } break; - case GDScriptFunction::OPCODE_GET_MEMBER: { - txt += " get_member "; - txt += DADDR(2); - txt += "="; - txt += "[\""; - txt += func.get_global_name(code[ip + 1]); - txt += "\"]"; - incr += 3; - - } break; - case GDScriptFunction::OPCODE_ASSIGN: { - txt += " assign "; - txt += DADDR(1); - txt += "="; - txt += DADDR(2); - incr += 3; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_TRUE: { - txt += " assign "; - txt += DADDR(1); - txt += "= true"; - incr += 2; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_FALSE: { - txt += " assign "; - txt += DADDR(1); - txt += "= false"; - incr += 2; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN: { - txt += " assign typed builtin ("; - txt += Variant::get_type_name((Variant::Type)code[ip + 1]); - txt += ") "; - txt += DADDR(2); - txt += " = "; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE: { - Variant className = func.get_constant(code[ip + 1]); - GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(className.operator Object *()); - - txt += " assign typed native ("; - txt += nc->get_name().operator String(); - txt += ") "; - txt += DADDR(2); - txt += " = "; - txt += DADDR(3); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_CAST_TO_SCRIPT: { - txt += " cast "; - txt += DADDR(3); - txt += "="; - txt += DADDR(1); - txt += " as "; - txt += DADDR(2); - incr += 4; - - } break; - case GDScriptFunction::OPCODE_CONSTRUCT: { - Variant::Type t = Variant::Type(code[ip + 1]); - int argc = code[ip + 2]; - - txt += " construct "; - txt += DADDR(3 + argc); - txt += " = "; - - txt += Variant::get_type_name(t) + "("; - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(i + 3); - } - txt += ")"; - - incr = 4 + argc; - - } break; - case GDScriptFunction::OPCODE_CONSTRUCT_ARRAY: { - int argc = code[ip + 1]; - txt += " make_array "; - txt += DADDR(2 + argc); - txt += " = [ "; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(2 + i); - } - - txt += "]"; - - incr += 3 + argc; - - } break; - case GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY: { - int argc = code[ip + 1]; - txt += " make_dict "; - txt += DADDR(2 + argc * 2); - txt += " = { "; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(2 + i * 2 + 0); - txt += ":"; - txt += DADDR(2 + i * 2 + 1); - } - - txt += "}"; - - incr += 3 + argc * 2; - - } break; - - case GDScriptFunction::OPCODE_CALL: - case GDScriptFunction::OPCODE_CALL_RETURN: { - bool ret = code[ip] == GDScriptFunction::OPCODE_CALL_RETURN; - - if (ret) { - txt += " call-ret "; - } else { - txt += " call "; - } - - int argc = code[ip + 1]; - if (ret) { - txt += DADDR(4 + argc) + "="; - } - - txt += DADDR(2) + "."; - txt += String(func.get_global_name(code[ip + 3])); - txt += "("; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(4 + i); - } - txt += ")"; - - incr = 5 + argc; - - } break; - case GDScriptFunction::OPCODE_CALL_BUILT_IN: { - txt += " call-built-in "; - - int argc = code[ip + 2]; - txt += DADDR(3 + argc) + "="; - - txt += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(code[ip + 1])); - txt += "("; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(3 + i); - } - txt += ")"; - - incr = 4 + argc; - - } break; - case GDScriptFunction::OPCODE_CALL_SELF_BASE: { - txt += " call-self-base "; - - int argc = code[ip + 2]; - txt += DADDR(3 + argc) + "="; - - txt += func.get_global_name(code[ip + 1]); - txt += "("; - - for (int i = 0; i < argc; i++) { - if (i > 0) { - txt += ", "; - } - txt += DADDR(3 + i); - } - txt += ")"; - - incr = 4 + argc; - - } break; - case GDScriptFunction::OPCODE_YIELD: { - txt += " yield "; - incr = 1; - - } break; - case GDScriptFunction::OPCODE_YIELD_SIGNAL: { - txt += " yield_signal "; - txt += DADDR(1); - txt += ","; - txt += DADDR(2); - incr = 3; - } break; - case GDScriptFunction::OPCODE_YIELD_RESUME: { - txt += " yield resume: "; - txt += DADDR(1); - incr = 2; - } break; - case GDScriptFunction::OPCODE_JUMP: { - txt += " jump "; - txt += itos(code[ip + 1]); - - incr = 2; - - } break; - case GDScriptFunction::OPCODE_JUMP_IF: { - txt += " jump-if "; - txt += DADDR(1); - txt += " to "; - txt += itos(code[ip + 2]); - - incr = 3; - } break; - case GDScriptFunction::OPCODE_JUMP_IF_NOT: { - txt += " jump-if-not "; - txt += DADDR(1); - txt += " to "; - txt += itos(code[ip + 2]); - - incr = 3; - } break; - case GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT: { - txt += " jump-to-default-argument "; - incr = 1; - } break; - case GDScriptFunction::OPCODE_RETURN: { - txt += " return "; - txt += DADDR(1); - - incr = 2; - - } break; - case GDScriptFunction::OPCODE_ITERATE_BEGIN: { - txt += " for-init " + DADDR(4) + " in " + DADDR(2) + " counter " + DADDR(1) + " end " + itos(code[ip + 3]); - incr += 5; - - } break; - case GDScriptFunction::OPCODE_ITERATE: { - txt += " for-loop " + DADDR(4) + " in " + DADDR(2) + " counter " + DADDR(1) + " end " + itos(code[ip + 3]); - incr += 5; - - } break; - case GDScriptFunction::OPCODE_LINE: { - int line = code[ip + 1] - 1; - if (line >= 0 && line < p_code.size()) { - txt = "\n" + itos(line + 1) + ": " + p_code[line] + "\n"; - } else { - txt = ""; - } - incr += 2; - } break; - case GDScriptFunction::OPCODE_END: { - txt += " end"; - incr += 1; - } break; - case GDScriptFunction::OPCODE_ASSERT: { - txt += " assert "; - txt += DADDR(1); - incr += 2; - - } break; - } - - if (incr == 0) { - ERR_BREAK_MSG(true, "Unhandled opcode: " + itos(code[ip])); - } - - ip += incr; - if (txt != "") { - print_line(txt); - } - } - } -} - -MainLoop *test(TestType p_type) { - List<String> cmdlargs = OS::get_singleton()->get_cmdline_args(); - - if (cmdlargs.empty()) { - return nullptr; - } - - String test = cmdlargs.back()->get(); - if (!test.ends_with(".gd") && !test.ends_with(".gdc")) { - print_line("This test expects a path to a GDScript file as its last parameter. Got: " + test); - return nullptr; - } - - FileAccess *fa = FileAccess::open(test, FileAccess::READ); - ERR_FAIL_COND_V_MSG(!fa, nullptr, "Could not open file: " + test); - - Vector<uint8_t> buf; - int flen = fa->get_len(); - buf.resize(fa->get_len() + 1); - fa->get_buffer(buf.ptrw(), flen); - buf.write[flen] = 0; - - String code; - code.parse_utf8((const char *)&buf[0]); - - Vector<String> lines; - int last = 0; - - for (int i = 0; i <= code.length(); i++) { - if (code[i] == '\n' || code[i] == 0) { - lines.push_back(code.substr(last, i - last)); - last = i + 1; - } - } - - if (p_type == TEST_TOKENIZER) { - GDScriptTokenizerText tk; - tk.set_code(code); - int line = -1; - while (tk.get_token() != GDScriptTokenizer::TK_EOF) { - String text; - if (tk.get_token() == GDScriptTokenizer::TK_IDENTIFIER) { - text = "'" + tk.get_token_identifier() + "' (identifier)"; - } else if (tk.get_token() == GDScriptTokenizer::TK_CONSTANT) { - const Variant &c = tk.get_token_constant(); - if (c.get_type() == Variant::STRING) { - text = "\"" + String(c) + "\""; - } else { - text = c; - } - - text = text + " (" + Variant::get_type_name(c.get_type()) + " constant)"; - } else if (tk.get_token() == GDScriptTokenizer::TK_ERROR) { - text = "ERROR: " + tk.get_token_error(); - } else if (tk.get_token() == GDScriptTokenizer::TK_NEWLINE) { - text = "newline (" + itos(tk.get_token_line()) + ") + indent: " + itos(tk.get_token_line_indent()); - } else if (tk.get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) { - text = "'" + String(GDScriptFunctions::get_func_name(tk.get_token_built_in_func())) + "' (built-in function)"; - } else { - text = tk.get_token_name(tk.get_token()); - } - - if (tk.get_token_line() != line) { - int from = line + 1; - line = tk.get_token_line(); - - for (int i = from; i <= line; i++) { - int l = i - 1; - if (l >= 0 && l < lines.size()) { - print_line("\n" + itos(i) + ": " + lines[l] + "\n"); - } - } - } - print_line("\t(" + itos(tk.get_token_column()) + "): " + text); - tk.advance(); - } - } - - if (p_type == TEST_PARSER) { - GDScriptParser parser; - Error err = parser.parse(code); - if (err) { - print_line("Parse Error:\n" + itos(parser.get_error_line()) + ":" + itos(parser.get_error_column()) + ":" + parser.get_error()); - memdelete(fa); - return nullptr; - } - - const GDScriptParser::Node *root = parser.get_parse_tree(); - ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, nullptr); - const GDScriptParser::ClassNode *cnode = static_cast<const GDScriptParser::ClassNode *>(root); - - _parser_show_class(cnode, 0, lines); - } - - if (p_type == TEST_COMPILER) { - GDScriptParser parser; - - Error err = parser.parse(code); - if (err) { - print_line("Parse Error:\n" + itos(parser.get_error_line()) + ":" + itos(parser.get_error_column()) + ":" + parser.get_error()); - memdelete(fa); - return nullptr; - } - - Ref<GDScript> gds; - gds.instance(); - - GDScriptCompiler gdc; - err = gdc.compile(&parser, gds.ptr()); - if (err) { - print_line("Compile Error:\n" + itos(gdc.get_error_line()) + ":" + itos(gdc.get_error_column()) + ":" + gdc.get_error()); - return nullptr; - } - - Ref<GDScript> current = gds; - - while (current.is_valid()) { - print_line("** CLASS **"); - _disassemble_class(current, lines); - - current = current->get_base(); - } - - } else if (p_type == TEST_BYTECODE) { - Vector<uint8_t> buf2 = GDScriptTokenizerBuffer::parse_code_string(code); - String dst = test.get_basename() + ".gdc"; - FileAccess *fw = FileAccess::open(dst, FileAccess::WRITE); - fw->store_buffer(buf2.ptr(), buf2.size()); - memdelete(fw); - } - - memdelete(fa); - - return nullptr; -} - -} // namespace TestGDScript - -#else - -namespace TestGDScript { - -MainLoop *test(TestType p_type) { - ERR_PRINT("The GDScript module is disabled, therefore GDScript tests cannot be used."); - return nullptr; -} - -} // namespace TestGDScript - -#endif diff --git a/main/tests/test_gdscript.h b/main/tests/test_gdscript.h deleted file mode 100644 index 6595da1430..0000000000 --- a/main/tests/test_gdscript.h +++ /dev/null @@ -1,48 +0,0 @@ -/*************************************************************************/ -/* test_gdscript.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_GDSCRIPT_H -#define TEST_GDSCRIPT_H - -#include "core/os/main_loop.h" - -namespace TestGDScript { - -enum TestType { - TEST_TOKENIZER, - TEST_PARSER, - TEST_COMPILER, - TEST_BYTECODE, -}; - -MainLoop *test(TestType p_type); -} // namespace TestGDScript - -#endif // TEST_GDSCRIPT_H diff --git a/main/tests/test_gui.cpp b/main/tests/test_gui.cpp deleted file mode 100644 index d46a13d2c0..0000000000 --- a/main/tests/test_gui.cpp +++ /dev/null @@ -1,271 +0,0 @@ -/*************************************************************************/ -/* test_gui.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef _3D_DISABLED - -#include "test_gui.h" - -#include "core/io/image_loader.h" -#include "core/os/os.h" -#include "core/print_string.h" -#include "scene/2d/sprite_2d.h" -#include "scene/gui/button.h" -#include "scene/gui/control.h" -#include "scene/gui/label.h" -#include "scene/gui/line_edit.h" -#include "scene/gui/menu_button.h" -#include "scene/gui/option_button.h" -#include "scene/gui/panel.h" -#include "scene/gui/popup_menu.h" -#include "scene/gui/progress_bar.h" -#include "scene/gui/rich_text_label.h" -#include "scene/gui/scroll_bar.h" -#include "scene/gui/spin_box.h" -#include "scene/gui/tab_container.h" -#include "scene/gui/texture_rect.h" -#include "scene/gui/tree.h" -#include "scene/main/scene_tree.h" - -#include "scene/3d/camera_3d.h" -#include "scene/main/window.h" - -namespace TestGUI { - -class TestMainLoop : public SceneTree { -public: - virtual void request_quit() { - quit(); - } - virtual void init() { - SceneTree::init(); - - Panel *frame = memnew(Panel); - frame->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END); - frame->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_END); - frame->set_end(Point2(0, 0)); - - Ref<Theme> t = memnew(Theme); - frame->set_theme(t); - - get_root()->add_child(frame); - - Label *label = memnew(Label); - - label->set_position(Point2(80, 90)); - label->set_size(Point2(170, 80)); - label->set_align(Label::ALIGN_FILL); - label->set_text("There was once upon a time a beautiful unicorn that loved to play with little girls..."); - - frame->add_child(label); - - Button *button = memnew(Button); - - button->set_position(Point2(20, 20)); - button->set_size(Point2(1, 1)); - button->set_text("This is a biggie button"); - - frame->add_child(button); - - Tree *tree = memnew(Tree); - tree->set_columns(2); - - tree->set_position(Point2(230, 210)); - tree->set_size(Point2(150, 250)); - - TreeItem *item = tree->create_item(); - item->set_editable(0, true); - item->set_text(0, "root"); - item = tree->create_item(tree->get_root()); - item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK); - item->set_editable(0, true); - item->set_text(0, "check"); - item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK); - item->set_editable(1, true); - item->set_text(1, "check2"); - item = tree->create_item(tree->get_root()); - item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE); - item->set_editable(0, true); - item->set_range_config(0, 0, 20, 0.1); - item->set_range(0, 2); - item->add_button(0, Theme::get_default()->get_icon("folder", "FileDialog")); - item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE); - item->set_editable(1, true); - item->set_range_config(1, 0, 20, 0.1); - item->set_range(1, 3); - - item = tree->create_item(tree->get_root()); - item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE); - item->set_editable(0, true); - item->set_text(0, "Have,Many,Several,Options!"); - item->set_range(0, 2); - - item = tree->create_item(item); - item->set_editable(0, true); - item->set_text(0, "Gershwin!"); - - frame->add_child(tree); - - LineEdit *line_edit = memnew(LineEdit); - - line_edit->set_position(Point2(30, 190)); - line_edit->set_size(Point2(180, 1)); - - frame->add_child(line_edit); - - HScrollBar *hscroll = memnew(HScrollBar); - - hscroll->set_position(Point2(30, 290)); - hscroll->set_size(Point2(180, 1)); - hscroll->set_max(10); - hscroll->set_page(4); - - frame->add_child(hscroll); - - SpinBox *spin = memnew(SpinBox); - - spin->set_position(Point2(30, 260)); - spin->set_size(Point2(120, 1)); - - frame->add_child(spin); - hscroll->share(spin); - - ProgressBar *progress = memnew(ProgressBar); - - progress->set_position(Point2(30, 330)); - progress->set_size(Point2(120, 1)); - - frame->add_child(progress); - hscroll->share(progress); - - MenuButton *menu_button = memnew(MenuButton); - - menu_button->set_text("I'm a menu!"); - menu_button->set_position(Point2(30, 380)); - menu_button->set_size(Point2(1, 1)); - - frame->add_child(menu_button); - - PopupMenu *popup = menu_button->get_popup(); - - popup->add_item("Hello, testing"); - popup->add_item("My Dearest"); - popup->add_separator(); - popup->add_item("Popup"); - popup->add_check_item("Check Popup"); - popup->set_item_checked(4, true); - popup->add_separator(); - popup->add_radio_check_item("Option A"); - popup->set_item_checked(6, true); - popup->add_radio_check_item("Option B"); - - OptionButton *options = memnew(OptionButton); - - options->add_item("Hello, testing"); - options->add_item("My Dearest"); - - options->set_position(Point2(230, 180)); - options->set_size(Point2(1, 1)); - - frame->add_child(options); - - RichTextLabel *richtext = memnew(RichTextLabel); - - richtext->set_position(Point2(600, 210)); - richtext->set_size(Point2(180, 250)); - richtext->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -20); - - frame->add_child(richtext); - - richtext->add_text("Hello, My Friends!\n\nWelcome to the amazing world of "); - - richtext->add_newline(); - richtext->add_newline(); - - richtext->push_color(Color(1, 0.5, 0.5)); - richtext->add_text("leprechauns"); - richtext->pop(); - - richtext->add_text(" and "); - richtext->push_color(Color(0, 1.0, 0.5)); - richtext->add_text("faeries.\n"); - richtext->pop(); - richtext->add_text("In this new episode, we will attempt to "); - richtext->push_font(richtext->get_theme_font("mono_font", "Fonts")); - richtext->push_color(Color(0.7, 0.5, 1.0)); - richtext->add_text("deliver something nice"); - richtext->pop(); - richtext->pop(); - richtext->add_text(" to all the viewers! Unfortunately, I need to "); - richtext->push_underline(); - richtext->add_text("keep writing a lot of text"); - richtext->pop(); - richtext->add_text(" so the label control overflows and the scrollbar appears.\n"); - richtext->push_meta("http://www.scrollingcapabilities.xz"); - richtext->add_text("This allows to test for the scrolling capabilities "); - richtext->pop(); - richtext->add_text("of the rich text label for huge text (not like this text will really be huge but, you know).\nAs long as it is so long that it will work nicely for a test/demo, then it's welcomed in my book...\nChanging subject, the day is cloudy today and I'm wondering if I'll get che chance to travel somewhere nice. Sometimes, watching the clouds from satellite images may give a nice insight about how pressure zones in our planet work, although it also makes it pretty obvious to see why most weather forecasts get it wrong so often.\nClouds are so difficult to predict!\nBut it's pretty cool how our civilization has adapted to having water falling from the sky each time it rains..."); - - TabContainer *tabc = memnew(TabContainer); - - Control *ctl = memnew(Control); - ctl->set_name("tab 1"); - tabc->add_child(ctl); - - ctl = memnew(Control); - ctl->set_name("tab 2"); - tabc->add_child(ctl); - label = memnew(Label); - label->set_text("Some Label"); - label->set_position(Point2(20, 20)); - ctl->add_child(label); - - ctl = memnew(Control); - ctl->set_name("tab 3"); - button = memnew(Button); - button->set_text("Some Button"); - button->set_position(Point2(30, 50)); - ctl->add_child(button); - - tabc->add_child(ctl); - - frame->add_child(tabc); - - tabc->set_position(Point2(400, 210)); - tabc->set_size(Point2(180, 250)); - } -}; - -MainLoop *test() { - return memnew(TestMainLoop); -} - -} // namespace TestGUI - -#endif diff --git a/main/tests/test_gui.h b/main/tests/test_gui.h deleted file mode 100644 index 5a23179eee..0000000000 --- a/main/tests/test_gui.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_gui.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_GUI_H -#define TEST_GUI_H - -#include "core/os/main_loop.h" - -namespace TestGUI { - -MainLoop *test(); -} - -#endif diff --git a/main/tests/test_main.cpp b/main/tests/test_main.cpp deleted file mode 100644 index 0bb8367240..0000000000 --- a/main/tests/test_main.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/*************************************************************************/ -/* test_main.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_main.h" - -#include "core/list.h" - -#ifdef DEBUG_ENABLED - -#include "test_astar.h" -#include "test_class_db.h" -#include "test_gdscript.h" -#include "test_gui.h" -#include "test_math.h" -#include "test_oa_hash_map.h" -#include "test_ordered_hash_map.h" -#include "test_physics_2d.h" -#include "test_physics_3d.h" -#include "test_render.h" -#include "test_shader_lang.h" -#include "test_string.h" - -const char **tests_get_names() { - static const char *test_names[] = { - "string", - "math", - "physics_2d", - "physics_3d", - "render", - "oa_hash_map", - "class_db", - "gui", - "shaderlang", - "gd_tokenizer", - "gd_parser", - "gd_compiler", - "gd_bytecode", - "ordered_hash_map", - "astar", - nullptr - }; - - return test_names; -} - -MainLoop *test_main(String p_test, const List<String> &p_args) { - if (p_test == "string") { - return TestString::test(); - } - - if (p_test == "math") { - return TestMath::test(); - } - - if (p_test == "physics_2d") { - return TestPhysics2D::test(); - } - - if (p_test == "physics_3d") { - return TestPhysics3D::test(); - } - - if (p_test == "render") { - return TestRender::test(); - } - - if (p_test == "oa_hash_map") { - return TestOAHashMap::test(); - } - - if (p_test == "class_db") { - return TestClassDB::test(); - } - -#ifndef _3D_DISABLED - if (p_test == "gui") { - return TestGUI::test(); - } -#endif - - if (p_test == "shaderlang") { - return TestShaderLang::test(); - } - - if (p_test == "gd_tokenizer") { - return TestGDScript::test(TestGDScript::TEST_TOKENIZER); - } - - if (p_test == "gd_parser") { - return TestGDScript::test(TestGDScript::TEST_PARSER); - } - - if (p_test == "gd_compiler") { - return TestGDScript::test(TestGDScript::TEST_COMPILER); - } - - if (p_test == "gd_bytecode") { - return TestGDScript::test(TestGDScript::TEST_BYTECODE); - } - - if (p_test == "ordered_hash_map") { - return TestOrderedHashMap::test(); - } - - if (p_test == "astar") { - return TestAStar::test(); - } - - print_line("Unknown test: " + p_test); - return nullptr; -} - -#else - -const char **tests_get_names() { - static const char *test_names[] = { - nullptr - }; - - return test_names; -} - -MainLoop *test_main(String p_test, const List<String> &p_args) { - return nullptr; -} - -#endif diff --git a/main/tests/test_main.h b/main/tests/test_main.h deleted file mode 100644 index bdb1668d21..0000000000 --- a/main/tests/test_main.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_main.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_MAIN_H -#define TEST_MAIN_H - -#include "core/list.h" -#include "core/os/main_loop.h" -#include "core/ustring.h" - -const char **tests_get_names(); -MainLoop *test_main(String p_test, const List<String> &p_args); - -#endif // TEST_MAIN_H diff --git a/main/tests/test_math.cpp b/main/tests/test_math.cpp deleted file mode 100644 index 11aa164709..0000000000 --- a/main/tests/test_math.cpp +++ /dev/null @@ -1,702 +0,0 @@ -/*************************************************************************/ -/* test_math.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_math.h" - -#include "core/math/basis.h" -#include "core/math/camera_matrix.h" -#include "core/math/delaunay_3d.h" -#include "core/math/math_funcs.h" -#include "core/math/transform.h" -#include "core/method_ptrcall.h" -#include "core/os/file_access.h" -#include "core/os/keyboard.h" -#include "core/os/os.h" -#include "core/print_string.h" -#include "core/ustring.h" -#include "core/variant.h" -#include "core/vmap.h" -#include "scene/main/node.h" -#include "scene/resources/texture.h" -#include "servers/rendering/shader_language.h" - -namespace TestMath { - -class GetClassAndNamespace { - String code; - int idx; - int line; - String error_str; - bool error; - Variant value; - - String class_name; - - enum Token { - TK_BRACKET_OPEN, - TK_BRACKET_CLOSE, - TK_CURLY_BRACKET_OPEN, - TK_CURLY_BRACKET_CLOSE, - TK_PERIOD, - TK_COLON, - TK_COMMA, - TK_SYMBOL, - TK_IDENTIFIER, - TK_STRING, - TK_NUMBER, - TK_EOF, - TK_ERROR - }; - - Token get_token() { - while (true) { - switch (code[idx]) { - case '\n': { - line++; - idx++; - break; - }; - case 0: { - return TK_EOF; - - } break; - case '{': { - idx++; - return TK_CURLY_BRACKET_OPEN; - }; - case '}': { - idx++; - return TK_CURLY_BRACKET_CLOSE; - }; - case '[': { - idx++; - return TK_BRACKET_OPEN; - }; - case ']': { - idx++; - return TK_BRACKET_CLOSE; - }; - case ':': { - idx++; - return TK_COLON; - }; - case ',': { - idx++; - return TK_COMMA; - }; - case '.': { - idx++; - return TK_PERIOD; - }; - case '#': { - //compiler directive - while (code[idx] != '\n' && code[idx] != 0) { - idx++; - } - continue; - } break; - case '/': { - switch (code[idx + 1]) { - case '*': { // block comment - - idx += 2; - while (true) { - if (code[idx] == 0) { - error_str = "Unterminated comment"; - error = true; - return TK_ERROR; - } else if (code[idx] == '*' && code[idx + 1] == '/') { - idx += 2; - break; - } else if (code[idx] == '\n') { - line++; - } - - idx++; - } - - } break; - case '/': { // line comment skip - - while (code[idx] != '\n' && code[idx] != 0) { - idx++; - } - - } break; - default: { - value = "/"; - idx++; - return TK_SYMBOL; - } - } - - continue; // a comment - } break; - case '\'': - case '"': { - CharType begin_str = code[idx]; - idx++; - String tk_string = String(); - while (true) { - if (code[idx] == 0) { - error_str = "Unterminated String"; - error = true; - return TK_ERROR; - } else if (code[idx] == begin_str) { - idx++; - break; - } else if (code[idx] == '\\') { - //escaped characters... - idx++; - CharType next = code[idx]; - if (next == 0) { - error_str = "Unterminated String"; - error = true; - return TK_ERROR; - } - CharType res = 0; - - switch (next) { - case 'b': - res = 8; - break; - case 't': - res = 9; - break; - case 'n': - res = 10; - break; - case 'f': - res = 12; - break; - case 'r': - res = 13; - break; - case '\"': - res = '\"'; - break; - case '\\': - res = '\\'; - break; - default: { - res = next; - } break; - } - - tk_string += res; - - } else { - if (code[idx] == '\n') { - line++; - } - tk_string += code[idx]; - } - idx++; - } - - value = tk_string; - - return TK_STRING; - - } break; - default: { - if (code[idx] <= 32) { - idx++; - break; - } - - if ((code[idx] >= 33 && code[idx] <= 47) || (code[idx] >= 58 && code[idx] <= 64) || (code[idx] >= 91 && code[idx] <= 96) || (code[idx] >= 123 && code[idx] <= 127)) { - value = String::chr(code[idx]); - idx++; - return TK_SYMBOL; - } - - if (code[idx] == '-' || (code[idx] >= '0' && code[idx] <= '9')) { - //a number - const CharType *rptr; - double number = String::to_double(&code[idx], &rptr); - idx += (rptr - &code[idx]); - value = number; - return TK_NUMBER; - - } else if ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) { - String id; - - while ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) { - id += code[idx]; - idx++; - } - - value = id; - return TK_IDENTIFIER; - } else { - error_str = "Unexpected character."; - error = true; - return TK_ERROR; - } - } - } - } - } - -public: - Error parse(const String &p_code, const String &p_known_class_name = String()) { - code = p_code; - idx = 0; - line = 0; - error_str = String(); - error = false; - value = Variant(); - class_name = String(); - - bool use_next_class = false; - Token tk = get_token(); - - Map<int, String> namespace_stack; - int curly_stack = 0; - - while (!error || tk != TK_EOF) { - if (tk == TK_BRACKET_OPEN) { - tk = get_token(); - if (tk == TK_IDENTIFIER && String(value) == "ScriptClass") { - if (get_token() == TK_BRACKET_CLOSE) { - use_next_class = true; - } - } - } else if (tk == TK_IDENTIFIER && String(value) == "class") { - tk = get_token(); - if (tk == TK_IDENTIFIER) { - String name = value; - if (use_next_class || p_known_class_name == name) { - for (Map<int, String>::Element *E = namespace_stack.front(); E; E = E->next()) { - class_name += E->get() + "."; - } - class_name += String(value); - break; - } - } - - } else if (tk == TK_IDENTIFIER && String(value) == "namespace") { - String name; - int at_level = curly_stack; - while (true) { - tk = get_token(); - if (tk == TK_IDENTIFIER) { - name += String(value); - } - - tk = get_token(); - if (tk == TK_PERIOD) { - name += "."; - } else if (tk == TK_CURLY_BRACKET_OPEN) { - curly_stack++; - break; - } else { - break; //whathever else - } - } - - if (name != String()) { - namespace_stack[at_level] = name; - } - - } else if (tk == TK_CURLY_BRACKET_OPEN) { - curly_stack++; - } else if (tk == TK_CURLY_BRACKET_CLOSE) { - curly_stack--; - if (namespace_stack.has(curly_stack)) { - namespace_stack.erase(curly_stack); - } - } - - tk = get_token(); - } - - if (error) { - return ERR_PARSE_ERROR; - } - - return OK; - } - - String get_error() { - return error_str; - } - - String get_class() { - return class_name; - } -}; - -void test_vec(Plane p_vec) { - CameraMatrix cm; - cm.set_perspective(45, 1, 0, 100); - Plane v0 = cm.xform4(p_vec); - - print_line("out: " + v0); - v0.normal.z = (v0.d / 100.0 * 2.0 - 1.0) * v0.d; - print_line("out_F: " + v0); -} - -uint32_t ihash(uint32_t a) { - a = (a + 0x7ed55d16) + (a << 12); - a = (a ^ 0xc761c23c) ^ (a >> 19); - a = (a + 0x165667b1) + (a << 5); - a = (a + 0xd3a2646c) ^ (a << 9); - a = (a + 0xfd7046c5) + (a << 3); - a = (a ^ 0xb55a4f09) ^ (a >> 16); - return a; -} - -uint32_t ihash2(uint32_t a) { - a = (a ^ 61) ^ (a >> 16); - a = a + (a << 3); - a = a ^ (a >> 4); - a = a * 0x27d4eb2d; - a = a ^ (a >> 15); - return a; -} - -uint32_t ihash3(uint32_t a) { - a = (a + 0x479ab41d) + (a << 8); - a = (a ^ 0xe4aa10ce) ^ (a >> 5); - a = (a + 0x9942f0a6) - (a << 14); - a = (a ^ 0x5aedd67d) ^ (a >> 3); - a = (a + 0x17bea992) + (a << 7); - return a; -} - -MainLoop *test() { - { - Vector<Vector3> points; - points.push_back(Vector3(0, 0, 0)); - points.push_back(Vector3(0, 0, 1)); - points.push_back(Vector3(0, 1, 0)); - points.push_back(Vector3(0, 1, 1)); - points.push_back(Vector3(1, 1, 0)); - points.push_back(Vector3(1, 0, 0)); - points.push_back(Vector3(1, 0, 1)); - points.push_back(Vector3(1, 1, 1)); - - for (int i = 0; i < 800; i++) { - points.push_back(Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * Vector3(25, 30, 33)); - } - - Vector<Delaunay3D::OutputSimplex> os = Delaunay3D::tetrahedralize(points); - print_line("simplices in the end: " + itos(os.size())); - for (int i = 0; i < os.size(); i++) { - print_line("Simplex " + itos(i) + ": "); - print_line(points[os[i].points[0]]); - print_line(points[os[i].points[1]]); - print_line(points[os[i].points[2]]); - print_line(points[os[i].points[3]]); - } - - { - FileAccessRef f = FileAccess::open("res://bsp.obj", FileAccess::WRITE); - for (int i = 0; i < os.size(); i++) { - f->store_line("o Simplex" + itos(i)); - for (int j = 0; j < 4; j++) { - f->store_line(vformat("v %f %f %f", points[os[i].points[j]].x, points[os[i].points[j]].y, points[os[i].points[j]].z)); - } - static const int face_order[4][3] = { - { 1, 2, 3 }, - { 1, 3, 4 }, - { 1, 2, 4 }, - { 2, 3, 4 } - }; - - for (int j = 0; j < 4; j++) { - f->store_line(vformat("f %d %d %d", 4 * i + face_order[j][0], 4 * i + face_order[j][1], 4 * i + face_order[j][2])); - } - } - f->close(); - } - - return nullptr; - } - - { - float r = 1; - float g = 0.5; - float b = 0.1; - - const float pow2to9 = 512.0f; - const float B = 15.0f; - const float N = 9.0f; - - float sharedexp = 65408.000f; - - float cRed = MAX(0.0f, MIN(sharedexp, r)); - float cGreen = MAX(0.0f, MIN(sharedexp, g)); - float cBlue = MAX(0.0f, MIN(sharedexp, b)); - - float cMax = MAX(cRed, MAX(cGreen, cBlue)); - - float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / Math_LN2)) + 1.0f + B; - - float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f); - - float exps = expp + 1.0f; - - if (0.0 <= sMax && sMax < pow2to9) { - exps = expp; - } - - float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f); - float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f); - float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f); - - print_line("R: " + rtos(sRed) + " G: " + rtos(sGreen) + " B: " + rtos(sBlue) + " EXP: " + rtos(exps)); - - uint32_t rgbe = (Math::fast_ftoi(sRed) & 0x1FF) | ((Math::fast_ftoi(sGreen) & 0x1FF) << 9) | ((Math::fast_ftoi(sBlue) & 0x1FF) << 18) | ((Math::fast_ftoi(exps) & 0x1F) << 27); - - float rb = rgbe & 0x1ff; - float gb = (rgbe >> 9) & 0x1ff; - float bb = (rgbe >> 18) & 0x1ff; - float eb = (rgbe >> 27); - float mb = Math::pow(2, eb - 15.0 - 9.0); - float rd = rb * mb; - float gd = gb * mb; - float bd = bb * mb; - - print_line("RGBE: " + Color(rd, gd, bd)); - } - - Vector<int> ints; - ints.resize(20); - - { - int *w; - w = ints.ptrw(); - for (int i = 0; i < ints.size(); i++) { - w[i] = i; - } - } - - Vector<int> posho = ints; - - { - const int *r = posho.ptr(); - for (int i = 0; i < posho.size(); i++) { - print_line(itos(i) + " : " + itos(r[i])); - } - } - - List<String> cmdlargs = OS::get_singleton()->get_cmdline_args(); - - if (cmdlargs.empty()) { - //try editor! - return nullptr; - } - - String test = cmdlargs.back()->get(); - if (test == "math") { - // Not a file name but the test name, abort. - // FIXME: This test is ugly as heck, needs fixing :) - return nullptr; - } - - FileAccess *fa = FileAccess::open(test, FileAccess::READ); - ERR_FAIL_COND_V_MSG(!fa, nullptr, "Could not open file: " + test); - - Vector<uint8_t> buf; - int flen = fa->get_len(); - buf.resize(fa->get_len() + 1); - fa->get_buffer(buf.ptrw(), flen); - buf.write[flen] = 0; - - String code; - code.parse_utf8((const char *)&buf[0]); - - GetClassAndNamespace getclass; - if (getclass.parse(code)) { - print_line("Parse error: " + getclass.get_error()); - } else { - print_line("Found class: " + getclass.get_class()); - } - - { - Vector<int> hashes; - List<StringName> tl; - ClassDB::get_class_list(&tl); - - for (List<StringName>::Element *E = tl.front(); E; E = E->next()) { - Vector<uint8_t> m5b = E->get().operator String().md5_buffer(); - hashes.push_back(hashes.size()); - } - - for (int i = nearest_shift(hashes.size()); i < 20; i++) { - bool success = true; - for (int s = 0; s < 10000; s++) { - Set<uint32_t> existing; - success = true; - - for (int j = 0; j < hashes.size(); j++) { - uint32_t eh = ihash2(ihash3(hashes[j] + ihash(s) + s)) & ((1 << i) - 1); - if (existing.has(eh)) { - success = false; - break; - } - existing.insert(eh); - } - - if (success) { - print_line("success at " + itos(i) + "/" + itos(nearest_shift(hashes.size())) + " shift " + itos(s)); - break; - } - } - if (success) { - break; - } - } - - print_line("DONE"); - } - - { - print_line("NUM: " + itos(-128)); - } - - { - Vector3 v(1, 2, 3); - v.normalize(); - float a = 0.3; - - Basis m(v, a); - - Vector3 v2(7, 3, 1); - v2.normalize(); - float a2 = 0.8; - - Basis m2(v2, a2); - - Quat q = m; - Quat q2 = m2; - - Basis m3 = m.inverse() * m2; - Quat q3 = (q.inverse() * q2); //.normalized(); - - print_line(Quat(m3)); - print_line(q3); - - print_line("before v: " + v + " a: " + rtos(a)); - q.get_axis_angle(v, a); - print_line("after v: " + v + " a: " + rtos(a)); - } - - String ret; - - List<String> args; - args.push_back("-l"); - Error err = OS::get_singleton()->execute("/bin/ls", args, true, nullptr, &ret); - print_line("error: " + itos(err)); - print_line(ret); - - Basis m3; - m3.rotate(Vector3(1, 0, 0), 0.2); - m3.rotate(Vector3(0, 1, 0), 1.77); - m3.rotate(Vector3(0, 0, 1), 212); - Basis m32; - m32.set_euler(m3.get_euler()); - print_line("ELEULEEEEEEEEEEEEEEEEEER: " + m3.get_euler() + " vs " + m32.get_euler()); - - { - Dictionary d; - d["momo"] = 1; - Dictionary b = d; - b["44"] = 4; - } - - print_line("inters: " + rtos(Geometry::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0))); - - print_line("cross: " + Vector3(1, 2, 3).cross(Vector3(4, 5, 7))); - print_line("dot: " + rtos(Vector3(1, 2, 3).dot(Vector3(4, 5, 7)))); - print_line("abs: " + Vector3(-1, 2, -3).abs()); - print_line("distance_to: " + rtos(Vector3(1, 2, 3).distance_to(Vector3(4, 5, 7)))); - print_line("distance_squared_to: " + rtos(Vector3(1, 2, 3).distance_squared_to(Vector3(4, 5, 7)))); - print_line("plus: " + (Vector3(1, 2, 3) + Vector3(Vector3(4, 5, 7)))); - print_line("minus: " + (Vector3(1, 2, 3) - Vector3(Vector3(4, 5, 7)))); - print_line("mul: " + (Vector3(1, 2, 3) * Vector3(Vector3(4, 5, 7)))); - print_line("div: " + (Vector3(1, 2, 3) / Vector3(Vector3(4, 5, 7)))); - print_line("mul scalar: " + (Vector3(1, 2, 3) * 2.0)); - print_line("premul scalar: " + (2.0 * Vector3(1, 2, 3))); - print_line("div scalar: " + (Vector3(1, 2, 3) / 3.0)); - print_line("length: " + rtos(Vector3(1, 2, 3).length())); - print_line("length squared: " + rtos(Vector3(1, 2, 3).length_squared())); - print_line("normalized: " + Vector3(1, 2, 3).normalized()); - print_line("inverse: " + Vector3(1, 2, 3).inverse()); - - { - Vector3 v(4, 5, 7); - v.normalize(); - print_line("normalize: " + v); - } - - { - Vector3 v(4, 5, 7); - v += Vector3(1, 2, 3); - print_line("+=: " + v); - } - - { - Vector3 v(4, 5, 7); - v -= Vector3(1, 2, 3); - print_line("-=: " + v); - } - - { - Vector3 v(4, 5, 7); - v *= Vector3(1, 2, 3); - print_line("*=: " + v); - } - - { - Vector3 v(4, 5, 7); - v /= Vector3(1, 2, 3); - print_line("/=: " + v); - } - - { - Vector3 v(4, 5, 7); - v *= 2.0; - print_line("scalar *=: " + v); - } - - { - Vector3 v(4, 5, 7); - v /= 2.0; - print_line("scalar /=: " + v); - } - - return nullptr; -} - -} // namespace TestMath diff --git a/main/tests/test_math.h b/main/tests/test_math.h deleted file mode 100644 index 77bce8dd66..0000000000 --- a/main/tests/test_math.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_math.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_MATH_H -#define TEST_MATH_H - -#include "core/os/main_loop.h" - -namespace TestMath { - -MainLoop *test(); -} - -#endif diff --git a/main/tests/test_oa_hash_map.cpp b/main/tests/test_oa_hash_map.cpp deleted file mode 100644 index 719817baf4..0000000000 --- a/main/tests/test_oa_hash_map.cpp +++ /dev/null @@ -1,216 +0,0 @@ -/*************************************************************************/ -/* test_oa_hash_map.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_oa_hash_map.h" - -#include "core/oa_hash_map.h" -#include "core/os/os.h" - -namespace TestOAHashMap { - -struct CountedItem { - static int count; - - int id = -1; - bool destroyed = false; - - CountedItem() { - count++; - } - - CountedItem(int p_id) : - id(p_id) { - count++; - } - - CountedItem(const CountedItem &p_other) : - id(p_other.id) { - count++; - } - - CountedItem &operator=(const CountedItem &p_other) = default; - - ~CountedItem() { - CRASH_COND(destroyed); - count--; - destroyed = true; - } -}; - -int CountedItem::count; - -MainLoop *test() { - OS::get_singleton()->print("\n\n\nHello from test\n"); - - // test element tracking. - { - OAHashMap<int, int> map; - - map.set(42, 1337); - map.set(1337, 21); - map.set(42, 11880); - - int value = 0; - map.lookup(42, value); - - OS::get_singleton()->print("capacity %d\n", map.get_capacity()); - OS::get_singleton()->print("elements %d\n", map.get_num_elements()); - - OS::get_singleton()->print("map[42] = %d\n", value); - } - - // rehashing and deletion - { - OAHashMap<int, int> map; - - for (int i = 0; i < 500; i++) { - map.set(i, i * 2); - } - - for (int i = 0; i < 500; i += 2) { - map.remove(i); - } - - uint32_t num_elems = 0; - for (int i = 0; i < 500; i++) { - int tmp; - if (map.lookup(i, tmp) && tmp == i * 2) { - num_elems++; - } - } - - OS::get_singleton()->print("elements %d == %d.\n", map.get_num_elements(), num_elems); - } - - // iteration - { - OAHashMap<String, int> map; - - map.set("Hello", 1); - map.set("World", 2); - map.set("Godot rocks", 42); - - for (OAHashMap<String, int>::Iterator it = map.iter(); it.valid; it = map.next_iter(it)) { - OS::get_singleton()->print("map[\"%s\"] = %d\n", it.key->utf8().get_data(), *it.value); - } - } - - // stress test / test for issue #22928 - { - OAHashMap<int, int> map; - int dummy = 0; - const int N = 1000; - uint32_t *keys = new uint32_t[N]; - - Math::seed(0); - - // insert a couple of random keys (with a dummy value, which is ignored) - for (int i = 0; i < N; i++) { - keys[i] = Math::rand(); - map.set(keys[i], dummy); - - if (!map.lookup(keys[i], dummy)) { - OS::get_singleton()->print("could not find 0x%X despite it was just inserted!\n", unsigned(keys[i])); - } - } - - // check whether the keys are still present - for (int i = 0; i < N; i++) { - if (!map.lookup(keys[i], dummy)) { - OS::get_singleton()->print("could not find 0x%X despite it has been inserted previously! (not checking the other keys, breaking...)\n", unsigned(keys[i])); - break; - } - } - - delete[] keys; - } - - // regression test / test for issue related to #31402 - { - OS::get_singleton()->print("test for issue #31402 started...\n"); - - const int num_test_values = 12; - int test_values[num_test_values] = { 0, 24, 48, 72, 96, 120, 144, 168, 192, 216, 240, 264 }; - - int dummy = 0; - OAHashMap<int, int> map; - map.clear(); - - for (int i = 0; i < num_test_values; ++i) { - map.set(test_values[i], dummy); - } - - OS::get_singleton()->print("test for issue #31402 passed.\n"); - } - - // test collision resolution, should not crash or run indefinitely - { - OAHashMap<int, int> map(4); - map.set(1, 1); - map.set(5, 1); - map.set(9, 1); - map.set(13, 1); - map.remove(5); - map.remove(9); - map.remove(13); - map.set(5, 1); - } - - // test memory management of items, should not crash or leak items - { - // Exercise different patterns of removal - for (int i = 0; i < 4; ++i) { - { - OAHashMap<String, CountedItem> map; - int id = 0; - for (int j = 0; j < 100; ++j) { - map.insert(itos(j), CountedItem(id)); - } - if (i <= 1) { - for (int j = 0; j < 100; ++j) { - map.remove(itos(j)); - } - } - if (i % 2 == 0) { - map.clear(); - } - } - - if (CountedItem::count != 0) { - OS::get_singleton()->print("%d != 0 (not performing the other test sub-cases, breaking...)\n", CountedItem::count); - break; - } - } - } - - return nullptr; -} - -} // namespace TestOAHashMap diff --git a/main/tests/test_oa_hash_map.h b/main/tests/test_oa_hash_map.h deleted file mode 100644 index eb2b3d1e99..0000000000 --- a/main/tests/test_oa_hash_map.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_oa_hash_map.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_OA_HASH_MAP_H -#define TEST_OA_HASH_MAP_H - -#include "core/os/main_loop.h" - -namespace TestOAHashMap { - -MainLoop *test(); -} - -#endif // TEST_OA_HASH_MAP_H diff --git a/main/tests/test_ordered_hash_map.cpp b/main/tests/test_ordered_hash_map.cpp deleted file mode 100644 index d18a3784be..0000000000 --- a/main/tests/test_ordered_hash_map.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/*************************************************************************/ -/* test_ordered_hash_map.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_ordered_hash_map.h" - -#include "core/ordered_hash_map.h" -#include "core/os/os.h" -#include "core/pair.h" -#include "core/vector.h" - -namespace TestOrderedHashMap { - -bool test_insert() { - OrderedHashMap<int, int> map; - OrderedHashMap<int, int>::Element e = map.insert(42, 84); - - return e && e.key() == 42 && e.get() == 84 && e.value() == 84 && map[42] == 84 && map.has(42) && map.find(42); -} - -bool test_insert_overwrite() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(42, 1234); - - return map[42] == 1234; -} - -bool test_erase_via_element() { - OrderedHashMap<int, int> map; - OrderedHashMap<int, int>::Element e = map.insert(42, 84); - - map.erase(e); - return !e && !map.has(42) && !map.find(42); -} - -bool test_erase_via_key() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.erase(42); - return !map.has(42) && !map.find(42); -} - -bool test_size() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(123, 84); - map.insert(123, 84); - map.insert(0, 84); - map.insert(123485, 84); - - return map.size() == 4; -} - -bool test_iteration() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(123, 12385); - map.insert(0, 12934); - map.insert(123485, 1238888); - map.insert(123, 111111); - - Vector<Pair<int, int>> expected; - expected.push_back(Pair<int, int>(42, 84)); - expected.push_back(Pair<int, int>(123, 111111)); - expected.push_back(Pair<int, int>(0, 12934)); - expected.push_back(Pair<int, int>(123485, 1238888)); - - int idx = 0; - for (OrderedHashMap<int, int>::Element E = map.front(); E; E = E.next()) { - if (expected[idx] != Pair<int, int>(E.key(), E.value())) { - return false; - } - ++idx; - } - return true; -} - -bool test_const_iteration(const OrderedHashMap<int, int> &map) { - Vector<Pair<int, int>> expected; - expected.push_back(Pair<int, int>(42, 84)); - expected.push_back(Pair<int, int>(123, 111111)); - expected.push_back(Pair<int, int>(0, 12934)); - expected.push_back(Pair<int, int>(123485, 1238888)); - - int idx = 0; - for (OrderedHashMap<int, int>::ConstElement E = map.front(); E; E = E.next()) { - if (expected[idx] != Pair<int, int>(E.key(), E.value())) { - return false; - } - ++idx; - } - return true; -} - -bool test_const_iteration() { - OrderedHashMap<int, int> map; - map.insert(42, 84); - map.insert(123, 12385); - map.insert(0, 12934); - map.insert(123485, 1238888); - map.insert(123, 111111); - - return test_const_iteration(map); -} - -typedef bool (*TestFunc)(); - -TestFunc test_funcs[] = { - - test_insert, - test_insert_overwrite, - test_erase_via_element, - test_erase_via_key, - test_size, - test_iteration, - test_const_iteration, - nullptr - -}; - -MainLoop *test() { - int count = 0; - int passed = 0; - - while (true) { - if (!test_funcs[count]) { - break; - } - bool pass = test_funcs[count](); - if (pass) { - passed++; - } - OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED"); - - count++; - } - - OS::get_singleton()->print("\n\n\n"); - OS::get_singleton()->print("*************\n"); - OS::get_singleton()->print("***TOTALS!***\n"); - OS::get_singleton()->print("*************\n"); - - OS::get_singleton()->print("Passed %i of %i tests\n", passed, count); - - return nullptr; -} - -} // namespace TestOrderedHashMap diff --git a/main/tests/test_ordered_hash_map.h b/main/tests/test_ordered_hash_map.h deleted file mode 100644 index f251da0ba2..0000000000 --- a/main/tests/test_ordered_hash_map.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_ordered_hash_map.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_ORDERED_HASH_MAP_H -#define TEST_ORDERED_HASH_MAP_H - -#include "core/os/main_loop.h" - -namespace TestOrderedHashMap { - -MainLoop *test(); -} - -#endif // TEST_ORDERED_HASH_MAP_H diff --git a/main/tests/test_physics_2d.cpp b/main/tests/test_physics_2d.cpp deleted file mode 100644 index 6cb9bf7b60..0000000000 --- a/main/tests/test_physics_2d.cpp +++ /dev/null @@ -1,407 +0,0 @@ -/*************************************************************************/ -/* test_physics_2d.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_physics_2d.h" - -#include "core/map.h" -#include "core/os/main_loop.h" -#include "core/os/os.h" -#include "core/print_string.h" -#include "scene/resources/texture.h" -#include "servers/display_server.h" -#include "servers/physics_server_2d.h" -#include "servers/rendering_server.h" - -static const unsigned char convex_png[] = { - 0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa, 0x0, 0x0, 0x0, 0xd, 0x49, 0x48, 0x44, 0x52, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x40, 0x8, 0x6, 0x0, 0x0, 0x0, 0xaa, 0x69, 0x71, 0xde, 0x0, 0x0, 0x0, 0x1, 0x73, 0x52, 0x47, 0x42, 0x0, 0xae, 0xce, 0x1c, 0xe9, 0x0, 0x0, 0x0, 0x6, 0x62, 0x4b, 0x47, 0x44, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf9, 0x43, 0xbb, 0x7f, 0x0, 0x0, 0x0, 0x9, 0x70, 0x48, 0x59, 0x73, 0x0, 0x0, 0xb, 0x13, 0x0, 0x0, 0xb, 0x13, 0x1, 0x0, 0x9a, 0x9c, 0x18, 0x0, 0x0, 0x0, 0x7, 0x74, 0x49, 0x4d, 0x45, 0x7, 0xdb, 0x6, 0xa, 0x3, 0x13, 0x31, 0x66, 0xa7, 0xac, 0x79, 0x0, 0x0, 0x4, 0xef, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0xed, 0x9b, 0xdd, 0x4e, 0x2a, 0x57, 0x14, 0xc7, 0xf7, 0x1e, 0xc0, 0x19, 0x38, 0x32, 0x80, 0xa, 0x6a, 0xda, 0x18, 0xa3, 0xc6, 0x47, 0x50, 0x7b, 0xa1, 0xd9, 0x36, 0x27, 0x7e, 0x44, 0xed, 0x45, 0x4d, 0x93, 0x3e, 0x40, 0x1f, 0x64, 0x90, 0xf4, 0x1, 0xbc, 0xf0, 0xc2, 0x9c, 0x57, 0x30, 0x4d, 0xbc, 0xa8, 0x6d, 0xc, 0x69, 0x26, 0xb5, 0x68, 0x8b, 0x35, 0x7e, 0x20, 0xb4, 0xf5, 0x14, 0xbf, 0x51, 0x3c, 0x52, 0xe, 0xc, 0xe, 0xc8, 0xf0, 0xb1, 0x7a, 0x51, 0x3d, 0xb1, 0x9e, 0x19, 0x1c, 0x54, 0x70, 0x1c, 0xdc, 0x9, 0x17, 0x64, 0x8, 0xc9, 0xff, 0xb7, 0xd6, 0x7f, 0xcd, 0x3f, 0x2b, 0xd9, 0x8, 0xbd, 0x9c, 0xda, 0x3e, 0xf8, 0x31, 0xff, 0xc, 0x0, 0x8, 0x42, 0x88, 0x9c, 0x9f, 0x9f, 0xbf, 0xa, 0x87, 0xc3, 0xad, 0x7d, 0x7d, 0x7d, 0x7f, 0x23, 0x84, 0x78, 0x8c, 0x31, 0xaf, 0x55, 0x0, 0xc6, 0xc7, 0x14, 0x1e, 0x8f, 0xc7, 0xbf, 0x38, 0x3c, 0x3c, 0x6c, 0x9b, 0x9f, 0x9f, 0x6f, 0xb8, 0x82, 0x9b, 0xee, 0xe8, 0xe8, 0xf8, 0x12, 0x0, 0xbe, 0xd3, 0x2a, 0x8, 0xfc, 0x50, 0xd1, 0xf9, 0x7c, 0x9e, 0x8a, 0x46, 0xa3, 0x5f, 0x9d, 0x9e, 0x9e, 0x7e, 0xb2, 0xb0, 0xb0, 0x60, 0xe5, 0x79, 0x1e, 0xf1, 0xfc, 0x7f, 0x3a, 0x9, 0x21, 0x88, 0x10, 0x82, 0x26, 0x26, 0x26, 0xde, 0x77, 0x75, 0x75, 0x85, 0x59, 0x96, 0xfd, 0x5e, 0x6b, 0x20, 0xf0, 0x7d, 0x85, 0x4b, 0x92, 0xf4, 0xfa, 0xe0, 0xe0, 0xe0, 0xd3, 0xb9, 0xb9, 0xb9, 0x46, 0x49, 0x92, 0xea, 0x6f, 0xa, 0xbf, 0x7d, 0x8, 0x21, 0x68, 0x70, 0x70, 0xb0, 0x38, 0x39, 0x39, 0x79, 0xd6, 0xd9, 0xd9, 0xb9, 0xcf, 0x30, 0xcc, 0xa2, 0xd6, 0xad, 0x21, 0x2b, 0x1c, 0x0, 0x38, 0x41, 0x10, 0xfc, 0xdb, 0xdb, 0xdb, 0x27, 0x1e, 0x8f, 0x27, 0x4b, 0x8, 0x1, 0x84, 0x90, 0xea, 0xf, 0x21, 0x4, 0x3c, 0x1e, 0x4f, 0x76, 0x67, 0x67, 0x67, 0x3f, 0x9f, 0xcf, 0xff, 0x7c, 0x5, 0xf3, 0xd9, 0x0, 0xe0, 0x2, 0x81, 0xc0, 0xa9, 0xdb, 0xed, 0x2e, 0x94, 0x2b, 0x5c, 0xe, 0xc4, 0xca, 0xca, 0x8a, 0x18, 0x8d, 0x46, 0x3, 0x0, 0xc0, 0x69, 0x1e, 0x4, 0x0, 0x90, 0x48, 0x24, 0x12, 0xe4, 0x38, 0xee, 0x41, 0xc2, 0x6f, 0x43, 0xe0, 0x38, 0xe, 0xfc, 0x7e, 0xbf, 0x10, 0x8b, 0xc5, 0xd6, 0x35, 0xd, 0x22, 0x9b, 0xcd, 0x7a, 0x96, 0x97, 0x97, 0x33, 0xf, 0xad, 0x7c, 0x29, 0x10, 0x9b, 0x9b, 0x9b, 0xef, 0x2e, 0x2e, 0x2e, 0x7e, 0xd5, 0x1c, 0x8, 0x0, 0x20, 0xe1, 0x70, 0x38, 0xfc, 0x98, 0xd5, 0x57, 0x2, 0xe1, 0x76, 0xbb, 0xf3, 0xa1, 0x50, 0xe8, 0x38, 0x9b, 0xcd, 0xfe, 0xa2, 0x9, 0x8, 0x0, 0x40, 0x2e, 0x2f, 0x2f, 0x7d, 0x4b, 0x4b, 0x4b, 0xb9, 0x4a, 0x54, 0x5f, 0x9, 0xc4, 0xd2, 0xd2, 0x92, 0xb4, 0xb7, 0xb7, 0xf7, 0x36, 0x97, 0xcb, 0x4d, 0x3d, 0x29, 0x8, 0x0, 0xe0, 0x42, 0xa1, 0xd0, 0x71, 0xb5, 0xc4, 0xdf, 0xb6, 0xc5, 0x93, 0xe, 0x4a, 0x0, 0x20, 0xa9, 0x54, 0xea, 0x37, 0xb7, 0xdb, 0x5d, 0xa8, 0xa6, 0x78, 0x39, 0x10, 0x6b, 0x6b, 0x6b, 0xf1, 0x64, 0x32, 0xb9, 0x5a, 0x55, 0x10, 0x0, 0xc0, 0x6d, 0x6c, 0x6c, 0x9c, 0x57, 0xbb, 0xfa, 0x25, 0x40, 0x14, 0x3, 0x81, 0x40, 0x34, 0x93, 0xc9, 0x2c, 0x57, 0x1c, 0x4, 0x0, 0x90, 0x58, 0x2c, 0xb6, 0x5e, 0xe9, 0xc1, 0x77, 0x1f, 0x10, 0x53, 0x53, 0x53, 0x52, 0xc5, 0x83, 0x14, 0x0, 0x70, 0x7e, 0xbf, 0x5f, 0xd0, 0x42, 0xf5, 0x95, 0x40, 0xf8, 0x7c, 0xbe, 0xcb, 0xa3, 0xa3, 0xa3, 0x3f, 0x1e, 0xbd, 0x1b, 0x0, 0x80, 0x1c, 0x1f, 0x1f, 0x87, 0xb4, 0x56, 0xfd, 0xaa, 0x5, 0x29, 0x51, 0x14, 0xbf, 0xf5, 0xf9, 0x7c, 0x97, 0x5a, 0xad, 0xbe, 0x12, 0x88, 0xf5, 0xf5, 0xf5, 0xd8, 0x83, 0x83, 0x54, 0xb5, 0x42, 0x8f, 0x66, 0x83, 0x94, 0xd6, 0xbd, 0x5f, 0xce, 0x7c, 0x38, 0x3c, 0x3c, 0xfc, 0xb3, 0x50, 0x28, 0xb8, 0xcb, 0x2, 0x1, 0x0, 0xdc, 0xf4, 0xf4, 0xf4, 0xfe, 0x73, 0x15, 0x2f, 0x17, 0xa4, 0x22, 0x91, 0x48, 0x50, 0xb5, 0x2d, 0x0, 0x80, 0x9b, 0x99, 0x99, 0x79, 0xfb, 0xdc, 0x1, 0xc8, 0x5, 0xa9, 0x44, 0x22, 0xf1, 0xfb, 0x9d, 0x10, 0x0, 0x80, 0x9b, 0x9d, 0x9d, 0xd, 0xea, 0x5, 0xc0, 0xad, 0xfd, 0x43, 0x1a, 0x0, 0xb8, 0xdb, 0x9a, 0xa9, 0x8f, 0xb6, 0xa4, 0x46, 0xa3, 0xa4, 0xb7, 0xd5, 0x37, 0xcf, 0xf3, 0x68, 0x75, 0x75, 0xf5, 0x4c, 0xee, 0x99, 0x1c, 0x80, 0x9c, 0x1e, 0xf7, 0xff, 0x16, 0x8b, 0x45, 0x50, 0x5, 0xa0, 0xb7, 0xb7, 0xb7, 0x85, 0x10, 0xa2, 0x2b, 0xf1, 0x84, 0x10, 0xd4, 0xdf, 0xdf, 0x6f, 0x57, 0x3, 0x80, 0x37, 0x18, 0xc, 0x5, 0x3d, 0x2, 0xa0, 0x69, 0x3a, 0x8b, 0x10, 0xe2, 0x4b, 0x2, 0xc0, 0x18, 0xf3, 0xc1, 0x60, 0x70, 0x47, 0x8f, 0x16, 0x38, 0x3a, 0x3a, 0x5a, 0x93, 0x5b, 0xc3, 0x7f, 0x64, 0x81, 0xba, 0xba, 0x3a, 0x49, 0x8f, 0x0, 0x1a, 0x1a, 0x1a, 0xd4, 0xcd, 0x0, 0x93, 0xc9, 0xa4, 0xcb, 0x21, 0xe8, 0x74, 0x3a, 0xd5, 0x1, 0xa0, 0x69, 0x5a, 0x77, 0x1d, 0x80, 0x31, 0x2e, 0x38, 0x9d, 0x4e, 0xb1, 0x66, 0x1, 0x30, 0xc, 0x23, 0x28, 0x3d, 0x93, 0x9b, 0x1, 0xb9, 0x9a, 0x6, 0x60, 0x36, 0x9b, 0x75, 0xd7, 0x1, 0x4a, 0x21, 0xa8, 0x26, 0x0, 0x94, 0xa, 0x41, 0xb2, 0x0, 0x18, 0x86, 0xc9, 0xe9, 0xd, 0x80, 0x52, 0x8, 0x92, 0x5, 0x60, 0xb1, 0x58, 0x74, 0x67, 0x1, 0xa5, 0x10, 0xa4, 0x4, 0x40, 0x77, 0x43, 0xd0, 0xe1, 0x70, 0xa8, 0x9f, 0x1, 0x14, 0x45, 0x1, 0x45, 0x51, 0x79, 0x3d, 0x1, 0x68, 0x6e, 0x6e, 0x4e, 0xaa, 0x6, 0x80, 0x10, 0x42, 0x6, 0x83, 0x41, 0x37, 0x36, 0x28, 0x15, 0x82, 0x6a, 0x2, 0x0, 0x4d, 0xd3, 0xa9, 0x52, 0xcf, 0x95, 0x0, 0xe8, 0x66, 0xe, 0x98, 0xcd, 0x66, 0xa1, 0x6c, 0x0, 0x7a, 0x5a, 0x8b, 0x59, 0x2c, 0x96, 0x64, 0xcd, 0x2, 0xb8, 0x2b, 0x4, 0xe9, 0xde, 0x2, 0x77, 0x85, 0xa0, 0x9a, 0xb0, 0x40, 0xa9, 0x10, 0xa4, 0x8, 0xc0, 0x64, 0x32, 0xe9, 0x6, 0x40, 0xa9, 0x10, 0x54, 0xaa, 0x3, 0x74, 0xf3, 0x16, 0x70, 0xb9, 0x5c, 0xe5, 0x3, 0xe8, 0xe9, 0xe9, 0x69, 0xd5, 0xc3, 0x66, 0x18, 0x63, 0x5c, 0x68, 0x6a, 0x6a, 0x12, 0xcb, 0x5, 0xa0, 0x9b, 0xd5, 0x38, 0x4d, 0xd3, 0x29, 0x8a, 0xa2, 0xa0, 0x2c, 0x0, 0x18, 0x63, 0x3e, 0x14, 0xa, 0xfd, 0x55, 0xb, 0x21, 0x48, 0xd1, 0x2, 0x7a, 0x59, 0x8d, 0xdf, 0x1b, 0x80, 0x1e, 0x56, 0xe3, 0x84, 0x10, 0x34, 0x30, 0x30, 0x60, 0xbb, 0xeb, 0x77, 0x46, 0x5, 0xef, 0x48, 0xcf, 0x4d, 0xec, 0x8d, 0x99, 0x5, 0xf5, 0xf5, 0xf5, 0xef, 0x46, 0x47, 0x47, 0xb, 0x2e, 0x97, 0xeb, 0xbc, 0x54, 0x8, 0x52, 0x4, 0xc0, 0x30, 0x8c, 0xf4, 0x5c, 0x4, 0x9b, 0x4c, 0xa6, 0xf4, 0xf8, 0xf8, 0xb8, 0xc8, 0xb2, 0x6c, 0x32, 0x9d, 0x4e, 0xff, 0xd4, 0xdd, 0xdd, 0x7d, 0x66, 0x34, 0x1a, 0x8b, 0xd7, 0x3, 0xfd, 0xae, 0x5b, 0x29, 0xb2, 0x57, 0x66, 0xb6, 0xb6, 0xb6, 0xde, 0xc4, 0xe3, 0xf1, 0x6f, 0xae, 0xaf, 0xc1, 0x28, 0x5d, 0x85, 0x79, 0x2, 0xc1, 0x60, 0xb5, 0x5a, 0xa3, 0xa3, 0xa3, 0xa3, 0x45, 0xab, 0xd5, 0x9a, 0x2a, 0x16, 0x8b, 0x8b, 0x6d, 0x6d, 0x6d, 0xef, 0xd5, 0x8a, 0x55, 0xd, 0x20, 0x91, 0x48, 0xbc, 0x3e, 0x38, 0x38, 0xf8, 0xda, 0x6e, 0xb7, 0xf7, 0x5f, 0x5c, 0x5c, 0xd4, 0x7b, 0xbd, 0xde, 0xbc, 0x20, 0x8, 0xcd, 0x85, 0x42, 0x81, 0xfe, 0xf0, 0xae, 0xac, 0x10, 0x98, 0x9b, 0xd5, 0xc5, 0x18, 0x17, 0x59, 0x96, 0x3d, 0x1d, 0x19, 0x19, 0x1, 0x96, 0x65, 0x5, 0x8a, 0xa2, 0x7e, 0x6c, 0x69, 0x69, 0x49, 0x3d, 0x44, 0xb0, 0x2a, 0x0, 0x1f, 0xcc, 0x74, 0x75, 0x41, 0xea, 0xfa, 0x7b, 0x32, 0x99, 0x64, 0x76, 0x77, 0x77, 0x5d, 0xe, 0x87, 0xa3, 0x5f, 0x14, 0xc5, 0x57, 0x57, 0x60, 0x5a, 0x8b, 0xc5, 0xa2, 0xf1, 0xbe, 0x50, 0x6e, 0xa, 0x66, 0x18, 0x26, 0x31, 0x36, 0x36, 0x96, 0x65, 0x59, 0x36, 0x29, 0x49, 0x92, 0xb7, 0xbd, 0xbd, 0xfd, 0x9f, 0x72, 0xda, 0xf9, 0xd1, 0x1, 0xa8, 0x1, 0x93, 0xcf, 0xe7, 0xa9, 0x93, 0x93, 0x13, 0x1b, 0x4d, 0xd3, 0x9f, 0xb, 0x82, 0x60, 0xf5, 0x7a, 0xbd, 0xd9, 0x54, 0x2a, 0xe5, 0xcc, 0x64, 0x32, 0xe, 0xb9, 0x6e, 0xb9, 0x16, 0x8c, 0x31, 0x2e, 0xda, 0x6c, 0xb6, 0xc8, 0xd0, 0xd0, 0x10, 0x65, 0xb3, 0xd9, 0x92, 0x95, 0xa8, 0x6e, 0xc5, 0x0, 0xa8, 0xe9, 0x96, 0x68, 0x34, 0x6a, 0xdd, 0xdf, 0xdf, 0x6f, 0x76, 0xb9, 0x5c, 0x9f, 0x89, 0xa2, 0x58, 0xbf, 0xb8, 0xb8, 0x8, 0x26, 0x93, 0x29, 0x3b, 0x3c, 0x3c, 0x8c, 0xed, 0x76, 0x7b, 0xd2, 0x68, 0x34, 0xfe, 0xd0, 0xd8, 0xd8, 0x98, 0xae, 0xb6, 0xe0, 0x8a, 0x1, 0x50, 0xb, 0xe6, 0xa9, 0x5, 0xbf, 0x9c, 0x97, 0xf3, 0xff, 0xf3, 0x2f, 0x6a, 0x82, 0x7f, 0xf6, 0x4e, 0xca, 0x1b, 0xf5, 0x0, 0x0, 0x0, 0x0, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 -}; - -class TestPhysics2DMainLoop : public MainLoop { - GDCLASS(TestPhysics2DMainLoop, MainLoop); - - RID circle_img; - RID circle_shape; - RID space; - RID canvas; - RID ray; - RID ray_query; - Transform2D view_xform; - - Vector2 ray_from, ray_to; - - struct BodyShapeData { - RID image; - RID shape; - }; - - BodyShapeData body_shape_data[8]; - - void _create_body_shape_data() { - RenderingServer *vs = RenderingServer::get_singleton(); - PhysicsServer2D *ps = PhysicsServer2D::get_singleton(); - - // SEGMENT - - { - Vector<uint8_t> pixels; - pixels.resize(32 * 2 * 2); - for (int i = 0; i < 2; i++) { - for (int j = 0; j < 32; j++) { - pixels.set(i * 32 * 2 + j * 2 + 0, (j == 0) ? 255 : 0); - pixels.set(i * 32 * 2 + j * 2 + 1, 255); - } - } - - Ref<Image> image = memnew(Image(32, 2, 0, Image::FORMAT_LA8, pixels)); - - body_shape_data[PhysicsServer2D::SHAPE_SEGMENT].image = vs->texture_2d_create(image); - - RID segment_shape = ps->segment_shape_create(); - Rect2 sg(Point2(-16, 0), Point2(16, 0)); - ps->shape_set_data(segment_shape, sg); - - body_shape_data[PhysicsServer2D::SHAPE_SEGMENT].shape = segment_shape; - } - // CIRCLE - - { - Vector<uint8_t> pixels; - pixels.resize(32 * 32 * 2); - for (int i = 0; i < 32; i++) { - for (int j = 0; j < 32; j++) { - bool black = Vector2(i - 16, j - 16).length_squared() < 16 * 16; - - pixels.set(i * 32 * 2 + j * 2 + 0, (i == 16 || j == 16) ? 255 : 0); - pixels.set(i * 32 * 2 + j * 2 + 1, black ? 255 : 0); - } - } - - Ref<Image> image = memnew(Image(32, 32, 0, Image::FORMAT_LA8, pixels)); - - body_shape_data[PhysicsServer2D::SHAPE_CIRCLE].image = vs->texture_2d_create(image); - - RID circle_shape = ps->circle_shape_create(); - ps->shape_set_data(circle_shape, 16); - - body_shape_data[PhysicsServer2D::SHAPE_CIRCLE].shape = circle_shape; - } - - // BOX - - { - Vector<uint8_t> pixels; - pixels.resize(32 * 32 * 2); - for (int i = 0; i < 32; i++) { - for (int j = 0; j < 32; j++) { - bool black = i > 0 && i < 31 && j > 0 && j < 31; - - pixels.set(i * 32 * 2 + j * 2 + 0, black ? 0 : 255); - pixels.set(i * 32 * 2 + j * 2 + 1, 255); - } - } - - Ref<Image> image = memnew(Image(32, 32, 0, Image::FORMAT_LA8, pixels)); - - body_shape_data[PhysicsServer2D::SHAPE_RECTANGLE].image = vs->texture_2d_create(image); - - RID rectangle_shape = ps->rectangle_shape_create(); - ps->shape_set_data(rectangle_shape, Vector2(16, 16)); - - body_shape_data[PhysicsServer2D::SHAPE_RECTANGLE].shape = rectangle_shape; - } - - // CAPSULE - - { - Vector<uint8_t> pixels; - pixels.resize(32 * 64 * 2); - for (int i = 0; i < 64; i++) { - for (int j = 0; j < 32; j++) { - int si = i > 48 ? i - 32 : (i < 16 ? i : 16); - bool black = Vector2(si - 16, j - 16).length_squared() < 16 * 16; - - pixels.set(i * 32 * 2 + j * 2 + 0, (i == 16 || j == 16 || i == 48) ? 255 : 0); - pixels.set(i * 32 * 2 + j * 2 + 1, black ? 255 : 0); - } - } - - Ref<Image> image = memnew(Image(32, 64, 0, Image::FORMAT_LA8, pixels)); - - body_shape_data[PhysicsServer2D::SHAPE_CAPSULE].image = vs->texture_2d_create(image); - - RID capsule_shape = ps->capsule_shape_create(); - ps->shape_set_data(capsule_shape, Vector2(16, 32)); - - body_shape_data[PhysicsServer2D::SHAPE_CAPSULE].shape = capsule_shape; - } - - // CONVEX - - { - Ref<Image> image = memnew(Image(convex_png)); - - body_shape_data[PhysicsServer2D::SHAPE_CONVEX_POLYGON].image = vs->texture_2d_create(image); - - RID convex_polygon_shape = ps->convex_polygon_shape_create(); - - Vector<Vector2> arr; - Point2 sb(32, 32); - arr.push_back(Point2(20, 3) - sb); - arr.push_back(Point2(58, 23) - sb); - arr.push_back(Point2(55, 54) - sb); - arr.push_back(Point2(27, 60) - sb); - arr.push_back(Point2(5, 56) - sb); - arr.push_back(Point2(4, 20) - sb); - arr.push_back(Point2(11, 7) - sb); - ps->shape_set_data(convex_polygon_shape, arr); - - body_shape_data[PhysicsServer2D::SHAPE_CONVEX_POLYGON].shape = convex_polygon_shape; - } - } - - void _do_ray_query() { - /* - PhysicsServer2D *ps = PhysicsServer2D::get_singleton(); - ps->query_intersection_segment(ray_query,ray_from,ray_to); - */ - } - -protected: - void input_event(const Ref<InputEvent> &p_event) { - Ref<InputEventMouseButton> mb = p_event; - - if (mb.is_valid()) { - if (mb->is_pressed()) { - Point2 p(mb->get_position().x, mb->get_position().y); - - if (mb->get_button_index() == 1) { - ray_to = p; - _do_ray_query(); - } else if (mb->get_button_index() == 2) { - ray_from = p; - _do_ray_query(); - } - } - } - - Ref<InputEventMouseMotion> mm = p_event; - - if (mm.is_valid()) { - Point2 p = mm->get_position(); - - if (mm->get_button_mask() & BUTTON_MASK_LEFT) { - ray_to = p; - _do_ray_query(); - } else if (mm->get_button_mask() & BUTTON_MASK_RIGHT) { - ray_from = p; - _do_ray_query(); - } - } - } - - RID _add_body(PhysicsServer2D::ShapeType p_shape, const Transform2D &p_xform) { - RenderingServer *vs = RenderingServer::get_singleton(); - PhysicsServer2D *ps = PhysicsServer2D::get_singleton(); - - RID body = ps->body_create(); - ps->body_add_shape(body, body_shape_data[p_shape].shape); - ps->body_set_space(body, space); - ps->body_set_continuous_collision_detection_mode(body, PhysicsServer2D::CCD_MODE_CAST_SHAPE); - ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, p_xform); - - //print_line("add body with xform: "+p_xform); - RID sprite = vs->canvas_item_create(); - vs->canvas_item_set_parent(sprite, canvas); - vs->canvas_item_set_transform(sprite, p_xform); - Size2 imgsize(5, 5); //vs->texture_get_width(body_shape_data[p_shape].image), vs->texture_get_height(body_shape_data[p_shape].image)); - vs->canvas_item_add_texture_rect(sprite, Rect2(-imgsize / 2.0, imgsize), body_shape_data[p_shape].image); - - ps->body_set_force_integration_callback(body, this, "_body_moved", sprite); - //RID q = ps->query_create(this,"_body_moved",sprite); - //ps->query_body_state(q,body); - - return body; - } - - void _add_plane(const Vector2 &p_normal, real_t p_d) { - PhysicsServer2D *ps = PhysicsServer2D::get_singleton(); - - Array arr; - arr.push_back(p_normal); - arr.push_back(p_d); - - RID plane = ps->line_shape_create(); - ps->shape_set_data(plane, arr); - - RID plane_body = ps->body_create(); - ps->body_set_mode(plane_body, PhysicsServer2D::BODY_MODE_STATIC); - ps->body_set_space(plane_body, space); - ps->body_add_shape(plane_body, plane); - } - - void _add_concave(const Vector<Vector2> &p_points, const Transform2D &p_xform = Transform2D()) { - PhysicsServer2D *ps = PhysicsServer2D::get_singleton(); - RenderingServer *vs = RenderingServer::get_singleton(); - - RID concave = ps->concave_polygon_shape_create(); - ps->shape_set_data(concave, p_points); - RID body = ps->body_create(); - ps->body_set_mode(body, PhysicsServer2D::BODY_MODE_STATIC); - ps->body_set_space(body, space); - ps->body_add_shape(body, concave); - ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, p_xform); - - RID sprite = vs->canvas_item_create(); - vs->canvas_item_set_parent(sprite, canvas); - vs->canvas_item_set_transform(sprite, p_xform); - for (int i = 0; i < p_points.size(); i += 2) { - vs->canvas_item_add_line(sprite, p_points[i], p_points[i + 1], Color(0, 0, 0), 2); - } - } - - void _body_moved(Object *p_state, RID p_sprite) { - PhysicsDirectBodyState2D *state = (PhysicsDirectBodyState2D *)p_state; - RenderingServer::get_singleton()->canvas_item_set_transform(p_sprite, state->get_transform()); - } - - void _ray_query_callback(const RID &p_rid, ObjectID p_id, int p_shape, const Vector2 &p_point, const Vector2 &p_normal) { - Vector2 ray_end; - - if (p_rid.is_valid()) { - ray_end = p_point; - } else { - ray_end = ray_to; - } - - RenderingServer *vs = RenderingServer::get_singleton(); - - vs->canvas_item_clear(ray); - vs->canvas_item_add_line(ray, ray_from, ray_end, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2); - if (p_rid.is_valid()) { - vs->canvas_item_add_line(ray, ray_end, ray_end + p_normal * 20, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2); - } - } - - static void _bind_methods() { - ClassDB::bind_method(D_METHOD("_body_moved"), &TestPhysics2DMainLoop::_body_moved); - ClassDB::bind_method(D_METHOD("_ray_query_callback"), &TestPhysics2DMainLoop::_ray_query_callback); - } - -public: - virtual void init() { - RenderingServer *vs = RenderingServer::get_singleton(); - PhysicsServer2D *ps = PhysicsServer2D::get_singleton(); - - space = ps->space_create(); - ps->space_set_active(space, true); - ps->set_active(true); - ps->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR, Vector2(0, 1)); - ps->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY, 98); - - { - RID vp = vs->viewport_create(); - canvas = vs->canvas_create(); - - Size2i screen_size = DisplayServer::get_singleton()->window_get_size(); - vs->viewport_attach_canvas(vp, canvas); - vs->viewport_set_size(vp, screen_size.x, screen_size.y); - vs->viewport_attach_to_screen(vp, Rect2(Vector2(), screen_size)); - vs->viewport_set_active(vp, true); - - Transform2D smaller; - //smaller.scale(Vector2(0.6,0.6)); - //smaller.elements[2]=Vector2(100,0); - - //view_xform = smaller; - vs->viewport_set_canvas_transform(vp, canvas, view_xform); - } - - ray = vs->canvas_item_create(); - vs->canvas_item_set_parent(ray, canvas); - //ray_query = ps->query_create(this,"_ray_query_callback",Variant()); - //ps->query_intersection(ray_query,space); - - _create_body_shape_data(); - - for (int i = 0; i < 32; i++) { - PhysicsServer2D::ShapeType types[4] = { - PhysicsServer2D::SHAPE_CIRCLE, - PhysicsServer2D::SHAPE_CAPSULE, - PhysicsServer2D::SHAPE_RECTANGLE, - PhysicsServer2D::SHAPE_CONVEX_POLYGON, - - }; - - PhysicsServer2D::ShapeType type = types[i % 4]; - //type=PhysicsServer2D::SHAPE_SEGMENT; - _add_body(type, Transform2D(i * 0.8, Point2(152 + i * 40, 100 - 40 * i))); - /* - if (i==0) - ps->body_set_mode(b,PhysicsServer2D::BODY_MODE_STATIC); - */ - } - - //RID b= _add_body(PhysicsServer2D::SHAPE_CIRCLE,Transform2D(0,Point2(101,140))); - //ps->body_set_mode(b,PhysicsServer2D::BODY_MODE_STATIC); - - Point2 prev; - - Vector<Point2> parr; - for (int i = 0; i < 30; i++) { - Point2 p(i * 60, Math::randf() * 70 + 340); - if (i > 0) { - parr.push_back(prev); - parr.push_back(p); - } - prev = p; - } - - _add_concave(parr); - //_add_plane(Vector2(0.0,-1).normalized(),-300); - //_add_plane(Vector2(1,0).normalized(),50); - //_add_plane(Vector2(-1,0).normalized(),-600); - } - - virtual bool idle(float p_time) { - return false; - } - virtual void finish() { - } - - TestPhysics2DMainLoop() {} -}; - -namespace TestPhysics2D { - -MainLoop *test() { - return memnew(TestPhysics2DMainLoop); -} - -} // namespace TestPhysics2D diff --git a/main/tests/test_physics_2d.h b/main/tests/test_physics_2d.h deleted file mode 100644 index 517d324f3b..0000000000 --- a/main/tests/test_physics_2d.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_physics_2d.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_PHYSICS_2D_H -#define TEST_PHYSICS_2D_H - -#include "core/os/main_loop.h" - -namespace TestPhysics2D { - -MainLoop *test(); -} - -#endif // TEST_PHYSICS_2D_H diff --git a/main/tests/test_physics_3d.cpp b/main/tests/test_physics_3d.cpp deleted file mode 100644 index fe54ece98e..0000000000 --- a/main/tests/test_physics_3d.cpp +++ /dev/null @@ -1,413 +0,0 @@ -/*************************************************************************/ -/* test_physics_3d.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_physics_3d.h" - -#include "core/map.h" -#include "core/math/math_funcs.h" -#include "core/math/quick_hull.h" -#include "core/os/main_loop.h" -#include "core/os/os.h" -#include "core/print_string.h" -#include "servers/display_server.h" -#include "servers/physics_server_3d.h" -#include "servers/rendering_server.h" - -class TestPhysics3DMainLoop : public MainLoop { - GDCLASS(TestPhysics3DMainLoop, MainLoop); - - enum { - LINK_COUNT = 20, - }; - - RID test_cube; - - RID plane; - RID sphere; - RID light; - RID camera; - RID mover; - RID scenario; - RID space; - - RID character; - - float ofs_x, ofs_y; - - Point2 joy_direction; - - List<RID> bodies; - Map<PhysicsServer3D::ShapeType, RID> type_shape_map; - Map<PhysicsServer3D::ShapeType, RID> type_mesh_map; - - void body_changed_transform(Object *p_state, RID p_visual_instance) { - PhysicsDirectBodyState3D *state = (PhysicsDirectBodyState3D *)p_state; - RenderingServer *vs = RenderingServer::get_singleton(); - Transform t = state->get_transform(); - vs->instance_set_transform(p_visual_instance, t); - } - - bool quit; - -protected: - static void _bind_methods() { - ClassDB::bind_method("body_changed_transform", &TestPhysics3DMainLoop::body_changed_transform); - } - - RID create_body(PhysicsServer3D::ShapeType p_shape, PhysicsServer3D::BodyMode p_body, const Transform p_location, bool p_active_default = true, const Transform &p_shape_xform = Transform()) { - RenderingServer *vs = RenderingServer::get_singleton(); - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - - RID mesh_instance = vs->instance_create2(type_mesh_map[p_shape], scenario); - RID body = ps->body_create(p_body, !p_active_default); - ps->body_set_space(body, space); - ps->body_set_param(body, PhysicsServer3D::BODY_PARAM_BOUNCE, 0.0); - //todo set space - ps->body_add_shape(body, type_shape_map[p_shape]); - ps->body_set_force_integration_callback(body, this, "body_changed_transform", mesh_instance); - - ps->body_set_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM, p_location); - bodies.push_back(body); - - if (p_body == PhysicsServer3D::BODY_MODE_STATIC) { - vs->instance_set_transform(mesh_instance, p_location); - } - return body; - } - - RID create_static_plane(const Plane &p_plane) { - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - - RID world_margin_shape = ps->shape_create(PhysicsServer3D::SHAPE_PLANE); - ps->shape_set_data(world_margin_shape, p_plane); - - RID b = ps->body_create(PhysicsServer3D::BODY_MODE_STATIC); - ps->body_set_space(b, space); - //todo set space - ps->body_add_shape(b, world_margin_shape); - return b; - } - - void configure_body(RID p_body, float p_mass, float p_friction, float p_bounce) { - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_MASS, p_mass); - ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_FRICTION, p_friction); - ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_BOUNCE, p_bounce); - } - - void init_shapes() { - RenderingServer *vs = RenderingServer::get_singleton(); - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - - /* SPHERE SHAPE */ - RID sphere_mesh = vs->make_sphere_mesh(10, 20, 0.5); - type_mesh_map[PhysicsServer3D::SHAPE_SPHERE] = sphere_mesh; - - RID sphere_shape = ps->shape_create(PhysicsServer3D::SHAPE_SPHERE); - ps->shape_set_data(sphere_shape, 0.5); - type_shape_map[PhysicsServer3D::SHAPE_SPHERE] = sphere_shape; - - /* BOX SHAPE */ - - Vector<Plane> box_planes = Geometry::build_box_planes(Vector3(0.5, 0.5, 0.5)); - RID box_mesh = vs->mesh_create(); - Geometry::MeshData box_data = Geometry::build_convex_mesh(box_planes); - vs->mesh_add_surface_from_mesh_data(box_mesh, box_data); - type_mesh_map[PhysicsServer3D::SHAPE_BOX] = box_mesh; - - RID box_shape = ps->shape_create(PhysicsServer3D::SHAPE_BOX); - ps->shape_set_data(box_shape, Vector3(0.5, 0.5, 0.5)); - type_shape_map[PhysicsServer3D::SHAPE_BOX] = box_shape; - - /* CAPSULE SHAPE */ - - Vector<Plane> capsule_planes = Geometry::build_capsule_planes(0.5, 0.7, 12, Vector3::AXIS_Z); - - RID capsule_mesh = vs->mesh_create(); - Geometry::MeshData capsule_data = Geometry::build_convex_mesh(capsule_planes); - vs->mesh_add_surface_from_mesh_data(capsule_mesh, capsule_data); - - type_mesh_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_mesh; - - RID capsule_shape = ps->shape_create(PhysicsServer3D::SHAPE_CAPSULE); - Dictionary capsule_params; - capsule_params["radius"] = 0.5; - capsule_params["height"] = 1.4; - ps->shape_set_data(capsule_shape, capsule_params); - type_shape_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_shape; - - /* CONVEX SHAPE */ - - Vector<Plane> convex_planes = Geometry::build_cylinder_planes(0.5, 0.7, 5, Vector3::AXIS_Z); - - RID convex_mesh = vs->mesh_create(); - Geometry::MeshData convex_data = Geometry::build_convex_mesh(convex_planes); - QuickHull::build(convex_data.vertices, convex_data); - vs->mesh_add_surface_from_mesh_data(convex_mesh, convex_data); - - type_mesh_map[PhysicsServer3D::SHAPE_CONVEX_POLYGON] = convex_mesh; - - RID convex_shape = ps->shape_create(PhysicsServer3D::SHAPE_CONVEX_POLYGON); - ps->shape_set_data(convex_shape, convex_data.vertices); - type_shape_map[PhysicsServer3D::SHAPE_CONVEX_POLYGON] = convex_shape; - } - - void make_trimesh(Vector<Vector3> p_faces, const Transform &p_xform = Transform()) { - RenderingServer *vs = RenderingServer::get_singleton(); - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - RID trimesh_shape = ps->shape_create(PhysicsServer3D::SHAPE_CONCAVE_POLYGON); - ps->shape_set_data(trimesh_shape, p_faces); - p_faces = ps->shape_get_data(trimesh_shape); // optimized one - Vector<Vector3> normals; // for drawing - for (int i = 0; i < p_faces.size() / 3; i++) { - Plane p(p_faces[i * 3 + 0], p_faces[i * 3 + 1], p_faces[i * 3 + 2]); - normals.push_back(p.normal); - normals.push_back(p.normal); - normals.push_back(p.normal); - } - - RID trimesh_mesh = vs->mesh_create(); - Array d; - d.resize(RS::ARRAY_MAX); - d[RS::ARRAY_VERTEX] = p_faces; - d[RS::ARRAY_NORMAL] = normals; - vs->mesh_add_surface_from_arrays(trimesh_mesh, RS::PRIMITIVE_TRIANGLES, d); - - RID triins = vs->instance_create2(trimesh_mesh, scenario); - - RID tribody = ps->body_create(PhysicsServer3D::BODY_MODE_STATIC); - ps->body_set_space(tribody, space); - //todo set space - ps->body_add_shape(tribody, trimesh_shape); - Transform tritrans = p_xform; - ps->body_set_state(tribody, PhysicsServer3D::BODY_STATE_TRANSFORM, tritrans); - vs->instance_set_transform(triins, tritrans); - } - - void make_grid(int p_width, int p_height, float p_cellsize, float p_cellheight, const Transform &p_xform = Transform()) { - Vector<Vector<float>> grid; - - grid.resize(p_width); - - for (int i = 0; i < p_width; i++) { - grid.write[i].resize(p_height); - - for (int j = 0; j < p_height; j++) { - grid.write[i].write[j] = 1.0 + Math::random(-p_cellheight, p_cellheight); - } - } - - Vector<Vector3> faces; - - for (int i = 1; i < p_width; i++) { - for (int j = 1; j < p_height; j++) { -#define MAKE_VERTEX(m_x, m_z) \ - faces.push_back(Vector3((m_x - p_width / 2) * p_cellsize, grid[m_x][m_z], (m_z - p_height / 2) * p_cellsize)) - - MAKE_VERTEX(i, j - 1); - MAKE_VERTEX(i, j); - MAKE_VERTEX(i - 1, j); - - MAKE_VERTEX(i - 1, j - 1); - MAKE_VERTEX(i, j - 1); - MAKE_VERTEX(i - 1, j); - } - } - - make_trimesh(faces, p_xform); - } - -public: - virtual void input_event(const Ref<InputEvent> &p_event) { - Ref<InputEventMouseMotion> mm = p_event; - if (mm.is_valid() && mm->get_button_mask() & 4) { - ofs_y -= mm->get_relative().y / 200.0; - ofs_x += mm->get_relative().x / 200.0; - } - - if (mm.is_valid() && mm->get_button_mask() & 1) { - float y = -mm->get_relative().y / 20.0; - float x = mm->get_relative().x / 20.0; - - if (mover.is_valid()) { - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - Transform t = ps->body_get_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM); - t.origin += Vector3(x, y, 0); - - ps->body_set_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM, t); - } - } - } - - virtual void request_quit() { - quit = true; - } - virtual void init() { - ofs_x = ofs_y = 0; - init_shapes(); - - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - space = ps->space_create(); - ps->space_set_active(space, true); - - RenderingServer *vs = RenderingServer::get_singleton(); - - /* LIGHT */ - RID lightaux = vs->directional_light_create(); - scenario = vs->scenario_create(); - vs->light_set_shadow(lightaux, true); - light = vs->instance_create2(lightaux, scenario); - Transform t; - t.rotate(Vector3(1.0, 0, 0), 0.6); - vs->instance_set_transform(light, t); - - /* CAMERA */ - - camera = vs->camera_create(); - - RID viewport = vs->viewport_create(); - Size2i screen_size = DisplayServer::get_singleton()->window_get_size(); - vs->viewport_set_size(viewport, screen_size.x, screen_size.y); - vs->viewport_attach_to_screen(viewport, Rect2(Vector2(), screen_size)); - vs->viewport_set_active(viewport, true); - vs->viewport_attach_camera(viewport, camera); - vs->viewport_set_scenario(viewport, scenario); - - vs->camera_set_perspective(camera, 60, 0.1, 40.0); - vs->camera_set_transform(camera, Transform(Basis(), Vector3(0, 9, 12))); - - Transform gxf; - gxf.basis.scale(Vector3(1.4, 0.4, 1.4)); - gxf.origin = Vector3(-2, 1, -2); - make_grid(5, 5, 2.5, 1, gxf); - test_fall(); - quit = false; - } - virtual bool iteration(float p_time) { - if (mover.is_valid()) { - static float joy_speed = 10; - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - Transform t = ps->body_get_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM); - t.origin += Vector3(joy_speed * joy_direction.x * p_time, -joy_speed * joy_direction.y * p_time, 0); - ps->body_set_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM, t); - }; - - Transform cameratr; - cameratr.rotate(Vector3(0, 1, 0), ofs_x); - cameratr.rotate(Vector3(1, 0, 0), -ofs_y); - cameratr.translate(Vector3(0, 2, 8)); - RenderingServer *vs = RenderingServer::get_singleton(); - vs->camera_set_transform(camera, cameratr); - - return quit; - } - virtual void finish() { - } - - void test_joint() { - } - - void test_hinge() { - } - - void test_character() { - RenderingServer *vs = RenderingServer::get_singleton(); - PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); - - Vector<Plane> capsule_planes = Geometry::build_capsule_planes(0.5, 1, 12, 5, Vector3::AXIS_Y); - - RID capsule_mesh = vs->mesh_create(); - Geometry::MeshData capsule_data = Geometry::build_convex_mesh(capsule_planes); - vs->mesh_add_surface_from_mesh_data(capsule_mesh, capsule_data); - type_mesh_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_mesh; - - RID capsule_shape = ps->shape_create(PhysicsServer3D::SHAPE_CAPSULE); - Dictionary capsule_params; - capsule_params["radius"] = 0.5; - capsule_params["height"] = 1; - Transform shape_xform; - shape_xform.rotate(Vector3(1, 0, 0), Math_PI / 2.0); - //shape_xform.origin=Vector3(1,1,1); - ps->shape_set_data(capsule_shape, capsule_params); - - RID mesh_instance = vs->instance_create2(capsule_mesh, scenario); - character = ps->body_create(PhysicsServer3D::BODY_MODE_CHARACTER); - ps->body_set_space(character, space); - //todo add space - ps->body_add_shape(character, capsule_shape); - - ps->body_set_force_integration_callback(character, this, "body_changed_transform", mesh_instance); - - ps->body_set_state(character, PhysicsServer3D::BODY_STATE_TRANSFORM, Transform(Basis(), Vector3(-2, 5, -2))); - bodies.push_back(character); - } - - void test_fall() { - for (int i = 0; i < 35; i++) { - static const PhysicsServer3D::ShapeType shape_idx[] = { - PhysicsServer3D::SHAPE_CAPSULE, - PhysicsServer3D::SHAPE_BOX, - PhysicsServer3D::SHAPE_SPHERE, - PhysicsServer3D::SHAPE_CONVEX_POLYGON - }; - - PhysicsServer3D::ShapeType type = shape_idx[i % 4]; - - Transform t; - - t.origin = Vector3(0.0 * i, 3.5 + 1.1 * i, 0.7 + 0.0 * i); - t.basis.rotate(Vector3(0.2, -1, 0), Math_PI / 2 * 0.6); - - create_body(type, PhysicsServer3D::BODY_MODE_RIGID, t); - } - - create_static_plane(Plane(Vector3(0, 1, 0), -1)); - } - - void test_activate() { - create_body(PhysicsServer3D::SHAPE_BOX, PhysicsServer3D::BODY_MODE_RIGID, Transform(Basis(), Vector3(0, 2, 0)), true); - create_static_plane(Plane(Vector3(0, 1, 0), -1)); - } - - virtual bool idle(float p_time) { - return false; - } - - TestPhysics3DMainLoop() { - } -}; - -namespace TestPhysics3D { - -MainLoop *test() { - return memnew(TestPhysics3DMainLoop); -} - -} // namespace TestPhysics3D diff --git a/main/tests/test_physics_3d.h b/main/tests/test_physics_3d.h deleted file mode 100644 index d03f2c6573..0000000000 --- a/main/tests/test_physics_3d.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_physics_3d.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_PHYSICS_H -#define TEST_PHYSICS_H - -#include "core/os/main_loop.h" - -namespace TestPhysics3D { - -MainLoop *test(); -} - -#endif diff --git a/main/tests/test_render.cpp b/main/tests/test_render.cpp deleted file mode 100644 index afc09374b9..0000000000 --- a/main/tests/test_render.cpp +++ /dev/null @@ -1,240 +0,0 @@ -/*************************************************************************/ -/* test_render.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_render.h" - -#include "core/math/math_funcs.h" -#include "core/math/quick_hull.h" -#include "core/os/keyboard.h" -#include "core/os/main_loop.h" -#include "core/os/os.h" -#include "core/print_string.h" -#include "servers/display_server.h" -#include "servers/rendering_server.h" - -#define OBJECT_COUNT 50 - -namespace TestRender { - -class TestMainLoop : public MainLoop { - RID test_cube; - RID instance; - RID camera; - RID viewport; - RID light; - RID scenario; - - struct InstanceInfo { - RID instance; - Transform base; - Vector3 rot_axis; - }; - - List<InstanceInfo> instances; - - float ofs; - bool quit; - -protected: -public: - virtual void input_event(const Ref<InputEvent> &p_event) { - if (p_event->is_pressed()) { - quit = true; - } - } - - virtual void init() { - print_line("INITIALIZING TEST RENDER"); - RenderingServer *vs = RenderingServer::get_singleton(); - test_cube = vs->get_test_cube(); - scenario = vs->scenario_create(); - - Vector<Vector3> vts; - - /* - Vector<Plane> sp = Geometry::build_sphere_planes(2,5,5); - Geometry::MeshData md2 = Geometry::build_convex_mesh(sp); - vts=md2.vertices; -*/ - /* - - static const int s = 20; - for(int i=0;i<s;i++) { - Basis rot(Vector3(0,1,0),i*Math_PI/s); - - for(int j=0;j<s;j++) { - Vector3 v; - v.x=Math::sin(j*Math_PI*2/s); - v.y=Math::cos(j*Math_PI*2/s); - - vts.push_back( rot.xform(v*2 ) ); - } - }*/ - /*for(int i=0;i<100;i++) { - - vts.push_back( Vector3(Math::randf()*2-1.0,Math::randf()*2-1.0,Math::randf()*2-1.0).normalized()*2); - }*/ - /* - vts.push_back(Vector3(0,0,1)); - vts.push_back(Vector3(0,0,-1)); - vts.push_back(Vector3(0,1,0)); - vts.push_back(Vector3(0,-1,0)); - vts.push_back(Vector3(1,0,0)); - vts.push_back(Vector3(-1,0,0));*/ - - vts.push_back(Vector3(1, 1, 1)); - vts.push_back(Vector3(1, -1, 1)); - vts.push_back(Vector3(-1, 1, 1)); - vts.push_back(Vector3(-1, -1, 1)); - vts.push_back(Vector3(1, 1, -1)); - vts.push_back(Vector3(1, -1, -1)); - vts.push_back(Vector3(-1, 1, -1)); - vts.push_back(Vector3(-1, -1, -1)); - - Geometry::MeshData md; - Error err = QuickHull::build(vts, md); - print_line("ERR: " + itos(err)); - test_cube = vs->mesh_create(); - vs->mesh_add_surface_from_mesh_data(test_cube, md); - //vs->scenario_set_debug(scenario,RS::SCENARIO_DEBUG_WIREFRAME); - - /* - RID sm = vs->shader_create(); - //vs->shader_set_fragment_code(sm,"OUT_ALPHA=mod(TIME,1);"); - //vs->shader_set_vertex_code(sm,"OUT_VERTEX=IN_VERTEX*mod(TIME,1);"); - vs->shader_set_fragment_code(sm,"OUT_DIFFUSE=vec3(1,0,1);OUT_GLOW=abs(sin(TIME));"); - RID tcmat = vs->mesh_surface_get_material(test_cube,0); - vs->material_set_shader(tcmat,sm); - */ - - List<String> cmdline = OS::get_singleton()->get_cmdline_args(); - int object_count = OBJECT_COUNT; - if (cmdline.size() > 0 && cmdline[cmdline.size() - 1].to_int()) { - object_count = cmdline[cmdline.size() - 1].to_int(); - }; - - for (int i = 0; i < object_count; i++) { - InstanceInfo ii; - - ii.instance = vs->instance_create2(test_cube, scenario); - - ii.base.translate(Math::random(-20, 20), Math::random(-20, 20), Math::random(-20, 18)); - ii.base.rotate(Vector3(0, 1, 0), Math::randf() * Math_PI); - ii.base.rotate(Vector3(1, 0, 0), Math::randf() * Math_PI); - vs->instance_set_transform(ii.instance, ii.base); - - ii.rot_axis = Vector3(Math::random(-1, 1), Math::random(-1, 1), Math::random(-1, 1)).normalized(); - - instances.push_back(ii); - } - - camera = vs->camera_create(); - - // vs->camera_set_perspective( camera, 60.0,0.1, 100.0 ); - - viewport = vs->viewport_create(); - Size2i screen_size = DisplayServer::get_singleton()->window_get_size(); - vs->viewport_set_size(viewport, screen_size.x, screen_size.y); - vs->viewport_attach_to_screen(viewport, Rect2(Vector2(), screen_size)); - vs->viewport_set_active(viewport, true); - vs->viewport_attach_camera(viewport, camera); - vs->viewport_set_scenario(viewport, scenario); - vs->camera_set_transform(camera, Transform(Basis(), Vector3(0, 3, 30))); - vs->camera_set_perspective(camera, 60, 0.1, 1000); - - /* - RID lightaux = vs->light_create( RenderingServer::LIGHT_OMNI ); - vs->light_set_var( lightaux, RenderingServer::LIGHT_VAR_RADIUS, 80 ); - vs->light_set_var( lightaux, RenderingServer::LIGHT_VAR_ATTENUATION, 1 ); - vs->light_set_var( lightaux, RenderingServer::LIGHT_VAR_ENERGY, 1.5 ); - light = vs->instance_create( lightaux ); - */ - RID lightaux; - - lightaux = vs->directional_light_create(); - //vs->light_set_color( lightaux, RenderingServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) ); - vs->light_set_color(lightaux, Color(1.0, 1.0, 1.0)); - //vs->light_set_shadow( lightaux, true ); - light = vs->instance_create2(lightaux, scenario); - Transform lla; - //lla.set_look_at(Vector3(),Vector3(1,-1,1),Vector3(0,1,0)); - lla.set_look_at(Vector3(), Vector3(-0.000000, -0.836026, -0.548690), Vector3(0, 1, 0)); - - vs->instance_set_transform(light, lla); - - lightaux = vs->omni_light_create(); - //vs->light_set_color( lightaux, RenderingServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,1.0) ); - vs->light_set_color(lightaux, Color(1.0, 1.0, 0.0)); - vs->light_set_param(lightaux, RenderingServer::LIGHT_PARAM_RANGE, 4); - vs->light_set_param(lightaux, RenderingServer::LIGHT_PARAM_ENERGY, 8); - //vs->light_set_shadow( lightaux, true ); - //light = vs->instance_create( lightaux ); - - ofs = 0; - quit = false; - } - virtual bool iteration(float p_time) { - RenderingServer *vs = RenderingServer::get_singleton(); - //Transform t; - //t.rotate(Vector3(0, 1, 0), ofs); - //t.translate(Vector3(0,0,20 )); - //vs->camera_set_transform(camera, t); - - ofs += p_time * 0.05; - - //return quit; - - for (List<InstanceInfo>::Element *E = instances.front(); E; E = E->next()) { - Transform pre(Basis(E->get().rot_axis, ofs), Vector3()); - vs->instance_set_transform(E->get().instance, pre * E->get().base); - /* - if( !E->next() ) { - - vs->free( E->get().instance ); - instances.erase(E ); - }*/ - } - - return quit; - } - - virtual bool idle(float p_time) { - return quit; - } - - virtual void finish() { - } -}; - -MainLoop *test() { - return memnew(TestMainLoop); -} - -} // namespace TestRender diff --git a/main/tests/test_render.h b/main/tests/test_render.h deleted file mode 100644 index 4a6340c443..0000000000 --- a/main/tests/test_render.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_render.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_RENDER_H -#define TEST_RENDER_H - -#include "core/os/main_loop.h" - -namespace TestRender { - -MainLoop *test(); -} - -#endif diff --git a/main/tests/test_shader_lang.cpp b/main/tests/test_shader_lang.cpp deleted file mode 100644 index 34ee3e3210..0000000000 --- a/main/tests/test_shader_lang.cpp +++ /dev/null @@ -1,361 +0,0 @@ -/*************************************************************************/ -/* test_shader_lang.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_shader_lang.h" - -#include "core/os/file_access.h" -#include "core/os/main_loop.h" -#include "core/os/os.h" - -#include "core/print_string.h" -#include "scene/gui/control.h" -#include "scene/gui/text_edit.h" -#include "servers/rendering/shader_language.h" - -typedef ShaderLanguage SL; - -namespace TestShaderLang { - -static String _mktab(int p_level) { - String tb; - for (int i = 0; i < p_level; i++) { - tb += "\t"; - } - - return tb; -} - -static String _typestr(SL::DataType p_type) { - return ShaderLanguage::get_datatype_name(p_type); -} - -static String _prestr(SL::DataPrecision p_pres) { - switch (p_pres) { - case SL::PRECISION_LOWP: - return "lowp "; - case SL::PRECISION_MEDIUMP: - return "mediump "; - case SL::PRECISION_HIGHP: - return "highp "; - case SL::PRECISION_DEFAULT: - return ""; - } - return ""; -} - -static String _opstr(SL::Operator p_op) { - return ShaderLanguage::get_operator_text(p_op); -} - -static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) { - switch (p_type) { - case SL::TYPE_BOOL: - return p_values[0].boolean ? "true" : "false"; - case SL::TYPE_BVEC2: - return String() + "bvec2(" + (p_values[0].boolean ? "true" : "false") + (p_values[1].boolean ? "true" : "false") + ")"; - case SL::TYPE_BVEC3: - return String() + "bvec3(" + (p_values[0].boolean ? "true" : "false") + "," + (p_values[1].boolean ? "true" : "false") + "," + (p_values[2].boolean ? "true" : "false") + ")"; - case SL::TYPE_BVEC4: - return String() + "bvec4(" + (p_values[0].boolean ? "true" : "false") + "," + (p_values[1].boolean ? "true" : "false") + "," + (p_values[2].boolean ? "true" : "false") + "," + (p_values[3].boolean ? "true" : "false") + ")"; - case SL::TYPE_INT: - return rtos(p_values[0].sint); - case SL::TYPE_IVEC2: - return String() + "ivec2(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + ")"; - case SL::TYPE_IVEC3: - return String() + "ivec3(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + "," + rtos(p_values[2].sint) + ")"; - case SL::TYPE_IVEC4: - return String() + "ivec4(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + "," + rtos(p_values[2].sint) + "," + rtos(p_values[3].sint) + ")"; - case SL::TYPE_UINT: - return rtos(p_values[0].real); - case SL::TYPE_UVEC2: - return String() + "uvec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")"; - case SL::TYPE_UVEC3: - return String() + "uvec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")"; - case SL::TYPE_UVEC4: - return String() + "uvec4(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + "," + rtos(p_values[3].real) + ")"; - case SL::TYPE_FLOAT: - return rtos(p_values[0].real); - case SL::TYPE_VEC2: - return String() + "vec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")"; - case SL::TYPE_VEC3: - return String() + "vec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")"; - case SL::TYPE_VEC4: - return String() + "vec4(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + "," + rtos(p_values[3].real) + ")"; - default: - ERR_FAIL_V(String()); - } -} - -static String dump_node_code(SL::Node *p_node, int p_level) { - String code; - - switch (p_node->type) { - case SL::Node::TYPE_SHADER: { - SL::ShaderNode *pnode = (SL::ShaderNode *)p_node; - - for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) { - String ucode = "uniform "; - ucode += _prestr(E->get().precision); - ucode += _typestr(E->get().type); - ucode += " " + String(E->key()); - - if (E->get().default_value.size()) { - ucode += " = " + get_constant_text(E->get().type, E->get().default_value); - } - - static const char *hint_name[SL::ShaderNode::Uniform::HINT_MAX] = { - "", - "color", - "range", - "albedo", - "normal", - "black", - "white" - }; - - if (E->get().hint) { - ucode += " : " + String(hint_name[E->get().hint]); - } - - code += ucode + "\n"; - } - - for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) { - String vcode = "varying "; - vcode += _prestr(E->get().precision); - vcode += _typestr(E->get().type); - vcode += " " + String(E->key()); - - code += vcode + "\n"; - } - for (int i = 0; i < pnode->functions.size(); i++) { - SL::FunctionNode *fnode = pnode->functions[i].function; - - String header; - header = _typestr(fnode->return_type) + " " + fnode->name + "("; - for (int j = 0; j < fnode->arguments.size(); j++) { - if (j > 0) { - header += ", "; - } - header += _prestr(fnode->arguments[j].precision) + _typestr(fnode->arguments[j].type) + " " + fnode->arguments[j].name; - } - - header += ")\n"; - code += header; - code += dump_node_code(fnode->body, p_level + 1); - } - - //code+=dump_node_code(pnode->body,p_level); - } break; - case SL::Node::TYPE_STRUCT: { - } break; - case SL::Node::TYPE_FUNCTION: { - } break; - case SL::Node::TYPE_BLOCK: { - SL::BlockNode *bnode = (SL::BlockNode *)p_node; - - //variables - code += _mktab(p_level - 1) + "{\n"; - for (Map<StringName, SL::BlockNode::Variable>::Element *E = bnode->variables.front(); E; E = E->next()) { - code += _mktab(p_level) + _prestr(E->get().precision) + _typestr(E->get().type) + " " + E->key() + ";\n"; - } - - for (int i = 0; i < bnode->statements.size(); i++) { - String scode = dump_node_code(bnode->statements[i], p_level); - - if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW) { - code += scode; //use directly - } else { - code += _mktab(p_level) + scode + ";\n"; - } - } - code += _mktab(p_level - 1) + "}\n"; - - } break; - case SL::Node::TYPE_VARIABLE: { - SL::VariableNode *vnode = (SL::VariableNode *)p_node; - code = vnode->name; - - } break; - case SL::Node::TYPE_VARIABLE_DECLARATION: { - // FIXME: Implement - } break; - case SL::Node::TYPE_ARRAY: { - SL::ArrayNode *vnode = (SL::ArrayNode *)p_node; - code = vnode->name; - } break; - case SL::Node::TYPE_ARRAY_DECLARATION: { - // FIXME: Implement - } break; - case SL::Node::TYPE_ARRAY_CONSTRUCT: { - // FIXME: Implement - } break; - case SL::Node::TYPE_CONSTANT: { - SL::ConstantNode *cnode = (SL::ConstantNode *)p_node; - return get_constant_text(cnode->datatype, cnode->values); - - } break; - case SL::Node::TYPE_OPERATOR: { - SL::OperatorNode *onode = (SL::OperatorNode *)p_node; - - switch (onode->op) { - case SL::OP_ASSIGN: - case SL::OP_ASSIGN_ADD: - case SL::OP_ASSIGN_SUB: - case SL::OP_ASSIGN_MUL: - case SL::OP_ASSIGN_DIV: - case SL::OP_ASSIGN_SHIFT_LEFT: - case SL::OP_ASSIGN_SHIFT_RIGHT: - case SL::OP_ASSIGN_MOD: - case SL::OP_ASSIGN_BIT_AND: - case SL::OP_ASSIGN_BIT_OR: - case SL::OP_ASSIGN_BIT_XOR: - code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level); - break; - case SL::OP_BIT_INVERT: - case SL::OP_NEGATE: - case SL::OP_NOT: - case SL::OP_DECREMENT: - case SL::OP_INCREMENT: - code = _opstr(onode->op) + dump_node_code(onode->arguments[0], p_level); - break; - case SL::OP_POST_DECREMENT: - case SL::OP_POST_INCREMENT: - code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op); - break; - case SL::OP_CALL: - case SL::OP_CONSTRUCT: - code = dump_node_code(onode->arguments[0], p_level) + "("; - for (int i = 1; i < onode->arguments.size(); i++) { - if (i > 1) { - code += ", "; - } - code += dump_node_code(onode->arguments[i], p_level); - } - code += ")"; - break; - default: { - code = "(" + dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level) + ")"; - break; - } - } - - } break; - case SL::Node::TYPE_CONTROL_FLOW: { - SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node; - if (cfnode->flow_op == SL::FLOW_OP_IF) { - code += _mktab(p_level) + "if (" + dump_node_code(cfnode->expressions[0], p_level) + ")\n"; - code += dump_node_code(cfnode->blocks[0], p_level + 1); - if (cfnode->blocks.size() == 2) { - code += _mktab(p_level) + "else\n"; - code += dump_node_code(cfnode->blocks[1], p_level + 1); - } - - } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) { - if (cfnode->blocks.size()) { - code = "return " + dump_node_code(cfnode->blocks[0], p_level); - } else { - code = "return"; - } - } - - } break; - case SL::Node::TYPE_MEMBER: { - SL::MemberNode *mnode = (SL::MemberNode *)p_node; - code = dump_node_code(mnode->owner, p_level) + "." + mnode->name; - - } break; - } - - return code; -} - -static Error recreate_code(void *p_str, SL::ShaderNode *p_program) { - String *str = (String *)p_str; - - *str = dump_node_code(p_program, 0); - - return OK; -} - -MainLoop *test() { - List<String> cmdlargs = OS::get_singleton()->get_cmdline_args(); - - if (cmdlargs.empty()) { - //try editor! - print_line("usage: godot -test shader_lang <shader>"); - return nullptr; - } - - String test = cmdlargs.back()->get(); - - FileAccess *fa = FileAccess::open(test, FileAccess::READ); - - if (!fa) { - ERR_FAIL_V(nullptr); - } - - String code; - - while (true) { - CharType c = fa->get_8(); - if (fa->eof_reached()) { - break; - } - code += c; - } - - SL sl; - print_line("tokens:\n\n" + sl.token_debug(code)); - - Map<StringName, SL::FunctionInfo> dt; - dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3; - dt["fragment"].can_discard = true; - - Vector<StringName> rm; - rm.push_back("popo"); - Set<String> types; - types.insert("spatial"); - - Error err = sl.compile(code, dt, rm, types, nullptr); - - if (err) { - print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text()); - return nullptr; - } else { - String code2; - recreate_code(&code2, sl.get_shader()); - print_line("code:\n\n" + code2); - } - - return nullptr; -} - -} // namespace TestShaderLang diff --git a/main/tests/test_shader_lang.h b/main/tests/test_shader_lang.h deleted file mode 100644 index 2811c5f46e..0000000000 --- a/main/tests/test_shader_lang.h +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************/ -/* test_shader_lang.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_SHADER_LANG_H -#define TEST_SHADER_LANG_H - -#include "core/os/main_loop.h" - -namespace TestShaderLang { - -MainLoop *test(); -} - -#endif // TEST_SHADER_LANG_H diff --git a/main/tests/test_string.cpp b/main/tests/test_string.cpp deleted file mode 100644 index 775c039282..0000000000 --- a/main/tests/test_string.cpp +++ /dev/null @@ -1,1204 +0,0 @@ -/*************************************************************************/ -/* test_string.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "test_string.h" - -#include "core/io/ip_address.h" -#include "core/os/os.h" -#include "core/ustring.h" - -#include "modules/modules_enabled.gen.h" -#ifdef MODULE_REGEX_ENABLED -#include "modules/regex/regex.h" -#endif - -#include <stdio.h> -#include <wchar.h> - -namespace TestString { - -bool test_1() { - OS::get_singleton()->print("\n\nTest 1: Assign from cstr\n"); - - String s = "Hello"; - - OS::get_singleton()->print("\tExpected: Hello\n"); - OS::get_singleton()->print("\tResulted: %ls\n", s.c_str()); - - return (wcscmp(s.c_str(), L"Hello") == 0); -} - -bool test_2() { - OS::get_singleton()->print("\n\nTest 2: Assign from string (operator=)\n"); - - String s = "Dolly"; - const String &t = s; - - OS::get_singleton()->print("\tExpected: Dolly\n"); - OS::get_singleton()->print("\tResulted: %ls\n", t.c_str()); - - return (wcscmp(t.c_str(), L"Dolly") == 0); -} - -bool test_3() { - OS::get_singleton()->print("\n\nTest 3: Assign from c-string (copycon)\n"); - - String s("Sheep"); - const String &t(s); - - OS::get_singleton()->print("\tExpected: Sheep\n"); - OS::get_singleton()->print("\tResulted: %ls\n", t.c_str()); - - return (wcscmp(t.c_str(), L"Sheep") == 0); -} - -bool test_4() { - OS::get_singleton()->print("\n\nTest 4: Assign from c-widechar (operator=)\n"); - - String s(L"Give me"); - - OS::get_singleton()->print("\tExpected: Give me\n"); - OS::get_singleton()->print("\tResulted: %ls\n", s.c_str()); - - return (wcscmp(s.c_str(), L"Give me") == 0); -} - -bool test_5() { - OS::get_singleton()->print("\n\nTest 5: Assign from c-widechar (copycon)\n"); - - String s(L"Wool"); - - OS::get_singleton()->print("\tExpected: Wool\n"); - OS::get_singleton()->print("\tResulted: %ls\n", s.c_str()); - - return (wcscmp(s.c_str(), L"Wool") == 0); -} - -bool test_6() { - OS::get_singleton()->print("\n\nTest 6: comparisons (equal)\n"); - - String s = "Test Compare"; - - OS::get_singleton()->print("\tComparing to \"Test Compare\"\n"); - - if (!(s == "Test Compare")) { - return false; - } - - if (!(s == L"Test Compare")) { - return false; - } - - if (!(s == String("Test Compare"))) { - return false; - } - - return true; -} - -bool test_7() { - OS::get_singleton()->print("\n\nTest 7: comparisons (unequal)\n"); - - String s = "Test Compare"; - - OS::get_singleton()->print("\tComparing to \"Test Compare\"\n"); - - if (!(s != "Peanut")) { - return false; - } - - if (!(s != L"Coconut")) { - return false; - } - - if (!(s != String("Butter"))) { - return false; - } - - return true; -} - -bool test_8() { - OS::get_singleton()->print("\n\nTest 8: comparisons (operator<)\n"); - - String s = "Bees"; - - OS::get_singleton()->print("\tComparing to \"Bees\"\n"); - - if (!(s < "Elephant")) { - return false; - } - - if (s < L"Amber") { - return false; - } - - if (s < String("Beatrix")) { - return false; - } - - return true; -} - -bool test_9() { - OS::get_singleton()->print("\n\nTest 9: Concatenation\n"); - - String s; - - s += "Have"; - s += ' '; - s += 'a'; - s += String(" "); - s = s + L"Nice"; - s = s + " "; - s = s + String("Day"); - - OS::get_singleton()->print("\tComparing to \"Have a Nice Day\"\n"); - - return (s == "Have a Nice Day"); -} - -bool test_10() { - OS::get_singleton()->print("\n\nTest 10: Misc funcs (size/length/empty/etc)\n"); - - if (!String("").empty()) { - return false; - } - - if (String("Mellon").size() != 7) { - return false; - } - - if (String("Oranges").length() != 7) { - return false; - } - - return true; -} - -bool test_11() { - OS::get_singleton()->print("\n\nTest 11: Operator[]\n"); - - String a = "Kugar Sane"; - - a[0] = 'S'; - a[6] = 'C'; - - if (a != "Sugar Cane") { - return false; - } - - if (a[1] != 'u') { - return false; - } - - return true; -} - -bool test_12() { - OS::get_singleton()->print("\n\nTest 12: case functions\n"); - - String a = "MoMoNgA"; - - if (a.to_upper() != "MOMONGA") { - return false; - } - - if (a.nocasecmp_to("momonga") != 0) { - return false; - } - - return true; -} - -bool test_13() { - OS::get_singleton()->print("\n\nTest 13: UTF8\n"); - - /* how can i embed UTF in here? */ - - static const CharType ustr[] = { 0x304A, 0x360F, 0x3088, 0x3046, 0 }; - //static const wchar_t ustr[] = { 'P', 0xCE, 'p',0xD3, 0 }; - String s = ustr; - - OS::get_singleton()->print("\tUnicode: %ls\n", ustr); - s.parse_utf8(s.utf8().get_data()); - OS::get_singleton()->print("\tConvert/Parse UTF8: %ls\n", s.c_str()); - - return (s == ustr); -} - -bool test_14() { - OS::get_singleton()->print("\n\nTest 14: ASCII\n"); - - String s = L"Primero Leche"; - OS::get_singleton()->print("\tAscii: %s\n", s.ascii().get_data()); - - String t = s.ascii().get_data(); - return (s == t); -} - -bool test_15() { - OS::get_singleton()->print("\n\nTest 15: substr\n"); - - String s = "Killer Baby"; - OS::get_singleton()->print("\tsubstr(3,4) of \"%ls\" is \"%ls\"\n", s.c_str(), s.substr(3, 4).c_str()); - - return (s.substr(3, 4) == "ler "); -} - -bool test_16() { - OS::get_singleton()->print("\n\nTest 16: find\n"); - - String s = "Pretty Woman"; - OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - OS::get_singleton()->print("\t\"tty\" is at %i pos.\n", s.find("tty")); - OS::get_singleton()->print("\t\"Revenge of the Monster Truck\" is at %i pos.\n", s.find("Revenge of the Monster Truck")); - - if (s.find("tty") != 3) { - return false; - } - - if (s.find("Revenge of the Monster Truck") != -1) { - return false; - } - - return true; -} - -bool test_17() { - OS::get_singleton()->print("\n\nTest 17: find no case\n"); - - String s = "Pretty Whale"; - OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA")); - OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck")); - - if (s.findn("WHA") != 7) { - return false; - } - - if (s.findn("Revenge of the Monster SawFish") != -1) { - return false; - } - - return true; -} - -bool test_18() { - OS::get_singleton()->print("\n\nTest 18: find no case\n"); - - String s = "Pretty Whale"; - OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA")); - OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck")); - - if (s.findn("WHA") != 7) { - return false; - } - - if (s.findn("Revenge of the Monster SawFish") != -1) { - return false; - } - - return true; -} - -bool test_19() { - OS::get_singleton()->print("\n\nTest 19: Search & replace\n"); - - String s = "Happy Birthday, Anna!"; - OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - - s = s.replace("Birthday", "Halloween"); - OS::get_singleton()->print("\tReplaced Birthday/Halloween: %ls.\n", s.c_str()); - - return (s == "Happy Halloween, Anna!"); -} - -bool test_20() { - OS::get_singleton()->print("\n\nTest 20: Insertion\n"); - - String s = "Who is Frederic?"; - - OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - s = s.insert(s.find("?"), " Chopin"); - OS::get_singleton()->print("\tInserted Chopin: %ls.\n", s.c_str()); - - return (s == "Who is Frederic Chopin?"); -} - -bool test_21() { - OS::get_singleton()->print("\n\nTest 21: Number -> String\n"); - - OS::get_singleton()->print("\tPi is %f\n", 33.141593); - OS::get_singleton()->print("\tPi String is %ls\n", String::num(3.141593).c_str()); - - return String::num(3.141593) == "3.141593"; -} - -bool test_22() { - OS::get_singleton()->print("\n\nTest 22: String -> Int\n"); - - static const char *nums[4] = { "1237461283", "- 22", "0", " - 1123412" }; - static const int num[4] = { 1237461283, -22, 0, -1123412 }; - - for (int i = 0; i < 4; i++) { - OS::get_singleton()->print("\tString: \"%s\" as Int is %i\n", nums[i], String(nums[i]).to_int()); - - if (String(nums[i]).to_int() != num[i]) { - return false; - } - } - - return true; -} - -bool test_23() { - OS::get_singleton()->print("\n\nTest 23: String -> Float\n"); - - static const char *nums[4] = { "-12348298412.2", "0.05", "2.0002", " -0.0001" }; - static const double num[4] = { -12348298412.2, 0.05, 2.0002, -0.0001 }; - - for (int i = 0; i < 4; i++) { - OS::get_singleton()->print("\tString: \"%s\" as Float is %f\n", nums[i], String(nums[i]).to_double()); - - if (ABS(String(nums[i]).to_double() - num[i]) > 0.00001) { - return false; - } - } - - return true; -} - -bool test_24() { - OS::get_singleton()->print("\n\nTest 24: Slicing\n"); - - String s = "Mars,Jupiter,Saturn,Uranus"; - - const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" }; - - OS::get_singleton()->print("\tSlicing \"%ls\" by \"%s\"..\n", s.c_str(), ","); - - for (int i = 0; i < s.get_slice_count(","); i++) { - OS::get_singleton()->print("\t\t%i- %ls\n", i + 1, s.get_slice(",", i).c_str()); - - if (s.get_slice(",", i) != slices[i]) { - return false; - } - } - - return true; -} - -bool test_25() { - OS::get_singleton()->print("\n\nTest 25: Erasing\n"); - - String s = "Josephine is such a cute girl!"; - - OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - OS::get_singleton()->print("\tRemoving \"cute\"\n"); - - s.erase(s.find("cute "), String("cute ").length()); - OS::get_singleton()->print("\tResult: %ls\n", s.c_str()); - - return (s == "Josephine is such a girl!"); -} - -bool test_26() { - OS::get_singleton()->print("\n\nTest 26: RegEx substitution\n"); - -#ifndef MODULE_REGEX_ENABLED - OS::get_singleton()->print("\tRegEx module disabled, can't run test."); - return false; -#else - String s = "Double all the vowels."; - - OS::get_singleton()->print("\tString: %ls\n", s.c_str()); - OS::get_singleton()->print("\tRepeating instances of 'aeiou' once\n"); - - RegEx re("(?<vowel>[aeiou])"); - s = re.sub(s, "$0$vowel", true); - - OS::get_singleton()->print("\tResult: %ls\n", s.c_str()); - - return (s == "Doouublee aall thee vooweels."); -#endif -} - -struct test_27_data { - char const *data; - char const *begin; - bool expected; -}; - -bool test_27() { - OS::get_singleton()->print("\n\nTest 27: begins_with\n"); - test_27_data tc[] = { - { "res://foobar", "res://", true }, - { "res", "res://", false }, - { "abc", "abc", true } - }; - size_t count = sizeof(tc) / sizeof(tc[0]); - bool state = true; - for (size_t i = 0; state && i < count; ++i) { - String s = tc[i].data; - state = s.begins_with(tc[i].begin) == tc[i].expected; - if (state) { - String sb = tc[i].begin; - state = s.begins_with(sb) == tc[i].expected; - } - if (!state) { - OS::get_singleton()->print("\n\t Failure on:\n\t\tstring: %s\n\t\tbegin: %s\n\t\texpected: %s\n", tc[i].data, tc[i].begin, tc[i].expected ? "true" : "false"); - break; - } - }; - return state; -}; - -bool test_28() { - OS::get_singleton()->print("\n\nTest 28: sprintf\n"); - - bool success, state = true; - char output_format[] = "\tTest:\t%ls => %ls (%s)\n"; - String format, output; - Array args; - bool error; - - // %% - format = "fish %% frog"; - args.clear(); - output = format.sprintf(args, &error); - success = (output == String("fish % frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - //////// INTS - - // Int - format = "fish %d frog"; - args.clear(); - args.push_back(5); - output = format.sprintf(args, &error); - success = (output == String("fish 5 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Int left padded with zeroes. - format = "fish %05d frog"; - args.clear(); - args.push_back(5); - output = format.sprintf(args, &error); - success = (output == String("fish 00005 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Int left padded with spaces. - format = "fish %5d frog"; - args.clear(); - args.push_back(5); - output = format.sprintf(args, &error); - success = (output == String("fish 5 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Int right padded with spaces. - format = "fish %-5d frog"; - args.clear(); - args.push_back(5); - output = format.sprintf(args, &error); - success = (output == String("fish 5 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Int with sign (positive). - format = "fish %+d frog"; - args.clear(); - args.push_back(5); - output = format.sprintf(args, &error); - success = (output == String("fish +5 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Negative int. - format = "fish %d frog"; - args.clear(); - args.push_back(-5); - output = format.sprintf(args, &error); - success = (output == String("fish -5 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Hex (lower) - format = "fish %x frog"; - args.clear(); - args.push_back(45); - output = format.sprintf(args, &error); - success = (output == String("fish 2d frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Hex (upper) - format = "fish %X frog"; - args.clear(); - args.push_back(45); - output = format.sprintf(args, &error); - success = (output == String("fish 2D frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Octal - format = "fish %o frog"; - args.clear(); - args.push_back(99); - output = format.sprintf(args, &error); - success = (output == String("fish 143 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ////// REALS - - // Real - format = "fish %f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish 99.990000 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Real left-padded - format = "fish %11f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish 99.990000 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Real right-padded - format = "fish %-11f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish 99.990000 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Real given int. - format = "fish %f frog"; - args.clear(); - args.push_back(99); - output = format.sprintf(args, &error); - success = (output == String("fish 99.000000 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Real with sign (positive). - format = "fish %+f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish +99.990000 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Real with 1 decimals. - format = "fish %.1f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish 100.0 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Real with 12 decimals. - format = "fish %.12f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish 99.990000000000 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Real with no decimals. - format = "fish %.f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish 100 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - /////// Strings. - - // String - format = "fish %s frog"; - args.clear(); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == String("fish cheese frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // String left-padded - format = "fish %10s frog"; - args.clear(); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == String("fish cheese frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // String right-padded - format = "fish %-10s frog"; - args.clear(); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == String("fish cheese frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ///// Characters - - // Character as string. - format = "fish %c frog"; - args.clear(); - args.push_back("A"); - output = format.sprintf(args, &error); - success = (output == String("fish A frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Character as int. - format = "fish %c frog"; - args.clear(); - args.push_back(65); - output = format.sprintf(args, &error); - success = (output == String("fish A frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ///// Dynamic width - - // String dynamic width - format = "fish %*s frog"; - args.clear(); - args.push_back(10); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == String("fish cheese frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Int dynamic width - format = "fish %*d frog"; - args.clear(); - args.push_back(10); - args.push_back(99); - output = format.sprintf(args, &error); - success = (output == String("fish 99 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Float dynamic width - format = "fish %*.*f frog"; - args.clear(); - args.push_back(10); - args.push_back(3); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == String("fish 99.990 frog") && !error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ///// Errors - - // More formats than arguments. - format = "fish %s %s frog"; - args.clear(); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == "not enough arguments for format string" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // More arguments than formats. - format = "fish %s frog"; - args.clear(); - args.push_back("hello"); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == "not all arguments converted during string formatting" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Incomplete format. - format = "fish %10"; - args.clear(); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == "incomplete format" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Bad character in format string - format = "fish %&f frog"; - args.clear(); - args.push_back("cheese"); - output = format.sprintf(args, &error); - success = (output == "unsupported format character" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Too many decimals. - format = "fish %2.2.2f frog"; - args.clear(); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == "too many decimal points in format" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // * not a number - format = "fish %*f frog"; - args.clear(); - args.push_back("cheese"); - args.push_back(99.99); - output = format.sprintf(args, &error); - success = (output == "* wants number" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Character too long. - format = "fish %c frog"; - args.clear(); - args.push_back("sc"); - output = format.sprintf(args, &error); - success = (output == "%c requires number or single-character string" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - // Character bad type. - format = "fish %c frog"; - args.clear(); - args.push_back(Array()); - output = format.sprintf(args, &error); - success = (output == "%c requires number or single-character string" && error); - OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - return state; -} - -bool test_29() { - bool state = true; - - IP_Address ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); - OS::get_singleton()->print("ip0 is %ls\n", String(ip0).c_str()); - - IP_Address ip(0x0123, 0x4567, 0x89ab, 0xcdef, true); - OS::get_singleton()->print("ip6 is %ls\n", String(ip).c_str()); - - IP_Address ip2("fe80::52e5:49ff:fe93:1baf"); - OS::get_singleton()->print("ip6 is %ls\n", String(ip2).c_str()); - - IP_Address ip3("::ffff:192.168.0.1"); - OS::get_singleton()->print("ip6 is %ls\n", String(ip3).c_str()); - - String ip4 = "192.168.0.1"; - bool success = ip4.is_valid_ip_address(); - OS::get_singleton()->print("Is valid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ip4 = "192.368.0.1"; - success = (!ip4.is_valid_ip_address()); - OS::get_singleton()->print("Is invalid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; - success = ip6.is_valid_ip_address(); - OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334"; - success = (!ip6.is_valid_ip_address()); - OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334"; - success = (!ip6.is_valid_ip_address()); - OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ip6 = "2001:0db8::0:8a2e:370:7334"; - success = (ip6.is_valid_ip_address()); - OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - ip6 = "::ffff:192.168.0.1"; - success = (ip6.is_valid_ip_address()); - OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL"); - state = state && success; - - return state; -}; - -bool test_30() { - bool state = true; - bool success = true; - String input = "bytes2var"; - String output = "Bytes 2 Var"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "linear2db"; - output = "Linear 2 Db"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "vector3"; - output = "Vector 3"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "sha256"; - output = "Sha 256"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "2db"; - output = "2 Db"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "PascalCase"; - output = "Pascal Case"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "PascalPascalCase"; - output = "Pascal Pascal Case"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "snake_case"; - output = "Snake Case"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "snake_snake_case"; - output = "Snake Snake Case"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "sha256sum"; - output = "Sha 256 Sum"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "cat2dog"; - output = "Cat 2 Dog"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "function(name)"; - output = "Function(name)"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls (existing incorrect behavior): %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "snake_case_function(snake_case_arg)"; - output = "Snake Case Function(snake Case Arg)"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls (existing incorrect behavior): %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - input = "snake_case_function( snake_case_arg )"; - output = "Snake Case Function( Snake Case Arg )"; - success = (input.capitalize() == output); - state = state && success; - OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL"); - - return state; -} - -bool test_31() { - bool state = true; - bool success; - - String a = ""; - success = a[0] == 0; - OS::get_singleton()->print("Is 0 String[0]:, %s\n", success ? "OK" : "FAIL"); - if (!success) { - state = false; - } - - String b = "Godot"; - success = b[b.size()] == 0; - OS::get_singleton()->print("Is 0 String[size()]:, %s\n", success ? "OK" : "FAIL"); - if (!success) { - state = false; - } - - const String c = ""; - success = c[0] == 0; - OS::get_singleton()->print("Is 0 const String[0]:, %s\n", success ? "OK" : "FAIL"); - if (!success) { - state = false; - } - - const String d = "Godot"; - success = d[d.size()] == 0; - OS::get_singleton()->print("Is 0 const String[size()]:, %s\n", success ? "OK" : "FAIL"); - if (!success) { - state = false; - } - - return state; -}; - -bool test_32() { -#define STRIP_TEST(x) \ - { \ - bool success = x; \ - state = state && success; \ - if (!success) { \ - OS::get_singleton()->print("\tfailed at: %s\n", #x); \ - } \ - } - - OS::get_singleton()->print("\n\nTest 32: lstrip and rstrip\n"); - bool state = true; - - // strip none - STRIP_TEST(String("abc").lstrip("") == "abc"); - STRIP_TEST(String("abc").rstrip("") == "abc"); - // strip one - STRIP_TEST(String("abc").lstrip("a") == "bc"); - STRIP_TEST(String("abc").rstrip("c") == "ab"); - // strip lots - STRIP_TEST(String("bababbababccc").lstrip("ab") == "ccc"); - STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("cb") == "aaa"); - // strip empty string - STRIP_TEST(String("").lstrip("") == ""); - STRIP_TEST(String("").rstrip("") == ""); - // strip to empty string - STRIP_TEST(String("abcabcabc").lstrip("bca") == ""); - STRIP_TEST(String("abcabcabc").rstrip("bca") == ""); - // don't strip wrong end - STRIP_TEST(String("abc").lstrip("c") == "abc"); - STRIP_TEST(String("abca").lstrip("a") == "bca"); - STRIP_TEST(String("abc").rstrip("a") == "abc"); - STRIP_TEST(String("abca").rstrip("a") == "abc"); - // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5) - // and the same second as "ÿ" (\u00ff) - STRIP_TEST(String::utf8("¿").lstrip(String::utf8("µÿ")) == String::utf8("¿")); - STRIP_TEST(String::utf8("¿").rstrip(String::utf8("µÿ")) == String::utf8("¿")); - STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("µÿ")) == String::utf8("¿ÿ")); - STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("µÿ")) == String::utf8("µ¿")); - - // the above tests repeated with additional superfluous strip chars - - // strip none - STRIP_TEST(String("abc").lstrip("qwjkl") == "abc"); - STRIP_TEST(String("abc").rstrip("qwjkl") == "abc"); - // strip one - STRIP_TEST(String("abc").lstrip("qwajkl") == "bc"); - STRIP_TEST(String("abc").rstrip("qwcjkl") == "ab"); - // strip lots - STRIP_TEST(String("bababbababccc").lstrip("qwabjkl") == "ccc"); - STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("qwcbjkl") == "aaa"); - // strip empty string - STRIP_TEST(String("").lstrip("qwjkl") == ""); - STRIP_TEST(String("").rstrip("qwjkl") == ""); - // strip to empty string - STRIP_TEST(String("abcabcabc").lstrip("qwbcajkl") == ""); - STRIP_TEST(String("abcabcabc").rstrip("qwbcajkl") == ""); - // don't strip wrong end - STRIP_TEST(String("abc").lstrip("qwcjkl") == "abc"); - STRIP_TEST(String("abca").lstrip("qwajkl") == "bca"); - STRIP_TEST(String("abc").rstrip("qwajkl") == "abc"); - STRIP_TEST(String("abca").rstrip("qwajkl") == "abc"); - // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5) - // and the same second as "ÿ" (\u00ff) - STRIP_TEST(String::utf8("¿").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿")); - STRIP_TEST(String::utf8("¿").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿")); - STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿ÿ")); - STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("µ¿")); - - return state; - -#undef STRIP_TEST -} - -bool test_33() { - OS::get_singleton()->print("\n\nTest 33: parse_utf8(null, -1)\n"); - - String empty; - return empty.parse_utf8(nullptr, -1); -} - -bool test_34() { - OS::get_singleton()->print("\n\nTest 34: Cyrillic to_lower()\n"); - - String upper = String::utf8("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"); - String lower = String::utf8("абвгдеёжзийклмнопрстуфхцчшщъыьэюя"); - - String test = upper.to_lower(); - - bool state = test == lower; - - return state; -} - -bool test_35() { -#define COUNT_TEST(x) \ - { \ - bool success = x; \ - state = state && success; \ - if (!success) { \ - OS::get_singleton()->print("\tfailed at: %s\n", #x); \ - } \ - } - - OS::get_singleton()->print("\n\nTest 35: count and countn function\n"); - bool state = true; - - COUNT_TEST(String("").count("Test") == 0); - COUNT_TEST(String("Test").count("") == 0); - COUNT_TEST(String("Test").count("test") == 0); - COUNT_TEST(String("Test").count("TEST") == 0); - COUNT_TEST(String("TEST").count("TEST") == 1); - COUNT_TEST(String("Test").count("Test") == 1); - COUNT_TEST(String("aTest").count("Test") == 1); - COUNT_TEST(String("Testa").count("Test") == 1); - COUNT_TEST(String("TestTestTest").count("Test") == 3); - COUNT_TEST(String("TestTestTest").count("TestTest") == 1); - COUNT_TEST(String("TestGodotTestGodotTestGodot").count("Test") == 3); - - COUNT_TEST(String("TestTestTestTest").count("Test", 4, 8) == 1); - COUNT_TEST(String("TestTestTestTest").count("Test", 4, 12) == 2); - COUNT_TEST(String("TestTestTestTest").count("Test", 4, 16) == 3); - COUNT_TEST(String("TestTestTestTest").count("Test", 4) == 3); - - COUNT_TEST(String("Test").countn("test") == 1); - COUNT_TEST(String("Test").countn("TEST") == 1); - COUNT_TEST(String("testTest-Testatest").countn("tEst") == 4); - COUNT_TEST(String("testTest-TeStatest").countn("tEsT", 4, 16) == 2); - - return state; -} - -typedef bool (*TestFunc)(); - -TestFunc test_funcs[] = { - - test_1, - test_2, - test_3, - test_4, - test_5, - test_6, - test_7, - test_8, - test_9, - test_10, - test_11, - test_12, - test_13, - test_14, - test_15, - test_16, - test_17, - test_18, - test_19, - test_20, - test_21, - test_22, - test_23, - test_24, - test_25, - test_26, - test_27, - test_28, - test_29, - test_30, - test_31, - test_32, - test_33, - test_34, - test_35, - nullptr - -}; - -MainLoop *test() { - /** A character length != wchar_t may be forced, so the tests won't work */ - - static_assert(sizeof(CharType) == sizeof(wchar_t)); - - int count = 0; - int passed = 0; - - while (true) { - if (!test_funcs[count]) { - break; - } - bool pass = test_funcs[count](); - if (pass) { - passed++; - } - OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED"); - - count++; - } - - OS::get_singleton()->print("\n\n\n"); - OS::get_singleton()->print("*************\n"); - OS::get_singleton()->print("***TOTALS!***\n"); - OS::get_singleton()->print("*************\n"); - - OS::get_singleton()->print("Passed %i of %i tests\n", passed, count); - - return nullptr; -} - -} // namespace TestString diff --git a/main/tests/test_string.h b/main/tests/test_string.h deleted file mode 100644 index 96fa811126..0000000000 --- a/main/tests/test_string.h +++ /dev/null @@ -1,42 +0,0 @@ -/*************************************************************************/ -/* test_string.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -#ifndef TEST_STRING_H -#define TEST_STRING_H - -#include "core/os/main_loop.h" -#include "core/ustring.h" - -namespace TestString { - -MainLoop *test(); -} - -#endif |