Lua_tinker 에서 wchat_t 타입을 처리할 수 있도록 해 봤습니다.

프로그래밍 일반에 관한 포럼입니다.

Moderator: 류광

Locked
milennium9
Posts: 71
Joined: 2007-02-01 12:06

Lua_tinker 에서 wchat_t 타입을 처리할 수 있도록 해 봤습니다.

Post by milennium9 »

lua_tinker.h

Code: Select all

    void    init_wchar(lua_State *L);


lua_tinker.cpp

Code: Select all

/*---------------------------------------------------------------------------*/ 
/* __wchar                                                                   */ 
/*---------------------------------------------------------------------------*/ 

static int tostring_wchar(lua_State *L)
{
    static char temp[2048];
    const wchar_t* pstr = (const wchar_t*)lua_topointer( L, 1 );
    WideCharToMultiByte( CP_ACP , 0 , pstr , (int)wcslen( pstr ) , 
        temp , 2048 , NULL , NULL ) ; 

    lua_pushstring(L, temp);
    return 1;
}

/*---------------------------------------------------------------------------*/ 
static int eq_wchar(lua_State *L)
{
    lua_pushboolean(L, wcscmp( (const wchar_t*)lua_topointer(L, 1), (const wchar_t*)lua_topointer(L, 2) ) );
    return 1;
}

/*---------------------------------------------------------------------------*/ 
static int lt_wchar(lua_State *L)
{
    lua_pushboolean(L, wcscmp( (const wchar_t*)lua_topointer(L, 1), (const wchar_t*)lua_topointer(L, 2) ) );
    return 1;
}

/*---------------------------------------------------------------------------*/ 
static int le_wchar(lua_State *L)
{
    lua_pushboolean(L, wcscmp( (const wchar_t*)lua_topointer(L, 1), (const wchar_t*)lua_topointer(L, 2) ) );
    return 1;
}

void lua_tinker::init_wchar( lua_State *L )
{
    const char* name = "__wchar";
    lua_pushstring(L, name);
    lua_newtable(L);

    lua_pushstring(L, "__name");
    lua_pushstring(L, name);
    lua_rawset(L, -3);

    lua_pushstring(L, "__tostring");
    lua_pushcclosure(L, tostring_wchar, 0);
    lua_rawset(L, -3);

    lua_pushstring(L, "__eq");
    lua_pushcclosure(L, eq_wchar, 0);
    lua_rawset(L, -3);	

    lua_pushstring(L, "__lt");
    lua_pushcclosure(L, lt_wchar, 0);
    lua_rawset(L, -3);	

    lua_pushstring(L, "__le");
    lua_pushcclosure(L, le_wchar, 0);
    lua_rawset(L, -3);	

    lua_settable(L, LUA_GLOBALSINDEX);
}


template<>
void lua_tinker::push(lua_State *L, wchar_t* ret)
{
    memcpy( lua_newuserdata(L, sizeof(wchar_t)*wcslen(ret)+2), ret, sizeof(wchar_t)*wcslen(ret)+2 );
    lua_pushstring(L, "__wchar");
    lua_gettable(L, LUA_GLOBALSINDEX);
    lua_setmetatable(L, -2);
}
template<>
void lua_tinker::push(lua_State *L, const wchar_t* ret)
{
    memcpy( lua_newuserdata(L, sizeof(wchar_t)*wcslen(ret)+2), ret, sizeof(wchar_t)*wcslen(ret)+2 );
    lua_pushstring(L, "__wchar");
    lua_gettable(L, LUA_GLOBALSINDEX);
    lua_setmetatable(L, -2);
}

template<>
wchar_t* lua_tinker::read(lua_State *L, int index)
{
    return (wchar_t*)lua_touserdata(L, index);
}
template<>
const wchar_t* lua_tinker::read(lua_State *L, int index)
{
    return (const wchar_t*)lua_touserdata(L, index);
}

위의 코드는 각각의 파일에 추가된 부분입니다.

이 기능을 추가한 이유.
1. 게임에서는 보통 Localization을 위하여 StringTable을 사용한다. 따라서 Lua스크립트에 Unicode 스트링을 바로 입력할 일이 없다.
2. Lua에서는 wchat_t 타입을 지원해 주지 않는다.
3. Lua에서 Unicode String을 바로 입력하도록 허용하고 싶지 않다.

이러한 이유에 의해서 위와 같이 추가하였습니다.
zupet 님께서 이미 __int64 기능을 구현해 두셨길래 그대로 따라만들었을 뿐이에요.
이미 많은 분들이 사용하고 계시겠지만, 혹시나 고민하시는 분들이 있으실까 싶어서 올려봅니다.
그리고 이왕이면 이 코드에 버그도 좀 봐주십사 ~ ㅋㅋ

printw 와 같은 함수를 Lua에 바인딩 해서 사용하시면 console에 출력도 가능할 것 같습니다.
Locked