void EditorPluginLoader::setWorldEditor(Lumix::WorldEditor& editor)
{
	m_global_state = luaL_newstate();
	registerAPI();
	auto lua_plugins = QDir("plugins").entryInfoList(QStringList() << "*.lua");
	for (auto& lua_plugin : lua_plugins)
	{
		QFile file(lua_plugin.absoluteFilePath());
		if (file.open(QIODevice::ReadOnly))
		{
			auto content = file.readAll();
			bool errors =
				luaL_loadbuffer(
					m_global_state,
					content.data(),
					content.size(),
					lua_plugin.absoluteFilePath().toLatin1().data()) != LUA_OK;
			errors = errors ||
					 lua_pcall(m_global_state, 0, LUA_MULTRET, 0) != LUA_OK;
			if (errors)
			{
				Lumix::g_log_error.log("editor")
					<< lua_plugin.absoluteFilePath().toLatin1().data() << ": "
					<< lua_tostring(m_global_state, -1);
				lua_pop(m_global_state, 1);
			}
		}
		else
		{
			Lumix::g_log_warning.log("editor")
				<< "Could not open plugin "
				<< lua_plugin.absoluteFilePath().toLatin1().data();
		}
	}
}
Exemple #2
0
int32_t main
#endif
(int argc,const char *argv[])
{
    char *retbuf,*line,*jsonargs,*transportstr,registerbuf[MAX_JSON_FIELD];
    struct plugin_info *plugin; double startmilli; cJSON *argjson;
    int32_t max,sendflag,sleeptime=1,len = 0;
#ifndef BUNDLED
    portable_OS_init();
#endif
    milliseconds2();
    max = 1000000;
    retbuf = malloc(max+1);
    plugin = calloc(1,sizeof(*plugin));// + PLUGIN_EXTRASIZE);
    //plugin->extrasize = PLUGIN_EXTRASIZE;
    plugin->ppid = OS_getppid();
    strcpy(plugin->name,PLUGINSTR);
    if ( 0 )
    {
        int32_t i;
        for (i=0; i<argc; i++)
            printf("(%s) ",argv[i]);
        printf("argc.%d\n",argc);
    }
    //printf("%s (%s).argc%d parent PID.%d\n",plugin->name,argv[0],argc,plugin->ppid);
    plugin->timeout = 1;
    if ( argc <= 2 )
    {
        jsonargs = (argc > 1) ? stringifyM((char *)argv[1]) : stringifyM("{}");
        configure_plugin(retbuf,max,plugin,jsonargs,-1);
        free(jsonargs);
//fprintf(stderr,"PLUGIN_RETURNS.[%s]\n",retbuf), fflush(stdout);
        return(0);
    }
    randombytes((uint8_t *)&plugin->myid,sizeof(plugin->myid));
    plugin->permanentflag = atoi(argv[1]);
    plugin->daemonid = calc_nxt64bits(argv[2]);
#ifdef BUNDLED
    plugin->bundledflag = 1;
#endif
    transportstr = get_localtransport(plugin->bundledflag);
    sprintf(plugin->connectaddr,"%s://SuperNET.agents",transportstr);
    sprintf(plugin->bindaddr,"%s://%llu",transportstr,(long long)plugin->daemonid);
    jsonargs = (argc >= 3) ? ((char *)argv[3]) : 0;
    configure_plugin(retbuf,max,plugin,jsonargs,1);
    printf("CONFIGURED.(%s) argc.%d: %s myid.%llu daemonid.%llu NXT.%s serviceNXT.%s\n",plugin->name,argc,plugin->permanentflag != 0 ? "PERMANENT" : "WEBSOCKET",(long long)plugin->myid,(long long)plugin->daemonid,plugin->NXTADDR,plugin->SERVICENXT);//,jsonargs!=0?jsonargs:"");
    if ( init_pluginsocks(plugin,plugin->permanentflag,plugin->myid,plugin->daemonid,plugin->timeout) == 0 )
    {
        argjson = cJSON_Parse(jsonargs);
        if ( (len= registerAPI(registerbuf,sizeof(registerbuf)-1,plugin,argjson)) > 0 )
        {
            //fprintf(stderr,">>>>>>>>>>>>>>> plugin.(%s) sends REGISTER SEND.(%s)\n",plugin->name,registerbuf);
            nn_local_broadcast2(plugin->pushsock,0,0,(uint8_t *)registerbuf,(int32_t)strlen(registerbuf)+1), plugin->numsent++;
        } else printf("error register API\n");
        if ( argjson != 0 )
            free_json(argjson);
    } else printf("error init_pluginsocks\n");
    while ( OS_getppid() == plugin->ppid )
    {
        retbuf[0] = 0;
        if ( (len= nn_recv(plugin->pullsock,&line,NN_MSG,0)) > 0 )
        {
            len = (int32_t)strlen(line);
            //printf("(s%d r%d) <<<<<<<<<<<<<< %s.RECEIVED (%s).%d -> bind.(%s) connect.(%s) %s\n",plugin->numsent,plugin->numrecv,plugin->name,line,len,plugin->bindaddr,plugin->connectaddr,plugin->permanentflag != 0 ? "PERMANENT" : "WEBSOCKET"), fflush(stdout);
            if ( (len= process_plugin_json(retbuf,max,&sendflag,plugin,plugin->permanentflag,plugin->daemonid,plugin->myid,line)) > 0 )
            {
                if ( plugin->bundledflag == 0 )
                    printf("%s\n",retbuf), fflush(stdout);
                if ( sendflag != 0 )
                {
                    nn_local_broadcast2(plugin->pushsock,0,0,(uint8_t *)retbuf,(int32_t)strlen(retbuf)+1), plugin->numsent++;
                    //fprintf(stderr,">>>>>>>>>>>>>> returned.(%s)\n",retbuf);
                }
            } //else printf("null return from process_plugin_json\n");
            nn_freemsg(line);
        }
        else
        {
            startmilli = milliseconds2();
            if ( PLUGNAME(_idle)(plugin) == 0 )
            {
                if ( plugin->sleepmillis != 0 )
                {
                    sleeptime = plugin->sleepmillis - (milliseconds2() - startmilli);
                    //printf("%s sleepmillis.%d sleeptime.%d\n",plugin->name,plugin->sleepmillis,sleeptime);
                    if ( sleeptime > 0 )
                        msleep2(sleeptime);
                }
            }
        }
    } fprintf(stderr,"ppid.%d changed to %d\n",plugin->ppid,OS_getppid());
    PLUGNAME(_shutdown)(plugin,len); // rc == 0 -> parent process died
    nn_shutdown(plugin->pushsock,0);
    if ( plugin->pushsock != plugin->pullsock )
        nn_shutdown(plugin->pullsock,0);
    free(plugin);
    return(len);
}