diff --git a/RetroFE/Source/Collection/CollectionInfoBuilder.cpp b/RetroFE/Source/Collection/CollectionInfoBuilder.cpp index c00a5f7..794cdd5 100644 --- a/RetroFE/Source/Collection/CollectionInfoBuilder.cpp +++ b/RetroFE/Source/Collection/CollectionInfoBuilder.cpp @@ -146,22 +146,25 @@ bool CollectionInfoBuilder::createCollectionDirectory(std::string name) struct CIBCallbackInfo { std::string collectionName; - void (*callback)(void *); + void (*callback)(void *, CollectionInfo *); void *context; CollectionInfoBuilder *cib; }; int CollectionInfoBuilder::buildCollectionThread(void *context) { - CIBCallbackInfo *info = (CIBCallbackInfo *)context; - info->cib->buildCollection(info->collectionName); - info->callback(info->context); - delete info; + CIBCallbackInfo *cbinfo = (CIBCallbackInfo *)context; + + CollectionInfo *ci = cbinfo->cib->buildCollection(cbinfo->collectionName); + + cbinfo->callback(cbinfo->context, ci); + + delete cbinfo; return 0; } -void CollectionInfoBuilder::buildCollection(std::string collectionName, void (*callback)(void *), void *context) +void CollectionInfoBuilder::buildCollection(std::string collectionName, void (*callback)(void *, CollectionInfo *), void *context) { CIBCallbackInfo *data = new CIBCallbackInfo(); data->collectionName = collectionName; @@ -219,9 +222,21 @@ CollectionInfo *CollectionInfoBuilder::buildCollection(std::string name, std::st ImportDirectory(collection, mergedCollectionName); + //todo: add a critical section + allocationMap_[collection] = collection; + return collection; } +void CollectionInfoBuilder::destroyCollection(CollectionInfo *collection) { + std::map::iterator it = allocationMap_.find(collection); + + if(it != allocationMap_.end()) { + delete it->second; + allocationMap_.erase(it); + } +} + bool CollectionInfoBuilder::ImportBasicList(CollectionInfo *info, std::string file, std::map &list) { diff --git a/RetroFE/Source/Collection/CollectionInfoBuilder.h b/RetroFE/Source/Collection/CollectionInfoBuilder.h index 9de57d7..673fbb7 100644 --- a/RetroFE/Source/Collection/CollectionInfoBuilder.h +++ b/RetroFE/Source/Collection/CollectionInfoBuilder.h @@ -29,10 +29,11 @@ class CollectionInfoBuilder public: CollectionInfoBuilder(Configuration &c, MetadataDatabase &mdb); virtual ~CollectionInfoBuilder(); - void buildCollection(std::string collectionName, void (*callback)(void *), void *context); + void buildCollection(std::string collectionName, void (*callback)(void *, CollectionInfo *), void *context); CollectionInfo *buildCollection(std::string collectionName); CollectionInfo *buildCollection(std::string collectionName, std::string mergedCollectionName); static bool createCollectionDirectory(std::string collectionName); + void destroyCollection(CollectionInfo *info); private: static int buildCollectionThread(void *context); @@ -40,4 +41,5 @@ private: MetadataDatabase &metaDB_; bool ImportBasicList(CollectionInfo *info, std::string file, std::map &list); bool ImportDirectory(CollectionInfo *info, std::string mergedCollectionName); + std::map allocationMap_; }; diff --git a/RetroFE/Source/Lua/LuaCollection.cpp b/RetroFE/Source/Lua/LuaCollection.cpp index 7a4d43b..01f551c 100644 --- a/RetroFE/Source/Lua/LuaCollection.cpp +++ b/RetroFE/Source/Lua/LuaCollection.cpp @@ -16,14 +16,24 @@ void LuaCollection::initialize(Configuration *c, CollectionInfoBuilder *b, LuaE int LuaCollection::load(lua_State *l) { std::string collection = luaL_checkstring(l, 1); + cib->buildCollection(collection, loadCallback, (void *)l); return 0; } -void LuaCollection::loadCallback(void *context) +int LuaCollection::destroy(lua_State *l) +{ + CollectionInfo *i = (CollectionInfo *)luaL_checkinteger(l, 1); + cib->destroyCollection(i); + + return 0; +} + + +void LuaCollection::loadCallback(void *context, CollectionInfo *info) { lua_State *l = (lua_State *)context; - events->trigger(l, "onCollectionLoaded"); + events->trigger(l, "onCollectionLoaded", (void *)info); } diff --git a/RetroFE/Source/Lua/LuaCollection.h b/RetroFE/Source/Lua/LuaCollection.h index ccd70fb..e053b7b 100644 --- a/RetroFE/Source/Lua/LuaCollection.h +++ b/RetroFE/Source/Lua/LuaCollection.h @@ -9,5 +9,7 @@ namespace LuaCollection { void initialize(Configuration *c, CollectionInfoBuilder *b, LuaEvent *e); int load(lua_State *l); - void loadCallback(void *context); + int destroy(lua_State *l); + + void loadCallback(void *context, CollectionInfo *info); }; \ No newline at end of file diff --git a/RetroFE/Source/Lua/LuaEvent.cpp b/RetroFE/Source/Lua/LuaEvent.cpp index 753dac3..6f5886e 100644 --- a/RetroFE/Source/Lua/LuaEvent.cpp +++ b/RetroFE/Source/Lua/LuaEvent.cpp @@ -7,6 +7,16 @@ void LuaEvent::trigger(lua_State *l, std::string type) lua_pcall(l, 0, 0, 0); } +void LuaEvent::trigger(lua_State *l, std::string type, void *value) +{ + lua_getglobal(l, type.c_str()); + lua_pushinteger(l, (int)value); + + lua_pcall(l, 1, 0, 0); +} + + + bool LuaEvent::isBusy(lua_State *l, std::string type) { bool retval = true; diff --git a/RetroFE/Source/Lua/LuaEvent.h b/RetroFE/Source/Lua/LuaEvent.h index 28ac0ed..2ba854a 100644 --- a/RetroFE/Source/Lua/LuaEvent.h +++ b/RetroFE/Source/Lua/LuaEvent.h @@ -6,5 +6,6 @@ class LuaEvent { public: void trigger(lua_State *l, std::string type); + void trigger(lua_State *l, std::string type, void *value); bool isBusy(lua_State *l, std::string type); }; \ No newline at end of file diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index dc584f8..230cbb3 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -82,6 +82,7 @@ const luaL_Reg RetroFE::luaImageFuncs[] = { const luaL_Reg RetroFE::luaCollectionFuncs[] = { {"load", LuaCollection::load}, + {"destroy", LuaCollection::destroy}, {NULL, NULL} };