Fixed HTML loading issue.

Author: Zoey76
Source: 3479827c55
This commit is contained in:
MobiusDevelopment 2019-04-08 15:48:31 +00:00
parent e002b6d8d0
commit 117572ff57
97 changed files with 748 additions and 1722 deletions

View File

@ -242,8 +242,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -737,6 +737,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1232,6 +1233,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -411,7 +411,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12802,7 +12802,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12833,7 +12833,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2760,7 +2760,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -744,6 +744,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1239,6 +1240,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -413,7 +413,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12809,7 +12809,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12840,7 +12840,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2762,7 +2762,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -745,6 +745,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1247,6 +1248,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -415,7 +415,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12811,7 +12811,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12842,7 +12842,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2763,7 +2763,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -738,6 +738,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1240,6 +1241,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -420,7 +420,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12794,7 +12794,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12825,7 +12825,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2763,7 +2763,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -733,6 +733,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1235,6 +1236,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -418,7 +418,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12784,7 +12784,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12815,7 +12815,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2763,7 +2763,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -733,6 +733,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1235,6 +1236,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -418,7 +418,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12784,7 +12784,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12815,7 +12815,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2763,7 +2763,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -734,6 +734,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1242,6 +1243,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -418,7 +418,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12790,7 +12790,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12821,7 +12821,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2763,7 +2763,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -235,7 +235,7 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = False LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -221,7 +221,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -874,6 +874,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1404,6 +1405,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -29,10 +29,10 @@ import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
@ -40,7 +40,7 @@ public class HtmCache
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -61,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,19 +114,18 @@ public class HtmCache
return null; return null;
} }
final String relpath = Util.getRelativePath(Config.DATAPACK_ROOT, file);
String content = null; String content = null;
try (FileInputStream fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis)) BufferedInputStream bis = new BufferedInputStream(fis))
{ {
final int bytes = bis.available(); final int bytes = bis.available();
final byte[] raw = new byte[bytes]; byte[] raw = new byte[bytes];
bis.read(raw); bis.read(raw);
content = new String(raw, "UTF-8"); content = new String(raw, "UTF-8");
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
final String oldContent = _cache.put(relpath, content); final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
if (oldContent == null) if (oldContent == null)
{ {
_bytesBuffLen += bytes; _bytesBuffLen += bytes;
@ -139,63 +138,36 @@ public class HtmCache
} }
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file " + e.getMessage(), e); LOGGER.log(Level.WARNING, "Problem with htm file:", e);
}
return content;
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{
content = "<html><body>My text is missing:<br>" + path + "</body></html>";
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -209,11 +181,11 @@ public class HtmCache
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -844,12 +844,9 @@ public class Npc extends Creature
return temp; return temp;
} }
} }
else else if (HtmCache.getInstance().isLoadable(temp))
{ {
if (HtmCache.getInstance().isLoadable(temp)) return temp;
{
return temp;
}
} }
// If the file is not found, the standard message "I have nothing to say to you" is returned // If the file is not found, the standard message "I have nothing to say to you" is returned

View File

@ -415,7 +415,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -13718,7 +13718,7 @@ public final class PlayerInstance extends Playable
public String getHtmlPrefix() public String getHtmlPrefix()
{ {
return Config.MULTILANG_ENABLE ? _htmlPrefix : null; return Config.MULTILANG_ENABLE ? _htmlPrefix : "";
} }
public String getLang() public String getLang()
@ -13746,7 +13746,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2491,7 +2491,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
@ -201,7 +201,7 @@ public final class ScriptEngineManager implements IXmlReader
final Map<Path, Throwable> invokationErrors = _javaExecutionContext.executeScripts(files); final Map<Path, Throwable> invokationErrors = _javaExecutionContext.executeScripts(files);
for (Entry<Path, Throwable> entry : invokationErrors.entrySet()) for (Entry<Path, Throwable> entry : invokationErrors.entrySet())
{ {
LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue());
} }
} }

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -742,6 +742,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1179,6 +1180,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -412,7 +412,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12644,7 +12644,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12675,7 +12675,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2721,7 +2721,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -742,6 +742,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1183,6 +1184,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -412,7 +412,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12651,7 +12651,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12682,7 +12682,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2721,7 +2721,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -742,6 +742,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1183,6 +1184,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -410,7 +410,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12636,7 +12636,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12667,7 +12667,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2721,7 +2721,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");

