Beispiel #1
0
bool wxLuaDebugIO::WriteDebugData(wxLuaSocket *pSocket, const wxLuaDebugData *pSortedList)
{
    bool result = false;
    size_t idx, idxMax = pSortedList->Count();

    pSocket->Write((const char *) &idxMax, sizeof(idxMax));

    for (idx = 0; idx < idxMax; ++idx)
    {
        try
        {
            const wxLuaDebugDataItem *item = pSortedList->Item(idx);

            int nameLength = item->GetName().Length() + 1;
            int typeLength = item->GetType().Length() + 1;
            int valueLength = item->GetValue().Length() + 1;

            int bufferLength = (2 * sizeof(int)) +
                                sizeof(unsigned char) +
                                nameLength +
                                typeLength +
                                valueLength;

            unsigned char *pBuffer = new unsigned char[bufferLength];
            unsigned char *pMemory = pBuffer;

            pSocket->Write((const char *) &bufferLength, sizeof(bufferLength));

            *(int *) pMemory = item->GetReference();
            pMemory += sizeof(int);

            *(int *) pMemory = item->GetIndex();
            pMemory += sizeof(int);

            *pMemory++ = item->IsExpanded();

            memcpy(pMemory, wx2lua(item->GetName()), nameLength);
            pMemory += nameLength;

            memcpy(pMemory, wx2lua(item->GetType()), typeLength);
            pMemory += typeLength;

            memcpy(pMemory, wx2lua(item->GetValue()), valueLength);

            pSocket->Write((const char *) pBuffer, bufferLength);

            delete[] pBuffer;
            result = true;
        }
        catch(wxLuaSocketException & /*e*/)
        {
        }

        if (result == false)
            break;
    }
    return result;
}
bool wxLuaCSocket::Connect(const wxString &addr, u_short port_number)
{
    m_port_number = port_number;
    hostent *pHost = NULL;

    if (m_sockstate != SOCKET_CLOSED)
    {
        AddErrorMessage(wxString::Format(wxT("Unable to connect to addr '%s' socket already open."), addr.c_str()));
        return false;
    }

    m_sock = ::socket(AF_INET, SOCK_STREAM, 0);
    if (m_sock == INVALID_SOCKET)
    {
        AddErrorMessage(wxString::Format(wxT("Unable to create client socket for addr '%s'."), addr.c_str()));
        return false;
    }

    unsigned long  address = ::inet_addr(wx2lua(addr));
    if (address != INADDR_NONE)
        pHost = ::gethostbyaddr((const char*) &address, 4, AF_INET);
    else
        pHost = ::gethostbyname(wx2lua(addr));

    if (pHost == NULL)
    {
        AddErrorMessage(wxString::Format(wxT("Unable to get hostbyaddr or gethostbyname for addr '%s'."), addr.c_str()));
        return false;
    }

    if (pHost->h_addrtype != AF_INET)
    {
        AddErrorMessage(wxString::Format(wxT("Socket for addr '%s' is wrong type, isn't AF_INET."), addr.c_str()));
        return false;
    }

    memset(&m_sockaddress, 0, sizeof(m_sockaddress));
    memcpy(&(m_sockaddress.sin_addr), pHost->h_addr_list[0], pHost->h_length);

    m_sockaddress.sin_family = AF_INET;
    m_sockaddress.sin_port   = htons(port_number);

    m_address = lua2wx(inet_ntoa(m_sockaddress.sin_addr));
    m_port_number = ntohs(m_sockaddress.sin_port);

    if (::connect(m_sock, (sockaddr *) &m_sockaddress, sizeof(m_sockaddress)) == SOCKET_ERROR)
    {
        AddErrorMessage(wxString::Format(wxT("Unable to connect socket to addr '%s'."), addr.c_str()));
        return false;
    }

    m_sockstate = SOCKET_CONNECTED;
    return true;
}
Beispiel #3
0
wxString wxLuaCheckStack::DumpTable(const wxString &tablename, const wxString& msg)
{
    wxCHECK_MSG(m_luaState, wxEmptyString, wxT("Invalid lua_State"));

    lua_State* L = m_luaState;
    wxSortedArrayString tableArray;
    wxString s;

    // Allow iteration through table1.table2.table3...
    wxString tname(tablename);
    lua_pushglobaltable(L);

    do {
        lua_pushstring(L, wx2lua(tname.BeforeFirst(wxT('.'))));
        lua_rawget(L, -2);

        if (lua_isnil(L, -1) || !lua_istable(L, -1))
        {
            lua_pop(L, 2);  // remove table and value

            s.Printf(wxT("wxLuaCheckStack::DumpTable(L=%p) Table: '%s' cannot be found!\n"), L, tablename.c_str());
            OutputMsg(s);
            return s;
        }

        lua_remove(L, -2);  // remove previous table
        tname = tname.AfterFirst(wxT('.'));
    } while (tname.Len() > 0);

    s = DumpTable(lua_gettop(L), tablename, msg, tableArray, 0);
    lua_pop(L, 1);

    return s;
}
Beispiel #4
0
void wxlCanObjAddScript::DoUpdate( double absx, double absy )
{
    if ( m_pending )
    {
        wxLuaState lst = *(m_canvas->GetLuastate());
        lua_State* L = lst.GetLuaState();
        if ( 0 != lst.RunString( m_script ) )
        {
            wxLogWarning( _("Error in Lua Script") );
        }
        else
        {
            lua_getglobal( L, wx2lua( wxT("AddChilds") ) );
            lst.wxluaT_PushUserDataType(this, wxluatype_wxlCanObj, true);

            lst.lua_PushNumber( m_canvas->WorldToDeviceX( absx ) );
            lst.lua_PushNumber( m_canvas->WorldToDeviceX( absy ) );

            if ( lst.lua_PCall( 3, 0, 0 ) != 0 )
            {
                wxLuaEvent event(wxEVT_LUA_ERROR, lst.GetId(), lst);
                event.SetString(wxT("wrong AddChilds function"));
                lst.SendEvent(event);
            }
        }
        m_pending = false;
    }
}
Beispiel #5
0
bool wxlCanObjScript::DoIsHit( double x, double y, double absx, double absy )
{
    double xh,yh;
    xh = x - absx;
    yh = y - absy;

    // run all statements in the script which should be returning a hit on the object or not

    wxLuaState lst = *(m_canvas->GetLuastate());
    lua_State* L = lst.GetLuaState();

    lua_getglobal( L, wx2lua(m_objectname + wxT("Hit")) ); // push func on stack

    lst.lua_PushNumber( xh );                            // push func parameters
    lst.lua_PushNumber( yh );

    if ( lst.lua_PCall( 2, 1, 0 ) != 0 )    // call function, 2 params, 1 return
    {
        wxLuaEvent event(wxEVT_LUA_ERROR, lst.GetId(), lst);
        event.SetString(wxT("wrong hit function"));
        lst.SendEvent(event);
    }

    bool hit = lst.GetBooleanType(-1);           // get return value
    lst.lua_Pop(1);                              // pop return value from stack

    //wxLogDebug( "hit %d", hit );

    if (hit)
        return true;
    return false;
}
Beispiel #6
0
bool wxLuaDebugTarget::Run()
{
    wxCHECK_MSG(m_pThread == NULL, false, wxT("wxLuaDebugTarget::Run already called"));

    // Assume something is going to go wrong
    m_fErrorsSeen = true;

    m_pThread = new LuaThread(this);
    // Start the thread
    if ((m_pThread != NULL) &&
        (m_pThread->Create() == wxTHREAD_NO_ERROR) &&
        (m_pThread->Run()    == wxTHREAD_NO_ERROR))
    {
        // Wait for the connection to the server to complete
        if (!IsConnected())
        {
            wxMessageBox(wxT("Unable to connect to server"), wxT("wxLua client"), wxOK | wxCENTRE, NULL);
        }
        else
        {
            // OK, now we can start running.
            m_runCondition.Wait();

            m_fRunning    = true;
            m_fErrorsSeen = false;

            size_t idx, count = m_bufferArray.GetCount();
            for (idx = 0; idx < count; ++idx)
            {
                wxString luaBuffer = m_bufferArray.Item(idx);
                wxString bufFilename = luaBuffer.BeforeFirst(wxT('\0'));
                wxString buf =  luaBuffer.AfterFirst(wxT('\0'));

                wxLuaCharBuffer char_buf(buf);
                int rc = m_wxlState.LuaDoBuffer(char_buf, char_buf.Length(),
                                            wx2lua(bufFilename));

                m_fErrorsSeen = (rc != 0);
                if (m_fErrorsSeen)
                {
                    NotifyError(wxlua_LUA_ERR_msg(rc));
                    break;
                }
            }

            m_bufferArray.Clear();
        }
    }

    return !m_fErrorsSeen;
}
// %override wxLua_function_type
// %function int type(int wxluaarg_tag)
static int LUACALL wxLua_function_type(lua_State *L)
{
    int ltype = lua_type(L, 1);
    const char* ltypename = lua_typename(L, ltype);

    int wxl_type = wxluaT_type(L, 1);
    wxString wxltypeName = wxluaT_typename(L, wxl_type);

    // push the results
    lua_pushstring(L, wx2lua(wxltypeName));
    lua_pushnumber(L, wxl_type);

    lua_pushstring(L, ltypename);
    lua_pushnumber(L, ltype);

    return 4;
}
Beispiel #8
0
void wxlCanObjScript::DoDraw( wxDC& dc, double absx, double absy )
{
    // run all statements in the script which should be drawing statements on a wxDC
    wxLuaState lst = *(m_canvas->GetLuastate());
    lua_State* L = lst.GetLuaState();
    lua_getglobal( L, wx2lua(m_objectname + wxT("Draw")) );

    lst.wxluaT_PushUserDataType(&dc, wxluatype_wxDC, true);

    lst.lua_PushNumber( m_canvas->WorldToDeviceX( absx ) );
    lst.lua_PushNumber( m_canvas->WorldToDeviceX( absy ) );

    if ( lst.lua_PCall( 3, 0, 0 ) != 0 )
    {
        wxLuaEvent event(wxEVT_LUA_ERROR, lst.GetId(), lst);
        event.SetString(wxT("wrong draw function"));
        lst.SendEvent(event);
    }
}
int LUACALL wxluabind_wxLuaBinding__index(lua_State* L)
{
    void **ptr = (void **)lua_touserdata(L, 1);
    wxLuaBinding* wxlBinding = (wxLuaBinding*)*ptr;

    int idx_type = lua_type(L, 2);

    if (idx_type == LUA_TSTRING)
    {
        const char* idx_str = lua_tostring(L, 2);

        if (strcmp(idx_str, "GetBindingName") == 0)
        {
            lua_pushstring(L, wx2lua(wxlBinding->GetBindingName()));
            return 1;
        }
        else if (strcmp(idx_str, "GetLuaNamespace") == 0)
        {
            lua_pushstring(L, wx2lua(wxlBinding->GetLuaNamespace()));
            return 1;
        }
        else if (strcmp(idx_str, "GetClassCount") == 0)
        {
            lua_pushnumber(L, wxlBinding->GetClassCount());
            return 1;
        }
        else if (strcmp(idx_str, "GetNumberCount") == 0)
        {
            lua_pushnumber(L, wxlBinding->GetNumberCount());
            return 1;
        }
        else if (strcmp(idx_str, "GetStringCount") == 0)
        {
            lua_pushnumber(L, wxlBinding->GetStringCount());
            return 1;
        }
        else if (strcmp(idx_str, "GetEventCount") == 0)
        {
            lua_pushnumber(L, wxlBinding->GetEventCount());
            return 1;
        }
        else if (strcmp(idx_str, "GetObjectCount") == 0)
        {
            lua_pushnumber(L, wxlBinding->GetObjectCount());
            return 1;
        }
        else if (strcmp(idx_str, "GetFunctionCount") == 0)
        {
            lua_pushnumber(L, wxlBinding->GetFunctionCount());
            return 1;
        }
        else if (strcmp(idx_str, "GetClassArray") == 0)
        {
            wxLuaBindClass* wxlClass = wxlBinding->GetClassArray();
            size_t idx, count = wxlBinding->GetClassCount();
            lua_createtable(L, count, 0);

            for (idx = 0; idx < count; ++idx, ++wxlClass)
            {
                // Create table { wxLuaBindClass userdata }
                const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
                *ptr = wxlClass;
                    lua_newtable(L);
                    lua_pushstring(L, "__index");
                    lua_pushlightuserdata(L, wxlBinding);
                    lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
                    lua_rawset(L, -3);
                    lua_setmetatable(L, -2);

                lua_rawseti(L, -2, idx + 1);
            }

            return 1;
        }
        else if (strcmp(idx_str, "GetFunctionArray") == 0)
        {
            wxLuaBindMethod* wxlMethod = wxlBinding->GetFunctionArray();
            size_t idx, count = wxlBinding->GetFunctionCount();
            lua_createtable(L, count, 0);

            for (idx = 0; idx < count; ++idx, ++wxlMethod)
            {
                // Create table { wxLuaBindClass userdata }
                const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
                *ptr = wxlMethod;
                    lua_newtable(L);
                    lua_pushstring(L, "__index");
                    lua_pushlightuserdata(L, wxlBinding);
                    lua_pushcclosure(L, wxluabind_wxLuaBindMethod__index, 1); // push func with tag as upvalue
                    lua_rawset(L, -3);
                    lua_setmetatable(L, -2);

                lua_rawseti(L, -2, idx + 1);
            }

            return 1;
        }
        else if (strcmp(idx_str, "GetNumberArray") == 0)
        {
            wxLuaBindNumber* wxlNumber = wxlBinding->GetNumberArray();
            size_t idx, count = wxlBinding->GetNumberCount();
            lua_createtable(L, count, 0);

            for (idx = 0; idx < count; ++idx, ++wxlNumber)
            {
                // Create table { name, value }
                lua_createtable(L, 0, 2);
                lua_pushstring(L, "name");
                lua_pushstring(L, wxlNumber->name);
                lua_rawset(L, -3);
                lua_pushstring(L, "value");
                lua_pushnumber(L, wxlNumber->value);
                lua_rawset(L, -3);

                lua_rawseti(L, -2, idx + 1);
            }

            return 1;
        }
        else if (strcmp(idx_str, "GetStringArray") == 0)
        {
            wxLuaBindString* wxlString = wxlBinding->GetStringArray();
            size_t idx, count = wxlBinding->GetStringCount();
            lua_createtable(L, count, 0);

            for (idx = 0; idx < count; ++idx, ++wxlString)
            {
                // Create table { name, value }
                lua_createtable(L, 0, 2);
                lua_pushstring(L, "name");
                lua_pushstring(L, wxlString->name);
                lua_rawset(L, -3);
                lua_pushstring(L, "value");
                lua_pushstring(L, wx2lua(wxlString->value));
                lua_rawset(L, -3);

                lua_rawseti(L, -2, idx + 1);
            }

            return 1;
        }
        else if (strcmp(idx_str, "GetEventArray") == 0)
        {
            wxLuaBindEvent* wxlEvent = wxlBinding->GetEventArray();
            size_t idx, count = wxlBinding->GetEventCount();
            lua_createtable(L, count, 0);

            for (idx = 0; idx < count; ++idx, ++wxlEvent)
            {
                // Create table { name, eventType, wxluatype }
                lua_createtable(L, 0, 3);
                lua_pushstring(L, "name");
                lua_pushstring(L, wxlEvent->name);
                lua_rawset(L, -3);
                lua_pushstring(L, "eventType");
                lua_pushnumber(L, *wxlEvent->eventType);
                lua_rawset(L, -3);
                lua_pushstring(L, "wxluatype");
                lua_pushnumber(L, *wxlEvent->wxluatype);
                lua_rawset(L, -3);

                lua_pushstring(L, "wxLuaBindClass");
                const wxLuaBindClass* wxlClass = wxlBinding->GetBindClass(*wxlEvent->wxluatype);
                if (wxlClass == NULL)
                {
                    lua_pushnil(L);
                }
                else
                {
                    const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
                    *ptr = wxlClass;
                        lua_newtable(L);
                        lua_pushstring(L, "__index");
                        lua_pushlightuserdata(L, wxlBinding);
                        lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
                        lua_rawset(L, -3);
                        lua_setmetatable(L, -2);
                }
                lua_rawset(L, -3);

                lua_rawseti(L, -2, idx + 1);
            }

            return 1;
        }
        else if (strcmp(idx_str, "GetObjectArray") == 0)
        {
            wxLuaBindObject* wxlObject = wxlBinding->GetObjectArray();
            size_t idx, count = wxlBinding->GetObjectCount();
            lua_createtable(L, count, 0);

            for (idx = 0; idx < count; ++idx, ++wxlObject)
            {
                // Create table { name, object, wxluatype }
                lua_createtable(L, 0, 3);
                lua_pushstring(L, "name");
                lua_pushstring(L, wxlObject->name);
                lua_rawset(L, -3);

                lua_pushstring(L, "object");
                if (wxlObject->objPtr != 0)
                    wxluaT_pushuserdatatype(L, wxlObject->objPtr, *wxlObject->wxluatype, false);
                else
                    wxluaT_pushuserdatatype(L, *wxlObject->pObjPtr, *wxlObject->wxluatype, false);
                lua_rawset(L, -3);

                lua_pushstring(L, "wxluatype");
                lua_pushnumber(L, *wxlObject->wxluatype);
                lua_rawset(L, -3);

                lua_pushstring(L, "wxLuaBindClass");
                const wxLuaBindClass* wxlClass = wxlBinding->GetBindClass(*wxlObject->wxluatype);
                if (wxlClass == NULL)
                {
                    lua_pushnil(L);
                }
                else
                {
                    const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
                    *ptr = wxlClass;
                        lua_newtable(L);
                        lua_pushstring(L, "__index");
                        lua_pushlightuserdata(L, wxlBinding);
                        lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
                        lua_rawset(L, -3);
                        lua_setmetatable(L, -2);
                }
                lua_rawset(L, -3);

                lua_rawseti(L, -2, idx + 1);
            }

            return 1;
        }
    }

    return 0;
}