Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit 65a0bb1

Browse files
committed
Merge branch 'main' into SBX-7-custom-items
2 parents 9941d5f + bd0a17e commit 65a0bb1

7 files changed

Lines changed: 89 additions & 30 deletions

File tree

src/main/kotlin/org/sandboxpowered/fabric/api/PolyglotStateProperty.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@ import org.graalvm.polyglot.HostAccess.Export
55

66
class PolyglotStateProperty private constructor(
77
private val property: Property<*>?,
8-
@Export val name: String,
9-
@Export val type: String,
10-
val extra: Array<out Any>
8+
@JvmField @Export val name: String,
9+
@JvmField @Export val type: String,
10+
@JvmField @Export val values: Array<*>
1111
) {
12-
// TODO: Coded : figure out what to do with this
13-
//Int Property
14-
@Export
15-
fun values(): Array<Int> {
16-
if (type == INT) return extra as Array<Int>
17-
throw UnsupportedOperationException("Cannot get minimum value of $type property")
18-
}
19-
2012
companion object {
2113
infix fun from(property: Property<*>) = PolyglotStateProperty(
2214
property = property,
@@ -28,17 +20,17 @@ class PolyglotStateProperty private constructor(
2820
is EnumProperty -> ENUM
2921
else -> error("Unknown Property type: $property")
3022
},
31-
extra = when (property) {
23+
values = when (property) {
3224
is BooleanProperty -> emptyArray()
3325
else -> property.values.toTypedArray()
3426
}
3527
)
3628

37-
fun from(name: String, type: String, extra: Array<out Any> = emptyArray()) = PolyglotStateProperty(
29+
fun from(name: String, type: String, extra: Array<*> = emptyArray<Any>()) = PolyglotStateProperty(
3830
property = null,
3931
name = name,
4032
type = type,
41-
extra = extra
33+
values = extra
4234
)
4335

4436
const val BOOLEAN = "boolean"

src/main/kotlin/org/sandboxpowered/fabric/api/SandboxResourcePolyglotContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class SandboxResourcePolyglotContext(private val resource: String, private val s
5858
}
5959

6060
@Export
61-
fun getStateProperty(name: String, type: String, vararg extra: Value): PolyglotStateProperty? {
61+
fun getStateProperty(name: String, type: String, vararg extra: Value): PolyglotStateProperty {
6262
return StateManagement.getStateProperty(name, type, *extra)
6363
}
6464
}

src/main/kotlin/org/sandboxpowered/fabric/api/StateManagement.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.sandboxpowered.fabric.api
22

3-
import com.google.common.hash.Hashing
43
import net.minecraft.state.property.Property
54
import org.graalvm.polyglot.Value
6-
import java.nio.charset.StandardCharsets
5+
import org.sandboxpowered.fabric.util.Hash
6+
import org.sandboxpowered.fabric.util.hash
77

88
object StateManagement {
99
private val stateMap: MutableMap<String, PolyglotStateProperty> = hashMapOf()
@@ -14,7 +14,10 @@ object StateManagement {
1414
stateMap.computeIfAbsent(hash(name, type)) { PolyglotStateProperty.from(name, type) }
1515
}
1616
PolyglotStateProperty.INT -> {
17-
val values = extra.map(Value::asInt).toTypedArray()
17+
require(extra.size == 2) { "Invalid number of arguments specified, expected 2 got ${extra.size}" }
18+
var values = extra.map(Value::asInt).toTypedArray()
19+
20+
values = (values[0]..values[1]).toList().toTypedArray()
1821
stateMap.computeIfAbsent(hash(name, type, values)) {
1922
PolyglotStateProperty.from(name, type, values)
2023
}
@@ -29,15 +32,14 @@ object StateManagement {
2932
}
3033

3134
private fun PolyglotStateProperty.hash(): String =
32-
hash(name, type, extra)
35+
hash(name, type, values)
3336

34-
private fun hash(name: String, type: String, extra: Array<out Any> = emptyArray()): String {
37+
private fun hash(name: String, type: String, extra: Array<*> = emptyArray<Any>()): String {
3538
val compiled = buildString {
3639
append(name)
3740
append(type)
3841
extra.forEach(this::append)
3942
}
40-
@Suppress("UnstableApiUsage") // apparently we ain't got anything better
41-
return Hashing.sha256()?.hashString(compiled, StandardCharsets.UTF_8)?.toString() ?: compiled
43+
return compiled.hash(Hash.SHA256)
4244
}
4345
}

src/main/kotlin/org/sandboxpowered/fabric/scripting/PolyglotScriptLoader.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import org.sandboxpowered.fabric.api.SandboxResourcePolyglotContext
77
import org.sandboxpowered.fabric.api.item.PolyglotGlobalItemManager
88
import org.sandboxpowered.fabric.api.item.PolyglotItemManager
99
import org.sandboxpowered.fabric.scripting.polyglot.PolyglotFileSystem
10+
import org.sandboxpowered.fabric.util.TimingUtil
1011
import java.util.concurrent.Executors
11-
import java.util.concurrent.TimeUnit
12-
import java.util.concurrent.TimeoutException
1312

1413
class PolyglotScriptLoader {
1514
private val executor = Executors.newSingleThreadExecutor()
@@ -56,12 +55,14 @@ class PolyglotScriptLoader {
5655
val bindings = context.getBindings(scriptSource.language)
5756
bindings.putMember("sandbox", getPolyglotContext(resource))
5857

59-
try {
60-
executor.submit { context.eval(scriptSource) }.get(5, TimeUnit.SECONDS)
61-
} catch (exception: TimeoutException) {
62-
error("Script ${scriptSource.name} failed to run in under 5 seconds")
63-
} catch (exception: Exception) {
64-
exception.printStackTrace()
58+
val result = TimingUtil.execute(func = { context.eval(scriptSource) }, executor = executor)
59+
60+
if (result is TimingUtil.Timeout) {
61+
println("Script $resource:${scriptSource.name} failed to run in under 5 seconds")
62+
//TODO: cancel and unload this script's resource due to failure, throw error if addon is deemed required
63+
} else if (result is TimingUtil.Error) {
64+
throw RuntimeException("Encountered unknown error when executing script $resource:${scriptSource.name}",
65+
result.error)
6566
}
6667
}
6768

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.sandboxpowered.fabric.util
2+
3+
import java.util.concurrent.ExecutorService
4+
import java.util.concurrent.Executors
5+
import java.util.concurrent.TimeUnit
6+
import java.util.concurrent.TimeoutException
7+
8+
object TimingUtil {
9+
private val executor = Executors.newSingleThreadExecutor()
10+
11+
fun execute(
12+
func: () -> Unit,
13+
timeout: Long = 5,
14+
unit: TimeUnit = TimeUnit.SECONDS,
15+
executor: ExecutorService? = null,
16+
): Result<Any, Exception> {
17+
return try {
18+
Success((executor ?: this.executor).submit(func).get(timeout, unit))
19+
} catch (exception: TimeoutException) {
20+
Timeout(exception)
21+
} catch (exception: Exception) {
22+
Error(exception)
23+
}
24+
}
25+
26+
fun <T> executeWithResponse(
27+
func: () -> T,
28+
timeout: Long = 5,
29+
unit: TimeUnit = TimeUnit.SECONDS,
30+
executor: ExecutorService? = null,
31+
): Result<T, Exception> {
32+
return try {
33+
Success((executor ?: this.executor).submit(func).get(timeout, unit))
34+
} catch (exception: TimeoutException) {
35+
Timeout(exception)
36+
} catch (exception: Exception) {
37+
Error(exception)
38+
}
39+
}
40+
41+
sealed class Result<out T, out E : Throwable>
42+
class Success<out T>(val value: T) : Result<T, Nothing>()
43+
class Timeout<out E : Throwable>(error: E) : Error<E>(error)
44+
open class Error<out E : Throwable>(val error: E) : Result<Nothing, E>()
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
package org.sandboxpowered.fabric.util
4+
5+
import com.google.common.hash.HashFunction
6+
import com.google.common.hash.Hashing
7+
import java.nio.charset.Charset
8+
import java.nio.charset.StandardCharsets
9+
10+
fun String.hash(hash: Hash, charset: Charset = StandardCharsets.UTF_8): String {
11+
return hash.hashFunction.hashString(this, charset).toString()
12+
}
13+
14+
@Suppress("unused")
15+
enum class Hash(val hashFunction: HashFunction) {
16+
MD5(Hashing.md5()),
17+
SHA256(Hashing.sha256()),
18+
SHA512(Hashing.sha512()),
19+
}

src/main/kotlin/org/sandboxpowered/fabric/util/PolyglotUtil.kt renamed to src/main/kotlin/org/sandboxpowered/fabric/util/polyglot.kt

File renamed without changes.

0 commit comments

Comments
 (0)