Skip to content

Commit

Permalink
refacor(runtime): more cbaseobject pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo972 committed Sep 1, 2023
1 parent 232957c commit 36bb701
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 40 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
run: runtime capi test

ifeq ($(OS), Windows_NT)
capi:
Expand All @@ -9,8 +10,9 @@ capi:
"$(CURDIR)/scripts/build-capi.sh"
runtime:
"$(CURDIR)/scripts/build-runtime.sh"
test: runtime
"$(CURDIR)/scripts/test.sh"
endif

.PHONY: capi runtime test
test:
@go test -v

.PHONY: capi runtime
24 changes: 18 additions & 6 deletions entity.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package altv

// #include <stdlib.h>
// #include "capi.h"
import "C"
import (
Expand All @@ -12,7 +13,7 @@ import (
"github.com/timo972/altv-go/internal/cutil"
)

func getEntityData(e C.struct_baseObject) (typ entity.BaseObjectType, ptr unsafe.Pointer, id uint32, model uint32) {
func getEntityData(e *C.struct_baseObject) (typ entity.BaseObjectType, ptr unsafe.Pointer, id uint32, model uint32) {
ptr = unsafe.Pointer(e.ptr)
id = uint32(e.id)
typ = entity.BaseObjectType(e.typ)
Expand All @@ -22,8 +23,9 @@ func getEntityData(e C.struct_baseObject) (typ entity.BaseObjectType, ptr unsafe
}

func newBaseObjectArray[T entity.BaseObject](arr C.struct_array) []T {
return cutil.NewArrayFunc[C.struct_baseObject, T](unsafe.Pointer(arr.ptr), int(arr.size), func(item C.struct_baseObject) T {
return cutil.NewArrayFunc[*C.struct_baseObject, T](unsafe.Pointer(arr.ptr), int(arr.size), func(item *C.struct_baseObject) T {
v, err := factory.GetBaseObject[T](getEntityData(item))
C.free(unsafe.Pointer(item))
if err != nil {
altlog.Errorln(fmt.Sprintf("[Go] newBaseObjectArray: %s", err.Error()))
}
Expand All @@ -38,18 +40,28 @@ func Players() []entity.Player {

func PlayerByID(id uint32) (entity.Player, error) {
p := C.core_get_base_object_by_id(C.uchar(entity.TypePlayer), C.uint(id))
defer C.free(unsafe.Pointer(p))
return factory.GetBaseObject[entity.Player](getEntityData(p))
}

func Vehicles() []entity.Vehicle {
arr := C.core_get_vehicles()
return newBaseObjectArray[entity.Vehicle](arr)
}

func VehicleByID(id uint32) (entity.Vehicle, error) {
e := C.core_get_base_object_by_id(C.uchar(entity.TypePlayer), C.uint(id))
return factory.GetBaseObject[entity.Vehicle](getEntityData(e))
vehicle := C.core_get_base_object_by_id(C.uchar(entity.TypePlayer), C.uint(id))
defer C.free(unsafe.Pointer(vehicle))
return factory.GetBaseObject[entity.Vehicle](getEntityData(vehicle))
}

func CreateVehicle(model uint32, pos Vector3, rot Vector3) (entity.Vehicle, error) {
// TODO: validate model beforehand. best solution: data/vehmodels.bin

e := C.core_create_vehicle(C.ulong(model), C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(rot.X), C.float(rot.Y), C.float(rot.Z))
// core_create_vehicle allocates C.struct_baseObject
baseObject := C.core_create_vehicle(C.ulong(model), C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(rot.X), C.float(rot.Y), C.float(rot.Z))
// free memory allocated by runtime
defer C.free(unsafe.Pointer(baseObject))

return factory.GetBaseObject[entity.Vehicle](getEntityData(e))
return factory.GetBaseObject[entity.Vehicle](getEntityData(baseObject))
}
1 change: 1 addition & 0 deletions event/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (s *unsubscriber) Stop(id int) error {

//export OnStart
func OnStart() {
processEventQueue()
for _, event := range once.startEvents {
event()
}
Expand Down
2 changes: 1 addition & 1 deletion event/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func unregisterOnEvent(event uint16) {
C.runtime_unregister_alt_event(cresource, C.ushort(event))
}

func processQueue() {
func processEventQueue() {
for event := range queue {
registerOnEvent(event)
}
Expand Down
18 changes: 9 additions & 9 deletions factory/factories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ import (
"github.com/timo972/altv-go/entity"
)

type testPlayer struct {
type myPlayer struct {
entity.Player
username string
}

func testPlayerFactory(ptr unsafe.Pointer, id uint32) entity.Player {
return &testPlayer{
func myPlayerFactory(ptr unsafe.Pointer, id uint32) entity.Player {
return &myPlayer{
Player: entity.NewPlayer(ptr, id),
}
}

func TestPlayerFactory(t *testing.T) {
SetPlayerFactory(testPlayerFactory)
p, ok := testPlayerFactory(nil, 1).(*testPlayer)
SetPlayerFactory(myPlayerFactory)
p, ok := myPlayerFactory(nil, 1).(*myPlayer)
if !ok {
t.Error("testPlayerFactory returned wrong type")
}

p.username = "test"
}

type testVehicle struct {
type myVehicle struct {
entity.Vehicle
}

func testVehicleFactory(ptr unsafe.Pointer, id uint32, model uint32) entity.Vehicle {
return &testVehicle{
func myVehicleFactory(ptr unsafe.Pointer, id uint32, model uint32) entity.Vehicle {
return &myVehicle{
Vehicle: entity.NewVehicle(ptr, id, model),
}
}

func TestVehicleFactory(t *testing.T) {
SetVehicleFactory(testVehicleFactory)
SetVehicleFactory(myVehicleFactory)
}
12 changes: 6 additions & 6 deletions internal/c-api/lib/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ typedef void (*capi_core_log_error)(const char *message);

typedef void (*capi_core_log_colored)(const char *message);

typedef CBaseObject (*capi_core_create_vehicle)(unsigned long model, float posX, float posY, float posZ,
typedef CBaseObject * (*capi_core_create_vehicle)(unsigned long model, float posX, float posY, float posZ,
float rotX, float rotY, float rotZ);

typedef CBaseObject
Expand All @@ -453,9 +453,9 @@ typedef int (*capi_core_file_exists)(const char *path);

typedef const char * (*capi_core_read_file)(const char *path);

typedef CBaseObject (*capi_core_get_entity_by_id)(unsigned short id);
typedef CBaseObject * (*capi_core_get_entity_by_sync_id)(unsigned short id);

typedef CBaseObject (*capi_core_get_base_object_by_id)(unsigned char type, unsigned int id);
typedef CBaseObject * (*capi_core_get_base_object_by_id)(unsigned char type, unsigned int id);

typedef CArray (*capi_core_get_entities)();

Expand Down Expand Up @@ -1622,7 +1622,7 @@ void core_log_error(const char *message);

void core_log_colored(const char *message);

CBaseObject core_create_vehicle(unsigned long model, float posX, float posY, float posZ,
CBaseObject * core_create_vehicle(unsigned long model, float posX, float posY, float posZ,
float rotX, float rotY, float rotZ);

CBaseObject
Expand All @@ -1643,9 +1643,9 @@ int core_file_exists(const char *path);

const char * core_read_file(const char *path);

CBaseObject core_get_entity_by_id(unsigned short id);
CBaseObject * core_get_entity_by_sync_id(unsigned short id);

CBaseObject core_get_base_object_by_id(unsigned char type, unsigned int id);
CBaseObject * core_get_base_object_by_id(unsigned char type, unsigned int id);

CArray core_get_entities();

Expand Down
Binary file modified internal/c-api/lib/linux/libcapi.a
Binary file not shown.
19 changes: 16 additions & 3 deletions runtime/src/GoResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Go::Resource::Resource(Go::Runtime *runtime, alt::IResource *resource) : _runtime(runtime), _resource(resource) {}

bool Go::Resource::Start() {
// load go library
Module = LOAD_LIB((_resource->GetPath() + SEPARATOR + _resource->GetMain()).c_str());
if (Module == nullptr) {
alt::ICore::Instance()
Expand All @@ -11,8 +12,6 @@ bool Go::Resource::Start() {
return false;
}

auto resourceName = _resource->GetName().c_str();
auto resourcePath = _resource->GetPath().c_str();
auto go = GET_FUNC(Module, "initGoResource",
int(*)(alt::IResource * resourcePtr, const char *resourceName, const char *ResourcePath, const char *version));
if (go == nullptr) {
Expand All @@ -22,18 +21,21 @@ bool Go::Resource::Start() {
return false;
}

// initialize go resource - trigger capi loading of runtime C exports
auto resourceName = _resource->GetName().c_str();
auto resourcePath = _resource->GetPath().c_str();
bool ok = go(_resource, resourceName, resourcePath, GO_MODULE_VERSION);
if (!ok) {
return ok;
}


auto start = GET_FUNC(Module, "OnStart", void(*)());
if (start == nullptr) {
alt::ICore::Instance().LogError("Main entrypoint not found");
return false;
}

// register event handlers
RegisterEventHandler(alt::CEvent::Type::PLAYER_CONNECT, new PlayerConnectEvent(Module));
RegisterEventHandler(alt::CEvent::Type::PLAYER_DISCONNECT, new PlayerDisconnectEvent(Module));
RegisterEventHandler(alt::CEvent::Type::PLAYER_DAMAGE, new PlayerDamageEvent(Module));
Expand Down Expand Up @@ -72,14 +74,17 @@ bool Go::Resource::Start() {
RegisterEventHandler(alt::CEvent::Type::PLAYER_CHANGE_ANIMATION_EVENT, new PlayerChangeAnimationEvent(Module));
RegisterEventHandler(alt::CEvent::Type::PLAYER_CHANGE_INTERIOR_EVENT, new PlayerChangeInteriorEvent(Module));

// call go library entrypoint
start();

// set resource exports
_resource->SetExports(_registeredExports);

return true;
}

bool Go::Resource::Stop() {
std::cout << "Go::Resource::Stop" << std::endl;
auto stop = GET_FUNC(Module, "OnStop", void(*)());
if (stop == nullptr) {
alt::ICore::Instance().LogError("Couldn't call OnStop.");
Expand All @@ -91,12 +96,15 @@ bool Go::Resource::Stop() {
// TODO: UNLOAD_LIB(Module)
// https://github.com/golang/go/issues/11100

std::cout << "Go::Resource::Stop succeeded" << std::endl;

return true;
}

void Go::Resource::OnEvent(const alt::CEvent *ev) {
auto type = ev->GetType();

std::cout << "Go::Resource::OnEvent" << std::endl;
NotifyEvent(ev, _resource->GetName().c_str());
}

Expand All @@ -121,14 +129,19 @@ void Go::Resource::OnCreateBaseObject(alt::IBaseObject* handle) {
}

void Go::Resource::OnRemoveBaseObject(alt::IBaseObject* handle) {
std::cout << "Go::Resource::OnRemoveBaseObject" << std::endl;
this->RemoveEntity(handle);

CBaseObject object;
Go::Runtime::GetCBaseObject(handle, &object);
std::cout << "Go::Runtime::GetCBaseObject" << std::endl;

static auto removeEntity = GET_FUNC(Module, "altRemoveBaseObject", void(*)(CBaseObject*));
if (removeEntity == nullptr) {
alt::ICore::Instance().LogError("Could not call altRemoveBaseObject.");
return;
}

std::cout << "executing altRemoveBaseObject" << std::endl;
removeEntity(&object);
}
5 changes: 3 additions & 2 deletions runtime/src/GoRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ namespace Go {
CArray arr;
arr.size = objects.size();

auto entities = new CBaseObject[arr.size];
// TODO: test if this works
auto entities = new CBaseObject*[arr.size];

for (uint64_t i = 0; i < arr.size; i++) {
entities[i] = GetCBaseObject(objects[i]);
GetCBaseObject(objects[i], entities[i]);
}

arr.ptr = entities;
Expand Down
19 changes: 12 additions & 7 deletions runtime/src/capi/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ EXPORT void Core_LogColored(const char *message) {
alt::ICore::Instance().LogColored(message);
}

EXPORT CBaseObject Core_CreateVehicle(unsigned long model, float posX, float posY, float posZ,
EXPORT CBaseObject *Core_CreateVehicle(unsigned long model, float posX, float posY, float posZ,
float rotX, float rotY, float rotZ) {
alt::Position position(posX, posY, posZ);
alt::Rotation rotation(rotX, rotY, rotZ);

auto vehicle = alt::ICore::Instance().CreateVehicle(model, position, rotation);
return Go::Runtime::GetCBaseObject(vehicle);
CBaseObject *vehicle = (CBaseObject*) malloc(sizeof(CBaseObject));
Go::Runtime::GetCBaseObject(alt::ICore::Instance().CreateVehicle(model, position, rotation), vehicle);
return vehicle;
}

EXPORT CBaseObject Core_CreateCheckpoint(unsigned char type, float x, float y, float z, float radius, float height, unsigned char r,
Expand Down Expand Up @@ -74,16 +75,20 @@ EXPORT const char *Core_ReadFile(const char *path) {
return content.c_str();
}

EXPORT CBaseObject Core_GetEntityByID(unsigned short id) {
EXPORT CBaseObject *Core_GetEntityBySyncID(unsigned short id) {
auto entity = alt::ICore::Instance().GetEntityBySyncID(id);

return Go::Runtime::GetCBaseObject(entity);
CBaseObject *ent = (CBaseObject *)malloc(sizeof(CBaseObject));
Go::Runtime::GetCBaseObject(entity, ent);
return ent;
}

EXPORT CBaseObject Core_GetBaseObjectByID(unsigned char type, unsigned int id) {
EXPORT CBaseObject *Core_GetBaseObjectByID(unsigned char type, unsigned int id) {
auto baseObject = alt::ICore::Instance().GetBaseObjectByID(static_cast<alt::IBaseObject::Type>(type), id);

return Go::Runtime::GetCBaseObject(baseObject);
CBaseObject *obj = (CBaseObject *)malloc(sizeof(CBaseObject));
Go::Runtime::GetCBaseObject(baseObject, obj);
return obj;
}

EXPORT CArray Core_GetEntities() {
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/capi/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ EXPORT void Core_LogDebug(const char *message);
EXPORT void Core_LogWarning(const char *message);
EXPORT void Core_LogError(const char *message);
EXPORT void Core_LogColored(const char *message);
EXPORT CBaseObject Core_CreateVehicle(unsigned long model, float posX, float posY, float posZ,
EXPORT CBaseObject *Core_CreateVehicle(unsigned long model, float posX, float posY, float posZ,
float rotX, float rotY, float rotZ);
EXPORT CBaseObject
Core_CreateCheckpoint(unsigned char type, float x, float y, float z, float radius, float height, unsigned char r,
Expand All @@ -25,8 +25,8 @@ EXPORT int Core_IsDebug();
EXPORT unsigned long Core_Hash(const char *str);
EXPORT int Core_FileExists(const char *path);
EXPORT const char *Core_ReadFile(const char *path);
EXPORT CBaseObject Core_GetEntityByID(unsigned short id);
EXPORT CBaseObject Core_GetBaseObjectByID(unsigned char type, unsigned int id);
EXPORT CBaseObject *Core_GetEntityBySyncID(unsigned short id);
EXPORT CBaseObject *Core_GetBaseObjectByID(unsigned char type, unsigned int id);
EXPORT CArray Core_GetEntities();
EXPORT CArray Core_GetPlayers();
EXPORT CArray Core_GetVehicles();
Expand Down

0 comments on commit 36bb701

Please sign in to comment.