RigsofRods
Soft-body Physics Simulation
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 #ifndef _WIN32
34 # include <iconv.h>
35 #endif
36 
37 #ifdef _WIN32
38 # include <windows.h> // Sleep()
39 #endif
40 
41 using namespace Ogre;
42 
43 String RoR::sha1sum(const char *key, int len)
44 {
45  RoR::CSHA1 sha1;
46  sha1.UpdateHash((uint8_t *)key, len);
47  sha1.Final();
48  return sha1.ReportHash();
49 }
50 
51 String RoR::HashData(const char *key, int len)
52 {
53  std::stringstream result;
54  result << std::hex << FastHash(key, len);
55  return result.str();
56 }
57 
58 UTFString RoR::tryConvertUTF(const char* buffer)
59 {
60  std::string str_in(buffer);
61  return UTFString(SanitizeUtf8String(str_in));
62 }
63 
64 UTFString RoR::formatBytes(double bytes)
65 {
66  wchar_t tmp[128] = L"";
67  const wchar_t* si_prefix[] = {L"B", L"KB", L"MB", L"GB", L"TB", L"EB", L"ZB", L"YB"};
68  int base = 1024;
69  int c = bytes > 0 ? std::min((int)(log(bytes) / log((float)base)), (int)sizeof(si_prefix) - 1) : 0;
70  swprintf(tmp, 128, L"%1.2f %ls", bytes / pow((float)base, c), si_prefix[c]);
71  return UTFString(tmp);
72 }
73 
74 std::time_t RoR::getTimeStamp()
75 {
76  return time(NULL); //this will overflow in 2038
77 }
78 
79 String RoR::getVersionString(bool multiline)
80 {
81  char tmp[1024] = "";
82  if (multiline)
83  {
84  sprintf(tmp, "Rigs of Rods\n"
85  " version: %s\n"
86  " protocol version: %s\n"
87  " build time: %s, %s\n"
89  }
90  else
91  {
92  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);
93  }
94 
95  return String(tmp);
96 }
97 
98 Real RoR::Round(Ogre::Real value, unsigned short ndigits /* = 0 */)
99 {
100  Real f = 1.0f;
101 
102  while (ndigits--)
103  f = f * 10.0f;
104 
105  value *= f;
106 
107  if (value >= 0.0f)
108  value = std::floor(value + 0.5f);
109  else
110  value = std::ceil(value - 0.5f);
111 
112  value /= f;
113 
114  return value;
115 }
116 
117 std::string RoR::SanitizeUtf8String(std::string const& str_in)
118 {
119  // Cloned from UTFCPP tutorial: http://utfcpp.sourceforge.net/#fixinvalid
120  std::string str_out;
121  utf8::replace_invalid(str_in.begin(), str_in.end(), std::back_inserter(str_out));
122  return str_out;
123 }
124 
125 std::string RoR::SanitizeUtf8CString(const char* start, const char* end /* = nullptr */)
126 {
127  if (end == nullptr)
128  {
129  end = (start + strlen(start));
130  }
131 
132  // Cloned from UTFCPP tutorial: http://utfcpp.sourceforge.net/#fixinvalid
133  std::string str_out;
134  utf8::replace_invalid(start, end, std::back_inserter(str_out));
135  return str_out;
136 }
137 
138 std::string RoR::Sha1Hash(std::string const & input)
139 {
140  RoR::CSHA1 sha1;
141  sha1.UpdateHash((uint8_t *)input.c_str(), (int)input.length());
142  sha1.Final();
143  return sha1.ReportHash();
144 }
145 
146 std::string RoR::JoinStrVec(Ogre::StringVector tokens, const std::string& delim)
147 {
148  Str<500> res;
149  for (String& tok : tokens)
150  {
151  if (res.GetLength() > 0)
152  res << delim;
153  res << tok;
154  }
155  return res.ToCStr();
156 }
157 
158 bool RoR::IsDistanceWithin(Ogre::Vector3 const& a, Ogre::Vector3 const& b, float max)
159 {
160  return a.squaredDistance(b) <= max * max;
161 }
162 
163 void formatVertexDeclInfo(RoR::Str<4000>& text, Ogre::VertexDeclaration* vertexDeclaration, int j)
164 {
165  const VertexElement* ve = vertexDeclaration->getElement(j);
166  text << "\n" << "\telement #" << (j) << "/" << (vertexDeclaration->getElementCount());
167  text << " binding:" << (ve->getSource());
168  text << ", offset:" << (ve->getOffset());
169  text << ", type:" << (ve->getType());
170  text << ", semantic:" << (ve->getSemantic());
171  text << ", size:" << (ve->getSize());
172 }
173 
174 std::string RoR::PrintMeshInfo(std::string const& title, MeshPtr mesh)
175 {
176  Str<4000> text;
177  text << title;
178 
179  if (mesh->sharedVertexData)
180  {
181  text << "\n" <<("Mesh has Shared Vertices:");
182  VertexData* vt=mesh->sharedVertexData;
183  for (int j=0; j<(int)vt->vertexDeclaration->getElementCount(); j++)
184  {
185  formatVertexDeclInfo(text, vt->vertexDeclaration, j);
186  }
187  }
188  text << "\n" <<("Mesh has "+TOSTRING(mesh->getNumSubMeshes())+" submesh(es)");
189  for (int i=0; i<mesh->getNumSubMeshes(); i++)
190  {
191  SubMesh* submesh = mesh->getSubMesh(i);
192  text << "\n" <<("SubMesh "+TOSTRING(i)+": uses shared?:"+TOSTRING(submesh->useSharedVertices));
193  if (!submesh->useSharedVertices)
194  {
195  VertexData* vt=submesh->vertexData;
196  for (int j=0; j<(int)vt->vertexDeclaration->getElementCount(); j++)
197  {
198  formatVertexDeclInfo(text, vt->vertexDeclaration, j);
199  }
200  }
201  }
202 
203  return text.ToCStr();
204 }
205 
206 void RoR::CvarAddFileToList(CVar* cvar, const std::string& filename)
207 {
208  StringVector files = StringUtil::split(cvar->getStr(), ",");
209  if (std::find(files.begin(), files.end(), filename) == files.end()) // Is file not in list yet?
210  {
211  files.push_back(filename);
212  }
213  cvar->setStr(JoinStrVec(files, ","));
214 }
215 
216 void RoR::CvarRemoveFileFromList(CVar* cvar, const std::string& filename)
217 {
218  StringVector files = StringUtil::split(cvar->getStr(), ",");
219  auto found = (std::find(files.begin(), files.end(), filename));
220  if (found != files.end()) // Was file in list?
221  {
222  files.erase(found);
223  }
224 
225  cvar->setStr(JoinStrVec(files, ","));
226 }
227 
228 void RoR::SplitBundleQualifiedFilename(const std::string& bundleQualifiedFilename, std::string& out_bundleName, std::string& out_filename)
229 {
230  size_t pos = bundleQualifiedFilename.find(':');
231  if (pos != std::string::npos)
232  {
233  out_bundleName = bundleQualifiedFilename.substr(0, pos);
234  out_filename = bundleQualifiedFilename.substr(pos + 1);
235  }
236  else
237  {
238  out_bundleName = "";
239  out_filename = bundleQualifiedFilename;
240  }
241 }
RoR::getVersionString
Ogre::String getVersionString(bool multiline=true)
Definition: Utils.cpp:79
Script2Game::log
void log(const string message)
This is an alias for game.log(string message).
RoR::CSHA1::Final
void Final()
Definition: SHA1.cpp:168
RORNET_VERSION
#define RORNET_VERSION
Definition: RoRnet.h:35
RoR::PrintMeshInfo
std::string PrintMeshInfo(std::string const &title, Ogre::MeshPtr mesh)
RoR::CSHA1
Definition: SHA1.h:84
RoR::SanitizeUtf8String
std::string SanitizeUtf8String(std::string const &str_in)
Definition: Utils.cpp:117
RoR::Round
Ogre::Real Round(Ogre::Real value, unsigned short ndigits=0)
Definition: Utils.cpp:98
RoR::SplitBundleQualifiedFilename
void SplitBundleQualifiedFilename(const std::string &bundleQualifiedFilename, std::string &out_bundleName, std::string &out_filename)
Definition: Utils.cpp:228
RoR::HashData
Ogre::String HashData(const char *key, int len)
Definition: Utils.cpp:51
Utils.h
RoR::Str::GetLength
size_t GetLength() const
Definition: Str.h:51
RoR::getTimeStamp
std::time_t getTimeStamp()
Definition: Utils.cpp:74
TOSTRING
#define TOSTRING(x)
Definition: Application.h:56
RoR::CVar::getStr
std::string const & getStr() const
Definition: CVar.h:95
RoR::Str< 500 >
RoR::formatBytes
Ogre::UTFString formatBytes(double bytes)
Definition: Utils.cpp:64
RoR::Str::ToCStr
const char * ToCStr() const
Definition: Str.h:46
Application.h
Central state/object manager and communications hub.
files
This is a raw Ogre binding for Imgui No project cmake no just four source files
Definition: README-OgreImGui.txt:3
RoRVersion.h
RoR::CSHA1::UpdateHash
void UpdateHash(uint8_t *data, uint32_t len)
Definition: SHA1.cpp:143
ROR_VERSION_STRING
const char *const ROR_VERSION_STRING
RoR::CvarAddFileToList
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:206
RoR::CVar
Quake-style console variable, defined in RoR.cfg or crated via Console UI and scripts.
Definition: CVar.h:52
RoR::SanitizeUtf8CString
std::string SanitizeUtf8CString(const char *start, const char *end=nullptr)
Definition: Utils.cpp:125
formatVertexDeclInfo
void formatVertexDeclInfo(RoR::Str< 4000 > &text, Ogre::VertexDeclaration *vertexDeclaration, int j)
Definition: Utils.cpp:163
RoR::JoinStrVec
std::string JoinStrVec(Ogre::StringVector tokens, const std::string &delim)
Definition: Utils.cpp:146
SHA1.h
RoR::Sha1Hash
std::string Sha1Hash(std::string const &data)
Definition: Utils.cpp:138
RoR::CvarRemoveFileFromList
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:216
RoR::CSHA1::ReportHash
std::string ReportHash()
Definition: SHA1.cpp:202
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::tryConvertUTF
Ogre::UTFString tryConvertUTF(const char *buffer)
Definition: Utils.cpp:58
ROR_BUILD_DATE
const char *const ROR_BUILD_DATE
CVar.h
ROR_BUILD_TIME
const char *const ROR_BUILD_TIME
RoR::CVar::setStr
void setStr(std::string const &str)
Definition: CVar.h:83
RoR::sha1sum
Ogre::String sha1sum(const char *key, int len)
Definition: Utils.cpp:43
RoR::IsDistanceWithin
bool IsDistanceWithin(Ogre::Vector3 const &a, Ogre::Vector3 const &b, float max)
Definition: Utils.cpp:158