GLUE for Service Script API

This commit is contained in:
Chewico 2025-06-24 19:02:27 +02:00
parent e83c55f0e4
commit 3cc97f579f
15 changed files with 205 additions and 86 deletions

View File

@ -67,8 +67,8 @@ Size=104,68
Collapsed=0
[Window][Test]
Pos=60,60
Size=55,82
Pos=560,275
Size=396,176
Collapsed=0
[Docking][Data]

View File

@ -1,14 +1,127 @@
#include "ServiceScriptGenericFunction.h"
#include "Deer/Log.h"
#include "DeerStudio/EditorEngine/ErrorHandle.h"
#include "angelscript.h"
namespace Deer {
void EditorEngine::apiFunction(asIScriptGeneric* func) {
DEER_CORE_TRACE("Called function {0}",
func->GetFunction()->GetDeclaration());
bool copyParameter(asIScriptContext* ctx, int paramIndex, int typeId,
asDWORD flags, asIScriptGeneric* gen);
bool copyReturnValue(asIScriptGeneric* gen, asIScriptContext* ctx,
asIScriptFunction* func);
asIScriptFunction* aux_funx = (asIScriptFunction*)func->GetAuxiliary();
DEER_CORE_TRACE("Real function {0}", aux_funx->GetDeclaration());
void EditorEngine::apiFunction(asIScriptGeneric* func) {
ServiceScriptFunction* serviceScriptFunction =
(ServiceScriptFunction*)func->GetAuxiliary();
asIScriptContext* context = serviceScriptFunction->scriptContext;
asIScriptFunction* apiFunction = serviceScriptFunction->apiFunction;
DEER_CORE_ASSERT(context, "Nullptr service script context calling {0}",
func->GetFunction()->GetDeclaration());
AS_CHECK(context->Prepare(apiFunction));
AS_CHECK(context->SetObject(serviceScriptFunction->scriptObject));
// Extract atributes
asUINT paramCount = apiFunction->GetParamCount();
for (asUINT i = 0; i < paramCount; i++) {
int type;
asDWORD flags;
apiFunction->GetParam(i, &type, &flags, nullptr);
copyParameter(context, i, type, flags, func);
}
AS_CHECK(context->Execute());
copyReturnValue(func, context, apiFunction);
AS_CHECK(context->Unprepare());
}
bool copyParameter(asIScriptContext* ctx, int paramIndex, int typeId,
asDWORD flags, asIScriptGeneric* gen) {
if (flags & asTM_OUTREF) {
// Skip out refs for now
// Unsupported type
DEER_EDITOR_ENGINE_WARN(
"Skipping our ref type {0} when calling {1}",
ctx->GetEngine()->GetTypeDeclaration(typeId, true),
gen->GetFunction()->GetDeclaration());
return false;
}
if (typeId == asTYPEID_INT32) {
ctx->SetArgDWord(paramIndex, gen->GetArgDWord(paramIndex));
} else if (typeId == asTYPEID_FLOAT) {
ctx->SetArgFloat(paramIndex, gen->GetArgFloat(paramIndex));
} else if (typeId == asTYPEID_DOUBLE) {
ctx->SetArgDouble(paramIndex, gen->GetArgDouble(paramIndex));
} else if (typeId == asTYPEID_INT64) {
ctx->SetArgQWord(paramIndex, gen->GetArgQWord(paramIndex));
} else if (typeId & asTYPEID_OBJHANDLE) {
void* obj =
gen->GetArgAddress(paramIndex); // Use GetArgAddress instead of
// GetArgObject for handles
ctx->SetArgObject(paramIndex, obj);
} else if (typeId & asTYPEID_MASK_OBJECT) {
void* obj = gen->GetArgObject(paramIndex);
ctx->SetArgObject(paramIndex, obj);
} else {
// Unsupported type
DEER_EDITOR_ENGINE_WARN(
"Unsuported type {0} when calling {1}",
ctx->GetEngine()->GetTypeDeclaration(typeId, true),
gen->GetFunction()->GetDeclaration());
return false;
}
return true;
}
bool copyReturnValue(asIScriptGeneric* gen, asIScriptContext* ctx,
asIScriptFunction* func) {
int returnTypeId = func->GetReturnTypeId();
asITypeInfo* typeInfo = ctx->GetEngine()->GetTypeInfoById(returnTypeId);
if (returnTypeId == asTYPEID_VOID) {
// Nothing to return
return true;
}
if (returnTypeId == asTYPEID_INT32) {
gen->SetReturnDWord(ctx->GetReturnDWord());
} else if (returnTypeId == asTYPEID_FLOAT) {
gen->SetReturnFloat(ctx->GetReturnFloat());
} else if (returnTypeId == asTYPEID_DOUBLE) {
gen->SetReturnDouble(ctx->GetReturnDouble());
} else if (returnTypeId == asTYPEID_INT64) {
gen->SetReturnQWord(ctx->GetReturnQWord());
} else if (returnTypeId & asTYPEID_OBJHANDLE) {
void* obj = ctx->GetReturnAddress(); // For handles, this returns
// the pointer
gen->SetReturnAddress(obj);
} else if (returnTypeId & asTYPEID_MASK_OBJECT) {
bool isRef = (typeInfo->GetFlags() & asOBJ_REF) != 0;
if (isRef) {
void* obj = ctx->GetReturnObject();
gen->SetReturnObject(obj);
} else {
void* src = ctx->GetReturnObject();
void* dst = gen->GetAddressOfReturnLocation();
memcpy(dst, src, typeInfo->GetSize());
}
} else {
DEER_EDITOR_ENGINE_WARN(
"Unsupported return type {0} in call to {1}",
ctx->GetEngine()->GetTypeDeclaration(returnTypeId, true),
func->GetDeclaration());
return false;
}
return true;
}
} // namespace Deer

View File

@ -1,11 +1,20 @@
#pragma once
#include "angelscript.h"
class asIScriptGeneric;
class asIScriptFunction;
class asIScriptContext;
class asIScriptObject;
namespace Deer {
namespace EditorEngine {
struct ServiceScriptObject;
struct ServiceScriptFunction {
asIScriptFunction* apiFunction = nullptr;
asIScriptContext* scriptContext = nullptr;
asIScriptObject* scriptObject = nullptr;
};
void apiFunction(asIScriptGeneric *gen);
void apiFunction(asIScriptGeneric* gen);
} // namespace EditorEngine
} // namespace Deer

View File

@ -15,7 +15,7 @@
namespace Deer {
namespace EditorEngine {
ServiceScriptObject::ServiceScriptObject(
asITypeInfo *_type, asIScriptContext *_scriptContext)
asITypeInfo* _type, asIScriptContext* _scriptContext)
: type(_type), scriptContext(_scriptContext) {
// Constructor
// "type@ type()"
@ -34,7 +34,7 @@ namespace Deer {
callString += type->GetName();
callString += "()";
asIScriptFunction *factory =
asIScriptFunction* factory =
type->GetFactoryByDecl(callString.c_str());
AS_CHECK(scriptContext->Prepare(factory));
@ -43,7 +43,7 @@ namespace Deer {
// Return value contains the ref to a asIScriptObject in the
// location provided
object =
*(asIScriptObject **)scriptContext->GetAddressOfReturnValue();
*(asIScriptObject**)scriptContext->GetAddressOfReturnValue();
if (!object) {
DEER_EDITOR_ENGINE_ERROR("Could not create object",
type->GetName());
@ -83,7 +83,7 @@ namespace Deer {
}
ServiceScriptObject::ServiceScriptObject(
ServiceScriptObject &&other) noexcept {
ServiceScriptObject&& other) noexcept {
type = other.type;
object = other.object;
updateFunction = other.updateFunction;
@ -97,8 +97,8 @@ namespace Deer {
other.scriptContext = nullptr;
}
ServiceScriptObject &
ServiceScriptObject::operator=(ServiceScriptObject &&other) noexcept {
ServiceScriptObject&
ServiceScriptObject::operator=(ServiceScriptObject&& other) noexcept {
if (&other != this) {
type = other.type;
object = other.object;
@ -117,13 +117,13 @@ namespace Deer {
void ServiceScriptObject::bindFunctions() {
for (int i = 0; i < type->GetMethodCount(); i++) {
asIScriptFunction *func = type->GetMethodByIndex(i);
asIScriptFunction* func = type->GetMethodByIndex(i);
std::vector<std::string> metadata =
scriptBuilder.GetMetadataForTypeMethod(type->GetTypeId(),
func);
bool containsServiceAPI = false;
for (std::string &str : metadata) {
for (std::string& str : metadata) {
if (str != "ServiceAPI")
continue;
containsServiceAPI = true;
@ -136,7 +136,7 @@ namespace Deer {
DEER_CORE_INFO("Registering function from object {0}.{1}",
type->GetName(), func->GetName());
asITypeInfo *retType =
asITypeInfo* retType =
EditorEngine::scriptEngine->GetTypeInfoById(
func->GetReturnTypeId());
@ -156,10 +156,10 @@ namespace Deer {
ext_dec << ", ";
int typeId;
const char *name;
const char* name;
func->GetParam(i, &typeId, nullptr, &name);
asITypeInfo *paramType =
asITypeInfo* paramType =
scriptEngine->GetTypeInfoById(typeId);
ext_dec << paramType->GetName();
@ -171,9 +171,16 @@ namespace Deer {
DEER_CORE_INFO(ext_dec.str().c_str());
ServiceScriptFunction* serviceScriptFunction =
new ServiceScriptFunction();
serviceScriptFunction->apiFunction = func;
serviceScriptFunction->scriptContext = scriptContext;
serviceScriptFunction->scriptObject = object;
AS_CHECK(scriptEngine->RegisterGlobalFunction(
ext_dec.str().c_str(), asFUNCTION(apiFunction),
asCALL_GENERIC, func));
asCALL_GENERIC, serviceScriptFunction));
}
}
} // namespace EditorEngine

View File

@ -1,44 +1,42 @@
#include "DeerStudio/DeerStudio.h"
#include "DeerStudio/EditorEngine.h"
#include "DeerStudio/EditorEngine/DockPanel/DockPanelObject.h"
#include "DeerStudio/EditorEngine/DockPanel/DockPanelContext.h"
#include "DeerStudio/EditorEngine/DockPanel/DockPanelObject.h"
#include "imgui.h"
namespace Deer {
namespace DeerStudio {
void panelEnable(EditorEngine::DockPanelObject& dpo);
void onPannelMenuBar() {
if (ImGui::BeginMenu("Dock Panels Show/Hide")) {
for (EditorEngine::DockPanelContext& pc : EditorEngine::dockPanelModules) {
if (pc.dockPanels.size() == 0)
continue;
if (pc.dockPanels.size() == 1){
panelEnable(pc.dockPanels[0]);
continue;
}
const char* name = pc.getName();
if (ImGui::BeginMenu(name)) {
for (EditorEngine::DockPanelObject& panel : pc.dockPanels) {
panelEnable(panel);
}
ImGui::EndMenu();
}
}
ImGui::EndMenu();
}
}
namespace DeerStudio {
void panelEnable(EditorEngine::DockPanelObject& dpo);
void panelEnable(EditorEngine::DockPanelObject& dpo) {
const char* name = dpo.getName();
bool enabled = dpo.getFlag(DockPannelFlag_ShowPannel);
if (ImGui::MenuItem(name, nullptr, enabled)) {
dpo.setFlag(DockPannelFlag_ShowPannel, !enabled);
}
}
}
}
void onPannelMenuBar() {
for (EditorEngine::DockPanelContext& pc :
EditorEngine::dockPanelModules) {
if (pc.dockPanels.size() == 0)
continue;
if (pc.dockPanels.size() == 1) {
panelEnable(pc.dockPanels[0]);
continue;
}
const char* name = pc.getName();
if (ImGui::BeginMenu(name)) {
for (EditorEngine::DockPanelObject& panel : pc.dockPanels) {
panelEnable(panel);
}
ImGui::EndMenu();
}
}
}
void panelEnable(EditorEngine::DockPanelObject& dpo) {
const char* name = dpo.getName();
bool enabled = dpo.getFlag(DockPannelFlag_ShowPannel);
if (ImGui::MenuItem(name, nullptr, enabled)) {
dpo.setFlag(DockPannelFlag_ShowPannel, !enabled);
}
}
} // namespace DeerStudio
} // namespace Deer

View File

@ -1,11 +1,10 @@
void addComponentPopup(any@ data) {
Entity entity;
data.retrieve(entity);
UI::titleCenter("Component");
UI::separator();
UI::menuSpace("Rendering", any(entity), addComponentRendering );
UI::menuSpace("Rendering", any(entity), addComponentRendering);
if (UI::menuItem("Script Component")) {
}
}
@ -37,4 +36,4 @@ void addComponentRendering(any@ data) {
entity.createCameraComponent();
}
}
}
}

View File

@ -7,10 +7,10 @@ void renderCameraComponent(any@ data) {
CameraComponent cameraComponent = entity.getCameraComponent();
float fov = cameraComponent.fov;
float aspect = cameraComponent.aspectRatio;
float nearZ = cameraComponent.nearZ;
float farZ = cameraComponent.farZ;
float fov = cameraComponent.fov;
float aspect = cameraComponent.aspectRatio;
float nearZ = cameraComponent.nearZ;
float farZ = cameraComponent.farZ;
fov = UI::magicSlider("Fov", fov, 0.1);
if (fov > 180)
@ -31,10 +31,10 @@ void renderCameraComponent(any@ data) {
if (nearZ > farZ)
farZ = nearZ;
cameraComponent.fov = fov;
cameraComponent.aspectRatio = aspect;
cameraComponent.fov = fov;
cameraComponent.aspectRatio = aspect;
cameraComponent.nearZ = nearZ;
cameraComponent.farZ = farZ;
cameraComponent.farZ = farZ;
}
void removeCameraComponent(any@ entity) {
@ -44,4 +44,4 @@ void removeCameraComponent(any@ entity) {
if (UI::menuItem("Remove")) {
ent.removeCameraComponent();
}
}
}

View File

@ -1,4 +1,3 @@
void renderMeshComponent(any@ data) {
Entity entity;
data.retrieve(entity);
@ -42,8 +41,7 @@ void renderMeshComponent(any@ data) {
}
}
void setMeshComponentMesh(any@ meshComponent_data, any@ mesh_data){
void setMeshComponentMesh(any@ meshComponent_data, any@ mesh_data) {
string mesh;
mesh_data.retrieve(mesh);
@ -60,4 +58,4 @@ void removeMeshComponent(any@ entity) {
if (UI::menuItem("Remove")) {
ent.removeMeshComponent();
}
}
}

View File

@ -1,11 +1,10 @@
class PropertiesPannel : DockPanel {
float slider = 0;
vec3 slid;
vec3 slid2;
void onRender() {
Entity entity = activeEntity;
Entity entity = Chewico::ActiveEntity::ActiveEntity::getActiveEntity();
// We don't want to change root options
if (entity.isRoot)
@ -19,7 +18,6 @@ class PropertiesPannel : DockPanel {
UI::separator();
UI::textColor(0.5, 0.5, 0.5f, "Id : " + entity.id);
UI::sameline();
if (UI::buttonEnd("Add Component")) {
UI::openPopup("ADD_COMPONENT", any(entity));
@ -40,8 +38,7 @@ class PropertiesPannel : DockPanel {
if (entity.hasCameraComponent()) {
UI::componentNode_contextMenu("Camera Component", any(entity), renderCameraComponent, removeCameraComponent);
}
UI::space();
UI::separator();
@ -65,4 +62,4 @@ class PropertiesPannel : DockPanel {
}
}
}
}

View File

@ -1,4 +1,3 @@
void setShaderComponent(any@ shaderComponent_data, any@ shader_data) {
string shader;
shader_data.retrieve(shader);

View File

@ -21,7 +21,7 @@ class TreePannel : DockPanel {
for (int i = 0; i < entity.childs.count; i++) {
Entity child = entity.childs[i];
bool isActive = child == activeEntity;
bool isActive = child == Chewico::ActiveEntity::ActiveEntity::getActiveEntity();
string displayName = child.name;
if (displayName == "") {
@ -52,7 +52,7 @@ class TreePannel : DockPanel {
// We can't select the entity
if (UI::isItemClicked(0)) {
activeEntity = entity;
Chewico::ActiveEntity::ActiveEntity::setActiveEntity(entity);
}
}

View File

@ -1,2 +0,0 @@
Entity activeEntity = Engine::getRoot();

View File

@ -1,4 +1,3 @@
void renameEntity(any@ data) {
Entity entity;
data.retrieve(entity);
@ -11,4 +10,4 @@ void renameEntity(any@ data) {
if (UI::button("Accept")) {
UI::closePopup();
}
}
}

View File

@ -1,6 +1,8 @@
class Test : DockPanel {
void onRender() {
Entity ent = Chewico::ActiveEntity::ActiveEntity::getActiveEntity();
UI::text("Hi");
Chewico::ActiveEntity::ActiveEntity::getActiveEntity();
UI::text(ent.name);
}
}

View File

@ -14,4 +14,4 @@ class ActiveEntity : ServiceScript {
void setActiveEntity(Entity ent) {
entity = ent;
}
}
}