// // load file // void Configuration::loadConfigFile(const char *filename) { Dictionary *dict = Dictionary::createWithContentsOfFile(filename); CCASSERT(dict, "cannot create dictionary"); // search for metadata bool validMetadata = false; Object *metadata = dict->objectForKey("metadata"); if (metadata && dynamic_cast<Dictionary*>(metadata)) { Object *format_o = static_cast<Dictionary*>(metadata)->objectForKey("format"); // XXX: cocos2d-x returns Strings when importing from .plist. This bug will be addressed in cocos2d-x v3.x if (format_o && dynamic_cast<String*>(format_o)) { int format = static_cast<String*>(format_o)->intValue(); // Support format: 1 if (format == 1) { validMetadata = true; } } } if (! validMetadata) { CCLOG("Invalid config format for file: %s", filename); return; } Object *data = dict->objectForKey("data"); if (!data || !dynamic_cast<Dictionary*>(data)) { CCLOG("Expected 'data' dict, but not found. Config file: %s", filename); return; } // Add all keys in the existing dictionary Dictionary *data_dict = static_cast<Dictionary*>(data); DictElement* element; CCDICT_FOREACH(data_dict, element) { if(! _valueDict->objectForKey( element->getStrKey() )) _valueDict->setObject(element->getObject(), element->getStrKey()); else CCLOG("Key already present. Ignoring '%s'", element->getStrKey()); } }
void PrettyPrinter::visit(const __Dictionary *p) { _result += "\n"; _result += _indentStr; _result += "<dict>\n"; setIndentLevel(_indentLevel+1); DictElement* element; bool bFirstElement = true; char buf[1000] = {0}; CCDICT_FOREACH(p, element) { if (!bFirstElement) { _result += "\n"; } sprintf(buf, "%s%s: ", _indentStr.c_str(),element->getStrKey()); _result += buf; PrettyPrinter v(_indentLevel); //FIXME:james element->getObject()->acceptVisitor(v); _result += v.getResult(); bFirstElement = false; } setIndentLevel(_indentLevel-1); _result += "\n"; _result += _indentStr; _result += "</dict>"; }
Dictionary* TextureCache::snapshotTextures() { Dictionary* pRet = new Dictionary(); DictElement* pElement = NULL; CCDICT_FOREACH(_textures, pElement) { pRet->setObject(pElement->getObject(), pElement->getStrKey()); }
void CCJSONConverter::convertDictionaryToJson(__Dictionary *Dictionary, cJSON *json) { #if COCOS2D_VERSION >= 0x00030000 DictElement * pElement = NULL; #else CCDictElement * pElement = NULL; #endif CCDICT_FOREACH(Dictionary, pElement){ Ref * obj = pElement->getObject(); cJSON * jsonItem = getObjJson(obj); cJSON_AddItemToObject(json, pElement->getStrKey(), jsonItem); }
void AnimationCache::parseVersion1(Dictionary* animations) { SpriteFrameCache *frameCache = SpriteFrameCache::sharedSpriteFrameCache(); DictElement* pElement = NULL; CCDICT_FOREACH(animations, pElement) { Dictionary* animationDict = (Dictionary*)pElement->getObject(); Array* frameNames = (Array*)animationDict->objectForKey("frames"); float delay = animationDict->valueForKey("delay")->floatValue(); Animation* animation = NULL; if ( frameNames == NULL ) { CCLOG("cocos2d: AnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", pElement->getStrKey()); continue; } Array* frames = Array::createWithCapacity(frameNames->count()); frames->retain(); Object* pObj = NULL; CCARRAY_FOREACH(frameNames, pObj) { const char* frameName = ((String*)pObj)->getCString(); SpriteFrame* spriteFrame = frameCache->spriteFrameByName(frameName); if ( ! spriteFrame ) { CCLOG("cocos2d: AnimationCache: Animation '%s' refers to frame '%s' which is not currently in the SpriteFrameCache. This frame will not be added to the animation.", pElement->getStrKey(), frameName); continue; } AnimationFrame* animFrame = new AnimationFrame(); animFrame->initWithSpriteFrame(spriteFrame, 1, NULL); frames->addObject(animFrame); animFrame->release(); } if ( frames->count() == 0 ) { CCLOG("cocos2d: AnimationCache: None of the frames for animation '%s' were found in the SpriteFrameCache. Animation is not being added to the Animation Cache.", pElement->getStrKey()); continue; } else if ( frames->count() != frameNames->count() ) { CCLOG("cocos2d: AnimationCache: An animation in your dictionary refers to a frame which is not in the SpriteFrameCache. Some or all of the frames for the animation '%s' may be missing.", pElement->getStrKey()); } animation = Animation::create(frames, delay, 1); AnimationCache::sharedAnimationCache()->addAnimation(animation, pElement->getStrKey()); frames->release(); }
mrb_value dictionary_to_rubyval(mrb_state* mrb, __Dictionary* dict) { mrb_value rhash = mrb_hash_new(mrb); DictElement* element = nullptr; std::string className = ""; __String* strVal = nullptr; __Dictionary* dictVal = nullptr; __Array* arrVal = nullptr; __Double* doubleVal = nullptr; __Bool* boolVal = nullptr; __Float* floatVal = nullptr; __Integer* intVal = nullptr; CCDICT_FOREACH(dict, element) { if (nullptr == element) continue; mrb_value rkey = mrb_str_new_cstr(mrb, element->getStrKey()); mrb_value rval; std::string typeName = typeid(element->getObject()).name(); auto iter = g_rubyType.find(typeName); if (g_rubyType.end() != iter) { className = iter->second; if (nullptr != dynamic_cast<Ref*>(element->getObject())) { rval = to_mrb_value(mrb, element->getObject()); } } else if((strVal = dynamic_cast<__String *>(element->getObject()))) { rval = mrb_str_new_cstr(mrb, strVal->getCString()); } else if ((dictVal = dynamic_cast<__Dictionary*>(element->getObject()))) { rval = dictionary_to_rubyval(mrb, dictVal); } else if ((arrVal = dynamic_cast<__Array*>(element->getObject()))) { rval = array_to_rubyval(mrb, arrVal); } else if ((doubleVal = dynamic_cast<__Double*>(element->getObject()))) { rval = mrb_float_value(mrb, (mrb_float)doubleVal->getValue()); } else if ((floatVal = dynamic_cast<__Float*>(element->getObject()))) { rval = mrb_float_value(mrb, (mrb_float)floatVal->getValue()); } else if ((intVal = dynamic_cast<__Integer*>(element->getObject()))) { rval = mrb_fixnum_value((mrb_int)intVal->getValue()); } else if ((boolVal = dynamic_cast<__Bool*>(element->getObject()))) { rval = mrb_bool_value((mrb_bool)boolVal->getValue()); } else { CCASSERT(false, "the type isn't suppored."); } mrb_hash_set(mrb, rhash, rkey, rval); } return rhash; }
//遍历 void CommonDictionary::foreach() { DictElement * pElement; CCDICT_FOREACH(pDict, pElement) { const char * key = pElement->getStrKey(); const char* value = (const char*)pDict->objectForKey(key); printf("CommonDictionary ---> key = %s, value = %s\n", key, value); //CCLog(key); //CCLog(value); //myStrcat(Str, value); //CCLog(Str); } }
static void dictionaryToMap(__Dictionary* dict, map<string,string>& ret) { if (dict == nullptr) return; DictElement* el = nullptr; CCDICT_FOREACH(dict, el) { auto str = dynamic_cast<__String*>(el->getObject()); auto boolean = dynamic_cast<__Bool*>(el->getObject()); auto number = dynamic_cast<__Double*>(el->getObject()); stringstream ss; if (str) ss << str->getCString(); else if (boolean) ss << (int)boolean->getValue(); else if (number) ss << number->getValue(); ret[el->getStrKey()] = ss.str(); }
void GB2ShapeCache::addShapesWithFile(const std::string &plist) { std::string fullName = FileUtils::getInstance()->fullPathForFilename(plist); __Dictionary* dict = __Dictionary::createWithContentsOfFile(fullName.c_str()); CCAssert(dict != NULL, "Shape-file not found"); // not triggered - cocos2dx delivers empty dict if non was found CCAssert(dict->count() != 0, "plist file empty or not existing"); __Dictionary *metadataDict = (__Dictionary *)dict->objectForKey("metadata"); int format = static_cast<__String *>(metadataDict->objectForKey("format"))->intValue(); ptmRatio = static_cast<__String *>(metadataDict->objectForKey("ptm_ratio"))->floatValue(); CCAssert(format == 1, "Format not supported"); __Dictionary *bodyDict = (__Dictionary *)dict->objectForKey("bodies"); b2Vec2 vertices[b2_maxPolygonVertices]; DictElement* pElement; std::string bodyName; __Dictionary *bodyData; CCDICT_FOREACH(bodyDict,pElement){ bodyName = pElement->getStrKey(); bodyData =(__Dictionary*)pElement->getObject(); BodyDef *bodyDef = new BodyDef(); bodyDef->anchorPoint = PointFromString(static_cast<__String *>(bodyData->objectForKey("anchorpoint"))->getCString()); __Array *fixtureList = (__Array *)(bodyData->objectForKey("fixtures")); FixtureDef **nextFixtureDef = &(bodyDef->fixtures); Ref* object; CCARRAY_FOREACH(fixtureList, object){ b2FixtureDef basicData; __Dictionary *fixtureData = (__Dictionary*)object; basicData.filter.categoryBits = static_cast< __String*>(fixtureData->objectForKey("filter_categoryBits"))->intValue(); basicData.filter.maskBits = static_cast<__String*>(fixtureData->objectForKey("filter_maskBits"))->intValue(); basicData.filter.groupIndex = static_cast<__String *>(fixtureData->objectForKey("filter_groupIndex"))->intValue(); basicData.friction = static_cast<__String *>(fixtureData->objectForKey("friction"))->floatValue(); basicData.density = static_cast<__String *>(fixtureData->objectForKey("density"))->floatValue(); basicData.restitution = static_cast<__String *>(fixtureData->objectForKey("restitution"))->floatValue(); basicData.isSensor = (bool)static_cast<__String *>(fixtureData->objectForKey("isSensor"))->boolValue(); __String *cb = static_cast<__String *>(fixtureData->objectForKey("userdataCbValue")); int callbackData = 0; if (cb) callbackData = cb->intValue(); std::string fixtureType = static_cast<__String *>(fixtureData->objectForKey("fixture_type"))->getCString(); if (fixtureType == "POLYGON") { __Array *polygonsArray = (__Array *)(fixtureData->objectForKey("polygons")); Ref* singlePolygon; CCARRAY_FOREACH(polygonsArray, singlePolygon){ FixtureDef *fix = new FixtureDef(); fix->fixture = basicData; // copy basic data fix->callbackData = callbackData; b2PolygonShape *polyshape = new b2PolygonShape(); int vindex = 0; __Array *verticeArray = (__Array *)singlePolygon; assert(verticeArray->count() <= b2_maxPolygonVertices); Ref* singlePoint; CCARRAY_FOREACH(verticeArray, singlePoint){ __String* vert = (__String*)singlePoint; cocos2d::Point offset = PointFromString(vert->getCString()); vertices[vindex].x = (offset.x / ptmRatio) ; vertices[vindex].y = (offset.y / ptmRatio) ; vindex++; }