many changes and refactoring

This commit is contained in:
Eduardo Bart
2011-07-13 18:12:36 -03:00
parent 6c05ee0e82
commit 8ef1b28546
120 changed files with 1545 additions and 1273 deletions

View File

@@ -1,28 +1,3 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <prerequisites.h>
#include <util/color.h>
Color Color::white(0xFF, 0xFF, 0xFF, 0xFF);

View File

@@ -1,31 +1,8 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef COLOR_H
#define COLOR_H
#include <prerequisites.h>
#include "types.h"
#include <ostream>
typedef uint32 RGBA;
@@ -76,18 +53,4 @@ inline std::ostream& operator<<(std::ostream& out, const Color& color)
return out;
}
inline bool operator>>(const FML::Node& node, Color& color)
{
int r, g, b, a;
if(node.readAt(0, &r) &&
node.readAt(1, &g) &&
node.readAt(2, &b)) {
a = 255;
node.readAt(3, &a);
color.setRGBA(r,g,b,a);
return true;
}
return false;
}
#endif // COLOR_H

View File

@@ -0,0 +1,30 @@
#include "convert.h"
bool safe_convert(const std::string& input, bool& b) {
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
bool ret = false;
for(int i=0;i<4;++i) {
if(input == validNames[0][i]) {
b = true;
ret = true;
break;
} else if(input == validNames[1][i]) {
b = false;
ret = true;
break;
}
}
return ret;
}
bool safe_convert(const std::string& input, std::string& output)
{
output = input;
return true;
}
bool safe_convert(const bool& b, std::string& out)
{
out = (b ? "true" : "false");
return true;
}

View File

@@ -0,0 +1,16 @@
#ifndef CONVERT_H
#define CONVERT_H
#include <string>
#include <sstream>
bool safe_convert(const std::string& input, bool& b);
bool safe_convert(const std::string& input, std::string& output);
bool safe_convert(const bool& b, std::string& out);
template <typename V, typename T>
bool safe_convert(const V& in, T& out) {std::stringstream ss; ss << in; ss >> out; return !!ss; }
template<typename R, typename T>
R convert(const T& t) { R r; safe_convert(t, r); return r; }
#endif // CONVERT_H

View File

