/**************************************************************************/
/*  register_server_types.cpp                                             */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "register_server_types.h"

#include "core/config/engine.h"
#include "core/config/project_settings.h"
#include "core/object/callable_mp.h"
#include "core/os/os.h"
#include "servers/audio/audio_effect.h"
#include "servers/audio/audio_server.h"
#include "servers/audio/audio_stream.h"
#include "servers/audio/effects/audio_effect_amplify.h"
#include "servers/audio/effects/audio_effect_capture.h"
#include "servers/audio/effects/audio_effect_chorus.h"
#include "servers/audio/effects/audio_effect_compressor.h"
#include "servers/audio/effects/audio_effect_delay.h"
#include "servers/audio/effects/audio_effect_distortion.h"
#include "servers/audio/effects/audio_effect_eq.h"
#include "servers/audio/effects/audio_effect_filter.h"
#include "servers/audio/effects/audio_effect_hard_limiter.h"
#include "servers/audio/effects/audio_effect_panner.h"
#include "servers/audio/effects/audio_effect_phaser.h"
#include "servers/audio/effects/audio_effect_pitch_shift.h"
#include "servers/audio/effects/audio_effect_record.h"
#include "servers/audio/effects/audio_effect_reverb.h"
#include "servers/audio/effects/audio_effect_spectrum_analyzer.h"
#include "servers/audio/effects/audio_effect_stereo_enhance.h"
#include "servers/audio/effects/audio_stream_generator.h"
#include "servers/camera/camera_feed.h"
#include "servers/camera/camera_server.h"
#include "servers/debugger/servers_debugger.h"
#include "servers/display/accessibility_server.h"
#include "servers/display/display_server.h"
#include "servers/display/native_menu.h"
#include "servers/movie_writer/movie_writer.h"
#include "servers/movie_writer/movie_writer_pngwav.h"
#include "servers/rendering/renderer_rd/framebuffer_cache_rd.h"
#include "servers/rendering/renderer_rd/storage_rd/render_data_rd.h"
#include "servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h"
#include "servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.h"
#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
#include "servers/rendering/rendering_device.h"
#include "servers/rendering/rendering_device_binds.h"
#include "servers/rendering/rendering_server.h"
#include "servers/rendering/shader_include_db.h"
#include "servers/rendering/shader_types.h"
#include "servers/rendering/storage/render_data.h"
#include "servers/rendering/storage/render_data_extension.h"
#include "servers/rendering/storage/render_scene_buffers.h"
#include "servers/rendering/storage/render_scene_data.h"
#include "servers/text/text_server.h"
#include "servers/text/text_server_dummy.h"
#include "servers/text/text_server_extension.h"

#ifndef DISABLE_DEPRECATED
#include "servers/audio/effects/audio_effect_limiter.h"
#endif

// 2D physics and navigation.
#ifndef NAVIGATION_2D_DISABLED
#include "servers/navigation_2d/navigation_server_2d.h"
#endif // NAVIGATION_2D_DISABLED
#ifndef PHYSICS_2D_DISABLED
#include "servers/physics_2d/physics_server_2d.h"
#include "servers/physics_2d/physics_server_2d_dummy.h"
#include "servers/physics_2d/physics_server_2d_extension.h"
#endif // PHYSICS_2D_DISABLED

// 3D physics and navigation.
#ifndef NAVIGATION_3D_DISABLED
#include "servers/navigation_3d/navigation_server_3d.h"
#endif // NAVIGATION_3D_DISABLED
#ifndef PHYSICS_3D_DISABLED
#include "servers/physics_3d/physics_server_3d.h"
#include "servers/physics_3d/physics_server_3d_dummy.h"
#include "servers/physics_3d/physics_server_3d_extension.h"
#endif // PHYSICS_3D_DISABLED

// XR
#ifndef XR_DISABLED
#include "servers/xr/xr_body_tracker.h"
#include "servers/xr/xr_controller_tracker.h"
#include "servers/xr/xr_face_tracker.h"
#include "servers/xr/xr_hand_tracker.h"
#include "servers/xr/xr_interface.h"
#include "servers/xr/xr_interface_extension.h"
#include "servers/xr/xr_positional_tracker.h"
#include "servers/xr/xr_server.h"
#include "servers/xr/xr_vrs.h"
#endif // XR_DISABLED

ShaderTypes *shader_types = nullptr;

#ifndef PHYSICS_2D_DISABLED
static PhysicsServer2D *_create_dummy_physics_server_2d() {
	return memnew(PhysicsServer2DDummy);
}
#endif // PHYSICS_2D_DISABLED

#ifndef PHYSICS_3D_DISABLED
static PhysicsServer3D *_create_dummy_physics_server_3d() {
	return memnew(PhysicsServer3DDummy);
}
#endif // PHYSICS_3D_DISABLED

static bool has_server_feature_callback(const String &p_feature) {
	if (RenderingServer::get_singleton()) {
		if (RenderingServer::get_singleton()->has_os_feature(p_feature)) {
			return true;
		}
	}

	return false;
}

static MovieWriterPNGWAV *writer_pngwav = nullptr;

