Skip to content

Commit

Permalink
lua API - LumixAPI.writeFile
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Jun 15, 2024
1 parent 4058c23 commit de84289
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
3 changes: 2 additions & 1 deletion data/editor/scripts/plugins/lua_type_defs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ declare LumixAPI: {
engine : any,
logError : (string) -> (),
logInfo : (string) -> (),
loadResource : (any, path:string, restype:string) -> any
loadResource : (any, path:string, restype:string) -> any,
writeFile : (string, string) -> boolean
}
declare class ComponentBase
Expand Down
47 changes: 24 additions & 23 deletions data/scripts/lumix.d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ declare class gui_canvas_component
virtual_size: any
end

declare class lua_script_component
scripts: any
getScriptPath : (lua_script_component, number) -> string
end

declare class particle_emitter_component
autodestroy: boolean
source: string
Expand Down Expand Up @@ -330,11 +335,6 @@ declare class physical_controller_component
getGravitySpeed : (physical_controller_component) -> number
end

declare class lua_script_component
scripts: any
getScriptPath : (lua_script_component, number) -> string
end

declare class gui_image_component
enabled: boolean
color: any
Expand Down Expand Up @@ -362,6 +362,20 @@ declare class animable_component
animation: string
end

declare class property_animator_component
animation: string
enabled: boolean
end

declare class animator_component
source: string
default_set: number
use_root_motion: boolean
setFloatInput : (animator_component, number, number) -> ()
setBoolInput : (animator_component, number, boolean) -> ()
getInputIndex : (animator_component, string) -> number
end

declare class distance_joint_component
connected_body: Entity
axis_position: Vec3
Expand Down Expand Up @@ -469,20 +483,6 @@ end
declare class gui_input_field_component
end

declare class property_animator_component
animation: string
enabled: boolean
end

declare class animator_component
source: string
default_set: number
use_root_motion: boolean
setFloatInput : (animator_component, number, number) -> ()
setBoolInput : (animator_component, number, boolean) -> ()
getInputIndex : (animator_component, string) -> number
end

declare class physical_instanced_cube_component
half_extents: Vec3
layer: number
Expand Down Expand Up @@ -528,6 +528,7 @@ declare class Entity
spline: spline_component
gui_rect: gui_rect_component
gui_canvas: gui_canvas_component
lua_script: lua_script_component
particle_emitter: particle_emitter_component
terrain: terrain_component
camera: camera_component
Expand All @@ -545,12 +546,13 @@ declare class Entity
rigid_actor: rigid_actor_component
physical_heightfield: physical_heightfield_component
physical_controller: physical_controller_component
lua_script: lua_script_component
gui_image: gui_image_component
gui_text: gui_text_component
gui_button: gui_button_component
gui_render_target: gui_render_target_component
animable: animable_component
property_animator: property_animator_component
animator: animator_component
distance_joint: distance_joint_component
hinge_joint: hinge_joint_component
spherical_joint: spherical_joint_component
Expand All @@ -561,8 +563,6 @@ declare class Entity
navmesh_zone: navmesh_zone_component
lua_script_inline: lua_script_inline_component
gui_input_field: gui_input_field_component
property_animator: property_animator_component
animator: animator_component
physical_instanced_cube: physical_instanced_cube_component
physical_instanced_mesh: physical_instanced_mesh_component
audio_listener: audio_listener_component
Expand Down Expand Up @@ -607,7 +607,8 @@ declare LumixAPI: {
engine : any,
logError : (string) -> (),
logInfo : (string) -> (),
loadResource : (any, path:string, restype:string) -> any
loadResource : (any, path:string, restype:string) -> any,
writeFile : (string, string) -> boolean
}

declare class ComponentBase
Expand Down
22 changes: 22 additions & 0 deletions src/engine/lua_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,27 @@ void registerCFunction(lua_State* L, const char* name, lua_CFunction f)
} // namespace LuaImGui


static int LUA_writeFile(lua_State* L) {
Engine* engine = LuaWrapper::getClosureObject<Engine>(L);
const char* path = LuaWrapper::checkArg<const char*>(L, 1);
if (!LuaWrapper::isType<const char*>(L, 2)) {
LuaWrapper::argError<const char*>(L, 2);
}
size_t len;
const char* content = lua_tolstring(L, 2, &len);
FileSystem& fs = engine->getFileSystem();
os::OutputFile file;
if (!fs.open(path, file)) {
lua_pushboolean(L, false);
return 1;
}

bool res = file.write(content, len);
file.close();
lua_pushboolean(L, res);
return 1;
}

static int LUA_pause(lua_State* L) {
bool pause = LuaWrapper::checkArg<bool>(L, 1);
Engine* engine = LuaWrapper::getClosureObject<Engine>(L);
Expand Down Expand Up @@ -949,6 +970,7 @@ void registerEngineAPI(lua_State* L, Engine* engine)
LuaWrapper::createSystemClosure(L, "LumixAPI", engine, "hasFilesystemWork", LUA_hasFilesystemWork);
LuaWrapper::createSystemClosure(L, "LumixAPI", engine, "processFilesystemWork", LUA_processFilesystemWork);
LuaWrapper::createSystemClosure(L, "LumixAPI", engine, "pause", LUA_pause);
LuaWrapper::createSystemClosure(L, "LumixAPI", engine, "writeFile", LUA_writeFile);

#undef REGISTER_FUNCTION

Expand Down

0 comments on commit de84289

Please sign in to comment.