<commit_before>package com.cardshifter.core.groovy

import com.cardshifter.core.modloader.GroovyModInterface
import com.cardshifter.core.modloader.ECSModTest
import com.cardshifter.modapi.base.ECSGame
import org.codehaus.groovy.control.CompilerConfiguration

class GroovyRunner implements GroovyModInterface {

    final String name
    final File modDirectory
    private GroovyMod groovyMod
    final ClassLoader classLoader

    GroovyRunner(File dir, String name, ClassLoader cl) {
        this.name = name
        this.modDirectory = dir
        this.classLoader = cl
    }

    @Override
    void declareConfiguration(ECSGame game) {
        Binding binding = new Binding()
        this.groovyMod = new GroovyMod(loader: classLoader, modDirectory: modDirectory, binding: binding)

        File file = new File(modDirectory, "Game.groovy")
        CompilerConfiguration cc = new CompilerConfiguration()
        cc.setScriptBaseClass(DelegatingScript.class.getName())
        GroovyShell sh = new GroovyShell(classLoader, groovyMod.binding, cc)
        DelegatingScript script = (DelegatingScript) sh.parse(file)
        script.setDelegate(groovyMod)
        script.run()
        groovyMod.declareConfiguration(game)
    }

    @Override
    void setupGame(ECSGame game) {
        groovyMod.setupGame(game)
    }

    @Override
    List<ECSModTest> getTests() {
        List<ECSModTest> result = new ArrayList<>();
        Binding binding = new Binding()
        def file = new File(modDirectory, "test.groovy")
        if (!file.exists()) {
            return null
        }

        def delegate = new TestDelegate(tests: result, mod: this)
        CompilerConfiguration cc = new CompilerConfiguration()
        cc.setScriptBaseClass(DelegatingScript.class.getName())
        GroovyShell sh = new GroovyShell(classLoader, binding, cc)
        DelegatingScript script = (DelegatingScript) sh.parse(file)
        script.setDelegate(delegate)
        script.run()
        return result
    }

    public CardDelegate getCardDelegate() {
        return groovyMod.cardDelegate
    }

    public GroovyMod getGroovyMod() {
        return groovyMod
    }
}<commit_msg>Use ScriptRunner class instead of copy-paste<commit_after>package com.cardshifter.core.groovy

import com.cardshifter.core.modloader.GroovyModInterface
import com.cardshifter.core.modloader.ECSModTest
import com.cardshifter.modapi.base.ECSGame
import org.codehaus.groovy.control.CompilerConfiguration

class GroovyRunner implements GroovyModInterface {

    final String name
    final File modDirectory
    private GroovyMod groovyMod
    final ClassLoader classLoader

    GroovyRunner(File dir, String name, ClassLoader cl) {
        this.name = name
        this.modDirectory = dir
        this.classLoader = cl
    }

    @Override
    void declareConfiguration(ECSGame game) {
        def scriptRunner = new ScriptRunner(classLoader, new Binding())
        this.groovyMod = new GroovyMod(loader: classLoader, modDirectory: modDirectory, binding: scriptRunner.binding)

        File file = new File(modDirectory, "Game.groovy")
        scriptRunner.runScript(file, groovyMod)
        groovyMod.declareConfiguration(game)
    }

    @Override
    void setupGame(ECSGame game) {
        groovyMod.setupGame(game)
    }

    @Override
    List<ECSModTest> getTests() {
        List<ECSModTest> result = new ArrayList<>();

        def file = new File(modDirectory, "test.groovy")
        if (!file.exists()) {
            return null
        }

        def delegate = new TestDelegate(tests: result, mod: this)
        def scriptRunner = new ScriptRunner(classLoader, new Binding())
        scriptRunner.runScript(file, delegate)

        return result
    }

    public CardDelegate getCardDelegate() {
        return groovyMod.cardDelegate
    }

    public GroovyMod getGroovyMod() {
        return groovyMod
    }
}

class ScriptRunner {

    final ClassLoader classLoader
    final Binding binding

    ScriptRunner(ClassLoader cl, Binding binding) {
        this.classLoader = cl
        this.binding = binding
    }

    void runScript(File file, delegate) {
        CompilerConfiguration config = new CompilerConfiguration()
        config.setScriptBaseClass(DelegatingScript.class.getName())
        GroovyShell shell = new GroovyShell(classLoader, binding, config)
        DelegatingScript script = (DelegatingScript) shell.parse(file)
        script.setDelegate(delegate)
        script.run()
    }

}
{'fraction_non_alphanumeric': 0.07855389422004017, 'fraction_numerical': 0.0, 'mean_word_length': 3.0017857142857145, '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': '3439422', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1214, 'n_words': 292}