Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dapetcu21 committed Sep 30, 2021
0 parents commit 1db17ad
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Defold Protocol Buffer Text Files (https://github.com/github/linguist/issues/5091)
*.animationset linguist-language=JSON5
*.atlas linguist-language=JSON5
*.camera linguist-language=JSON5
*.collection linguist-language=JSON5
*.collectionfactory linguist-language=JSON5
*.collectionproxy linguist-language=JSON5
*.collisionobject linguist-language=JSON5
*.cubemap linguist-language=JSON5
*.display_profiles linguist-language=JSON5
*.factory linguist-language=JSON5
*.font linguist-language=JSON5
*.gamepads linguist-language=JSON5
*.go linguist-language=JSON5
*.gui linguist-language=JSON5
*.input_binding linguist-language=JSON5
*.label linguist-language=JSON5
*.material linguist-language=JSON5
*.mesh linguist-language=JSON5
*.model linguist-language=JSON5
*.particlefx linguist-language=JSON5
*.render linguist-language=JSON5
*.sound linguist-language=JSON5
*.sprite linguist-language=JSON5
*.spinemodel linguist-language=JSON5
*.spinescene linguist-language=JSON5
*.texture_profiles linguist-language=JSON5
*.tilemap linguist-language=JSON5
*.tilesource linguist-language=JSON5

# Defold JSON Files
*.buffer linguist-language=JSON

# Defold GLSL Shaders
*.fp linguist-language=GLSL
*.vp linguist-language=GLSL

# Defold Lua Files
*.editor_script linguist-language=Lua
*.render_script linguist-language=Lua
*.script linguist-language=Lua
*.gui_script linguist-language=Lua
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.internal
/build
.externalToolBuilders
.DS_Store
Thumbs.db
.lock-wscript
*.pyc
.project
.cproject
builtins
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Defold

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Defold Project Directory extension
This is a small native extension that makes it so that you can get the project directory of a game
that is being run from the editor (where bundled resources are not available).

```lua
-- Will return nil when not running from the editor or the path to the project root otherwise
print(defoldprojectdir.get_project_directory())

-- Will return the path to the game's executable (or nil on unsupported platforms)
print(defoldprojectdir.get_executable_path())

-- The path separator ("/" on POSIX, "\\" on Windows)
print(defoldprojectdir.PATH_SEP)
```
2 changes: 2 additions & 0 deletions defoldprojectdir/ext.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# C++ symbol in your extension
name: "defoldprojectdir"
216 changes: 216 additions & 0 deletions defoldprojectdir/src/extension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// myextension.cpp
// Extension lib defines
#define LIB_NAME "defoldprojectdir"
#define MODULE_NAME "defoldprojectdir"

// include the Defold SDK
#include <dmsdk/sdk.h>

#ifndef _WIN32
#include <libgen.h>
#else
#include <Shlwapi.h>
#endif

#include <string.h>
#include <stdlib.h>

#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif

#ifdef __linux__
#include <unistd.h>
#include <linux/limits.h>
#endif

#if defined(_WIN32)
#define SEP "\\"
#define SEPCH '\\'
#else
#define SEP "/"
#define SEPCH '/'
#endif

static char * getExecutablePath() {
char *exePath = NULL;

#ifdef __APPLE__
uint32_t bufsize = 0;
_NSGetExecutablePath(NULL, &bufsize);
exePath = (char*)malloc(bufsize);
_NSGetExecutablePath(exePath, &bufsize);

#elif defined(__linux__)
exePath = (char*)malloc(PATH_MAX + 2);
ssize_t ret = readlink("/proc/self/exe", exePath, PATH_MAX + 2);
if (ret >= 0 && ret <= PATH_MAX + 1) {
exePath[ret] = 0;
} else {
exePath[0] = 0;
}

#elif defined(_WIN32)
exePath = (char*)malloc(MAX_PATH);
size_t ret = GetModuleFileNameA(GetModuleHandle(NULL), exePath, MAX_PATH);
if (ret <= 0 || ret >= MAX_PATH) {
exePath[0] = 0;
}
#endif

return exePath;
}

static int GetExecutablePath(lua_State* L) {
char *exePath = getExecutablePath();
if (exePath && exePath[0]) {
lua_pushstring(L, exePath);
} else {
lua_pushnil(L);
}
free(exePath);
return 1;
}

#if defined(DM_PLATFORM_OSX) || defined(DM_PLATFORM_WINDOWS) || defined(DM_PLATFORM_LINUX)

#ifdef _WIN32
static char * dirname(char * path) {
size_t i = strlen(path);
do {
i -= 1;
if (path[i] == SEPCH) {
path[i] = 0;
break;
}
} while (i);
return path;
}
#endif

static bool endsIn(const char * haystack, const char * needle) {
size_t needleLen = strlen(needle);
size_t haystackLen = strlen(haystack);
return (haystackLen >= needleLen && 0 == strcmp(needle, haystack + haystackLen - needleLen));
}

static char * getProjectDir() {
char *exePath = getExecutablePath();

// Detect if the game is running in the editor

#if defined(__APPLE__)
#define FMB_PLATFORM "osx"
#define FMB_PLATFORM_ALT "darwin"
#define FMB_EXT ""
#elif defined(__linux__)
#define FMB_PLATFORM "linux"
#define FMB_EXT ""
#elif defined(_WIN32)
#define FMB_PLATFORM "win32"
#define FMB_EXT ".exe"
#endif

#if defined(__x86_64__) || defined(_M_X64)
#define FMB_ARCH "x86_64"
#elif defined(__i386) || defined(_M_IX86)
#define FMB_ARCH "x86"
#endif

#if defined(FMB_PLATFORM) && defined(FMB_ARCH)
#define FMB_EDITOR_SUFFIX SEP "build" SEP FMB_ARCH "-" FMB_PLATFORM SEP "dmengine" FMB_EXT

#ifdef FMB_PLATFORM_ALT
#define FMB_EDITOR_SUFFIX_ALT SEP "build" SEP FMB_ARCH "-" FMB_PLATFORM_ALT SEP "dmengine" FMB_EXT
if (endsIn(exePath, FMB_EDITOR_SUFFIX) || endsIn(exePath, FMB_EDITOR_SUFFIX_ALT)) {
#else
if (endsIn(exePath, FMB_EDITOR_SUFFIX)) {
#endif
char* projPath = (char*)dirname(dirname(dirname(exePath)));
return projPath;
}
#endif

free(exePath);
return NULL;
}

#else

static char * getProjectDir() {
return NULL;
}

#endif

static int GetProjectDir(lua_State* L) {
char *projPath = getProjectDir();
if (projPath && projPath[0]) {
lua_pushstring(L, projPath);
} else {
lua_pushnil(L);
}
free(projPath);
return 1;
}

// Functions exposed to Lua
static const luaL_reg Module_methods[] =
{
{"get_project_directory", GetProjectDir},
{"get_executable_path", GetExecutablePath},
{0, 0}
};

static void LuaInit(lua_State* L)
{
int top = lua_gettop(L);

// Register lua names
luaL_register(L, MODULE_NAME, Module_methods);

lua_pushstring(L, SEP);
lua_setfield(L, -2, "PATH_SEP");

lua_pop(L, 1);
assert(top == lua_gettop(L));
}

dmExtension::Result AppInitializeMyExtension(dmExtension::AppParams* params)
{
return dmExtension::RESULT_OK;
}

dmExtension::Result InitializeMyExtension(dmExtension::Params* params)
{
// Init Lua
LuaInit(params->m_L);
return dmExtension::RESULT_OK;
}

dmExtension::Result AppFinalizeMyExtension(dmExtension::AppParams* params)
{
return dmExtension::RESULT_OK;
}

dmExtension::Result FinalizeMyExtension(dmExtension::Params* params)
{
return dmExtension::RESULT_OK;
}

dmExtension::Result OnUpdateMyExtension(dmExtension::Params* params)
{
return dmExtension::RESULT_OK;
}

void OnEventMyExtension(dmExtension::Params* params, const dmExtension::Event* event)
{
}

// Defold SDK uses a macro for setting up extension entry points:
//
// DM_DECLARE_EXTENSION(symbol, name, app_init, app_final, init, update, on_event, final)

// MyExtension is the C++ symbol that holds all relevant extension data.
// It must match the name field in the `ext.manifest`
DM_DECLARE_EXTENSION(defoldprojectdir, LIB_NAME, AppInitializeMyExtension, AppFinalizeMyExtension, InitializeMyExtension, OnUpdateMyExtension, OnEventMyExtension, FinalizeMyExtension)
37 changes: 37 additions & 0 deletions example/example.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "main"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"example\"\n"
" component: \"/example/example.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
5 changes: 5 additions & 0 deletions example/example.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function init(self)
print(defoldprojectdir.get_project_directory())
print(defoldprojectdir.get_executable_path())
print(defoldprojectdir.PATH_SEP)
end
16 changes: 16 additions & 0 deletions game.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[bootstrap]
main_collection = /example/example.collectionc

[script]
shared_state = 1

[display]
width = 960
height = 640

[project]
title = defold-project-dir

[library]
include_dirs = defoldprojectdir

4 changes: 4 additions & 0 deletions input/game.input_binding
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mouse_trigger {
input: MOUSE_BUTTON_1
action: "touch"
}

0 comments on commit 1db17ad

Please sign in to comment.