Collection loading and deletion support (in a thread).

This commit is contained in:
emb
2015-11-21 10:49:38 -06:00
parent 34e59adb1d
commit 45ffcb148c
7 changed files with 51 additions and 10 deletions

View File

@@ -146,22 +146,25 @@ bool CollectionInfoBuilder::createCollectionDirectory(std::string name)
struct CIBCallbackInfo { struct CIBCallbackInfo {
std::string collectionName; std::string collectionName;
void (*callback)(void *); void (*callback)(void *, CollectionInfo *);
void *context; void *context;
CollectionInfoBuilder *cib; CollectionInfoBuilder *cib;
}; };
int CollectionInfoBuilder::buildCollectionThread(void *context) int CollectionInfoBuilder::buildCollectionThread(void *context)
{ {
CIBCallbackInfo *info = (CIBCallbackInfo *)context; CIBCallbackInfo *cbinfo = (CIBCallbackInfo *)context;
info->cib->buildCollection(info->collectionName);
info->callback(info->context); CollectionInfo *ci = cbinfo->cib->buildCollection(cbinfo->collectionName);
delete info;
cbinfo->callback(cbinfo->context, ci);
delete cbinfo;
return 0; 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(); CIBCallbackInfo *data = new CIBCallbackInfo();
data->collectionName = collectionName; data->collectionName = collectionName;
@@ -219,9 +222,21 @@ CollectionInfo *CollectionInfoBuilder::buildCollection(std::string name, std::st
ImportDirectory(collection, mergedCollectionName); ImportDirectory(collection, mergedCollectionName);
//todo: add a critical section
allocationMap_[collection] = collection;
return collection; return collection;
} }
void CollectionInfoBuilder::destroyCollection(CollectionInfo *collection) {
std::map<CollectionInfo *, CollectionInfo *>::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<std::string, Item *> &list) bool CollectionInfoBuilder::ImportBasicList(CollectionInfo *info, std::string file, std::map<std::string, Item *> &list)
{ {

View File

@@ -29,10 +29,11 @@ class CollectionInfoBuilder
public: public:
CollectionInfoBuilder(Configuration &c, MetadataDatabase &mdb); CollectionInfoBuilder(Configuration &c, MetadataDatabase &mdb);
virtual ~CollectionInfoBuilder(); 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);
CollectionInfo *buildCollection(std::string collectionName, std::string mergedCollectionName); CollectionInfo *buildCollection(std::string collectionName, std::string mergedCollectionName);
static bool createCollectionDirectory(std::string collectionName); static bool createCollectionDirectory(std::string collectionName);
void destroyCollection(CollectionInfo *info);
private: private:
static int buildCollectionThread(void *context); static int buildCollectionThread(void *context);
@@ -40,4 +41,5 @@ private:
MetadataDatabase &metaDB_; MetadataDatabase &metaDB_;
bool ImportBasicList(CollectionInfo *info, std::string file, std::map<std::string, Item *> &list); bool ImportBasicList(CollectionInfo *info, std::string file, std::map<std::string, Item *> &list);
bool ImportDirectory(CollectionInfo *info, std::string mergedCollectionName); bool ImportDirectory(CollectionInfo *info, std::string mergedCollectionName);
std::map<CollectionInfo *, CollectionInfo *> allocationMap_;
}; };

View File

@@ -16,14 +16,24 @@ void LuaCollection::initialize(Configuration *c, CollectionInfoBuilder *b, LuaE
int LuaCollection::load(lua_State *l) int LuaCollection::load(lua_State *l)
{ {
std::string collection = luaL_checkstring(l, 1); std::string collection = luaL_checkstring(l, 1);
cib->buildCollection(collection, loadCallback, (void *)l); cib->buildCollection(collection, loadCallback, (void *)l);
return 0; 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; lua_State *l = (lua_State *)context;
events->trigger(l, "onCollectionLoaded"); events->trigger(l, "onCollectionLoaded", (void *)info);
} }

View File

@@ -9,5 +9,7 @@ namespace LuaCollection
{ {
void initialize(Configuration *c, CollectionInfoBuilder *b, LuaEvent *e); void initialize(Configuration *c, CollectionInfoBuilder *b, LuaEvent *e);
int load(lua_State *l); int load(lua_State *l);
void loadCallback(void *context); int destroy(lua_State *l);
void loadCallback(void *context, CollectionInfo *info);
}; };

View File

@@ -7,6 +7,16 @@ void LuaEvent::trigger(lua_State *l, std::string type)
lua_pcall(l, 0, 0, 0); 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 LuaEvent::isBusy(lua_State *l, std::string type)
{ {
bool retval = true; bool retval = true;

View File

@@ -6,5 +6,6 @@
class LuaEvent { class LuaEvent {
public: public:
void trigger(lua_State *l, std::string type); 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); bool isBusy(lua_State *l, std::string type);
}; };

View File

@@ -82,6 +82,7 @@ const luaL_Reg RetroFE::luaImageFuncs[] = {
const luaL_Reg RetroFE::luaCollectionFuncs[] = { const luaL_Reg RetroFE::luaCollectionFuncs[] = {
{"load", LuaCollection::load}, {"load", LuaCollection::load},
{"destroy", LuaCollection::destroy},
{NULL, NULL} {NULL, NULL}
}; };