@@ -1,324 +0,0 @@
#include "fml.h"
namespace FML
{
bool fml_convert(const std::string& input, bool& b) {
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
bool ret = false;
for(int i=0;i<4;++i) {
if(input == validNames[0][i]) {
b = true;
ret = true;
break;
} else if(input == validNames[1][i]) {
b = false;
ret = true;
break;
}
}
return ret;
}
bool fml_convert(const std::string& input, std::string& output)
{
output = input;
return true;
}
Node* Node::at(const std::string& childTag) const {
int i=0;
while(i<size() && at(i)->tag()!=childTag)
++i;
return at(i);
}
Node* Node::createNode(std::string tag)
{
Node* node = new Node;
node->setTag(tag);
addNode(node);
return node;
}
void Node::addNode(Node* node) {
if(node->hasTag() && node->hasValue()) {
if(Node* other = at(node->tag())) {
if(removeNode(other))
delete node;
}
}
m_children.push_back(node);
node->setParent(this);
}
bool Node::removeNode(Node* node) {
for(NodeList::iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == node) {
m_children.erase(it);
return true;
}
}
return false;
}
std::string Node::generateErrorMessage(const std::string& message) const {
std::stringstream ss;
ss << "FML error";
if(!what().empty())
ss << " in '" << what() << "'";
if(m_line > 0)
ss << " at line " << m_line;
if(m_line > 0 && hasTag())
ss << ", in node '" << tag() << "'";
ss << ": " << message;
return ss.str();
}
void Parser::load(std::istream& in)
{
if(m_rootNode)
delete m_rootNode;
m_rootNode = new Node(what());
m_rootNode->setTag("root");
m_currentParent = m_rootNode;
m_currentDepth = m_currentLine = 0;
m_multilineMode = DONT_MULTILINE;
m_multilineData.clear();
while(in.good() && !in.eof()) {
m_currentLine++;
std::string line;
std::getline(in, line);
parseLine(line);
}
if(isMultilining())
stopMultilining();
}
void Parser::parseLine(std::string& line)
{
// process multiline data first
if(isMultilining() && parseMultiline(line))
return;
// calculate depth
std::size_t numSpaces = line.find_first_not_of(' ');
// trim left whitespaces
boost::trim_left(line);
// skip comment or empty lines
if(line[0] == '#' || line.empty())
return;
// calculate depth
int depth = 0;
if(numSpaces != std::string::npos)
depth = numSpaces / 2;
// check for syntax error
if(numSpaces != std::string::npos && numSpaces % 2 != 0)
throwError("file must be idented every 2 whitespaces", m_currentLine);
// a depth above
if(depth == m_currentDepth+1) {
// change current parent to the previous added node
m_currentParent = m_previousNode;
// a depth below, change parent to previus parent and add new node inside previuos parent
} else if(depth < m_currentDepth) {
// change current parent to the the new depth parent
for(int i=0;i<m_currentDepth-depth;++i)
m_currentParent = m_currentParent->parent();
// else if nots the same depth it's a syntax error
} else if(depth != m_currentDepth) {
throwError("invalid indentation level", m_currentLine);
}
// update current depth
m_currentDepth = depth;
// add node
Node* node = m_currentParent->createNode();
parseNode(node, line);
m_previousNode = node;
}
void Parser::parseNode(Node* node, std::string& line)
{
// determine node tag and value
std::string tag;
std::string value;
// its a node that has tag and possible a value
std::size_t dotsPos = line.find_first_of(':');
if(dotsPos != std::string::npos) {
tag = line.substr(0, dotsPos);
value = line.substr(dotsPos+1);
} else if(line[0] == '-') // its a node that has a value but no tag
value = line.substr(1);
else // its a node that has only a tag
tag = line;
// set node tag
boost::trim(tag);
node->setTag(tag);
// set node line
node->setLine(m_currentLine);
// process node value
parseNodeValue(node, value);
}
void Parser::parseNodeValue(Node* node, std::string& value)
{
boost::trim(value);
if(value.empty())
return;
// multiline text scalar
if(value[0] == '|') {
// determine multiline mode
m_multilineMode = MULTILINE_DONT_FOLD;
if(value.length() == 2) {
switch(value[1]) {
case '-':
m_multilineMode = MULTILINE_FOLD_BLOCK;
break;
case '+':
m_multilineMode = MULTILINE_FOLD_FLOW;
break;
default:
throwError("invalid multiline identifier", m_currentLine);
break;
}
}
m_currentDepth++;
}
// sequence
else if(value[0] == '[') {
if(!boost::ends_with(value, "]"))
throwError("missing ']' in sequence", m_currentLine);
// erase '[' and ']'
value.erase(value.length()-1, 1);
value.erase(0, 1);
// split commas
boost::tokenizer<boost::escaped_list_separator<char> > tokens(value);
for(boost::tokenizer<boost::escaped_list_separator<char> >::iterator it = tokens.begin(); it != tokens.end(); ++it) {
std::string tmp = (*it);
boost::trim(tmp);
if(!tmp.empty()) {
Node* child = node->createNode();
child->setLine(m_currentLine);
child->setValue(tmp);
}
}
}
// text scalar
else {
node->setValue(parseTextValue(value));
}
}
std::string Parser::parseTextValue(std::string value)
{
if(value[0] == '"' && value[value.length()-1] == '"') {
value = value.substr(1, value.length()-2);
// escape characters
boost::replace_all(value, "\\\\", "\\");
boost::replace_all(value, "\\\"", "\"");
boost::replace_all(value, "\\n", "\n");
}
return value;
}
void Parser::stopMultilining()
{
// remove all new lines at the end
if(m_multilineMode == MULTILINE_DONT_FOLD || m_multilineMode == MULTILINE_FOLD_BLOCK) {
while(*m_multilineData.rbegin() == '\n')
m_multilineData.erase(m_multilineData.length()-1, 1);
}
if(m_multilineMode == MULTILINE_FOLD_BLOCK)
m_multilineData.append("\n");
m_previousNode->setValue(m_multilineData);
m_multilineMode = DONT_MULTILINE;
m_currentDepth--;
m_multilineData.clear();
}
bool Parser::parseMultiline(std::string line)
{
// calculate depth
std::size_t numSpaces = line.find_first_not_of(' ');
// depth above or equal current depth, add the text to the multiline
if(numSpaces != std::string::npos && (int)numSpaces >= m_currentDepth*2) {
m_multilineData += parseTextValue(line.substr(m_currentDepth*2)) + "\n";
return true;
// depth below the current depth, check if it is a node
} else if(numSpaces == std::string::npos || (int)numSpaces < m_currentDepth*2) {
// if it has contents, its a node, then we must end multiline
if(line.find_first_not_of(' ') != std::string::npos) {
stopMultilining();
// the line still have a node on it
}
// no contents, just an extra line
else {
m_multilineData += "\n";
return true;
}
}
return false;
}
void Parser::throwError(const std::string& message, int line)
{
std::stringstream ss;
ss << "FML syntax error";
if(!m_what.empty())
ss << " in '" << m_what << "'";
if(line > 0)
ss << " at line " << line;
ss << ": " << message;
throw Exception(ss.str());
}
std::string Emitter::emitNodeValue(Node* node)
{
std::string value = node->value();
if(value[0] == '"' || *value.rbegin() == '"' || value.find("\n") != std::string::npos) {
boost::replace_all(value, "\\", "\\\\");
boost::replace_all(value, "\"", "\\\"");
boost::replace_all(value, "\n", "\\n");
value.append("\"");
value.insert(0, "\"");
}
return value;
}
std::string Emitter::emitNode(Node* node, int currentDepth)
{
std::stringstream ss;
for(int i=1;i<currentDepth;++i)
ss << " ";
if(currentDepth > 0) {
if(node->hasTag())
ss << node->tag();
if(node->hasValue())
ss << (node->hasTag() ? ": " : "- ") << emitNodeValue(node) << std::endl;
else
ss << std::endl;
}
for(int i=0;i<node->size();++i)
ss << emitNode(node->at(i), currentDepth+1);
return ss.str();
}
}

