From 33397ec3101e5a67292a69095c3c1012b311ddac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 13 Sep 2014 23:29:36 +0200 Subject: [PATCH] Fixed broken Java 1.7 runtime check. --- .../scala/li/cil/oc/common/SaveHandler.scala | 49 ++++++++++--------- .../component/machine/luac/ComputerAPI.scala | 2 +- .../component/machine/luaj/ComputerAPI.scala | 2 +- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/SaveHandler.scala b/src/main/scala/li/cil/oc/common/SaveHandler.scala index 289e5ca09..1fab21e06 100644 --- a/src/main/scala/li/cil/oc/common/SaveHandler.scala +++ b/src/main/scala/li/cil/oc/common/SaveHandler.scala @@ -14,6 +14,7 @@ import net.minecraft.world.{ChunkCoordIntPair, World} import net.minecraftforge.common.DimensionManager import net.minecraftforge.event.ForgeSubscribe import net.minecraftforge.event.world.{ChunkDataEvent, WorldEvent} +import org.apache.commons.lang3.{JavaVersion, SystemUtils} import scala.collection.mutable @@ -181,31 +182,33 @@ object SaveHandler { // Touch all externally saved data when loading, to avoid it getting // deleted in the next save (because the now - save time will usually // be larger than the time out after loading a world again). - try { - Files.walkFileTree(statePath.toPath, new FileVisitor[Path] { - override def visitFile(file: Path, attrs: BasicFileAttributes) = { - file.toFile.setLastModified(System.currentTimeMillis()) - FileVisitResult.CONTINUE - } + if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_7)) visitJava17() + else visitJava16() + } - override def visitFileFailed(file: Path, exc: IOException) = FileVisitResult.CONTINUE - - override def preVisitDirectory(dir: Path, attrs: BasicFileAttributes) = FileVisitResult.CONTINUE - - override def postVisitDirectory(dir: Path, exc: IOException) = FileVisitResult.CONTINUE - }) - } - catch { - case _: Throwable => - // Most likely we're on Java 1.6, so we can't use the walker... - // Which means we may run into infinite loops if there are - // evil symlinks. But that's really not something I'm bothered by. - def recurse(file: File) { - file.setLastModified(System.currentTimeMillis()) - if (file.isDirectory) file.listFiles().foreach(recurse) - } - recurse(statePath) + private def visitJava16() { + // This may run into infinite loops if there are evil symlinks. + // But that's really not something I'm bothered by, it's a fallback. + def recurse(file: File) { + file.setLastModified(System.currentTimeMillis()) + if (file.isDirectory) file.listFiles().foreach(recurse) } + recurse(statePath) + } + + private def visitJava17() { + Files.walkFileTree(statePath.toPath, new FileVisitor[Path] { + override def visitFile(file: Path, attrs: BasicFileAttributes) = { + file.toFile.setLastModified(System.currentTimeMillis()) + FileVisitResult.CONTINUE + } + + override def visitFileFailed(file: Path, exc: IOException) = FileVisitResult.CONTINUE + + override def preVisitDirectory(dir: Path, attrs: BasicFileAttributes) = FileVisitResult.CONTINUE + + override def postVisitDirectory(dir: Path, exc: IOException) = FileVisitResult.CONTINUE + }) } @ForgeSubscribe diff --git a/src/main/scala/li/cil/oc/server/component/machine/luac/ComputerAPI.scala b/src/main/scala/li/cil/oc/server/component/machine/luac/ComputerAPI.scala index ba6b32850..48cf30670 100644 --- a/src/main/scala/li/cil/oc/server/component/machine/luac/ComputerAPI.scala +++ b/src/main/scala/li/cil/oc/server/component/machine/luac/ComputerAPI.scala @@ -51,7 +51,7 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) { lua.pushScalaFunction(lua => { if (lua.isNoneOrNil(1)) owner.bootAddress = "" - else owner.bootAddress = lua.checkString(1) + else owner.bootAddress = lua.checkString(1).take(36) 0 }) lua.setField(-2, "setBootAddress") diff --git a/src/main/scala/li/cil/oc/server/component/machine/luaj/ComputerAPI.scala b/src/main/scala/li/cil/oc/server/component/machine/luaj/ComputerAPI.scala index 308ebd64f..31510bc65 100644 --- a/src/main/scala/li/cil/oc/server/component/machine/luaj/ComputerAPI.scala +++ b/src/main/scala/li/cil/oc/server/component/machine/luaj/ComputerAPI.scala @@ -35,7 +35,7 @@ class ComputerAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) { computer.set("setBootAddress", (args: Varargs) => { if (args.isnoneornil(1)) owner.bootAddress = "" - else owner.bootAddress = args.checkjstring(1) + else owner.bootAddress = args.checkjstring(1).take(36) LuaValue.NIL })