Ejemplo n.º 1
0
void wxLuaFreezeApp::OnLua(wxLuaEvent &event)
{
    OutputPrint(event.GetString());

    //if (event.GetEventType() == wxEVT_LUA_PRINT)
    //else if (event.GetEventType() == wxEVT_LUA_ERROR)
}
Ejemplo n.º 2
0
bool wxLuaFreezeApp::OnInit()
{
#ifdef __WXGTK__
    // this call is very important since otherwise scripts using the decimal
    // point '.' could not work with those locales which use a different symbol
    // (e.g. the comma) for the decimal point...
    // It doesn't work to put os.setlocale('c', 'numeric') in the Lua file that
    // you want to use decimal points in. That's because the file has been lexed
    // and compiler before the locale has changed, so the lexer - the part that
    // recognises numbers - will use the old locale.
    setlocale(LC_NUMERIC, "C");
#endif

    // load all image handlers for the wxLua script to make things easy
    wxInitAllImageHandlers();

    // Initialize the wxLua bindings we want to use.
    // See notes for WXLUA_DECLARE_BIND_ALL above.
    WXLUA_IMPLEMENT_BIND_ALL

    m_fileName = argv[0]; // the filename of 'this' program

    // When this function returns wxApp:MainLoop() will be called by wxWidgets
    // and so we want the Lua code wx.wxGetApp:MailLoop() to not
    // prematurely start it.
    wxLuaState::sm_wxAppMainLoop_will_run = true;

    m_wxlState = wxLuaState(this, wxID_ANY);
    if (!m_wxlState.Ok())
        return false;

    // if no script attached try to run a the program on the command line
    if (LoadScript(m_fileName, true) == LOADSCRIPT_MISSING)
    {
        if (argc > 1)
        {
            wxlua_pushargs(m_wxlState.GetLuaState(), argv, argc, 1);
            // just run it, lua gives a nice error message on failure
            m_wxlState.RunFile(argv[1]);
        }
        else
        {
            OutputPrint(wxString::Format(wxT("Expecting '<wxLuaFreeze:[script length]>' at end of '%s'.\n")
                                         wxT("Did you forget to run wxluafreeze.lua to attach your script?\n")
                                         wxT("You may run wxLua programs by specifying them on the command line."),
                                         m_fileName.c_str()));
        }
    }
    else
    {
        // now load our script to run that should have been appended to this file
        //  by wxluafreeze.lua
        wxString script = LoadScript(m_fileName);
        if (!script.IsEmpty())
        {
            wxlua_pushargs(m_wxlState.GetLuaState(), argv, argc, 0);
            m_wxlState.RunString(script);
        }
    }

    // no matter what - try to see any toplevel windows exist, if not then
    // there's no way to exit so assume that the program is all done
    wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
    if (node)
        return true;

    return false;
}
Ejemplo n.º 3
0
wxString wxLuaFreezeApp::LoadScript(const wxString& filename, bool only_check)
{
    // lots of debugging to make sure that everything is ok
    wxFile f;

    if (!f.Open(filename, wxFile::read))
    {
        OutputPrint(wxString::Format(wxT("Unable to open this executable file '%s' in read-only mode.\n"), filename.c_str()));
        return wxEmptyString;
    }

    // end of file is : "<wxLuaFreeze:%010d>", script_len
    #define END_TAG_LEN 24

    if (f.Seek(-END_TAG_LEN, wxFromEnd) == wxInvalidOffset)
    {
        f.Close();
        OutputPrint(wxString::Format(wxT("Unable to seek to last %d bytes at end of '%s'.\n"), END_TAG_LEN, filename.c_str()));
        return wxEmptyString;
    }

    // do some sanity checking before reading the script length

    char tag_buf[END_TAG_LEN+2] = {0};
    memset(tag_buf, 0, sizeof(char)*(END_TAG_LEN+2));
    long script_len = 0;

    if (f.Read((void*)tag_buf, END_TAG_LEN) == wxInvalidOffset)
    {
        f.Close();
        OutputPrint(wxString::Format(wxT("Unable to read wxLuaFreeze tag info at end of '%s'.\n"), filename.c_str()));
        return wxEmptyString;
    }

    if (sscanf(tag_buf, "<wxLuaFreeze:%ld>", &script_len) != 1)
    {
        f.Close();

        // they only wanted to know if the script exists, assume it's valid
        if (only_check)
            return LOADSCRIPT_MISSING;

        OutputPrint(wxString::Format(wxT("Expecting '<wxLuaFreeze:[script length]>' at end of '%s'.\n")
                                     wxT("Did you forget to run wxluafreeze.lua to attach your script?\n"), filename.c_str()));
        return wxEmptyString;
    }
    else if (f.Seek(-END_TAG_LEN - script_len, wxFromEnd) == wxInvalidOffset)
    {
        f.Close();
        OutputPrint(wxString::Format(wxT("Unable to seek to beginning of wxLua script at end of '%s'.\n"), filename.c_str()));
        return wxEmptyString;
    }

    // size should be valid from Seek statement
    char *script = (char*)malloc(script_len+2);
    if (script)
    {
        memset(script, 0, sizeof(script_len+2));
        f.Read(script, script_len);
        script[script_len] = 0;
    }

    f.Close();

    // we finally have our script!
    wxString scriptStr(lua2wx(script));
    free(script);

    return scriptStr;
}
Ejemplo n.º 4
0
void HandlrApp::OnLuaError(wxLuaEvent &event) {
    OutputPrint(event.GetString());
}