Skip to content

Commit

Permalink
writeFile, deleteFile
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowMario committed Jun 25, 2022
1 parent d6ad85b commit 0c733d2
Showing 1 changed file with 61 additions and 29 deletions.
90 changes: 61 additions & 29 deletions source/FunkinLua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class FunkinLua {
public var scriptName:String = '';
public var closed:Bool = false;

public var accessedProps:Map<String, Dynamic> = null;
public function new(script:String) {
#if LUA_ALLOWED
lua = LuaL.newstate();
Expand Down Expand Up @@ -87,12 +86,6 @@ class FunkinLua {
scriptName = script;
trace('lua file loaded succesfully:' + script);

#if (haxe >= "4.0.0")
accessedProps = new Map();
#else
accessedProps = new Map<String, Dynamic>();
#end

// Lua shit
set('Function_StopLua', Function_StopLua);
set('Function_Stop', Function_Stop);
Expand Down Expand Up @@ -1695,24 +1688,6 @@ class FunkinLua {
Lua_helper.add_callback(lua, "luaSoundExists", function(tag:String) {
return PlayState.instance.modchartSounds.exists(tag);
});
Lua_helper.add_callback(lua, "checkFileExists", function(filename:String, ?absolute:Bool = false) {
#if MODS_ALLOWED
if(absolute)
{
return FileSystem.exists(filename);
}

var path:String = Paths.modFolders(filename);
if(FileSystem.exists(path))
{
return true;
}
return FileSystem.exists('assets/$filename');
#else
luaTrace('Platform not suppoted for checkFileExists!', true, false, FlxColor.RED);
return false;
#end
});

Lua_helper.add_callback(lua, "setHealthBarColors", function(leftHex:String, rightHex:String) {
var left:FlxColor = Std.parseInt(leftHex);
Expand Down Expand Up @@ -2215,6 +2190,67 @@ class FunkinLua {
luaTrace('Save file not initialized: ' + name, false, false, FlxColor.RED);
});

Lua_helper.add_callback(lua, "checkFileExists", function(filename:String, ?absolute:Bool = false) {
#if MODS_ALLOWED
if(absolute)
{
return FileSystem.exists(filename);
}

var path:String = Paths.modFolders(filename);
if(FileSystem.exists(path))
{
return true;
}
return FileSystem.exists(Paths.getPath('assets/$filename', TEXT));
#else
if(absolute)
{
return Assets.exists(filename);
}
return Assets.exists(Paths.getPath('assets/$filename', TEXT));
#end
});
Lua_helper.add_callback(lua, "saveFile", function(path:String, content:String, ?absolute:Bool = false)
{
try {
if(!absolute)
File.saveContent(Paths.mods(path), content);
else
File.saveContent(path, content);

return true;
} catch (e:Dynamic) {
luaTrace("Error trying to save " + path + ": " + e, false, false, FlxColor.RED);
}
return false;
});
Lua_helper.add_callback(lua, "deleteFile", function(path:String, ?ignoreModFolders:Bool = false)
{
try {
#if MODS_ALLOWED
if(!ignoreModFolders)
{
var lePath:String = Paths.modFolders(path);
if(FileSystem.exists(lePath))
{
FileSystem.deleteFile(lePath);
return true;
}
}
#end

var lePath:String = Paths.getPath(path, TEXT);
if(Assets.exists(lePath))
{
FileSystem.deleteFile(lePath);
return true;
}
} catch (e:Dynamic) {
luaTrace("Error trying to delete " + path + ": " + e, false, false, FlxColor.RED);
}
return false;
});
Lua_helper.add_callback(lua, "getTextFromFile", function(path:String, ?ignoreModFolders:Bool = false) {
return Paths.getTextFromFile(path, ignoreModFolders);
});
Expand Down Expand Up @@ -2763,10 +2799,6 @@ class FunkinLua {
return;
}

if(accessedProps != null) {
accessedProps.clear();
}

Lua.close(lua);
lua = null;
#end
Expand Down

1 comment on commit 0c733d2

@DragShot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About time we got these! Although I'm not so sure about allowing absolute paths.

Please sign in to comment.