View File

@@ -1,280 +0,0 @@
#ifndef __FML_H
#define __FML_H
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <iterator>
#include <map>
#include <stdexcept>
#include <typeinfo>
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <boost/utility.hpp>
namespace FML {
bool fml_convert(const std::string& input, bool& b);
bool fml_convert(const std::string& input, std::string& output);
template <typename V, typename T>
bool fml_convert(const V& in, T& out) { std::stringstream ss; ss << in; ss >> out; return !!ss; }
inline std::string fml_tostr(bool b) { return (b ? "true" : "false"); }
template < typename T>
std::string fml_tostr(T v) { std::stringstream ss; ss << v; return ss.str(); }
class Exception : public std::runtime_error {
public:
Exception(const std::string& what) : std::runtime_error(what) {}
};
class Parser;
class Node : boost::noncopyable
{
public:
typedef std::vector<Node*> NodeList;
typedef NodeList::iterator iterator;
typedef NodeList::const_iterator const_iterator;
Node(std::string what = "") : m_parent(0), m_line(0), m_what(what) { }
~Node() { for(int i=0;i<size();++i) delete at(i); }
bool hasTag() const { return !m_tag.empty(); }
bool hasChildren() const { return size() > 0; }
bool hasValue() const { return !m_value.empty(); }
bool hasNode(const std::string ctag) const { return at(ctag) != 0; }
void setTag(std::string tag) { m_tag = tag; }
void setLine(int line) { m_line = line; }
void setValue(const std::string& value) { m_value = value; }
void setParent(Node* parent) { m_parent = parent; }
std::string tag() const { return m_tag; }
int line() const { return m_line; }
int size() const { return m_children.size(); }
Node* parent() { return m_parent; }
std::string what() const { return (m_parent ? m_parent->what() : m_what); }
iterator begin() { return m_children.begin(); }
iterator end() { return m_children.end(); }
const_iterator begin() const { return m_children.begin(); }
const_iterator end() const { return m_children.end(); }
Node* front() const { return at(0); }
Node* back() const { return at(size()-1); }
void throwError(const std::string& message) const { throw Exception(generateErrorMessage(message)); }
Node* at(const std::string& ctag) const;
Node* at(int pos) const { return ((pos < size() && pos >= 0) ? m_children[pos] : 0); }
std::string value(const std::string& def = "") const { return (m_value.empty() ? def : m_value); }
std::string valueAt(const std::string ctag, const std::string& def = "") const { Node* c = at(ctag); return (c ? c->value() : def); }
std::string valueAt(int pos, const std::string& def = "") const { Node* n = at(pos); return (n ? n->value() : def); }
Node* createNode(std::string tag = "");
void addNode(Node* node);
bool removeNode(Node* node);
std::string generateErrorMessage(const std::string& message) const;
// read into memory
template <typename T>
void read(T* v) const {
if(!(*this >> *v))
throw Exception(generateErrorMessage("failed to cast node value to type " + std::string(typeid(T).name())));
}
template <typename T>
bool readAt(const std::string& ctag, T* v) const {
if(Node* node = at(ctag)) {
node->read<T>(v);
return true;
}
return false;
}
template <typename T>
bool readAt(int pos, T* v) const {
if(Node* node = at(pos)) {
node->read<T>(v);
return true;
}
return false;
}
// read returning the result
template <typename T>
T read() const { T v; read<T>(&v); return v;}
template <typename T>
T readAt(const std::string& ctag) const {
T v;
if(!readAt(ctag, &v))
throw Exception(generateErrorMessage("child node " + ctag + " not found"));
return v;
}
template <typename T>
T readAt(int pos) const {
T v;
if(!readAt(pos, &v))
throw Exception(generateErrorMessage("child node at pos " + fml_tostr(pos) + " not found"));
return v;
}
// read with default supplied
template <typename T>
T readAt(const std::string& ctag, const T& def) const { Node* c = at(ctag); return (c ? c->read<T>() : def); }
template <typename T>
T readAt(int pos, const T& def) const { Node* c = at(pos); return (c ? c->read<T>() : def); }
// writing
template <typename T>
void write(T v) {
if(!(*this << v))
throw Exception(generateErrorMessage("failed to cast to string node value of type " + std::string(typeid(T).name())));
}
template <typename T>
void writeIn(int pos, T v) {
Node* c;
while(!at(pos))
c = createNode();
c->write<T>(v);
}
template <typename T>
void writeIn(const std::string& ctag, T v) {
Node* c = createNode(ctag);
c->write<T>(v);
}
private:
Node* m_parent;
int m_line;
std::string m_what;
NodeList m_children;
std::string m_tag;
std::string m_value;
};
// read operators
template <typename T>
bool operator >> (const Node& node, T& v) { return fml_convert(node.value(), v); }
template <typename T>
bool operator >> (const Node& node, std::vector<T>& v) {
v.resize(node.size());
for(unsigned i=0;i<node.size();++i)
v[i] = node.readAt<T>(i);
return true;
}
template <typename T>
bool operator >> (const Node& node, std::list<T>& v) {
for(unsigned i=0;i<node.size();++i)
v.push_back(node.readAt<T>(i));
return true;
}
template <typename K, typename T>
bool operator >> (const Node& node, std::map<K, T>& m) {
for(int i=0;i<node.size();++i)
m[node.at(i)->read<K>()] = node.at(i)->read<T>();
return true;
}
// write operators
template <typename T>
bool operator << (Node& node, const T& v) { node.setValue(fml_tostr(v)); return true; }
template <typename T>
bool operator << (Node& node, const std::vector<T>& v) {
for(unsigned i=0;i<v.size();++i)
node.createNode()->write(v[i]);
return true;
}
template <typename T>
bool operator << (Node& node, const std::list<T>& v) {
for(unsigned i=0;i<v.size();++i)
node.createNode()->write(v[i]);
return true;
}
template <typename K, typename T>
bool operator << (Node& node, const std::map<K, T>& m) {
typename std::map<K, T>::const_iterator it;
for(it = m.begin(); it != m.end(); ++it)
node.createNode(fml_tostr(it->first))->write(it->second);
return true;
}
class Parser
{
enum MultilineMode {
DONT_MULTILINE = 0,
MULTILINE_DONT_FOLD,
MULTILINE_FOLD_BLOCK,
MULTILINE_FOLD_FLOW
};
public:
Parser(std::string what = std::string()) : m_rootNode(0), m_what(what) { }
Parser(std::istream& in, std::string what = std::string()) : m_rootNode(0), m_what(what) { load(in); }
~Parser() { if(m_rootNode) delete m_rootNode; }
Node* getDocument() const { return m_rootNode; }
std::string what() { return m_what; }
void load(std::istream& in);
protected:
void parseLine(std::string& line);
void parseNode(Node* node, std::string& line);
void parseNodeValue(Node* node, std::string& value);
std::string parseTextValue(std::string value);
void stopMultilining();
bool parseMultiline(std::string line);
bool isMultilining() { return m_multilineMode != DONT_MULTILINE; }
void throwError(const std::string& message, int line);
private:
int m_currentDepth;
int m_currentLine;
Node* m_currentParent;
Node* m_previousNode;
Node* m_rootNode;
MultilineMode m_multilineMode;
std::string m_multilineData;
std::string m_what;
};
class Emitter
{
public:
Emitter() : m_rootNode(0) { }
~Emitter() { if(m_rootNode) delete m_rootNode; }
Node* createDocument() { m_rootNode = new Node; return m_rootNode; }
std::string emitDocument() { if(m_rootNode) return emitNode(m_rootNode, 0); return std::string(); }
static std::string emitNodeValue(Node* node);
static std::string emitNode(Node* node, int currentDepth = 0);
private:
Node* m_rootNode;
};
} // namespace FML {
#endif // __FML_H

