Addition of Scripts.xml for easier scripts management.
Contributed by Sahar.
This commit is contained in:
@ -18,10 +18,16 @@ package com.l2jmobius.gameserver.scripting;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -33,14 +39,17 @@ import java.util.ServiceLoader;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.commons.util.IGameXmlReader;
|
||||
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
|
||||
public final class ScriptEngineManager implements IGameXmlReader
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName());
|
||||
public static final Path SCRIPT_FOLDER = Paths.get(Config.DATAPACK_ROOT.getAbsolutePath(), "data", "scripts");
|
||||
@ -52,6 +61,7 @@ public final class ScriptEngineManager
|
||||
|
||||
private final Map<String, IExecutionContext> _extEngines = new HashMap<>();
|
||||
private IExecutionContext _currentExecutionContext = null;
|
||||
static final List<String> _exclusions = new ArrayList<>();
|
||||
|
||||
protected ScriptEngineManager()
|
||||
{
|
||||
@ -62,6 +72,79 @@ public final class ScriptEngineManager
|
||||
|
||||
// Load external script engines
|
||||
ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props));
|
||||
|
||||
// Load Scripts.xml
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load()
|
||||
{
|
||||
_exclusions.clear();
|
||||
parseDatapackFile("config/Scripts.xml");
|
||||
LOGGER.info("Loaded " + _exclusions.size() + " files to exclude.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parseDocument(Document doc, File f)
|
||||
{
|
||||
try
|
||||
{
|
||||
final Map<String, List<String>> excludePaths = new HashMap<>();
|
||||
forEach(doc, "list", listNode -> forEach(listNode, "exclude", excludeNode ->
|
||||
{
|
||||
final String excludeFile = parseString(excludeNode.getAttributes(), "file");
|
||||
excludePaths.putIfAbsent(excludeFile, new ArrayList<>());
|
||||
|
||||
forEach(excludeNode, "include", includeNode -> excludePaths.get(excludeFile).add(parseString(includeNode.getAttributes(), "file")));
|
||||
}));
|
||||
|
||||
final int nameCount = SCRIPT_FOLDER.getNameCount();
|
||||
Files.walkFileTree(SCRIPT_FOLDER, new SimpleFileVisitor<Path>()
|
||||
{
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
|
||||
{
|
||||
final String fileName = file.getFileName().toString();
|
||||
if (fileName.endsWith(".java"))
|
||||
{
|
||||
final Iterator<Path> relativePath = file.subpath(nameCount, file.getNameCount()).iterator();
|
||||
while (relativePath.hasNext())
|
||||
{
|
||||
final String nextPart = relativePath.next().toString();
|
||||
if (excludePaths.containsKey(nextPart))
|
||||
{
|
||||
boolean excludeScript = true;
|
||||
|
||||
final List<String> includePath = excludePaths.get(nextPart);
|
||||
if (includePath != null)
|
||||
{
|
||||
while (relativePath.hasNext())
|
||||
{
|
||||
if (includePath.contains(relativePath.next().toString()))
|
||||
{
|
||||
excludeScript = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (excludeScript)
|
||||
{
|
||||
_exclusions.add(fileName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return super.visitFile(file, attrs);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (final IOException e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, "Couldn't load script exclusions.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Properties loadProperties()
|
||||
@ -222,17 +305,9 @@ public final class ScriptEngineManager
|
||||
|
||||
private void processFile(File file, Map<IExecutionContext, List<Path>> files)
|
||||
{
|
||||
switch (file.getName())
|
||||
if (_exclusions.contains(file.getName()))
|
||||
{
|
||||
case "package-info.java":
|
||||
case "MasterHandler.java":
|
||||
case "EffectMasterHandler.java":
|
||||
case "SkillConditionMasterHandler.java":
|
||||
case "ConditionMasterHandler.java":
|
||||
case "DailyMissionMasterHandler.java":
|
||||
{
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Path sourceFile = file.toPath();
|
||||
|
Reference in New Issue
Block a user