2015-05-13 22:02:54 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package appeng.core;
|
|
|
|
|
|
|
|
|
|
|
|
import appeng.api.recipes.IRecipeHandler;
|
2015-08-20 19:15:52 +02:00
|
|
|
import appeng.recipes.CustomRecipeConfig;
|
2015-05-13 22:02:54 +02:00
|
|
|
import appeng.recipes.loader.ConfigLoader;
|
|
|
|
import appeng.recipes.loader.JarLoader;
|
|
|
|
import appeng.recipes.loader.RecipeResourceCopier;
|
2017-04-12 16:10:13 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URISyntaxException;
|
2015-05-13 22:02:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handles the decision if recipes should be loaded from jar, loaded from file system or force copied from jar
|
|
|
|
*
|
|
|
|
* @author thatsIch
|
|
|
|
* @version rv3 - 12.05.2015
|
|
|
|
* @since rv3 12.05.2015
|
|
|
|
*/
|
|
|
|
public class RecipeLoader implements Runnable
|
|
|
|
{
|
2015-08-20 19:15:52 +02:00
|
|
|
/**
|
|
|
|
* recipe path in the jar
|
|
|
|
*/
|
|
|
|
private static final String ASSETS_RECIPE_PATH = "/assets/appliedenergistics2/recipes/";
|
|
|
|
|
|
|
|
@Nonnull
|
2015-05-13 22:02:54 +02:00
|
|
|
private final IRecipeHandler handler;
|
2015-08-20 19:15:52 +02:00
|
|
|
@Nonnull
|
|
|
|
private final CustomRecipeConfig config;
|
|
|
|
@Nonnull
|
|
|
|
private final File recipeDirectory;
|
2015-05-13 22:02:54 +02:00
|
|
|
|
|
|
|
/**
|
2017-04-12 16:10:13 +02:00
|
|
|
* @param config configuration for the knowledge how to handle the loading process
|
2015-05-13 22:02:54 +02:00
|
|
|
* @param handler handler to load the recipes
|
|
|
|
* @throws NullPointerException if handler is <tt>null</tt>
|
|
|
|
*/
|
2015-08-20 19:15:52 +02:00
|
|
|
public RecipeLoader( @Nonnull final File recipeDirectory, @Nonnull final CustomRecipeConfig config, @Nonnull final IRecipeHandler handler )
|
2015-05-13 22:02:54 +02:00
|
|
|
{
|
2015-08-20 19:15:52 +02:00
|
|
|
this.recipeDirectory = Preconditions.checkNotNull( recipeDirectory );
|
|
|
|
Preconditions.checkArgument( !recipeDirectory.isFile() );
|
|
|
|
this.config = Preconditions.checkNotNull( config );
|
|
|
|
this.handler = Preconditions.checkNotNull( handler );
|
2015-05-13 22:02:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-20 19:15:52 +02:00
|
|
|
public final void run()
|
2015-05-13 22:02:54 +02:00
|
|
|
{
|
2015-08-20 19:15:52 +02:00
|
|
|
if( this.config.isEnabled() )
|
2015-05-13 22:02:54 +02:00
|
|
|
{
|
2015-08-20 19:15:52 +02:00
|
|
|
// setup copying
|
|
|
|
final RecipeResourceCopier copier = new RecipeResourceCopier( "assets/appliedenergistics2/recipes/" );
|
2015-05-13 22:02:54 +02:00
|
|
|
|
2015-08-20 19:15:52 +02:00
|
|
|
final File generatedRecipesDir = new File( this.recipeDirectory, "generated" );
|
|
|
|
final File userRecipesDir = new File( this.recipeDirectory, "user" );
|
2015-05-13 22:02:54 +02:00
|
|
|
|
2015-08-20 19:15:52 +02:00
|
|
|
// generates generated and user recipes dir
|
|
|
|
// will clean the generated every time to keep it up to date
|
|
|
|
// copies over the recipes in the jar over to the generated folder
|
|
|
|
// copies over the readmes
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FileUtils.forceMkdir( generatedRecipesDir );
|
|
|
|
FileUtils.forceMkdir( userRecipesDir );
|
|
|
|
FileUtils.cleanDirectory( generatedRecipesDir );
|
|
|
|
|
|
|
|
copier.copyTo( ".recipe", generatedRecipesDir );
|
2015-10-08 15:42:42 +02:00
|
|
|
copier.copyTo( ".html", this.recipeDirectory );
|
2015-08-20 19:15:52 +02:00
|
|
|
|
|
|
|
// parse recipes prioritising the user scripts by using the generated as template
|
|
|
|
this.handler.parseRecipes( new ConfigLoader( generatedRecipesDir, userRecipesDir ), "index.recipe" );
|
|
|
|
}
|
|
|
|
// on failure use jar parsing
|
|
|
|
catch( final IOException e )
|
|
|
|
{
|
2015-11-22 15:54:29 +01:00
|
|
|
AELog.debug( e );
|
2015-08-20 19:15:52 +02:00
|
|
|
this.handler.parseRecipes( new JarLoader( ASSETS_RECIPE_PATH ), "index.recipe" );
|
|
|
|
}
|
|
|
|
catch( final URISyntaxException e )
|
|
|
|
{
|
2015-11-22 15:54:29 +01:00
|
|
|
AELog.debug( e );
|
2015-08-20 19:15:52 +02:00
|
|
|
this.handler.parseRecipes( new JarLoader( ASSETS_RECIPE_PATH ), "index.recipe" );
|
|
|
|
}
|
2015-05-13 22:02:54 +02:00
|
|
|
}
|
2015-08-20 19:15:52 +02:00
|
|
|
else
|
2015-05-13 22:02:54 +02:00
|
|
|
{
|
2015-08-20 19:15:52 +02:00
|
|
|
this.handler.parseRecipes( new JarLoader( ASSETS_RECIPE_PATH ), "index.recipe" );
|
2015-05-13 22:02:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|