Skip to content

Commit

Permalink
build one package per component of the MSFS SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
svengcz committed Dec 10, 2022
1 parent 32e818f commit 3b0e616
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"types": "dist/index.d.ts",
"scripts": {
"test": "npx jest",
"build:bindings": "node-gyp rebuild",
"build:bindings:dev": "node-gyp rebuild --debug",
"build:simconnect-bindings": "cd src/bindings/simconnect && node-gyp rebuild",
"build:bindings": "npm run build:simconnect-bindings",
"build:rollup": "rollup -c",
"build:all": "npm run build:bindings && npm run build:rollup",
"lint": "eslint src/**/*.ts",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
}),
copy({
targets: [
{ src: 'build/Release/msfs.node', dest: 'dist/libs/' },
{ src: 'src/bindings/simconnect/build/Release/simconnect.node', dest: 'dist/libs/' },
],
}),
],
Expand Down
9 changes: 5 additions & 4 deletions binding.gyp → src/bindings/simconnect/binding.gyp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"targets": [
{
"target_name": "msfs",
"target_name": "simconnect",
"sources": [
"src/bindings/simconnect/connection.cc",
"src/bindings/simconnect/helper.cc",
"src/bindings/msfs.cc"
"connection.cc",
"helper.cc",
"simconnect.cc",
"wrapper.cc"
],
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
"include_dirs": [
Expand Down
49 changes: 6 additions & 43 deletions src/bindings/simconnect/connection.cc
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
#include "helper.h"
#include "connection.h"
#include "wrapper.h"

#include <SimConnect.h>

using namespace msfs::simconnect;

Connection::Connection(const Napi::CallbackInfo& info) :
Napi::ObjectWrap<Connection>(info),
_simConnect(0),
_lastError() { }

Connection::~Connection() {
this->close();
}

void Connection::close() {
void Wrapper::close() {
if (this->_simConnect != 0) {
SimConnect_Close(this->_simConnect);
this->_simConnect = 0;
}
}

Napi::Value Connection::open(const Napi::CallbackInfo& info) {
Napi::Value Wrapper::open(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() != 1) {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (info[0].IsString()) {
if (!info[0].IsString()) {
Napi::TypeError::New(env, "Invalid argument type. 'name' must be a string").ThrowAsJavaScriptException();
return env.Null();
}
Expand All @@ -38,43 +29,15 @@ Napi::Value Connection::open(const Napi::CallbackInfo& info) {
std::string nameStr = name.Utf8Value();
HRESULT result = SimConnect_Open(&this->_simConnect, nameStr.c_str(), nullptr, 0, 0, 0);
if (result != S_OK) {
this->_lastError = "Unable to open a connection: " + Helper::translateException((SIMCONNECT_EXCEPTION)result);
this->_lastError = "Unable to open a Wrapper: " + Helper::translateException((SIMCONNECT_EXCEPTION)result);
return Napi::Boolean::New(env, false);
}
}

return Napi::Boolean::New(env, true);
}

void Connection::close(const Napi::CallbackInfo& info) {
void Wrapper::close(const Napi::CallbackInfo& info) {
std::ignore = info;
this->close();
}

Napi::Value Connection::lastError(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, this->_lastError);
}

Napi::Value Connection::createNewItem(const Napi::CallbackInfo& info) {
Napi::FunctionReference* constructor = info.Env().GetInstanceData<Napi::FunctionReference>();
return constructor->New({});
}

Napi::Object Connection::initialize(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "SimConnect_Connection", {
InstanceMethod<&Connection::open>("open", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&Connection::close>("close", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&Connection::lastError>("lastError", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
StaticMethod<&Connection::createNewItem>("createNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
});

Napi::FunctionReference* constructor = new Napi::FunctionReference();

*constructor = Napi::Persistent(func);
exports.Set("SimConnect_Connection", func);

env.SetInstanceData<Napi::FunctionReference>(constructor);

return exports;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <napi.h>

#include "simconnect/connection.h"
#include "wrapper.h"

using namespace msfs;
using namespace msfs::simconnect;

void test(const Napi::CallbackInfo& info) { }

Napi::Object initialize(Napi::Env env, Napi::Object exports) {
exports = simconnect::Connection::initialize(env, exports);
exports = Wrapper::initialize(env, exports);
return exports;
}

Expand Down
43 changes: 43 additions & 0 deletions src/bindings/simconnect/wrapper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "helper.h"
#include "wrapper.h"

#include <SimConnect.h>

using namespace msfs::simconnect;

Wrapper::Wrapper(const Napi::CallbackInfo& info) :
Napi::ObjectWrap<Wrapper>(info),
_simConnect(0),
_lastError() { }

Wrapper::~Wrapper() {
this->close();
}

Napi::Value Wrapper::lastError(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, this->_lastError);
}

Napi::Value Wrapper::createNewItem(const Napi::CallbackInfo& info) {
Napi::FunctionReference* constructor = info.Env().GetInstanceData<Napi::FunctionReference>();
return constructor->New({});
}

Napi::Object Wrapper::initialize(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "SimConnect", {
InstanceMethod<&Wrapper::open>("open", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&Wrapper::close>("close", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&Wrapper::lastError>("lastError", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
StaticMethod<&Wrapper::createNewItem>("createNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
});

Napi::FunctionReference* constructor = new Napi::FunctionReference();

*constructor = Napi::Persistent(func);
exports.Set("SimConnect", func);

env.SetInstanceData<Napi::FunctionReference>(constructor);

return exports;
}
60 changes: 60 additions & 0 deletions src/bindings/simconnect/wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <Windows.h>
#include <SimConnect.h>
#include <napi.h>

namespace msfs {
namespace simconnect {
class Wrapper : public Napi::ObjectWrap<Wrapper> {
private:
HANDLE _simConnect;
std::string _lastError;

void close();
public:
/**
* @brief Constructs a new Wrapper object
* @param info Parameter block for the Wrapper
*/
Wrapper(const Napi::CallbackInfo& info);
/**
* @brief Destroys the Wrapper object
*/
~Wrapper();

/**
* @brief Opens a SimConnect connection to the server
* @param info The callback block where the first element needs to be the client's name
* @return Returns a Napi::Boolean and sets the last error, if the function returned false
* @throw Excpetions if the arguments do not match
*/
Napi::Value open(const Napi::CallbackInfo& info);
/**
* @brief Closes a SimConnect connection
* @param info The parameter block without additional parameters
*/
void close(const Napi::CallbackInfo& info);
/**
* @brief Returns the last error of an other call
* @param info The parameter block without additional parameters
* @return Returns Napi::String with the last error
*/
Napi::Value lastError(const Napi::CallbackInfo& info);

/**
* @brief Creates a new Wrapper object
* @param info Parameter block for the new object
* @return Returns the newly created object
*/
static Napi::Value createNewItem(const Napi::CallbackInfo& info);
/**
* @brief Registers all binding functions in the JS environment
* @param env The javascript environment
* @param exports The collection of exported functions
* @return The extended exported functions
*/
static Napi::Object initialize(Napi::Env env, Napi::Object exports);
};
}
}

0 comments on commit 3b0e616

Please sign in to comment.