2

I am making my first public server modification for Crysis Wars and to ensure nobody steals my code, I'm putting as much as possible within the C++ based DLL (the alternative is Lua). To do this I have to put commands in the DLL, some of which require additional variables. Here's an example:

!ban [playername] [time] [reason]

How would I go about retrieving variables playername, time and reason, all of which have different character lengths? The reason variable may also have more than one word (such as 'offensive messages and cheating') that will need to be picked up.

In Lua this will be done with a simple 'string.match'; I suppose I could always do the message sorting in Lua then get it to send back to C++, but this could cause the whole chatcommand system to be dis-organised.

It would need to extract the variables from the 'const char *msg', which is analysed by the system on every message sent. I already analyse it for command messages (those which begin with '!'). What is the best way to do this?

Examples: !ban Con 5 spam- This will kick the player 'Confl!ct' (I already have the partial scan code to identify partial names) for five minutes

!ban Con spam- This will permanently ban the player 'Confl!ct'

AStopher
  • 4,207
  • 11
  • 50
  • 75
  • "How would I go about retrieving variables playername, time and reason" I don't get it. Please ask it again in different words. – Michał Walenciak Sep 25 '13 at 17:55
  • How would I use string::match or similar to extract these variables from a chatmessage. I'll update the original post now with the info. Thanks for pointing that out! :) – AStopher Sep 25 '13 at 17:56
  • If the game uses Lua for scripting, why would you care if anyone tried to 'steal' your code? All you're doing is making a wrapper for console commands (from what I could see.) –  Sep 25 '13 at 17:58
  • @AlexanderStopher: use boost::regex or std::regex if you have a compiler with c++11 support – Michał Walenciak Sep 25 '13 at 17:59
  • In this game, there are certain people who basically steal as much Lua code as possible and claim that they created it. If I put the commands within the DLL, they will not be able to access it without the sourcecode (which I will not be releasing). – AStopher Sep 25 '13 at 17:59
  • @MichałWalenciak- I use Visual Studio 2008 and the built-in compiler. I cannot use a higher version of Visual Studio as they cause the compiler to whine about errors that are not present. – AStopher Sep 25 '13 at 18:02
  • You could also be putting malicious code in the DLL. If you released the source code, people would be able to trust it. And as I said, it seems to be a petty thing to steal code that is essentially a wrapper for scripting commands. –  Sep 25 '13 at 18:02
  • @AlexanderStopher: then you can use boost with your VS, as it doesn't support c++11 – Michał Walenciak Sep 25 '13 at 18:03
  • @MichałWalenciak- I know, I'm releasing private and public editions and I want to easily disable the commands within the private edition without the risk of someone else adding them into the public edition. – AStopher Sep 25 '13 at 18:05

1 Answers1

0

The way to go here is to use Regular Expressions, aka regex. As I was going through my old questions, smartening them up, I quickly used txt2re to cook up a quick regex recipe, as seen below:

#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;
  }
}

This code requires these libraries as C++ does not include a regular expressions function by default:

AStopher
  • 4,207
  • 11
  • 50
  • 75