<commit_before>package com.suiton2d.assets.loaders;


import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.SynchronousAssetLoader;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.suiton2d.assets.JavascriptScript;
import com.suiton2d.assets.Script;
import com.suiton2d.util.ByteUtils;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class ScriptLoader extends SynchronousAssetLoader<Script, ScriptLoader.ScriptParameter>{

    public ScriptLoader(FileHandleResolver resolver) {
        super(resolver);
    }

    @Override
    public Script load(AssetManager assetManager, String fileName, FileHandle file, ScriptParameter parameter) {
        String contents = ByteUtils.decodeBase64String(new String(file.readBytes()));
        if ("js".equalsIgnoreCase(file.extension())) {
            Context context = Context.enter();
            context.setOptimizationLevel(-1);
            try {
                ScriptableObject globalScope = JavascriptScript.getGlobalScope();
                Scriptable scope = context.newObject(globalScope);
                scope.setPrototype(globalScope);
                scope.setParentScope(null);
                context.evaluateString(scope, contents, fileName, 1, null);
                Object startFunction = scope.get("start", scope);
                Object updateFunction = scope.get("update", scope);
                Object finishFunction = scope.get("finish", scope);
                Object beginCollisionFunction = scope.get("beginCollision", scope);
                Object endCollisionFunction = scope.get("endCollision", scope);
                return new JavascriptScript(file.path(),
                        scope,
                        startFunction != Scriptable.NOT_FOUND ? (Function) startFunction : null,
                        updateFunction != Scriptable.NOT_FOUND ? (Function) updateFunction : null,
                        finishFunction != Scriptable.NOT_FOUND ? (Function) finishFunction : null,
                        beginCollisionFunction != Scriptable.NOT_FOUND ? (Function) beginCollisionFunction : null,
                        endCollisionFunction != Scriptable.NOT_FOUND ? (Function) endCollisionFunction : null);
            } finally {
                Context.exit();
            }
        }

        // TODO: Figure out what needs to be done here in case bad input is provided...
        return null;
    }

    @Override<commit_msg>no longer require base64 encoded script files.<commit_after>package com.suiton2d.assets.loaders;


import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.SynchronousAssetLoader;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.suiton2d.assets.JavascriptScript;
import com.suiton2d.assets.Script;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class ScriptLoader extends SynchronousAssetLoader<Script, ScriptLoader.ScriptParameter>{

    public ScriptLoader(FileHandleResolver resolver) {
        super(resolver);
    }

    @Override
    public Script load(AssetManager assetManager, String fileName, FileHandle file, ScriptParameter parameter) {
        String contents = file.readString("UTF-8");
        if ("js".equalsIgnoreCase(file.extension())) {
            Context context = Context.enter();
            context.setOptimizationLevel(-1);
            try {
                ScriptableObject globalScope = JavascriptScript.getGlobalScope();
                Scriptable scope = context.newObject(globalScope);
                scope.setPrototype(globalScope);
                scope.setParentScope(null);
                context.evaluateString(scope, contents, fileName, 1, null);
                Object startFunction = scope.get("start", scope);
                Object updateFunction = scope.get("update", scope);
                Object finishFunction = scope.get("finish", scope);
                Object beginCollisionFunction = scope.get("beginCollision", scope);
                Object endCollisionFunction = scope.get("endCollision", scope);
                return new JavascriptScript(file.path(),
                        scope,
                        startFunction != Scriptable.NOT_FOUND ? (Function) startFunction : null,
                        updateFunction != Scriptable.NOT_FOUND ? (Function) updateFunction : null,
                        finishFunction != Scriptable.NOT_FOUND ? (Function) finishFunction : null,
                        beginCollisionFunction != Scriptable.NOT_FOUND ? (Function) beginCollisionFunction : null,
                        endCollisionFunction != Scriptable.NOT_FOUND ? (Function) endCollisionFunction : null);
            } finally {
                Context.exit();
            }
        }

        // TODO: Figure out what needs to be done here in case bad input is provided...
        return null;
    }

    @Override
{'fraction_non_alphanumeric': 0.09114062215754047, 'fraction_numerical': 0.0029106785519374206, 'mean_word_length': 2.7249322493224932, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3520315', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1340, 'n_words': 290}