解析游戏的控制台命令?

发布于 2024-12-26 10:04:55 字数 857 浏览 1 评论 0原文

我需要能够处理看起来像这样的数据:

set setting1 "bind button_x +actionslot1;bind button_y \" bind button_x +stance \" "

bind button_a jump

set setting2 1 1 0 1

toggle setting_3 " \"value 1\" \"value 2\" \"value 3\" "

这些是游戏控制台的一些命令的样子,我正在尝试编写一个模拟器,它将以与游戏相同的方式解释代码将要。

我首先想到的是正则表达式,但我不确定它是最好的选择。例如,当匹配设置的值时,我可能会尝试类似 /set [\w_]+ "?(.+)"?/ 的内容,但通配符与结尾引号匹配,因为它是不是懒惰,但如果我让它变得懒惰,它就会与值内的引号匹配。如果我让它变得贪婪并阻止它匹配引号,它就不会匹配值中的转义引号。

即使有可能的正则表达式解决方案,它们似乎也是错误的选择。我之前曾询问过像 Visual Studio 和 Notepad++ 这样的程序如何知道哪些括号和大括号匹配,我被告知在某些方面与正则表达式类似,但功能更强大。

我唯一能想到的另一件事是逐个字符地遍历代码行并使用布尔值来确定当前字符的状态。

我在这里有什么选择?游戏开发人员使用什么来处理控制台命令?

编辑:这是另一个可能的命令,它强烈阻止我使用正则表达式:

set setting4 "bind button_a \" bind button_b "\" set setting1 0 \" " \" "

这些命令不仅包括转义引号,还包括转义引号内 "\" 方式的引号。

I need to be able to handle data that can look like:

set setting1 "bind button_x +actionslot1;bind button_y \" bind button_x +stance \" "

bind button_a jump

set setting2 1 1 0 1

toggle setting_3 " \"value 1\" \"value 2\" \"value 3\" "

These are what some of the commands for the console of a game look like, and I'm trying to write an emulator of sorts that will interpret the code the same way the game will.

The first thing that comes to mind is regex, but I'm not sure it's the best option. For example, when matching for the value of a setting, I might trying something like /set [\w_]+ "?(.+)"?/, but the wildcard matches the ending quote because it's not lazy, but if I make it lazy, it matches the quote inside the value. If I make it greedy and stop it from matching the quotes, it won't match the escaped quotes in the values.

Even if there are possible regex solutions, they seem like the wrong option. I had asked before about how programs like Visual Studio and Notepad++ know which parentheses and curly braces matched, and I was told there was something similar to regex in some ways but much more powerful.

The only other thing I can think of is to go through the lines of code character by character and use booleans to determine that state of the current character.

What are my options here? What do game developers use to handle console commands?

edit: Here's another possible command which strongly deters me from using regex:

set setting4 "bind button_a \" bind button_b "\" set setting1 0 \" " \" "

The commands include not just escaped quotes, but quotes of the manner "\" inside escaped quotes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2025-01-02 10:04:55

我建议您阅读词法分析
,这是使用语法对文本进行标记的过程。
我认为这会对您想做的事情有所帮助。

I would suggest you read about Lexical Analysis
, this is the process of tokenizing your text using a grammar.
I think it will help you with what you are trying to do.

独享拥抱 2025-01-02 10:04:55

我不想让你继续走正则表达式的道路——你是对的,有可能更合适的非正则表达式解决方案(我只是不知道它们是什么)。但是,这里有一个可能的正则表达式可以解决您的引号问题:

/set [\w_]+ "?((\\"|[^"])+)"?/

我将 .+ 更改为 (\\"|[^"])+。基本上,它匹配 \" 或任何非引号的出现。换句话说,它将匹配除未转义的引号之外的任何内容。

同样,如果有人可以建议更复杂的方法非正则表达式解决方案,您应该强烈考虑它

编辑:您提供的更新示例会破坏此解决方案,并且我认为它会破坏任何正则表达式解决方案

编辑2: 。 > 这是您的 C# 字符串版本它使用 @ 告诉编译器将字符串视为 逐字文字,这意味着它会忽略 \ 作为转义字符。唯一需要注意的是,为了在逐字文字中表示 ",您必须键入。它作为"",但它仍然比到处都是斜杠要好。鉴于正则表达式中转义序列的普遍存在,我建议在必须在字符串中键入正则表达式的任何地方使用逐字文字。

string pattern = @"set [\w_]+ ""?((\\""|[^""])+)""?"

I don't want to keep you on the path of regex -- you are correct that there are non-regex solutions that may be more appropriate (I just don't know what they are). However, here is one possible regex that should fix your quotes issue:

/set [\w_]+ "?((\\"|[^"])+)"?/

I changed .+ to (\\"|[^"])+. Basically it's matching occurrences of \" OR of anything that isn't a quote. In other words, it will will match anything except quotes that aren't escaped.

Again, if someone can suggest a more sophisticated non-regex solution, you should strongly consider it.

Edit: The updated example you've provided breaks this solution, and I think it would break any regex solution.

Edit 2: Here is a C# string version of your regex. It uses @ to tell the compiler to treat the string as a verbatim literal, which means it ignores \ as an escape character. The only caveat is that in order to represent " in a verbatim literal you have to type it as "", but it's still better than having slashes everywhere. Given the prevalence of escape sequences in regexes, I recommend using verbatim literals anywhere that you have to type a regex in a string.

string pattern = @"set [\w_]+ ""?((\\""|[^""])+)""?"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文