From bc8c29812f928e1bedb2eddb29cf4380c1cba0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 20 Jun 2014 22:10:43 +0200 Subject: [PATCH] Fixed primaries being determined using the fuzzy component.list getter by adding a second parameter to list, `exact`, which will suppress fuzzy search. This prevents the inventory controller upgrade to be returned for `component.inventory`, for example, and will only return it for `component.inventory_controller` now, as was originally intended. --- .../assets/opencomputers/loot/OpenOS/boot/03_component.lua | 6 +++--- src/main/resources/assets/opencomputers/lua/kernel.lua | 4 ++-- .../cil/oc/server/component/machine/luac/ComponentAPI.scala | 4 +++- .../cil/oc/server/component/machine/luaj/ComponentAPI.scala | 4 +++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/OpenOS/boot/03_component.lua b/src/main/resources/assets/opencomputers/loot/OpenOS/boot/03_component.lua index a4cccfa9d..2effd4be2 100644 --- a/src/main/resources/assets/opencomputers/loot/OpenOS/boot/03_component.lua +++ b/src/main/resources/assets/opencomputers/loot/OpenOS/boot/03_component.lua @@ -17,7 +17,7 @@ setmetatable(component, { __index = function(_, key) function component.get(address, componentType) checkArg(1, address, "string") checkArg(2, componentType, "string", "nil") - for c in component.list(componentType) do + for c in component.list(componentType, true) do if c:sub(1, address:len()) == address then return c end @@ -31,7 +31,7 @@ function component.isAvailable(componentType) -- This is mostly to avoid out of memory errors preventing proxy -- creation cause confusion by trying to create the proxy again, -- causing the oom error to be thrown again. - component.setPrimary(componentType, component.list(componentType)()) + component.setPrimary(componentType, component.list(componentType, true)()) end return primaries[componentType] ~= nil end @@ -108,7 +108,7 @@ local function onComponentRemoved(_, address, componentType) if primaries[componentType] and primaries[componentType].address == address or adding[componentType] and adding[componentType].address == address then - component.setPrimary(componentType, component.list(componentType)()) + component.setPrimary(componentType, component.list(componentType, true)()) end end diff --git a/src/main/resources/assets/opencomputers/lua/kernel.lua b/src/main/resources/assets/opencomputers/lua/kernel.lua index 402177213..87be3e92e 100644 --- a/src/main/resources/assets/opencomputers/lua/kernel.lua +++ b/src/main/resources/assets/opencomputers/lua/kernel.lua @@ -464,9 +464,9 @@ libcomponent = { end error("no such method", 1) end, - list = function(filter) + list = function(filter, exact) checkArg(1, filter, "string", "nil") - local list = spcall(component.list, filter) + local list = spcall(component.list, filter, not not exact) local key = nil return function() key = next(list, key) diff --git a/src/main/scala/li/cil/oc/server/component/machine/luac/ComponentAPI.scala b/src/main/scala/li/cil/oc/server/component/machine/luac/ComponentAPI.scala index cec8fcb1f..f07a57f3c 100644 --- a/src/main/scala/li/cil/oc/server/component/machine/luac/ComponentAPI.scala +++ b/src/main/scala/li/cil/oc/server/component/machine/luac/ComponentAPI.scala @@ -12,9 +12,11 @@ class ComponentAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) { lua.pushScalaFunction(lua => components.synchronized { val filter = if (lua.isString(1)) Option(lua.toString(1)) else None + val exact = if (lua.isBoolean(2)) lua.toBoolean(2) else true lua.newTable(0, components.size) + def matches(name: String) = if (exact) name == filter.get else name.contains(filter.get) for ((address, name) <- components) { - if (filter.isEmpty || name.contains(filter.get)) { + if (filter.isEmpty || matches(name)) { lua.pushString(address) lua.pushString(name) lua.rawSet(-3) diff --git a/src/main/scala/li/cil/oc/server/component/machine/luaj/ComponentAPI.scala b/src/main/scala/li/cil/oc/server/component/machine/luaj/ComponentAPI.scala index 1d3fd88b7..27be57838 100644 --- a/src/main/scala/li/cil/oc/server/component/machine/luaj/ComponentAPI.scala +++ b/src/main/scala/li/cil/oc/server/component/machine/luaj/ComponentAPI.scala @@ -14,9 +14,11 @@ class ComponentAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) { component.set("list", (args: Varargs) => components.synchronized { val filter = if (args.isstring(1)) Option(args.tojstring(1)) else None + val exact = args.optboolean(2, false) val table = LuaValue.tableOf(0, components.size) + def matches(name: String) = if (exact) name == filter.get else name.contains(filter.get) for ((address, name) <- components) { - if (filter.isEmpty || name.contains(filter.get)) { + if (filter.isEmpty || matches(name)) { table.set(address, name) } }