RigsofRods
Soft-body Physics Simulation
SkinFileFormat.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 "SkinFileFormat.h"
23 
24 #include "Application.h"
25 #include "Console.h"
26 #include "Utils.h"
27 
28 #include <OgreEntity.h>
29 #include <OgreMaterialManager.h>
30 #include <OgrePass.h>
31 #include <OgreSubEntity.h>
32 #include <OgreTechnique.h>
33 
34 std::vector<std::shared_ptr<RoR::SkinDef>> RoR::SkinParser::ParseSkins(Ogre::DataStreamPtr& stream)
35 {
36  std::vector<std::shared_ptr<RoR::SkinDef>> result;
37  std::unique_ptr<RoR::SkinDef> curr_skin;
38  try
39  {
40  while(!stream->eof())
41  {
42  std::string line = SanitizeUtf8String(stream->getLine());
43 
44  // Ignore blanks & comments
45  if (!line.length() || line.substr(0, 2) == "//")
46  {
47  continue;
48  }
49 
50  if (!curr_skin)
51  {
52  // No current skin -- So first valid data should be skin name
53  Ogre::StringUtil::trim(line);
54  curr_skin = std::unique_ptr<SkinDef>(new SkinDef);
55  curr_skin->name = line;
56  stream->skipLine("{");
57  }
58  else
59  {
60  // Already in skin
61  if (line == "}")
62  {
63  result.push_back(std::shared_ptr<SkinDef>(curr_skin.release())); // Finished
64  }
65  else
66  {
67  RoR::SkinParser::ParseSkinAttribute(line, curr_skin.get());
68  }
69  }
70  }
71 
72  if (curr_skin)
73  {
76  fmt::format("Skin '{}' in file '{}' not properly closed with '}}'",
77  curr_skin->name, stream->getName()));
78  result.push_back(std::shared_ptr<SkinDef>(curr_skin.release())); // Submit anyway
79  }
80  }
81  catch (Ogre::Exception& e)
82  {
85  fmt::format("Error parsing skin file '{}', message: {}",
86  stream->getName(), e.getFullDescription()));
87  }
88  return result;
89 }
90 
91 void RoR::SkinParser::ParseSkinAttribute(const std::string& line, SkinDef* skin_def) // static
92 {
93  Ogre::StringVector params = Ogre::StringUtil::split(line, "\t=,;\n");
94  for (unsigned int i=0; i < params.size(); i++)
95  {
96  Ogre::StringUtil::trim(params[i]);
97  }
98  Ogre::String& attrib = params[0];
99  Ogre::StringUtil::toLowerCase(attrib);
100 
101  if (attrib == "replacetexture" && params.size() == 3) { skin_def->replace_textures.insert(std::make_pair(params[1], params[2])); return; }
102  if (attrib == "replacematerial" && params.size() == 3) { skin_def->replace_materials.insert(std::make_pair(params[1], params[2])); return; }
103  if (attrib == "preview" && params.size() >= 2) { skin_def->thumbnail = params[1]; return; }
104  if (attrib == "description" && params.size() >= 2) { skin_def->description = params[1]; return; }
105  if (attrib == "authorname" && params.size() >= 2) { skin_def->author_name = params[1]; return; }
106  if (attrib == "authorid" && params.size() == 2) { skin_def->author_id = PARSEINT(params[1]); return; }
107  if (attrib == "guid" && params.size() >= 2) { skin_def->guid = params[1]; Ogre::StringUtil::trim(skin_def->guid); Ogre::StringUtil::toLowerCase(skin_def->guid); return; }
108  if (attrib == "name" && params.size() >= 2) { skin_def->name = params[1]; Ogre::StringUtil::trim(skin_def->name); return; }
109 }
format
Truck file format(technical spec)
RoR::SanitizeUtf8String
std::string SanitizeUtf8String(std::string const &str_in)
Definition: Utils.cpp:117
Console.h
RoR::Console::putMessage
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition: Console.cpp:97
Utils.h
RoR::SkinDef::author_name
std::string author_name
Definition: SkinFileFormat.h:45
PARSEINT
#define PARSEINT(x)
Definition: Application.h:57
RoR::SkinDef::description
std::string description
Definition: SkinFileFormat.h:44
RoR::SkinDef::author_id
int author_id
Definition: SkinFileFormat.h:46
Application.h
Central state/object manager and communications hub.
RoR::App::GetConsole
Console * GetConsole()
Definition: Application.cpp:270
RoR::SkinDef
Definition: SkinFileFormat.h:35
RoR::SkinDef::guid
std::string guid
Definition: SkinFileFormat.h:42
RoR::SkinDef::thumbnail
std::string thumbnail
Definition: SkinFileFormat.h:43
RoR::SkinParser::ParseSkinAttribute
static void ParseSkinAttribute(const std::string &line, SkinDef *skin_def)
Definition: SkinFileFormat.cpp:91
SkinFileFormat.h
RoR::SkinDef::name
std::string name
Definition: SkinFileFormat.h:41
RoR::SkinParser::ParseSkins
static std::vector< std::shared_ptr< RoR::SkinDef > > ParseSkins(Ogre::DataStreamPtr &stream)
Definition: SkinFileFormat.cpp:34
RoR::SkinDef::replace_materials
std::map< std::string, std::string > replace_materials
Definition: SkinFileFormat.h:40
RoR::Console::CONSOLE_MSGTYPE_ACTOR
@ CONSOLE_MSGTYPE_ACTOR
Parsing/spawn/simulation messages for actors.
Definition: Console.h:63
RoR::Console::CONSOLE_SYSTEM_WARNING
@ CONSOLE_SYSTEM_WARNING
Definition: Console.h:53
RoR::SkinDef::replace_textures
std::map< std::string, std::string > replace_textures
Definition: SkinFileFormat.h:39