brigadier: wip signed arguments

This commit is contained in:
Bixilon 2023-01-07 16:39:31 +01:00
parent 90ccc42a17
commit 43176d274b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 60 additions and 17 deletions

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -15,13 +15,16 @@ package de.bixilon.minosoft.commands.nodes
import de.bixilon.minosoft.commands.nodes.builder.CommandNodeBuilder
import de.bixilon.minosoft.commands.parser.ArgumentParser
import de.bixilon.minosoft.commands.parser.SignedParser
import de.bixilon.minosoft.commands.stack.CommandExecutor
import de.bixilon.minosoft.commands.stack.CommandStack
import de.bixilon.minosoft.commands.suggestion.types.SuggestionType
import de.bixilon.minosoft.commands.util.CommandReader
class ArgumentNode : ExecutableNode {
class ArgumentNode : ExecutableNode, SignedNode {
private val parser: ArgumentParser<*>
override val sign: Boolean
get() = parser is SignedParser
constructor(
name: String,
@ -49,7 +52,7 @@ class ArgumentNode : ExecutableNode {
override fun execute(reader: CommandReader, stack: CommandStack) {
reader.skipWhitespaces(1)
val parsed = parser.parse(reader)
stack.push(name, parsed)
stack.push(this, parsed)
super.execute(reader, stack)
}
@ -61,7 +64,7 @@ class ArgumentNode : ExecutableNode {
// try parsing our argument
// it should succeed if we are not the last
val parsed = parser.parse(reader)
stack.push(name, parsed)
stack.push(this, parsed)
return super.getSuggestions(reader, stack)
} catch (error: Throwable) {
if (stack.size > stackSize + 1) {

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -48,7 +48,7 @@ class LiteralNode : ExecutableNode {
if (literalName != name && literalName !in aliases) {
throw InvalidLiteralArgumentError(reader, literalName)
}
stack.push(name, name)
stack.push(this, name)
return name
}
@ -62,7 +62,7 @@ class LiteralNode : ExecutableNode {
val literalName = reader.readUnquotedString()
if (literalName == name || literalName in aliases) {
stack.push(name, name)
stack.push(this, name)
return super.getSuggestions(reader, stack)
}
return suggester.suggest(literalName) ?: throw InvalidLiteralArgumentError(reader, literalName ?: "")

View File

@ -0,0 +1,18 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.nodes
interface SignedNode {
val sign: Boolean
}

View File

@ -0,0 +1,16 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser
interface SignedParser

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -85,7 +85,7 @@ object ArgumentParserFactories : DefaultFactory<ArgumentParserFactory<*>>(
minecraft:block_predicate
minecraft:item_stack
minecraft:item_predicate
minecraft:message
minecraft:message, SIGNED
minecraft:nbt
minecraft:nbt_path
minecraft:objective

View File

@ -14,6 +14,8 @@
package de.bixilon.minosoft.commands.stack
import de.bixilon.kutil.cast.CastUtil.nullCast
import de.bixilon.minosoft.commands.nodes.NamedNode
import de.bixilon.minosoft.commands.nodes.SignedNode
import de.bixilon.minosoft.commands.stack.print.PrintTarget
import de.bixilon.minosoft.commands.stack.print.SystemPrintTarget
import de.bixilon.minosoft.data.chat.signature.signer.MessageSigner
@ -43,7 +45,7 @@ class CommandStack(
}
fun getAny(name: String): Any? {
return stack.find { it.name == name }?.data
return stack.find { it.node.name == name }?.data
}
fun reset(size: Int) {
@ -51,14 +53,18 @@ class CommandStack(
stack.removeAll { index++ >= size }
}
fun push(name: String, data: Any?) {
stack.add(CommandStackEntry(name, data))
fun push(node: NamedNode, data: Any?) {
stack.add(CommandStackEntry(node, data))
}
fun sign(chain: MessageSigner, key: PrivateKey, salt: Long, time: Instant): Map<String, ByteArray> {
fun sign(signer: MessageSigner, key: PrivateKey, salt: Long, time: Instant): Map<String, ByteArray> {
val output: MutableMap<String, ByteArray> = mutableMapOf()
for (entry in stack) {
output[entry.name] = entry.sign(connection, chain, key, salt, time)
if (entry.node !is SignedNode || !entry.node.sign) {
continue
}
// TODO: properly sign
output[entry.node.name] = entry.sign(connection, signer, key, salt, time)
}
return output
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.commands.stack
import de.bixilon.minosoft.commands.nodes.NamedNode
import de.bixilon.minosoft.data.chat.signature.LastSeenMessageList
import de.bixilon.minosoft.data.chat.signature.signer.MessageSigner
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@ -20,7 +21,7 @@ import java.security.PrivateKey
import java.time.Instant
data class CommandStackEntry(
val name: String,
val node: NamedNode,
val data: Any?,
) {

View File

@ -36,7 +36,7 @@ interface MessageSigner {
if (version < ProtocolVersions.V_22W42A) {
return MessageSigner2(version)
}
return MessageSigner3(version, connection)
return MessageSigner3(version, connection.sessionId)
}
}
}

View File

@ -62,7 +62,6 @@ class MessageSigner3(
signature.update(lastSeenMessage.signature)
}
return signature.sign()
}
}