@@ -38,94 +38,116 @@ namespace MWLua
38
38
39
39
sol::table initInputPackage (const Context& context)
40
40
{
41
+ sol::object cached = context.getTypePackage (" openmw_input" );
42
+ if (cached != sol::nil)
43
+ return cached;
41
44
sol::state_view lua = context.sol ();
42
- {
43
- if (lua[" openmw_input" ] != sol::nil)
44
- return lua[" openmw_input" ];
45
- }
46
-
47
- sol::usertype<SDL_Keysym> keyEvent = lua.new_usertype <SDL_Keysym>(" KeyEvent" );
48
- keyEvent[" symbol" ] = sol::readonly_property ([](const SDL_Keysym& e) {
49
- if (e.sym > 0 && e.sym <= 255 )
50
- return std::string (1 , static_cast <char >(e.sym ));
51
- else
52
- return std::string ();
45
+
46
+ context.cachePackage (" openmw_input_keyevent" , [&lua]() {
47
+ sol::usertype<SDL_Keysym> keyEvent = lua.new_usertype <SDL_Keysym>(" KeyEvent" );
48
+ keyEvent[" symbol" ] = sol::readonly_property ([](const SDL_Keysym& e) {
49
+ if (e.sym > 0 && e.sym <= 255 )
50
+ return std::string (1 , static_cast <char >(e.sym ));
51
+ else
52
+ return std::string ();
53
+ });
54
+ keyEvent[" code" ] = sol::readonly_property ([](const SDL_Keysym& e) -> int { return e.scancode ; });
55
+ keyEvent[" withShift" ]
56
+ = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_SHIFT; });
57
+ keyEvent[" withCtrl" ]
58
+ = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_CTRL; });
59
+ keyEvent[" withAlt" ] = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_ALT; });
60
+ keyEvent[" withSuper" ]
61
+ = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_GUI; });
62
+
63
+ return sol::table (lua, sol::create);
53
64
});
54
- keyEvent[" code" ] = sol::readonly_property ([](const SDL_Keysym& e) -> int { return e.scancode ; });
55
- keyEvent[" withShift" ] = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_SHIFT; });
56
- keyEvent[" withCtrl" ] = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_CTRL; });
57
- keyEvent[" withAlt" ] = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_ALT; });
58
- keyEvent[" withSuper" ] = sol::readonly_property ([](const SDL_Keysym& e) -> bool { return e.mod & KMOD_GUI; });
59
-
60
- auto touchpadEvent = lua.new_usertype <SDLUtil::TouchEvent>(" TouchpadEvent" );
61
- touchpadEvent[" device" ] = sol::readonly_property ([](const SDLUtil::TouchEvent& e) -> int { return e.mDevice ; });
62
- touchpadEvent[" finger" ] = sol::readonly_property ([](const SDLUtil::TouchEvent& e) -> int { return e.mFinger ; });
63
- touchpadEvent[" position" ] = sol::readonly_property ([](const SDLUtil::TouchEvent& e) -> osg::Vec2f {
64
- return { e.mX , e.mY };
65
+
66
+ context.cachePackage (" openmw_input_touchpadevent" , [&lua]() {
67
+ auto touchpadEvent = lua.new_usertype <SDLUtil::TouchEvent>(" TouchpadEvent" );
68
+ touchpadEvent[" device" ]
69
+ = sol::readonly_property ([](const SDLUtil::TouchEvent& e) -> int { return e.mDevice ; });
70
+ touchpadEvent[" finger" ]
71
+ = sol::readonly_property ([](const SDLUtil::TouchEvent& e) -> int { return e.mFinger ; });
72
+ touchpadEvent[" position" ] = sol::readonly_property ([](const SDLUtil::TouchEvent& e) -> osg::Vec2f {
73
+ return { e.mX , e.mY };
74
+ });
75
+ touchpadEvent[" pressure" ]
76
+ = sol::readonly_property ([](const SDLUtil::TouchEvent& e) -> float { return e.mPressure ; });
77
+ return sol::table (lua, sol::create);
65
78
});
66
- touchpadEvent[ " pressure " ]
67
- = sol::readonly_property ([]( const SDLUtil::TouchEvent& e) -> float { return e. mPressure ; });
68
-
69
- auto inputActions = lua. new_usertype <LuaUtil::InputAction::Registry>( " InputActions " );
70
- inputActions[sol::meta_function:: index ]
71
- = [](LuaUtil::InputAction::Registry& registry, std::string_view key) { return registry[key]; };
72
- {
73
- auto pairs = [](LuaUtil::InputAction::Registry& registry) {
74
- auto next
75
- = [](LuaUtil::InputAction::Registry& registry,
76
- std::string_view key) -> sol::optional<std::tuple<std::string, LuaUtil::InputAction::Info>> {
77
- std::optional<std::string> nextKey (registry. nextKey (key)) ;
78
- if (!nextKey. has_value ())
79
- return sol::nullopt ;
80
- else
81
- return std::make_tuple (*nextKey , registry[*nextKey]. value ());
79
+
80
+ context. cachePackage ( " openmw_input_inputactions " , [&lua]() {
81
+ auto inputActions = lua. new_usertype <LuaUtil::InputAction::Registry>( " InputActions " );
82
+ inputActions[sol::meta_function:: index ]
83
+ = [](LuaUtil::InputAction::Registry& registry, std::string_view key) { return registry[key]; };
84
+ {
85
+ auto pairs = [](LuaUtil::InputAction::Registry& registry) {
86
+ auto next = [](LuaUtil::InputAction::Registry& registry, std::string_view key)
87
+ -> sol::optional<std::tuple<std::string, LuaUtil::InputAction::Info>> {
88
+ std::optional<std::string> nextKey ( registry. nextKey (key));
89
+ if (!nextKey. has_value ())
90
+ return sol::nullopt ;
91
+ else
92
+ return std::make_tuple (*nextKey, registry[*nextKey]. value ()) ;
93
+ };
94
+ return std::make_tuple (next , registry, registry. firstKey ());
82
95
};
83
- return std::make_tuple (next, registry, registry.firstKey ());
84
- };
85
- inputActions[sol::meta_function::pairs] = pairs;
86
- }
87
-
88
- auto actionInfo = lua.new_usertype <LuaUtil::InputAction::Info>(" ActionInfo" );
89
- actionInfo[" key" ] = sol::readonly_property (
90
- [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mKey ; });
91
- actionInfo[" name" ] = sol::readonly_property (
92
- [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mName ; });
93
- actionInfo[" description" ] = sol::readonly_property (
94
- [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mDescription ; });
95
- actionInfo[" l10n" ] = sol::readonly_property (
96
- [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mL10n ; });
97
- actionInfo[" type" ] = sol::readonly_property ([](const LuaUtil::InputAction::Info& info) { return info.mType ; });
98
- actionInfo[" defaultValue" ]
99
- = sol::readonly_property ([](const LuaUtil::InputAction::Info& info) { return info.mDefaultValue ; });
100
-
101
- auto inputTriggers = lua.new_usertype <LuaUtil::InputTrigger::Registry>(" InputTriggers" );
102
- inputTriggers[sol::meta_function::index ]
103
- = [](LuaUtil::InputTrigger::Registry& registry, std::string_view key) { return registry[key]; };
104
- {
105
- auto pairs = [](LuaUtil::InputTrigger::Registry& registry) {
106
- auto next
107
- = [](LuaUtil::InputTrigger::Registry& registry,
108
- std::string_view key) -> sol::optional<std::tuple<std::string, LuaUtil::InputTrigger::Info>> {
109
- std::optional<std::string> nextKey (registry.nextKey (key));
110
- if (!nextKey.has_value ())
111
- return sol::nullopt;
112
- else
113
- return std::make_tuple (*nextKey, registry[*nextKey].value ());
96
+ inputActions[sol::meta_function::pairs] = pairs;
97
+ }
98
+ return sol::table (lua, sol::create);
99
+ });
100
+
101
+ context.cachePackage (" openmw_input_actioninfo" , [&lua]() {
102
+ auto actionInfo = lua.new_usertype <LuaUtil::InputAction::Info>(" ActionInfo" );
103
+ actionInfo[" key" ] = sol::readonly_property (
104
+ [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mKey ; });
105
+ actionInfo[" name" ] = sol::readonly_property (
106
+ [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mName ; });
107
+ actionInfo[" description" ] = sol::readonly_property (
108
+ [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mDescription ; });
109
+ actionInfo[" l10n" ] = sol::readonly_property (
110
+ [](const LuaUtil::InputAction::Info& info) -> std::string_view { return info.mL10n ; });
111
+ actionInfo[" type" ]
112
+ = sol::readonly_property ([](const LuaUtil::InputAction::Info& info) { return info.mType ; });
113
+ actionInfo[" defaultValue" ]
114
+ = sol::readonly_property ([](const LuaUtil::InputAction::Info& info) { return info.mDefaultValue ; });
115
+ return sol::table (lua, sol::create);
116
+ });
117
+
118
+ context.cachePackage (" openmw_input_inputtriggers" , [&lua]() {
119
+ auto inputTriggers = lua.new_usertype <LuaUtil::InputTrigger::Registry>(" InputTriggers" );
120
+ inputTriggers[sol::meta_function::index ]
121
+ = [](LuaUtil::InputTrigger::Registry& registry, std::string_view key) { return registry[key]; };
122
+ {
123
+ auto pairs = [](LuaUtil::InputTrigger::Registry& registry) {
124
+ auto next = [](LuaUtil::InputTrigger::Registry& registry, std::string_view key)
125
+ -> sol::optional<std::tuple<std::string, LuaUtil::InputTrigger::Info>> {
126
+ std::optional<std::string> nextKey (registry.nextKey (key));
127
+ if (!nextKey.has_value ())
128
+ return sol::nullopt;
129
+ else
130
+ return std::make_tuple (*nextKey, registry[*nextKey].value ());
131
+ };
132
+ return std::make_tuple (next, registry, registry.firstKey ());
114
133
};
115
- return std::make_tuple (next, registry, registry.firstKey ());
116
- };
117
- inputTriggers[sol::meta_function::pairs] = pairs;
118
- }
119
-
120
- auto triggerInfo = lua.new_usertype <LuaUtil::InputTrigger::Info>(" TriggerInfo" );
121
- triggerInfo[" key" ] = sol::readonly_property (
122
- [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mKey ; });
123
- triggerInfo[" name" ] = sol::readonly_property (
124
- [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mName ; });
125
- triggerInfo[" description" ] = sol::readonly_property (
126
- [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mDescription ; });
127
- triggerInfo[" l10n" ] = sol::readonly_property (
128
- [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mL10n ; });
134
+ inputTriggers[sol::meta_function::pairs] = pairs;
135
+ }
136
+ return sol::table (lua, sol::create);
137
+ });
138
+
139
+ context.cachePackage (" openmw_input_triggerinfo" , [&lua]() {
140
+ auto triggerInfo = lua.new_usertype <LuaUtil::InputTrigger::Info>(" TriggerInfo" );
141
+ triggerInfo[" key" ] = sol::readonly_property (
142
+ [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mKey ; });
143
+ triggerInfo[" name" ] = sol::readonly_property (
144
+ [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mName ; });
145
+ triggerInfo[" description" ] = sol::readonly_property (
146
+ [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mDescription ; });
147
+ triggerInfo[" l10n" ] = sol::readonly_property (
148
+ [](const LuaUtil::InputTrigger::Info& info) -> std::string_view { return info.mL10n ; });
149
+ return sol::table (lua, sol::create);
150
+ });
129
151
130
152
MWBase::InputManager* input = MWBase::Environment::get ().getInputManager ();
131
153
sol::table api (lua, sol::create);
@@ -449,8 +471,8 @@ namespace MWLua
449
471
{ " Tab" , SDL_SCANCODE_TAB },
450
472
}));
451
473
452
- lua[ " openmw_input " ] = LuaUtil::makeReadOnly (api);
453
- return lua[ " openmw_input" ] ;
474
+ sol::table readOnlyApi = LuaUtil::makeReadOnly (api);
475
+ return context. setTypePackage (readOnlyApi, " openmw_input" ) ;
454
476
}
455
477
456
478
}
0 commit comments