Files management functions.

This commit is contained in:
jussi
2023-12-06 18:41:01 +02:00
parent 4a783a156e
commit 6ecbbcc282
11 changed files with 260 additions and 113 deletions

54
API.md
View File

@@ -4220,6 +4220,50 @@ Set Lua garbage collection to unload object data
---
> buffer = RL.LoadFileData( string fileName )
Load file data as byte array (read). Buffer type is BUFFER_UNSIGNED_CHAR
- Success return Buffer
---
> success = RL.SaveFileData( string fileName, buffer Buffer )
Save data to file from byte array (write), returns true on success
- Success return bool
---
> success = RL.ExportDataAsCode( Buffer buffer, string fileName )
Export data to code (.h), returns true on success
- Success return bool
---
> text = RL.LoadFileText( string fileName )
Load text data from file (read)
- Success return string
---
> success = RL.SaveFileText( string fileName, string text )
Save text data to file (write), returns true on success
- Success return bool
---
## Core - Files system functions
---
> path = RL.GetBasePath()
Return game directory (where main.lua is located)
@@ -4995,7 +5039,7 @@ Unload buffer data
> data = RL.GetBufferData( Buffer buffer )
Get buffer data as table in the format is was stored
Get buffer data as table in the format it was stored
- Success return data{}
@@ -5023,14 +5067,6 @@ Write buffer data to binary file
---
> success = RL.ExportBufferAsCode( Buffer buffer, string fileName )
Export buffer data to code (.h), returns true on success
- Success return bool
---
## Shapes - Basic shapes drawing functions
---

View File

@@ -1774,6 +1774,41 @@ function RL.SetGCUnload( enabled ) end
-- Core - Files management functions
---Load file data as byte array (read). Buffer type is BUFFER_UNSIGNED_CHAR
---- Success return Buffer
---@param fileName string
---@return any buffer
function RL.LoadFileData( fileName ) end
---Save data to file from byte array (write), returns true on success
---- Success return bool
---@param fileName string
---@param Buffer any
---@return any success
function RL.SaveFileData( fileName, Buffer ) end
---Export data to code (.h), returns true on success
---- Success return bool
---@param buffer any
---@param fileName string
---@return any success
function RL.ExportDataAsCode( buffer, fileName ) end
---Load text data from file (read)
---- Success return string
---@param fileName string
---@return any text
function RL.LoadFileText( fileName ) end
---Save text data to file (write), returns true on success
---- Success return bool
---@param fileName string
---@param text string
---@return any success
function RL.SaveFileText( fileName, text ) end
-- Core - Files system functions
---Return game directory (where main.lua is located)
---- Success return string
---@return any path
@@ -2379,7 +2414,7 @@ function RL.LoadBufferFromFile( path, int ) end
---@return any RL.UnloadBuffer
function RL.UnloadBuffer( buffer ) end
---Get buffer data as table in the format is was stored
---Get buffer data as table in the format it was stored
---- Success return data{}
---@param buffer any
---@return any data
@@ -2403,13 +2438,6 @@ function RL.GetBufferSize( buffer ) end
---@return any RL.ExportBuffer
function RL.ExportBuffer( buffer, path ) end
---Export buffer data to code (.h), returns true on success
---- Success return bool
---@param buffer any
---@param fileName string
---@return any success
function RL.ExportBufferAsCode( buffer, fileName ) end
-- Shapes - Basic shapes drawing functions
---Set texture and rectangle to be used on shapes drawing

View File

@@ -19,6 +19,7 @@ KEY CHANGES:
- ADDED: Raygui textures with SetShapesTexture.
- CHANGE: Raygui wrapper library is now object based. Possible to have multiple separated gui systems.
- ADDED: Raygui wrapper library disabled and styles for each element.
- ADDED: Files management functions.
DETAILED CHANGES:
- REMOVED: DrawLineBezierQuad, DrawLineBezierCubic.
@@ -36,6 +37,8 @@ DETAILED CHANGES:
- CHANGE: Event documentation is now described in c files.
- ADDED: ExportBufferAsCode.
- ADDED: GetTextureDefault.
- CHANGE: Renamed Files management functions to Files system functions as in raylib.
- CHANGE: Renamed ExportBufferAsCode to ExportDataAsCode.
------------------------------------------------------------------------
Release: ReiLua version 0.6.0 Using Raylib 4.5