View File

@ -246,8 +246,8 @@ ForceInventoryUpdate = False
# True = Load html's into cache only on first time html is requested. # True = Load html's into cache only on first time html is requested.
# False = Load all html's into cache on server startup. # False = Load all html's into cache on server startup.
# Default: True # Default: False
LazyCache = True LazyCache = False
# Cache all character names in to memory on server startup # Cache all character names in to memory on server startup
# False - names are loaded from Db when they are requested # False - names are loaded from Db when they are requested

View File

@ -94,6 +94,9 @@ AcceptAlternateID = True
# Default: . # Default: .
DatapackRoot = . DatapackRoot = .
# Scripts root directory.
ScriptRoot = ./data/scripts
# Define how many players are allowed to play simultaneously on your server. # Define how many players are allowed to play simultaneously on your server.
# Default: 2000 # Default: 2000
MaximumOnlineUsers = 2000 MaximumOnlineUsers = 2000

View File

@ -220,7 +220,7 @@ public final class AdminPForge implements IAdminCommandHandler
private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format) private void showValuesPage(PlayerInstance activeChar, String[] opCodes, String format)
{ {
String sendBypass = null; String sendBypass = null;
String valuesHtml = HtmCache.getInstance().getHtmForce(activeChar, "data/html/admin/pforge/values.htm"); String valuesHtml = HtmCache.getInstance().getHtm(activeChar, "data/html/admin/pforge/values.htm");
if (opCodes.length == 3) if (opCodes.length == 3)
{ {
valuesHtml = valuesHtml.replace("%opformat%", "chd"); valuesHtml = valuesHtml.replace("%opformat%", "chd");

View File

@ -742,6 +742,7 @@ public final class Config
public static String CLAN_NAME_TEMPLATE; public static String CLAN_NAME_TEMPLATE;
public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT; public static int MAX_CHARACTERS_NUMBER_PER_ACCOUNT;
public static File DATAPACK_ROOT; public static File DATAPACK_ROOT;
public static File SCRIPT_ROOT;
public static boolean ACCEPT_ALTERNATE_ID; public static boolean ACCEPT_ALTERNATE_ID;
public static int REQUEST_ID; public static int REQUEST_ID;
public static boolean RESERVE_HOST_ON_LOGIN = false; public static boolean RESERVE_HOST_ON_LOGIN = false;
@ -1183,6 +1184,16 @@ public final class Config
DATAPACK_ROOT = new File("."); DATAPACK_ROOT = new File(".");
} }
try
{
SCRIPT_ROOT = new File(serverSettings.getString("ScriptRoot", "./data/scripts").replaceAll("\\\\", "/")).getCanonicalFile();
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Error setting script root!", e);
SCRIPT_ROOT = new File(".");
}
Pattern charNamePattern; Pattern charNamePattern;
try try

View File

@ -16,36 +16,31 @@
*/ */
package com.l2jmobius.gameserver.cache; package com.l2jmobius.gameserver.cache;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.io.FileInputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.commons.util.file.filter.HTMLFilter; import com.l2jmobius.commons.util.file.filter.HTMLFilter;
import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import com.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import com.l2jmobius.gameserver.util.BuilderUtil; import com.l2jmobius.gameserver.util.BuilderUtil;
import com.l2jmobius.gameserver.util.Util;
/** /**
* @author Layane * @author Layane
* @author Zoey76
*/ */
public class HtmCache public class HtmCache
{ {
private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName()); private static final Logger LOGGER = Logger.getLogger(HtmCache.class.getName());
private static final HTMLFilter HTML_FILTER = new HTMLFilter(); private static final HTMLFilter HTML_FILTER = new HTMLFilter();
private static final Pattern EXTEND_PATTERN = Pattern.compile("<extend template=\"([a-zA-Z0-9-_./\\ ]*)\">(.*?)</extend>", Pattern.DOTALL);
private static final Pattern ABSTRACT_BLOCK_PATTERN = Pattern.compile("<abstract block=\"([a-zA-Z0-9-_. ]*)\" ?/>", Pattern.DOTALL);
private static final Pattern BLOCK_PATTERN = Pattern.compile("<block name=\"([a-zA-Z0-9-_. ]*)\">(.*?)</block>", Pattern.DOTALL);
private static final Map<String, String> _cache = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>(); private static final Map<String, String> HTML_CACHE = Config.LAZY_CACHE ? new ConcurrentHashMap<>() : new HashMap<>();
private int _loadedFiles; private int _loadedFiles;
private long _bytesBuffLen; private long _bytesBuffLen;
@ -66,14 +61,14 @@ public class HtmCache
{ {
LOGGER.info("Html cache start..."); LOGGER.info("Html cache start...");
parseDir(f); parseDir(f);
LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded"); LOGGER.info("Cache[HTML]: " + String.format("%.3f", getMemoryUsage()) + " megabytes on " + _loadedFiles + " files loaded.");
} }
else else
{ {
_cache.clear(); HTML_CACHE.clear();
_loadedFiles = 0; _loadedFiles = 0;
_bytesBuffLen = 0; _bytesBuffLen = 0;
LOGGER.info("Cache[HTML]: Running lazy cache"); LOGGER.info("Cache[HTML]: Running lazy cache.");
} }
} }
@ -114,86 +109,65 @@ public class HtmCache
public String loadFile(File file) public String loadFile(File file)
{ {
if (HTML_FILTER.accept(file)) if (!HTML_FILTER.accept(file))
{ {
try return null;
{ }
String content = processHtml(Util.readAllLines(file, StandardCharsets.UTF_8, null));
content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
// content = content.replaceAll("\r", "").replaceAll("\n", ""); // Remove new lines
final String oldContent = _cache.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content); String content = null;
if (oldContent == null) try (FileInputStream fis = new FileInputStream(file);
{ BufferedInputStream bis = new BufferedInputStream(fis))
_bytesBuffLen += content.length() * 2; {
_loadedFiles++; final int bytes = bis.available();
} byte[] raw = new byte[bytes];
else
{ bis.read(raw);
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + (content.length() * 2); content = new String(raw, "UTF-8");
} content = content.replaceAll("(?s)<!--.*?-->", ""); // Remove html comments
return content;
} final String oldContent = HTML_CACHE.put(file.toURI().getPath().substring(Config.DATAPACK_ROOT.toURI().getPath().length()), content);
catch (Exception e) if (oldContent == null)
{ {
LOGGER.log(Level.WARNING, "Problem with htm file:", e); _bytesBuffLen += bytes;
_loadedFiles++;
}
else
{
_bytesBuffLen = (_bytesBuffLen - oldContent.length()) + bytes;
} }
} }
return null; catch (Exception e)
}
public String getHtmForce(PlayerInstance player, String path)
{
String content = getHtm(player, path);
if (content == null)
{ {
content = "<html><body>My text is missing:<br>" + path + "</body></html>"; LOGGER.log(Level.WARNING, "Problem with htm file:", e);
LOGGER.warning("Cache[HTML]: Missing HTML page: " + path);
} }
return content; return content;
} }
public String getHtm(PlayerInstance player, String path) public String getHtm(PlayerInstance player, String path)
{ {
final String prefix = player != null ? player.getHtmlPrefix() : "en"; final String prefix = player != null ? player.getHtmlPrefix() : "";
String newPath = null; final String newPath = prefix + path;
String content; String content = HTML_CACHE.get(newPath);
if ((prefix != null) && !prefix.isEmpty()) if (Config.LAZY_CACHE && (content == null))
{ {
newPath = prefix + path; content = loadFile(new File(Config.DATAPACK_ROOT, newPath));
content = getHtm(newPath); if (content == null)
if (content != null)
{ {
if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS) content = loadFile(new File(Config.SCRIPT_ROOT, newPath));
{
BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
}
return content;
} }
} }
content = getHtm(path); if ((player != null) && player.isGM() && Config.GM_DEBUG_HTML_PATHS)
if ((content != null) && (newPath != null))
{ {
_cache.put(newPath, content); BuilderUtil.sendHtmlMessage(player, newPath.substring(5));
} }
if ((player != null) && player.isGM() && (path != null) && Config.GM_DEBUG_HTML_PATHS)
{
BuilderUtil.sendHtmlMessage(player, path.substring(5));
}
return content; return content;
} }
private String getHtm(String path)
{
// TODO: Check why some files do not get in cache on server startup.
return (path == null) || path.isEmpty() ? "" : _cache.get(path) == null ? loadFile(new File(Config.DATAPACK_ROOT, path)) : _cache.get(path);
}
public boolean contains(String path) public boolean contains(String path)
{ {
return _cache.containsKey(path); return HTML_CACHE.containsKey(path);
} }
/** /**
@ -205,88 +179,13 @@ public class HtmCache
return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path)); return HTML_FILTER.accept(new File(Config.DATAPACK_ROOT, path));
} }
private String parseTemplateName(String name)
{
if (!name.startsWith("data/"))
{
if (name.startsWith("html/"))
{
return "data/" + name;
}
else if (name.startsWith("CommunityBoard/"))
{
return "data/html/" + name;
}
else if (name.startsWith("scripts/"))
{
return "data/scripts/" + name;
}
}
return name;
}
private String processHtml(String result)
{
final Matcher extendMatcher = EXTEND_PATTERN.matcher(result);
if (extendMatcher.find())
{
// If extend matcher finds something, process template
final String templateName = parseTemplateName(extendMatcher.group(1));
// Generate block name -> content map
final Map<String, String> blockMap = generateBlockMap(result);
// Attempt to find the template
String template = getHtm(templateName + "-template.htm");
if (template != null)
{
// Attempt to find the abstract blocks
final Matcher blockMatcher = ABSTRACT_BLOCK_PATTERN.matcher(template);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
if (!blockMap.containsKey(name))
{
LOGGER.warning(getClass().getSimpleName() + ": Abstract block definition [" + name + "] is not implemented!");
continue;
}
// Replace the matched content with the block.
template = template.replace(blockMatcher.group(0), blockMap.get(name));
}
// Replace the entire extend block
result = result.replace(extendMatcher.group(0), template);
}
else
{
LOGGER.warning(getClass().getSimpleName() + ": Missing template: " + templateName + "-template.htm !");
}
}
return result;
}
private Map<String, String> generateBlockMap(String data)
{
final Map<String, String> blockMap = new LinkedHashMap<>();
final Matcher blockMatcher = BLOCK_PATTERN.matcher(data);
while (blockMatcher.find())
{
final String name = blockMatcher.group(1);
final String content = blockMatcher.group(2);
blockMap.put(name, content);
}
return blockMap;
}
public static HtmCache getInstance() public static HtmCache getInstance()
{ {
return SingletonHolder._instance; return SingletonHolder.INSTANCE;
} }
private static class SingletonHolder private static class SingletonHolder
{ {
protected static final HtmCache _instance = new HtmCache(); protected static final HtmCache INSTANCE = new HtmCache();
} }
} }

View File

@ -410,7 +410,7 @@ public final class PlayerInstance extends Playable
private Calendar _createDate = Calendar.getInstance(); private Calendar _createDate = Calendar.getInstance();
private String _lang = null; private String _lang = null;
private String _htmlPrefix = null; private String _htmlPrefix = "";
private volatile boolean _isOnline = false; private volatile boolean _isOnline = false;
private long _onlineTime; private long _onlineTime;
@ -12636,7 +12636,7 @@ public final class PlayerInstance extends Playable
{ {
if (!Config.MULTILANG_ENABLE) if (!Config.MULTILANG_ENABLE)
{ {
return null; return "";
} }
return _htmlPrefix; return _htmlPrefix;
@ -12667,7 +12667,7 @@ public final class PlayerInstance extends Playable
else else
{ {
_lang = null; _lang = null;
_htmlPrefix = null; _htmlPrefix = "";
} }
return result; return result;

View File

@ -2721,7 +2721,7 @@ public class Quest extends AbstractScript implements IIdentifiable
content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName); content = hc.getHtm(player, "data/scripts/" + getPath() + "/" + fileName);
if (content == null) if (content == null)
{ {
content = hc.getHtmForce(player, "data/scripts/quests/" + getName() + "/" + fileName); content = hc.getHtm(player, "data/scripts/quests/" + getName() + "/" + fileName);
} }
} }
return content; return content;

View File

@ -50,7 +50,7 @@ import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
public final class ScriptEngineManager implements IXmlReader public final class ScriptEngineManager implements IXmlReader
{ {
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); 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 SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath();
public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); 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 EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java");
public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");