Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2020 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Utils.h"
23
24#include "CVar.h"
25#include "RoRnet.h"
26#include "RoRVersion.h"
27#include "SHA1.h"
28#include "Application.h"
29
30#include <Ogre.h>
31#include <string>
32
33#include <codecvt> // https://en.cppreference.com/w/cpp/locale/codecvt_utf8_utf16
34#include <locale>
35
36#ifndef _WIN32
37# include <iconv.h>
38#endif
39
40#ifdef _WIN32
41# include <windows.h> // Sleep()
42#endif
43
44using namespace Ogre;
45
46String RoR::sha1sum(const char *key, int len)
47{
48 RoR::CSHA1 sha1;
49 sha1.UpdateHash((uint8_t *)key, len);
50 sha1.Final();
51 return sha1.ReportHash();
52}
53
54String RoR::HashData(const char *key, int len)
55{
56 std::stringstream result;
57 result << std::hex << FastHash(key, len);
58 return result.str();
59}
60
61std::string RoR::tryConvertUTF(const char* buffer)
62{
63 std::string str_in(buffer);
64 return std::string(SanitizeUtf8String(str_in));
65}
66
67std::string RoR::formatBytes(double bytes)
68{
69 char tmp[128] = "";
70 const char* si_prefix[] = {"B", "KB", "MB", "GB", "TB", "EB", "ZB", "YB"};
71 int base = 1024;
72 int c = bytes > 0 ? std::min((int)(log(bytes) / log((float)base)), (int)sizeof(si_prefix) - 1) : 0;
73 snprintf(tmp, 128, "%1.2f %s", bytes / pow((float)base, c), si_prefix[c]);
74 return std::string(tmp);
75}
76
77std::time_t RoR::getTimeStamp()
78{
79 return time(NULL); //this will overflow in 2038
80}
81
82String RoR::getVersionString(bool multiline)
83{
84 char tmp[1024] = "";
85 if (multiline)
86 {
87 sprintf(tmp, "Rigs of Rods\n"
88 " version: %s\n"
89 " protocol version: %s\n"
90 " build time: %s, %s\n"
92 }
93 else
94 {
95 sprintf(tmp, "Rigs of Rods version %s, protocol version: %s, build time: %s, %s", ROR_VERSION_STRING, RORNET_VERSION, ROR_BUILD_DATE, ROR_BUILD_TIME);
96 }
97
98 return String(tmp);
99}
100
101Real RoR::Round(Ogre::Real value, unsigned short ndigits /* = 0 */)
102{
103 Real f = 1.0f;
104
105 while (ndigits--)
106 f = f * 10.0f;
107
108 value *= f;
109
110 if (value >= 0.0f)
111 value = std::floor(value + 0.5f);
112 else
113 value = std::ceil(value - 0.5f);
114
115 value /= f;
116
117 return value;
118}
119
120std::string RoR::SanitizeUtf8String(std::string const& str_in)
121{
122 // Cloned from UTFCPP tutorial: http://utfcpp.sourceforge.net/#fixinvalid
123 std::string str_out;
124 utf8::replace_invalid(str_in.begin(), str_in.end(), std::back_inserter(str_out));
125 return str_out;
126}
127
128std::string RoR::SanitizeUtf8CString(const char* start, const char* end /* = nullptr */)
129{
130 if (end == nullptr)
131 {
132 end = (start + strlen(start));
133 }
134
135 // Cloned from UTFCPP tutorial: http://utfcpp.sourceforge.net/#fixinvalid
136 std::string str_out;
137 utf8::replace_invalid(start, end, std::back_inserter(str_out));
138 return str_out;
139}
140
141std::string RoR::Sha1Hash(std::string const & input)
142{
143 RoR::CSHA1 sha1;
144 sha1.UpdateHash((uint8_t *)input.c_str(), (int)input.length());
145 sha1.Final();
146 return sha1.ReportHash();
147}
148
149std::wstring RoR::Utf8ToWideChar(std::string input_utf8)
150{
151 // https://stackoverflow.com/a/14809553
152 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> convert;
153 std::wstring temp_utf16 = convert.from_bytes(input_utf8);
154 return std::wstring(reinterpret_cast<const wchar_t*>(temp_utf16.c_str()));
155}
156
157std::string RoR::JoinStrVec(Ogre::StringVector tokens, const std::string& delim)
158{
159 Str<500> res;
160 for (String& tok : tokens)
161 {
162 if (res.GetLength() > 0)
163 res << delim;
164 res << tok;
165 }
166 return res.ToCStr();
167}
168
169bool RoR::IsDistanceWithin(Ogre::Vector3 const& a, Ogre::Vector3 const& b, float max)
170{
171 return a.squaredDistance(b) <= max * max;
172}
173
174void formatVertexDeclInfo(RoR::Str<4000>& text, Ogre::VertexDeclaration* vertexDeclaration, int j)
175{
176 const VertexElement* ve = vertexDeclaration->getElement(j);
177 text << "\n" << "\telement #" << (j) << "/" << (vertexDeclaration->getElementCount());
178 text << " binding:" << (ve->getSource());
179 text << ", offset:" << (ve->getOffset());
180 text << ", type:" << (ve->getType());
181 text << ", semantic:" << (ve->getSemantic());
182 text << ", size:" << (ve->getSize());
183}
184
185std::string RoR::PrintMeshInfo(std::string const& title, MeshPtr mesh)
186{
187 Str<4000> text;
188 text << title;
189
190 if (mesh->sharedVertexData)
191 {
192 text << "\n" <<("Mesh has Shared Vertices:");
193 VertexData* vt=mesh->sharedVertexData;
194 for (int j=0; j<(int)vt->vertexDeclaration->getElementCount(); j++)
195 {
196 formatVertexDeclInfo(text, vt->vertexDeclaration, j);
197 }
198 }
199 text << "\n" <<("Mesh has "+TOSTRING(mesh->getNumSubMeshes())+" submesh(es)");
200 for (int i=0; i<mesh->getNumSubMeshes(); i++)
201 {
202 SubMesh* submesh = mesh->getSubMesh(i);
203 text << "\n" <<("SubMesh "+TOSTRING(i)+": uses shared?:"+TOSTRING(submesh->useSharedVertices));
204 if (!submesh->useSharedVertices)
205 {
206 VertexData* vt=submesh->vertexData;
207 for (int j=0; j<(int)vt->vertexDeclaration->getElementCount(); j++)
208 {
209 formatVertexDeclInfo(text, vt->vertexDeclaration, j);
210 }
211 }
212 }
213
214 return text.ToCStr();
215}
216
217void RoR::CvarAddFileToList(CVar* cvar, const std::string& filename)
218{
219 StringVector files = StringUtil::split(cvar->getStr(), ",");
220 if (std::find(files.begin(), files.end(), filename) == files.end()) // Is file not in list yet?
221 {
222 files.push_back(filename);
223 }
224 cvar->setStr(JoinStrVec(files, ","));
225}
226
227void RoR::CvarRemoveFileFromList(CVar* cvar, const std::string& filename)
228{
229 StringVector files = StringUtil::split(cvar->getStr(), ",");
230 auto found = (std::find(files.begin(), files.end(), filename));
231 if (found != files.end()) // Was file in list?
232 {
233 files.erase(found);
234 }
235
236 cvar->setStr(JoinStrVec(files, ","));
237}
238
239void RoR::SplitBundleQualifiedFilename(const std::string& bundleQualifiedFilename, std::string& out_bundleName, std::string& out_filename)
240{
241 size_t pos = bundleQualifiedFilename.find(':');
242 if (pos != std::string::npos)
243 {
244 out_bundleName = bundleQualifiedFilename.substr(0, pos);
245 out_filename = bundleQualifiedFilename.substr(pos + 1);
246 }
247 else
248 {
249 out_bundleName = "";
250 out_filename = bundleQualifiedFilename;
251 }
252}
Central state/object manager and communications hub.
#define TOSTRING(x)
Definition Application.h:57
const char *const ROR_BUILD_DATE
const char *const ROR_VERSION_STRING
const char *const ROR_BUILD_TIME
void formatVertexDeclInfo(RoR::Str< 4000 > &text, Ogre::VertexDeclaration *vertexDeclaration, int j)
Definition Utils.cpp:174
void UpdateHash(uint8_t *data, uint32_t len)
Definition SHA1.cpp:143
std::string ReportHash()
Definition SHA1.cpp:202
void Final()
Definition SHA1.cpp:168
Quake-style console variable, defined in RoR.cfg or crated via Console UI and scripts.
Definition CVar.h:53
std::string const & getStr() const
Definition CVar.h:95
void setStr(std::string const &str)
Definition CVar.h:83
Wrapper for classic c-string (local buffer) Refresher: strlen() excludes '\0' terminator; strncat() A...
Definition Str.h:36
const char * ToCStr() const
Definition Str.h:46
size_t GetLength() const
Definition Str.h:51
Ogre::String HashData(const char *key, int len)
Definition Utils.cpp:54
bool IsDistanceWithin(Ogre::Vector3 const &a, Ogre::Vector3 const &b, float max)
Definition Utils.cpp:169
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition Utils.cpp:217
std::string JoinStrVec(Ogre::StringVector tokens, const std::string &delim)
Definition Utils.cpp:157
std::wstring Utf8ToWideChar(std::string input_utf8)
Definition Utils.cpp:149
std::string SanitizeUtf8CString(const char *start, const char *end=nullptr)
Definition Utils.cpp:128
std::string Sha1Hash(std::string const &data)
Definition Utils.cpp:141
std::string SanitizeUtf8String(std::string const &str_in)
Definition Utils.cpp:120
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition Utils.cpp:227
void SplitBundleQualifiedFilename(const std::string &bundleQualifiedFilename, std::string &out_bundleName, std::string &out_filename)
Definition Utils.cpp:239
std::string formatBytes(double bytes)
Definition Utils.cpp:67
Ogre::String getVersionString(bool multiline=true)
Definition Utils.cpp:82
std::string PrintMeshInfo(std::string const &title, Ogre::MeshPtr mesh)
std::string tryConvertUTF(const char *buffer)
Definition Utils.cpp:61
Ogre::Real Round(Ogre::Real value, unsigned short ndigits=0)
Definition Utils.cpp:101
Ogre::String sha1sum(const char *key, int len)
Definition Utils.cpp:46
std::time_t getTimeStamp()
Definition Utils.cpp:77
#define RORNET_VERSION
Definition RoRnet.h:35