Skip to content

Commit f7d8520

Browse files
committed
Graphics now close when entered in sleep mode to avoid black screen. Fix possibility of script exhaustion time for opening keyboard. Keyboard functions returns nil if the player closes it or if there is an error. Fix Camera FOV offset
1 parent 07faa91 commit f7d8520

File tree

14 files changed

+249
-50
lines changed

14 files changed

+249
-50
lines changed

Includes/Core/Async.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "lua_common.h"
44

55
namespace Core {
6+
void AsyncRestartClock();
7+
68
void AsyncHandlerCallback();
79

810
bool RegisterAsyncModule(lua_State *L);

Includes/Core/Event.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "lua_common.h"
44

55
namespace Core {
6+
void EventRestartClock();
7+
68
void EventHandlerCallback();
79

810
namespace Event {

Includes/Core/Graphics.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
#include <CTRPluginFramework.hpp>
66

77
namespace Core {
8-
void GraphicsHandlerCallback();
8+
using GraphicsFrameCallback = void(*)(void);
9+
using GraphicsExitCallback = void(*)(void);
10+
11+
void GraphicsOpen(GraphicsFrameCallback frameCallback, GraphicsExitCallback exitCallback);
12+
13+
void GraphicsHandlerMainloop();
914

1015
namespace Module {
1116
bool RegisterGraphicsModule(lua_State *L);

Sources/Core/Async.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace CTRPF = CTRPluginFramework;
1010
extern lua_State *Lua_global;
1111
CTRPluginFramework::Clock timeoutAsynClock;
1212

13+
void Core::AsyncRestartClock() {
14+
timeoutAsynClock.Restart();
15+
}
16+
1317
static void TimeoutAsyncHook(lua_State *L, lua_Debug *ar)
1418
{
1519
if (timeoutAsynClock.HasTimePassed(CTRPluginFramework::Milliseconds(5000)))

Sources/Core/Event.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ extern lua_State *Lua_global;
1515
CTRPF::Clock timeoutEventClock;
1616
extern std::atomic<bool> graphicsIsTop;
1717

18+
void Core::EventRestartClock() {
19+
timeoutEventClock.Restart();
20+
}
21+
1822
void TimeoutEventHook(lua_State *L, lua_Debug *ar)
1923
{
2024
if (timeoutEventClock.HasTimePassed(CTRPluginFramework::Milliseconds(5000)))

Sources/Core/Graphics.cpp

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,23 @@ static int lua_callback = LUA_NOREF;
1313
static bool graphicsOpen = false;
1414
static bool shouldGraphicsClose = false;
1515
static const CTRPF::Screen *currentScreen = NULL;
16+
static Core::GraphicsFrameCallback graphicsFrameCallback = NULL;
17+
static Core::GraphicsExitCallback graphicsExitCallback = NULL;
1618

17-
void Core::GraphicsHandlerCallback() {
19+
static void processEventHandler(CTRPF::Process::Event event) {
20+
if (event == CTRPF::Process::Event::SLEEP_ENTER)
21+
shouldGraphicsClose = true;
22+
}
23+
24+
void Core::GraphicsOpen(GraphicsFrameCallback frameCallback, GraphicsExitCallback exitCallback) {
25+
graphicsFrameCallback = frameCallback;
26+
graphicsExitCallback = exitCallback;
27+
gmenu->Callback(Core::GraphicsHandlerMainloop); // Better add it as Callback to avoid script exahustion
28+
}
29+
30+
void Core::GraphicsHandlerMainloop() {
1831
CTRPF::Process::Pause();
19-
lua_State *L = Lua_global;
32+
CTRPF::Process::SetProcessEventCallback(processEventHandler);
2033

2134
graphicsOpen = true;
2235
bool exit = false;
@@ -29,33 +42,57 @@ void Core::GraphicsHandlerCallback() {
2942
if (CTRPF::OSD::TryLock())
3043
continue;
3144

32-
const CTRPF::Screen& topScreen = CTRPF::OSD::GetTopScreen();
33-
currentScreen = &topScreen;
34-
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_callback);
35-
lua_pushstring(L, "top");
36-
if (lua_pcall(L, 1, 0, 0))
37-
lua_pop(L, 1);
38-
39-
const CTRPF::Screen& bottomScreen = CTRPF::OSD::GetBottomScreen();
40-
currentScreen = &bottomScreen;
41-
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_callback);
42-
lua_pushstring(L, "bottom");
43-
if (lua_pcall(L, 1, 0, 0))
44-
lua_pop(L, 1);
45+
if (graphicsFrameCallback == NULL) {
46+
exit = true;
47+
continue;
48+
}
49+
50+
graphicsFrameCallback();
4551

4652
CTRPF::OSD::SwapBuffers();
4753
CTRPF::OSD::Unlock();
4854

49-
exit = CTRPF::Controller::IsKeyReleased(CTRPF::Key::Select);
55+
if (CTRPF::Controller::IsKeyReleased(CTRPF::Key::Select)) {
56+
exit = true;
57+
gmenu->ForceOpen();
58+
}
5059
}
51-
luaL_unref(L, LUA_REGISTRYINDEX, lua_callback);
52-
lua_callback = LUA_NOREF;
53-
*gmenu -= Core::GraphicsHandlerCallback;
60+
61+
// Exit Graphics Mainloop
62+
if (graphicsExitCallback != NULL)
63+
graphicsExitCallback();
64+
CTRPF::Process::SetProcessEventCallback(nullptr);
65+
*gmenu -= Core::GraphicsHandlerMainloop;
66+
graphicsFrameCallback = NULL;
67+
graphicsExitCallback = NULL;
5468
graphicsOpen = false;
5569
shouldGraphicsClose = false;
5670
CTRPF::Process::Play();
5771
}
5872

73+
static void LuaGraphicsFrameCallback() {
74+
lua_State *L = Lua_global;
75+
const CTRPF::Screen& topScreen = CTRPF::OSD::GetTopScreen();
76+
currentScreen = &topScreen;
77+
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_callback);
78+
lua_pushstring(L, "top");
79+
if (lua_pcall(L, 1, 0, 0))
80+
lua_pop(L, 1);
81+
82+
const CTRPF::Screen& bottomScreen = CTRPF::OSD::GetBottomScreen();
83+
currentScreen = &bottomScreen;
84+
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_callback);
85+
lua_pushstring(L, "bottom");
86+
if (lua_pcall(L, 1, 0, 0))
87+
lua_pop(L, 1);
88+
}
89+
90+
static void LuaGraphicsExitCallback() {
91+
lua_State *L = Lua_global;
92+
luaL_unref(L, LUA_REGISTRYINDEX, lua_callback);
93+
lua_callback = LUA_NOREF;
94+
}
95+
5996
// ----------------------------------------------------------------------------
6097

6198
//$Core.Graphics
@@ -74,10 +111,15 @@ static int l_Graphics_open(lua_State *L) {
74111

75112
if (graphicsOpen)
76113
return 0;
114+
115+
if (lua_callback != LUA_NOREF) {
116+
luaL_unref(L, LUA_REGISTRYINDEX, lua_callback);
117+
lua_callback = LUA_NOREF;
118+
}
77119

78120
lua_pushvalue(L, 1);
79121
lua_callback = luaL_ref(L, LUA_REGISTRYINDEX);
80-
gmenu->Callback(Core::GraphicsHandlerCallback); // Better add it as Callback to avoid script exahustion
122+
Core::GraphicsOpen(LuaGraphicsFrameCallback, LuaGraphicsExitCallback);
81123
return 0;
82124
}
83125

Sources/Core/Keyboard.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include <CTRPluginFramework.hpp>
44

5+
#include "Core/Event.hpp"
6+
#include "Core/Async.hpp"
7+
58
namespace CTRPF = CTRPluginFramework;
69

710
// ----------------------------------------------------------------------------
@@ -13,7 +16,7 @@ namespace CTRPF = CTRPluginFramework;
1316
/*
1417
- Opens the keyboard and returns the user input as string
1518
## message: string?
16-
## return: string
19+
## return: string?
1720
### Core.Keyboard.getString
1821
*/
1922
static int l_Keyboard_getString(lua_State *L) {
@@ -25,17 +28,22 @@ static int l_Keyboard_getString(lua_State *L) {
2528
keyboard.DisplayTopScreen = true;
2629
}
2730
std::string inputText;
28-
if (keyboard.Open(inputText) == 0)
31+
if (keyboard.Open(inputText) == 0) {
32+
Core::EventRestartClock();
33+
Core::AsyncRestartClock();
2934
lua_pushstring(L, inputText.c_str());
30-
else
31-
lua_pushstring(L, "");
35+
} else {
36+
Core::EventRestartClock();
37+
Core::AsyncRestartClock();
38+
lua_pushnil(L);
39+
}
3240
return 1;
3341
}
3442

3543
/*
3644
- Opens the keyboard and returns the user input as number
3745
## message: string?
38-
## return: number
46+
## return: number?
3947
### Core.Keyboard.getNumber
4048
*/
4149
static int l_Keyboard_getNumber(lua_State *L) {
@@ -47,17 +55,22 @@ static int l_Keyboard_getNumber(lua_State *L) {
4755
keyboard.DisplayTopScreen = true;
4856
}
4957
float inputNumber;
50-
if (keyboard.Open(inputNumber) == 0)
58+
if (keyboard.Open(inputNumber) == 0) {
59+
Core::EventRestartClock();
60+
Core::AsyncRestartClock();
5161
lua_pushnumber(L, inputNumber);
52-
else
53-
lua_pushnumber(L, -1);
62+
} else {
63+
Core::EventRestartClock();
64+
Core::AsyncRestartClock();
65+
lua_pushnil(L);
66+
}
5467
return 1;
5568
}
5669

5770
/*
5871
- Opens the keyboard and returns the user input as unsigned integer
5972
## message: string?
60-
## return: integer
73+
## return: integer?
6174
### Core.Keyboard.getInteger
6275
*/
6376
static int l_Keyboard_getInteger(lua_State *L) {
@@ -69,17 +82,22 @@ static int l_Keyboard_getInteger(lua_State *L) {
6982
keyboard.DisplayTopScreen = true;
7083
}
7184
u32 inputNumber;
72-
if (keyboard.Open(inputNumber) == 0)
85+
if (keyboard.Open(inputNumber) == 0) {
86+
Core::EventRestartClock();
87+
Core::AsyncRestartClock();
7388
lua_pushinteger(L, inputNumber);
74-
else
75-
lua_pushinteger(L, -1);
89+
} else {
90+
Core::EventRestartClock();
91+
Core::AsyncRestartClock();
92+
lua_pushnil(L);
93+
}
7694
return 1;
7795
}
7896

7997
/*
8098
- Opens the keyboard and returns the user input as hexadecimal
8199
## message: string?
82-
## return: integer
100+
## return: integer?
83101
### Core.Keyboard.getHex
84102
*/
85103
static int l_Keyboard_getHex(lua_State *L) {
@@ -92,10 +110,15 @@ static int l_Keyboard_getHex(lua_State *L) {
92110
}
93111
u32 inputNumber;
94112
keyboard.IsHexadecimal(true);
95-
if (keyboard.Open(inputNumber) == 0)
113+
if (keyboard.Open(inputNumber) == 0) {
114+
Core::EventRestartClock();
115+
Core::AsyncRestartClock();
96116
lua_pushinteger(L, inputNumber);
97-
else
98-
lua_pushinteger(L, -1);
117+
} else {
118+
Core::EventRestartClock();
119+
Core::AsyncRestartClock();
120+
lua_pushnil(L);
121+
}
99122
return 1;
100123
}
101124

Sources/Game/Player/Camera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CTRPF = CTRPluginFramework;
99

1010
enum player_camera_offsets : u32 {
1111
itemCameraFOV = 0x100000 + 0x2CEE80,
12-
playerCameraFOV = 0x341F313C,
12+
playerCameraFOV = 0x341F30F8,
1313
};
1414

1515
// ----------------------------------------------------------------------------

Sources/Modules.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,33 @@ void Core::LoadModules(lua_State *L)
9292
Core::RegisterGameModule(L);
9393

9494
Core::UnregisterUtilsModule(L);
95+
96+
// Add some other functionalities
97+
/*
98+
$string
99+
- Returns a table with a string splitted
100+
## s: string
101+
## separator: string
102+
## return: table
103+
### string.split
104+
*/
105+
const char *lua_Code = R"(
106+
function string.split(s, separator)
107+
if separator == nil then
108+
separator = "%s"
109+
end
110+
local escaped_delimiter = separator:gsub('([%.%*%+%?%^%$%[%]%(%)%{%}%|%\\])', '%%%1')
111+
local pattern = string.format('([^%s]+)', escaped_delimiter)
112+
local result = {}
113+
for str in s:gmatch(pattern) do
114+
table.insert(result, str)
115+
end
116+
return result
117+
end
118+
)";
119+
if (luaL_dostring(L, lua_Code))
120+
{
121+
Core::Debug::LogError("Core::Load error: "+std::string(lua_tostring(L, -1)));
122+
lua_pop(L, 1);
123+
}
95124
}

Sources/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#define PLUGIN_VERSION_MAJOR 0
3232
#define PLUGIN_VERSION_MINOR 10
33-
#define PLUGIN_VERSION_PATCH 0
33+
#define PLUGIN_VERSION_PATCH 1
3434
#define PLUGIN_FOLDER "sdmc:/Minecraft 3DS"
3535
#define LOG_FILE PLUGIN_FOLDER"/log.txt"
3636
#define CONFIG_FILE PLUGIN_FOLDER"/config.txt"

0 commit comments

Comments
 (0)