Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.hxp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class EngineAsset {
// script related
if (scriptSupport.exists()) {
canHaxeScript.enable();
canLuaScript.enable();
// canLuaScript.enable();

disableDCE.enable();
scriptedStates.enable();
Expand Down Expand Up @@ -284,7 +284,7 @@ class EngineAsset {
// chart converter
addHaxelib('moonchart', 'git');

if ( canHaxeScript.exists()) addHaxelib('hscript-improved', 'git');
if ( canHaxeScript.exists()) addHaxelib('rulescript');
if ( canLuaScript .exists()) addHaxelib('linc_luajit', 'git');
if ( discordRPC .exists()) addHaxelib('hxdiscord_rpc');
if (allowVideos .exists()) addHaxelib('hxvlc', 'git');
Expand Down
7 changes: 2 additions & 5 deletions commands/setup/Setup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ class Setup {
Sys.println('The libraries json doesn\'t exist!\nPlease make one in the setup folder.\nHere\'s an example of one.\n${Json.stringify([
{
dependencies: [
{name: 'thx.semver'},
{
global: false,
name: 'hscript-improved',
name: 'rulescript',
version: 'git',
url: 'https://github.com/CodenameCrew/hscript-improved',
branch: 'custom-classes'
url: 'https://github.com/Kriptel/RuleScript'
}
],
questions: [
Expand Down
7 changes: 2 additions & 5 deletions commands/setup/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
"url": "https://github.com/Funkin-Imaginative/moonchart"
},

{
"name": "hscript-improved", "version": "git", "branch": "custom-classes",
"url": "https://github.com/CodenameCrew/hscript-improved"
},
{"name": "rulescript"},
{
"name": "linc_luajit", "version": "git",
"url": "https://github.com/superpowers04/linc_luajit"
Expand All @@ -50,7 +47,7 @@
],
"questions": [
{
"name": "hscript-improved",
"name": "rulescript",
"description": "include haxe scripting support"
},
{
Expand Down
150 changes: 72 additions & 78 deletions engine/source/imaginative/backend/scripting/Script.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import imaginative.backend.scripting.types.InvalidScript;
import imaginative.backend.scripting.types.LuaScript;

/**
* Help's clarify a script language instance.
* Helps clarify a script language instance.
*/
enum abstract ScriptType(String) from String to String {
/**
Expand Down Expand Up @@ -36,45 +36,43 @@ enum abstract ScriptType(String) from String to String {

/**
* All your scripting needs are right here!
* @author Class started by @Zyflx. Expanded on by @rodney528.
* Class started by @Zyflx, expanded on by @rodney528.
* @author @Zyflx & @rodney528
*/
class Script extends FlxBasic implements IScript {
@:allow(imaginative.backend.system.Main)
inline static function init():Void {
exts = [
class Script implements IFlxDestroyable implements IScript {
/**
* Every script instance that currently exists.
*/
public static var scripts:Array<IScript> = [];
/**
* All possible script extension types.
*/
public static var exts(get, never):Array<String>;
inline static function get_exts():Array<String> {
return [
for (exts in [HaxeScript.exts, LuaScript.exts])
for (ext in exts)
ext
];
}

/**
* Every script instance created.
*/
public static var scripts:Array<Script> = [];
public static var staticVars:Map<String, Dynamic> = new Map<String, Dynamic>();

/**
* All possible script extension types.
* Contains the mod path information.
*/
public static var exts(default, null):Array<String> = ['hx', 'lua'];
public var scriptPath(default, null):ModPath;

/**
* This variable holds the name of the script.
* This variable holds the file name of the script.
*/
public var name(get, never):String;
inline function get_name():String
return FilePath.withoutDirectory(pathing?.path) ?? 'none';
/**
* Contains the mod path information.
*/
public var pathing(default, null):ModPath;
public var fileName(get, never):String;
inline function get_fileName():String
return FilePath.withoutDirectory(scriptPath.path);
/**
* This variable holds the name of the file extension.
*/
public var extension(get, never):String;
inline function get_extension():String
return pathing?.extension ?? 'none';
return scriptPath.extension;

/**
* Creates a script instance(s).
Expand All @@ -97,7 +95,6 @@ class Script extends FlxBasic implements IScript {
#else
var paths:Array<String> = [Paths.script(file).format()];
#end
// trace(paths);

var scripts:Array<Script> = [];
for (path in paths) {
Expand All @@ -119,66 +116,66 @@ class Script extends FlxBasic implements IScript {
public var type(get, never):ScriptType;
inline function get_type():ScriptType {
return switch (this.getClassName()) {
case 'Script': TypeUnregistered;
case 'HaxeScript': TypeHaxe;
case 'LuaScript': TypeLua;
case 'InvalidScript': TypeInvalid;
default: TypeUnregistered;
case 'Script': TypeUnregistered;
case 'HaxeScript': TypeHaxe;
case 'LuaScript': TypeLua;
case 'InvalidScript': TypeInvalid;
default: TypeUnregistered;
}
}

function renderNecessities():Void {}

function new(file:ModPath, ?code:String):Void {
if (code == null)
pathing = file;
super();
renderScript(pathing);
renderNecessities();
scriptPath = file;
loadScriptCode(scriptPath, code);
loadNecessities();
if (code == null) {
scripts.push(this);
GlobalScript.call('scriptCreated', [this, type]);
}
}

var code:String = '';
function renderScript(file:ModPath, ?code:String):Void {
var scriptCode(null, null):String = '';
function loadScriptCode(file:ModPath, ?code:String):Void {
try {
var content:String = Assets.text(file);
this.code = content.isNullOrEmpty() ? code : content;
scriptCode = code == null ? Assets.text(file) : code;
} catch(error:haxe.Exception) {
log('Error while trying to get script contents: ${error.message}', ErrorMessage);
this.code = '';
scriptCode = '';
}
}
function loadCodeString(code:String):Void {}

function loadNecessities():Void {}

function launchScript(code:String):Void {}

// /**
// * Loads code from string.
// * @param code The script code.
// * @param vars Variables to input into the script instance.
// * @param funcToRun Function to run inside the script instance.
// * @param funcArgs Arguments to run for said function.
// * @return `Script` ~ The script instance from string.
// */
// public static function loadCodeFromString(code:String, ?vars:Map<String, Dynamic>, ?funcToRun:String, ?funcArgs:Array<Dynamic>):Script
// return this;

/**
* Load's code from string.
* @param code The script code.
* @param vars Variables to input into the script instance.
* @param funcToRun Function to run inside the script instance.
* @param funcArgs Arguments to run for said function.
* @return `Script` ~ The script instance from string.
* If true, the script is active and can mess around with the game.
*/
/* public static function loadCodeFromString(code:String, ?vars:Map<String, Dynamic>, ?funcToRun:String, ?funcArgs:Array<Dynamic>):Script
return this; */

public var active(get, default):Bool = true;
inline function get_active():Bool
return active && canRun;
/**
* States if the script has loaded.
*/
public var loaded(default, null):Bool = false;
/**
* Load's the script, pretty self-explanatory.
* Loads the script, pretty self-explanatory.
*/
public function load():Void
if (!loaded)
loadCodeString(code);
/**
* Reload's the script, pretty self-explanatory.
* Only if it's possible for that script type.
*/
public function reload():Void {}
inline public function load():Void
if (!loaded && canRun)
launchScript(scriptCode);

/**
* The parent object that the script is tied to.
Expand All @@ -187,13 +184,7 @@ class Script extends FlxBasic implements IScript {
function get_parent():Dynamic
return null;
function set_parent(value:Dynamic):Dynamic
return value = null;

/**
* Sets the public map for getting global variables.
* @param map The map itself.
*/
public function setPublicMap(map:Map<String, Dynamic>):Void {}
return null;

/**
* Sets a variable to the script.
Expand All @@ -202,23 +193,24 @@ class Script extends FlxBasic implements IScript {
*/
public function set(variable:String, value:Dynamic):Void {}
/**
* Get's a variable from the script.
* Gets a variable from the script.
* @param variable The variable to receive.
* @param def If it's null then return this.
* @return `Dynamic` ~ The value the variable will hold.
* @return `T` ~ The value the variable will hold.
*/
public function get(variable:String, ?def:Dynamic):Dynamic
public function get<T>(variable:String, ?def:T):T
return def;
/**
* Call's a function in the script instance.
* Calls a function in the script instance.
* @param func Name of the function to call.
* @param args Arguments of said function.
* @return `Dynamic` ~ Whatever is in the functions return statement.
* @param def If your using this to return something, then this would be if it returns null.
* @return `T` ~ Whatever is in the functions return statement.
*/
public function call(func:String, ?args:Array<Dynamic>):Dynamic
return null;
public function call<T>(func:String, ?args:Array<Dynamic>, ?def:T):T
return def;
/**
* Call's a function in the script instance and triggers an event.
* Calls a function in the script instance and triggers an event.
* @param func Name of the function to call.
* @param event The event class.
* @return `ScriptEvent`
Expand All @@ -227,18 +219,20 @@ class Script extends FlxBasic implements IScript {
return event;

/**
* End's the script.
* @param funcName Custom function call name.
* Ends the script, basically **destroy**, but with an extra step.
* @param funcName The function name to call that tells the script that it's time is over.
*/
inline public function end(funcName:String = 'end'):Void {
call(funcName);
destroy();
}

override public function destroy():Void {
/**
* Destroys the script instance when called.
*/
public function destroy():Void {
GlobalScript.call('scriptDestroyed', [this, type]);
if (scripts.contains(this))
scripts.remove(this);
super.destroy();
}
}
Loading