View File

@@ -0,0 +1,7 @@
#ifndef FOREACH_H
#define FOREACH_H
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
#endif // FOREACH_H

View File

@@ -1,33 +1,9 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <util/logger.h>
#include <util/util.h>
#include "logger.h"
#include <iostream>
#include <cstdlib>
void Logger::log(int level, const std::string& text, const char *trace)
void log(LogLevel level, const std::string& message, const char *trace)
{
std::string strace;
if(trace) {
@@ -47,7 +23,7 @@ void Logger::log(int level, const std::string& text, const char *trace)
if(!strace.empty())
std::cout << "[" << strace << "] ";
std::cout << text;
std::cout << message;
#ifdef linux
if(colored)
@@ -56,7 +32,6 @@ void Logger::log(int level, const std::string& text, const char *trace)
std::cout << std::endl;
if(level == LFATAL)
if(level == LogFatal)
exit(-1);
}

View File

@@ -1,74 +1,38 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef LOGGER_H
#define LOGGER_H
#include <sstream>
#include <boost/format.hpp>
#include "makestring.h"
class Logger {
public:
enum ELogLevel {
LFATAL = 0,
LERROR,
LWARNING,
LINFO,
LDEBUG
};
static void log(int level, const std::string& text = "", const char *trace = NULL);
enum LogLevel {
LogDebug = 0,
LogInfo,
LogWarning,
LogError,
LogFatal
};
void log(LogLevel level, const std::string& message, const char *trace = NULL);
#define logFatal(a) Logger::log(Logger::LFATAL, a)
#define logError(a) Logger::log(Logger::LERROR, a)
#define logWarning(a) Logger::log(Logger::LWARNING, a)
#define logDebug(a) Logger::log(Logger::LDEBUG, a)
#define logInfo(a) Logger::log(Logger::LINFO, a)
// specialized logging
template<class... T>
void debug(const T&... args) { log(LogInfo, make_string(args...)); }
template<class... T>
void info(const T&... args) { log(LogDebug, make_string(args...)); }
template<class... T>
void warning(const T&... args) { log(LogWarning, make_string(args...)); }
template<class... T>
void error(const T&... args) { log(LogError, make_string(args...)); }
template<class... T>
void fatal(const T&... args) { log(LogFatal, make_string(args...)); }
#define flogFatal(a,b) Logger::log(Logger::LFATAL, (boost::format(a) % b).str())
#define flogError(a,b) Logger::log(Logger::LERROR, (boost::format(a) % b).str())
#define flogWarning(a,b) Logger::log(Logger::LWARNING, (boost::format(a) % b).str())
#define flogDebug(a,b) Logger::log(Logger::LDEBUG, (boost::format(a) % b).str())
#define flogInfo(a,b) Logger::log(Logger::LINFO, (boost::format(a) % b).str())
#define logTrace() Logger::log(Logger::LDEBUG, "", __PRETTY_FUNCTION__)
#define logTraceFatal(a) Logger::log(Logger::LFATAL, a, __PRETTY_FUNCTION__)
#define logTraceError(a) Logger::log(Logger::LERROR, a, __PRETTY_FUNCTION__)
#define logTraceWarning(a) Logger::log(Logger::LWARNING, a, __PRETTY_FUNCTION__)
#define logTraceDebug(a) Logger::log(Logger::LDEBUG, a, __PRETTY_FUNCTION__)
#define logTraceInfo(a) Logger::log(Logger::LINFO, a, __PRETTY_FUNCTION__)
#define trace() debug(LogDebug, "", __PRETTY_FUNCTION__)
// dump utility
struct Dump {
public:
Dump() { }
~Dump() { logDebug(m_buf.str().c_str()); }
template<class T> Dump &operator<<(const T &x) { m_buf << x << " "; return *this; }
private:
std::ostringstream m_buf;
~Dump() { debug(s.str().c_str()); }
template<class T>
Dump& operator<<(const T& v) { s << v << " "; return *this; }
std::ostringstream s;
};
#define dump Dump()
#endif // LOGGER_H

View File

@@ -0,0 +1,21 @@
#ifndef MAKESTRING_H
#define MAKESTRING_H
#include <sstream>
#include <string>
inline void fill_ostream(std::ostringstream&) { }
template<class T, class... Args>
void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) {
stream << first;
fill_ostream(stream, rest...);
}
template<class... T>
std::string make_string(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
return buf.str();
}
#endif // MAKESTRING_H

View File

@@ -1,31 +1,9 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef POINT_H
#define POINT_H
#include <prerequisites.h>
#include "types.h"
#include <ostream>
#include <cmath>
template <class T>
class TSize;
@@ -85,16 +63,4 @@ inline std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
return out;
}
template <class T>
inline bool operator>>(const FML::Node& node, TPoint<T>& point)
{
T x, y;
if(node.readAt(0, &x) && node.readAt(1, &y)) {
point.x = x;
point.y = y;
return true;
}
return false;
}
#endif

