scripting
This commit is contained in:
@@ -1,18 +1,33 @@
|
||||
package funkin.scripting;
|
||||
|
||||
import hscript.Expr.Error;
|
||||
import openfl.Assets;
|
||||
import hscript.*;
|
||||
|
||||
class HScript extends Script {
|
||||
var interp:Interp;
|
||||
public override function create(path:String) {
|
||||
super.create(path);
|
||||
public override function onCreate(path:String) {
|
||||
super.onCreate(path);
|
||||
|
||||
interp = new Interp();
|
||||
|
||||
var code:String = Assets.getText(path);
|
||||
interp = new Interp();
|
||||
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;
|
||||
}
|
||||
|
||||
public override function call() {
|
||||
private function _errorHandler(error:Error) {
|
||||
error('$fileName:${error.line}: ${Std.string(error.e)}');
|
||||
}
|
||||
|
||||
public override function call(func:String, parameters:Array<Dynamic>) {
|
||||
// TODO: call
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,43 @@
|
||||
package funkin.scripting;
|
||||
|
||||
import haxe.io.Path;
|
||||
import openfl.utils.Assets;
|
||||
|
||||
/**
|
||||
* Class used for scripting.
|
||||
*/
|
||||
class Script {
|
||||
/**
|
||||
* [Description] All available script extensions
|
||||
*/
|
||||
public static var scriptExtensions:Array<String> = [
|
||||
"hx", "hscript", "hsc"
|
||||
];
|
||||
|
||||
/**
|
||||
* [Description] Currently executing script.
|
||||
*/
|
||||
public static var curScript:Script = null;
|
||||
|
||||
/**
|
||||
* [Description] Script name (with extension)
|
||||
*/
|
||||
public var fileName:String;
|
||||
|
||||
/**
|
||||
* [Description] Creates a script from the specified asset path. The language is automatically determined.
|
||||
* @param path
|
||||
* @param path Path in assets
|
||||
*/
|
||||
public static function create(path:String):Script {
|
||||
if (Assets.exists(file)) {
|
||||
if (Assets.exists(path)) {
|
||||
return switch(Path.extension(path).toLowerCase()) {
|
||||
case "hx" | "hscript" | "hsc": new HScript(path);
|
||||
default: new DummyScript();
|
||||
case "hx" | "hscript" | "hsc":
|
||||
new HScript(path);
|
||||
default:
|
||||
new DummyScript(path);
|
||||
}
|
||||
} else {
|
||||
return new DummyScript(path);
|
||||
}
|
||||
return new DummyScript(path);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,9 +45,10 @@ class Script {
|
||||
* @param path
|
||||
*/
|
||||
public function new(path:String) {
|
||||
fileName = Path.withoutDirectory(path);
|
||||
var oldScript = curScript;
|
||||
curScript = this;
|
||||
create(path);
|
||||
onCreate(path);
|
||||
curScript = oldScript;
|
||||
}
|
||||
|
||||
@@ -42,21 +57,21 @@ class Script {
|
||||
* [Description] Internal. Creates the script.
|
||||
* @param path Path to the script.
|
||||
*/
|
||||
public function create(path:String) {}
|
||||
public function onCreate(path:String) {}
|
||||
/**
|
||||
* [Description] 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)
|
||||
*/
|
||||
public function call(func:String, ?parameters:Array<Dynamic>):Dynamic {}
|
||||
public function call(func:String, ?parameters:Array<Dynamic>):Dynamic {return null;}
|
||||
|
||||
/**
|
||||
* [Description] 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 {}
|
||||
public function get(variable:String):Dynamic {return null;}
|
||||
|
||||
/**
|
||||
* [Description] Gets the variable `variable` from the script's variables.
|
||||
@@ -70,5 +85,8 @@ class Script {
|
||||
* @param text Text of the error (ex: Null Object Reference).
|
||||
* @param additionalInfo Additional information you could provide.
|
||||
*/
|
||||
public function error(text:String, additionalInfo:Dynamic):Void {}
|
||||
public function error(text:String, additionalInfo:Dynamic):Void {
|
||||
// TODO: Logs (like on YCE)
|
||||
trace('$text');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package funkin.system;
|
||||
|
||||
import flixel.FlxG;
|
||||
import flixel.graphics.frames.FlxAtlasFrames;
|
||||
import openfl.utils.AssetType;
|
||||
import openfl.utils.Assets as OpenFlAssets;
|
||||
import funkin.mods.ModsFolder;
|
||||
import funkin.scripting.Script;
|
||||
|
||||
using StringTools;
|
||||
|
||||
class Paths
|
||||
{
|
||||
/**
|
||||
* Preferred sound extension for the game's audio files.
|
||||
* Currently is set to `mp3` for web targets, and `ogg` for other targets.
|
||||
*/
|
||||
inline public static var SOUND_EXT = #if web "mp3" #else "ogg" #end;
|
||||
private static var __useSourceAssets = false;
|
||||
|
||||
static function getPath(file:String, type:AssetType, library:Null<String>, skipModsVerification:Bool = false)
|
||||
{
|
||||
if (library != null && library.startsWith("mods/")) {
|
||||
library = library.toLowerCase();
|
||||
} else if (!skipModsVerification && ModsFolder.currentModFolder != null) {
|
||||
var modPath = getPath(file, type, 'mods/${ModsFolder.currentModFolder}');
|
||||
if (OpenFlAssets.exists(modPath)) return modPath;
|
||||
}
|
||||
|
||||
if (library != null)
|
||||
return getLibraryPath(file, library);
|
||||
|
||||
return getPreloadPath(file);
|
||||
}
|
||||
|
||||
static public function getLibraryPath(file:String, library = "preload")
|
||||
{
|
||||
return if (library == "preload" || library == "default") getPreloadPath(file); else getLibraryPathForce(file, library);
|
||||
}
|
||||
|
||||
inline static function getLibraryPathForce(file:String, library:String)
|
||||
{
|
||||
return '$library:assets/$library/$file';
|
||||
}
|
||||
|
||||
inline static function getPreloadPath(file:String)
|
||||
{
|
||||
return (__useSourceAssets) ? getLibraryPathForce(file, 'sourceassets') : 'assets/$file';
|
||||
}
|
||||
|
||||
inline static public function file(file:String, type:AssetType = TEXT, ?library:String)
|
||||
{
|
||||
return getPath(file, type, library);
|
||||
}
|
||||
|
||||
inline static public function txt(key:String, ?library:String)
|
||||
{
|
||||
return getPath('data/$key.txt', TEXT, library);
|
||||
}
|
||||
|
||||
inline static public function xml(key:String, ?library:String)
|
||||
{
|
||||
return getPath('data/$key.xml', TEXT, library);
|
||||
}
|
||||
|
||||
inline static public function json(key:String, ?library:String)
|
||||
{
|
||||
return getPath('data/$key.json', TEXT, library);
|
||||
}
|
||||
|
||||
static public function sound(key:String, ?library:String)
|
||||
{
|
||||
return getPath('sounds/$key.$SOUND_EXT', SOUND, library);
|
||||
}
|
||||
|
||||
inline static public function soundRandom(key:String, min:Int, max:Int, ?library:String)
|
||||
{
|
||||
return sound(key + FlxG.random.int(min, max), library);
|
||||
}
|
||||
|
||||
inline static public function music(key:String, ?library:String)
|
||||
{
|
||||
return getPath('music/$key.$SOUND_EXT', MUSIC, library);
|
||||
}
|
||||
|
||||
inline static public function voices(song:String)
|
||||
{
|
||||
return getPath('songs/${song.toLowerCase()}/Voices.ogg', MUSIC, null);
|
||||
}
|
||||
|
||||
inline static public function inst(song:String)
|
||||
{
|
||||
return getPath('songs/${song.toLowerCase()}/Inst.ogg', MUSIC, null);
|
||||
}
|
||||
|
||||
inline static public function image(key:String, ?library:String)
|
||||
{
|
||||
return getPath('images/$key.png', IMAGE, library);
|
||||
}
|
||||
|
||||
inline static public function script(key:String, ?library:String) {
|
||||
var scriptPath = getPath(key, TEXT, library);
|
||||
var p:String;
|
||||
for(ex in Script.scriptExtensions) {
|
||||
p = getPath('$key.$ex', TEXT, library);
|
||||
if (OpenFlAssets.exists(p)) {
|
||||
scriptPath = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return scriptPath;
|
||||
}
|
||||
|
||||
static public function chart(song:String, ?difficulty:String = "normal"):String {
|
||||
difficulty = difficulty.toLowerCase();
|
||||
song = song.toLowerCase();
|
||||
|
||||
var difficultyEnd = (difficulty == "normal") ? "" : '-$difficulty';
|
||||
|
||||
// charts/your-song/hard.json
|
||||
var p = getPath('charts/$song/$difficulty.json', TEXT, null);
|
||||
if (OpenFlAssets.exists(p)) return p;
|
||||
|
||||
// charts/your-song/your-song-hard.json
|
||||
var p2 = getPath('charts/$song/$song$difficultyEnd.json', TEXT, null);
|
||||
if (OpenFlAssets.exists(p2)) return p2;
|
||||
|
||||
// data/your-song/your-song-hard.json (default old format)
|
||||
p2 = json('$song/$song$difficultyEnd');
|
||||
if (OpenFlAssets.exists(p2)) return p2;
|
||||
|
||||
return p; // returns the normal one so that it shows the correct path in the error message.
|
||||
}
|
||||
|
||||
inline static public function font(key:String)
|
||||
{
|
||||
return 'assets/fonts/$key';
|
||||
}
|
||||
|
||||
inline static public function getSparrowAtlas(key:String, ?library:String)
|
||||
{
|
||||
return FlxAtlasFrames.fromSparrow(image(key, library), file('images/$key.xml', library));
|
||||
}
|
||||
|
||||
inline static public function getPackerAtlas(key:String, ?library:String)
|
||||
{
|
||||
return FlxAtlasFrames.fromSpriteSheetPacker(image(key, library), file('images/$key.txt', library));
|
||||
}
|
||||
|
||||
inline static public function getFolderContent(key:String, includeSource:Bool = false, ?library:String):Array<String> {
|
||||
// designed to work both on windows and web
|
||||
|
||||
while(key.charAt(key.length-1) == "/") key.substr(0, key.length-1);
|
||||
var path = getPath('$key/.foldercontent', TEXT, library);
|
||||
var content:Array<String> = [];
|
||||
if (OpenFlAssets.exists(path)) {
|
||||
var text = OpenFlAssets.getText(path);
|
||||
for(e in text.split("\n")) content.push(e.trim());
|
||||
}
|
||||
if (includeSource) {
|
||||
var path = getPath('$key/.foldercontent', TEXT, library, true);
|
||||
if (OpenFlAssets.exists(path)) {
|
||||
var text = OpenFlAssets.getText(path);
|
||||
for(e in text.split("\n")) content.push(e.trim());
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user