<commit_before>package net.clgd.ccemux.emulation;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;

import dan200.computercraft.core.filesystem.FileMount;
import net.clgd.ccemux.Config;

public class CCEmuX implements Runnable {

	private static final Field computerRootField;

	static {
		Field f;

		try {
			f = EmulatedComputer.class.getSuperclass().getDeclaredField("m_rootMount");
			f.setAccessible(true);
		} catch (NoSuchFieldException | SecurityException e) {
			f = null;
		}

		computerRootField = f;
	}

	public static String getVersion() {
		String v = Config.class.getPackage().getImplementationVersion();
		return v == null ? "[Unknown]" : v ;
	}

	public final Logger logger;
	public final Config conf;
	public final Path dataDir;
	public final File ccJar;

	public final EmulatedEnvironment env;

	private boolean running;
	private long timeStarted;
	private final List<EmulatedComputer> computers = new ArrayList<>();

	public CCEmuX(Logger logger, Config conf) {
		this.logger = logger;
		this.conf = conf;
		this.dataDir = conf.getDataDir();
		this.ccJar = dataDir.resolve(conf.getCCLocal()).toFile();

		env = new EmulatedEnvironment(this);
	}

	public EmulatedComputer createEmulatedComputer(int id, Path saveDir) {
		logger.trace("Creating emulated computer");
		synchronized (computers) {
			EmulatedComputer ec = new EmulatedComputer(this, conf.getTermWidth(), conf.getTermHeight(), id);
			logger.info("Created emulated computer ID {}", ec.getID());

			if (saveDir != null) {
				logger.info("Overriding save dir for computer {} to '{}'", ec.getID(), saveDir.toString());
				try {
					computerRootField.set(ec, new FileMount(saveDir.toFile(), env.getComputerSpaceLimit()));
				} catch (IllegalArgumentException | IllegalAccessException e) {
					e.printStackTrace();
				}
			}

			ec.turnOn();
			computers.add(ec);
			return ec;
		}
	}

	public EmulatedComputer createEmulatedComputer() {
		return createEmulatedComputer(-1, null);
	}

	public boolean removeEmulatedComputer(EmulatedComputer ec) {
		synchronized (computers) {
			logger.trace("Removing emulated computer ID {}", ec.getID());

			boolean success = computers.remove(ec);

			if (computers.isEmpty()) {
				running = false;
				logger.info("All emulated computers removed, stopping event loop");
			}

			return success;
		}
	}

	private void advance(double dt) {
		synchronized (computers) {
			computers.forEach(c -> {
				synchronized (c) {
					c.advance(dt);
				}
			});
		}
	}

	@Override
	public void run() {
		running = true;

		timeStarted = System.currentTimeMillis();
		long lastTime = System.currentTimeMillis();

		double computerTickTimer = 0.0;

		while (running) {
			long now = System.currentTimeMillis();
			double dt = (now - lastTime) / 1000d;

			computerTickTimer += dt;

			if (computerTickTimer >= 0.05) {
				advance(dt);
				computerTickTimer = 0.0f;
			}

			lastTime = System.currentTimeMillis();

			try {
				Thread.sleep(1000 / conf.getFramerate());
			} catch (InterruptedException ignored) {}
		}

		logger.debug("Emulation stopped");
	}

	public boolean isRunning() {
		return running;
	}

	public long getTimeStarted() {
		return timeStarted;
	}
<commit_msg>Fix a problem<commit_after>package net.clgd.ccemux.emulation;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;

import dan200.computercraft.core.filesystem.FileMount;
import net.clgd.ccemux.Config;

public class CCEmuX implements Runnable {

	private static final Field computerRootField;

	static {
		Field f;

		try {
			f = EmulatedComputer.class.getSuperclass().getDeclaredField("m_rootMount");
			f.setAccessible(true);
		} catch (NoSuchFieldException | SecurityException e) {
			f = null;
		}

		computerRootField = f;
	}

	public static String getVersion() {
		String v = Config.class.getPackage().getImplementationVersion();
		return v == null ? "[Unknown]" : v ;
	}

	public final Logger logger;
	public final Config conf;
	public final Path dataDir;
	public final File ccJar;

	public final EmulatedEnvironment env;

	private boolean running;
	private long timeStarted;
	private final List<EmulatedComputer> computers = new ArrayList<>();

	public CCEmuX(Logger logger, Config conf) {
		this.logger = logger;
		this.conf = conf;
		this.dataDir = conf.getDataDir();
		this.ccJar = dataDir.resolve(conf.getCCLocal()).toFile();

		env = new EmulatedEnvironment(this);
	}

	public EmulatedComputer createEmulatedComputer(int id, Path saveDir) {
		logger.trace("Creating emulated computer");
		synchronized (computers) {
			EmulatedComputer ec = new EmulatedComputer(this, conf.getTermWidth(), conf.getTermHeight(), id);
			logger.info("Created emulated computer ID {}", ec.getID());

			if (saveDir != null) {
				logger.info("Overriding save dir for computer {} to '{}'", ec.getID(), saveDir.toString());
				try {
					computerRootField.set(ec, new FileMount(saveDir.toFile(), env.getComputerSpaceLimit()));
				} catch (IllegalArgumentException | IllegalAccessException e) {
					e.printStackTrace();
				}
			}

			ec.turnOn();
			computers.add(ec);
			return ec;
		}
	}

	public EmulatedComputer createEmulatedComputer() {
		return createEmulatedComputer(-1, null);
	}

	public boolean removeEmulatedComputer(EmulatedComputer ec) {
		synchronized (computers) {
			logger.trace("Removing emulated computer ID {}", ec.getID());

			boolean success = computers.remove(ec);

			if (computers.isEmpty()) {
				running = false;
				logger.info("All emulated computers removed, stopping event loop");
			}

			return success;
		}
	}

	private void advance(double dt) {
		synchronized (computers) {
			computers.forEach(c -> {
				synchronized (c) {
					c.advance(dt);
				}
			});
		}
	}

	@Override
	public void run() {
		running = true;

		timeStarted = System.currentTimeMillis();
		long lastTime = System.currentTimeMillis();

		double computerTickTimer = 0.0;

		while (running) {
			long now = System.currentTimeMillis();
			double dt = (now - lastTime) / 1000d;

			computerTickTimer += dt;

			if (computerTickTimer >= 0.05) {
				advance(dt);
				computerTickTimer = 0.0;
			}

			lastTime = System.currentTimeMillis();

			try {
				Thread.sleep(1000 / conf.getFramerate());
			} catch (InterruptedException ignored) {}
		}

		logger.debug("Emulation stopped");
	}

	public boolean isRunning() {
		return running;
	}

	public long getTimeStarted() {
		return timeStarted;
	}

{'fraction_non_alphanumeric': 0.12062433702076072, 'fraction_numerical': 0.006061524473405061, 'mean_word_length': 3.9512378094523632, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '130501', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2057, 'n_words': 494}