philly stage
This commit is contained in:
@@ -6,6 +6,8 @@ import hscript.*;
|
||||
|
||||
class HScript extends Script {
|
||||
var interp:Interp;
|
||||
var expr:Expr;
|
||||
|
||||
public override function onCreate(path:String) {
|
||||
super.onCreate(path);
|
||||
|
||||
@@ -14,14 +16,12 @@ class HScript extends Script {
|
||||
var code:String = Assets.getText(path);
|
||||
var parser = new hscript.Parser();
|
||||
parser.allowJSON = parser.allowMetadata = parser.allowTypes = true;
|
||||
var expr:Expr;
|
||||
try {
|
||||
expr = parser.parseString(code, fileName);
|
||||
} catch(e) {
|
||||
return;
|
||||
}
|
||||
interp.errorHandler = _errorHandler;
|
||||
interp.execute(expr);
|
||||
}
|
||||
|
||||
private function _errorHandler(error:Error) {
|
||||
@@ -32,6 +32,10 @@ class HScript extends Script {
|
||||
interp.scriptObject = parent;
|
||||
}
|
||||
|
||||
public override function onLoad() {
|
||||
interp.execute(expr);
|
||||
}
|
||||
|
||||
private override function onCall(funcName:String, parameters:Array<Dynamic>):Dynamic {
|
||||
if (interp == null) return null;
|
||||
|
||||
|
||||
@@ -1,31 +1,91 @@
|
||||
package funkin.scripting;
|
||||
|
||||
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
|
||||
import flixel.FlxBasic;
|
||||
import haxe.io.Path;
|
||||
import openfl.utils.Assets;
|
||||
|
||||
/**
|
||||
* Class used for scripting.
|
||||
*/
|
||||
class Script {
|
||||
class Script extends FlxBasic implements IFlxDestroyable {
|
||||
public static function getDefaultVariables():Map<String, Dynamic> {
|
||||
return [
|
||||
// Haxe related stuff
|
||||
"Std" => Std,
|
||||
"Math" => Math,
|
||||
"StringTools" => StringTools,
|
||||
"Json" => haxe.Json,
|
||||
|
||||
// OpenFL & Lime related stuff
|
||||
"Assets" => openfl.utils.Assets,
|
||||
"Application" => lime.app.Application,
|
||||
"window" => lime.app.Application.current.window,
|
||||
|
||||
// Flixel related stuff
|
||||
"FlxG" => flixel.FlxG,
|
||||
"FlxSprite" => flixel.FlxSprite,
|
||||
"FlxEase" => flixel.tweens.FlxEase,
|
||||
"FlxTween" => flixel.tweens.FlxTween,
|
||||
"FlxCamera" => flixel.FlxCamera,
|
||||
"state" => flixel.FlxG.state,
|
||||
"FlxSound" => flixel.system.FlxSound,
|
||||
"FlxAssets" => flixel.system.FlxAssets,
|
||||
"FlxMath" => flixel.math.FlxMath,
|
||||
"FlxPoint" => flixel.math.FlxPoint,
|
||||
"FlxGroup" => flixel.group.FlxGroup,
|
||||
"FlxTypedGroup" => flixel.group.FlxGroup.FlxTypedGroup,
|
||||
"FlxSpriteGroup" => flixel.group.FlxSpriteGroup,
|
||||
"FlxTypeText" => flixel.addons.text.FlxTypeText,
|
||||
"FlxText" => flixel.text.FlxText,
|
||||
"FlxAxes" => flixel.util.FlxAxes,
|
||||
"FlxTimer" => flixel.util.FlxTimer,
|
||||
|
||||
// Engine related stuff
|
||||
"engine" => {
|
||||
build: funkin.macros.BuildCounterMacro.getBuildNumber(),
|
||||
name: "Codename Engine"
|
||||
},
|
||||
"PlayState" => funkin.game.PlayState,
|
||||
"GameOverSubstate" => funkin.game.GameOverSubstate,
|
||||
"Note" => funkin.game.Note,
|
||||
"Strum" => funkin.game.Strum,
|
||||
"Character" => funkin.game.Character,
|
||||
"Boyfriend" => funkin.game.Character, // for compatibility
|
||||
"FreeplayState" => funkin.menus.FreeplayState,
|
||||
"MainMenuState" => funkin.menus.MainMenuState,
|
||||
"PauseSubState" => funkin.menus.PauseSubState,
|
||||
"StoryMenuState" => funkin.menus.StoryMenuState,
|
||||
"TitleState" => funkin.menus.TitleState,
|
||||
"Paths" => funkin.system.Paths,
|
||||
"Conductor" => funkin.system.Conductor,
|
||||
"CoolUtil" => funkin.system.CoolUtil,
|
||||
];
|
||||
}
|
||||
/**
|
||||
* [Description] All available script extensions
|
||||
* All available script extensions
|
||||
*/
|
||||
public static var scriptExtensions:Array<String> = [
|
||||
"hx", "hscript", "hsc"
|
||||
];
|
||||
|
||||
/**
|
||||
* [Description] Currently executing script.
|
||||
* Currently executing script.
|
||||
*/
|
||||
public static var curScript:Script = null;
|
||||
|
||||
/**
|
||||
* [Description] Script name (with extension)
|
||||
* Script name (with extension)
|
||||
*/
|
||||
public var fileName:String;
|
||||
|
||||
/**
|
||||
* [Description] Creates a script from the specified asset path. The language is automatically determined.
|
||||
* Path to the script.
|
||||
*/
|
||||
public var path:String = null;
|
||||
|
||||
/**
|
||||
* Creates a script from the specified asset path. The language is automatically determined.
|
||||
* @param path Path in assets
|
||||
*/
|
||||
public static function create(path:String):Script {
|
||||
@@ -41,26 +101,33 @@ class Script {
|
||||
}
|
||||
|
||||
/**
|
||||
* [Description] Creates a new instance of the script class.
|
||||
* Creates a new instance of the script class.
|
||||
* @param path
|
||||
*/
|
||||
public function new(path:String) {
|
||||
super();
|
||||
|
||||
fileName = Path.withoutDirectory(path);
|
||||
var oldScript = curScript;
|
||||
curScript = this;
|
||||
this.path = path;
|
||||
onCreate(path);
|
||||
curScript = oldScript;
|
||||
for(k=>e in getDefaultVariables()) {
|
||||
set(k, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* [Description] Internal. Creates the script.
|
||||
* @param path Path to the script.
|
||||
* Loads the script
|
||||
*/
|
||||
public function onCreate(path:String) {}
|
||||
public function load() {
|
||||
var oldScript = curScript;
|
||||
curScript = this;
|
||||
onLoad();
|
||||
curScript = oldScript;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Description] Calls the function `func` defined in the script.
|
||||
* Calls the function `func` defined in the script.
|
||||
* @param func Name of the function
|
||||
* @param parameters (Optional) Parameters of the function.
|
||||
* @return Dynamic Result (if void, then null)
|
||||
@@ -76,27 +143,27 @@ class Script {
|
||||
}
|
||||
|
||||
/**
|
||||
* [Description] Sets a script's parent object so that its properties can be accessed easily. Ex: Passing `PlayState.instace` will allow `boyfriend` to be typed instead of `PlayState.instance.boyfriend`.
|
||||
* Sets a script's parent object so that its properties can be accessed easily. Ex: Passing `PlayState.instace` will allow `boyfriend` to be typed instead of `PlayState.instance.boyfriend`.
|
||||
* @param variable Parent variable.
|
||||
*/
|
||||
public function setParent(variable:Dynamic) {}
|
||||
|
||||
/**
|
||||
* [Description] Gets the variable `variable` from the script's variables.
|
||||
* Gets the variable `variable` from the script's variables.
|
||||
* @param variable Name of the variable.
|
||||
* @return Dynamic Variable (or null if it doesn't exists)
|
||||
*/
|
||||
public function get(variable:String):Dynamic {return null;}
|
||||
|
||||
/**
|
||||
* [Description] Gets the variable `variable` from the script's variables.
|
||||
* Gets the variable `variable` from the script's variables.
|
||||
* @param variable Name of the variable.
|
||||
* @return Dynamic Variable (or null if it doesn't exists)
|
||||
*/
|
||||
public function set(variable:String, value:Dynamic):Void {}
|
||||
|
||||
/**
|
||||
* [Description] Shows an error from this script.
|
||||
* Shows an error from this script.
|
||||
* @param text Text of the error (ex: Null Object Reference).
|
||||
* @param additionalInfo Additional information you could provide.
|
||||
*/
|
||||
@@ -111,4 +178,14 @@ class Script {
|
||||
private function onCall(func:String, parameters:Array<Dynamic>):Dynamic {
|
||||
return null;
|
||||
}
|
||||
public function onCreate(path:String) {}
|
||||
|
||||
public function onLoad() {}
|
||||
|
||||
public function onDestroy() {};
|
||||
|
||||
public override function destroy() {
|
||||
super.destroy();
|
||||
onDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package funkin.scripting;
|
||||
|
||||
class ScriptPack extends Script {
|
||||
public var scripts:Array<Script> = [];
|
||||
public var additionalDefaultVariables:Map<String, Dynamic> = [];
|
||||
public var parent:Dynamic = null;
|
||||
|
||||
public override function load() {
|
||||
for(e in scripts) {
|
||||
e.load();
|
||||
}
|
||||
}
|
||||
|
||||
public override function call(func:String, ?parameters:Array<Dynamic>):Dynamic {
|
||||
for(e in scripts)
|
||||
e.call(func, parameters);
|
||||
return null;
|
||||
}
|
||||
|
||||
public override function get(val:String):Dynamic {
|
||||
for(e in scripts) {
|
||||
var v = e.get(val);
|
||||
if (v != null) return v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override function set(val:String, value:Dynamic) {
|
||||
for(e in scripts) e.set(val, value);
|
||||
}
|
||||
|
||||
public override function setParent(parent:Dynamic) {
|
||||
this.parent = parent;
|
||||
for(e in scripts) e.setParent(parent);
|
||||
}
|
||||
|
||||
public override function onDestroy() {
|
||||
for(e in scripts) e.destroy();
|
||||
}
|
||||
|
||||
public override function onCreate(path:String) {}
|
||||
|
||||
public function add(script:Script) {
|
||||
scripts.push(script);
|
||||
__configureNewScript(script);
|
||||
}
|
||||
|
||||
public function remove(script:Script) {
|
||||
scripts.remove(script);
|
||||
}
|
||||
|
||||
public function insert(pos:Int, script:Script) {
|
||||
scripts.insert(pos, script);
|
||||
__configureNewScript(script);
|
||||
}
|
||||
|
||||
private function __configureNewScript(script:Script) {
|
||||
if (parent != null) script.setParent(parent);
|
||||
for(k=>e in additionalDefaultVariables) script.set(k, e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user