cxAny cxActionRootCreate(cxConstChars xml) { cxEngine engine = cxEngineInstance(); cxActionRoot this = cxHashGet(engine->actions, cxHashStrKey(xml)); if(this != NULL){ return this; } cxXMLScript script = cxEngineGetXMLScript(xml); if(script == NULL || script->bytes == NULL){ CX_ERROR("%s script not register",xml); return NULL; } this = CX_CREATE(cxActionRoot); xmlTextReaderPtr reader = cxXMLReaderForScript(script,cxActionRootReaderError, this); if(reader == NULL){ CX_ERROR("create xml reader failed"); return NULL; } CX_EVENT_FIRE(this, onBegin); cxActionRootLoadCodesWithReader(this, reader); cxHashKey key = cxHashStrKey(xml); cxHashSet(this->codes, key, script->bytes); cxHashSet(engine->actions, key, this); CX_EVENT_FIRE(this, onEnd); return this; }
cxAny cxPlayEffect(cxConstChars file,cxBool loop) { cxPlayer this = cxPlayerInstance(); JniMethodInfo methodInfo; cxBool ret = cxGetStaticMethodInfo(&methodInfo, CLASS_NAME, "cxEnginePlayEffect","(Ljava/lang/String;Z)I"); CX_ASSERT(ret, "get static method info failed"); CX_UNUSED_PARAM(ret); jstring path = (*methodInfo.env)->NewStringUTF(methodInfo.env,file); cxInt soundId = (*methodInfo.env)->CallStaticIntMethod(methodInfo.env, methodInfo.classID, methodInfo.methodID, path, loop); (*methodInfo.env)->DeleteLocalRef(methodInfo.env,path); if(soundId <= 0){ CX_ERROR("play file failed %s",file); return NULL; } cxTrack track = cxHashGet(this->tracks, cxHashStrKey(file)); //add or replace if(track == NULL || track->soundId != soundId){ track = CX_ALLOC(cxTrack); track->file = cxStringAllocChars(file); track->soundId = soundId; cxHashSet(this->tracks, cxHashStrKey(file), track); CX_RELEASE(track); } return track; }
cxTypes cxHashRootReadHash(cxHashRoot root,xmlTextReaderPtr reader) { cxTypes types = cxHashTypesCreate(); int depth = xmlTextReaderDepth(reader); while(xmlTextReaderRead(reader) && depth != xmlTextReaderDepth(reader)){ if(xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT){ continue; } cxConstChars key = cxXMLAttr(reader,"key"); if(key == NULL){ continue; } cxAny value = NULL; cxConstChars temp = cxXMLReadElementName(reader); if(ELEMENT_IS_TYPE(cxString)){ cxTypes types = cxHashRootReadString(root, reader); value = types != NULL ? types->any : NULL; } else if(ELEMENT_IS_TYPE(cxHash)){ cxTypes types = cxHashRootReadHash(root, reader); value = types != NULL ? types->any : NULL; } else if(ELEMENT_IS_TYPE(cxArray)){ cxTypes types = cxHashRootReadArray(root, reader); value = types != NULL ? types->any : NULL; } else { value = cxReadValues(root, temp, reader); } if(value != NULL){ cxHashSet(types->any, cxHashStrKey(key), value); } } return types; }
static void cxActionRootLoadCodesWithReader(cxAny pav,xmlTextReaderPtr reader) { cxActionRoot this = pav; cxReaderAttrInfo *info = cxReaderAttrInfoMake(reader, pav, pav); while(xmlTextReaderRead(reader) && !this->isError){ if(xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT){ continue; } cxConstChars temp = cxXMLReadElementName(reader); if(!ELEMENT_IS_TYPE(cxActionRoot)){ continue; } cxObjectReadAttrRun(info); break; } while(xmlTextReaderRead(reader)){ CX_CONTINUE(xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT); cxConstChars id = cxXMLAttr(reader, "id"); CX_CONTINUE(id == NULL); cxString code = cxXMLReaderReadOuterXml(reader); cxHashSet(this->codes, cxHashStrKey(id), code); } }
//use global cached cxTexture cxTextureFactoryLoadFile(cxConstChars file) { cxTextureFactory factory = cxTextureFactoryInstance(); cxTexture texture = cxHashGet(factory->caches, cxHashStrKey(file)); if(texture != NULL){ return texture; } if((texture = cxTextureCreate(file)) != NULL){ cxHashSet(factory->caches, cxHashStrKey(file), texture); } return texture; }
cxBool cxHashRootLoadWithReader(cxHashRoot root,xmlTextReaderPtr reader) { cxBool ret = false; while(xmlTextReaderRead(reader)){ if(root->isError){ break; } if(xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT){ continue; } cxConstChars temp = cxXMLReadElementName(reader); if(ELEMENT_IS_TYPE(cxHashRoot)){ ret = true; break; } } CX_RETURN(!ret,false); cxAutoPoolPush(); while(xmlTextReaderRead(reader)){ if(xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT){ continue; } cxConstChars temp = cxXMLReadElementName(reader); if(ELEMENT_IS_TYPE(cxDBEnv)){ cxHashRootReadDBEnv(root,reader); continue; } cxConstChars sid = cxXMLAttr(reader, "id"); if(sid == NULL){ CX_WARN("element %s:not set id,data not save to hash table",temp); continue; } cxAny object = NULL; if(ELEMENT_IS_TYPE(cxString)){ //cxTypesString object = cxHashRootReadString(root,reader); }else if(ELEMENT_IS_TYPE(cxHash)){ //cxTypesHash object = cxHashRootReadHash(root,reader); }else if(ELEMENT_IS_TYPE(cxArray)){ //cxTypesArray object = cxHashRootReadArray(root,reader); }else{ //cxTypesNumber object = cxHashRootReadNumber(root, temp, reader); } if(object != NULL){ cxHashSet(root->items, cxHashStrKey(sid), object); } } cxAutoPoolPop(); return ret; }
cxBMPFont cxEngineLoadBMPFont(cxConstChars file) { cxEngine this = cxEngineInstance(); cxBMPFont font = cxHashGet(this->bmpfonts, cxHashStrKey(file)); if(font != NULL){ return font; } font = cxBMPFontCreate(file); if(font == NULL){ return NULL; } cxHashSet(this->bmpfonts, cxHashStrKey(file), font); return font; }
cxAny cxEventBaseHttpConnect(cxConstChars host,cxInt port) { cxEventBase this = cxEventBaseInstance(); cxConstChars key = CX_CONST_STRING("%s:%d",host,port); cxHttpConn conn = cxHashGet(this->conns, cxHashStrKey(key)); if(conn != NULL){ return conn; } conn = cxHttpConnectOpen(host, port); if(conn == NULL){ CX_ERROR("http open %s:%d failed",host,port); return NULL; } cxHashSet(this->conns, cxHashStrKey(key), conn); return conn; }
static void cxHashRootReadDB(cxDBEnv env,cxHashRoot root,xmlTextReaderPtr reader) { cxReaderAttrInfo *info = cxReaderAttrInfoMake(reader, root, env); int depth = xmlTextReaderDepth(reader); while(xmlTextReaderRead(reader) && depth != xmlTextReaderDepth(reader)){ if(xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT){ continue; } cxConstChars temp = cxXMLReadElementName(reader); if(!ELEMENT_IS_TYPE(cxDB)){ continue; } cxConstChars file = cxXMLAttr(reader,"file"); cxConstChars table = cxXMLAttr(reader,"table"); cxConstChars type = cxXMLAttr(reader,"type"); cxConstChars sid = cxXMLAttr(reader,"id"); cxConstChars path = cxXMLAttr(reader,"path"); //assert file copy ->to document cxBool copy = cxXMLReadBoolAttr(info, "copy", false); if(copy && file != NULL){ cxCopyFile(file, NULL, NULL); } cxBool rdonly = cxXMLReadBoolAttr(info, "rdonly", false); if(sid == NULL){ CX_WARN("db id not set,will can't add dataset"); } cxString sfile = NULL; if(cxConstCharsEqu(path, "assert")){ sfile = cxAssetsPath(file); //assert must set true rdonly = true; }else if(cxConstCharsEqu(path, "document")){ sfile = cxDocumentPath(file); }else{ CX_ERROR("must set path assert or document"); } cxAny db = NULL; if(file != NULL && table != NULL && type != NULL){ db = cxDBCreate(env, cxStringBody(sfile), table, type, rdonly); } if(db != NULL && sid != NULL){ cxHashSet(root->items, cxHashStrKey(sid), cxDBTypesCreate(db)); }else{ CX_ERROR("open dbenv type %s,db %s:%s failed",cxStringBody(env->type),file,table); } } }
cxAny cxEngineTypes(cxConstChars url) { CX_RETURN(url == NULL, NULL); cxEngine this = cxEngineInstance(); cxUrlPath path = cxUrlPathParse(url); CX_RETURN(path == NULL, NULL); cxHashRoot sets = cxHashGet(this->datasets, cxHashStrKey(path->path)); if(sets == NULL){ sets = cxHashRootCreate(path->path); CX_RETURN(sets == NULL, NULL); cxHashSet(this->datasets, cxHashStrKey(path->path), sets); } cxAny ret = NULL; if(path->count >= 2){ ret = cxHashGet(sets->items, cxHashStrKey(path->key)); } return ret; }
cxXMLScript cxEngineGetXMLScript(cxConstChars file) { cxEngine this = cxEngineInstance(); CX_RETURN(file == NULL,NULL); cxXMLScript script = cxHashGet(this->scripts, cxHashStrKey(file)); if(script != NULL){ return script; } script = CX_CREATE(cxXMLScript); cxStream stream = cxAssetsStreamCreate(file); CX_RETURN(stream == NULL, NULL); CX_RETAIN_SWAP(script->bytes, cxStreamAllBytes(stream)); CX_RETURN(script->bytes == NULL, NULL); cxHashSet(this->scripts, cxHashStrKey(file), script); return script; }
static cxBool cxTextureMTFMake(cxAny pthis,cxStr data,cxInt idx) { CX_ASSERT_THIS(pthis, cxTextureMTF); CX_ASSERT(idx >= 0 && idx < this->header.count, "idx range"); cxTexture ret = NULL; cxInt off = sizeof(cxTextureMTFHeader) + idx * this->header.length; cxStr d = cxStrNoCopy(cxStrBody(data) + off, this->header.length); if (d == NULL) { return false; } cxStream stream = cxMemStreamCreateRefStr(d); if(this->header.type == 1){ ret = cxTexturePVRLoadStream(stream); } if(this->header.type == 2){ ret = cxTexturePKMLoadStream(stream); } if(ret == NULL){ return false; } cxHashSet(this->caches, cxHashIntKey(idx), ret); return true; }
void cxViewSetCache(cxAny pview,cxConstChars key,cxAny object) { cxView this = pview; CX_ASSERT(object != NULL, "args error"); cxHashSet(this->caches, cxHashStrKey(key), object); }
void cxSetConvert(cxConstChars name,cxAny func) { CX_ASSERT(convertFuncs != NULL, "global cxjson not init"); CX_ASSERT(cxConstCharsOK(name) && func != NULL, "name or func error"); cxHashSet(convertFuncs, cxHashStrKey(name), cxConvertCreate(func)); }