Skip to content

Commit

Permalink
remove config directory argument from Apathy
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Apr 9, 2023
1 parent 8ad8d17 commit 316e42d
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@
import org.apache.logging.log4j.LogManager;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

public abstract class Apathy118 extends Apathy {
public static Apathy118 instance118;

public Apathy118(Path configPath) {
super(configPath, VerConv.toLogFacade(LogManager.getLogger(MODID)));
public Apathy118() {
super(VerConv.toLogFacade(LogManager.getLogger(MODID)));

if(instance118 == null) {
instance118 = this;
Expand Down Expand Up @@ -107,10 +106,10 @@ public Rule bakeMobsConfigRule() {
ruleSpec = new RuleSpecChain(ruleSpecList);
}

if(generalCfg.get(CoreGenOptions.debugBuiltinRule)) JsonRule.dump(ruleSpec, configPath, "builtin-rule");
if(generalCfg.get(CoreGenOptions.debugBuiltinRule)) JsonRule.dump(ruleSpec, "builtin-rule");
if(generalCfg.get(CoreGenOptions.runRuleOptimizer)) {
ruleSpec = ruleSpec.optimize();
if(generalCfg.get(CoreGenOptions.debugBuiltinRule)) JsonRule.dump(ruleSpec, configPath, "builtin-rule-opt");
if(generalCfg.get(CoreGenOptions.debugBuiltinRule)) JsonRule.dump(ruleSpec, "builtin-rule-opt");
}

return ruleSpec.build();
Expand Down
19 changes: 4 additions & 15 deletions core/src/main/java/agency/highlysuspect/apathy/core/Apathy.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import com.google.gson.JsonObject;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
Expand All @@ -42,10 +40,6 @@ public abstract class Apathy {

/** constructor sets this as a side effect */
public static Apathy instance;

/** Ideally this should be a subdirectory of the platform-specific config directory */
//TODO: move dump directory somewhere else?
public final Path configPath;
public final LogFacade log;

public final NotRegistry<JsonSerializer<? extends Spec<Rule, ?>>> ruleSerializers = new NotRegistry<>();
Expand All @@ -58,7 +52,7 @@ public abstract class Apathy {
public Rule configuredRule = RuleSpecAlways.ALWAYS_ALLOW.build();
public @Nullable Rule jsonRule;

public Apathy(Path configPath, LogFacade log) {
public Apathy(LogFacade log) {
if(instance == null) {
instance = this;
} else {
Expand All @@ -67,17 +61,10 @@ public Apathy(Path configPath, LogFacade log) {
throw e;
}

this.configPath = configPath;
this.log = log;
}

public void init() {
try {
Files.createDirectories(configPath);
} catch (IOException e) {
throw new RuntimeException("Problem creating Apathy config directory at " + configPath, e);
}

//rule setup
addRules();

Expand Down Expand Up @@ -126,7 +113,7 @@ public boolean refreshConfig() {

Rule newJsonRule = jsonRule;
try {
newJsonRule = JsonRule.loadJson(configPath.resolve("mobs.json"));
newJsonRule = JsonRule.loadJson(mobsJsonPath());
} catch (Exception e) {
log.error("Problem reading mobs.json: ", e);
ok = false;
Expand Down Expand Up @@ -175,6 +162,8 @@ public void addBossConfig(ConfigSchema schema) {
public abstract ConfigSchema.Bakery generalConfigBakery();
public abstract ConfigSchema.Bakery mobsConfigBakery();
public abstract ConfigSchema.Bakery bossConfigBakery();
public abstract Path mobsJsonPath();
public abstract Path dumpsDirPath();

public abstract Rule bakeMobsConfigRule();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public class JsonRule {
boolean debug = Apathy.instance.generalCfg.get(CoreGenOptions.debugJsonRule);
boolean opt = Apathy.instance.generalCfg.get(CoreGenOptions.runRuleOptimizer);

if(debug) dump(spec, Apathy.instance.configPath, "json-rule");
if(debug) dump(spec, "json-rule");

if(opt) {
spec = spec.optimize();
if(debug) dump(spec, Apathy.instance.configPath, "json-rule-opt");
if(debug) dump(spec, "json-rule-opt");
}

return spec.build();
Expand All @@ -70,9 +70,9 @@ public class JsonRule {
/**
* Write a rule as json and poop it out.
*/
public static <RULE extends Spec<Rule, RULE>> void dump(Spec<Rule, RULE> ruleSpec, Path configFolder, String filename) {
public static <RULE extends Spec<Rule, RULE>> void dump(Spec<Rule, RULE> ruleSpec, String filename) {
try {
Path dumpDir = configFolder.resolve("dumps");
Path dumpDir = Apathy.instance.dumpsDirPath();

Files.createDirectories(dumpDir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public <T> void visitOption(ConfigProperty<T> option) {
}
});

if(path.getParent() != null) Files.createDirectories(path.getParent()); //can be null if it's at the fs root lol
Files.write(path, out, StandardCharsets.UTF_8);
}

Expand Down
4 changes: 3 additions & 1 deletion docs/2.x/GENERAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ Options that might help when you are making a config file.

## Debug rules

You can debug the config file rule and the JSON rule. When these options are enabled, the rule will be serialized and written to the directory `config/apathy/dumps/`. If the rule optimizer is enabled, rules will also be debugged after the optimization pass to a separate file, so you can observe the effects of the optimizer.
You can debug the config file rule and the JSON rule. When these options are enabled, the rule in internal-format will be serialized to json and written to a directory. If the rule optimizer is enabled, rules will also be debugged after the optimization pass to a separate file, so you can observe the effects of the optimizer.

The directory is `config/apathy/dumps/` in old versions, and `apathy-dumps` in version >=2.5.
9 changes: 6 additions & 3 deletions docs/2.x/JSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ If mobs.cfg's `ruleOrder` option does not contain `json`, or a rule ordered befo

## Dumping

`general.cfg` has options for dumping the built-in rule as a JSON file (in `/config/apathy/dumps/builtin-rule.json` and `builtin-rule-opt.json`), if you would like to see an example.
`general.cfg` has options for dumping the built-in rule as a JSON file if you would like to see an example. If you turn that on, the file will be dumped to:

Previous versions used some DataFixerUpper `Codec` stuff that outputted the json with a strange format (`type` at the bottom). **In versions >=2.5**, the JSON handling code has been completely rewritten and does not use `Codec`, so the JSON is nice looking.
* old versions: `config/apathy/dumps/builtin-rule.json`
* 2.5: `apathy-dumps/builtin-rule.json`

(If you'd like to use the dumped rule as a starting point to create your own `mobs.json`, remember to remove the `evaluate_json_file` rule!)
Previous versions used some DataFixerUpper `Codec` stuff that outputted the json with a strange format (`type` at the bottom). **In versions >=2.5** the JSON handling code has been completely rewritten and does not use `Codec`, so the JSON is nice looking.

(If you'd like to use the dumped rule as a starting point to create your own `mobs.json`, remember to remove the `evaluate_json_file` rule!!!)

# Json Format

Expand Down
2 changes: 1 addition & 1 deletion docs/2.x/MOBS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Quick note: There's a fairly aggressive rule optimizer included in the mod. The

## JSON

This rule does not have any options in the config file, but its ordering relative to other rules may be specified in `ruleOrder`.
This rule does not have anything to configure in this config file, but its ordering relative to other rules may be specified in `ruleOrder`.

## Difficulty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.ResourceManager;

import java.nio.file.Path;

public class FabricInit extends Apathy118 implements ModInitializer {
public FabricInit() {
super(FabricLoader.getInstance().getConfigDir().resolve(MODID));
}

@Override
public void onInitialize() {
init();
Expand Down Expand Up @@ -50,18 +48,32 @@ public void installPlayerSetManagerTicker() {
ServerTickEvents.START_SERVER_TICK.register(server -> PlayerSetManager.getFor(server).syncWithConfig());
}

private Path cfgPath() {
return FabricLoader.getInstance().getConfigDir().resolve(MODID); //inside a subfolder of main config directory
}

@Override
public ConfigSchema.Bakery generalConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("general.cfg"));
return new CrummyConfig.Bakery(cfgPath().resolve("general.cfg"));
}

@Override
public ConfigSchema.Bakery mobsConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("mobs.cfg"));
return new CrummyConfig.Bakery(cfgPath().resolve("mobs.cfg"));
}

@Override
public ConfigSchema.Bakery bossConfigBakery() {
return new CrummyConfig.Bakery(configPath.resolve("boss.cfg"));
return new CrummyConfig.Bakery(cfgPath().resolve("boss.cfg"));
}

@Override
public Path mobsJsonPath() {
return cfgPath().resolve("mobs.json");
}

@Override
public Path dumpsDirPath() {
return FabricLoader.getInstance().getGameDir().resolve("apathy-dumps");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import net.minecraftforge.network.NetworkConstants;
import net.minecraftforge.server.ServerLifecycleHooks;

import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

@Mod("apathy")
public class ForgeInit extends Apathy118 {
public ForgeInit() {
//TODO should really use a real forge config
super(FMLPaths.CONFIGDIR.get().resolve(MODID));

LegacyToTomlUpgrader.doIt();

//borrowed from IExtensionPoint javadoc in fmlcore
Expand Down Expand Up @@ -118,4 +116,14 @@ public ConfigSchema.Bakery mobsConfigBakery() {
public ConfigSchema.Bakery bossConfigBakery() {
return new ForgeBackedConfig.Bakery(bossForgeSpec);
}

@Override
public Path mobsJsonPath() {
return FMLPaths.CONFIGDIR.get().resolve("apathy-mobs.json");
}

@Override
public Path dumpsDirPath() {
return FMLPaths.GAMEDIR.get().resolve("apathy-dumps");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import agency.highlysuspect.apathy.core.Apathy;
import net.minecraftforge.fml.loading.FMLPaths;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -29,13 +30,7 @@ public static void doIt() {
reallyDoIt(dir.resolve(Apathy.MODID).resolve("boss.cfg"), dir.resolve("apathy-boss.toml"));
} catch (Exception e) {
Apathy.instance.log.error("Problem upgrading old config: " + e.getMessage(), e);
return;
}

//clean up the apathy/ subdir if it's empty
try {
Files.delete(dir.resolve(Apathy.MODID));
} catch (Exception ignored) {}
}

private static final Pattern headerPattern = Pattern.compile("^## (.*) ##$");
Expand Down

0 comments on commit 316e42d

Please sign in to comment.