void register_server_types() {
	OS::get_singleton()->benchmark_begin_measure("Servers", "Register Extensions");

	shader_types = memnew(ShaderTypes);

	GDREGISTER_CLASS(TextServerManager);
	GDREGISTER_ABSTRACT_CLASS(TextServer);
	GDREGISTER_CLASS(TextServerExtension);
	GDREGISTER_CLASS(TextServerDummy);

	GDREGISTER_NATIVE_STRUCT(Glyph, "int start = -1;int end = -1;uint8_t count = 0;uint8_t repeat = 1;uint16_t flags = 0;float x_off = 0.f;float y_off = 0.f;float advance = 0.f;RID font_rid;int font_size = 0;int32_t index = 0");
	GDREGISTER_NATIVE_STRUCT(CaretInfo, "Rect2 leading_caret;Rect2 trailing_caret;TextServer::Direction leading_direction;TextServer::Direction trailing_direction");

	Engine::get_singleton()->add_singleton(Engine::Singleton("TextServerManager", TextServerManager::get_singleton(), "TextServerManager"));

	OS::get_singleton()->set_has_server_feature_callback(has_server_feature_callback);

	GDREGISTER_ABSTRACT_CLASS(AccessibilityServer);
	GDREGISTER_ABSTRACT_CLASS(DisplayServer);
	GDREGISTER_ABSTRACT_CLASS(RenderingServer);
	GDREGISTER_CLASS(AudioServer);

	GDREGISTER_CLASS(NativeMenu);

	GDREGISTER_CLASS(CameraServer);

	GDREGISTER_ABSTRACT_CLASS(RenderingDevice);

	GDREGISTER_CLASS(AudioStream);
	GDREGISTER_CLASS(AudioStreamPlayback);
	GDREGISTER_VIRTUAL_CLASS(AudioStreamPlaybackResampled);
	GDREGISTER_CLASS(AudioStreamMicrophone);
	GDREGISTER_CLASS(AudioStreamRandomizer);
	GDREGISTER_CLASS(AudioSample);
	GDREGISTER_CLASS(AudioSamplePlayback);
	GDREGISTER_VIRTUAL_CLASS(AudioEffect);
	GDREGISTER_VIRTUAL_CLASS(AudioEffectInstance);
	GDREGISTER_CLASS(AudioEffectEQ);
	GDREGISTER_CLASS(AudioEffectFilter);
	GDREGISTER_CLASS(AudioBusLayout);

	GDREGISTER_CLASS(AudioStreamGenerator);
	GDREGISTER_ABSTRACT_CLASS(AudioStreamGeneratorPlayback);

	{
		//audio effects
		GDREGISTER_CLASS(AudioEffectAmplify);

		GDREGISTER_CLASS(AudioEffectReverb);

		GDREGISTER_CLASS(AudioEffectLowPassFilter);
		GDREGISTER_CLASS(AudioEffectHighPassFilter);
		GDREGISTER_CLASS(AudioEffectBandPassFilter);
		GDREGISTER_CLASS(AudioEffectNotchFilter);
		GDREGISTER_CLASS(AudioEffectBandLimitFilter);
		GDREGISTER_CLASS(AudioEffectLowShelfFilter);
		GDREGISTER_CLASS(AudioEffectHighShelfFilter);

		GDREGISTER_CLASS(AudioEffectEQ6);
		GDREGISTER_CLASS(AudioEffectEQ10);
		GDREGISTER_CLASS(AudioEffectEQ21);

		GDREGISTER_CLASS(AudioEffectDistortion);

		GDREGISTER_CLASS(AudioEffectStereoEnhance);

		GDREGISTER_CLASS(AudioEffectPanner);
		GDREGISTER_CLASS(AudioEffectChorus);
		GDREGISTER_CLASS(AudioEffectDelay);
		GDREGISTER_CLASS(AudioEffectCompressor);
		GDREGISTER_CLASS(AudioEffectHardLimiter);
		GDREGISTER_CLASS(AudioEffectPitchShift);
		GDREGISTER_CLASS(AudioEffectPhaser);
		GDREGISTER_CLASS(AudioEffectRecord);
		GDREGISTER_CLASS(AudioEffectSpectrumAnalyzer);
		GDREGISTER_ABSTRACT_CLASS(AudioEffectSpectrumAnalyzerInstance);

		GDREGISTER_CLASS(AudioEffectCapture);

#ifndef DISABLE_DEPRECATED
		GDREGISTER_CLASS(AudioEffectLimiter);
#endif
	}

	GDREGISTER_ABSTRACT_CLASS(RenderingDevice);
	GDREGISTER_CLASS(ShaderIncludeDB);
	GDREGISTER_CLASS(RDTextureFormat);
	GDREGISTER_CLASS(RDTextureView);
	GDREGISTER_CLASS(RDAttachmentFormat);
	GDREGISTER_CLASS(RDFramebufferPass);
	GDREGISTER_CLASS(RDSamplerState);
	GDREGISTER_CLASS(RDVertexAttribute);
	GDREGISTER_CLASS(RDUniform);
	GDREGISTER_CLASS(RDPipelineRasterizationState);
	GDREGISTER_CLASS(RDPipelineMultisampleState);
	GDREGISTER_CLASS(RDPipelineDepthStencilState);
	GDREGISTER_CLASS(RDPipelineColorBlendStateAttachment);
	GDREGISTER_CLASS(RDPipelineColorBlendState);
	GDREGISTER_CLASS(RDShaderSource);
	GDREGISTER_CLASS(RDShaderSPIRV);
	GDREGISTER_CLASS(RDShaderFile);
	GDREGISTER_CLASS(RDPipelineSpecializationConstant);
	GDREGISTER_CLASS(RDAccelerationStructureGeometry);
	GDREGISTER_CLASS(RDAccelerationStructureInstance);
	GDREGISTER_CLASS(RDPipelineShader);
	GDREGISTER_CLASS(RDHitGroup);

	GDREGISTER_ABSTRACT_CLASS(RenderData);
	GDREGISTER_CLASS(RenderDataExtension);
	GDREGISTER_CLASS(RenderDataRD);

	GDREGISTER_ABSTRACT_CLASS(RenderSceneData);
	GDREGISTER_CLASS(RenderSceneDataExtension);
	GDREGISTER_CLASS(RenderSceneDataRD);

	GDREGISTER_CLASS(RenderSceneBuffersConfiguration);
	GDREGISTER_ABSTRACT_CLASS(RenderSceneBuffers);
	GDREGISTER_CLASS(RenderSceneBuffersExtension);
	GDREGISTER_CLASS(RenderSceneBuffersRD);

	GDREGISTER_CLASS(FramebufferCacheRD);
	GDREGISTER_CLASS(UniformSetCacheRD);

	GDREGISTER_CLASS(CameraFeed);

	GDREGISTER_VIRTUAL_CLASS(MovieWriter);

	ServersDebugger::initialize();

#ifndef NAVIGATION_2D_DISABLED
	GDREGISTER_CLASS(NavigationServer2DManager);
	Engine::get_singleton()->add_singleton(Engine::Singleton("NavigationServer2DManager", NavigationServer2DManager::get_singleton(), "NavigationServer2DManager"));

	GDREGISTER_ABSTRACT_CLASS(NavigationServer2D);
	GDREGISTER_CLASS(NavigationPathQueryParameters2D);
	GDREGISTER_CLASS(NavigationPathQueryResult2D);

	GLOBAL_DEF(PropertyInfo(Variant::STRING, NavigationServer2DManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT"), "DEFAULT");

	NavigationServer2DManager::get_singleton()->register_server("Dummy", callable_mp_static(NavigationServer2DManager::create_dummy_server_callback));
#endif // NAVIGATION_2D_DISABLED

#ifndef PHYSICS_2D_DISABLED
	// Physics 2D
	GDREGISTER_CLASS(PhysicsServer2DManager);
	Engine::get_singleton()->add_singleton(Engine::Singleton("PhysicsServer2DManager", PhysicsServer2DManager::get_singleton(), "PhysicsServer2DManager"));

	GDREGISTER_ABSTRACT_CLASS(PhysicsServer2D);
	GDREGISTER_VIRTUAL_CLASS(PhysicsServer2DExtension);
	GDREGISTER_ABSTRACT_CLASS(PhysicsDirectBodyState2D);
	GDREGISTER_VIRTUAL_CLASS(PhysicsDirectBodyState2DExtension);
	GDREGISTER_ABSTRACT_CLASS(PhysicsDirectSpaceState2D);
	GDREGISTER_VIRTUAL_CLASS(PhysicsDirectSpaceState2DExtension);

	GDREGISTER_NATIVE_STRUCT(PhysicsServer2DExtensionRayResult, "Vector2 position;Vector2 normal;RID rid;ObjectID collider_id;Object *collider;int shape");
	GDREGISTER_NATIVE_STRUCT(PhysicsServer2DExtensionShapeResult, "RID rid;ObjectID collider_id;Object *collider;int shape");
	GDREGISTER_NATIVE_STRUCT(PhysicsServer2DExtensionShapeRestInfo, "Vector2 point;Vector2 normal;RID rid;ObjectID collider_id;int shape;Vector2 linear_velocity");
	GDREGISTER_NATIVE_STRUCT(PhysicsServer2DExtensionMotionResult, "Vector2 travel;Vector2 remainder;Vector2 collision_point;Vector2 collision_normal;Vector2 collider_velocity;real_t collision_depth;real_t collision_safe_fraction;real_t collision_unsafe_fraction;int collision_local_shape;ObjectID collider_id;RID collider;int collider_shape");

	GDREGISTER_CLASS(PhysicsRayQueryParameters2D);
	GDREGISTER_CLASS(PhysicsPointQueryParameters2D);
	GDREGISTER_CLASS(PhysicsShapeQueryParameters2D);
	GDREGISTER_CLASS(PhysicsTestMotionParameters2D);
	GDREGISTER_CLASS(PhysicsTestMotionResult2D);

	GLOBAL_DEF(PropertyInfo(Variant::STRING, PhysicsServer2DManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT"), "DEFAULT");

	PhysicsServer2DManager::get_singleton()->register_server("Dummy", callable_mp_static(_create_dummy_physics_server_2d));
#endif // PHYSICS_2D_DISABLED

#ifndef NAVIGATION_3D_DISABLED
	GDREGISTER_CLASS(NavigationServer3DManager);
	Engine::get_singleton()->add_singleton(Engine::Singleton("NavigationServer3DManager", NavigationServer3DManager::get_singleton(), "NavigationServer3DManager"));

	GDREGISTER_ABSTRACT_CLASS(NavigationServer3D);
	GDREGISTER_CLASS(NavigationPathQueryParameters3D);
	GDREGISTER_CLASS(NavigationPathQueryResult3D);

	GLOBAL_DEF(PropertyInfo(Variant::STRING, NavigationServer3DManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT"), "DEFAULT");

	NavigationServer3DManager::get_singleton()->register_server("Dummy", callable_mp_static(NavigationServer3DManager::create_dummy_server_callback));
#endif // NAVIGATION_3D_DISABLED

#ifndef PHYSICS_3D_DISABLED
	// Physics 3D
	GDREGISTER_CLASS(PhysicsServer3DManager);
	Engine::get_singleton()->add_singleton(Engine::Singleton("PhysicsServer3DManager", PhysicsServer3DManager::get_singleton(), "PhysicsServer3DManager"));

	GDREGISTER_ABSTRACT_CLASS(PhysicsServer3D);
	GDREGISTER_VIRTUAL_CLASS(PhysicsServer3DExtension);
	GDREGISTER_ABSTRACT_CLASS(PhysicsDirectBodyState3D);
	GDREGISTER_VIRTUAL_CLASS(PhysicsDirectBodyState3DExtension);
	GDREGISTER_ABSTRACT_CLASS(PhysicsDirectSpaceState3D);
	GDREGISTER_VIRTUAL_CLASS(PhysicsDirectSpaceState3DExtension)
	GDREGISTER_VIRTUAL_CLASS(PhysicsServer3DRenderingServerHandler)

	GDREGISTER_NATIVE_STRUCT(PhysicsServer3DExtensionRayResult, "Vector3 position;Vector3 normal;RID rid;ObjectID collider_id;Object *collider;int shape;int face_index");
	GDREGISTER_NATIVE_STRUCT(PhysicsServer3DExtensionShapeResult, "RID rid;ObjectID collider_id;Object *collider;int shape");
	GDREGISTER_NATIVE_STRUCT(PhysicsServer3DExtensionShapeRestInfo, "Vector3 point;Vector3 normal;RID rid;ObjectID collider_id;int shape;Vector3 linear_velocity");
	GDREGISTER_NATIVE_STRUCT(PhysicsServer3DExtensionMotionCollision, "Vector3 position;Vector3 normal;Vector3 collider_velocity;Vector3 collider_angular_velocity;real_t depth;int local_shape;ObjectID collider_id;RID collider;int collider_shape");
	GDREGISTER_NATIVE_STRUCT(PhysicsServer3DExtensionMotionResult, "Vector3 travel;Vector3 remainder;real_t collision_depth;real_t collision_safe_fraction;real_t collision_unsafe_fraction;PhysicsServer3DExtensionMotionCollision collisions[32];int collision_count");

	GDREGISTER_CLASS(PhysicsRayQueryParameters3D);
	GDREGISTER_CLASS(PhysicsPointQueryParameters3D);
	GDREGISTER_CLASS(PhysicsShapeQueryParameters3D);
	GDREGISTER_CLASS(PhysicsTestMotionParameters3D);
	GDREGISTER_CLASS(PhysicsTestMotionResult3D);

	GLOBAL_DEF(PropertyInfo(Variant::STRING, PhysicsServer3DManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT"), "DEFAULT");

	PhysicsServer3DManager::get_singleton()->register_server("Dummy", callable_mp_static(_create_dummy_physics_server_3d));
#endif // PHYSICS_3D_DISABLED

#ifndef XR_DISABLED
	GDREGISTER_ABSTRACT_CLASS(XRInterface);
	GDREGISTER_ABSTRACT_CLASS(XRTracker);
	GDREGISTER_CLASS(XRVRS);
	GDREGISTER_CLASS(XRPositionalTracker);
	GDREGISTER_CLASS(XRBodyTracker);
	GDREGISTER_CLASS(XRControllerTracker);
	GDREGISTER_CLASS(XRFaceTracker);
	GDREGISTER_CLASS(XRHandTracker);
	GDREGISTER_CLASS(XRInterfaceExtension); // can't register this as virtual because we need a creation function for our extensions.
	GDREGISTER_CLASS(XRPose);
	GDREGISTER_CLASS(XRServer);
#endif // XR_DISABLED

	if constexpr (GD_IS_CLASS_ENABLED(MovieWriterPNGWAV)) {
		writer_pngwav = memnew(MovieWriterPNGWAV);
		MovieWriter::add_writer(writer_pngwav);
	}

	OS::get_singleton()->benchmark_end_measure("Servers", "Register Extensions");
}

void unregister_server_types() {
	OS::get_singleton()->benchmark_begin_measure("Servers", "Unregister Extensions");

	ServersDebugger::deinitialize();
	memdelete(shader_types);
	if constexpr (GD_IS_CLASS_ENABLED(MovieWriterPNGWAV)) {
		memdelete(writer_pngwav);
	}

	OS::get_singleton()->benchmark_end_measure("Servers", "Unregister Extensions");
}

void register_server_singletons() {
	OS::get_singleton()->benchmark_begin_measure("Servers", "Register Singletons");

	Engine::get_singleton()->add_singleton(Engine::Singleton("AccessibilityServer", AccessibilityServer::get_singleton(), "AccessibilityServer"));
	Engine::get_singleton()->add_singleton(Engine::Singleton("AudioServer", AudioServer::get_singleton(), "AudioServer"));
	Engine::get_singleton()->add_singleton(Engine::Singleton("CameraServer", CameraServer::get_singleton(), "CameraServer"));
	Engine::get_singleton()->add_singleton(Engine::Singleton("DisplayServer", DisplayServer::get_singleton(), "DisplayServer"));
	Engine::get_singleton()->add_singleton(Engine::Singleton("NativeMenu", NativeMenu::get_singleton(), "NativeMenu"));
	Engine::get_singleton()->add_singleton(Engine::Singleton("RenderingServer", RenderingServer::get_singleton(), "RenderingServer"));
#ifndef NAVIGATION_2D_DISABLED
	Engine::get_singleton()->add_singleton(Engine::Singleton("NavigationServer2D", NavigationServer2D::get_singleton(), "NavigationServer2D"));
#endif // NAVIGATION_2D_DISABLED
#ifndef NAVIGATION_3D_DISABLED
	Engine::get_singleton()->add_singleton(Engine::Singleton("NavigationServer3D", NavigationServer3D::get_singleton(), "NavigationServer3D"));
#endif // NAVIGATION_3D_DISABLED
#ifndef PHYSICS_2D_DISABLED
	Engine::get_singleton()->add_singleton(Engine::Singleton("PhysicsServer2D", PhysicsServer2D::get_singleton(), "PhysicsServer2D"));
#endif // PHYSICS_2D_DISABLED
#ifndef PHYSICS_3D_DISABLED
	Engine::get_singleton()->add_singleton(Engine::Singleton("PhysicsServer3D", PhysicsServer3D::get_singleton(), "PhysicsServer3D"));
#endif // PHYSICS_3D_DISABLED
#ifndef XR_DISABLED
	Engine::get_singleton()->add_singleton(Engine::Singleton("XRServer", XRServer::get_singleton(), "XRServer"));
#endif // XR_DISABLED

	OS::get_singleton()->benchmark_end_measure("Servers", "Register Singletons");
}
