Added various method to the .Net API

- IsKeyDown
- GetMousePosition
- GetEntity
- HasComponent
- GetComponent
- Added InputKeyCode to C#
This commit is contained in:
Antoine Pilote
2023-10-16 23:41:22 -04:00
parent c3da3fb8f1
commit 1ccd338fc6
7 changed files with 487 additions and 132 deletions

View File

@@ -9,7 +9,7 @@ namespace Nuake {
void EngineNetAPI::RegisterMethods()
{
RegisterMethod("LoggerLogIcall", (void*)(&Log));
RegisterMethod("Engine.LoggerLogIcall", (void*)(&Log));
}
}

View File

@@ -0,0 +1,28 @@
#include "InputNetAPI.h"
#include "src/Core/Input.h"
#include <Coral/NativeArray.hpp>
namespace Nuake {
bool IsKeyDown(int keyCode)
{
return Input::IsKeyDown(keyCode);
}
Coral::NativeArray<float> GetMousePosition()
{
Vector2 mousePosition = Input::GetMousePosition();
return { mousePosition.x, mousePosition.y};
}
void InputNetAPI::RegisterMethods()
{
RegisterMethod("Input.IsKeyDownIcall", &IsKeyDown);
RegisterMethod("Input.GetMousePositionIcall", &GetMousePosition);
}
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "NetAPIModule.h"
namespace Nuake {
class InputNetAPI : public NetAPIModule
{
public:
virtual const std::string GetModuleName() const override { return "Input"; }
virtual void RegisterMethods() override;
};
}

View File

@@ -0,0 +1,108 @@
#include "SceneNetAPI.h"
#include "Engine.h"
#include "src/Scene/Entities/Entity.h"
#include <src/Scene/Components/ParentComponent.h>
#include <src/Scene/Components/PrefabComponent.h>
#include <src/Scene/Components/CameraComponent.h>
#include <src/Scene/Components/AudioEmitterComponent.h>
#include <src/Scene/Components/ModelComponent.h>
#include <src/Scene/Components/SkinnedModelComponent.h>
#include <src/Scene/Components/BoneComponent.h>
#include <src/Scene/Components/BoxCollider.h>
#include <src/Scene/Components/SphereCollider.h>
#include <src/Scene/Components/CapsuleColliderComponent.h>
#include <src/Scene/Components/CylinderColliderComponent.h>
#include <src/Scene/Components/MeshCollider.h>
#include <src/Scene/Components/CharacterControllerComponent.h>
#include <src/Scene/Components/ParticleEmitterComponent.h>
#include <src/Scene/Components/BSPBrushComponent.h>
#include <src/Scene/Components/SpriteComponent.h>
#include <src/Scene/Components/QuakeMap.h>
namespace Nuake {
uint32_t GetEntity(Coral::NativeString entityName)
{
auto scene = Engine::GetCurrentScene();
std::string entityNameString = entityName.ToString();
if (!scene->EntityExists(entityNameString))
{
return UINT32_MAX; // Error code: entity not found.
}
return scene->GetEntity(entityNameString).GetHandle();
}
static enum ComponentTypes
{
Unknown = -1,
PARENT = 0,
NAME,
PREFAB,
TRANSFORM,
LIGHT,
CAMERA,
AUDIO_EMITTER,
MODEL,
SKINNED_MODEL,
BONE,
RIGIDBODY,
BOX_COLLIDER,
SPHERE_COLLIDER,
CAPSULE_COLLIDER,
CYLINDER_COLLIDER,
MESH_COLLIDER,
CHARACTER_CONTROLLER,
PARTICLE_EMITTER,
QUAKE_MAP,
BSP_BRUSH,
SPRITE
};
bool EntityHasComponent(uint32_t handle, uint32_t componentEnumValue)
{
Entity entity = { (entt::entity)handle, Engine::GetCurrentScene().get()};
if (entity.IsValid())
{
return false;
}
switch (static_cast<ComponentTypes>(componentEnumValue))
{
case PARENT: return entity.HasComponent<ParentComponent>();
case NAME: return entity.HasComponent<NameComponent>();
case PREFAB: return entity.HasComponent<PrefabComponent>();
case TRANSFORM: return entity.HasComponent<TransformComponent>();
case LIGHT: return entity.HasComponent<LightComponent>();
case CAMERA: return entity.HasComponent<CameraComponent>();
case AUDIO_EMITTER: return entity.HasComponent<AudioEmitterComponent>();
case MODEL: return entity.HasComponent<ModelComponent>();
case SKINNED_MODEL: return entity.HasComponent<SkinnedModelComponent>();
case BONE: return entity.HasComponent<BoneComponent>();
case BOX_COLLIDER: return entity.HasComponent<BoxColliderComponent>();
case SPHERE_COLLIDER: return entity.HasComponent<SphereColliderComponent>();
case CAPSULE_COLLIDER: return entity.HasComponent<CapsuleColliderComponent>();
case CYLINDER_COLLIDER: return entity.HasComponent<CylinderColliderComponent>();
case MESH_COLLIDER: return entity.HasComponent<MeshColliderComponent>();
case CHARACTER_CONTROLLER: return entity.HasComponent<CharacterControllerComponent>();
case PARTICLE_EMITTER: return entity.HasComponent<ParticleEmitterComponent>();
case QUAKE_MAP: return entity.HasComponent<QuakeMapComponent>();
case BSP_BRUSH: return entity.HasComponent<BSPBrushComponent>();
case SPRITE: return entity.HasComponent<SpriteComponent>();
default:
return false;
}
}
void Nuake::SceneNetAPI::RegisterMethods()
{
RegisterMethod("Entity.EntityHasComponentIcall", &EntityHasComponent);
RegisterMethod("Scene.GetEntityIcall", &GetEntity);
}
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "NetAPIModule.h"
namespace Nuake {
class SceneNetAPI : public NetAPIModule
{
public:
virtual const std::string GetModuleName() const override { return "Scene"; }
virtual void RegisterMethods() override;
};
}

View File

@@ -4,14 +4,16 @@
#include "src/Core/FileSystem.h"
#include "src/Core/OS.h"
#include "src/Resource/Project.h"
#include "src/Scene/Components/NetScriptComponent.h"
#include "NetModules/EngineNetAPI.h"
#include "NetModules/InputNetAPI.h"
#include "NetModules/SceneNetAPI.h"
#include <Coral/HostInstance.hpp>
#include <Coral/GC.hpp>
#include <Coral/NativeArray.hpp>
#include <Coral/Attribute.hpp>
#include <src/Scene/Components/NetScriptComponent.h>
void ExceptionCallback(std::string_view InMessage)
@@ -20,19 +22,29 @@ void ExceptionCallback(std::string_view InMessage)
Nuake::Logger::Log(message, ".net", Nuake::CRITICAL);
}
namespace Nuake
{
ScriptingEngineNet::ScriptingEngineNet()
{
// Initialize Coral
// ----------------------------------
Coral::HostSettings settings =
{
.CoralDirectory = "",
.ExceptionCallback = ExceptionCallback
};
m_HostInstance = new Coral::HostInstance();
m_HostInstance->Initialize(settings);
// Initialize API modules
// ----------------------------------
m_Modules =
{
CreateRef<EngineNetAPI>()
CreateRef<EngineNetAPI>(),
CreateRef<InputNetAPI>(),
CreateRef<SceneNetAPI>()
};
for (auto& m : m_Modules)
@@ -54,16 +66,7 @@ namespace Nuake
void ScriptingEngineNet::Initialize()
{
Coral::HostSettings settings =
{
.CoralDirectory = "",
.ExceptionCallback = ExceptionCallback
};
m_HostInstance = new Coral::HostInstance();
m_HostInstance->Initialize(settings);
m_LoadContext = std::move(m_HostInstance->CreateAssemblyLoadContext(m_ContextName));
m_LoadContext = m_HostInstance->CreateAssemblyLoadContext(m_ContextName);
// Load Nuake assembly DLL
const std::string absoluteAssemblyPath = FileSystem::Root + m_NetDirectory + "/" + m_EngineAssemblyName;
@@ -73,10 +76,10 @@ namespace Nuake
// --------------------------------------------------
for (const auto& netModule : m_Modules)
{
const std::string inClassName = m_Scope + '.' + netModule->GetModuleName();
for (const auto& [methodName, methodPtr] : netModule->GetMethods())
{
m_NuakeAssembly.AddInternalCall(inClassName, methodName, methodPtr);
auto namespaceClassSplit = String::Split(methodName, '.');
m_NuakeAssembly.AddInternalCall(m_Scope + '.' + namespaceClassSplit[0], namespaceClassSplit[1], methodPtr);
}
}
@@ -85,19 +88,17 @@ namespace Nuake
void ScriptingEngineNet::Uninitialize()
{
// We have to manually destroy every managed object we have created
for (auto& [entity, managedObject] : m_EntityToManagedObjects)
{
managedObject.Destroy();
}
Coral::GC::Collect(1, Coral::GCCollectionMode::Forced);
Coral::GC::WaitForPendingFinalizers();
m_GameEntityTypes.clear();
Coral::GC::Collect();
m_HostInstance->UnloadAssemblyLoadContext(m_LoadContext);
m_EntityToManagedObjects.clear();
m_GameEntityTypes.clear();
}
void ScriptingEngineNet::LoadProjectAssembly(Ref<Project> project)