<commit_before>package haxepunk.assets;

#if macro
import sys.FileSystem;
import sys.io.File;
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.Compiler;
#end

import haxe.io.Path;
import haxe.ds.StringMap;

typedef PreloadPath = {
	path:String,
	?alias:String
}

class Preloader
{
	#if !macro

	public var required(default, null):Int = 0;
	public var loaded(default, null):Int = 0;
	public var failed(default, null):Int = 0;

	var cache:AssetCache;
	public var onLoad = new haxepunk.Signal.Signal0();
	var assets = new StringMap<Array<String>>();

	public function new(?cache:AssetCache)
	{
		if (cache == null)
		{
			cache = haxepunk.assets.AssetCache.global;
		}
		this.cache = cache;
	}

	public function addAsset(path:String, ?aliases:Array<String>)
	{
		required += 1;
		assets.set(path, [path].concat(aliases == null ? [] : aliases));
	}

	function check()
	{
		if (required == loaded + failed)
			onLoad.invoke();
	}

	function success()
	{
		loaded += 1;
		check();
	}

	function fail()
	{
		failed += 1;
		check();
	}

	function loadTexture(path:String)
	{
		var aliases = assets.get(path);
		#if (!lime && js)
		haxepunk.backend.html5.Texture.loadFromURL(path).then(function(texture) {
			for (alias in aliases) {
				cache.addTexture(alias, texture);
			}
			success();
		}, function(_) {
			fail();
		});
		#else
		var texture = haxepunk.HXP.assetLoader.getTexture(path);
		if (texture == null)
		{
			Log.critical('Failed to load texture: ${path}');
			fail();
		}
		else
		{
			for (alias in aliases) {
				cache.addTexture(alias, texture);
			}
			success();
		}
		#end
	}

	function loadSound(path:String)
	{
		var aliases = assets.get(path);
		#if (!lime && js)
		haxepunk.backend.html5.Sound.loadFromURL(path).then(function(sound) {
			for (alias in aliases) {
				cache.addSound(alias, sound);
			}
			success();
		}, function(_) {
			fail();
		});
		#else
		var sound = HXP.assetLoader.getSound(path);
		if (sound == null)
		{
			Log.critical('Failed to load sound: ${path}');
			fail();
		}
		else
		{
			for (alias in aliases) {
				cache.addSound(alias, sound);
			}
			success();
		}
		#end
	}

	function loadText(path:String)
	{
		var aliases = assets.get(path);
		var text = HXP.assetLoader.getText(path);
		if (text == null)
		{
			Log.critical('Failed to load text: ${path}');
			fail();
		}
		else
		{
			for (alias in aliases) {
				cache.addText(alias, text);
			}
			success();
		}
	}

	public function load()
	{
		for (path in assets.keys())<commit_msg>Add text preloading for html5 target<commit_after>package haxepunk.assets;

#if macro
import sys.FileSystem;
import sys.io.File;
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.Compiler;
#end

import haxe.io.Path;
import haxe.ds.StringMap;

typedef PreloadPath = {
	path:String,
	?alias:String
}

class Preloader
{
	#if !macro

	public var required(default, null):Int = 0;
	public var loaded(default, null):Int = 0;
	public var failed(default, null):Int = 0;

	var cache:AssetCache;
	public var onLoad = new haxepunk.Signal.Signal0();
	var assets = new StringMap<Array<String>>();

	public function new(?cache:AssetCache)
	{
		if (cache == null)
		{
			cache = haxepunk.assets.AssetCache.global;
		}
		this.cache = cache;
	}

	public function addAsset(path:String, ?aliases:Array<String>)
	{
		required += 1;
		assets.set(path, [path].concat(aliases == null ? [] : aliases));
	}

	function check()
	{
		if (required == loaded + failed)
			onLoad.invoke();
	}

	function success()
	{
		loaded += 1;
		check();
	}

	function fail()
	{
		failed += 1;
		check();
	}

	function loadTexture(path:String)
	{
		var aliases = assets.get(path);
		#if (!lime && js)
		haxepunk.backend.html5.Texture.loadFromURL(path).then(function(texture) {
			for (alias in aliases) {
				cache.addTexture(alias, texture);
			}
			success();
		}, function(_) {
			fail();
		});
		#else
		var texture = haxepunk.HXP.assetLoader.getTexture(path);
		if (texture == null)
		{
			Log.critical('Failed to load texture: ${path}');
			fail();
		}
		else
		{
			for (alias in aliases) {
				cache.addTexture(alias, texture);
			}
			success();
		}
		#end
	}

	function loadSound(path:String)
	{
		var aliases = assets.get(path);
		#if (!lime && js)
		haxepunk.backend.html5.Sound.loadFromURL(path).then(function(sound) {
			for (alias in aliases) {
				cache.addSound(alias, sound);
			}
			success();
		}, function(_) {
			fail();
		});
		#else
		var sound = HXP.assetLoader.getSound(path);
		if (sound == null)
		{
			Log.critical('Failed to load sound: ${path}');
			fail();
		}
		else
		{
			for (alias in aliases) {
				cache.addSound(alias, sound);
			}
			success();
		}
		#end
	}

	function loadText(path:String)
	{
		var aliases = assets.get(path);
		#if (!lime && js)
		var http = new js.html.XMLHttpRequest();
		http.onreadystatechange = function() {
			if (http.readyState == js.html.XMLHttpRequest.DONE) {
				if (http.status == 200) {
					var text = http.responseText;
					for (alias in aliases) {
						cache.addText(alias, text);
					}
					success();
				} else {
					fail();
				}
			}
		}
		http.open('GET', path, true);
		http.send();
		#else
		var text = HXP.assetLoader.getText(path);
		if (text == null)
		{
			Log.critical('Failed to load text: ${path}');
			fail();
		}
		else
		{
			for (alias in aliases) {
				cache.addText(alias, text);
			}
			success();
		}
		#end
	}

	public function load()
	{
		for (path in assets.keys())
{'fraction_non_alphanumeric': 0.156794425087108, 'fraction_numerical': 0.004034476434989913, 'mean_word_length': 3.2542901716068644, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1334315', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2001, 'n_words': 441}