Fix some resource leaks
This commit is contained in:
1 parent
2a4d678aa7
commit
d273d4ffb8
3 files changed
+40
-29
No files matched your search
@@ -51,6 +51,7 @@ import java.util.Optional;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class CarpetScriptServer extends ScriptServer
|
public class CarpetScriptServer extends ScriptServer
|
||||||
{
|
{
|
||||||
@@ -144,26 +145,29 @@ public class CarpetScriptServer extends ScriptServer
|
|||||||
Path folder = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
|
Path folder = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
|
||||||
if (!Files.exists(folder))
|
if (!Files.exists(folder))
|
||||||
Files.createDirectories(folder);
|
Files.createDirectories(folder);
|
||||||
Optional<Path>
|
try (Stream<Path> folderLister = Files.list(folder)) {
|
||||||
scriptPath = Files.list(folder)
|
Optional<Path> scriptPath = folderLister
|
||||||
.filter(script ->
|
.filter(script ->
|
||||||
script.getFileName().toString().equalsIgnoreCase(name+".sc") ||
|
script.getFileName().toString().equalsIgnoreCase(name + ".sc") ||
|
||||||
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name+".scl"))
|
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name+".scl"))
|
||||||
).findFirst();
|
).findFirst();
|
||||||
if (scriptPath.isPresent())
|
if (scriptPath.isPresent())
|
||||||
return Module.fromPath(scriptPath.get());
|
return Module.fromPath(scriptPath.get());
|
||||||
|
}
|
||||||
|
|
||||||
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
|
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
|
||||||
{
|
{
|
||||||
Path globalFolder = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
Path globalFolder = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
||||||
if (!Files.exists(globalFolder))
|
if (!Files.exists(globalFolder))
|
||||||
Files.createDirectories(globalFolder);
|
Files.createDirectories(globalFolder);
|
||||||
scriptPath = Files.walk(globalFolder)
|
try (Stream<Path> folderWalker = Files.walk(globalFolder)) {
|
||||||
.filter(script -> script.getFileName().toString().equalsIgnoreCase(name + ".sc") ||
|
Optional<Path> scriptPath = folderWalker
|
||||||
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name + ".scl")))
|
.filter(script -> script.getFileName().toString().equalsIgnoreCase(name + ".sc") ||
|
||||||
.findFirst();
|
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name + ".scl")))
|
||||||
if (scriptPath.isPresent())
|
.findFirst();
|
||||||
return Module.fromPath(scriptPath.get());
|
if (scriptPath.isPresent())
|
||||||
|
return Module.fromPath(scriptPath.get());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
CarpetSettings.LOG.error("Exception while loading the app: ", e);
|
CarpetSettings.LOG.error("Exception while loading the app: ", e);
|
||||||
@@ -204,18 +208,22 @@ public class CarpetScriptServer extends ScriptServer
|
|||||||
Path worldScripts = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
|
Path worldScripts = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
|
||||||
if (!Files.exists(worldScripts))
|
if (!Files.exists(worldScripts))
|
||||||
Files.createDirectories(worldScripts);
|
Files.createDirectories(worldScripts);
|
||||||
Files.list(worldScripts)
|
try (Stream<Path> folderLister = Files.list(worldScripts)) {
|
||||||
.filter(f -> f.toString().endsWith(".sc"))
|
folderLister
|
||||||
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
|
.filter(f -> f.toString().endsWith(".sc"))
|
||||||
|
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
|
||||||
|
}
|
||||||
|
|
||||||
if (includeBuiltIns && (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT))
|
if (includeBuiltIns && (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT))
|
||||||
{
|
{
|
||||||
Path globalScripts = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
Path globalScripts = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
||||||
if (!Files.exists(globalScripts))
|
if (!Files.exists(globalScripts))
|
||||||
Files.createDirectories(globalScripts);
|
Files.createDirectories(globalScripts);
|
||||||
Files.walk(globalScripts, FileVisitOption.FOLLOW_LINKS)
|
try (Stream<Path> folderWalker = Files.walk(globalScripts, FileVisitOption.FOLLOW_LINKS)) {
|
||||||
.filter(f -> f.toString().endsWith(".sc"))
|
folderWalker
|
||||||
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
|
.filter(f -> f.toString().endsWith(".sc"))
|
||||||
|
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
CarpetSettings.LOG.error("Exception while searching for apps: ", e);
|
CarpetSettings.LOG.error("Exception while searching for apps: ", e);
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ public record Module(String name, String code, boolean library) {
|
|||||||
{
|
{
|
||||||
Path dataFile = resolveResource(module);
|
Path dataFile = resolveResource(module);
|
||||||
if (dataFile == null) return null;
|
if (dataFile == null) return null;
|
||||||
if (!Files.exists(dataFile) || !(dataFile.toFile().isFile())) return null;
|
if (!Files.exists(dataFile) || !(Files.isRegularFile(dataFile))) return null;
|
||||||
synchronized (FileArgument.writeIOSync) { return FileArgument.readTag(dataFile); }
|
synchronized (FileArgument.writeIOSync) { return FileArgument.readTag(dataFile); }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,13 @@ public record Module(String name, String code, boolean library) {
|
|||||||
{
|
{
|
||||||
Path dataFile = resolveResource(module);
|
Path dataFile = resolveResource(module);
|
||||||
if (dataFile == null) return;
|
if (dataFile == null) return;
|
||||||
if (!Files.exists(dataFile.getParent()) && !dataFile.toFile().getParentFile().mkdirs()) return;
|
if (!Files.exists(dataFile.getParent())) {
|
||||||
|
try {
|
||||||
|
Files.createDirectories(dataFile.getParent());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
synchronized (FileArgument.writeIOSync) { FileArgument.writeTagDisk(globalState, dataFile, false); }
|
synchronized (FileArgument.writeIOSync) { FileArgument.writeTagDisk(globalState, dataFile, false); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import carpet.script.value.Value;
|
|||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import com.google.gson.stream.JsonReader;
|
|
||||||
import net.minecraft.ReportedException;
|
import net.minecraft.ReportedException;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.nbt.NbtAccounter;
|
import net.minecraft.nbt.NbtAccounter;
|
||||||
@@ -264,18 +263,16 @@ public class FileArgument
|
|||||||
public Stream<String> listFolder(Module module)
|
public Stream<String> listFolder(Module module)
|
||||||
{
|
{
|
||||||
Stream<String> strings;
|
Stream<String> strings;
|
||||||
try { synchronized (writeIOSync)
|
try (Stream<Path> result = listFiles(module)) { synchronized (writeIOSync)
|
||||||
{
|
{
|
||||||
Stream<Path> result;
|
|
||||||
result = listFiles(module);
|
|
||||||
if (result == null) return null;
|
if (result == null) return null;
|
||||||
Path rootPath = moduleRootPath(module);
|
Path rootPath = moduleRootPath(module);
|
||||||
if (rootPath == null) return null;
|
if (rootPath == null) return null;
|
||||||
String zipComponent = (zipContainer != null) ? rootPath.relativize(zipPath).toString() : null;
|
String zipComponent = (zipContainer != null) ? rootPath.relativize(zipPath).toString() : null;
|
||||||
|
// need to evaluate the stream before exiting try-with-resources else there'll be no data to stream
|
||||||
strings = (zipContainer == null)
|
strings = (zipContainer == null)
|
||||||
? result.map(p -> rootPath.relativize(p).toString().replaceAll("[\\\\/]+", "/"))
|
? result.map(p -> rootPath.relativize(p).toString().replaceAll("[\\\\/]+", "/")).toList().stream()
|
||||||
// need to remove ties to the zip file system before closing, so wrapping the stream
|
: result.map(p -> (zipComponent + '/'+ p.toString()).replaceAll("[\\\\/]+", "/")).toList().stream();
|
||||||
: result.map(p -> (zipComponent + '/'+ p.toString()).replaceAll("[\\\\/]+", "/")).collect(Collectors.toList()).stream();
|
|
||||||
} }
|
} }
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user