Skip to content

Commit

Permalink
Merge pull request #727 from bjornbytes/watch
Browse files Browse the repository at this point in the history
Live Reloading
  • Loading branch information
bjornbytes committed May 27, 2024
2 parents 5248872 + 2df61e9 commit 4ba215a
Show file tree
Hide file tree
Showing 13 changed files with 1,897 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,16 @@ if(LOVR_ENABLE_FILESYSTEM)
src/modules/filesystem/filesystem.c
src/api/l_filesystem.c
src/api/l_filesystem_file.c
src/lib/dmon/dmon.c
src/lib/miniz/miniz_tinfl.c
)

# dmon
if(APPLE)
find_library(CORE_FOUNDATION CoreFoundation)
find_library(CORE_SERVICES CoreServices)
target_link_libraries(lovr "${CORE_FOUNDATION}" "${CORE_SERVICES}")
endif()
else()
target_compile_definitions(lovr PRIVATE LOVR_DISABLE_FILESYSTEM)
endif()
Expand Down
1 change: 1 addition & 0 deletions Tupfile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ src += 'src/lib/miniz/*.c'
src += (config.modules.audio or config.modules.data) and 'src/lib/miniaudio/*.c' or nil
src += config.modules.data and 'src/lib/jsmn/*.c' or nil
src += config.modules.data and 'src/lib/minimp3/*.c' or nil
src += config.modules.filesystem and 'src/lib/dmon/*.c' or nil
src += config.modules.math and 'src/lib/noise/*.c' or nil
src += config.modules.thread and 'src/core/job.c' or nil

Expand Down
6 changes: 6 additions & 0 deletions etc/boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ function lovr.threaderror(thread, err)
error('Thread error\n\n' .. err, 0)
end

function lovr.filechanged(path, action, oldpath)
if not path:match('^%.') then
lovr.event.restart()
end
end

function lovr.log(message, level, tag)
message = message:gsub('\n$', '')
print(message)
Expand Down
7 changes: 6 additions & 1 deletion etc/nogame/arg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ function lovr.arg(arg)
_help = { short = '-h', long = '--help', help = 'Show help and exit' },
_version = { short = '-v', long = '--version', help = 'Show version and exit' },
console = { long = '--console', help = 'Attach Windows console' },
debug = { long = '--debug', help = 'Enable debugging checks and logging' }
debug = { long = '--debug', help = 'Enable debugging checks and logging' },
watch = { short = '-w', long = '--watch', help = 'Watch files and restart on change' }
}

local shift
Expand Down Expand Up @@ -68,5 +69,9 @@ function lovr.arg(arg)
if arg.debug then
conf.graphics.debug = true
end

if arg.watch then
lovr.filesystem.watch()
end
end
end
1 change: 1 addition & 0 deletions src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern StringEntry lovrDrawMode[];
extern StringEntry lovrDrawStyle[];
extern StringEntry lovrEffect[];
extern StringEntry lovrEventType[];
extern StringEntry lovrFileAction[];
extern StringEntry lovrFilterMode[];
extern StringEntry lovrHeadsetDriver[];
extern StringEntry lovrHorizontalAlign[];
Expand Down
9 changes: 9 additions & 0 deletions src/api/l_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ StringEntry lovrEventType[] = {
#ifndef LOVR_DISABLE_THREAD
[EVENT_THREAD_ERROR] = ENTRY("threaderror"),
#endif
[EVENT_FILECHANGED] = ENTRY("filechanged"),
[EVENT_PERMISSION] = ENTRY("permission"),
{ 0 }
};
Expand Down Expand Up @@ -207,6 +208,14 @@ static int nextEvent(lua_State* L) {
return 3;
#endif

case EVENT_FILECHANGED:
lua_pushstring(L, event.data.file.path);
luax_pushenum(L, FileAction, event.data.file.action);
lua_pushstring(L, event.data.file.oldpath);
free(event.data.file.path);
free(event.data.file.oldpath);
return 4;

case EVENT_PERMISSION:
luax_pushenum(L, Permission, event.data.permission.permission);
lua_pushboolean(L, event.data.permission.granted);
Expand Down
20 changes: 20 additions & 0 deletions src/api/l_filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#include <stdlib.h>
#include <string.h>

StringEntry lovrFileAction[] = {
[FILE_CREATE] = ENTRY("create"),
[FILE_DELETE] = ENTRY("delete"),
[FILE_MODIFY] = ENTRY("modify"),
[FILE_RENAME] = ENTRY("rename"),
{ 0 }
};

StringEntry lovrOpenMode[] = {
[OPEN_READ] = ENTRY("r"),
[OPEN_WRITE] = ENTRY("w"),
Expand Down Expand Up @@ -349,6 +357,16 @@ static int l_lovrFilesystemUnmount(lua_State* L) {
return 1;
}

static int l_lovrFilesystemUnwatch(lua_State* L) {
lovrFilesystemUnwatch();
return 0;
}

static int l_lovrFilesystemWatch(lua_State* L) {
lovrFilesystemWatch();
return 0;
}

static int l_lovrFilesystemWrite(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
size_t size;
Expand Down Expand Up @@ -411,6 +429,8 @@ static const luaL_Reg lovrFilesystem[] = {
{ "setRequirePath", l_lovrFilesystemSetRequirePath },
{ "setSource", l_lovrFilesystemSetSource },
{ "unmount", l_lovrFilesystemUnmount },
{ "unwatch", l_lovrFilesystemUnwatch },
{ "watch", l_lovrFilesystemWatch },
{ "write", l_lovrFilesystemWrite },
{ "newFile", l_lovrFilesystemNewFile },
{ NULL, NULL }
Expand Down
2 changes: 2 additions & 0 deletions src/lib/dmon/dmon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define DMON_IMPL
#include "dmon.h"

0 comments on commit 4ba215a

Please sign in to comment.