All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.neoforged.art.internal.RenamerBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) Forge Development LLC and contributors
 * SPDX-License-Identifier: LGPL-2.1-only
 */

package net.neoforged.art.internal;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import net.neoforged.art.api.ClassProvider;
import net.neoforged.art.api.Renamer;
import net.neoforged.art.api.Renamer.Builder;
import net.neoforged.art.api.Transformer;
import net.neoforged.srgutils.IMappingFile;

import static java.util.Objects.requireNonNull;

public class RenamerBuilder implements Builder {
    private final List libraries = new ArrayList<>();
    private final List classProviders = new ArrayList<>();
    private final List transformerFactories = new ArrayList<>();
    private int threads = Runtime.getRuntime().availableProcessors();
    private boolean withJvmClasspath = false;
    private Consumer logger = System.out::println;
    private Consumer debug = s -> {};
    private boolean collectAbstractParams = true;

    @Override
    public Builder lib(File value) {
        this.libraries.add(value);
        return this;
    }

    @Override
    public Builder map(File value) {
        try {
            add(Transformer.renamerFactory(IMappingFile.load(value), collectAbstractParams));
        } catch (IOException e) {
            throw new RuntimeException("Could not map file: " + value.getAbsolutePath(), e);
        }
        return this;
    }

    @Override
    public Builder addClassProvider(ClassProvider classProvider) {
        this.classProviders.add(classProvider);
        return this;
    }

    @Override
    public Builder withJvmClasspath() {
        // We use a property to ensure the JVM classpath is always added last
        this.withJvmClasspath = true;
        return this;
    }

    @Override
    public Builder add(Transformer value) {
        this.transformerFactories.add(Transformer.Factory.always(requireNonNull(value, "value")));
        return this;
    }

    @Override
    public Builder add(Transformer.Factory factory) {
        this.transformerFactories.add(requireNonNull(factory, "factory"));
        return this;
    }

    @Override
    public Builder threads(int value) {
        this.threads = value;
        return this;
    }

    @Override
    public Builder logger(Consumer out) {
        this.logger = requireNonNull(out, "out");
        return this;
    }

    @Override
    public Builder debug(Consumer debug) {
        this.debug = requireNonNull(debug, "debug");
        return this;
    }

    @Override
    public Builder setCollectAbstractParams(boolean collectAbstractParams) {
        this.collectAbstractParams = collectAbstractParams;
        return this;
    }

    @Override
    public Renamer build() {
        List classProviders = new ArrayList<>(this.classProviders);
        if (this.withJvmClasspath)
            classProviders.add(ClassProvider.fromJvmClasspath());

        SortedClassProvider sortedClassProvider = new SortedClassProvider(classProviders, this.logger);
        final Transformer.Context ctx = new Transformer.Context() {
            @Override
            public Consumer getLog() {
                return logger;
            }

            @Override
            public Consumer getDebug() {
                return debug;
            }

            @Override
            public ClassProvider getClassProvider() {
                return sortedClassProvider;
            }
        };

        final List transformers = new ArrayList<>(transformerFactories.size());
        for (Transformer.Factory factory : transformerFactories) {
            transformers.add(requireNonNull(factory.create(ctx), "output of " + factory));
        }
        return new RenamerImpl(libraries, transformers, sortedClassProvider, classProviders, threads, logger, debug);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy