Search in sources :

Example 1 with ProfileScript

use of org.orecruncher.dsurround.registry.config.Profiles.ProfileScript in project DynamicSurroundings by OreCruncher.

the class ConfigData method load.

public static ConfigData load() {
    final FastByteArrayOutputStream bits = new FastByteArrayOutputStream(16 * 1024);
    try (final OutputStreamWriter output = new OutputStreamWriter(new GZIPOutputStream(bits))) {
        // We are writing a Json array of objects so start with the open
        output.write("[");
        boolean prependComma = false;
        // Collect the locations where DS data is configured
        final List<IMyResourcePack> packs = ResourcePacks.findResourcePacks();
        final List<ModContainer> activeMods = Loader.instance().getActiveModList();
        // files from the dsurround jar.
        for (final ModContainer mod : activeMods) {
            final ResourceLocation rl = new ResourceLocation(ModInfo.MOD_ID, "data/" + mod.getModId().toLowerCase() + ".json");
            for (final IMyResourcePack p : packs) {
                if (p.resourceExists(rl))
                    prependComma = copy(p, rl, output, "[" + rl.toString() + "] from [" + p.getModName() + "]", prependComma);
            }
        }
        // Get config data from our JAR.
        final ResourceLocation rl = ResourcePacks.CONFIGURE_RESOURCE;
        for (final IMyResourcePack p : packs) {
            if (p.resourceExists(rl))
                prependComma = copy(p, rl, output, "[" + rl.toString() + "] from [" + p.getModName() + "]", prependComma);
        }
        // Built in toggle profiles for turning feature sets on/off
        final List<ProfileScript> resources = Profiles.getProfileStreams();
        for (final ProfileScript script : resources) {
            try (final InputStreamReader reader = new InputStreamReader(script.stream)) {
                prependComma = copy(reader, output, script.packName, prependComma);
            } catch (@Nonnull final Throwable t) {
                ModBase.log().error("Error reading profile script", t);
            }
        }
        // by players or pack makers.
        for (final String cfg : ModOptions.general.externalScriptFiles) {
            final File file = getFileReference(cfg);
            if (file.exists()) {
                ModBase.log().info("Loading external configuration script '%s'", cfg);
                try (final InputStream stream = new FileInputStream(file)) {
                    try (final InputStreamReader input = new InputStreamReader(stream)) {
                        prependComma = copy(input, output, cfg, prependComma);
                    } catch (final Throwable t) {
                        ModBase.log().error(String.format("Unable to load configuration script '%s'", cfg), t);
                    }
                } catch (final Throwable t) {
                    ModBase.log().error(String.format("Unable to load configuration script '%s'", cfg), t);
                }
            }
        }
        // The tap - need to close out the json array and flush
        // the stream to make sure.
        output.write("]");
        output.flush();
    } catch (@Nonnull final Throwable t) {
        ModBase.log().error("Something went horribly wrong", t);
    }
    // dump(bits.toByteArray());
    bits.trim();
    return new ConfigData(bits.array);
}
Also used : ModContainer(net.minecraftforge.fml.common.ModContainer) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) FastByteArrayInputStream(it.unimi.dsi.fastutil.io.FastByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IMyResourcePack(org.orecruncher.dsurround.registry.config.packs.IMyResourcePack) FileInputStream(java.io.FileInputStream) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ResourceLocation(net.minecraft.util.ResourceLocation) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) ProfileScript(org.orecruncher.dsurround.registry.config.Profiles.ProfileScript)

Aggregations

FastByteArrayInputStream (it.unimi.dsi.fastutil.io.FastByteArrayInputStream)1 FastByteArrayOutputStream (it.unimi.dsi.fastutil.io.FastByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 ModContainer (net.minecraftforge.fml.common.ModContainer)1 ProfileScript (org.orecruncher.dsurround.registry.config.Profiles.ProfileScript)1 IMyResourcePack (org.orecruncher.dsurround.registry.config.packs.IMyResourcePack)1