|
@@ -74,6 +74,7 @@ The optional *manifests* folder of an extension contains additional files used i
|
|
|
|
|
|
Extensions are treated just like any other assets in your project and they can be shared in the same way. If a native extension folder is added as a Library folder it can be shared and used by others as a project dependency. Refer to the [Library project manual](/manuals/libraries/) for more information.
|
|
|
|
|
|
+
|
|
|
## A simple example extension
|
|
|
|
|
|
Let's build a very simple extension. First, we create a new root folder *myextension* and add a file *ext.manifest* containing the name of the extension "MyExtension". Note that the name is a C++ symbol and must match the first argument to `DM_DECLARE_EXTENSION` (see below).
|
|
@@ -99,42 +100,27 @@ The extension source file contains the following code:
|
|
|
|
|
|
// include the Defold SDK
|
|
|
#include <dmsdk/sdk.h>
|
|
|
-#include <stdlib.h>
|
|
|
|
|
|
-static int Rot13(lua_State* L)
|
|
|
+static int Reverse(lua_State* L)
|
|
|
{
|
|
|
- int top = lua_gettop(L);
|
|
|
+ // The number of expected items to be on the Lua stack
|
|
|
+ // once this struct goes out of scope
|
|
|
+ DM_LUA_STACK_CHECK(L, 1);
|
|
|
|
|
|
// Check and get parameter string from stack
|
|
|
- const char* str = luaL_checkstring(L, 1);
|
|
|
+ char* str = (char*)luaL_checkstring(L, 1);
|
|
|
|
|
|
- // Allocate new string
|
|
|
+ // Reverse the string
|
|
|
int len = strlen(str);
|
|
|
- char *rot = (char *) malloc(len + 1);
|
|
|
-
|
|
|
- // Iterate over the parameter string and create rot13 string
|
|
|
- for(int i = 0; i <= len; i++) {
|
|
|
- const char c = str[i];
|
|
|
- if((c >= 'A' && c <= 'M') || (c >= 'a' && c <= 'm')) {
|
|
|
- // Between A-M just add 13 to the char.
|
|
|
- rot[i] = c + 13;
|
|
|
- } else if((c >= 'N' && c <= 'Z') || (c >= 'n' && c <= 'z')) {
|
|
|
- // If rolling past 'Z' which happens below 'M', wrap back (subtract 13)
|
|
|
- rot[i] = c - 13;
|
|
|
- } else {
|
|
|
- // Leave character intact
|
|
|
- rot[i] = c;
|
|
|
- }
|
|
|
+ for(int i = 0; i < len / 2; i++) {
|
|
|
+ const char a = str[i];
|
|
|
+ const char b = str[len - i - 1];
|
|
|
+ str[i] = b;
|
|
|
+ str[len - i - 1] = a;
|
|
|
}
|
|
|
|
|
|
- // Put the rotated string on the stack
|
|
|
- lua_pushstring(L, rot);
|
|
|
-
|
|
|
- // Free string memory. Lua has a copy by now.
|
|
|
- free(rot);
|
|
|
-
|
|
|
- // Assert that there is one item on the stack.
|
|
|
- assert(top + 1 == lua_gettop(L));
|
|
|
+ // Put the reverse string on the stack
|
|
|
+ lua_pushstring(L, str);
|
|
|
|
|
|
// Return 1 item
|
|
|
return 1;
|
|
@@ -143,7 +129,7 @@ static int Rot13(lua_State* L)
|
|
|
// Functions exposed to Lua
|
|
|
static const luaL_reg Module_methods[] =
|
|
|
{
|
|
|
- {"rot13", Rot13},
|
|
|
+ {"reverse", Reverse},
|
|
|
{0, 0}
|
|
|
};
|
|
|
|
|
@@ -199,12 +185,13 @@ To test the extension, create a game object and add a script component with some
|
|
|
|
|
|
```lua
|
|
|
local s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
-local rot_s = myextension.rot13(s)
|
|
|
-print(rot_s) --> nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM
|
|
|
+local reverse_s = myextension.reverse(s)
|
|
|
+print(reverse_s) --> ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba
|
|
|
```
|
|
|
|
|
|
And that's it! We have created a fully working native extension.
|
|
|
|
|
|
+
|
|
|
## Extension Lifecycle
|
|
|
|
|
|
As we saw above the `DM_DECLARE_EXTENSION` macro is used to declare the various entry points into the extension code:
|
|
@@ -240,6 +227,11 @@ The following identifiers are defined by the builder on each respective platform
|
|
|
* DM_PLATFORM_LINUX
|
|
|
* DM_PLATFORM_HTML5
|
|
|
|
|
|
+## Build server logs
|
|
|
+
|
|
|
+Build server logs are available when the project is using native extensions. The build server log (`log.txt`) is downloaded together with the custom engine when the project is built and stored inside the file `.internal/%platform%/build.zip`.
|
|
|
+
|
|
|
+
|
|
|
## The ext.manifest file
|
|
|
|
|
|
Apart from the name of the extension, the manifest file can contain platform specific compile flags, link flags, libs and frameworks. If the *ext.manifest* file does not contain a "platforms" segment, or a platform is missing from the list, the platform you bundle for will still build, but without any extra flags set.
|