core/sourcehook/generate/shworker/shworker.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 684 8015336442cb
child 733 2365240861a2
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     1 // SHWorker
     2 // Inspired by "Hopter" that comes with FastDelegate (http://www.codeproject.com/cpp/FastDelegate.asp)
     3 // Much more powerful (and ugly) though
     4 
     5 /*
     6 
     7 	INPUT FILE DIRECTIVES
     8 
     9 	$a is the first additional argument, $b the second, ...
    10 
    11 	---
    12 	ITERATION
    13 
    14 	@[variable,min,max:code|separator@]
    15 
    16 	variable:	this will be replaced in code by its current value.
    17 				vars are always $ and a number.
    18 	min:		first value to be used for variable
    19 	max:		last value to be used for variable
    20 	code:		the code that will be inserted on each iteration.
    21 	separator:	optional. this will be inserted between iterations.
    22 				If you don't use a separator, you may leave out the |
    23 	IMPORTANT:	iterations will only be performed if max >= min
    24 
    25 	--- ARITHMETIC EXPRESSION
    26 
    27 	@(expr)
    28 
    29 	expr may contain:
    30 		variables
    31 		constants
    32 		operators (currently only + and * are supported)
    33 
    34 	--- CONDITION
    35 
    36 	@[expr operator expr:code@]
    37 
    38 	Example: @[$1!=0:hello@]
    39 
    40 	Currently only != and == are supported operators.
    41 
    42 
    43 	Yes, error handling in here is weird, some stuff uses return values, other code uses exceptions.
    44 */
    45 
    46 #include <iostream>
    47 #include <fstream>
    48 #include <string>
    49 #include <sstream>
    50 #include <algorithm>
    51 #include <stack>
    52 #include <map>
    53 #include "stdio.h"
    54 
    55 #ifdef __linux__
    56 # define stricmp strcasecmp
    57 #endif
    58 
    59 // Ensure that the template version is being used!
    60 #ifdef min
    61 #undef min
    62 #endif
    63 
    64 using namespace std;
    65 
    66 extern int action_hopter(int numargs, const char *filenamein, const char *filenameout);
    67 
    68 typedef map<int,int> varmap;
    69 
    70 struct MyError
    71 {
    72 	const char *m_desc;
    73 
    74 	MyError(const char *desc) : m_desc(desc)
    75 	{
    76 	}
    77 
    78 	void Print()
    79 	{
    80 		cout << m_desc << endl;
    81 	}
    82 };
    83 
    84 struct SyntaxError : MyError
    85 {
    86 	SyntaxError() : MyError("Syntax error in expression")
    87 	{
    88 	}
    89 };
    90 struct OtherError : MyError
    91 {
    92 	OtherError() : MyError("WTF")
    93 	{
    94 	}
    95 };
    96 
    97 void trim_string(std::string &str)
    98 {
    99 	size_t first = str.find_first_not_of(" \t\v\n\r");
   100 	if (first == std::string::npos)
   101 	{
   102 		str.clear();
   103 		return;
   104 	}
   105 
   106 	size_t last = str.length();
   107 	for (std::string::reverse_iterator riter = str.rbegin(); riter != str.rend(); ++riter)
   108 	{
   109 		char ch = *riter;
   110 		if (ch != ' '	&&
   111 			ch != '\t'	&&
   112 			ch != '\v'	&&
   113 			ch != '\n'	&&
   114 			ch != '\r')
   115 			break;
   116 		--last;
   117 	}
   118 	str = str.substr(first, last - first);
   119 }
   120 
   121 // unused
   122 bool ExtractToken(std::string &strin, std::string &strout)
   123 {
   124 	trim_string(strin);
   125 	if (strin.begin() == strin.end())
   126 	{
   127 		strout.clear();
   128 		return false;
   129 	}
   130 	size_t first = strin.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ_0123456789");
   131 	if (first == 0)
   132 	{
   133 		if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '/')
   134 		{
   135 			// One-line comment, find its end
   136 			first = strin.find('\n') + 1;
   137 		}
   138 		else if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '*')
   139 		{
   140 			// Multi-line comment, find its end
   141 			first = strin.find("*/") + 2;
   142 		}
   143 		strin = strin.substr(1);
   144 		strout.clear();
   145 		return true;
   146 	}
   147 	strout = strin.substr(0, first);
   148 	strin = strin.substr(first);
   149 	return true;
   150 }
   151 
   152 // Returns the number of occurencies replaced
   153 int DoReplace(string &str, const string &what, const string &with)
   154 {
   155 	int cnt=0;
   156 	size_t where = str.find(what);
   157 
   158 	while (where != string::npos)
   159 	{
   160 		str.replace(where, what.size(), with);
   161 		++cnt;
   162 		where = str.find(what, where);
   163 	}
   164 	return cnt;
   165 }
   166 
   167 int DoReplace(string &str, const char *what, const char *with)
   168 {
   169 	int cnt=0;
   170 	size_t where = str.find(what);
   171 	size_t whatsize = strlen(what);
   172 	while (where != string::npos)
   173 	{
   174 		str.replace(where, whatsize, with);
   175 		++cnt;
   176 		where = str.find(what, where);
   177 	}
   178 	return cnt;
   179 }
   180 
   181 
   182 class ExprParser
   183 {
   184 	// grammar:
   185 	/*
   186 	expr -> expr + term			{ do_add }
   187 	| expr - term			{ do_sub }
   188 	| term
   189 
   190 	term -> term * factor		{ do_mul }
   191 	| term / factor		{ do_div }
   192 	| term % factor		{ do_mod }
   193 
   194 	factor -> (expr)
   195 	| numeric constant	{ push }
   196 
   197 
   198 	equivalent to:
   199 
   200 	expr -> term moreterms
   201 	moreterms -> + term { do_add } moreterms
   202 	moreterms -> - term { do_sub } moreterms
   203 	moreterms -> epsilon
   204 
   205 	term -> factor morefactors
   206 	morefactors -> * factor { do_mul } morefactors
   207 	morefactors -> / factor { do_div } morefactors
   208 	morefactors -> % factor { do_mod } morefactors
   209 	morefactors -> epsilon
   210 
   211 	factor -> (expr)
   212 	factor -> numeric constant { push }
   213 
   214 	*/
   215 
   216 	string::const_iterator m_begin;
   217 	string::const_iterator m_end;
   218 	string::const_iterator m_iter;
   219 
   220 	int m_lookahead;
   221 	int m_tokenval;
   222 
   223 	stack<int> m_stack;
   224 	static const int DONE = 256;
   225 	static const int NUM = 257;
   226 
   227 	int lexan()
   228 	{
   229 		while (1)
   230 		{
   231 			if (m_iter == m_end)
   232 				return DONE;
   233 
   234 			int t = *m_iter++;
   235 
   236 			if (t == ' ' || t == '\t')
   237 				;			// Remove whitespace
   238 			else if (isdigit(t))
   239 			{
   240 				--m_iter;
   241 
   242 				m_tokenval = 0;
   243 				while (m_iter != m_end && isdigit(*m_iter))
   244 				{
   245 					m_tokenval *= 10;
   246 					m_tokenval += *m_iter - '0';
   247 					++m_iter;
   248 				}
   249 				return NUM;
   250 			}
   251 			else
   252 				return t;
   253 		}
   254 	}
   255 
   256 	void match(int t)
   257 	{
   258 		if (m_lookahead == t)
   259 			m_lookahead = lexan();
   260 		else
   261 			throw SyntaxError();
   262 	}
   263 	void factor()
   264 	{
   265 		switch (m_lookahead)
   266 		{
   267 		case '(':
   268 			match('('); expr(); match(')');
   269 			break;
   270 		case NUM:
   271 			m_stack.push(m_tokenval); match(NUM);
   272 			break;
   273 		default:
   274 			throw SyntaxError();
   275 		}
   276 	}
   277 	void term()
   278 	{
   279 		factor();
   280 		while (1)
   281 		{
   282 			switch (m_lookahead)
   283 			{
   284 			case '*':
   285 				match('*'); factor(); do_mul();
   286 				continue;
   287 			case '/':
   288 				match('/'); factor(); do_div();
   289 				continue;
   290 			case '%':
   291 				match('%'); factor(); do_mod();
   292 				continue;
   293 			default:
   294 				return;
   295 			}
   296 
   297 		}
   298 	}
   299 
   300 	void expr()
   301 	{
   302 		term();
   303 		while (1)
   304 		{
   305 			switch (m_lookahead)
   306 			{
   307 			case '+':
   308 				match('+'); term(); do_add();
   309 				continue;
   310 			case '-':
   311 				match('-'); term(); do_sub();
   312 				continue;
   313 			default:
   314 				return;
   315 			}
   316 		}
   317 	}
   318 
   319 	void do_add()
   320 	{
   321 		int a2 = m_stack.top(); m_stack.pop();
   322 		int a1 = m_stack.top(); m_stack.pop();
   323 		m_stack.push(a1 + a2);
   324 	}
   325 
   326 	void do_sub()
   327 	{
   328 		int a2 = m_stack.top(); m_stack.pop();
   329 		int a1 = m_stack.top(); m_stack.pop();
   330 		m_stack.push(a1 - a2);
   331 	}
   332 
   333 	void do_mul()
   334 	{
   335 		int a2 = m_stack.top(); m_stack.pop();
   336 		int a1 = m_stack.top(); m_stack.pop();
   337 		m_stack.push(a1 * a2);
   338 	}
   339 
   340 	void do_div()
   341 	{
   342 		int a2 = m_stack.top(); m_stack.pop();
   343 		int a1 = m_stack.top(); m_stack.pop();
   344 		m_stack.push(a1 / a2);
   345 	}
   346 
   347 	void do_mod()
   348 	{
   349 		int a2 = m_stack.top(); m_stack.pop();
   350 		int a1 = m_stack.top(); m_stack.pop();
   351 		m_stack.push(a1 % a2);
   352 	}
   353 
   354 public:
   355 	ExprParser(string::const_iterator begin, string::const_iterator end) :
   356 	m_begin(begin), m_end(end), m_iter(begin)
   357 	{
   358 		m_lookahead = lexan();
   359 		expr();
   360 	}
   361 
   362 	operator int()
   363 	{
   364 		if (m_stack.size() != 1)
   365 			throw OtherError();
   366 
   367 		return m_stack.top();
   368 	}
   369 };
   370 
   371 int parse_expr(string::const_iterator begin, string::const_iterator end)
   372 {
   373 	return ExprParser(begin, end);
   374 }
   375 
   376 size_t find_first_directive(const string &buf, size_t begin=0)
   377 {
   378 	for (;;)
   379 	{
   380 		if (begin >= buf.size())
   381 			return string::npos;
   382 
   383 		size_t firstdirpos = buf.find('@', begin);
   384 		if (firstdirpos == string::npos)
   385 			return firstdirpos;
   386 
   387 		if (buf.size() > firstdirpos+1)
   388 		{
   389 			if (buf[firstdirpos+1] == '[' || buf[firstdirpos+1] == '(')
   390 				return firstdirpos;
   391 		}
   392 		begin = firstdirpos+1;
   393 	}
   394 }
   395 
   396 // buf begins with a section. Find its end!
   397 size_t find_section_end(const string &buf)
   398 {
   399 	int starttype = buf[1];
   400 	int endtype = (buf[1] == '(') ? ')' : ']';
   401 
   402 	int nestlevel = 0;
   403 
   404 	if (starttype == '(')
   405 	{
   406 		for (string::const_iterator iter = buf.begin(); iter != buf.end(); ++iter)
   407 		{
   408 			if (*iter == starttype)
   409 				++nestlevel;
   410 			if (*iter == endtype)
   411 			{
   412 				if (--nestlevel == 0)
   413 					return iter - buf.begin() + 1;
   414 			}
   415 		}
   416 		return string::npos;
   417 	}
   418 	else if (starttype == '[')
   419 	{
   420 		int lastchar = 0;
   421 		for (string::const_iterator iter = buf.begin(); iter != buf.end(); ++iter)
   422 		{
   423 			if (lastchar == '@' && *iter == starttype)
   424 				++nestlevel;
   425 			if (lastchar == '@' && *iter == endtype)
   426 			{
   427 				if (--nestlevel == 0)
   428 					return iter - buf.begin() + 1;
   429 			}
   430 			lastchar = *iter;
   431 		}
   432 		return string::npos;
   433 	}
   434 	
   435 	return string::npos;
   436 }
   437 
   438 // replaces variables and additional arguments
   439 void replace_vars(string &buf, int argc, int *argv, const varmap &vars)
   440 {
   441 	char varname[] = "$ ";
   442 	char value[32];
   443 	
   444 	for (int i = 0; i < argc; ++i)
   445 	{
   446 		varname[1] = 'a' + i;
   447 		sprintf(value, "%d", argv[i]);
   448 		DoReplace(buf, varname, value);
   449 	}
   450 
   451 	for (varmap::const_iterator iter = vars.begin(); iter != vars.end(); ++iter)
   452 	{
   453 		varname[1] = '0' + iter->first;
   454 		sprintf(value, "%d", iter->second);
   455 		DoReplace(buf, varname, value);
   456 	}
   457 }
   458 
   459 // do_input
   460 // params:
   461 //  argc:			number of additional arguments
   462 //	argv:			additional arguments
   463 //  outfile:		output file
   464 //  buf:			string to be processed.  IMPORTANT: buf is modified!
   465 //  curvars:		variables buffer.
   466 // retval:
   467 //  0 on success, non-zero on error
   468 
   469 int do_input(int argc, int *argv, ofstream &outfile, string &buf, varmap &curvars)
   470 {
   471 	for (;;)
   472 	{
   473 		// Find the next directive.
   474 		size_t firstdirpos = find_first_directive(buf);
   475 
   476 		// Output everything that came before, and remove it from buf
   477 		outfile << buf.substr(0, firstdirpos);
   478 		if (firstdirpos == string::npos)
   479 			return 0;
   480 		buf = buf.substr(firstdirpos);
   481 
   482 		// Now find the matching end.
   483 		size_t sectionend = find_section_end(buf);
   484 		if (sectionend == string::npos)
   485 		{
   486 			cout << "ERROR: Section not closed!" << endl;
   487 			return 1;
   488 		}
   489 
   490 		// Place the section in its own buffer and remove it from the input string.
   491 		string sect(buf.begin(), buf.begin() + sectionend);
   492 		buf = buf.substr(sectionend);
   493 
   494 		// CASE1: Arithmetic expression
   495 		if (sect[1] == '(')
   496 		{
   497 			replace_vars(sect, argc, argv, curvars);
   498 			outfile << parse_expr(sect.begin()+1, sect.end());
   499 		}
   500 		else if (sect[1] == '[')
   501 		{
   502 			int is_iter = 0;	// 0 -> no; 1 -> maybe (only used in check); 2 -> yes
   503 			char lastchar = 0;
   504 			// This could be an iteration OR a conditional thing.
   505 			// Pretty braindead check: iterations begin with a variable, then a comma.
   506 			for (string::iterator iter = sect.begin() + 2; iter != sect.end(); ++iter)
   507 			{
   508 				if (*iter == ' ' || *iter == '\t')
   509 					;
   510 				else if (is_iter == 0 && lastchar == '$' && isdigit(*iter))
   511 					is_iter = 1;
   512 				else if (is_iter == 1 && *iter == ',')
   513 				{
   514 					is_iter = 2;
   515 					break;
   516 				}
   517 				else if (*iter == '$')
   518 					;
   519 				else
   520 					break;
   521 				lastchar = *iter;
   522 			}
   523 			if (is_iter == 2)
   524 			{
   525 				// CASE2: iteration
   526 				// Looks like: @[var,min,max:code|sep@]
   527 				// Replace known variables / additional arguments
   528 				replace_vars(sect, argc, argv, curvars);
   529 
   530 				// get the parts!
   531 				string varname;
   532 				int varnum;
   533 				int expr_min;
   534 				int expr_max;
   535 
   536 				// varname
   537 				size_t comma = sect.find(',');
   538 				if (comma == string::npos)
   539 				{
   540 					cout << "Invalid iteration syntax" << endl;
   541 					return 1;
   542 				}
   543 				varname.assign(sect.begin() + 2, sect.begin() + comma);
   544 				trim_string(varname);
   545 				if (varname.size() != 2 || varname[0] != '$' || !isdigit(varname[1]))
   546 				{
   547 					cout << "Invalid variable name" << endl;
   548 					return 1;
   549 				}
   550 				varnum = varname[1] - '0';
   551 
   552 				// min
   553 				++comma;
   554 				size_t nextcomma = sect.find(',', comma);
   555 				if (nextcomma == string::npos)
   556 				{
   557 					cout << "Invalid iteration syntax" << endl;
   558 					return 1;
   559 				}
   560 				expr_min = parse_expr(sect.begin() + comma, sect.begin() + nextcomma);
   561 
   562 				// max
   563 				comma = nextcomma + 1;
   564 				nextcomma = sect.find(':', comma);
   565 				if (nextcomma == string::npos)
   566 				{
   567 					cout << "Invalid iteration syntax" << endl;
   568 					return 1;
   569 				}
   570 
   571 				expr_max = parse_expr(sect.begin() + comma, sect.begin() + nextcomma);
   572 				
   573 				// separator
   574 				size_t sepbegin = sect.find('|');
   575 				size_t sepend = string::npos;
   576 				if (sepbegin != string::npos && sepbegin < nextcomma)
   577 				{
   578 					// There's a separator!
   579 					++sepbegin;
   580 					sepend = nextcomma;
   581 				}
   582 				else
   583 					sepbegin = string::npos;
   584 
   585 
   586 				++nextcomma; // nextcomma is now where code begins!
   587 
   588 				size_t codeend = sect.size() - 2;
   589 
   590 				// Check whether the var is already taken
   591 				if (curvars.find(varnum) != curvars.end())
   592 				{
   593 					cout << "Variable $" << varnum << "already taken!" << endl;
   594 					return 1;
   595 				}
   596 
   597 				// Do iterations!!
   598 				for (int i = expr_min; i <= expr_max; ++i)
   599 				{
   600 					curvars[varnum] = i;
   601 
   602 					string code(sect.begin() + nextcomma, sect.begin() + codeend);
   603 					replace_vars(code, argc, argv, curvars);
   604 
   605 					// Feed it through the input routine (RECURSE!!!!!! YEAH!)
   606 					do_input(argc, argv, outfile, code, curvars);
   607 					
   608 					// Add separator if required
   609 					if (sepbegin != string::npos && i != expr_max)
   610 					{
   611 						string tmp(sect.begin() + sepbegin, sect.begin() + sepend);
   612 						do_input(argc, argv, outfile, tmp, curvars);
   613 					}
   614 				}
   615 				// Remove the var!
   616 				curvars.erase(varnum);
   617 			}
   618 			else
   619 			{
   620 				// CASE3: conditional thing.
   621 				// Looks like: @[expr1 operator expr2:code@]
   622 				
   623 				// Find the operator position
   624 
   625 				enum OP_TYPE
   626 				{
   627 					OP_EQ,
   628 					OP_NEQ
   629 				};
   630 
   631 				OP_TYPE op;
   632 				size_t oppos = sect.find("==");
   633 				if (oppos != string::npos)
   634 					op = OP_EQ;
   635 				else
   636 				{
   637 					oppos = sect.find("!=");
   638 					if (oppos != string::npos)
   639 						op = OP_NEQ;
   640 					else
   641 					{
   642 						cout << "Conditional expression without operator!?" << endl;
   643 						return 1;
   644 					}
   645 				}
   646 				size_t colon = sect.find(':');
   647 
   648 				// Now we've got everything. Parse first expr:
   649 				int expr1 = parse_expr(sect.begin() + 2, sect.begin() + oppos);
   650 				int expr2 = parse_expr(sect.begin() + oppos + 2, sect.begin() + colon);
   651 				if ((op == OP_EQ  && expr1 == expr2) ||
   652 					(op == OP_NEQ && expr1 != expr2))
   653 				{
   654 					// Condition is true, process it!
   655 					// The text may still contain arithmetic exprs or other cond. exprs
   656 					// so send it through do_input
   657 					string tmp(sect.substr(colon+1, sect.size() - colon - 3));
   658 					do_input(argc, argv, outfile, tmp, curvars);
   659 				}
   660 			}
   661 		}
   662 		else
   663 		{
   664 			cout << "WTF" << endl;
   665 			return 1;
   666 		}
   667 	}
   668 	
   669 	return 0;
   670 }
   671 
   672 
   673 
   674 // action_iter
   675 // params:
   676 //  filenamein:		input file name
   677 //  filenameout:	output file name
   678 //  argc:			number of additional arguments
   679 //	argv:			additional arguments
   680 // retval: 0 on success, non-zero on error
   681 
   682 // Convert additional arguments 
   683 // Read whole input file to memory and open output file
   684 // Pass to do_input()
   685 int action_iter(const char *filenamein, const char *filenameout, int argc, const char *argv[])
   686 {
   687 	// Convert additional arguments
   688 	const int MAX_ARGC = 10;
   689 	int converted_argv[MAX_ARGC];
   690 
   691 	int i;
   692 	for (i = 0; i < argc && i < MAX_ARGC; ++i)
   693 		converted_argv[i] = atoi(argv[i]);
   694 
   695 	if (argc != i)
   696 		cout << "WARNING: Not all additional arguments processed!" << endl;
   697 
   698 
   699 	// Read whole input file to memory and open output file
   700 	ifstream fin(filenamein);
   701 	ofstream fout(filenameout);
   702 
   703 	if (!fin)
   704 	{
   705 		cout << "Could not open file \"" << filenamein << "\"." << endl;
   706 		return 1;
   707 	}
   708 	if (!fout)
   709 	{
   710 		cout << "Could not open file \"" << filenameout << "\"." << endl;
   711 		return 1;
   712 	}
   713 	string input_str(
   714 		istreambuf_iterator<char> (fin.rdbuf()),
   715 		istreambuf_iterator<char> ());
   716 
   717 
   718 	// Begin processing input
   719 	varmap vars;
   720 	try
   721 	{
   722 		return do_input(argc, converted_argv, fout, input_str, vars);
   723 	}
   724 	catch (MyError err)
   725 	{
   726 		err.Print();
   727 		return 1;
   728 	}
   729 }
   730 
   731 // MAIN
   732 //  Prints usage if required
   733 //  Calls action_hopter OR action_iter
   734 int main(int argc, const char **argv)
   735 {
   736 	if (argc < 4)
   737 	{
   738 		cout << "Usage:" << endl << "   shworker [iter/hopter] ..." << endl;
   739 		cout << "   shworker iter filename.in filename.out [param1, param2, ...]" << endl;
   740 		cout << "   shworker hopter filename.in filename.out [num-of-args]" << endl;
   741 		return 1;
   742 	}
   743 
   744 	const char *action = argv[1];
   745 
   746 	if (strcasecmp(action, "hopter") == 0)
   747 	{
   748 		const char *filenamein = argv[2];
   749 		const char *filenameout = argv[3];
   750 		int argsnum = atoi(argv[4]);
   751 
   752 		return action_hopter(argsnum, filenamein, filenameout); 
   753 	}
   754 	else if (strcasecmp(action, "iter") == 0)
   755 	{
   756 		const char *filenamein = argv[2];
   757 		const char *filenameout = argv[3];
   758 		int additional_argc = argc - 4;
   759 		const char ** additional_argv = argv + 4;
   760 		return action_iter(filenamein, filenameout, additional_argc, additional_argv);
   761 	}
   762 	else
   763 	{
   764 		cout << "Unrecognized action: " << argv[1] << endl;
   765 		return 1;
   766 	}
   767 }