View File

@@ -1,31 +1,8 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef RECT_H
#define RECT_H
#include <prerequisites.h>
#include "types.h"
#include <ostream>
template <class T>
class TPoint;
@@ -309,18 +286,4 @@ inline std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
return out;
}
template <class T>
inline bool operator>>(const FML::Node& node, TRect<T>& rect)
{
T x, y, width, height;
if(node.readAt(0, &x) &&
node.readAt(1, &y) &&
node.readAt(2, &width) &&
node.readAt(3, &height)) {
rect.setRect(x, y, width, height);
return true;
}
return false;
}
#endif // RECT_H

View File

@@ -1,156 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <prerequisites.h>
#include <util/rsa.h>
Rsa::Rsa()
{
m_keySet = false;
mpz_init2(m_p, 1024);
mpz_init2(m_q, 1024);
mpz_init2(m_d, 1024);
mpz_init2(m_u, 1024);
mpz_init2(m_dp, 1024);
mpz_init2(m_dq, 1024);
mpz_init2(m_mod, 1024);
}
Rsa::~Rsa()
{
mpz_clear(m_p);
mpz_clear(m_q);
mpz_clear(m_d);
mpz_clear(m_u);
mpz_clear(m_dp);
mpz_clear(m_dq);
mpz_clear(m_mod);
}
bool Rsa::setKey(const std::string& file)
{
//loads p,q and d from a file
FILE* f = fopen(file.c_str(), "r");
if(!f){
return false;
}
char p[512];
char q[512];
char d[512];
delete fgets(p, 512, f);
delete fgets(q, 512, f);
delete fgets(d, 512, f);
setKey(p, q, d);
return true;
}
void Rsa::setKey(const char* p, const char* q, const char* d)
{
mpz_set_str(m_p, p, 10);
mpz_set_str(m_q, q, 10);
mpz_set_str(m_d, d, 10);
mpz_t pm1,qm1;
mpz_init2(pm1,520);
mpz_init2(qm1,520);
mpz_sub_ui(pm1, m_p, 1);
mpz_sub_ui(qm1, m_q, 1);
mpz_invert(m_u, m_p, m_q);
mpz_mod(m_dp, m_d, pm1);
mpz_mod(m_dq, m_d, qm1);
mpz_mul(m_mod, m_p, m_q);
mpz_clear(pm1);
mpz_clear(qm1);
}
bool Rsa::encrypt(char* msg, int32_t size, const char* key)
{
mpz_t plain, c;
mpz_init2(plain, 1024);
mpz_init2(c, 1024);
mpz_t e;
mpz_init(e);
mpz_set_ui(e,65537);
mpz_t mod;
mpz_init2(mod, 1024);
mpz_set_str(mod, key, 10);
mpz_import(plain, 128, 1, 1, 0, 0, msg);
mpz_powm(c, plain, e, mod);
size_t count = (mpz_sizeinbase(c, 2) + 7)/8;
memset(msg, 0, 128 - count);
mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c);
mpz_clear(c);
mpz_clear(plain);
mpz_clear(e);
mpz_clear(mod);
return true;
}
bool Rsa::decrypt(char* msg, int32_t size)
{
mpz_t c,v1,v2,u2,tmp;
mpz_init2(c, 1024);
mpz_init2(v1, 1024);
mpz_init2(v2, 1024);
mpz_init2(u2, 1024);
mpz_init2(tmp, 1024);
mpz_import(c, 128, 1, 1, 0, 0, msg);
mpz_mod(tmp, c, m_p);
mpz_powm(v1, tmp, m_dp, m_p);
mpz_mod(tmp, c, m_q);
mpz_powm(v2, tmp, m_dq, m_q);
mpz_sub(u2, v2, v1);
mpz_mul(tmp, u2, m_u);
mpz_mod(u2, tmp, m_q);
if(mpz_cmp_si(u2, 0) < 0){
mpz_add(tmp, u2, m_q);
mpz_set(u2, tmp);
}
mpz_mul(tmp, u2, m_p);
mpz_set_ui(c, 0);
mpz_add(c, v1, tmp);
size_t count = (mpz_sizeinbase(c, 2) + 7)/8;
memset(msg, 0, 128 - count);
mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c);
mpz_clear(c);
mpz_clear(v1);
mpz_clear(v2);
mpz_clear(u2);
mpz_clear(tmp);
return true;
}

