/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.l2jmobius.gameserver.scripting;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.l2jmobius.Config;
import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
/**
* Caches script engines and provides functionality for executing and managing scripts.
* @author KenM, HorridoJoho
*/
public final class ScriptEngineManager
{
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName());
public static final Path SCRIPT_FOLDER = Paths.get(Config.DATAPACK_ROOT.getAbsolutePath(), "data", "scripts");
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java");
public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(ScriptEngineManager.SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");
public static final Path CONDITION_HANDLER_FILE = Paths.get(ScriptEngineManager.SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java");
public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "OneDayRewardMasterHandler.java");
private final Map _extEngines = new HashMap<>();
private IExecutionContext _currentExecutionContext = null;
protected ScriptEngineManager()
{
final Properties props = loadProperties();
// Default java engine implementation
registerEngine(new JavaScriptingEngine(), props);
// Load external script engines
ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props));
}
private Properties loadProperties()
{
Properties props = null;
try (FileInputStream fis = new FileInputStream("config/ScriptEngines.ini"))
{
props = new Properties();
props.load(fis);
}
catch (Exception e)
{
props = null;
LOGGER.warning("Couldn't load ScriptEngines.properties: " + e.getMessage());
}
return props;
}
private void registerEngine(IScriptingEngine engine, Properties props)
{
maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine);
final IExecutionContext context = engine.createExecutionContext();
for (String commonExtension : engine.getCommonFileExtensions())
{
_extEngines.put(commonExtension, context);
}
LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")");
}
private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine)
{
if (props == null)
{
return;
}
for (Entry