mirror of
https://github.com/FunKey-Project/RetroFE.git
synced 2026-04-02 02:05:55 +02:00
Changed lua monitoring calls. Support for threading.
This commit is contained in:
@@ -3,9 +3,10 @@ syntax: glob
|
||||
*.suo
|
||||
.git
|
||||
Build/
|
||||
Documentation/**
|
||||
Documentation/Documentation/*
|
||||
Documentation/Artifacts/*
|
||||
Documentation/Manual/_build/*
|
||||
Documentation/Manual/**
|
||||
Configuration/Configuration/bin/**
|
||||
Configuration/Configuration/obj/**
|
||||
Artifacts/**
|
||||
|
||||
@@ -91,12 +91,19 @@ if(NOT WIN32)
|
||||
endif()
|
||||
|
||||
set(RETROFE_HEADERS
|
||||
"${RETROFE_DIR}/Source/Collection/CollectionInfo.h"
|
||||
"${RETROFE_DIR}/Source/Collection/CollectionInfoBuilder.h"
|
||||
"${RETROFE_DIR}/Source/Collection/Item.h"
|
||||
"${RETROFE_DIR}/Source/Database/Configuration.h"
|
||||
"${RETROFE_DIR}/Source/Database/DB.h"
|
||||
"${RETROFE_DIR}/Source/Database/Metadata.h"
|
||||
"${RETROFE_DIR}/Source/Database/MetadataDatabase.h"
|
||||
"${RETROFE_DIR}/Source/Graphics/Component/Image.h"
|
||||
"${RETROFE_DIR}/Source/Graphics/Component/Component.h"
|
||||
"${RETROFE_DIR}/Source/Graphics/Component/ComponentData.h"
|
||||
"${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.h"
|
||||
"${RETROFE_DIR}/Source/Lua/Lua.h"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaCollection.h"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaDisplay.h"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaEvent.h"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaImage.h"
|
||||
@@ -108,7 +115,12 @@ set(RETROFE_HEADERS
|
||||
)
|
||||
|
||||
set(RETROFE_SOURCES
|
||||
"${RETROFE_DIR}/Source/Collection/CollectionInfo.cpp"
|
||||
"${RETROFE_DIR}/Source/Collection/CollectionInfoBuilder.cpp"
|
||||
"${RETROFE_DIR}/Source/Collection/Item.cpp"
|
||||
"${RETROFE_DIR}/Source/Database/Configuration.cpp"
|
||||
"${RETROFE_DIR}/Source/Database/DB.cpp"
|
||||
"${RETROFE_DIR}/Source/Database/MetadataDatabase.cpp"
|
||||
"${RETROFE_DIR}/Source/Graphics/Component/Image.cpp"
|
||||
"${RETROFE_DIR}/Source/Graphics/Component/Component.cpp"
|
||||
"${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.cpp"
|
||||
@@ -117,6 +129,7 @@ set(RETROFE_SOURCES
|
||||
"${RETROFE_DIR}/Source/Lua/Lua.cpp"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaDisplay.cpp"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaImage.cpp"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaCollection.cpp"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaEvent.cpp"
|
||||
"${RETROFE_DIR}/Source/Lua/LuaLog.cpp"
|
||||
"${RETROFE_DIR}/Source/Main.cpp"
|
||||
@@ -124,6 +137,7 @@ set(RETROFE_SOURCES
|
||||
"${RETROFE_DIR}/Source/StateMachine.cpp"
|
||||
"${RETROFE_DIR}/Source/SDL.cpp"
|
||||
"${RETROFE_DIR}/Source/Version.cpp"
|
||||
"${SQLITE3_ROOT}/sqlite3.c"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "../Database/DB.h"
|
||||
#include "../Utility/Log.h"
|
||||
#include "../Utility/Utils.h"
|
||||
#include "SDL2/SDL_thread.h"
|
||||
#include <dirent.h>
|
||||
|
||||
#ifdef __linux
|
||||
@@ -142,6 +143,35 @@ bool CollectionInfoBuilder::createCollectionDirectory(std::string name)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct CIBCallbackInfo {
|
||||
std::string collectionName;
|
||||
void (*callback)(void *);
|
||||
void *context;
|
||||
CollectionInfoBuilder *cib;
|
||||
};
|
||||
|
||||
int CollectionInfoBuilder::buildCollectionThread(void *context)
|
||||
{
|
||||
CIBCallbackInfo *info = (CIBCallbackInfo *)context;
|
||||
info->cib->buildCollection(info->collectionName);
|
||||
info->callback(info->context);
|
||||
delete info;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void CollectionInfoBuilder::buildCollection(std::string collectionName, void (*callback)(void *), void *context)
|
||||
{
|
||||
CIBCallbackInfo *data = new CIBCallbackInfo();
|
||||
data->collectionName = collectionName;
|
||||
data->callback = callback;
|
||||
data->context = context;
|
||||
data->cib = this;
|
||||
|
||||
SDL_Thread *thread = SDL_CreateThread(buildCollectionThread, "buildCollection", (void *)data);
|
||||
}
|
||||
|
||||
CollectionInfo *CollectionInfoBuilder::buildCollection(std::string name)
|
||||
{
|
||||
return buildCollection(name, "");
|
||||
|
||||
@@ -29,11 +29,13 @@ class CollectionInfoBuilder
|
||||
public:
|
||||
CollectionInfoBuilder(Configuration &c, MetadataDatabase &mdb);
|
||||
virtual ~CollectionInfoBuilder();
|
||||
void buildCollection(std::string collectionName, void (*callback)(void *), void *context);
|
||||
CollectionInfo *buildCollection(std::string collectionName);
|
||||
CollectionInfo *buildCollection(std::string collectionName, std::string mergedCollectionName);
|
||||
static bool createCollectionDirectory(std::string collectionName);
|
||||
|
||||
private:
|
||||
static int buildCollectionThread(void *context);
|
||||
Configuration &conf_;
|
||||
MetadataDatabase &metaDB_;
|
||||
bool ImportBasicList(CollectionInfo *info, std::string file, std::map<std::string, Item *> &list);
|
||||
|
||||
29
RetroFE/Source/Lua/LuaCollection.cpp
Normal file
29
RetroFE/Source/Lua/LuaCollection.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "LuaCollection.h"
|
||||
#include "../Collection/CollectionInfo.h"
|
||||
#include <string>
|
||||
#include "../Collection/Item.h"
|
||||
|
||||
Configuration *config;
|
||||
CollectionInfoBuilder *cib;
|
||||
LuaEvent *events;
|
||||
void LuaCollection::initialize(Configuration *c, CollectionInfoBuilder *b, LuaEvent *e)
|
||||
{
|
||||
config = c;
|
||||
cib = b;
|
||||
events = e;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
lua_State *l = (lua_State *)context;
|
||||
events->trigger(l, "onCollectionLoaded");
|
||||
|
||||
}
|
||||
13
RetroFE/Source/Lua/LuaCollection.h
Normal file
13
RetroFE/Source/Lua/LuaCollection.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Lua.h"
|
||||
#include "LuaEvent.h"
|
||||
#include "../Collection/CollectionInfoBuilder.h"
|
||||
#include "../Database/Configuration.h"
|
||||
|
||||
namespace LuaCollection
|
||||
{
|
||||
void initialize(Configuration *c, CollectionInfoBuilder *b, LuaEvent *e);
|
||||
int load(lua_State *l);
|
||||
void loadCallback(void *context);
|
||||
};
|
||||
@@ -1,27 +1,21 @@
|
||||
#include "LuaEvent.h"
|
||||
|
||||
void LuaEvent::registerCallback(std::string type, std::string functionName, std::string complete)
|
||||
{
|
||||
callbacks_[type] = functionName;
|
||||
completeCallbacks_[type] = complete;
|
||||
}
|
||||
|
||||
|
||||
void LuaEvent::trigger(lua_State *l, std::string type)
|
||||
{
|
||||
lua_getglobal(l, callbacks_[type].c_str());
|
||||
lua_call(l, 0, 0);
|
||||
lua_getglobal(l, type.c_str());
|
||||
|
||||
lua_pcall(l, 0, 0, 0);
|
||||
}
|
||||
|
||||
bool LuaEvent::isComplete(lua_State *l, std::string type)
|
||||
bool LuaEvent::isBusy(lua_State *l, std::string type)
|
||||
{
|
||||
bool retval = true;
|
||||
lua_getglobal(l, callbacks_[type].c_str());
|
||||
lua_getglobal(l, type.c_str());
|
||||
if(lua_pcall(l, 0, 1, 0) == 0)
|
||||
{
|
||||
if(!lua_isnil(l, 1))
|
||||
if(lua_isboolean(l, -1))
|
||||
{
|
||||
if(!lua_toboolean(l, 1))
|
||||
if(!lua_toboolean(l, -1))
|
||||
{
|
||||
retval = false;
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@
|
||||
|
||||
class LuaEvent {
|
||||
public:
|
||||
void registerCallback(std::string type, std::string functionName, std::string complete);
|
||||
void trigger(lua_State *l, std::string type);
|
||||
bool isComplete(lua_State *l, std::string type);
|
||||
private:
|
||||
std::map<std::string, std::string> callbacks_;
|
||||
std::map<std::string, std::string> completeCallbacks_;
|
||||
bool isBusy(lua_State *l, std::string type);
|
||||
};
|
||||
@@ -20,7 +20,7 @@ int LuaImage::create(lua_State *l)
|
||||
int LuaImage::destroy(lua_State *l)
|
||||
{
|
||||
Image *i = (Image *)luaL_checkinteger(l, 1);
|
||||
delete i;
|
||||
factory->deleteComponent(i);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "SDL.h"
|
||||
#include "Graphics/Component/Image.h"
|
||||
#include "Graphics/Component/Component.h"
|
||||
#include "Lua/LuaCollection.h"
|
||||
#include "Lua/LuaDisplay.h"
|
||||
#include "Lua/LuaImage.h"
|
||||
#include "Lua/LuaLog.h"
|
||||
@@ -79,6 +80,11 @@ const luaL_Reg RetroFE::luaImageFuncs[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
const luaL_Reg RetroFE::luaCollectionFuncs[] = {
|
||||
{"load", LuaCollection::load},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
const luaL_Reg RetroFE::luaDisplayFuncs[] = {
|
||||
{"getCenter", LuaDisplay::getCenter},
|
||||
{"getDimensions", LuaDisplay::getDimensions},
|
||||
@@ -98,6 +104,12 @@ void RetroFE::initializeLua()
|
||||
{
|
||||
lua_.initialize();
|
||||
LuaImage::initialize(factory_);
|
||||
LuaCollection::initialize(&config_, cib_, &luaEvent_);
|
||||
|
||||
lua_newtable(lua_.state);
|
||||
luaL_setfuncs (lua_.state, luaCollectionFuncs, 0);
|
||||
lua_pushvalue(lua_.state, -1);
|
||||
lua_setglobal(lua_.state, "collection");
|
||||
|
||||
lua_newtable(lua_.state);
|
||||
luaL_setfuncs (lua_.state, luaDisplayFuncs, 0);
|
||||
@@ -122,17 +134,20 @@ void RetroFE::reloadLuaScripts()
|
||||
luaL_loadfile(lua_.state, path.c_str());
|
||||
lua_pcall(lua_.state, 0, LUA_MULTRET, 0);
|
||||
|
||||
luaEvent_.registerCallback("onInitEnter", "onInitEnter", "onInitEnterComplete");
|
||||
luaEvent_.registerCallback("onInitExit", "onInitExit", "onInitExitComplete");
|
||||
}
|
||||
|
||||
RetroFE::RetroFE(Configuration &c)
|
||||
: config_(c)
|
||||
, db_(c.absolutePath + "/meta.db")
|
||||
, mdb_(NULL)
|
||||
, cib_(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
RetroFE::~RetroFE()
|
||||
{
|
||||
if(cib_) delete cib_;
|
||||
if(mdb_) delete mdb_;
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +155,11 @@ RetroFE::~RetroFE()
|
||||
void RetroFE::run()
|
||||
{
|
||||
if(!SDL::initialize(config_)) return;
|
||||
|
||||
|
||||
db_.initialize();
|
||||
mdb_ = new MetadataDatabase(db_, config_);
|
||||
cib_ = new CollectionInfoBuilder(config_, *mdb_);
|
||||
mdb_->initialize();
|
||||
initializeLua();
|
||||
StateMachine state(lua_.state, &luaEvent_);
|
||||
|
||||
|
||||
@@ -15,9 +15,12 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "Collection/CollectionInfoBuilder.h"
|
||||
#include "Database/DB.h"
|
||||
#include "Database/Configuration.h"
|
||||
#include "Database/MetadataDatabase.h"
|
||||
#include "Lua/Lua.h"
|
||||
#include "Lua/LuaEvent.h"
|
||||
#include "Database/Configuration.h"
|
||||
#include "Graphics/Component/ComponentFactory.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <vector>
|
||||
@@ -38,4 +41,8 @@ private:
|
||||
static const luaL_Reg luaDisplayFuncs[];
|
||||
static const luaL_Reg luaLogFuncs[];
|
||||
static const luaL_Reg luaImageFuncs[];
|
||||
static const luaL_Reg luaCollectionFuncs[];
|
||||
DB db_;
|
||||
MetadataDatabase *mdb_;
|
||||
CollectionInfoBuilder *cib_;
|
||||
};
|
||||
|
||||
@@ -3,28 +3,36 @@
|
||||
StateMachine::StateMachine(lua_State *l, LuaEvent *e)
|
||||
: luaState_(l)
|
||||
, luaEvent_(e)
|
||||
, state_(ON_INIT_ENTER)
|
||||
, state_(ON_STARTUP_ENTER)
|
||||
{
|
||||
}
|
||||
|
||||
void StateMachine::update(float dt)
|
||||
{
|
||||
switch(state_) {
|
||||
case ON_INIT_ENTER:
|
||||
luaEvent_->trigger(luaState_, "onInitEnter");
|
||||
state_ = ON_INIT_ENTER_WAIT;
|
||||
case ON_STARTUP_ENTER:
|
||||
luaEvent_->trigger(luaState_, "startuponEnter");
|
||||
state_ = ON_STARTUP_UPDATE;
|
||||
break;
|
||||
|
||||
case ON_INIT_ENTER_WAIT:
|
||||
if(luaEvent_->isComplete(luaState_, "onInitEnter")) {
|
||||
state_ = ON_INIT_EXIT;
|
||||
case ON_STARTUP_UPDATE:
|
||||
//todo: only do it if the main system is not busy
|
||||
luaEvent_->trigger(luaState_, "startuponUpdate");
|
||||
|
||||
if(!luaEvent_->isBusy(luaState_, "startupisBusy")) {
|
||||
state_ = ON_STARTUP_EXIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case ON_INIT_EXIT:
|
||||
luaEvent_->trigger(luaState_, "onInitExit");
|
||||
state_ = ON_INIT_EXIT_WAIT;
|
||||
case ON_STARTUP_EXIT:
|
||||
luaEvent_->trigger(luaState_, "startuponExit");
|
||||
state_ = ON_STARTUP_EXIT_WAIT;
|
||||
break;
|
||||
|
||||
case ON_STARTUP_EXIT_WAIT:
|
||||
if(!luaEvent_->isBusy(luaState_, "startupisBusy")) {
|
||||
state_ = ON_IDLE_ENTER;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,17 @@ private:
|
||||
LuaEvent *luaEvent_;
|
||||
|
||||
enum {
|
||||
ON_INIT_ENTER,
|
||||
ON_INIT_ENTER_WAIT,
|
||||
ON_INIT_EXIT,
|
||||
ON_INIT_EXIT_WAIT,
|
||||
ON_STARTUP_ENTER,
|
||||
ON_STARTUP_UPDATE,
|
||||
ON_STARTUP_EXIT,
|
||||
ON_STARTUP_EXIT_WAIT,
|
||||
|
||||
ON_COLLECTION_ENTER,
|
||||
ON_COLLECTION_UPDATE,
|
||||
ON_COLLECTION_EXIT,
|
||||
ON_COLLECTION_EXIT_WAIT,
|
||||
|
||||
ON_STARTUP_EXIT_DONE,
|
||||
ON_IDLE_ENTER
|
||||
} state_;
|
||||
};
|
||||
Reference in New Issue
Block a user