techtree_t::techtree_t(fs_t& fs,const std::string& name): mgr_t(fs), xml_loadable_t(name), path(fs.canocial(std::string("techs")+"/"+name)) { { const strings_t subdirs = fs.list_dirs(path+"/factions"); for(strings_t::const_iterator i=subdirs.begin(); i!=subdirs.end(); i++) if(fs.exists(path+"/factions/"+*i+"/"+*i+".xml")) { factions.push_back(*i); faction_refs.push_back(ref_t(*this,FACTION,*i)); } if(!factions.size()) data_error("techtree "<<name<<" contains no factions"); }{ const strings_t subdirs = fs.list_dirs(path+"/resources"); for(strings_t::const_iterator i=subdirs.begin(); i!=subdirs.end(); i++) if(fs.exists(path+"/resources/"+*i+"/"+*i+".xml")) { resources.push_back(*i); resource_refs.push_back(ref_t(*this,RESOURCE,*i)); } if(!resources.size()) data_error("techtree "<<name<<" contains no resources"); } fs_file_t::ptr_t f(fs.get(path+"/"+name+".xml")); istream_t::ptr_t in(f->reader()); load_xml(*in); }
virtual ~py_exec_request_t() { // Need to GIL-ensure here, since this might be called // from the main thread. PYW_GIL_GET; py_callable = ref_t(); // Release callable }
//--------------------------------------------------------------------------- ref_t PyW_IntVecToPyList(const intvec_t &intvec) { size_t c = intvec.size(); PYW_GIL_CHECK_LOCKED_SCOPE(); newref_t py_list(PyList_New(c)); for ( size_t i=0; i<c; i++ ) PyList_SetItem(py_list.o, i, PyInt_FromLong(intvec[i])); return ref_t(py_list); }
int main(int argc,char** args) { srand(time(NULL)); try { if (SDL_Init(SDL_INIT_VIDEO)) { fprintf(stderr,"Unable to initialize SDL: %s\n",SDL_GetError()); return EXIT_FAILURE; } atexit(SDL_Quit); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); #if 0 screen = SDL_SetVideoMode(1440,900,32,SDL_OPENGL|SDL_FULLSCREEN); #else screen = SDL_SetVideoMode(1024,768,32,SDL_OPENGL/*|SDL_FULLSCREEN*/); #endif if(!screen) { fprintf(stderr,"Unable to create SDL screen: %s\n",SDL_GetError()); return EXIT_FAILURE; } SDL_WM_SetCaption("GlestNG","GlestNG"); GLenum err = glewInit(); if(GLEW_OK != err) { fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); return EXIT_FAILURE; } fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); // we have a GL context so we can go ahead and init all the singletons std::auto_ptr<fs_t> fs_settings(fs_t::create("data/")); fs_t::settings = fs_settings.get(); // set it globally std::auto_ptr<xml_parser_t> xml_settings(new xml_parser_t("UI Settings",fs_settings->get_body("ui_settings.xml"))); xml_settings->set_as_settings(); std::auto_ptr<graphics_t::mgr_t> graphics_mgr(graphics_t::create()); std::auto_ptr<fonts_t> fonts(fonts_t::create()); std::auto_ptr<fs_t> fs; try { fs.reset(fs_t::create("data/Glest")); if(false) { fs_file_t::ptr_t logo_file(fs_settings->get("logo.g3d")); istream_t::ptr_t logostream(logo_file->reader()); logo = std::auto_ptr<model_g3d_t>(new model_g3d_t(*logostream)); } load(*fs); } catch(glest_exception_t* e) { std::cerr << "cannot load glest data: " << e << std::endl; delete e; } std::auto_ptr<ui_mgr_t> ui_(ui_mgr()); std::auto_ptr<mod_ui_t> mod_ui(mod_ui_t::create()); std::auto_ptr<terrain_t> terrain(terrain_t::gen_planet(5,500,3)); //world()->dump(std::cout); v4_t light_amb(0,0,0,1), light_dif(1.,1.,1.,1.), light_spec(1.,1.,1.,1.), light_pos(1.,1.,-1.,0.), mat_amb(.7,.7,.7,1.), mat_dif(.8,.8,.8,1.), mat_spec(1.,1.,1.,1.); glLightfv(GL_LIGHT0,GL_AMBIENT,light_amb.v); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_dif.v); glLightfv(GL_LIGHT0,GL_SPECULAR,light_spec.v); glLightfv(GL_LIGHT0,GL_POSITION,light_pos.v); glLightfv(GL_LIGHT1,GL_AMBIENT,light_amb.v); glLightfv(GL_LIGHT1,GL_DIFFUSE,light_dif.v); glLightfv(GL_LIGHT1,GL_SPECULAR,light_spec.v); glMaterialfv(GL_FRONT,GL_AMBIENT,mat_amb.v); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_dif.v); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_spec.v); glMaterialf(GL_FRONT,GL_SHININESS,100.0); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glAlphaFunc(GL_GREATER,0.4); glEnable(GL_COLOR_MATERIAL); glEnable(GL_RESCALE_NORMAL); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_NORMALIZE); glFrontFace(GL_CW); camera(); bool quit = false; SDL_Event event; SDL_EnableKeyRepeat(200,20); SDL_EnableUNICODE(true); const unsigned start = SDL_GetTicks(); framerate.reset(); while(!quit) { set_now(SDL_GetTicks()-start); while(!quit && SDL_PollEvent(&event)) { if(ui_mgr()->offer(event)) continue; switch (event.type) { case SDL_MOUSEMOTION: if(selection) std::cout << "drag" << std::endl; /*printf("Mouse moved by %d,%d to (%d,%d)\n", event.motion.xrel, event.motion.yrel, event.motion.x, event.motion.y);*/ break; case SDL_MOUSEBUTTONDOWN: click(event.button.x,event.button.y); if(selection) std::cout << "selection: "<<selected_point<<std::endl; break; case SDL_MOUSEBUTTONUP: if(selection) std::cout << "selection stopped" << std::endl; selection = false; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_PLUS: zoom += 1; camera(); break; case SDLK_MINUS: zoom -= 1; camera(); break; case SDLK_ESCAPE: quit = true; break; case SDLK_m: // MODDING MODE if(!fs.get()) { std::cerr << "(modding menu triggered but mod not loaded)" << std::endl; break; } if(mod_ui->is_shown()) mod_ui->hide(); else mod_ui->show(ref_t(*techtree,TECHTREE,techtree->name)); break; default: std::cout << "Ignoring key " << (int)event.key.keysym.scancode << "," << event.key.keysym.sym << "," << event.key.keysym.mod << "," << event.key.keysym.unicode << std::endl; } break; case SDL_QUIT: quit = true; break; } } framerate.tick(now()); tick(); } for(tests_t::iterator i=objs.begin(); i!=objs.end(); i++) delete *i; return EXIT_SUCCESS; } catch(data_error_t* de) { std::cerr << "Oh! " << de << std::endl; } catch(graphics_error_t* ge) { std::cerr << "Oh! " << ge << std::endl; } catch(panic_t* panic) { std::cerr << "Oh! " << panic << std::endl; } return EXIT_FAILURE; }
int register_dt(PyObject *py_obj) { PYW_GIL_CHECK_LOCKED_SCOPE(); // Already registered? if ( dtid >= 0 ) return dtid; memset(&dt, 0, sizeof(dt)); dt.cbsize = sizeof(dt); dt.ud = this; do { ref_t py_attr; // name if ( !PyW_GetStringAttr(py_obj, S_NAME, &dt_name) ) break; dt.name = dt_name.c_str(); // menu_name (optional) if ( PyW_GetStringAttr(py_obj, S_MENU_NAME, &dt_menu_name) ) dt.menu_name = dt_menu_name.c_str(); // asm_keyword (optional) if ( PyW_GetStringAttr(py_obj, S_ASM_KEYWORD, &dt_asm_keyword) ) dt.asm_keyword = dt_asm_keyword.c_str(); // hotkey (optional) if ( PyW_GetStringAttr(py_obj, S_HOTKEY, &dt_hotkey) ) dt.hotkey = dt_hotkey.c_str(); // value_size py_attr = PyW_TryGetAttrString(py_obj, S_VALUE_SIZE); if ( py_attr != NULL && PyInt_Check(py_attr.o) ) dt.value_size = PyInt_AsLong(py_attr.o); py_attr = ref_t(); // props py_attr = PyW_TryGetAttrString(py_obj, S_PROPS); if ( py_attr != NULL && PyInt_Check(py_attr.o) ) dt.props = PyInt_AsLong(py_attr.o); py_attr = ref_t(); // may_create_at py_attr = PyW_TryGetAttrString(py_obj, S_MAY_CREATE_AT); if ( py_attr != NULL && PyCallable_Check(py_attr.o) ) dt.may_create_at = s_may_create_at; py_attr = ref_t(); // calc_item_size py_attr = PyW_TryGetAttrString(py_obj, S_CALC_ITEM_SIZE); if ( py_attr != NULL && PyCallable_Check(py_attr.o) ) dt.calc_item_size = s_calc_item_size; py_attr = ref_t(); // Now try to register dtid = register_custom_data_type(&dt); if ( dtid < 0 ) break; // Hold reference to the PyObject Py_INCREF(py_obj); py_self = py_obj; py_attr = newref_t(PyInt_FromLong(dtid)); PyObject_SetAttrString(py_obj, S_ID, py_attr.o); } while ( false ); return dtid; }
//------------------------------------------------------------------------- // Converts an IDC variable to a Python variable // If py_var points to an existing object then the object will be updated // If py_var points to an existing immutable object then ZERO is returned // Returns one of CIP_xxxx. Check pywraps.hpp int idcvar_to_pyvar( const idc_value_t &idc_var, ref_t *py_var) { PYW_GIL_CHECK_LOCKED_SCOPE(); switch ( idc_var.vtype ) { case VT_PVOID: if ( *py_var == NULL ) { newref_t nr(PyCObject_FromVoidPtr(idc_var.pvoid, NULL)); *py_var = nr; } else { return CIP_IMMUTABLE; } break; case VT_INT64: { // Recycle? if ( *py_var != NULL ) { // Recycling an int64 object? int t = get_pyidc_cvt_type(py_var->o); if ( t != PY_ICID_INT64 ) return CIP_IMMUTABLE; // Cannot recycle immutable object // Update the attribute PyObject_SetAttrString(py_var->o, S_PY_IDCCVT_VALUE_ATTR, PyLong_FromLongLong(idc_var.i64)); return CIP_OK; } ref_t py_cls(get_idaapi_attr_by_id(PY_CLSID_CVT_INT64)); if ( py_cls == NULL ) return CIP_FAILED; *py_var = newref_t(PyObject_CallFunctionObjArgs(py_cls.o, PyLong_FromLongLong(idc_var.i64), NULL)); if ( PyW_GetError() || *py_var == NULL ) return CIP_FAILED; break; } #if !defined(NO_OBSOLETE_FUNCS) || defined(__EXPR_SRC) case VT_STR: *py_var = newref_t(PyString_FromString(idc_var.str)); break; #endif case VT_STR2: if ( *py_var == NULL ) { const qstring &s = idc_var.qstr(); *py_var = newref_t(PyString_FromStringAndSize(s.begin(), s.length())); break; } else return CIP_IMMUTABLE; // Cannot recycle immutable object case VT_LONG: // Cannot recycle immutable objects if ( *py_var != NULL ) return CIP_IMMUTABLE; *py_var = newref_t(cvt_to_pylong(idc_var.num)); break; case VT_FLOAT: if ( *py_var == NULL ) { double x; if ( ph.realcvt(&x, (uint16 *)idc_var.e, (sizeof(x)/2-1)|010) != 0 ) INTERR(30160); *py_var = newref_t(PyFloat_FromDouble(x)); break; } else return CIP_IMMUTABLE; case VT_REF: { if ( *py_var == NULL ) { ref_t py_cls(get_idaapi_attr_by_id(PY_CLSID_CVT_BYREF)); if ( py_cls == NULL ) return CIP_FAILED; // Create a byref object with None value. We populate it later *py_var = newref_t(PyObject_CallFunctionObjArgs(py_cls.o, Py_None, NULL)); if ( PyW_GetError() || *py_var == NULL ) return CIP_FAILED; } int t = get_pyidc_cvt_type(py_var->o); if ( t != PY_ICID_BYREF ) return CIP_FAILED; // Dereference // (Since we are not using VREF_COPY flag, we can safely const_cast) idc_value_t *dref_v = VarDeref(const_cast<idc_value_t *>(&idc_var), VREF_LOOP); if ( dref_v == NULL ) return CIP_FAILED; // Can we recycle the object? ref_t new_py_val(PyW_TryGetAttrString(py_var->o, S_PY_IDCCVT_VALUE_ATTR)); if ( new_py_val != NULL ) { // Recycle t = idcvar_to_pyvar(*dref_v, &new_py_val); // Success? Nothing more to be done if ( t == CIP_OK ) return CIP_OK; // Clear it so we don't recycle it new_py_val = ref_t(); } // Try to convert (not recycle) if ( idcvar_to_pyvar(*dref_v, &new_py_val) != CIP_OK ) return CIP_FAILED; // Update the attribute PyObject_SetAttrString(py_var->o, S_PY_IDCCVT_VALUE_ATTR, new_py_val.o); break; } // Can convert back into a Python object or Python dictionary // (Depending if py_var will be recycled and it was a dictionary) case VT_OBJ: { // Check if this IDC object has __cvt_id__ and the __idc_cvt_value__ fields idc_value_t idc_val; if ( VarGetAttr(&idc_var, S_PY_IDCCVT_ID_ATTR, &idc_val) == eOk && VarGetAttr(&idc_var, S_PY_IDCCVT_VALUE_ATTR, &idc_val) == eOk ) { // Extract the object *py_var = borref_t((PyObject *) idc_val.pvoid); return CIP_OK_OPAQUE; } ref_t obj; bool is_dict = false; // Need to create a new object? if ( *py_var == NULL ) { // Get skeleton class reference ref_t py_cls(get_idaapi_attr_by_id(PY_CLSID_APPCALL_SKEL_OBJ)); if ( py_cls == NULL ) return CIP_FAILED; // Call constructor obj = newref_t(PyObject_CallFunctionObjArgs(py_cls.o, NULL)); if ( PyW_GetError() || obj == NULL ) return CIP_FAILED; } else { // Recycle existing variable obj = *py_var; if ( PyDict_Check(obj.o) ) is_dict = true; } // Walk the IDC attributes and store into python for (const char *attr_name = VarFirstAttr(&idc_var); attr_name != NULL; attr_name = VarNextAttr(&idc_var, attr_name) ) { // Get the attribute idc_value_t v; VarGetAttr(&idc_var, attr_name, &v, true); // Convert attribute to a python value (recursively) ref_t py_attr; int cvt = idcvar_to_pyvar(v, &py_attr); if ( cvt <= CIP_IMMUTABLE ) return CIP_FAILED; if ( is_dict ) PyDict_SetItemString(obj.o, attr_name, py_attr.o); else PyObject_SetAttrString(obj.o, attr_name, py_attr.o); } *py_var = obj; break; } // Unhandled type default: *py_var = ref_t(); return CIP_FAILED; } return CIP_OK; }