View File

@@ -1,48 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef RSA_H
#define RSA_H
#include <prerequisites.h>
#include <gmp.h>
class Rsa{
public:
Rsa();
~Rsa();
void setKey(const char* p, const char* q, const char* d);
bool setKey(const std::string& file);
bool decrypt(char* msg, int32_t size);
static bool encrypt(char* msg, int32_t size, const char* key);
protected:
bool m_keySet;
mpz_t m_p, m_q, m_u, m_d, m_dp, m_dq, m_mod;
};
typedef boost::shared_ptr<Rsa> RsaPtr;
#endif //RSA_H

View File

@@ -1,32 +1,7 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef SIZE_H
#define SIZE_H
#include <prerequisites.h>
#include <util/point.h>
#include "point.h"
enum ESizeScaleMode {
IGNORE_ASPECT_RATIO,
@@ -111,15 +86,13 @@ private:
typedef TSize<int> Size;
typedef TSize<float> SizeF;
template <class T>
inline bool operator>>(const FML::Node& node, TSize<T>& size)
inline std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
{
T w, h;
if(node.readAt(0, &w) && node.readAt(1, &h)) {
size.setSize(w, h);
return true;
}
return false;
out << "Size(" << size.width() << ","
<< size.height() << ")";
return out;
}
#endif

View File

@@ -0,0 +1,27 @@
#include "translator.h"
#include <boost/algorithm/string.hpp>
AlignmentFlag parseAlignment(std::string aligment)
{
boost::to_lower(aligment);
boost::erase_all(aligment, " ");
if(aligment == "topleft")
return AlignTopLeft;
else if(aligment == "topright")
return AlignTopRight;
else if(aligment == "bottomleft")
return AlignBottomLeft;
else if(aligment == "bottomright")
return AlignBottomRight;
else if(aligment == "left")
return AlignLeftCenter;
else if(aligment == "right")
return AlignRightCenter;
else if(aligment == "top")
return AlignTopCenter;
else if(aligment == "bottom")
return AlignBottomCenter;
else
return AlignCenter;
}

View File

@@ -0,0 +1,10 @@
#ifndef TRANSLATOR_H
#define TRANSLATOR_H
#include <const.h>
#include <string>
AlignmentFlag parseAlignment(std::string aligment);
#endif // TRANSLATOR_H

View File

@@ -0,0 +1,19 @@
#ifndef TYPES_H
#define TYPES_H
#include <stdint.h>
// easy types
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef uint64_t uint64;
typedef uint32_t uint32;
typedef uint16_t uint16;
typedef uint8_t uint8;
typedef int32_t int32;
typedef int16_t int16;
typedef int8_t int8;
#endif // TYPES_H

View File

@@ -1,49 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <util/util.h>
#include <boost/algorithm/string.hpp>
AlignmentFlag parseAlignment(std::string aligment)
{
boost::to_lower(aligment);
boost::erase_all(aligment, " ");
if(aligment == "topleft")
return AlignTopLeft;
else if(aligment == "topright")
return AlignTopRight;
else if(aligment == "bottomleft")
return AlignBottomLeft;
else if(aligment == "bottomright")
return AlignBottomRight;
else if(aligment == "left")
return AlignLeftCenter;
else if(aligment == "right")
return AlignRightCenter;
else if(aligment == "top")
return AlignTopCenter;
else if(aligment == "bottom")
return AlignBottomCenter;
else
return AlignCenter;
}