2020-11-13 15:18:09 -05:00
|
|
|
#ifndef UTIL_HPP
|
2020-11-13 18:17:47 -05:00
|
|
|
#define UTIL_HPP
|
2020-11-13 15:18:09 -05:00
|
|
|
|
|
|
|
|
#include <string>
|
2020-11-27 13:21:07 -05:00
|
|
|
#include <set>
|
2021-10-04 17:45:18 -04:00
|
|
|
#include <map>
|
2020-11-13 15:18:09 -05:00
|
|
|
#include <algorithm>
|
2020-12-05 18:57:53 -05:00
|
|
|
#include <sstream>
|
2021-02-02 16:29:07 -05:00
|
|
|
#include <ostream>
|
2021-10-05 12:54:37 -04:00
|
|
|
#include <memory>
|
2021-01-12 14:14:38 -05:00
|
|
|
#include <tuple>
|
2021-01-16 01:24:33 -05:00
|
|
|
#include <utility>
|
2021-07-26 13:12:44 -04:00
|
|
|
#include "luastack.hpp"
|
2021-08-09 12:54:32 -04:00
|
|
|
#include "spookyv2.hpp"
|
2020-11-13 15:18:09 -05:00
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
2021-07-18 17:48:39 -04:00
|
|
|
enum WorldType {
|
|
|
|
|
WORLD_TYPE_STANDALONE,
|
|
|
|
|
WORLD_TYPE_C_SYNC,
|
|
|
|
|
WORLD_TYPE_S_SYNC,
|
|
|
|
|
WORLD_TYPE_MASTER,
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-09 16:27:39 -05:00
|
|
|
enum MessageType {
|
|
|
|
|
MSG_NULL,
|
|
|
|
|
MSG_DIFF,
|
|
|
|
|
MSG_ACK,
|
|
|
|
|
MSG_INVOKE,
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-30 13:22:23 -04:00
|
|
|
using StringVec = std::vector<std::string>;
|
2021-10-04 17:45:18 -04:00
|
|
|
using StringPair = std::pair<std::string, std::string>;
|
2021-10-05 12:54:37 -04:00
|
|
|
using LuaSourceVec = std::vector<StringPair>;
|
|
|
|
|
using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
|
2021-07-18 17:48:39 -04:00
|
|
|
using HashValue = std::pair<uint64_t, uint64_t>;
|
2021-07-25 19:22:39 -04:00
|
|
|
using IdVector = std::vector<int64_t>;
|
2020-11-13 15:18:09 -05:00
|
|
|
|
2021-09-07 17:37:23 -04:00
|
|
|
// Return true if the string is a valid lua identifier.
|
|
|
|
|
bool is_identifier(const std::string &str);
|
|
|
|
|
|
2021-09-08 01:32:08 -04:00
|
|
|
// Output a string to a stream using Lua string escaping and quoting.
|
|
|
|
|
void quote_string(const std::string &str, std::ostream *os);
|
|
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
// ID vector quick create.
|
|
|
|
|
IdVector id_vector_create(int64_t id1=-1, int64_t id2=-1, int64_t id3=-1, int64_t id4=-1);
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
// ID vector debug string.
|
|
|
|
|
std::string id_vector_debug_string(const IdVector &idv);
|
|
|
|
|
|
2021-07-30 13:22:23 -04:00
|
|
|
// Unions and sorts two ID vectors.
|
|
|
|
|
IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2);
|
|
|
|
|
|
2021-09-23 12:40:21 -04:00
|
|
|
// Get a 64-bit hashvalue for a string.
|
|
|
|
|
HashValue hash_string(const std::string &str);
|
|
|
|
|
|
2021-08-09 12:54:32 -04:00
|
|
|
// Get a 64-bit hashvalue for an ID vector.
|
|
|
|
|
HashValue hash_id_vector(const IdVector &idv);
|
|
|
|
|
|
2021-09-23 12:40:21 -04:00
|
|
|
// Convert a hash to a hexadecimal string.
|
|
|
|
|
std::string hash_to_hex(const HashValue &hash);
|
|
|
|
|
|
2021-07-21 00:50:06 -04:00
|
|
|
// Split a string into multiple strings
|
2021-07-30 13:22:23 -04:00
|
|
|
StringVec split(const std::string &s, char sep);
|
2021-07-21 00:50:06 -04:00
|
|
|
|
2021-11-04 13:40:42 -04:00
|
|
|
// Return N repetitions of string A
|
|
|
|
|
std::string repeat_string(const std::string &a, int n);
|
|
|
|
|
|
|
|
|
|
// Return the length of the common prefix of A and B.
|
|
|
|
|
int common_prefix_length(const std::string &a, const std::string &b);
|
|
|
|
|
|
2021-07-30 13:22:23 -04:00
|
|
|
// String to lowercase/uppercase. Ascii only, no unicode.
|
2021-02-07 15:35:31 -05:00
|
|
|
std::string tolower(std::string input);
|
|
|
|
|
std::string toupper(std::string input);
|
|
|
|
|
|
2021-02-10 16:22:24 -05:00
|
|
|
// Return true if the string can be parsed as an integer.
|
|
|
|
|
bool validinteger(const std::string &value);
|
|
|
|
|
|
2021-02-02 16:29:07 -05:00
|
|
|
// String to integer. Returns errval if the number is not parseable.
|
|
|
|
|
int64_t strtoint(const std::string &value, int64_t errval);
|
|
|
|
|
|
2021-07-21 00:50:06 -04:00
|
|
|
// String to double. Returns NAN if the number is not parseable.
|
|
|
|
|
double strtodouble(const std::string &value);
|
|
|
|
|
|
2021-10-07 14:58:20 -04:00
|
|
|
// Split a string of the form "hostname:port" into a hostname and a port.
|
|
|
|
|
// On failure, returns empty strings.
|
|
|
|
|
void split_host_port(const std::string &target, std::string &host, std::string &port);
|
|
|
|
|
|
2021-02-02 16:29:07 -05:00
|
|
|
// Trim strings: left end, right end, both ends.
|
|
|
|
|
std::string ltrim(std::string s);
|
|
|
|
|
std::string rtrim(std::string s);
|
|
|
|
|
std::string trim(std::string s);
|
|
|
|
|
|
2021-07-30 13:22:23 -04:00
|
|
|
// Calculate distance between two points
|
|
|
|
|
double distance_squared(double x1, double y1, double x2, double y2);
|
|
|
|
|
|
2021-11-11 16:23:11 -05:00
|
|
|
// Remove nullptrs from a vector of unique pointers.
|
|
|
|
|
template<class T>
|
|
|
|
|
void remove_nullptrs(std::vector<std::unique_ptr<T>> &vec) {
|
|
|
|
|
std::unique_ptr<T> nullp;
|
|
|
|
|
auto iter = std::remove(vec.begin(), vec.end(), nullp);
|
|
|
|
|
vec.erase(iter, vec.end());
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 17:45:18 -04:00
|
|
|
// Read the lua source code from the specified directory.
|
2021-10-05 12:54:37 -04:00
|
|
|
LuaSourcePtr read_lua_source(const std::string &directory);
|
2020-11-13 15:18:09 -05:00
|
|
|
|
2021-02-02 16:29:07 -05:00
|
|
|
// An XYZ coordinate, general purpose.
|
2021-01-12 16:21:29 -05:00
|
|
|
struct XYZ {
|
|
|
|
|
float x, y, z;
|
|
|
|
|
XYZ() { x=0; y=0; z=0; }
|
|
|
|
|
XYZ(float ix, float iy, float iz) { x=ix; y=iy; z=iz; }
|
2021-03-13 13:05:00 -05:00
|
|
|
bool operator ==(const XYZ &o) const { return x==o.x && y == o.y && z==o.z; }
|
|
|
|
|
bool operator !=(const XYZ &o) const { return x!=o.x || y != o.y || z!=o.z; }
|
2021-07-21 00:50:06 -04:00
|
|
|
std::string debug_string() const;
|
2021-01-12 16:21:29 -05:00
|
|
|
};
|
2021-07-18 17:48:39 -04:00
|
|
|
|
2021-07-21 16:10:29 -04:00
|
|
|
// These are formatting directives that can be sent to a std::ostream.
|
|
|
|
|
class hex64 {};
|
|
|
|
|
class hex32 {};
|
|
|
|
|
class hex16 {};
|
|
|
|
|
class hex8 {};
|
|
|
|
|
|
2020-11-13 15:18:09 -05:00
|
|
|
} // namespace util
|
2021-07-21 00:50:06 -04:00
|
|
|
|
2021-07-21 16:10:29 -04:00
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex64 &v);
|
|
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex32 &v);
|
|
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex16 &v);
|
|
|
|
|
std::ostream &operator<<(std::ostream &oss, const util::hex8 &v);
|
|
|
|
|
|
2020-11-13 15:18:09 -05:00
|
|
|
#endif // UTIL_HPP
|