#pragma once
#include "squirrel.h"
#include "sqstdaux.h"
#include "sqstdblob.h"
#include "sqstdio.h"
#include "sqstdmath.h"
#include "sqstdstring.h"
#include "sqstdsystem.h"
#include <iostream>

template <typename R, typename A, typename... ARG>
R CallDungeon(A call_addr, const ARG... arguments)
{
    if (!call_addr)
    {
        return R();
    }
    const auto control = reinterpret_cast<R (*)(ARG...)>(call_addr);
    try
    {
        return control(arguments...);
    }
    catch (...)
    {
    }
    return R();
}

// 获取副本编号
static SQInteger Dungeon_GetIdex(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQInteger Idx = CallDungeon<int>(0x80FDCF0, P);
    sq_pushinteger(v, Idx);
    return 1;
}

static SQInteger register_Dungeon_func(HSQUIRRELVM v, SQFUNCTION f, const char *fname)
{
    sq_pushroottable(v);
    sq_pushstring(v, fname, -1);
    sq_newclosure(v, f, 0); // create a new function
    sq_newslot(v, -3, SQFalse);
    sq_pop(v, 1); // pops the root table
}

static void RegisterDungeon(HSQUIRRELVM v)
{
    // 获取编号
    register_Dungeon_func(v, Dungeon_GetIdex, _SC("Sq_Dungeon_GetIdex"));
}