RigsofRods
Soft-body Physics Simulation
OgreScriptBuilder.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 
25 
26 #include "OgreScriptBuilder.h"
27 
28 #include "Application.h"
29 #include "Actor.h"
30 #include "Console.h"
31 #include "ContentManager.h"
32 #include "Utils.h"
33 
34 #include <string>
35 
36 using namespace RoR;
37 
38 int OgreScriptBuilder::LoadScriptSection(const char* full_path_cstr)
39 {
40  // Get filename - required to retrieve file from OGRe's resource system.
41  // This function received filename in older AngelScript versions, but now receives full path
42  // (reconstructed wrong by CScriptBuilder because it doesn't know about OGRE's ZIP files).
43  // TODO: Refactor the entire script building logic
44  // - create fully RoR-custom builder instead of hacked stock CScriptBuilder + our overload. ~ only_a_ptr, 08/2017
45 
46  std::string full_path(full_path_cstr);
47  std::string filename;
48  size_t slash_pos = full_path.rfind('/'); // AngelScript always uses forward slashes in paths.
49  if (slash_pos != std::string::npos)
50  {
51  filename = full_path.substr(slash_pos+1);
52  }
53  else
54  {
55  filename = full_path;
56  }
57 
58  Ogre::DataStreamPtr ds;
59  try
60  {
61  ds = Ogre::ResourceGroupManager::getSingleton().openResource(filename, Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME);
62  //TODO: do not use `AUTODETECT_RESOURCE_GROUP_NAME`, use specific group, lookups are slow!
63  //see also https://github.com/OGRECave/ogre/blob/master/Docs/1.10-Notes.md#resourcemanager-strict-mode ~ only_a_ptr, 08/2017
64  }
65  catch (Ogre::Exception& e)
66  {
68  return -2;
69  }
70  // In some cases (i.e. when fed a full path with '/'-s on Windows), `openResource()` will silently return NULL for datastream. ~ only_a_ptr, 08/2017
71  if (ds.isNull())
72  {
73  LOG("[RoR|Scripting] Failed to load file '"+filename+"', reason unknown.");
74  return -1;
75  }
76 
77  const std::string& code = ds->getAsString();
78  hash = RoR::Sha1Hash(code);
79 
80  return ProcessScriptSection(code.c_str(), static_cast<unsigned int>(code.length()), filename.c_str(), 0);
81 }
ContentManager.h
OgreScriptBuilder.h
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::Console::CONSOLE_SYSTEM_ERROR
@ CONSOLE_SYSTEM_ERROR
Definition: Console.h:52
Actor.h
Application.h
Central state/object manager and communications hub.
RoR::App::GetConsole
Console * GetConsole()
Definition: Application.cpp:270
RoR::Sha1Hash
std::string Sha1Hash(std::string const &data)
Definition: Utils.cpp:138
OgreScriptBuilder::LoadScriptSection
int LoadScriptSection(const char *filename)
Definition: OgreScriptBuilder.cpp:38
RoR::Console::CONSOLE_MSGTYPE_INFO
@ CONSOLE_MSGTYPE_INFO
Generic message.
Definition: Console.h:60
RoR
Definition: AppContext.h:36