Java Code Examples for com.electronwill.nightconfig.core.file.FileConfig#load()

The following examples show how to use com.electronwill.nightconfig.core.file.FileConfig#load() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonConfigTest.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testAuto() throws InterruptedException {
	FileConfig config = FileConfig.builder(file).autoreload().autosave().build();
	config.load();
	System.out.println(config);
	double d;
	for (int i = 0; i < 20; i++) {
		d = Math.random();
		config.set("double", d);
		Thread.sleep(20);
		//System.out.println(i + ":" + d);
		assertEquals(d, config.<Double>get("double").doubleValue());
	}
	for (int i = 0; i < 1000; i++) {
		config.set("double", 0.123456);
	}
	config.close();
}
 
Example 2
Source File: JsonConfigTest.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testEmptyDataTolerance() throws IOException {
	File f = new File("empty.json");
	assertEquals(0, f.length());

	FileConfig cDefault = FileConfig.of(f);
	assertThrows(ParsingException.class, cDefault::load);

	JsonFormat<?> f1 = JsonFormat.fancyInstance();
	FileConfig c1 = FileConfig.of(f, f1);
	assertThrows(ParsingException.class, c1::load);

	JsonFormat<?> f2 = JsonFormat.emptyTolerantInstance();
	FileConfig c2 = FileConfig.of(f, f2);
	c2.load();
	assertTrue(c2.isEmpty());

	assertEquals(0, f.length());

	assertThrows(ParsingException.class, ()->new JsonParser().parse(""));
	new JsonParser(true).parse("");
}
 
Example 3
Source File: Patchwork.java    From patchwork-patcher with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ForgeModJar parseModManifest(Path jarPath) throws IOException, URISyntaxException, ManifestParseException {
	String mod = jarPath.getFileName().toString().split("\\.jar")[0];
	// Load metadata
	LOGGER.trace("Loading and parsing metadata for %s", mod);
	URI inputJar = new URI("jar:" + jarPath.toUri());

	FileConfig toml;
	ForgeAccessTransformer at = null;

	try (FileSystem fs = FileSystems.newFileSystem(inputJar, Collections.emptyMap())) {
		Path manifestPath = fs.getPath("/META-INF/mods.toml");
		toml = FileConfig.of(manifestPath);
		toml.load();

		Path atPath = fs.getPath("/META-INF/accesstransformer.cfg");

		if (Files.exists(atPath)) {
			at = ForgeAccessTransformer.parse(atPath);
		}
	}

	Map<String, Object> map = toml.valueMap();

	ModManifest manifest = ModManifest.parse(map);

	if (!manifest.getModLoader().equals("javafml")) {
		LOGGER.error("Unsupported modloader %s", manifest.getModLoader());
	}

	if (at != null) {
		at.remap(accessTransformerRemapper, ex -> LOGGER.throwing(Level.WARN, ex));
	}

	return new ForgeModJar(jarPath, manifest, at);
}
 
Example 4
Source File: JsonConfigTest.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testReadEmptyFile() throws IOException {
	File f = new File("tmp.json");
	FileConfig config = FileConfig.of(f);
	config.load();
	config.close();
	Config conf = new JsonParser().parse(f, FileNotFoundAction.THROW_ERROR);
	System.out.println(conf);
	assertTrue(conf.isEmpty());
	System.out.println("tmp.json:\n" + Files.readAllLines(f.toPath()).get(0));
	f.delete();
}
 
Example 5
Source File: FileConfigExample.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
	// Creates the FileConfig:
	FileConfig config = FileConfig.of("config.toml");

	// Loads the config (reads the file):
	config.load();
	// Note: the load() call is always blocking: it returns when the reading operation terminates.
	// load() is also used to reload the configuration.

	System.out.println("Config: " + config);

	// Modifies the config:
	config.set("key", "value");
	config.set("number", 123456);
	config.set("floatingPoint", 3.1415926535);

	// You can also use sub configs!
	Config subConfig = Config.inMemory();
	subConfig.set("subKey", "value");
	config.set("subConfig", subConfig);

	// NightConfig supports dot-separated paths:
	config.set("subConfig.subKey", "newValue");

	// If you want to use a key that contains a dot in its name, use a list:
	config.set(Arrays.asList("subConfig", "127.0.0.1"),
			   "test");// the key "127.0.0.1" is in subConfig

	System.out.println("Config: " + config);

	// Saves the config:
	config.save();
	// Note: by default, the save operation is done in the background, and config.save()
	// returns immediately without waiting for the operation to terminates.

	/* Once you don't need the FileConfig anymore, remember to close it, in order to release
	the associated resources. There aren't always such resources, but it's a good practise to
	call the close() method anyway.
	Closing the FileConfig also ensures that all the data has been written, in particular it
	waits for the background saving operations to complete. */
	config.close();
}