<commit_before>package pony.ui;

import haxe.rtti.Meta;
#if heaps
import pony.heaps.HeapsAssets;
#elseif pixijs
import pony.pixi.PixiAssets;
#elseif openfl
import pony.openfl.OpenflAssets;
#end
import pony.Or;
import pony.Tasks;
import pony.math.MathTools;
import pony.time.DeltaTime;
import pony.ui.gui.slices.SliceTools;

using Lambda;

/**
 * AssetManager
 * @author AxGord <<EMAIL>>
 */
class AssetManager {

	public static var baseUrl:String = '';
	private static var loadedAssets:Array<String> = [];
	private static var globalLoad:Map<String, Array<Int -> Int -> Void>> = new Map();
	
	public static var local:String = '';

	public static function resetAll():Void {
		for (e in loadedAssets.copy()) reset(e);
	}

	public static inline function reset(asset:String):Void {
		loadedAssets.remove(asset);
		_reset(asset);
	}
	
	@:extern public static inline function getPath(asset:String):String {
		return baseUrl + StringTools.replace(asset, '{local}', local);
	}
	
	public static dynamic function monitor(current:Int, total:Int):Void {}
	
	public static function loadPack(pathes:Array<String>, assets:Array<String>, cb:Int -> Int -> Void):Void {
		if (assets.length == 0) {
			cb(0, 0);
			return;
		}
		if (pathes.length == 0) {
			load(assets, cb);
			return;
		} else if (pathes.length == 1) {
			if (pathes[0] != '')
				load([for (a in assets) pathes[0] + '/' + a], cb);
			else
				load(assets, cb);
			return;
		}
		var loaded:Array<Int> = [for (_ in 0...pathes.length) 0];
		var totals:Array<Int> = [for (_ in 0...pathes.length) 10];
		var i:Int = 0;
		var prevLoaded:Int = 0;
		var prevTotals:Int = 0;
		for (path in pathes) {
			var n:Int = i++;
			load(path, assets, function(a:Int, t:Int) {
				loaded[n] = a;
				totals[n] = t;
				var loadedSum = sum(loaded);
				var totalSum = sum(totals);
				if (loadedSum != prevLoaded || totalSum != prevTotals) {
					prevLoaded = loadedSum;
					prevTotals = totalSum;
					cb(loadedSum, totalSum);
				}
			});
		}
	}
	
	public static function load(path:String = '', asset:Or<String, Array<String>>, cb:Int -> Int -> Void):Void {
		switch asset {
			case OrState.A(a):
				var r:Array<String> = parseInterval(a);
				if (r.length == 1)
					r = SliceTools.getNames(r[0]);
				if (r.length > 1) asset = OrState.B(r);
			case OrState.B(a):
				if (a.length == 1)
					return load(path, OrState.A(a[0]), cb);
				else if (a.length == 0)
					return cb(0, 0);
		}
		switch asset {
			case OrState.A(asset):
				asset = (path == '' ? '' : path + '/') + asset;
				if (loadedAssets.indexOf(asset) != -1) {
					cb(10, 10);
					return;
				}
				if (globalLoad.exists(asset)) {
					globalLoad[asset].push(cb);
				} else {
					globalLoad[asset] = [];
					var called:Bool = false;
					_load(asset, function(c:Int, t:Int) {
						cb(c, t);
						globalLoaded(asset, c, t);
						called = true;
					});
					if (!called) cb(0, 10);
				}<commit_msg>Global load bug fix<commit_after>package pony.ui;

import haxe.rtti.Meta;
#if heaps
import pony.heaps.HeapsAssets;
#elseif pixijs
import pony.pixi.PixiAssets;
#elseif openfl
import pony.openfl.OpenflAssets;
#end
import pony.Or;
import pony.Tasks;
import pony.math.MathTools;
import pony.time.DeltaTime;
import pony.ui.gui.slices.SliceTools;

using Lambda;

/**
 * AssetManager
 * @author AxGord <<EMAIL>>
 */
class AssetManager {

	public static var baseUrl:String = '';
	private static var loadedAssets:Array<String> = [];
	private static var globalLoad:Map<String, Array<Int -> Int -> Void>> = new Map();
	
	public static var local:String = '';

	public static function resetAll():Void {
		for (e in loadedAssets.copy()) reset(e);
	}

	public static inline function reset(asset:String):Void {
		loadedAssets.remove(asset);
		_reset(asset);
	}
	
	@:extern public static inline function getPath(asset:String):String {
		return baseUrl + StringTools.replace(asset, '{local}', local);
	}
	
	public static dynamic function monitor(current:Int, total:Int):Void {}
	
	public static function loadPack(pathes:Array<String>, assets:Array<String>, cb:Int -> Int -> Void):Void {
		if (assets.length == 0) {
			cb(0, 0);
			return;
		}
		if (pathes.length == 0) {
			load(assets, cb);
			return;
		} else if (pathes.length == 1) {
			if (pathes[0] != '')
				load([for (a in assets) pathes[0] + '/' + a], cb);
			else
				load(assets, cb);
			return;
		}
		var loaded:Array<Int> = [for (_ in 0...pathes.length) 0];
		var totals:Array<Int> = [for (_ in 0...pathes.length) 10];
		var i:Int = 0;
		var prevLoaded:Int = 0;
		var prevTotals:Int = 0;
		for (path in pathes) {
			var n:Int = i++;
			load(path, assets, function(a:Int, t:Int) {
				loaded[n] = a;
				totals[n] = t;
				var loadedSum = sum(loaded);
				var totalSum = sum(totals);
				if (loadedSum != prevLoaded || totalSum != prevTotals) {
					prevLoaded = loadedSum;
					prevTotals = totalSum;
					cb(loadedSum, totalSum);
				}
			});
		}
	}
	
	public static function load(path:String = '', asset:Or<String, Array<String>>, cb:Int -> Int -> Void):Void {
		switch asset {
			case OrState.A(a):
				var r:Array<String> = parseInterval(a);
				if (r.length == 1)
					r = SliceTools.getNames(r[0]);
				if (r.length > 1) asset = OrState.B(r);
			case OrState.B(a):
				if (a.length == 1)
					return load(path, OrState.A(a[0]), cb);
				else if (a.length == 0)
					return cb(0, 0);
		}
		switch asset {
			case OrState.A(asset):
				asset = (path == '' ? '' : path + '/') + asset;
				if (loadedAssets.indexOf(asset) != -1) {
					cb(10, 10);
					return;
				}
				if (globalLoad.exists(asset)) {
					cb(0, 10);
					globalLoad[asset].push(cb);
				} else {
					globalLoad[asset] = [];
					var called:Bool = false;
					_load(asset, function(c:Int, t:Int) {
						cb(c, t);
						globalLoaded(asset, c, t);
						called = true;
					});
					if (!called) cb(0, 10);
				}
{'fraction_non_alphanumeric': 0.1631813507027768, 'fraction_numerical': 0.011141583818992115, 'mean_word_length': 3.2129963898916967, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1976997', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2094, 'n_words': 519}