Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit accfe6b

Browse files
more lua api
1 parent 31bfd73 commit accfe6b

2 files changed

Lines changed: 106 additions & 24 deletions

File tree

LuaApiReference.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ end
5454
| `print(msg)`|String| nil | Will print to the OFS log file.|
5555
| `ofs.Bind(functionName, description)` |String, String| nil | Will create a "Dynamic" key binding.<br/>Must be called from within `init()`.<br/>Bindings will always run in another thread. |
5656
| `ofs.Task(functionName)` | String | nil |Will run a function in another thread. Use cautiously. |
57+
| `ofs.ExtensionDir()` | None | String | Path to extension directory. |
5758

5859

5960
# Video player API
@@ -66,6 +67,7 @@ end
6667
| `player.Duration()`| None | Number | Returns the total duration of the video in seconds. |
6768
| `player.IsPlaying()`| None | bool | Returns a boolean if the player is playing or not. |
6869
| `player.CurrentVideo()`| None | String | Returns a path to the currently playing video. |
70+
| `player.FPS()`| None | Number | Returns the fps of the video. |
6971

7072
# Funscript API
7173

@@ -146,6 +148,9 @@ end
146148
|`ofs.HasSelection(script)`| Funscript | bool | Returns if the script has a selection. |
147149
|`ofs.Commit(script)`| Funscript | nil | This creates an undo snapshot and saves changes back to OFS.<br/>If you forget to call commit nothing will change in OFS. |
148150
|`ofs.Undo()`| None | nil | Will undo the last modification done by a Lua script.<br/>It will do nothing if the last modification wasn't done by a script.<br/>Essentially you only undo modifications by `ofs.Commit`. |
151+
|`ofs.ScriptTitle(index)`| Number | String | Returns title of a funscript. |
152+
|`ofs.ClosestActionAfter(script, time)`| Funscript, Number | Number | Given a funscript and a time in seconds it returns an action index or nil. |
153+
|`ofs.ClosestActionBefore(script, time)`| Funscript, Number | Number | Given a funscript and a time in seconds it returns an action index or nil. |
149154

150155

151156
# GUI API

src/lua/OFS_LuaExtensions.cpp

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,6 @@
1515
#include <algorithm>
1616
#include "EASTL/string.h"
1717

18-
//#ifdef WIN32
19-
////used to obtain file age on windows
20-
//#define WIN32_LEAN_AND_MEAN
21-
//#include "windows.h"
22-
//#endif
23-
24-
//static uint64_t GetWriteTime(const wchar_t* path) {
25-
// OFS_PROFILE(__FUNCTION__);
26-
// std::error_code ec;
27-
// uint64_t timestamp = 0;
28-
// HANDLE file = CreateFileW((wchar_t*)path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
29-
// if (file == INVALID_HANDLE_VALUE) { }
30-
// else {
31-
// FILETIME ftCreate;
32-
// GetFileTime(file, NULL, NULL, &ftCreate);
33-
// timestamp = *(uint64_t*)&ftCreate;
34-
// CloseHandle(file);
35-
// }
36-
// return timestamp;
37-
//}
38-
3918
constexpr const char* LuaDefaultFunctions = R"(
4019
function clamp(val, min, max)
4120
return math.min(max, math.max(val, min))
@@ -381,6 +360,7 @@ static int LuaPlayerPlay(lua_State* L) noexcept;
381360
static int LuaPlayerCurrentTime(lua_State* L) noexcept;
382361
static int LuaPlayerDuration(lua_State* L) noexcept;
383362
static int LuaPlayerGetVideo(lua_State* L) noexcept;
363+
static int LuaPlayerGetFPS(lua_State* L) noexcept;
384364

