载入中...
搜索中...
未找到
main.cpp
浏览该文件的文档.
1#include "common/config.h"
3#include "common/CrashLog.h"
5#include "cmdline/cmdline.h"
6#include <CLI11.hpp>
7#include <rang.hpp>
8#include <chrono>
9#include <cstdio>
10#include <ctime>
11#include <iostream>
12#include <string>
13#include <vector>
14
15#if defined(EVENGINE_ANDROID) || defined(EVENGINE_IOS) || defined(EVENGINE_WEBGPU)
16#include <SDL2/SDL.h>
17#include <SDL2/SDL_main.h>
18#endif
19
20#if defined(EVENGINE_IOS)
21#include "ios/ios.h"
22#endif
23
24#if defined(EVENGINE_WEBGPU)
25#include "webgpu/webplatform.h"
26#endif
27
28using namespace eve;
29using namespace std;
30
31namespace {
32// Earliest code we control: runs during static initialization, before main().
33const auto gEveStaticInitStart = std::chrono::steady_clock::now();
34} // namespace
35
36class MyFormatter : public CLI::Formatter {
37public:
38 std::string make_usage(const CLI::App *app, std::string name) const override {
39 std::string usage = "Usage: ";
40 usage += name;
41 usage += " [Options] [Root Folder]";
42 return usage;
43 }
44};
45
46int main(int argc, char **argv)
47{
48 // Make script print() / cout diagnostics visible immediately when stdout is
49 // redirected (CI logs, pipes): the C runtime fully buffers stdout when it
50 // is not a console, so a startup marker from eve_init could otherwise stay
51 // in the buffer and be lost when the process is killed after a smoke run.
52 std::setvbuf(stdout, nullptr, _IONBF, 0);
53 std::cout.setf(std::ios::unitbuf);
54
55 // Open the crash/error log before anything that could fail, so hard crashes
56 // and uncaught exceptions are persisted (see common/CrashLog.h).
58
59 // Host registration point for the default snapshot content hasher. Script and tooling
60 // paths cannot inject a SnapshotHashProvider, so without this they would have no digest
61 // at all; a module that registered a stronger hasher first is left untouched (NoOp).
62 // See common/SnapshotHash.h for why the built-in one is named non-cryptographic.
64
65#if defined(EVENGINE_WINDOWS) || defined(_WIN32)
66 eve::installCrashHandler();
67 // Diagnostic hook: EVE_TEST_CRASH=1 forces an access violation right after
68 // startup so the crash handler output can be verified.
69 const char *testCrash = std::getenv("EVE_TEST_CRASH");
70 if (testCrash && testCrash[0] != '\0' && testCrash[0] != '0') {
71 std::fprintf(stderr, "[crash] EVE_TEST_CRASH: forcing an access violation\n");
72 *static_cast<volatile int *>(nullptr) = 0;
73 }
74#endif
75 const auto tMain = std::chrono::steady_clock::now();
76 std::fprintf(stderr, "[startup] static-init -> main(): %.1f ms\n",
77 std::chrono::duration<double, std::milli>(tMain - gEveStaticInitStart).count());
78 std::fprintf(stderr, "[startup] process clock() at main(): %.1f ms\n",
79 (double) std::clock() * 1000.0 / (double) CLOCKS_PER_SEC);
80#if defined(EVENGINE_IOS)
81 // UIKit launches with no CLI args; inject `run <bundle game dir>` like Android Activity.
82 // Qualify eve::ios to avoid ambiguity with std::ios after using-directives.
83 static std::string gamePath;
84 static std::vector<char *> injected;
85 if (argc <= 1) {
86 gamePath = eve::ios::getGameDirectory();
87 if (gamePath.empty())
88 gamePath = ".";
89 static char runFlag[] = "run";
90 injected = {argv[0], runFlag, gamePath.data()};
91 argc = static_cast<int>(injected.size());
92 argv = injected.data();
93 }
94 eve::ios::initAudioSessionInterruptionHandler();
95#elif defined(EVENGINE_WEBGPU)
96 // The browser launches with no CLI args; inject `run <game root>` where
97 // the game is the preloaded VFS mount (/game) or the CWD.
98 static std::string gamePath;
99 static std::vector<char *> injected;
100 if (argc <= 1) {
101 gamePath = eve::webgpu_platform::getGameDirectory();
102 if (gamePath.empty())
103 gamePath = ".";
104 static char runFlag[] = "run";
105 injected = {argv[0], runFlag, gamePath.data()};
106 argc = static_cast<int>(injected.size());
107 argv = injected.data();
108 }
109#endif
110 const int exitCode = requireModInst(eve::cmd, Cmdline)->runArgs(argc, argv);
111 // An unattended reader tells "the run finished" from "the run died" by
112 // whether a session-end marker follows the session-start marker in eve.log.
113 eve::recordSessionEnd(exitCode);
114 return exitCode;
115}
std::string usage
Persistent, thread-safe record of engine crash and problem events.
std::string name
#define requireModInst(N, T)
Definition Module.h:39
Injectable content-digest provider for snapshots, with an honest default.
std::uint32_t count
std::string make_usage(const CLI::App *app, std::string name) const override
Definition main.cpp:38
const Status & status() const noexcept
Inspect the structured operation status.
Definition Result.h:487
int main(int argc, char **argv)
Definition main.cpp:46
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
void recordSessionEnd(int exitCode)
Appends the session-end marker carrying the process exit code.
Definition CrashLog.cpp:102
void initSystemLogging(const std::string &logDir)
Opens (append) the crash/error log file and records a session marker.
Definition CrashLog.cpp:64
Result< void > registerBuiltinSnapshotHasher()
Register the built-in hasher, unless a module already registered one.