mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-15 05:57:38 +00:00
Some core cleanup
- Move some ArgumentHelpers methods to the core project. - Remove static imports in CobaltLuaMachine, avoiding confusing calls to valueOf.
This commit is contained in:
@@ -19,18 +19,6 @@ public final class ArgumentHelpers {
|
|||||||
private ArgumentHelpers() {
|
private ArgumentHelpers() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertBetween(double value, double min, double max, String message) throws LuaException {
|
|
||||||
if (value < min || value > max || Double.isNaN(value)) {
|
|
||||||
throw new LuaException(String.format(message, "between " + min + " and " + max));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void assertBetween(int value, int min, int max, String message) throws LuaException {
|
|
||||||
if (value < min || value > max) {
|
|
||||||
throw new LuaException(String.format(message, "between " + min + " and " + max));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> T getRegistryEntry(String name, String typeName, RegistryWrappers.RegistryWrapper<T> registry) throws LuaException {
|
public static <T> T getRegistryEntry(String name, String typeName, RegistryWrappers.RegistryWrapper<T> registry) throws LuaException {
|
||||||
ResourceLocation id;
|
ResourceLocation id;
|
||||||
try {
|
try {
|
||||||
|
@@ -30,9 +30,6 @@ import java.io.Serial;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.squiddev.cobalt.ValueFactory.valueOf;
|
|
||||||
import static org.squiddev.cobalt.ValueFactory.varargsOf;
|
|
||||||
|
|
||||||
public class CobaltLuaMachine implements ILuaMachine {
|
public class CobaltLuaMachine implements ILuaMachine {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(CobaltLuaMachine.class);
|
private static final Logger LOG = LoggerFactory.getLogger(CobaltLuaMachine.class);
|
||||||
|
|
||||||
@@ -76,8 +73,8 @@ public class CobaltLuaMachine implements ILuaMachine {
|
|||||||
var globals = state.getMainThread().getfenv();
|
var globals = state.getMainThread().getfenv();
|
||||||
CoreLibraries.debugGlobals(state);
|
CoreLibraries.debugGlobals(state);
|
||||||
Bit32Lib.add(state, globals);
|
Bit32Lib.add(state, globals);
|
||||||
globals.rawset("_HOST", valueOf(environment.hostString()));
|
globals.rawset("_HOST", ValueFactory.valueOf(environment.hostString()));
|
||||||
globals.rawset("_CC_DEFAULT_SETTINGS", valueOf(CoreConfig.defaultComputerSettings));
|
globals.rawset("_CC_DEFAULT_SETTINGS", ValueFactory.valueOf(CoreConfig.defaultComputerSettings));
|
||||||
if (CoreConfig.disableLua51Features) globals.rawset("_CC_DISABLE_LUA51_FEATURES", Constants.TRUE);
|
if (CoreConfig.disableLua51Features) globals.rawset("_CC_DISABLE_LUA51_FEATURES", Constants.TRUE);
|
||||||
|
|
||||||
// Add default APIs
|
// Add default APIs
|
||||||
@@ -121,7 +118,7 @@ public class CobaltLuaMachine implements ILuaMachine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var resumeArgs = eventName == null ? Constants.NONE : varargsOf(valueOf(eventName), toValues(arguments));
|
var resumeArgs = eventName == null ? Constants.NONE : ValueFactory.varargsOf(ValueFactory.valueOf(eventName), toValues(arguments));
|
||||||
|
|
||||||
// Resume the current thread, or the main one when first starting off.
|
// Resume the current thread, or the main one when first starting off.
|
||||||
var thread = state.getCurrentThread();
|
var thread = state.getCurrentThread();
|
||||||
@@ -189,14 +186,14 @@ public class CobaltLuaMachine implements ILuaMachine {
|
|||||||
|
|
||||||
private LuaValue toValue(@Nullable Object object, @Nullable IdentityHashMap<Object, LuaValue> values) {
|
private LuaValue toValue(@Nullable Object object, @Nullable IdentityHashMap<Object, LuaValue> values) {
|
||||||
if (object == null) return Constants.NIL;
|
if (object == null) return Constants.NIL;
|
||||||
if (object instanceof Number num) return valueOf(num.doubleValue());
|
if (object instanceof Number num) return ValueFactory.valueOf(num.doubleValue());
|
||||||
if (object instanceof Boolean bool) return valueOf(bool);
|
if (object instanceof Boolean bool) return ValueFactory.valueOf(bool);
|
||||||
if (object instanceof String str) return valueOf(str);
|
if (object instanceof String str) return ValueFactory.valueOf(str);
|
||||||
if (object instanceof byte[] b) return valueOf(Arrays.copyOf(b, b.length));
|
if (object instanceof byte[] b) return ValueFactory.valueOf(Arrays.copyOf(b, b.length));
|
||||||
if (object instanceof ByteBuffer b) {
|
if (object instanceof ByteBuffer b) {
|
||||||
var bytes = new byte[b.remaining()];
|
var bytes = new byte[b.remaining()];
|
||||||
b.get(bytes);
|
b.get(bytes);
|
||||||
return valueOf(bytes);
|
return ValueFactory.valueOf(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (values == null) values = new IdentityHashMap<>(1);
|
if (values == null) values = new IdentityHashMap<>(1);
|
||||||
@@ -261,7 +258,7 @@ public class CobaltLuaMachine implements ILuaMachine {
|
|||||||
var object = objects[i];
|
var object = objects[i];
|
||||||
values[i] = toValue(object, result);
|
values[i] = toValue(object, result);
|
||||||
}
|
}
|
||||||
return varargsOf(values);
|
return ValueFactory.varargsOf(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@@ -0,0 +1,27 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
|
package dan200.computercraft.core.util;
|
||||||
|
|
||||||
|
import dan200.computercraft.api.lua.LuaException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A few helpers for working with arguments.
|
||||||
|
* <p>
|
||||||
|
* This should really be moved into the public API. However, until I have settled on a suitable format, we'll keep it
|
||||||
|
* where it is used.
|
||||||
|
*/
|
||||||
|
public class ArgumentHelpers {
|
||||||
|
public static void assertBetween(double value, double min, double max, String message) throws LuaException {
|
||||||
|
if (value < min || value > max || Double.isNaN(value)) {
|
||||||
|
throw new LuaException(String.format(message, "between " + min + " and " + max));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void assertBetween(int value, int min, int max, String message) throws LuaException {
|
||||||
|
if (value < min || value > max) {
|
||||||
|
throw new LuaException(String.format(message, "between " + min + " and " + max));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -30,7 +30,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static dan200.computercraft.shared.util.ArgumentHelpers.assertBetween;
|
import static dan200.computercraft.core.util.ArgumentHelpers.assertBetween;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Methods for interacting with inventories. This mirrors the Forge version.
|
* Methods for interacting with inventories. This mirrors the Forge version.
|
||||||
|
@@ -25,7 +25,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static dan200.computercraft.shared.util.ArgumentHelpers.assertBetween;
|
import static dan200.computercraft.core.util.ArgumentHelpers.assertBetween;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Methods for interacting with inventories.
|
* Methods for interacting with inventories.
|
||||||
|
Reference in New Issue
Block a user