Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for build parameters (example: EXTENSIVE_TESTING).
We can put through build parameters for testing and other purposes so NCP can see them during testing or runtime otherwise. Not sure this is the final version, though.
- Loading branch information
Showing
6 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/BuildParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package fr.neatmonster.nocheatplus.utilities.build; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Experimental support for build parameters. | ||
* @author mc_dev | ||
* | ||
*/ | ||
public class BuildParameters { | ||
|
||
private static final Map<String, String> fileContents = new HashMap<String, String>(); | ||
|
||
static{ | ||
// Fetch file content from resources. | ||
String content = null; | ||
try{ | ||
content = ResourceUtil.fetchResource(BuildParameters.class, "BuildParameters.properties"); | ||
} | ||
catch(Throwable t){ | ||
t.printStackTrace(); | ||
} | ||
// Parse properties. | ||
if (content != null){ | ||
ResourceUtil.parseToMap(content, fileContents); | ||
} | ||
} | ||
|
||
////////////////////// | ||
// Auxiliary methods. | ||
///////////////////// | ||
|
||
public static String getString(String path, String preset){ | ||
String input = fileContents.get(path); | ||
if (input == null) return preset; | ||
else return input; | ||
} | ||
|
||
public static Boolean getBoolean(String path, Boolean preset){ | ||
String input = fileContents.get(path); | ||
if (input == null) return preset; | ||
else return ResourceUtil.getBoolean(input, preset); | ||
} | ||
|
||
////////////////////// | ||
// Public members. | ||
////////////////////// | ||
|
||
/** Extend testing amount. */ | ||
public static final boolean extensiveTesting = getBoolean("EXTENSIVE_TESTING", false); | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/build/ResourceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package fr.neatmonster.nocheatplus.utilities.build; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
public class ResourceUtil { | ||
|
||
public static Boolean getBoolean(String input, Boolean preset){ | ||
if (input == null) return preset; | ||
input = input.trim().toLowerCase(); | ||
if (input.matches("1|true|yes")) return true; | ||
else if (input.matches("0|false|no")) return false; | ||
else return preset; | ||
} | ||
|
||
/** | ||
* Might have a newline at the end.<br> | ||
* TODO: Move to other utility. | ||
* | ||
* @param name | ||
* @param clazz | ||
* @param folderPart | ||
* @return | ||
*/ | ||
public static String fetchResource(Class<?> clazz, String path) { | ||
String className = clazz.getSimpleName() + ".class"; | ||
String classPath = clazz.getResource(className).toString(); | ||
if (!classPath.startsWith("jar")) return null; | ||
String absPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/"+path; | ||
try { | ||
URL url = new URL(absPath); | ||
BufferedReader r = null; | ||
try { | ||
Object obj = url.getContent(); | ||
if (obj instanceof InputStream){ | ||
r = new BufferedReader(new InputStreamReader((InputStream) obj)); | ||
StringBuilder builder = new StringBuilder(); | ||
String last = r.readLine(); | ||
while (last != null){ | ||
builder.append(last); | ||
builder.append("\n"); // does not hurt if one too many. | ||
last = r.readLine(); | ||
} | ||
r.close(); | ||
return builder.toString(); | ||
} | ||
else return null; | ||
} catch (IOException e) { | ||
if (r != null){ | ||
try { | ||
r.close(); | ||
} catch (IOException e1) { | ||
} | ||
} | ||
return null; | ||
} | ||
} catch (MalformedURLException e) { | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* New line separated entries, lines starting with '#' are ignored (trim + check), otherwise ini-file style x=y.<br> | ||
* All keys and values are trimmed, lines without assignment still get added, all mappings will be the empty string or some content. | ||
* @param input | ||
* @param map | ||
*/ | ||
public static void parseToMap(String input, Map<String, String> map){ | ||
final String[] split = input.split("\n"); | ||
for (final String line : split){ | ||
final String trimmed = line.trim(); | ||
if (trimmed.startsWith("#")) continue; | ||
final String[] parts = line.split("=", 2); | ||
if (parts.length == 1){ | ||
map.put(parts[0].trim(), ""); | ||
} | ||
else{ | ||
map.put(parts[0].trim(), parts[1].trim()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# @GENERATED_NOTE@ | ||
# These parameters are filled in during building (maven), they are not strictly needed. | ||
# Replacement mappings are defined in the pom.xml. | ||
EXTENSIVE_TESTING=@EXTENSIVE_TESTING@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters