Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REveAssetDir.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Author: Matevz Tadel, 2026
3
4/*************************************************************************
5 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "REveAssetDir.hxx"
13
14#include <ROOT/REveManager.hxx>
15#include <ROOT/RWebWindow.hxx>
16
17#include "THttpServer.h"
18#include "TEnv.h"
19#include "TROOT.h"
20#include "TString.h"
21#include "TSystem.h"
22
23#include <algorithm>
24
25using namespace ROOT::Experimental::Internal;
26namespace REX = ROOT::Experimental;
27
28/** \class ROOT::Experimental::Internal::REveAssetDir
29Directories of one kind of file the REve server serves under one http location
30prefix, such as the SDF fonts under "sdf-fonts/". Not public API: REveText and
31REveLogo each hold one instance and expose its setters.
32
33A file is looked up in the user directories, in the order they were set and
34added, then in the fallback directories, which hold files shipped with ROOT and
35may be read-only. The shipped files of a kind are in its subdirectory of
36GetAssetsDir(). The server serves a requested file from the first directory
37that has it. An optional write directory is where new files are written; it is
38created only when something is written.
39
40Register() is the only place that passes the directories to the server. The
41setters call it, and the REveManager constructor calls RegisterAll(), so the
42directories can be set before REveManager::Create().
43*/
44
45////////////////////////////////////////////////////////////////////////////////
46/// All existing instances, for RegisterAll().
47
48std::vector<REveAssetDir *> &REveAssetDir::Instances()
49{
50 static std::vector<REveAssetDir *> s_instances;
51 return s_instances;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor. `prefix` is the http location prefix, with a trailing slash.
56
57REveAssetDir::REveAssetDir(const std::string &prefix) : fPrefix(prefix)
58{
59 Instances().push_back(this);
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Destructor.
64
66{
67 auto &v = Instances();
68 v.erase(std::remove(v.begin(), v.end(), this), v.end());
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Return `dir` with a trailing slash; an empty `dir` stays empty.
73
74std::string REveAssetDir::NormalizeDir(const std::string &dir)
75{
76 if (dir.empty() || dir.back() == '/')
77 return dir;
78 return dir + '/';
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// Return the directory the server maps to "rootui5sys/", found as
83/// RWebWindowsManager finds it: $ROOTUI5SYS, else WebGui.RootUi5Path, else the
84/// ui5 directory in TROOT::GetDataDir().
85
87{
88 TString ui5dir = gSystem->Getenv("ROOTUI5SYS");
89 if (ui5dir.Length() == 0)
90 ui5dir = gEnv->GetValue("WebGui.RootUi5Path", "");
91 if (ui5dir.Length() == 0)
92 ui5dir.Form("%s/ui5", TROOT::GetDataDir().Data());
93 gSystem->ExpandPathName(ui5dir);
94 return ui5dir.Data();
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Return eve7/assets/ in the ui5 directory, where the build installs the
99/// RenderCore assets tarball. It has one subdirectory per kind of asset, the
100/// fallback directory of that kind.
101
103{
104 return GetUi5Dir() + "/eve7/assets/";
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Replace the user directories with `dir`.
109
110void REveAssetDir::SetUserDir(const std::string &dir)
111{
112 fUserDirs = {NormalizeDir(dir)};
113 Register();
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// Append `dir` to the user directories.
118
119void REveAssetDir::AddUserDir(const std::string &dir)
120{
121 fUserDirs.push_back(NormalizeDir(dir));
122 Register();
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// Append `dir` to the fallback directories.
127
128void REveAssetDir::AddFallbackDir(const std::string &dir)
129{
130 fFallbackDirs.push_back(NormalizeDir(dir));
131 Register();
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Return the path of `file` in the first directory that has it, user
136/// directories first, or an empty string if none has it.
137
138std::string REveAssetDir::Find(const std::string &file) const
139{
140 for (auto *dirs : {&fUserDirs, &fFallbackDirs}) {
141 for (auto &dir : *dirs) {
142 std::string path = dir + file;
143 if (!gSystem->AccessPathName(path.c_str()))
144 return path;
145 }
146 }
147 return {};
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Return the first directory that has `file1` or `file2` if it has both, else
152/// an empty string. The server then serves both files from that directory, as
153/// it serves each file from the first directory that has it.
154
155std::string REveAssetDir::FindPair(const std::string &file1, const std::string &file2) const
156{
157 for (auto *dirs : {&fUserDirs, &fFallbackDirs}) {
158 for (auto &dir : *dirs) {
159 bool has1 = !gSystem->AccessPathName((dir + file1).c_str());
160 bool has2 = !gSystem->AccessPathName((dir + file2).c_str());
161 if (has1 || has2)
162 return has1 && has2 ? dir : std::string();
163 }
164 }
165 return {};
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Return the path to write `file` to, creating the write directory if it does
170/// not exist. Return an empty string if there is no write directory or it cannot
171/// be created.
172
173std::string REveAssetDir::WritePath(const std::string &file) const
174{
175 if (fWriteDir.empty())
176 return {};
177 if (gSystem->AccessPathName(fWriteDir.c_str()) && gSystem->mkdir(fWriteDir.c_str(), true))
178 return {};
179 return fWriteDir + file;
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Pass the directories to the server, user directories first, then fallback
184/// directories, each once. Does nothing before REveManager exists or when there
185/// are no directories.
186
188{
189 if (!REX::gEve)
190 return;
191 auto serv = REX::gEve->GetWebWindow()->GetServer();
192 if (!serv)
193 return;
194
195 std::vector<std::string> dirs;
196 for (auto *src : {&fUserDirs, &fFallbackDirs}) {
197 for (auto &dir : *src) {
198 if (std::find(dirs.begin(), dirs.end(), dir) == dirs.end())
199 dirs.push_back(dir);
200 }
201 }
202 if (dirs.empty())
203 return;
204
205 serv->AddLocation(fPrefix.c_str(), dirs.front().c_str());
206 for (size_t i = 1; i < dirs.size(); ++i)
207 serv->AppendLocation(fPrefix.c_str(), dirs[i].c_str());
208}
209
210////////////////////////////////////////////////////////////////////////////////
211/// Register all instances. Called from the REveManager constructor.
212
214{
215 for (auto *ad : Instances())
216 ad->Register();
217}
R__EXTERN TEnv * gEnv
Definition TEnv.h:126
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
static std::string NormalizeDir(const std::string &dir)
Return dir with a trailing slash; an empty dir stays empty.
void SetUserDir(const std::string &dir)
Replace the user directories with dir.
void AddUserDir(const std::string &dir)
Append dir to the user directories.
std::vector< std::string > fUserDirs
set by the user, searched first, in order
std::string WritePath(const std::string &file) const
Return the path to write file to, creating the write directory if it does not exist.
std::string fWriteDir
where new files are written; empty if none
void AddFallbackDir(const std::string &dir)
Append dir to the fallback directories.
static void RegisterAll()
Register all instances. Called from the REveManager constructor.
std::string fPrefix
http location prefix, such as "sdf-fonts/"
std::string Find(const std::string &file) const
Return the path of file in the first directory that has it, user directories first,...
static std::vector< REveAssetDir * > & Instances()
All existing instances, for RegisterAll().
REveAssetDir(const std::string &prefix)
Constructor. prefix is the http location prefix, with a trailing slash.
static std::string GetUi5Dir()
Return the directory the server maps to "rootui5sys/", found as RWebWindowsManager finds it: $ROOTUI5...
void Register() const
Pass the directories to the server, user directories first, then fallback directories,...
static std::string GetAssetsDir()
Return eve7/assets/ in the ui5 directory, where the build installs the RenderCore assets tarball.
std::string FindPair(const std::string &file1, const std::string &file2) const
Return the first directory that has file1 or file2 if it has both, else an empty string.
std::vector< std::string > fFallbackDirs
shipped with ROOT, read-only, searched after fUserDirs
std::shared_ptr< ROOT::RWebWindow > GetWebWindow() const
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:511
static const TString & GetDataDir()
Get the data directory in the installation. Static utility function.
Definition TROOT.cxx:3407
Basic string class.
Definition TString.h:137
Ssiz_t Length() const
Definition TString.h:426
const char * Data() const
Definition TString.h:385
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2438
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1296
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1687
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition TSystem.cxx:921
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1318
Namespace for ROOT features in testing.
Definition TROOT.h:100
R__EXTERN REveManager * gEve