early custom classes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package funkin.scripting;
|
||||
|
||||
import haxe.io.Path;
|
||||
import funkin.scripting.Script.ScriptClass;
|
||||
import hscript.Expr.ClassDecl;
|
||||
import hscript.Expr.ModuleDecl;
|
||||
@@ -14,6 +15,7 @@ class HScript extends Script {
|
||||
public var expr:Expr;
|
||||
public var decls:Array<ModuleDecl> = null;
|
||||
public var code:String;
|
||||
public var folderlessPath:String;
|
||||
|
||||
public static function initParser() {
|
||||
var parser = new Parser();
|
||||
@@ -29,7 +31,10 @@ class HScript extends Script {
|
||||
|
||||
code = Assets.getText(path);
|
||||
parser = initParser();
|
||||
folderlessPath = Path.directory(path);
|
||||
|
||||
interp.errorHandler = _errorHandler;
|
||||
interp.importFailedCallback = importFailedCallback;
|
||||
interp.staticVariables = Script.staticVariables;
|
||||
interp.allowStaticVariables = interp.allowPublicVariables = true;
|
||||
|
||||
@@ -49,6 +54,25 @@ class HScript extends Script {
|
||||
}
|
||||
}
|
||||
|
||||
private function importFailedCallback(cl:Array<String>):Bool {
|
||||
var path = cl.join("/");
|
||||
|
||||
var scr = Script.create(Paths.script('classes/$path', null, false));
|
||||
if (!(scr is DummyScript)) {
|
||||
// script is valid
|
||||
var cla = scr.getClass(cl.last());
|
||||
if (cla != null) {
|
||||
interp.variables.set(cl.last(), Script.createCustomClass(cla));
|
||||
} else {
|
||||
interp.variables.set(cl.last(), scr);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function _errorHandler(error:Error) {
|
||||
|
||||
var fn = '$fileName:${error.line}: ';
|
||||
@@ -131,16 +155,18 @@ class HScript extends Script {
|
||||
if (decls == null) {
|
||||
decls = parser.parseModule(code, fileName);
|
||||
}
|
||||
var imports:Array<Array<String>> = [];
|
||||
for(d in decls) {
|
||||
switch(d) {
|
||||
case DPackage(path):
|
||||
// ignore
|
||||
case DImport(path, everything):
|
||||
// ignore
|
||||
imports.push(path);
|
||||
case DClass(c):
|
||||
if (c.name == name) {
|
||||
|
||||
}
|
||||
trace(c.name);
|
||||
trace(name);
|
||||
if (c.name == name)
|
||||
return new HScriptClass(c, interp, imports, folderlessPath);
|
||||
case DTypedef(c):
|
||||
// ignore
|
||||
}
|
||||
@@ -221,17 +247,23 @@ class HScriptClass extends ScriptClass {
|
||||
|
||||
var interp:Interp;
|
||||
|
||||
public function new(decl:ClassDecl, sInterp:Interp) {
|
||||
public function new(decl:ClassDecl, sInterp:Interp, classesToImport:Array<Array<String>>, forderlessPath:String) {
|
||||
super();
|
||||
this.name = decl.name;
|
||||
switch(decl.extend) {
|
||||
case CTPath(p, params):
|
||||
this.classPath = p.join(".");
|
||||
default:
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
interp = new Interp();
|
||||
interp.errorHandler = sInterp.errorHandler;
|
||||
interp.staticVariables = sInterp.staticVariables;
|
||||
interp.allowStaticVariables = interp.allowPublicVariables = false;
|
||||
// interp.execute({ e : EBlock(decl.fields), pmin : 0, pmax : 0, origin : fileName, line : 0 });
|
||||
interp.allowStaticVariables = interp.allowPublicVariables = true;
|
||||
interp.importFailedCallback = importFailedCallback;
|
||||
|
||||
var parser = HScript.initParser();
|
||||
|
||||
@@ -246,7 +278,22 @@ class HScriptClass extends ScriptClass {
|
||||
}
|
||||
}
|
||||
|
||||
public override function hasField(field:String) {
|
||||
return interp.variables.exists(field);
|
||||
}
|
||||
public override function get(field:String) {
|
||||
return interp.variables[field];
|
||||
}
|
||||
|
||||
public override function set(field:String, v:Dynamic) {
|
||||
interp.variables[field] = v;
|
||||
}
|
||||
|
||||
public override function onCall(field:String, parameters:Array<Dynamic>, parent:Dynamic) {
|
||||
interp.scriptObject = parent;
|
||||
var v = interp.variables[field];
|
||||
if (v != null && Reflect.isFunction(v))
|
||||
return (parameters != null && parameters.length > 0) ? Reflect.callMethod(null, v, parameters) : v();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package funkin.scripting;
|
||||
import lime.app.Application;
|
||||
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
|
||||
import haxe.io.Path;
|
||||
import hscript.IHScriptCustomConstructor;
|
||||
|
||||
/**
|
||||
* Class used for scripting.
|
||||
@@ -154,6 +155,15 @@ class Script extends FlxBasic implements IFlxDestroyable {
|
||||
return new DummyScript(path);
|
||||
}
|
||||
|
||||
public static function createCustomClass(s:ScriptClass) {
|
||||
var t:Class<IScriptClassInstance> = cast Type.resolveClass('${s.classPath}__Softcoded');
|
||||
if (t == null) {
|
||||
Logs.trace('Class at ${s.classPath} was not found.', ERROR);
|
||||
return null;
|
||||
}
|
||||
return new ScriptCustomClass(s, t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the script class.
|
||||
* @param path
|
||||
@@ -277,11 +287,64 @@ class Script extends FlxBasic implements IFlxDestroyable {
|
||||
}
|
||||
|
||||
class ScriptClass {
|
||||
public var classPath:String = null;
|
||||
public var folderlessPath:String = null;
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function get(field:String):Dynamic {return null;}
|
||||
|
||||
public function set(field:String, v:Dynamic) {}
|
||||
|
||||
public function call(field:String, ?args:Array<Dynamic>) {return null;}
|
||||
public function hasField(field:String):Bool {return false;}
|
||||
|
||||
/**
|
||||
* Calls the function `func` defined in the class.
|
||||
* @param func Name of the function
|
||||
* @param parameters (Optional) Parameters of the function.
|
||||
* @return Result (if void, then null)
|
||||
*/
|
||||
public function call(func:String, parameters:Array<Dynamic>, parent:Dynamic):Dynamic {
|
||||
var result = onCall(func, parameters == null ? [] : parameters, parent);
|
||||
return result;
|
||||
}
|
||||
|
||||
public function onCall(func:String, parameters:Array<Dynamic>, parent:Dynamic):Dynamic {return null;}
|
||||
|
||||
private function importFailedCallback(cl:Array<String>):Bool {
|
||||
var path = cl.join("/");
|
||||
|
||||
var scr = Script.create(Paths.script('$folderlessPath$path', null, true));
|
||||
if (!(scr is DummyScript)) {
|
||||
// script is valid
|
||||
var cla = scr.getClass(cl.last());
|
||||
if (cla != null) {
|
||||
set(cl.last(), Script.createCustomClass(cla));
|
||||
} else {
|
||||
set(cl.last(), scr);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class ScriptCustomClass implements IHScriptCustomConstructor {
|
||||
var scrClass:ScriptClass;
|
||||
var scrType:Class<IScriptClassInstance>;
|
||||
public function new(scrClass:ScriptClass, scrType:Class<IScriptClassInstance>) {
|
||||
this.scrClass = scrClass;
|
||||
this.scrType = scrType;
|
||||
}
|
||||
public function hnew(...args:Dynamic):Dynamic {
|
||||
var a = [for(arg in args) arg];
|
||||
a.insert(0, scrClass);
|
||||
return Type.createInstance(scrType, a);
|
||||
}
|
||||
}
|
||||
|
||||
interface IScriptClassInstance {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user