Tobee_Night_Funkin_PSYCH/source/editors/EditorLua.hx

267 lines
7.4 KiB
Haxe
Raw Permalink Normal View History

2021-11-05 21:34:27 +00:00
package editors;
#if LUA_ALLOWED
import llua.Lua;
import llua.LuaL;
import llua.State;
import llua.Convert;
#end
import flixel.FlxG;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxEase;
import flixel.text.FlxText;
import flixel.group.FlxGroup.FlxTypedGroup;
import flixel.math.FlxPoint;
import flixel.system.FlxSound;
import flixel.util.FlxTimer;
import flixel.FlxSprite;
import flixel.FlxCamera;
import flixel.util.FlxColor;
import flixel.FlxBasic;
#if sys
import sys.FileSystem;
import sys.io.File;
#end
import Type.ValueType;
import Controls;
import DialogueBoxPsych;
using StringTools;
class EditorLua {
public static var Function_Stop = 1;
public static var Function_Continue = 0;
#if LUA_ALLOWED
public var lua:State = null;
#end
var lePlayState:editors.EditorPlayState = null;
public function new(script:String) {
#if LUA_ALLOWED
lua = LuaL.newstate();
LuaL.openlibs(lua);
Lua.init_callbacks(lua);
//trace('Lua version: ' + Lua.version());
//trace("LuaJIT version: " + Lua.versionJIT());
var result:Dynamic = LuaL.dofile(lua, script);
var resultStr:String = Lua.tostring(lua, result);
if(resultStr != null && result != 0) {
lime.app.Application.current.window.alert(resultStr, 'Error on .LUA script!');
trace('Error on .LUA script! ' + resultStr);
lua = null;
return;
}
trace('Lua file loaded succesfully:' + script);
var curState:Dynamic = FlxG.state;
lePlayState = curState;
// Lua variables
set('Function_Stop', Function_Stop);
set('Function_Continue', Function_Continue);
set('inChartEditor', true);
set('curBpm', Conductor.bpm);
set('bpm', PlayState.SONG.bpm);
set('scrollSpeed', PlayState.SONG.speed);
set('crochet', Conductor.crochet);
set('stepCrochet', Conductor.stepCrochet);
set('songLength', FlxG.sound.music.length);
set('songName', PlayState.SONG.song);
set('screenWidth', FlxG.width);
set('screenHeight', FlxG.height);
for (i in 0...4) {
set('defaultPlayerStrumX' + i, 0);
set('defaultPlayerStrumY' + i, 0);
set('defaultOpponentStrumX' + i, 0);
set('defaultOpponentStrumY' + i, 0);
}
set('downscroll', ClientPrefs.downScroll);
set('middlescroll', ClientPrefs.middleScroll);
//stuff 4 noobz like you B)
Lua_helper.add_callback(lua, "getProperty", function(variable:String) {
var killMe:Array<String> = variable.split('.');
if(killMe.length > 1) {
var coverMeInPiss:Dynamic = Reflect.getProperty(lePlayState, killMe[0]);
for (i in 1...killMe.length-1) {
coverMeInPiss = Reflect.getProperty(coverMeInPiss, killMe[i]);
}
return Reflect.getProperty(coverMeInPiss, killMe[killMe.length-1]);
}
return Reflect.getProperty(lePlayState, variable);
});
Lua_helper.add_callback(lua, "setProperty", function(variable:String, value:Dynamic) {
var killMe:Array<String> = variable.split('.');
if(killMe.length > 1) {
var coverMeInPiss:Dynamic = Reflect.getProperty(lePlayState, killMe[0]);
for (i in 1...killMe.length-1) {
coverMeInPiss = Reflect.getProperty(coverMeInPiss, killMe[i]);
}
return Reflect.setProperty(coverMeInPiss, killMe[killMe.length-1], value);
}
return Reflect.setProperty(lePlayState, variable, value);
});
Lua_helper.add_callback(lua, "getPropertyFromGroup", function(obj:String, index:Int, variable:Dynamic) {
if(Std.isOfType(Reflect.getProperty(lePlayState, obj), FlxTypedGroup)) {
return Reflect.getProperty(Reflect.getProperty(lePlayState, obj).members[index], variable);
}
var leArray:Dynamic = Reflect.getProperty(lePlayState, obj)[index];
if(leArray != null) {
if(Type.typeof(variable) == ValueType.TInt) {
return leArray[variable];
}
return Reflect.getProperty(leArray, variable);
}
return null;
});
Lua_helper.add_callback(lua, "setPropertyFromGroup", function(obj:String, index:Int, variable:Dynamic, value:Dynamic) {
if(Std.isOfType(Reflect.getProperty(lePlayState, obj), FlxTypedGroup)) {
return Reflect.setProperty(Reflect.getProperty(lePlayState, obj).members[index], variable, value);
}
var leArray:Dynamic = Reflect.getProperty(lePlayState, obj)[index];
if(leArray != null) {
if(Type.typeof(variable) == ValueType.TInt) {
return leArray[variable] = value;
}
return Reflect.setProperty(leArray, variable, value);
}
});
Lua_helper.add_callback(lua, "removeFromGroup", function(obj:String, index:Int, dontDestroy:Bool = false) {
if(Std.isOfType(Reflect.getProperty(lePlayState, obj), FlxTypedGroup)) {
var sex = Reflect.getProperty(lePlayState, obj).members[index];
if(!dontDestroy)
sex.kill();
Reflect.getProperty(lePlayState, obj).remove(sex, true);
if(!dontDestroy)
sex.destroy();
return;
}
Reflect.getProperty(lePlayState, obj).remove(Reflect.getProperty(lePlayState, obj)[index]);
});
Lua_helper.add_callback(lua, "getColorFromHex", function(color:String) {
if(!color.startsWith('0x')) color = '0xff' + color;
return Std.parseInt(color);
});
Lua_helper.add_callback(lua, "setGraphicSize", function(obj:String, x:Int, y:Int = 0) {
var poop:FlxSprite = Reflect.getProperty(lePlayState, obj);
if(poop != null) {
poop.setGraphicSize(x, y);
poop.updateHitbox();
return;
}
});
Lua_helper.add_callback(lua, "scaleObject", function(obj:String, x:Float, y:Float) {
var poop:FlxSprite = Reflect.getProperty(lePlayState, obj);
if(poop != null) {
poop.scale.set(x, y);
poop.updateHitbox();
return;
}
});
Lua_helper.add_callback(lua, "updateHitbox", function(obj:String) {
var poop:FlxSprite = Reflect.getProperty(lePlayState, obj);
if(poop != null) {
poop.updateHitbox();
return;
}
});
call('onCreate', []);
#end
}
public function call(event:String, args:Array<Dynamic>):Dynamic {
#if LUA_ALLOWED
if(lua == null) {
return Function_Continue;
}
Lua.getglobal(lua, event);
for (arg in args) {
Convert.toLua(lua, arg);
}
var result:Null<Int> = Lua.pcall(lua, args.length, 1, 0);
if(result != null && resultIsAllowed(lua, result)) {
/*var resultStr:String = Lua.tostring(lua, result);
var error:String = Lua.tostring(lua, -1);
Lua.pop(lua, 1);*/
if(Lua.type(lua, -1) == Lua.LUA_TSTRING) {
var error:String = Lua.tostring(lua, -1);
Lua.pop(lua, 1);
if(error == 'attempt to call a nil value') { //Makes it ignore warnings and not break stuff if you didn't put the functions on your lua file
return Function_Continue;
}
}
var conv:Dynamic = Convert.fromLua(lua, result);
return conv;
}
#end
return Function_Continue;
}
#if LUA_ALLOWED
function resultIsAllowed(leLua:State, leResult:Null<Int>) { //Makes it ignore warnings
switch(Lua.type(leLua, leResult)) {
case Lua.LUA_TNIL | Lua.LUA_TBOOLEAN | Lua.LUA_TNUMBER | Lua.LUA_TSTRING | Lua.LUA_TTABLE:
return true;
}
return false;
}
#end
public function set(variable:String, data:Dynamic) {
#if LUA_ALLOWED
if(lua == null) {
return;
}
Convert.toLua(lua, data);
Lua.setglobal(lua, variable);
#end
}
#if LUA_ALLOWED
public function getBool(variable:String) {
var result:String = null;
Lua.getglobal(lua, variable);
result = Convert.fromLua(lua, -1);
Lua.pop(lua, 1);
if(result == null) {
return false;
}
// YES! FINALLY IT WORKS
//trace('variable: ' + variable + ', ' + result);
return (result == 'true');
}
#end
public function stop() {
#if LUA_ALLOWED
if(lua == null) {
return;
}
Lua.close(lua);
lua = null;
#end
}
}