385365
static constexpr struct luaL_Reg playerLib[] = {
386366
{"Play", LuaPlayerPlay},
@@ -389,9 +369,17 @@ static constexpr struct luaL_Reg playerLib[] = {
389369
{"Duration", LuaPlayerDuration},
390370
{"IsPlaying", LuaPlayerIsPlaying},
391371
{"CurrentVideo", LuaPlayerGetVideo},
372+
{"FPS", LuaPlayerGetFPS},
392373
{NULL, NULL}
393374
};
394375

376+
static int LuaPlayerGetFPS(lua_State* L) noexcept
377+
{
378+
auto app = OpenFunscripter::ptr;
379+
lua_pushnumber(L, app->player->getFps());
380+
return 1;
381+
}
382+
395383
static int LuaPlayerGetVideo(lua_State* L) noexcept
396384
{
397385
auto app = OpenFunscripter::ptr;
@@ -459,20 +447,55 @@ static int LuaScheduleTask(lua_State* L) noexcept;
459447
static int LuaCommitChanges(lua_State* L) noexcept;
460448
static int LuaUndo(lua_State* L) noexcept;
461449
static int LuaHasSelection(lua_State* L) noexcept;
450+
static int LuaGetExtensionDir(lua_State* L) noexcept;
451+
static int LuaGetScriptTitle(lua_State* L) noexcept;
452+
static int LuaClosestActionAfter(lua_State* L) noexcept;
453+
static int LuaClosestActionBefore(lua_State* L) noexcept;
454+
462455
static constexpr struct luaL_Reg ofsLib[] = {
456+
// core
457+
{"Task", LuaScheduleTask},
458+
{"Bind", LuaBindFunction},
459+
{"Undo", LuaUndo},
460+
{"ExtensionDir", LuaGetExtensionDir},
461+
462+
// funscript api
463463
{"Script", LuaGetScript},
464464
{"AddAction", LuaAddAction},
465465
{"RemoveAction", LuaRemoveAction},
466466
{"ActiveIdx", LuaGetActiveIdx},
467467
{"ClearScript", LuaClearScript},
468468
{"HasSelection", LuaHasSelection},
469-
{"Task", LuaScheduleTask},
470-
{"Bind", LuaBindFunction},
471469
{"Commit", LuaCommitChanges},
472-
{"Undo", LuaUndo},
470+
{"ScriptTitle", LuaGetScriptTitle},
471+
{"ClosestActionAfter", LuaClosestActionAfter},
472+
{"ClosestActionBefore", LuaClosestActionBefore},
473473
{NULL, NULL}
474474
};
475475

476+
static int LuaGetScriptTitle(lua_State* L) noexcept
477+
{
478+
auto app = OpenFunscripter::ptr;
479+
int nargs = lua_gettop(L);
480+
if(nargs >= 1) {
481+
luaL_argcheck(L, lua_isinteger(L, 1), 1, "Expected script index.");
482+
int scriptIndex = lua_tointeger(L, 1) - 1;
483+
luaL_argcheck(L, scriptIndex >= 0 && scriptIndex < app->LoadedFunscripts().size(), 1, "Script index invalid.");
484+
lua_pushstring(L, app->LoadedFunscripts()[scriptIndex]->Title.c_str());
485+
return 1;
486+
}
487+
return 0;
488+
}
489+
490+
static int LuaGetExtensionDir(lua_State* L) noexcept
491+
{
492+
lua_getglobal(L, OFS_LuaExtensions::GlobalExtensionPtr);
493+
assert(lua_isuserdata(L, -1));
494+
OFS_LuaExtension* ext = (OFS_LuaExtension*)lua_touserdata(L, -1);
495+
lua_pushstring(L, ext->Directory.c_str());
496+
return 1;
497+
}
498+
476499
static int LuaHasSelection(lua_State* L) noexcept
477500
{
478501
auto app = OpenFunscripter::ptr;
@@ -489,6 +512,60 @@ static int LuaHasSelection(lua_State* L) noexcept
489512
return 1;
490513
}
491514

515+
static int LuaClosestActionAfter(lua_State* L) noexcept
516+
{
517+
int nargs = lua_gettop(L);
518+
if(nargs >= 2) {
519+
luaL_argcheck(L, lua_istable(L, 1), 1, "Expected script");
520+
luaL_argcheck(L, lua_isnumber(L, 2), 2, "Expected time in ms");
521+
522+
lua_getfield(L, 1, OFS_LuaExtensions::ScriptDataUserdata);
523+
assert(lua_isuserdata(L, -1));
524+
auto scriptData = (Funscript::FunscriptData*)lua_touserdata(L, -1);
525+
526+
lua_Number time = lua_tonumber(L, 2);
527+
528+
{
529+
if (scriptData->Actions.empty()) return 0;
530+
auto it = scriptData->Actions.upper_bound(FunscriptAction(time, 0));
531+
if(it != scriptData->Actions.end()) {
532+
int actionIndex = std::distance(scriptData->Actions.begin(), it);
533+
lua_pushinteger(L, actionIndex + 1);
534+
return 1;
535+
}
536+
}
537+
}
538+
return 0;
539+
}
540+
541+
static int LuaClosestActionBefore(lua_State* L) noexcept
542+
{
543+
int nargs = lua_gettop(L);
544+
if(nargs >= 2) {
545+
luaL_argcheck(L, lua_istable(L, 1), 1, "Expected script");
546+
luaL_argcheck(L, lua_isnumber(L, 2), 2, "Expected time in ms");
547+
548+
lua_getfield(L, 1, OFS_LuaExtensions::ScriptDataUserdata);
549+
assert(lua_isuserdata(L, -1));
550+
auto scriptData = (Funscript::FunscriptData*)lua_touserdata(L, -1);
551+
552+
lua_Number time = lua_tonumber(L, 2);
553+
554+
{
555+
if (scriptData->Actions.empty()) return 0;
556+
auto it = scriptData->Actions.lower_bound(FunscriptAction(time, 0));
557+
if(it-1 >= scriptData->Actions.begin()) {
558+
auto before = it - 1;
559+
int actionIndex = std::distance(scriptData->Actions.begin(), before);
560+
lua_pushinteger(L, actionIndex + 1);
561+
return 1;
562+
}
563+
}
564+
}
565+
566+
return 0;
567+
}
568+
492569
static int LuaClearScript(lua_State* L) noexcept
493570
{
494571
auto app = OpenFunscripter::ptr;

0 commit comments

Comments
 (0)