diff --git a/source/funkin/backend/scripting/HScript.hx b/source/funkin/backend/scripting/HScript.hx index 6bb7300b0f..586f3067fa 100644 --- a/source/funkin/backend/scripting/HScript.hx +++ b/source/funkin/backend/scripting/HScript.hx @@ -80,42 +80,91 @@ class HScript extends Script { return this; } - private function importFailedCallback(cl:Array, ?asName:String):Bool { - if(_importFailedCallback(cl, "source/") || _importFailedCallback(cl, "")) { + private function importFailedCallback(cl:Array, ?asName:String, ?isWildcard:Bool):Bool { + if(_importFailedCallback(cl, "source/", isWildcard) || _importFailedCallback(cl, "", isWildcard)) { return true; } return false; } - private function _importFailedCallback(cl:Array, prefix:String):Bool { - var assetsPath = 'assets/$prefix${cl.join("/")}'; - for(hxExt in ["hx", "hscript", "hsc", "hxs"]) { - var p = '$assetsPath.$hxExt'; - if (__importedPaths.exists(p)) - return true; // no need to reimport again - if (Assets.exists(p)) { - var code = Assets.getText(p); - var expr:Expr = null; - try { - if (code != null && code.length > 0) { - parser.line = 1; // fun fact: this is all you need to reuse a parser without issues. all the other vars get reset on parse. - expr = parser.parseString(code, cl.join("/") + "." + hxExt); - } - } catch(e:Error) { - _errorHandler(e); - } catch(e) { - _errorHandler(new Error(ECustom(e.toString()), 0, 0, fileName, 0)); - } - if (expr != null) { - @:privateAccess - interp.exprReturn(expr); - __importedPaths.set(p, true); + + private function _runScriptAsset(p:String, origin:String):Bool { + if (__importedPaths.exists(p)) + return true; // no need to reimport again + if (Assets.exists(p)) { + var code = Assets.getText(p); + var expr:Expr = null; + try { + if (code != null && code.length > 0) { + parser.line = 1; // fun fact: this is all you need to reuse a parser without issues. all the other vars get reset on parse. + expr = parser.parseString(code, origin); } - return true; + } catch(e:Error) { + _errorHandler(e); + } catch(e) { + _errorHandler(new Error(ECustom(e.toString()), 0, 0, fileName, 0)); } + if (expr != null) { + @:privateAccess + interp.exprReturn(expr); + __importedPaths.set(p, true); + } + return true; } return false; } + static final hxExts = ["hx", "hscript", "hsc", "hxs"]; + + private function _importFailedCallback(cl:Array, prefix:String, ?isWildcard):Bool { + if(isWildcard) return _importWildcard(cl, prefix); + + var assetsPath = 'assets/$prefix${cl.join("/")}'; + for(hxExt in hxExts) { + var p = '$assetsPath.$hxExt'; + final origin = cl.join('/')+'.'+hxExt; + if(_runScriptAsset(p, origin)) return true; + } + return false; + } + + private function _importWildcard(cl:Array, prefix:String):Bool { + final p = cl.copy(); + if(p.length >0 && p[p.length - 1] == "*") p.pop(); + + final relativeDir = p.join("/"); + return _loadWildcardDirScripts(prefix + relativeDir, relativeDir); + } + + // the uh `/` + private static inline function _directorySeparator(path:String):String { + return (path.length > 0 && !path.endsWith("/")) ? '/' : ''; + } + + private function _loadWildcardDirScripts(dir, relDir):Bool { + var finished = false; + + // load files + for(f in Paths.getFolderContent(dir, false)) { + for(i in hxExts) if(f.endsWith('.$i')) { + final path = 'assets/$dir${_directorySeparator(dir)}$f'; + final origin = (relDir.length > 0 ? '$relDir/' : '') + f; + + if(_runScriptAsset(path, origin)) finished = true; + break; + } + } + + // recurse directories + for(direct in Paths.getFolderDirectories(dir, false)) { + final nextDir = dir + _directorySeparator(dir) + direct; + final nextRelDir = relDir + _directorySeparator(relDir) + direct; + + if(_loadWildcardDirScripts(nextDir, nextRelDir)) finished = true; + } + + return finished; + } + private function _errorHandler(error:Error) { var fileName = error.origin; var oldfn = '$fileName:${error.line}: ';