View File

@@ -25,6 +25,4 @@ end
function RL.draw()
RL.ClearBackground( RL.RAYWHITE )
RL.DrawText( text, textPos, 20, textColor )
RL.DrawCircle( { 300, 300 }, 180, RL.BLACK )
end

View File

@@ -108,6 +108,12 @@ int lcoreOpenURL( lua_State *L );
int lcoreIsGCUnloadEnabled( lua_State *L );
int lcoreSetGCUnload( lua_State *L );
/* Files management functions. */
int lcoreLoadFileData( lua_State *L );
int lcoreSaveFileData( lua_State *L );
int lcoreExportDataAsCode( lua_State *L );
int lcoreLoadFileText( lua_State *L );
int lcoreSaveFileText( lua_State *L );
/* Files system functions. */
int lcoreGetBasePath( lua_State *L );
int lcoreFileExists( lua_State *L );
int lcoreDirectoryExists( lua_State *L );
@@ -220,4 +226,3 @@ int lcoreGetBufferData( lua_State *L );
int lcoreGetBufferType( lua_State *L );
int lcoreGetBufferSize( lua_State *L );
int lcoreExportBuffer( lua_State *L );
int lcoreExportBufferAsCode( lua_State *L );

View File

@@ -1418,6 +1418,95 @@ int lcoreSetGCUnload( lua_State *L ) {
## Core - Files management functions
*/
/*
> buffer = RL.LoadFileData( string fileName )
Load file data as byte array (read). Buffer type is BUFFER_UNSIGNED_CHAR
- Success return Buffer
*/
int lcoreLoadFileData( lua_State *L ) {
const char *fileName = luaL_checkstring( L, 1 );
Buffer buffer = {
.type = BUFFER_UNSIGNED_CHAR
};
buffer.data = LoadFileData( fileName, (int*)&buffer.size );
uluaPushBuffer( L, buffer );
return 1;
}
/*
> success = RL.SaveFileData( string fileName, buffer Buffer )
Save data to file from byte array (write), returns true on success
- Success return bool
*/
int lcoreSaveFileData( lua_State *L ) {
const char *fileName = luaL_checkstring( L, 1 );
Buffer *buffer = uluaGetBuffer( L, 2 );
lua_pushboolean( L, SaveFileData( fileName, buffer->data, buffer->size ) );
return 1;
}
/*
> success = RL.ExportDataAsCode( Buffer buffer, string fileName )
Export data to code (.h), returns true on success
- Success return bool
*/
int lcoreExportDataAsCode( lua_State *L ) {
Buffer *buffer = uluaGetBuffer( L, 1 );
const char *fileName = luaL_checkstring( L, 2 );
lua_pushboolean( L, ExportDataAsCode( buffer->data, buffer->size, fileName ) );
return 1;
}
/*
> text = RL.LoadFileText( string fileName )
Load text data from file (read)
- Success return string
*/
int lcoreLoadFileText( lua_State *L ) {
const char *fileName = luaL_checkstring( L, 1 );
char *text = LoadFileText( fileName );
lua_pushstring( L, text );
UnloadFileText( text );
return 1;
}
/*
> success = RL.SaveFileText( string fileName, string text )
Save text data to file (write), returns true on success
- Success return bool
*/
int lcoreSaveFileText( lua_State *L ) {
const char *fileName = luaL_checkstring( L, 1 );
char *text = (char*)luaL_checkstring( L, 2 );
lua_pushboolean( L, SaveFileText( fileName, text ) );
return 1;
}
/*
## Core - Files system functions
*/
/*
> path = RL.GetBasePath()
@@ -2951,8 +3040,8 @@ Read buffer data from binary file
- Success return Buffer
*/
int lcoreLoadBufferFromFile( lua_State *L ) {
int type = luaL_checkinteger( L, 2 );
const char *path = luaL_checkstring( L, 1 );
int type = luaL_checkinteger( L, 2 );
int fileLen = GetFileLength( path );
Buffer buffer = {
@@ -2993,7 +3082,7 @@ int lcoreUnloadBuffer( lua_State *L ) {
/*
> data = RL.GetBufferData( Buffer buffer )
Get buffer data as table in the format is was stored
Get buffer data as table in the format it was stored
- Success return data{}
*/
@@ -3140,19 +3229,3 @@ int lcoreExportBuffer( lua_State *L ) {
return 0;
}
/*
> success = RL.ExportBufferAsCode( Buffer buffer, string fileName )
Export buffer data to code (.h), returns true on success
- Success return bool
*/
int lcoreExportBufferAsCode( lua_State *L ) {
Buffer *buffer = uluaGetBuffer( L, 1 );
const char *fileName = luaL_checkstring( L, 2 );
lua_pushboolean( L, ExportDataAsCode( buffer->data, buffer->size, fileName ) );
return 1;
}

View File

@@ -1264,6 +1264,12 @@ void luaRegister() {
assingGlobalFunction( "IsGCUnloadEnabled", lcoreIsGCUnloadEnabled );
assingGlobalFunction( "SetGCUnload", lcoreSetGCUnload );
/* Files management functions. */
assingGlobalFunction( "LoadFileData", lcoreLoadFileData );
assingGlobalFunction( "SaveFileData", lcoreSaveFileData );
assingGlobalFunction( "ExportDataAsCode", lcoreExportDataAsCode );
assingGlobalFunction( "LoadFileText", lcoreLoadFileText );
assingGlobalFunction( "SaveFileText", lcoreSaveFileText );
/* Files system functions. */
assingGlobalFunction( "GetBasePath", lcoreGetBasePath );
assingGlobalFunction( "FileExists", lcoreFileExists );
assingGlobalFunction( "DirectoryExists", lcoreDirectoryExists );
@@ -1374,7 +1380,6 @@ void luaRegister() {
assingGlobalFunction( "GetBufferType", lcoreGetBufferType );
assingGlobalFunction( "GetBufferSize", lcoreGetBufferSize );
assingGlobalFunction( "ExportBuffer", lcoreExportBuffer );
assingGlobalFunction( "ExportBufferAsCode", lcoreExportBufferAsCode );
/* Shapes. */
/* Basic shapes drawing functions. */

View File

@@ -519,41 +519,41 @@ static void joystickEvent( int jid, int event ) {
Called when the pen tablet data is updated. Type GLFW_PEN_TABLET_DATA_EVENT
NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445
*/
// static void penTabletDataEvent( double x, double y, double z, double pressure, double pitch, double yaw, double roll ) {
// lua_State *L = state->luaState;
static void penTabletDataEvent( double x, double y, double z, double pressure, double pitch, double yaw, double roll ) {
lua_State *L = state->luaState;
// lua_pushcfunction( L, luaTraceback );
// int tracebackidx = lua_gettop( L );
lua_pushcfunction( L, luaTraceback );
int tracebackidx = lua_gettop( L );
// lua_getglobal( L, "RL" );
// lua_getfield( L, -1, "event" );
lua_getglobal( L, "RL" );
lua_getfield( L, -1, "event" );
// if ( lua_isfunction( L, -1 ) ) {
// lua_createtable( L, 8, 0 );
// lua_pushinteger( L, GLFW_PEN_TABLET_DATA_EVENT );
// lua_setfield( L, -2, "type" );
// lua_pushnumber( L, x );
// lua_setfield( L, -2, "x" );
// lua_pushnumber( L, y );
// lua_setfield( L, -2, "y" );
// lua_pushnumber( L, z );
// lua_setfield( L, -2, "z" );
// lua_pushnumber( L, pressure );
// lua_setfield( L, -2, "pressure" );
// lua_pushnumber( L, pitch );
// lua_setfield( L, -2, "pitch" );
// lua_pushnumber( L, yaw );
// lua_setfield( L, -2, "yaw" );
// lua_pushnumber( L, roll );
// lua_setfield( L, -2, "roll" );
if ( lua_isfunction( L, -1 ) ) {
lua_createtable( L, 8, 0 );
lua_pushinteger( L, GLFW_PEN_TABLET_DATA_EVENT );
lua_setfield( L, -2, "type" );
lua_pushnumber( L, x );
lua_setfield( L, -2, "x" );
lua_pushnumber( L, y );
lua_setfield( L, -2, "y" );
lua_pushnumber( L, z );
lua_setfield( L, -2, "z" );
lua_pushnumber( L, pressure );
lua_setfield( L, -2, "pressure" );
lua_pushnumber( L, pitch );
lua_setfield( L, -2, "pitch" );
lua_pushnumber( L, yaw );
lua_setfield( L, -2, "yaw" );
lua_pushnumber( L, roll );
lua_setfield( L, -2, "roll" );
// if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
// TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
// state->run = false;
// }
// }
// lua_pop( L, -1 );
// }
if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
state->run = false;
}
}
lua_pop( L, -1 );
}
/*
> GLFWpentabletcursorEvent = { int type, int identifier }
@@ -561,29 +561,29 @@ NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445
Called when the pen tablet cursor has changed. Type GLFW_PEN_TABLET_CURSOR_EVENT
NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445
*/
// static void penTabletCursorEvent( unsigned int identifier ) {
// lua_State *L = state->luaState;
static void penTabletCursorEvent( unsigned int identifier ) {
lua_State *L = state->luaState;
// lua_pushcfunction( L, luaTraceback );
// int tracebackidx = lua_gettop( L );
lua_pushcfunction( L, luaTraceback );
int tracebackidx = lua_gettop( L );
// lua_getglobal( L, "RL" );
// lua_getfield( L, -1, "event" );
lua_getglobal( L, "RL" );
lua_getfield( L, -1, "event" );
// if ( lua_isfunction( L, -1 ) ) {
// lua_createtable( L, 2, 0 );
// lua_pushinteger( L, GLFW_PEN_TABLET_CURSOR_EVENT );
// lua_setfield( L, -2, "type" );
// lua_pushinteger( L, identifier );
// lua_setfield( L, -2, "identifier" );
if ( lua_isfunction( L, -1 ) ) {
lua_createtable( L, 2, 0 );
lua_pushinteger( L, GLFW_PEN_TABLET_CURSOR_EVENT );
lua_setfield( L, -2, "type" );
lua_pushinteger( L, identifier );
lua_setfield( L, -2, "identifier" );
// if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
// TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
// state->run = false;
// }
// }
// lua_pop( L, -1 );
// }
if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
state->run = false;
}
}
lua_pop( L, -1 );
}
/*
> GLFWpentabletproximityEvent = { int type, int proxState }
@@ -591,29 +591,29 @@ NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445
Called when the pen tablet proximity has changed. Type GLFW_PEN_TABLET_PROXIMITY_EVENT
NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445
*/
// static void penTabletProximityEvent( int proxState ) {
// lua_State *L = state->luaState;
static void penTabletProximityEvent( int proxState ) {
lua_State *L = state->luaState;
// lua_pushcfunction( L, luaTraceback );
// int tracebackidx = lua_gettop( L );
lua_pushcfunction( L, luaTraceback );
int tracebackidx = lua_gettop( L );
// lua_getglobal( L, "RL" );
// lua_getfield( L, -1, "event" );
lua_getglobal( L, "RL" );
lua_getfield( L, -1, "event" );
// if ( lua_isfunction( L, -1 ) ) {
// lua_createtable( L, 2, 0 );
// lua_pushinteger( L, GLFW_PEN_TABLET_PROXIMITY_EVENT );
// lua_setfield( L, -2, "type" );
// lua_pushinteger( L, proxState );
// lua_setfield( L, -2, "state" );
if ( lua_isfunction( L, -1 ) ) {
lua_createtable( L, 2, 0 );
lua_pushinteger( L, GLFW_PEN_TABLET_PROXIMITY_EVENT );
lua_setfield( L, -2, "type" );
lua_pushinteger( L, proxState );
lua_setfield( L, -2, "state" );
// if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
// TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
// state->run = false;
// }
// }
// lua_pop( L, -1 );
// }
if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
state->run = false;
}
}
lua_pop( L, -1 );
}
static void platformRegisterEvents() {
/* Window events. */

View File

@@ -16,5 +16,4 @@ void platformDefineGlobals() {
/* Events. */
void luaPlatformRegister() {
return;
}