<commit_before>import { Resource } from 'resource-loader';
import path from 'path';
import * as core from '../core';

const BATCH_SIZE = 1000;

export default function ()
{
    return function spritesheetParser(resource, next)
    {
        let resourcePath;
        const imageResourceName = `${resource.name}_image`;

        // skip if no data, its not json, it isn't spritesheet data, or the image resource already exists
        if (!resource.data || !resource.isJson || !resource.data.frames || this.resources[imageResourceName])
        {
            next();

            return;
        }

        const loadOptions = {
            crossOrigin: resource.crossOrigin,
            loadType: Resource.LOAD_TYPE.IMAGE,
            metadata: resource.metadata.imageMetadata,
        };

        // Prepend url path unless the resource image is a data url
        if (resource.isDataUrl)
        {
            resourcePath = resource.data.meta.image;
        }
        else
        {
            resourcePath = `${path.dirname(resource.url.replace(this.baseUrl, ''))}/${resource.data.meta.image}`;
        }

        // load the image for this sheet
        this.add(imageResourceName, resourcePath, loadOptions, function onImageLoad(res)
        {
            resource.textures = {};

            const frames = resource.data.frames;
            const frameKeys = Object.keys(frames);
            const resolution = core.utils.getResolutionOfUrl(resource.url);
            let batchIndex = 0;

            function processFrames(initialFrameIndex, maxFrames)
            {
                let frameIndex = initialFrameIndex;

                while (frameIndex - initialFrameIndex < maxFrames && frameIndex < frameKeys.length)
                {
                    const i = frameKeys[frameIndex];
                    const rect = frames[i].frame;

                    if (rect)
                    {
                        let frame = null;
                        let trim = null;
                        const orig = new core.Rectangle(
                            0,
                            0,
                            frames[i].sourceSize.w / resolution,
                            frames[i].sourceSize.h / resolution
                        );

                        if (frames[i].rotated)
                        {
                            frame = new core.Rectangle(
                                rect.x / resolution,
                                rect.y / resolution,
                                rect.h / resolution,
                                rect.w / resolution
                            );
                        }
                        else
                        {
                            frame = new core.Rectangle(
                                rect.x / resolution,
                                rect.y / resolution,
                                rect.w / resolution,
                                rect.h / resolution
                            );
                        }

                        //  Check to see if the sprite is trimmed
                        if (frames[i].trimmed)
                        {
                            trim = new core.Rectangle(
                                frames[i].spriteSourceSize.x / resolution,
                                frames[i].spriteSourceSize.y / resolution,
                                rect.w / resolution,
                                rect.h / resolution
                            );
                        }

                        resource.textures[i] = new core.Texture(
                            res.texture.baseTexture,
                            frame,<commit_msg>adds support for scale field in spritesheets<commit_after>import { Resource } from 'resource-loader';
import path from 'path';
import * as core from '../core';

const BATCH_SIZE = 1000;

export default function ()
{
    return function spritesheetParser(resource, next)
    {
        let resourcePath;
        const imageResourceName = `${resource.name}_image`;

        // skip if no data, its not json, it isn't spritesheet data, or the image resource already exists
        if (!resource.data || !resource.isJson || !resource.data.frames || this.resources[imageResourceName])
        {
            next();

            return;
        }

        const loadOptions = {
            crossOrigin: resource.crossOrigin,
            loadType: Resource.LOAD_TYPE.IMAGE,
            metadata: resource.metadata.imageMetadata,
        };

        // Prepend url path unless the resource image is a data url
        if (resource.isDataUrl)
        {
            resourcePath = resource.data.meta.image;
        }
        else
        {
            resourcePath = `${path.dirname(resource.url.replace(this.baseUrl, ''))}/${resource.data.meta.image}`;
        }

        // load the image for this sheet
        this.add(imageResourceName, resourcePath, loadOptions, function onImageLoad(res)
        {
            resource.textures = {};

            const frames = resource.data.frames;
            const frameKeys = Object.keys(frames);
            const baseTexture = res.texture.baseTexture;
            let resolution = core.utils.getResolutionOfUrl(resource.url);
            const scale = resource.data.meta.scale;

            // Support scale field on spritesheet
            if (scale !== undefined && scale !== 1)
            {
                baseTexture.resolution = resolution = scale;
                baseTexture.update();
            }

            let batchIndex = 0;

            function processFrames(initialFrameIndex, maxFrames)
            {
                let frameIndex = initialFrameIndex;

                while (frameIndex - initialFrameIndex < maxFrames && frameIndex < frameKeys.length)
                {
                    const i = frameKeys[frameIndex];
                    const rect = frames[i].frame;

                    if (rect)
                    {
                        let frame = null;
                        let trim = null;
                        const orig = new core.Rectangle(
                            0,
                            0,
                            frames[i].sourceSize.w / resolution,
                            frames[i].sourceSize.h / resolution
                        );

                        if (frames[i].rotated)
                        {
                            frame = new core.Rectangle(
                                rect.x / resolution,
                                rect.y / resolution,
                                rect.h / resolution,
                                rect.w / resolution
                            );
                        }
                        else
                        {
                            frame = new core.Rectangle(
                                rect.x / resolution,
                                rect.y / resolution,
                                rect.w / resolution,
                                rect.h / resolution
                            );
                        }

                        //  Check to see if the sprite is trimmed
                        if (frames[i].trimmed)
                        {
                            trim = new core.Rectangle(
                                frames[i].spriteSourceSize.x / resolution,
                                frames[i].spriteSourceSize.y / resolution,
                                rect.w / resolution,
                                rect.h / resolution
                            );
                        }

                        resource.textures[i] = new core.Texture(
                            baseTexture,
                            frame,
{'fraction_non_alphanumeric': 0.07702198791307702, 'fraction_numerical': 0.0019287643050019288, 'mean_word_length': 0.9751142712036567, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2741742', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1639, 'n_words': 405}