very early wip

might crash idk
stuff is super very undone i still havent tested as i gtg for now
This commit is contained in:
⍚~Nex
2024-04-01 21:10:09 +02:00
parent 20fe94cd5a
commit aa9af9b052
5 changed files with 138 additions and 0 deletions
+3
View File
@@ -124,6 +124,9 @@
<!-- Comment this out to disable mods folder !-->
<haxedef name="MOD_SUPPORT" unless="web"/>
<!-- Comment this out to disable translations folders !-->
<haxedef name="TRANSLATIONS_SUPPORT"/>
<!-- Comment this out to disable global scripts !-->
<haxedef name="GLOBAL_SCRIPT" />
+6
View File
@@ -36,6 +36,12 @@ class Paths
public static inline function getPath(file:String, ?library:String)
return library != null ? '$library:assets/$library/$file' : 'assets/$file';
public static inline function translationsMain(key:String)
return getPath(key, 'translations');
public static inline function translations(key:String)
return translationsMain('${TranslationsUtil.get_curLanguage()}/$key');
public static inline function video(key:String, ?ext:String = "mp4")
return getPath('videos/$key.$ext');
@@ -55,6 +55,7 @@ class MainState extends FlxState {
Main.refreshAssets();
ModsFolder.onModSwitch.dispatch(ModsFolder.currentModFolder);
TranslationsUtil.setTransl();
DiscordUtil.init();
EventsData.reloadEvents();
TitleState.initialized = false;
@@ -0,0 +1,127 @@
package funkin.backend.utils;
import openfl.utils.Assets;
import haxe.io.Path;
import haxe.xml.Access;
import haxe.Exception;
/**
* The class used for translations based on the XMLs inside the translations folders.
*
* Made by @NexIsDumb originally for the Poldhub mod.
*/
class TranslationsUtil
{
/**
* The current language selected translation map; If the current language it's english, this map will be empty.
*
* Using this class function it'll never be `null`.
*/
public static var transMap(default, null):Map<String, String> = [];
/**
* The default language used inside of the source code (`en` is English in this case).
*/
public static var DEFAULT_LANGUAGE:String = 'en'; // no inline so psycopathic mods can edit it - Nex
/**
* Returns the current language.
*/
inline public static function get_curLanguage():String
return Options.language;
/**
* Returns if the current language is the default one (`DEFAULT_LANGUAGE`).
*/
inline public static function is_defaultLanguage():Bool
return Options.language == DEFAULT_LANGUAGE;
/**
* Returns if any translation is loaded.
*/
inline public static function is_anyTransLoaded():Bool
return transMap != [];
/**
* Changes the translations map.
*
* If `name` is `null`, it's gonna use the current language.
*/
public static function setTransl(?name:String)
transMap = loadLanguage(name == null ? get_curLanguage() : name);
/**
* This is for checking a translation, `defString` it's just the string that gets returned just in case it won't find the translation OR the current language selected is ``DEFAULT_LANGUAGE``.
*
* If `id` is `null` then it's gonna search using `defString`.
*/
public static function checkTransl(defString:String, ?id:String):String
{
#if TRANSLATIONS_SUPPORT
if(id == null) id = defString;
if(transMap.exists(id)) return transMap.get(id);
#end
return defString;
}
/**
* Returns an array that specifies which translations were found.
*/
public static function translList():Array<String>
{
var translations:Array<String> = [];
#if TRANSLATIONS_SUPPORT
// to finish once done with the lib thing - Nex
for(l in Assets.list()) {
var d = Path.directory(l);
if(d.startsWith(Paths.translationsMain(''))) {
d = (d.replace(Paths.translationsMain(''), "").split("/"))[0];
if(!translations.contains(d)) translations.push(d);
}
}
#end
return translations;
}
/**
* Returns a map of translations based on its main.xml.
*/
public static function loadLanguage(name:String):Map<String, String>
{
#if TRANSLATIONS_SUPPORT
var path:String = 'main.xml';
if (!Assets.exists(Paths.translations('$name/$path'), TEXT)) return [];
var xml:Access = null;
try xml = new Access(Xml.parse(Assets.getText(path)))
catch(e) {
FlxG.log.error('Error while parsing main.xml: ${Std.string(e)}');
throw new Exception('Error while parsing main.xml: ${Std.string(e)}');
}
if (xml == null) return [];
if (!xml.hasNode.translations) {
FlxG.log.warn("A translation xml file requires a translations root element.");
return [];
}
var leMap:Map<String, String> = [];
for(node in xml.node.translations.elements) {
switch(node.name) {
case "trans":
if (!node.has.id) {
FlxG.log.warn("A translation node requires an ID attribute.");
continue;
}
if (!node.has.string) {
FlxG.log.warn("A translation node requires a string attribute.");
continue;
}
leMap.set(node.att.id, node.att.string);
}
}
return leMap;
#else
return [];
#end
}
}
+1
View File
@@ -35,6 +35,7 @@ class Options
public static var songOffset:Float = 0;
public static var framerate:Int = 120;
public static var gpuOnlyBitmaps:Bool = #if (mac || web) false #else true #end; // causes issues on mac and web
public static var language = funkin.backend.utils.TranslationsUtil.DEFAULT_LANGUAGE;
public static var lastLoadedMod:String = null;