Skip to content

Commit

Permalink
Merge pull request #162 from Onran0/main
Browse files Browse the repository at this point in the history
"file" library update
  • Loading branch information
MihailRis authored Feb 25, 2024
2 parents 97479d5 + 8acb074 commit fcc5d11
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
5 changes: 3 additions & 2 deletions res/content/base/blocks/glass.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"texture": "glass",
"draw-group": 2,
"light-passing": true
}
"light-passing": true,
"sky-light-passing": true
}
16 changes: 10 additions & 6 deletions src/lighting/LightSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ void LightSolver::solve(){
remqueue.pop();

for (int i = 0; i < 6; i++) {
int x = entry.x+coords[i*3+0];
int y = entry.y+coords[i*3+1];
int z = entry.z+coords[i*3+2];
int imul3 = i*3;
int x = entry.x+coords[imul3];
int y = entry.y+coords[imul3+1];
int z = entry.z+coords[imul3+2];

Chunk* chunk = chunks->getChunkByVoxel(x,y,z);
if (chunk) {
int lx = x - chunk->x * CHUNK_W;
Expand All @@ -85,9 +87,11 @@ void LightSolver::solve(){
addqueue.pop();

for (int i = 0; i < 6; i++) {
int x = entry.x+coords[i*3+0];
int y = entry.y+coords[i*3+1];
int z = entry.z+coords[i*3+2];
int imul3 = i*3;
int x = entry.x+coords[imul3];
int y = entry.y+coords[imul3+1];
int z = entry.z+coords[imul3+2];

Chunk* chunk = chunks->getChunkByVoxel(x,y,z);
if (chunk) {
int lx = x - chunk->x * CHUNK_W;
Expand Down
10 changes: 4 additions & 6 deletions src/lighting/Lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){

void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
Block* block = content->getIndices()->getBlockDef(id);
solverR->remove(x,y,z);
solverG->remove(x,y,z);
solverB->remove(x,y,z);

if (id == 0){
solverR->remove(x,y,z);
solverG->remove(x,y,z);
solverB->remove(x,y,z);
solverR->solve();
solverG->solve();
solverB->solve();
Expand All @@ -178,9 +179,6 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
solverB->solve();
solverS->solve();
} else {
solverR->remove(x,y,z);
solverG->remove(x,y,z);
solverB->remove(x,y,z);
if (!block->skyLightPassing){
solverS->remove(x,y,z);
for (int i = y-1; i >= 0; i--){
Expand Down
72 changes: 72 additions & 0 deletions src/logic/scripting/lua/api_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,78 @@ int l_file_mkdir(lua_State* L) {
return 1;
}

int l_file_mkdirs(lua_State* L) {
fs::path path = resolve_path(L, lua_tostring(L, 1));
lua_pushboolean(L, fs::create_directories(path));
return 1;
}

int l_file_read_bytes(lua_State* L) {
fs::path path = resolve_path(L, lua_tostring(L, 1));
if (fs::is_regular_file(path)) {
size_t length = (size_t) fs::file_size(path);

ubyte* bytes = files::read_bytes(path, length);

lua_createtable(L, length, 0);
int newTable = lua_gettop(L);

for(int i = 0;i < length;i++) {
lua_pushnumber(L, bytes[i]);
lua_rawseti(L, newTable, i+1);
}
return 1;
}
return luaL_error(L, "file does not exists '%s'", path.u8string().c_str());
}

int l_file_write_bytes(lua_State* L) {
int pathIndex = 1;

if(!lua_isstring(L, pathIndex)) {
return luaL_error(L, "string expected");
}

fs::path path = resolve_path(L, lua_tostring(L, pathIndex));

std::vector<ubyte> bytes;

size_t len = lua_objlen(L, 2);

int bytesIndex = -1;

if(!lua_istable(L, bytesIndex)) {
return luaL_error(L, "table expected");
} else {
lua_pushnil(L);

bytesIndex--;

int i = 1;

while(lua_next(L, bytesIndex) != 0) {
if(lua_isnumber(L, -1)) {
const int byte = lua_tointeger(L, -1);

if(byte < 0 || byte > 255) {
return luaL_error(L, "byte '%i' at index '%i' is less than 0 or greater than 255", byte, i);
}

bytes.push_back(byte);

lua_pop(L, 1);

i++;
} else {
return luaL_error(L, "number expected at index '%i'", i);
}
}

lua_pushboolean(L, files::write_bytes(path, &bytes[0], len));
return 1;
}
}

/* == time library == */
int l_time_uptime(lua_State* L) {
lua_pushnumber(L, Window::time());
Expand Down
6 changes: 6 additions & 0 deletions src/logic/scripting/lua/api_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ extern int l_file_isfile(lua_State* L);
extern int l_file_isdir(lua_State* L);
extern int l_file_length(lua_State* L);
extern int l_file_mkdir(lua_State* L);
extern int l_file_mkdirs(lua_State* L);
extern int l_file_read_bytes(lua_State* L);
extern int l_file_write_bytes(lua_State* L);

static const luaL_Reg filelib [] = {
{"resolve", lua_wrap_errors<l_file_resolve>},
Expand All @@ -23,6 +26,9 @@ static const luaL_Reg filelib [] = {
{"isdir", lua_wrap_errors<l_file_isdir>},
{"length", lua_wrap_errors<l_file_length>},
{"mkdir", lua_wrap_errors<l_file_mkdir>},
{"mkdirs", lua_wrap_errors<l_file_mkdirs>},
{"read_bytes", lua_wrap_errors<l_file_read_bytes>},
{"write_bytes", lua_wrap_errors<l_file_write_bytes>},
{NULL, NULL}
};

Expand Down

0 comments on commit fcc5d11

Please sign in to comment.