-
Notifications
You must be signed in to change notification settings - Fork 1
/
DAConstants.java
122 lines (112 loc) · 3.86 KB
/
DAConstants.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.artemis;
import java.io.File;
import java.io.FileReader;
import java.nio.file.FileSystems;
import java.util.Properties;
/**
* dustArtemis constants used through the framework.
*
* <p>
* These values are loaded from a config file when the class is loaded. Config
* file path is retrieved from {@value #CFG_FILE_PROPERTY_NAME} property in
* Java's system properties.
* </p>
*
* <p>
* Constants not specified in the file will be set to a reasonable default, also
* all constants have minimum bounds too.
* </p>
*
* <p>
* Remember that most of these values aren't limits, they're approximates. If
* you add more entities than you specified, backing collections will grow and
* dustArtemis keeps working. These values are useful mostly to specify default
* values so the backing collections don't trash arrays at application startup
* when adding components/entities to your World instance.
* </p>
*
* <p>
* For example, to set the Bag constants, you'd need to put this in a plain text
* file:
* </p>
*
* <pre>
* BAG_DEFAULT_CAPACITY=32
* BAG_GROW_RATE_THRESOLD=1024
* </pre>
*
* <p>
* Then just point to its relative path (from the application's POV) where the
* file is located like this:
* </p>
*
* <pre>
* System.getProperties().put( {@value #CFG_FILE_PROPERTY_NAME},
* "cfg/artemis.cfg");
* </pre>
*
* <p>
* Extension is not important, it just needs to be a plain text file. Since this
* uses Java Properties API, all {@link java.util.Properties} particulars apply.
* You can always check dustArtemis sources if you need to know something
* specific about it.
* </p>
*
* @author dustContributor
*
*/
public final class DAConstants {
/** Non-instantiable class. */
private DAConstants() {
throw new DustException(DAConstants.class, "Can't create an instance of this class!");
}
/** Name of the property that will be used to fetch config file path from. */
public static final String CFG_FILE_PROPERTY_NAME = "dustArtemis.cfgpath";
/** Default capacity for ImmutableBag and subclasses. */
public static final int BAG_DEFAULT_CAPACITY;
/** Threshold in which growth strategy is changed for bags. */
public static final int BAG_GROW_RATE_THRESHOLD;
/** Approximate amount of live entities in the world. */
public static final int APPROX_LIVE_ENTITIES;
/** Approximate amount of entities active in each system. */
public static final int APPROX_ENTITIES_PER_SYSTEM;
static {
final Properties props = loadCfgFile();
BAG_DEFAULT_CAPACITY = Math.max(getIntOrDefault(props, "BAG_DEFAULT_CAPACITY", 16), 4);
BAG_GROW_RATE_THRESHOLD = Math.max(getIntOrDefault(props, "BAG_GROW_RATE_THRESHOLD", 2048), 256);
APPROX_LIVE_ENTITIES = Math.max(getIntOrDefault(props, "APPROX_LIVE_ENTITIES", 1024), 64);
APPROX_ENTITIES_PER_SYSTEM = Math.max(getIntOrDefault(props, "APPROX_ENTITIES_PER_SYSTEM", 1024), 16);
}
/**
* Returns an int fetched and parsed from the properties table, or defaults to
* passed value if there isn't one or the value couldn't be parsed.
*/
private static final int getIntOrDefault(final Properties props, final String key, final int defValue) {
try {
return Integer.parseInt(props.getProperty(key));
}
// Prolly the only exception type that could be raised here.
catch (final Exception ex) {
// Fail silently.
}
return defValue;
}
/**
* Loads configuration file for dustArtemis constants, fetching the path from
* the property {@value #CFG_FILE_PROPERTY_NAME}.
*/
private static final Properties loadCfgFile() {
final Properties props = new Properties();
try {
final String dir = System.getProperty(CFG_FILE_PROPERTY_NAME);
final File file = FileSystems.getDefault().getPath(dir).toFile();
// Try load the constants configuration from the file.
try (final FileReader fr = new FileReader(file)) {
props.load(fr);
}
} catch (final Exception ex) {
// Fail silently.
}
return props;
}
}