Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
GUI_ScriptMonitor.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2021 tritonas00
4 For more information, see http://www.rigsofrods.org/
5 Rigs of Rods is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 3, as
7 published by the Free Software Foundation.
8 Rigs of Rods is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
14*/
15
16
17#include "GUI_ScriptMonitor.h"
18
19#include "Actor.h"
20#include "ContentManager.h"
21#include "GameContext.h"
22#include "ScriptEngine.h"
23#include "Utils.h"
24
25#include <Ogre.h>
26#include "OgreImGui.h"
27
28using namespace RoR;
29using namespace GUI;
30using namespace Ogre;
31
33{
34 // Table setup
35 ImGui::Columns(3);
36 ImGui::SetColumnWidth(0, ImGui::GetFontSize() * 1.5625f);
37 ImGui::SetColumnWidth(1, ImGui::GetFontSize() * 12.5f);
38 ImGui::SetColumnWidth(2, ImGui::GetFontSize() * 12.5f);
39
40 // Header
41 ImGui::TextDisabled(_LC("ScriptMonitor", "ID"));
42 ImGui::NextColumn();
43 ImGui::TextDisabled(_LC("ScriptMonitor", "File name"));
44 ImGui::NextColumn();
45 ImGui::TextDisabled(_LC("ScriptMonitor", "Options"));
46
47 this->DrawCommentedSeparator(_LC("ScriptMonitor", "Active"));
48
49 StringVector autoload = StringUtil::split(App::app_custom_scripts->getStr(), ",");
50 for (auto& pair : App::GetScriptEngine()->getScriptUnits())
51 {
52 ScriptUnitID_t id = pair.first;
53 ImGui::PushID(id);
54
55 ScriptUnit const& unit = pair.second;
56 ImGui::NextColumn();
57 ImGui::AlignTextToFramePadding();
58 ImGui::TextDisabled("%d", id);
59 ImGui::NextColumn();
60 ImGui::AlignTextToFramePadding();
62 {
63 ImGui::Text("%s", unit.originatingGadget->fname.c_str());
64 }
65 else
66 {
67 ImGui::Text("%s", unit.scriptName.c_str());
68 }
69 ImGui::NextColumn();
70 switch (unit.scriptCategory)
71 {
73 ImGui::Text("(%s) [%u] '%s'", _LC("ScriptMonitor", "actor"), unit.associatedActor->ar_vector_index, unit.associatedActor->getTruckName().c_str());
74 break;
75
77 ImGui::Text("%s", _LC("ScriptMonitor", "(terrain)"));
78 break;
79
81 ImGui::Text("(%s) [%d]", _LC("ScriptMonitor", "bot"), unit.associatedNetUid);
82 ImGui::SameLine();
83 if (ImGui::Button(_LC("ScriptMonitor", "Kill")))
84 {
86 }
87 break;
88
91 {
92 std::string filename = unit.scriptName;
94 {
95 filename = unit.originatingGadget->fname;
96 }
97
98 if (ImGui::Button(_LC("ScriptMonitor", "Reload")))
99 {
102 req->lsr_category = unit.scriptCategory;
103 req->lsr_filename = filename;
105 }
106 ImGui::SameLine();
107 if (ImGui::Button(_LC("ScriptMonitor", "Stop")))
108 {
110 }
111
112 ImGui::SameLine();
113 bool autoload_set = std::find(autoload.begin(), autoload.end(), filename) != autoload.end();
114 if (ImGui::Checkbox(_LC("ScriptMonitor", "Autoload"), &autoload_set))
115 {
116 if (autoload_set)
118 else
120 }
121 break;
122 }
123 default:;
124 }
125
126 ImGui::PopID(); // ScriptUnitID_t id
127 }
128
129 if (App::app_recent_scripts->getStr() != "")
130 {
131 // Prepare display list for recent scripts
132 m_recent_displaylist.clear();
133 StringVector recent = StringUtil::split(App::app_recent_scripts->getStr(), ",");
134 for (String& filename : recent)
135 {
136 bool is_running = std::find_if(
137 App::GetScriptEngine()->getScriptUnits().begin(),
138 App::GetScriptEngine()->getScriptUnits().end(),
139 [filename](ScriptUnitMap::const_iterator::value_type pair) { return filename == pair.second.scriptName; })
141 if (!is_running)
142 {
143 m_recent_displaylist.push_back(filename);
144 }
145 }
146
147 // Draw recent scripts from the displaylist
148 if (m_recent_displaylist.size() > 0)
149 {
150 this->DrawCommentedSeparator(_LC("ScriptMonitor", "Recent"));
151
152 for (String& filename : m_recent_displaylist)
153 {
154 ImGui::PushID(filename.c_str());
155
156 ImGui::NextColumn();
157 ImGui::NextColumn(); // skip "ID"
158 ImGui::AlignTextToFramePadding();
159 ImGui::Text("%s", filename.c_str());
160 ImGui::NextColumn();
161 float cursorx = ImGui::GetCursorPosX();
162 if (ImGui::Button(_LC("ScriptMonitor", "Load")))
163 {
166 req->lsr_filename = filename;
168 }
169
170 ImVec2 rem_size = ImGui::CalcTextSize(_LC("ScriptMonitor", "Remove"));
171 ImGui::SameLine();
172 ImGui::SetCursorPosX(((cursorx + ImGui::GetFontSize() * 11.875f) - rem_size.x) - 2*ImGui::GetStyle().FramePadding.x);
173 if (ImGui::SmallButton(_LC("ScriptMonitor", "Remove")))
174 {
176 }
177
178 ImGui::PopID(); // filename.c_str()
179 }
180 }
181 }
182
183 ImGui::Columns(1); // reset
184}
185
187{
188 ImGui::NextColumn(); // begin new row
189 ImGui::NextColumn(); // skip ID column
190 ImVec2 pos = ImGui::GetCursorScreenPos() + ImVec2(ImGui::GetFontSize() * 0.625f, 0.f);
191 ImGui::Dummy(ImVec2(0.1f, ImGui::GetFontSize() * 0.15625f));
192 ImGui::Separator();
193 ImGui::Dummy(ImVec2(0.1f, ImGui::GetFontSize() * 0.15625f));
194 ImDrawList* drawlist = ImGui::GetWindowDrawList();
195 ImVec2 padding(ImGui::GetFontSize() * 0.3125f, 0.f);
196 ImVec2 rect_max = pos + padding*2 + ImGui::CalcTextSize(text);
197 drawlist->AddRectFilled(pos, rect_max, ImColor(ImGui::GetStyle().Colors[ImGuiCol_PopupBg]), ImGui::GetStyle().WindowRounding);
198 drawlist->AddText(pos + padding, ImColor(ImGui::GetStyle().Colors[ImGuiCol_TextDisabled]), text);
199 ImGui::NextColumn(); // skip Name column
200}
Game state manager and message-queue provider.
#define _LC(ctx, str)
Definition Language.h:38
unsigned int ar_vector_index
Sim attr; actor element index in std::vector<m_actors>
Definition Actor.h:465
std::string getTruckName()
Definition Actor.h:301
Ogre::String fname
filename
Definition CacheSystem.h:67
void DrawCommentedSeparator(const char *text)
Ogre::StringVector m_recent_displaylist
void PushMessage(Message m)
Doesn't guarantee order! Use ChainMessage() if order matters.
void ChainMessage(Message m)
Add to last pushed message's chain.
ScriptUnitMap const & getScriptUnits() const
@ MSG_APP_UNLOAD_SCRIPT_REQUESTED
Payload = RoR::ScriptUnitId_t* (owner)
Definition Application.h:95
@ MSG_APP_LOAD_SCRIPT_REQUESTED
Payload = RoR::LoadScriptRequest* (owner)
Definition Application.h:94
@ CUSTOM
Loaded by user via either: A) ingame console 'loadscript'; B) RoR.cfg 'app_custom_scripts'; C) comman...
@ AI_BOT
Only valid under MpState::LOCAL_SCRIPT; registers as RoRnet user in the server script (i....
@ GADGET
Associated with a .gadget mod file, launched via UI or any method given below for CUSTOM scripts (use...
@ TERRAIN
Defined in terrn2 file under '[Scripts]', receives terrain eventbox notifications.
@ ACTOR
Defined in truck file under 'scripts', contains global variable BeamClass@ thisActor.
CVar * app_recent_scripts
GameContext * GetGameContext()
ScriptEngine * GetScriptEngine()
CVar * app_custom_scripts
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition Utils.cpp:217
int ScriptUnitID_t
Unique sequentially generated ID of a loaded and running scriptin session. Use ScriptEngine::getScrip...
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition Utils.cpp:227
std::string lsr_filename
Load from resource ('.as' file or '.gadget' file); If buffer is supplied, use this as display name on...
ScriptCategory lsr_category
Unified game event system - all requests and state changes are reported using a message.
Definition GameContext.h:52
Represents a loaded script and all associated resources/handles.
CacheEntryPtr originatingGadget
For ScriptCategory::GADGET ~ determines resource group.
int32_t associatedNetUid
For ScriptCategory::AI_BOT ~ used to identify the bot in the server script.
ScriptCategory scriptCategory
Ogre::String scriptName
Name of the '.as' file exclusively.
ActorPtr associatedActor
For ScriptCategory::ACTOR.