Fray Engine
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/* main.cpp */
2#include "main.hpp"
3
4#include <filesystem>
5#include <string>
6
16 const char* executable,
17 F_ushort port)
18{
19 const std::filesystem::path electronDirectory =
20 (std::filesystem::absolute(executable).parent_path() / ".." / ".." / ".." / "Electron").lexically_normal();
21 return fray::process::spawn({"npm", "run", "start"}, electronDirectory,
22 {{"FRAY_ENGINE_PORT", std::to_string(port)}});
23}
24
34 F_long argc,
35 F_char *argv[])
36{
39 bool launchUI = true;
40 std::string arguments;
41 for (F_long i = 0; i < argc; i++)
42 {
43 arguments += (i ? "\n" : "") + std::string(argv[i]);
44 if (std::string(argv[i]) == "--no-ui")
45 launchUI = false;
46 }
47 fray::log::engineInfo("Received the following arguments:\n%s", arguments.c_str());
48
49 fray::log::engineInfo("Starting Engine.");
50
51 // main owns the ThreadManager and declares it first, so it is destroyed last. Every engine thread
52 // registers here, and it names any thread still running when the engine exits.
54 fray::FrayManager app(threads);
55 fray::ipc::IPCManager ipc(threads, app);
56
57 // Port 0 lets the OS pick a free port, which Electron is told about when it is launched.
58 app.logStore().setApplicationFolder(std::filesystem::absolute(argv[0]).parent_path() / "logs");
59
60 // Replay startup lines and forward subsequent logs while the managers are alive.
61 fray::log::EngineLogSink::Attachment logAttachment(logSink, app, ipc);
62
63 const F_err stateResult = app.openApplicationState(std::filesystem::absolute(argv[0]).parent_path());
64 if (stateResult != fray::errors::NONE)
65 {
66 fray::log::engineError("Cannot open the application database; Engine startup stopped.");
67 return stateResult;
68 }
69
71 const auto port = ipc.createTCPServer(0)->port();
72 ipc.start();
73 const F_err restoreResult = app.restoreProjects(ipc);
74 if (restoreResult != fray::errors::NONE)
75 {
76 fray::log::engineError("Could not restore the previous project session.");
78 fray::FrayManager::OpeningOrigin{fray::PROCESS_KIND_ELECTRON_RENDERER, fray::message::MAIN_WINDOW_ID, 0, false},
79 "", restoreResult);
80 }
81
82 F_err result = fray::errors::NONE;
83 if (launchUI)
84 {
85 result = launchElectron(argv[0], port);
86 if (result != fray::errors::NONE)
88 else
89 {
90 ipc.startLoading(BUILD_NAME, std::to_string(APPLICATION_VERSION_MAJOR) + "." +
91 std::to_string(APPLICATION_VERSION_MINOR) + "." + std::to_string(APPLICATION_VERSION_BUG));
92 }
93 }
94
95 // The network and message threads do the work; main waits until the UI sends the quit command.
96 if (result == fray::errors::NONE)
97 ipc.wait();
98 logSink.stopForwarding();
99
100 fray::log::engineInfo("Exiting Engine.");
101 ipc.finish();
102 return result;
103}
Owns and coordinates projects, their queues, application work, prompts, and logs. Project work runs o...
F_err openApplicationState(const std::filesystem::path &directory)
Opens the application directory and its default state namespace.
F_err restoreProjects(ipc::IPCManager &ipc)
Queues restoration of the saved project session when enabled.
logs::LogStore & logStore()
Returns the manager-owned log store by reference.
Owns the TCP servers and the threads around them.
F_void setFontReader(std::function< std::vector< std::string >()> reader)
Sets how installed font families are listed during startup. Call before startLoading().
F_void start()
Starts the network thread and the engine message thread, then returns. Does nothing if already starte...
F_void startLoading(const std::string &name, const std::string &version)
Queues the initial hidden loading window and its application information.
TCPServerSP createTCPServer(F_ushort port)
Creates a TCP server and starts it accepting. Must be called before start(). Not thread-safe; call fr...
F_void finish()
Stops IPC, releases servers, and shuts the application down exactly once. Call on the controlling thr...
F_void wait()
Blocks until every server has stopped and the queued messages have been handled. While waiting it pos...
Keeps the sink attached to live managers and detaches it on destruction. Declare after the managers s...
Captures Engine logs before managers exist and forwards them once attached. Construct before startup ...
F_void stopForwarding()
Stops forwarding to the renderer while continuing to store Engine logs.
void setApplicationFolder(const std::filesystem::path &folder)
Set the destination folder for application and fallback entries. Locks _mutex and closes the current ...
Registry of the engine's threads: starts them, names them, asks them to stop and joins them....
F_long F_err
Error return type used across the Engine; the value is a fray::errors::code (0 / NONE means success).
Definition errors.hpp:13
static F_err launchElectron(const char *executable, F_ushort port)
Starts the Electron UI (npm run start in the Electron folder) without waiting for it....
Definition main.cpp:15
F_err main(F_long argc, F_char *argv[])
Engine entry point: builds the managers, starts the TCP server and message threads,...
Definition main.cpp:33
#define APPLICATION_VERSION_BUG
Application bug-fix (patch) version number, shared by Engine and Electron.
Definition main.hpp:17
#define APPLICATION_VERSION_MINOR
Application minor version number, shared by Engine and Electron.
Definition main.hpp:15
#define BUILD_NAME
Public name of the current application build.
Definition main.hpp:11
#define APPLICATION_VERSION_MAJOR
Application major version number, shared by Engine and Electron.
Definition main.hpp:13
const F_char * description(code err)
Formats an error code as "Error <number>: <description>". Not thread-safe: the text lives in a shared...
Definition errors.hpp:96
std::vector< std::string > installedFamilies()
Lists the font families installed on this computer. Safe to call from any thread; it can take a while...
F_void engineInfo(const F_char *fmt,...)
Log info for Engine.
F_void engineError(const F_char *fmt,...)
Log error for Engine.
F_void engineSession(const std::string &name)
Marks the start of a log session under the calling thread's project tag. The Engine records it as a s...
constexpr std::uint32_t MAIN_WINDOW_ID
Renderer instance ID assigned to the first application window.
Definition message.hpp:40
F_err spawn(const std::vector< std::string > &command, const std::filesystem::path &workingDirectory, const std::vector< std::pair< std::string, std::string > > &environment={})
Starts a child process without waiting for it (fire and forget). The child inherits this process's st...
void sendProjectOpenResult(ipc::IPCManager &ipc, const FrayManager::OpeningOrigin &origin, const std::string &path, F_err result)
Sends the result of a project-open or create request.
Address of the process that requested a project open.
uint16_t F_ushort
Unsigned 16-bit integer (used for ports).
Definition types.hpp:30
int32_t F_long
Signed 32-bit integer (also the base of F_err).
Definition types.hpp:32
char F_char
Alias for char.
Definition types.hpp:24