20 package net.sf.gridarta.model.treasurelist;
22 import java.io.BufferedReader;
24 import java.io.FileInputStream;
25 import java.io.FilenameFilter;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.Reader;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Comparator;
34 import java.util.List;
36 import javax.swing.tree.DefaultMutableTreeNode;
37 import javax.swing.tree.MutableTreeNode;
38 import javax.swing.tree.TreeNode;
45 import net.
sf.japi.util.EnumerationIterator;
46 import org.apache.log4j.Category;
47 import org.apache.log4j.Logger;
48 import org.jetbrains.annotations.NotNull;
49 import org.jetbrains.annotations.Nullable;
73 return String.CASE_INSENSITIVE_ORDER.compare(o1.getTreasureObj().getName(), o2.getTreasureObj().getName());
85 public boolean accept(@NotNull
final File dir, @NotNull
final String name) {
86 final File fullPath =
new File(dir, name);
87 if (name.startsWith(
".")) {
90 if (fullPath.isDirectory()) {
93 final String lowerCaseName = name.toLowerCase();
94 return lowerCaseName.endsWith(
".tl") || lowerCaseName.endsWith(
".trs");
115 final List<TreasureTreeNode> tmpList =
new ArrayList<>();
116 final List<TreasureTreeNode> needLink =
new ArrayList<>();
121 final File treasureLocation;
123 treasureLocation = configSource.getFile(projectSettings,
"treasures", index);
124 }
catch (
final IOException ex) {
129 if (treasureLocation == null) {
133 final int tmpListSize = tmpList.size();
134 if (treasureLocation.isDirectory()) {
136 }
else if (treasureLocation.isFile()) {
141 if (LOG.isInfoEnabled()) {
142 LOG.info(
"Loaded " + (tmpList.size() - tmpListSize) +
" treasurelists from '" + treasureLocation +
"'.");
159 private static TreasureTree createTreasureTree(@NotNull
final List<TreasureTreeNode> tmpList, @NotNull
final Iterable<TreasureTreeNode> needLink, @NotNull
final Map<String, TreasureTreeNode> specialTreasureLists) {
160 final DefaultMutableTreeNode root =
new DefaultMutableTreeNode(
"Treasurelists:");
162 treasures.
putAll(tmpList);
177 private static void addTopLevelEntries(@NotNull
final List<TreasureTreeNode> tmpList, @NotNull
final Map<String, TreasureTreeNode> specialTreasureLists, @NotNull
final DefaultMutableTreeNode root) {
178 tmpList.sort(TREASURE_NODE_COMPARATOR);
183 realNode.recalculateChances();
186 final DefaultMutableTreeNode specialTreasureList = specialTreasureLists.get(realNode.getTreasureObj().getName());
187 if (specialTreasureList != null) {
188 specialTreasureList.add(realNode);
201 final List<TreasureTreeNode> needSecondLink =
new ArrayList<>();
202 linkSubList2(needLink, treasures,
false, needSecondLink);
206 private static void linkSubList2(@NotNull
final Iterable<TreasureTreeNode> needLink, @NotNull
final TreasureTree treasures,
final boolean processSecondLinking, @Nullable
final List<TreasureTreeNode> needSecondLink) {
209 if (realNode != null) {
212 for (
final TreeNode ttn :
new EnumerationIterator<TreeNode>(realNode.children())) {
213 node.add(((
TreasureTreeNode) ttn).getClone(processSecondLinking, needSecondLink));
227 return treasures.get(node.getTreasureObj().getName());
236 private static void addSpecialEntries(@NotNull
final Map<String, TreasureTreeNode> specialTreasureLists, @NotNull
final DefaultMutableTreeNode root) {
237 for (
final MutableTreeNode folder : specialTreasureLists.values()) {
251 private static void loadTreasureList(@NotNull
final ErrorView errorView, @NotNull
final File file,
final Collection<TreasureTreeNode> tmpList,
final List<TreasureTreeNode> needLink) {
254 try (InputStream inputStream =
new FileInputStream(file)) {
256 try (BufferedReader bufferedReader =
new BufferedReader(reader)) {
258 final String rawLine = bufferedReader.readLine();
259 if (rawLine == null) {
262 final String line = rawLine.trim();
263 if (!line.isEmpty() && !line.startsWith(
"#")) {
264 if (line.startsWith(
"treasure")) {
265 final int i = line.indexOf(
' ');
269 final String name = line.substring(i).trim();
273 readInsideList(errorViewCollector, node, bufferedReader, needLink);
283 }
catch (
final IOException ex) {
297 private static void loadTreasureDir(@NotNull
final ErrorView errorView, @NotNull
final File dir, @NotNull
final List<TreasureTreeNode> tmpList, @NotNull
final List<TreasureTreeNode> needLink) {
298 final String[] traverse = dir.list(TREASURE_LIST_FILTER);
299 if (traverse == null) {
303 Arrays.sort(traverse);
304 for (
final String entry : traverse) {
305 final File file =
new File(dir, entry);
308 }
else if (file.isDirectory()) {
327 boolean insideArch =
false;
330 final String rawLine = reader.readLine();
331 if (rawLine == null) {
334 final String line = rawLine.trim();
335 if (line.equals(
"end")) {
338 if (!line.isEmpty() && !line.startsWith(
"#")) {
340 if (line.equals(
"more")) {
342 }
else if (line.startsWith(
"chance")) {
345 }
catch (
final NumberFormatException ignored) {
348 }
else if (line.startsWith(
"nrof")) {
351 }
catch (
final NumberFormatException ignored) {
354 }
else if (line.startsWith(
"magic")) {
357 }
catch (
final NumberFormatException ignored) {
360 }
else if (line.startsWith(
"artifact_chance") || line.startsWith(
"title") || line.startsWith(
"difficulty") || line.startsWith(
"quality_quality") || line.startsWith(
"quality_range") || line.startsWith(
"material_quality") || line.startsWith(
"material_range")) {
362 }
else if (line.startsWith(
"name") || line.startsWith(
"t_style") || line.startsWith(
"affinity")) {
364 }
else if (line.equals(
"no")) {
371 chance = 100 - parentChance;
378 }
else if (line.equals(
"yes")) {
386 if (line.startsWith(
"arch ")) {
388 parentNode.add(node);
390 }
else if (line.startsWith(
"list ")) {
391 final String newName = line.substring(line.indexOf(
' ')).trim();
393 parentNode.add(node);
A TreasureObj representing a "no" entry.
TreasureLoader()
Private constructor to prevent instantiation.
Convenience class for adding messages to a ErrorView instance using a fixed category name...
void setNrof(final int nrof)
Sets the maximum number of generated items.
String getName()
Returns the name of this treasure object.
Settings that apply to a project.
static final String MAP_ENCODING
Encoding to use for maps and other data.
void addWarning(@NotNull final ErrorViewCategory category)
Adds a warning message.
TreasureObj getTreasureObj()
static void addTopLevelEntries(@NotNull final List< TreasureTreeNode > tmpList, @NotNull final Map< String, TreasureTreeNode > specialTreasureLists, @NotNull final DefaultMutableTreeNode root)
Adds a list of TreasureTreeNodes to a root DefaultMutableTreeNode.
static void loadTreasureList(@NotNull final ErrorView errorView, @NotNull final File file, final Collection< TreasureTreeNode > tmpList, final List< TreasureTreeNode > needLink)
Parses one treasurelist file.
void setHasLoop(final boolean hasLoop)
Sets whether this treasure object contains itself.
Defines possible error categories for ErrorView instances.
Interface for classes displaying error messages.
Base package of all Gridarta classes.
Utility-class for Gridarta's I/O.
Possible source locations for configuration files.
static void readInsideList(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final TreasureTreeNode parentNode, @NotNull final BufferedReader reader, @NotNull final List< TreasureTreeNode > needLink)
Reads and parses the text inside a treasurelist definition.
final boolean isTreasureList
Whether this treasure object is a "treasure" or "treasureone" object.
static final FilenameFilter TREASURE_LIST_FILTER
The FilenameFilter for recursively loading treasure list files.
static void addSpecialEntries(@NotNull final Map< String, TreasureTreeNode > specialTreasureLists, @NotNull final DefaultMutableTreeNode root)
Add the special treasure list parents to the root DefaultMutableTreeNode.
static void linkSubList2(@NotNull final Iterable< TreasureTreeNode > needLink, @NotNull final TreasureTree treasures, final boolean processSecondLinking, @Nullable final List< TreasureTreeNode > needSecondLink)
static void loadTreasureDir(@NotNull final ErrorView errorView, @NotNull final File dir, @NotNull final List< TreasureTreeNode > tmpList, @NotNull final List< TreasureTreeNode > needLink)
Recursively traverses a directory and parses all treasurelist files.
Type of TreasureListTreasureObj instances.
A TreasureObj representing an "arch" entry.
A TreasureObj representing a "treasure" entry.
Stores all defined treasure lists.
static final int UNSET
Unset values.
static final Comparator< TreasureTreeNode > TREASURE_NODE_COMPARATOR
Comparator for TreasureTreeNodes that compares their object name case insensitive.
A TreasureObj representing a "yes" entry.
static void linkSubLists(@NotNull final Iterable< TreasureTreeNode > needLink, @NotNull final TreasureTree treasures)
Links sub-treasure tree nodes to their parent nodes.
static TreasureTree createTreasureTree(@NotNull final List< TreasureTreeNode > tmpList, @NotNull final Iterable< TreasureTreeNode > needLink, @NotNull final Map< String, TreasureTreeNode > specialTreasureLists)
Creates a TreasureTree instance from a list of TreasureTreeNodes.
Subclass: Nodes in the CFTreasureListTree.
Subclass: UserObject (= content object) for nodes in the CFTreasureListTree These can be either treas...
Loader for treasure list files.
static TreasureTreeNode getRealNode(@NotNull final TreasureTree treasures, @NotNull final TreasureTreeNode node)
Returns the "real" (top-level) node that corresponds to a given node.
void putAll(final Iterable< TreasureTreeNode > treasureTreeNodes)
Adds all TreasureTreeNode.
void setChance(final int value)
Sets the chance attribute.
int getChance()
Returns the chance attribute.
static TreasureTree parseTreasures(@NotNull final ErrorView errorView, @NotNull final Map< String, TreasureTreeNode > specialTreasureLists, @NotNull final ConfigSource configSource, @NotNull final ProjectSettings projectSettings)
Parses a treasure file into a TreasureTree instance.
static final Category LOG
The Logger for printing log messages.
void setMagic(final int magic)
Sets the magic attribute.