<commit_before>﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace AssetHandler.Loaders
{
	public class SpriteLoader : SynchronousAssetLoader<Sprite, SpriteParameters>
	{
		// Constants
		private static readonly Vector2 _pivot = new Vector2( 0.5f, 0.5f );
		private static readonly int _pixelsPerUnit = 100;
		private static readonly uint _extrude = 0;
		private static readonly SpriteMeshType _meshType = SpriteMeshType.FullRect;

		// Loader parameters

		/// <summary>
		/// Sprite's borders, used for nine-patches (sliced sprites)
		/// </summary>
		private Vector4 borders = Vector4.zero;

		/// <summary>
		/// Parameters for the TextureLoader
		/// </summary>
		private Texture2DParameters textureParams = null;

		public SpriteLoader( IFileInfoResolver resolver ) : base( resolver ) { }

		public override Sprite LoadSync( AssetManager manager, string path, FileInfo fileHandle, SpriteParameters param )
		{
			Texture2D tex = manager.Get<Texture2D>( path, textureParams );
			Rect rect = new Rect( 0, 0, tex.width, tex.height );

			Sprite result = Sprite.Create( tex, rect, _pivot, _pixelsPerUnit, _extrude, _meshType, borders );
			result.name = path;
			return result;
		}

		public override List<AssetDescriptor> GetDependencies( string path, FileInfo fileHandle, SpriteParameters param )
		{
			borders = Vector4.zero;
			textureParams = null;

			if ( param != null ) {
				textureParams = new Texture2DParameters();
				textureParams.filterMode = param.filterMode;
				textureParams.wrapMode = param.wrapMode;

				borders = param.borders;
			}

			return new List<AssetDescriptor>() { AssetDescriptor.Create<Texture2D>( path, textureParams ) };
		}
	}

	public sealed class SpriteParameters : AssetLoaderParameters<Sprite>
	{
		public FilterMode filterMode = FilterMode.Point;
		public TextureWrapMode wrapMode = TextureWrapMode.Clamp;
		public Vector4 borders = Vector4.zero;

		public override bool Equals( object obj )
		{
			if ( obj is SpriteParameters == false )
				return false;
			SpriteParameters o = (SpriteParameters)obj;
			return filterMode == o.filterMode && wrapMode == o.wrapMode && borders == o.borders;
		}

		public override int GetHashCode()
		{
			int hash = 17;
			hash = hash * 31 + filterMode.GetHashCode();
			hash = hash * 31 + wrapMode.GetHashCode();
			hash = hash * 31 + borders.GetHashCode();
			return hash;
		}
	}
}
<commit_msg>Made all parameters customizable for SpriteLoader, added a ctor with pxPerUnit ratio. Documentation.<commit_after>﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace AssetHandler.Loaders
{
	public class SpriteLoader : SynchronousAssetLoader<Sprite, SpriteParameters>
	{
		/// <summary>
		/// Defines the type of the mesh.
		/// </summary>
		private SpriteMeshType meshType = SpriteMeshType.FullRect;

		/// <summary>
		/// Sprite's borders, used for nine-patches (sliced sprites)
		/// </summary>
		private Vector4 borders = Vector4.zero;

		/// <summary>
		/// How much empty area to leave around the sprite in the generated mesh.
		/// </summary>
		private uint extrude = 0;

		/// <summary>
		/// Pixels per game unit. This should be constant for all sprites.
		/// </summary>
		private int pixelsPerUnit = 1;

		/// <summary>
		/// The pivot of the sprite.
		/// </summary>
		private Vector2 pivot = new Vector2( 0.5f, 0.5f );

		/// <summary>
		/// Parameters for the TextureLoader
		/// </summary>
		private Texture2DParameters textureParams = null;

		public SpriteLoader( IFileInfoResolver resolver, int pxPerUnit = 100 )
			: base( resolver )
		{
			pixelsPerUnit = pxPerUnit;
		}

		public override Sprite LoadSync( AssetManager manager, string path, FileInfo fileHandle, SpriteParameters param )
		{
			Texture2D tex = manager.Get<Texture2D>( path, textureParams );
			Rect rect = new Rect( 0, 0, tex.width, tex.height );

			Sprite result = Sprite.Create( tex, rect, pivot, pixelsPerUnit, extrude, meshType, borders );
			result.name = path;
			return result;
		}

		public override List<AssetDescriptor> GetDependencies( string path, FileInfo fileHandle, SpriteParameters param )
		{
			borders = Vector4.zero;
			textureParams = null;

			if ( param != null ) {
				textureParams = new Texture2DParameters();
				textureParams.filterMode = param.filterMode;
				textureParams.wrapMode = param.wrapMode;

				meshType = param.meshType;
				borders = param.borders;
				extrude = param.extrude;
				pivot = param.pivot;

				if ( param.pixelsPerUnit > 0 )
					pixelsPerUnit = param.pixelsPerUnit;
			}

			return new List<AssetDescriptor>() { AssetDescriptor.Create<Texture2D>( path, textureParams ) };
		}
	}

	public sealed class SpriteParameters : AssetLoaderParameters<Sprite>
	{
		public FilterMode filterMode = FilterMode.Point;
		public TextureWrapMode wrapMode = TextureWrapMode.Clamp;

		/// <summary>
		/// Defines the type of the mesh.
		/// </summary>
		public SpriteMeshType meshType = SpriteMeshType.FullRect;

		/// <summary>
		/// Sprite's borders, used for nine-patches (sliced sprites)
		/// </summary>
		public Vector4 borders = Vector4.zero;

		/// <summary>
		/// How much empty area to leave around the sprite in the generated mesh.
		/// </summary>
		public uint extrude = 0;

		/// <summary>
		/// Pixels per game unit. This should be constant for all sprites.
		/// </summary>
		public int pixelsPerUnit = -1;

		/// <summary>
		/// The pivot of the sprite.
		/// </summary>
		public Vector2 pivot = new Vector2( 0.5f, 0.5f );

		public override bool Equals( object obj )
		{
			if ( obj is SpriteParameters == false )
				return false;
			SpriteParameters o = (SpriteParameters)obj;
			return filterMode == o.filterMode &&
				wrapMode == o.wrapMode &&
				meshType == o.meshType &&
				borders == o.borders &&
				extrude == o.extrude &&
				pixelsPerUnit == o.pixelsPerUnit &&
				pivot == o.pivot;
		}

		public override int GetHashCode()
		{
			int hash = 17;
			hash = hash * 31 + filterMode.GetHashCode();
			hash = hash * 31 + wrapMode.GetHashCode();
			hash = hash * 31 + meshType.GetHashCode();
			hash = hash * 31 + borders.GetHashCode();
			hash = hash * 31 + extrude.GetHashCode();
			hash = hash * 31 + pixelsPerUnit.GetHashCode();
			hash = hash * 31 + pivot.GetHashCode();
			return hash;
		}
	}
}

{'fraction_non_alphanumeric': 0.11081081081081082, 'fraction_numerical': 0.012400635930047695, 'mean_word_length': 3.3326446280991737, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 42, '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': '4764539', 'n_tokens_mistral': 2242, 'n_tokens_neox': 1915, 'n_words': 563}