Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
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-2026 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
36using namespace RoR;
37
38int 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. ~ ohlidalp, 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 // Check for _DEVEL.as version first if the CVar is enabled
59 std::string devel_filename;
60 std::string basename, ext;
61 Ogre::StringUtil::splitBaseFilename(filename, basename, ext);
62 if (ext == "as" && App::diag_load_devel_scripts->getBool())
63 {
64 devel_filename = basename + "_DEVEL.as";
65 }
66
67 Ogre::DataStreamPtr ds;
68 std::string actual_filename = filename;
69
70 // Try to load _DEVEL version first if applicable
71 // Note we use 'throwOnFailure=false' to avoid spamming the log with 'Not found' errors. ~ ohlidalp, 01/2026
72 if (!devel_filename.empty())
73 {
74 try
75 {
76 ds = Ogre::ResourceGroupManager::getSingleton().openResource(
77 devel_filename,
79 /*resourceBeingLoaded: */nullptr,
80 /*throwOnFailure:*/false);
81
82 if (ds)
83 {
84 actual_filename = devel_filename;
86 fmt::format("Using development version of script: '{}'", devel_filename));
87 }
88 }
89 catch (Ogre::Exception&)
90 {
91 // _DEVEL version not found, fall back to regular version
92 ds.reset();
93 }
94 }
95
96 // If _DEVEL version wasn't found or not applicable, load the regular version
97
98 if (!ds)
99 {
100 try
101 {
102 ds = Ogre::ResourceGroupManager::getSingleton().openResource(filename, mResourceGroup);
103 }
104 catch (Ogre::Exception& e)
105 {
107 return -2;
108 }
109 }
110
111 // In some cases (i.e. when fed a full path with '/'-s on Windows), `openResource()` will silently return NULL for datastream. ~ ohlidalp, 08/2017
112 if (!ds)
113 {
114 LOG("[RoR|Scripting] Failed to load file '"+filename+"', reason unknown.");
115 return -1;
116 }
117
118 const std::string& code = ds->getAsString();
119 hash = RoR::Sha1Hash(code);
120
121 return ProcessScriptSection(code.c_str(), static_cast<unsigned int>(code.length()), actual_filename.c_str(), 0);
122}
Central state/object manager and communications hub.
void LOG(const char *msg)
Legacy alias - formerly a macro.
Ogre::String mResourceGroup
int LoadScriptSection(const char *filename)
@ CONSOLE_MSGTYPE_INFO
Generic message.
Definition Console.h:60
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition Console.cpp:103
@ CONSOLE_SYSTEM_ERROR
Definition Console.h:52
@ CONSOLE_SYSTEM_NOTICE
Definition Console.h:51
Console * GetConsole()
CVar * diag_load_devel_scripts
std::string Sha1Hash(std::string const &data)
Definition Utils.cpp:141