Skip to content

Commit

Permalink
Added console output when file gets skipped
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 19, 2023
1 parent 64f0441 commit 77dc4d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
2 changes: 1 addition & 1 deletion common/src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static void loadScripts(ScriptPack pack, Path dir, String path) {
for (var file : Files.walk(dir, 10, FileVisitOption.FOLLOW_LINKS).filter(Files::isRegularFile).toList()) {
var fileName = dir.relativize(file).toString().replace(File.separatorChar, '/');

if (fileName.endsWith(".js")) {
if (fileName.endsWith(".js") || fileName.endsWith(".ts") && !fileName.endsWith(".d.ts")) {
pack.info.scripts.add(new ScriptFileInfo(pack.info, pathPrefix + fileName));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dev.latvian.mods.kubejs.CommonProperties;
import dev.latvian.mods.kubejs.util.UtilsJS;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -42,18 +41,11 @@ public ScriptFileInfo(ScriptPackInfo p, String f) {
lines = UtilsJS.EMPTY_STRING_ARRAY;
}

@Nullable
public Throwable preload(ScriptSource source) {
public void preload(ScriptSource source) throws Throwable {
properties.clear();
priority = 0;
ignored = false;
lines = UtilsJS.EMPTY_STRING_ARRAY;

try {
lines = source.readSource(this).toArray(UtilsJS.EMPTY_STRING_ARRAY);
} catch (Throwable ex) {
return ex;
}
lines = source.readSource(this).toArray(UtilsJS.EMPTY_STRING_ARRAY);

for (int i = 0; i < lines.length; i++) {
var tline = lines[i].trim();
Expand All @@ -71,16 +63,10 @@ public Throwable preload(ScriptSource source) {
}
}

try {
priority = Integer.parseInt(getProperty("priority", "0"));
ignored = getProperty("ignored", "false").equals("true") || getProperty("ignore", "false").equals("true");
packMode = getProperty("packmode", "default");
requiredMods.addAll(getProperties("requires"));
} catch (Exception ex) {
return ex;
}

return null;
priority = Integer.parseInt(getProperty("priority", "0"));
ignored = getProperty("ignored", "false").equals("true") || getProperty("ignore", "false").equals("true");
packMode = getProperty("packmode", "default");
requiredMods.addAll(getProperties("requires"));
}

public List<String> getProperties(String s) {
Expand All @@ -96,23 +82,23 @@ public int getPriority() {
return priority;
}

public boolean skipLoading() {
public String skipLoading() {
if (ignored) {
return true;
return "Ignored";
}

if (!packMode.isEmpty() && !packMode.equals(CommonProperties.get().packMode)) {
return true;
return "Pack mode mismatch";
}

if (!requiredMods.isEmpty()) {
for (String mod : requiredMods) {
if (!Platform.isModLoaded(mod)) {
return true;
return "Mod " + mod + " is not loaded";
}
}
}

return false;
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ public void reload(@Nullable ResourceManager resourceManager) {
load();
}

private void loadFile(ScriptPack pack, ScriptFileInfo fileInfo, ScriptSource source) {
try {
fileInfo.preload(source);
var skip = fileInfo.skipLoading();

if (skip.isEmpty()) {
pack.scripts.add(new ScriptFile(pack, fileInfo, source));
} else {
scriptType.console.info("Skipped " + fileInfo.location + ": " + skip);
}
} catch (Throwable error) {
scriptType.console.error("Failed to pre-load script file " + fileInfo.location + ": " + error);
}
}

private void loadFromResources(ResourceManager resourceManager) {
Map<String, List<ResourceLocation>> packMap = new HashMap<>();

Expand All @@ -96,17 +111,7 @@ private void loadFromResources(ResourceManager resourceManager) {

for (var fileInfo : pack.info.scripts) {
var scriptSource = (ScriptSource.FromResource) info -> resourceManager.getResourceOrThrow(info.id);
var error = fileInfo.preload(scriptSource);

if (fileInfo.skipLoading()) {
continue;
}

if (error == null) {
pack.scripts.add(new ScriptFile(pack, fileInfo, scriptSource));
} else {
scriptType.console.error("Failed to pre-load script file " + fileInfo.location + ": " + error);
}
loadFile(pack, fileInfo, scriptSource);
}

pack.scripts.sort(null);
Expand Down Expand Up @@ -139,18 +144,7 @@ public void loadFromDirectory() {

for (var fileInfo : pack.info.scripts) {
var scriptSource = (ScriptSource.FromPath) info -> directory.resolve(info.file);

var error = fileInfo.preload(scriptSource);

if (fileInfo.skipLoading()) {
continue;
}

if (error == null) {
pack.scripts.add(new ScriptFile(pack, fileInfo, scriptSource));
} else {
KubeJS.LOGGER.error("Failed to pre-load script file " + fileInfo.location + ": " + error);
}
loadFile(pack, fileInfo, scriptSource);
}

pack.scripts.sort(null);
Expand Down

0 comments on commit 77dc4d9

Please sign in to comment.