2

我正在为 Crysis Wars 进行我的第一个公共服务器修改,并确保没有人窃取我的代码,我将尽可能多地放在基于 C++ 的 DLL 中(替代方案是 Lua)。为此,我必须将命令放入 DLL 中,其中一些需要额外的变量。这是一个例子:

!ban [玩家名] [时间] [原因]

我将如何检索变量 playername、time 和 reason,所有这些变量都有不同的字符长度?原因变量也可能有多个需要选择的词(例如“冒犯性消息和作弊”)。

在 Lua 中,这将通过一个简单的 'string.match' 来完成;我想我总是可以在 Lua 中进行消息排序,然后将其发送回 C++,但这可能会导致整个聊天命令系统混乱。

它需要从“const char *msg”中提取变量,系统会根据发送的每条消息对其进行分析。我已经分析了它的命令消息(以'!'开头的那些)。做这个的最好方式是什么?

示例: !ban Con 5 spam - 这将踢玩家 'Confl!ct' (我已经有部分扫描码来识别部分名称)五分钟

!ban Con spam - 这将永久禁止玩家“Confl!ct”

4

1 回答 1

0

去这里的方法是使用正则表达式,也就是regex。当我在处理我的旧问题时,将它们变得聪明起来,我迅速使用txt2re 制作了一个快速的正则表达式配方,如下所示:

#include <stdlib.h>
#include <string>
#include <iostream>
#include <pme.h>

int main()
{
  std::string txt="!ban command variables";

  std::string re1=".*?";    // Non-greedy match on filler
  std::string re2="((?:[a-z][a-z]+))";  // Word 1
  std::string re3=".*?";    // Non-greedy match on filler
  std::string re4="((?:[a-z][a-z]+))";  // Word 2
  std::string re5=".*?";    // Non-greedy match on filler
  std::string re6="((?:[a-z][a-z]+))";  // Word 3

  PME re(re1+re2+re3+re4+re5+re6,"gims");
  int n;
  if ((n=re.match(txt))>0)
  {
      std::string word1=re[1].c_str();
      std::string word2=re[2].c_str();
      std::string word3=re[3].c_str();
      std::cout << "("<<word1<<")"<<"("<<word2<<")"<<"("<<word3<<")"<< std::endl;
  }
}

此代码需要这些库,因为默认情况下 C++ 不包含正则表达式函数:

于 2015-01-26T22:45:03.860 回答