<commit_before>package legends.helper;

import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class WorldConfig {
	private Path legendsPath;
	private Path legendsPlusPath;
	private Path worldGenPath;
	private Path historyPath;
	private Path sitesAndPropsPath;
	private Path imagePath;

	public WorldConfig(Path path) throws IOException {
		if (path.toString().endsWith(".xml")) {
			worldGenPath = makeWorldGenPath(path);
			legendsPath = path;
			legendsPlusPath = makeLegendsPlusPath(path);
			historyPath = makeHistoryPath(path);
			sitesAndPropsPath = makeSitesAndPropsPath(path);
			Path dir = path.getParent();
			String prefix = path.getFileName().toString().replace("-legends.xml", "");
			Files.newDirectoryStream(dir, prefix + "-world_map.*").forEach(this::setImagePath);
			Files.newDirectoryStream(dir, prefix + "-detailed.*").forEach(this::setImagePath);
		} else if (path.toString().endsWith(".zip")) {
			Map<String, String> env = new HashMap<>();
			env.put("create", "false");

			URI uri = URI.create("jar:" + path.toUri());
			System.out.println(uri);
			FileSystem fs;
			try {
				fs = FileSystems.getFileSystem(uri);
			} catch (FileSystemNotFoundException e) {
				fs = FileSystems.newFileSystem(uri, env);
			}

			Path dir = fs.getPath("/");
			Files.list(dir).forEach(p -> {
				String s = p.toString();
				if (s.endsWith("-legends.xml"))
					legendsPath = p;
				else if (s.endsWith("-legends_plus.xml"))
					legendsPlusPath = p;
				else if (s.endsWith("-world_history.txt"))
					historyPath = p;
				else if (s.endsWith("-world_sites_and_pops.txt"))
					sitesAndPropsPath = p;
				else if (s.endsWith("-world_sites_and_pops.txt"))
					sitesAndPropsPath = p;
			});

			String prefix = path.getFileName().toString().replace("-legends_archive.zip", "");
			Files.newDirectoryStream(dir, prefix + "*-world_map.*").forEach(this::setImagePath);
			Files.newDirectoryStream(dir, prefix + "*-detailed.*").forEach(this::setImagePath);
		}
	}

	private Path makeSitesAndPropsPath(Path path) {
		String cp = path.getFileName().toString().toLowerCase();<commit_msg>find world_gen_param.txt (in archive, next to archive, next to xml)<commit_after>package legends.helper;

import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import legends.xml.handlers.AnnotationContentHandler;

public class WorldConfig {
	private static final Log LOG = LogFactory.getLog(WorldConfig.class);

	private Path legendsPath;
	private Path legendsPlusPath;
	private Path worldGenPath;
	private Path historyPath;
	private Path sitesAndPropsPath;
	private Path imagePath;

	public WorldConfig(Path path) throws IOException {
		if (path.toString().endsWith(".xml")) {
			worldGenPath = makeWorldGenPath(path);
			legendsPath = path;
			legendsPlusPath = makeLegendsPlusPath(path);
			historyPath = makeHistoryPath(path);
			sitesAndPropsPath = makeSitesAndPropsPath(path);

			Path dir = path.getParent();
			String prefix = path.getFileName().toString().replace("-legends.xml", "");
			// try to load images (priorize detailed)
			Files.newDirectoryStream(dir, prefix + "-world_map.*").forEach(this::setImagePath);
			Files.newDirectoryStream(dir, prefix + "-detailed.*").forEach(this::setImagePath);
		} else if (path.toString().endsWith(".zip")) {
			Map<String, String> env = new HashMap<>();
			env.put("create", "false");

			URI uri = URI.create("jar:" + path.toUri());
			LOG.info(uri);
			FileSystem fs;
			try {
				fs = FileSystems.getFileSystem(uri);
			} catch (FileSystemNotFoundException e) {
				fs = FileSystems.newFileSystem(uri, env);
			}

			Path baseDir = path.getParent();
			Path dir = fs.getPath("/");

			// try to load world_gen_param.txt from base directory
			String region = path.getFileName().toString().substring(0, path.getFileName().toString().indexOf("-"));
			Files.newDirectoryStream(baseDir, region + "-world_gen_param.txt").forEach(p -> worldGenPath = p);

			// try to load files from archive
			Files.list(dir).forEach(p -> {
				String s = p.toString();
				if (s.endsWith("-legends.xml"))
					legendsPath = p;
				else if (s.endsWith("-legends_plus.xml"))
					legendsPlusPath = p;
				else if (s.endsWith("-world_history.txt"))
					historyPath = p;
				else if (s.endsWith("-world_sites_and_pops.txt"))
					sitesAndPropsPath = p;
				else if (s.endsWith("-world_sites_and_pops.txt"))
					sitesAndPropsPath = p;
				else if (s.endsWith("-world_gen_param.txt"))
					worldGenPath = p;
			});

			String prefix = path.getFileName().toString().replace("-legends_archive.zip", "");
			// try to load images from archive (priorize detailed)
			Files.newDirectoryStream(dir, prefix + "*-world_map.*").forEach(this::setImagePath);
			Files.newDirectoryStream(dir, prefix + "*-detailed.*").forEach(this::setImagePath);
		}
	}

	private Path makeSitesAndPropsPath(Path path) {
		String cp = path.getFileName().toString().toLowerCase();
{'fraction_non_alphanumeric': 0.14461143695014664, 'fraction_numerical': 0.00018328445747800586, 'mean_word_length': 4.435258964143427, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7478264', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1713, 'n_words': 354}