Skip to content

Commit

Permalink
add json as a dep, Scritpz to pack an unpack zipped files (#155)
Browse files Browse the repository at this point in the history
add json as a dep, Scritpz to pack and unpack zipped lua files

Console now uses scriptz to load a file and load each by a custom require using 
scripts now in extraspace

https://stackoverflow.com/questions/18965489/lua-require-but-files-are-only-in-memory

adds test for using packed scripts, and requiring others within them
  • Loading branch information
Jerboa-app authored Jan 24, 2024
1 parent 8bd06da commit 0ebb478
Show file tree
Hide file tree
Showing 26 changed files with 25,379 additions and 344 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-and-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- name: linux-windows
run: |
./build.sh -t -r -d -w
cp build/*.dll build/TestScriptPack/
- name: Tests
working-directory: ${{github.workspace}}/build
Expand Down
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ endif()

target_link_libraries(Hop PUBLIC jGL ${GLEW_LIBRARIES} zlibstatic stduuid Lua Miniaudio)

# add_library(FreeType ALIAS freetype)

if (NOT ANDROID)
add_subdirectory(tools/)
endif ()
Expand Down Expand Up @@ -170,6 +168,7 @@ IF (TEST_SUITE)
add_executable(testSuite
${TEST_SRC}
"src/World/mapFile.cpp"
"src/Util/z.cpp"
)

target_compile_definitions(testSuite PUBLIC GLSL_VERSION="330")
Expand All @@ -180,6 +179,8 @@ IF (TEST_SUITE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/tests/cmake/)
include(Catch)
catch_discover_tests(testSuite)

add_subdirectory("${PROJECT_SOURCE_DIR}/tests/regression")
endif()

ENDIF(TEST_SUITE)
Expand Down
3 changes: 0 additions & 3 deletions demo/desktop/perlinWorld/standalone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ int main(int argc, char ** argv)
float posX = 0.0;
float posY = 0.0;

Hop::World::Boundary * bounds;
Hop::World::MapSource * source;

Hop::World::FiniteBoundary mapBounds(0,0,16,16);
Hop::World::FixedSource mapSource;
mapSource.load("tile",false);
Expand Down
10 changes: 0 additions & 10 deletions demo/desktop/softBodyTetris/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,4 @@ hop.configure(config);
-- some code for mesh generation
require("meshes")

routines =
{
{
["file"] = "loop.lua",
["every"] = 1
}
}

hop.setLoopRoutines(routines);

math.randomseed(os.time())
16 changes: 1 addition & 15 deletions demo/desktop/softBodyTetris/standalone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ int main(int argc, char ** argv)
float posX = 0.0;
float posY = 0.0;

Hop::World::Boundary * bounds;
Hop::World::MapSource * source;

Hop::World::FiniteBoundary mapBounds(0,0,16,16);
Hop::World::FixedSource mapSource;
mapSource.load("bordered",false);
Expand Down Expand Up @@ -69,15 +66,12 @@ int main(int argc, char ** argv)
collisions.setDetector(std::move(det));
collisions.setResolver(std::move(res));

Hop::LoopRoutines loop;

Hop::LuaExtraSpace luaStore;

luaStore.ecs = &manager;
luaStore.world = world.get();
luaStore.physics = &physics;
luaStore.resolver = &collisions;
luaStore.loopRoutines = &loop;

console.luaStore(&luaStore);
console.runFile("config.lua");
Expand All @@ -86,22 +80,14 @@ int main(int argc, char ** argv)

high_resolution_clock::time_point t0, t1, tp0, tp1, tr0, tr1;

Hop::System::Sound::sSound & sound = manager.getSystem<Hop::System::Sound::sSound>();

while (display.isOpen())
{

if (display.getEvent(GLFW_KEY_SPACE).type == jGL::EventType::PRESS) { paused = !paused; }

if (!paused)
{
for (const auto & routine : loop.routines)
{
if (frameId % routine.every == 0)
{
console.runFile(routine.filename);
}
}
console.runFile("loop.lua");
}

jGLInstance->beginFrame();
Expand Down
140 changes: 69 additions & 71 deletions include/Console/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <System/Physics/sCollision.h>
#include <jLog/jLog.h>
#include <Object/id.h>
#include <Console/scriptz.h>

#include <memory>
#include <vector>
Expand All @@ -30,43 +31,16 @@ namespace Hop
using jLog::Log;
using jLog::ERRORCODE;

struct Routine
{
std::string filename;
uint16_t every;

bool read(lua_State * lua, const char * name);
bool read(lua_State * lua, int index);
};

struct LoopRoutines
{
std::vector<Routine> routines;

int lua_setRoutines(lua_State * lua);
};

struct LuaExtraSpace
{
EntityComponentSystem * ecs;
AbstractWorld * world;
sPhysics * physics;
sCollision * resolver;
LoopRoutines * loopRoutines;
Console * console;
Scriptz * scripts;
};

// LoopRoutines

typedef int (LoopRoutines::*LoopRoutinesMember)(lua_State * lua);

template <LoopRoutinesMember function>
int dispatchLoopRoutines(lua_State * lua)
{
LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
LoopRoutines * ptr = store->loopRoutines;
return ((*ptr).*function)(lua);
}

// ECS

typedef int (EntityComponentSystem::*EntityComponentSystemMember)(lua_State * lua);
Expand Down Expand Up @@ -115,42 +89,24 @@ namespace Hop
return ((*ptr).*function)(lua);
}

int configure(lua_State * lua);
// Scriptz

typedef int (Scriptz::*ScriptzMember)(lua_State * lua);

int timeMillis(lua_State * lua);
template <ScriptzMember function>
int dispatchScriptz(lua_State * lua)
{
LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
Scriptz * ptr = store->scripts;
return ((*ptr).*function)(lua);
}

int lua_applyForce(lua_State * lua);

// register lib
int configure(lua_State * lua);

const luaL_Reg hopLib[] =
{
{"loadObject", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_loadObject>},
{"getTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_getTransform>},
{"setTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_setTransform>},
///////////////////////////////////////////////////////////////////////////////////////////
{"maxCollisionPrimitiveSize",&dispatchWorld<&AbstractWorld::lua_worldMaxCollisionPrimitiveSize>},
///////////////////////////////////////////////////////////////////////////////////////////
{"setPhysicsTimeStep",&dispatchsPhysics<&sPhysics::lua_setTimeStep>},
{"setPhysicsSubSamples",&dispatchsPhysics<&sPhysics::lua_setSubSamples>},
{"kineticEnergy", &dispatchsPhysics<&sPhysics::lua_kineticEnergy>},
{"setGravity", &dispatchsPhysics<&sPhysics::lua_setGravity>},
///////////////////////////////////////////////////////////////////
{"setCoefRestitution",&dispatchsCollision<&sCollision::lua_setCOR>},
{"setSurfaceFriction",&dispatchsCollision<&sCollision::lua_setFriction>},
////////////////////////////////////////////////////////////////////
{"setLoopRoutines",&dispatchLoopRoutines<&LoopRoutines::lua_setRoutines>},
{"configure", &configure},
{"timeMillis", &timeMillis},
{"applyForce", &lua_applyForce},
{NULL, NULL}
};
int timeMillis(lua_State * lua);

static int load_hopLib(lua_State * lua)
{
luaL_newlib(lua,hopLib);
return 1;
}
int lua_applyForce(lua_State * lua);

class Console
{
Expand Down Expand Up @@ -248,19 +204,29 @@ namespace Hop
*static_cast<LuaExtraSpace**>(lua_getextraspace(lua)) = ptr;
}

const std::vector<Routine> & getLoopRoutines() const
{
LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
LoopRoutines * loop = store->loopRoutines;
return loop->routines;
}

void setRoutines(std::vector<Routine> r)
void runScript(std::string name)
{

LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
LoopRoutines * loop = store->loopRoutines;
loop->routines = r;
return;
Scriptz * scripts = store->scripts;

std::string body = scripts->get(name);

if (body == "")
{
lua_pushliteral(lua, "script not found");
lua_error(lua);
}

lastCommandOrProgram = name;
lastStatus =
(
luaL_loadstring(lua, body.c_str()) ||
lua_pcall(lua, 0, LUA_MULTRET, 0)
);

handleErrors();

}

private:
Expand All @@ -284,6 +250,38 @@ namespace Hop
lua_pop(lua, 1);
return 0;
}

// register lib

static int load_hopLib(lua_State * lua)
{
luaL_Reg hopLib[15] =
{
{"loadObject", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_loadObject>},
{"getTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_getTransform>},
{"setTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_setTransform>},
///////////////////////////////////////////////////////////////////////////////////////////
{"maxCollisionPrimitiveSize",&dispatchWorld<&AbstractWorld::lua_worldMaxCollisionPrimitiveSize>},
///////////////////////////////////////////////////////////////////////////////////////////
{"setPhysicsTimeStep",&dispatchsPhysics<&sPhysics::lua_setTimeStep>},
{"setPhysicsSubSamples",&dispatchsPhysics<&sPhysics::lua_setSubSamples>},
{"kineticEnergy", &dispatchsPhysics<&sPhysics::lua_kineticEnergy>},
{"setGravity", &dispatchsPhysics<&sPhysics::lua_setGravity>},
///////////////////////////////////////////////////////////////////
{"setCoefRestitution",&dispatchsCollision<&sCollision::lua_setCOR>},
{"setSurfaceFriction",&dispatchsCollision<&sCollision::lua_setFriction>},
////////////////////////////////////////////////////////////////////
{"configure", &configure},
{"timeMillis", &timeMillis},
{"applyForce", &lua_applyForce},
////////////////////////////////////////////////////////////////////
{"require", &dispatchScriptz<&Scriptz::require>},
{NULL, NULL}
};

luaL_newlib(lua,hopLib);
return 1;
}
};
}

Expand Down
Loading

0 comments on commit 0ebb478

Please sign in to comment.