+ return chain;
+}
+
+Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, const InputState &input_state)
+{
+ const char *func_name = "get_scene"; // For error reporting.
+ Chain chain;
+
+ lock_guard<mutex> lock(m);
+ assert(lua_gettop(L) == 0);
+ lua_getglobal(L, "get_scene"); /* function to be called */
+ if (lua_isnil(L, -1)) {
+ // Try the pre-1.9.0 name for compatibility.
+ lua_pop(L, 1);
+ lua_getglobal(L, "get_chain");
+ func_name = "get_chain";
+ }
+ lua_pushnumber(L, num);
+ lua_pushnumber(L, t);
+ lua_pushnumber(L, width);
+ lua_pushnumber(L, height);
+ wrap_lua_object<InputStateInfo>(L, "InputStateInfo", input_state);
+
+ if (lua_pcall(L, 5, LUA_MULTRET, 0) != 0) {
+ fprintf(stderr, "error running function “%s”: %s\n", func_name, lua_tostring(L, -1));
+ abort();
+ }
+
+ if (luaL_testudata(L, -1, "Scene") != nullptr) {
+ if (lua_gettop(L) != 1) {
+ luaL_error(L, "%s() for chain number %d returned an Scene, but also other items", func_name);
+ }
+ Scene *auto_effect_chain = (Scene *)luaL_testudata(L, -1, "Scene");
+ auto chain_and_setup = auto_effect_chain->get_chain(this, L, num, input_state);
+ chain.chain = chain_and_setup.first;
+ chain.setup_chain = move(chain_and_setup.second);
+ } else if (luaL_testudata(L, -2, "EffectChain") != nullptr) {
+ // Old-style (pre-Nageru 1.9.0) return of a single chain and prepare function.
+ if (lua_gettop(L) != 2) {
+ luaL_error(L, "%s() for chain number %d returned an EffectChain, but needs to also return a prepare function (or use Scene)", func_name);
+ }
+ EffectChain *effect_chain = (EffectChain *)luaL_testudata(L, -2, "EffectChain");
+ chain = get_chain_from_effect_chain(effect_chain, num, input_state);
+ } else {
+ luaL_error(L, "%s() for chain number %d did not return an EffectChain or Scene\n", func_name, num);
+ }
+ assert(lua_gettop(L) == 0);