Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Created April 22, 2024 12:21
Show Gist options
  • Save akarnokd/4835b408c0b51e0c7e02a871789a78e3 to your computer and use it in GitHub Desktop.
Save akarnokd/4835b408c0b51e0c7e02a871789a78e3 to your computer and use it in GitHub Desktop.
The Planet Crafter Fusion Reactor Fixer

Fusion Reactor Fixer

Prerequisites

Install Java 19 or later. Make sure you check the option to include in the PATH variable.

Verify if it worked: java -version

Download

Download the fusionreactorfix.bat and FusionReactorFix.java files from this gist into the game's save folder:

%USERPROFILE%\AppData\LocalLow\MijuGames\Planet Crafter

Usage

Make note the filename you want to fix, for example, Standard-1.json

Open a command line:

Win+R, type in cmd.exe, press enter.

Switch to the game's save directory:

cd %USERPROFILE%\AppData\LocalLow\MijuGames\Planet Crafter

Type in this and press enter:

fusionreactorfix.bat Standard-1.json

You should see the repair results and a new file with the _fixed postfix, for example, Standard-1.json_fixed.

Rename/overwrite this fixed file back to the original filename.

@echo off
java FusionReactorFix.java %1
package hu.akarnokd.theplanetcrafter;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.regex.Pattern;
public class FusionReactorFix {
public static void main(String[] args) throws Throwable {
System.out.println("The Planet Crafter Fusion Reactor Inventory Fix");
if (args.length == 0) {
System.out.println("Usage:");
System.out.println("FusionReactorFix filename.json");
return;
}
var generators = new HashMap<String, String>();
generators.put("109487734", "hill wreck");
generators.put("102606011", "battleship");
generators.put("101703877", "stargate");
generators.put("101484917", "luxury cruiser");
generators.put("107983344", "lava ship");
var lines = Files.readAllLines(Paths.get(args[0]), StandardCharsets.UTF_8);
var items = new HashMap<String, String>();
var itempatter = Pattern.compile("\"id\":(\\d+),\"gId\":\"([a-zA-Z0-9_-]+)\"");
var inventorypatter = Pattern.compile("\"id\":(\\d+),\"woIds\":\"([a-zA-Z0-9_,]+)\"");
boolean save = false;
for (int i = 0; i < lines.size(); i++) {
var line = lines.get(i);
var m = itempatter.matcher(line);
if (m.find()) {
var id = m.group(1);
var gid = m.group(2);
items.put(id, gid);
} else {
var m2 = inventorypatter.matcher(line);
if (m2.find()) {
var id = m2.group(1);
var gen = generators.get(id);
if (gen != null) {
System.out.println("Generator: " + gen + " (" + id + "): " + m2.group(2));
var woIds = new ArrayList<String>();
var changed = false;
for (var woId : Arrays.asList(m2.group(2).split(","))) {
var gId = items.get(woId);
if (gId != null) {
if (!gId.equals("FusionEnergyCell")) {
System.out.printf(" %s - %s -> removed%n", woId, gId);
changed = true;
} else {
woIds.add(woId);
}
}
}
if (changed) {
var woIds1 = m2.start(2);
var woIds2 = m2.end(2);
System.out.println(" - " + line);
line = line.substring(0, woIds1) + String.join(",", woIds) + line.substring(woIds2);
System.out.println(" + " + line);
lines.set(i, line);
save = true;
} else {
System.out.println(" OK");
}
}
}
}
}
if (save) {
System.out.println("Done. Saved in:");
System.out.println(Paths.get(args[0] + "_fixed"));
Files.write(Paths.get(args[0] + "_fixed"), lines, StandardCharsets.UTF_8);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment