载入中...
搜索中...
未找到
PcgPhotoModeApplySeam.cpp
浏览该文件的文档.
2
3#include "common/Capability.h"
4#include "common/CrashLog.h"
7#include "common/Value.h"
8
9#include <algorithm>
10#include <array>
11#include <cstdint>
12#include <memory>
13#include <string>
14#include <string_view>
15#include <utility>
16#include <vector>
17
18namespace eve::ui {
19namespace {
20
29struct DomainName {
31 std::string_view name;
32};
33
34constexpr std::array<DomainName, 12> kDomainNames = {{
35 {PhotoModeDomain::Photo, "photo"},
36 {PhotoModeDomain::FramePacing, "framepacing"},
37 {PhotoModeDomain::Graphics, "graphics"},
38 {PhotoModeDomain::Camera, "camera"},
39 {PhotoModeDomain::Streaming, "streaming"},
40 {PhotoModeDomain::Weather, "weather"},
41 {PhotoModeDomain::Lighting, "lighting"},
42 {PhotoModeDomain::Water, "water"},
43 {PhotoModeDomain::PostFx, "postfx"},
44 {PhotoModeDomain::Terrain, "terrain"},
45 {PhotoModeDomain::Grass, "grass"},
46 {PhotoModeDomain::Audio, "audio"},
47}};
48
50std::string_view domainName(PhotoModeDomain domain) {
51 for (const DomainName& entry : kDomainNames) {
52 if (entry.domain == domain) return entry.name;
53 }
54 return {};
55}
56
58bool parseDomain(std::string_view text, PhotoModeDomain* out) {
59 for (const DomainName& entry : kDomainNames) {
60 if (entry.name == text) {
61 if (out != nullptr) *out = entry.domain;
62 return true;
63 }
64 }
65 return false;
66}
67
69Value photoModeValueToScript(const PhotoModeValue& value) {
70 if (const auto* boolean = std::get_if<bool>(&value)) return Value::boolean(*boolean);
71 if (const auto* integer = std::get_if<std::int64_t>(&value)) return Value::integer(*integer);
72 if (const auto* number = std::get_if<float>(&value)) return Value::number(double(*number));
73 if (const auto* text = std::get_if<std::string>(&value)) return Value::string(*text);
74 const auto& color = std::get<PhotoModeColor>(value);
75 return Value::object({{"r", Value::number(double(color.r))},
76 {"g", Value::number(double(color.g))},
77 {"b", Value::number(double(color.b))},
78 {"a", Value::number(double(color.a))}});
79}
80
88class ScriptPhotoModeApplySink final : public IPhotoModeApplySink {
89public:
90 explicit ScriptPhotoModeApplySink(ScriptProviderHandle handle) : handle_(std::move(handle)) {}
91
93 [[nodiscard]] const ScriptProviderHandle& handle() const noexcept { return handle_; }
94
95 Result<void> applyPhotoModeAssignment(const PhotoModeAssignment& assignment) override {
96 const std::string_view name = domainName(assignment.domain);
97 const ScriptArg args[] = {ScriptArg::fromTable({{"field", Value::string(assignment.field)},
98 {"domain", Value::string(std::string(name))},
99 {"value", photoModeValueToScript(assignment.value)}})};
100 return ScriptProviderRegistry::instance().call(handle_, "applyPhotoModeAssignment", args);
101 }
102
103private:
104 ScriptProviderHandle handle_;
105};
106
114struct ScriptPhotoModeApplyProviders {
115 std::vector<std::unique_ptr<ScriptPhotoModeApplySink>> live;
116 std::vector<std::unique_ptr<ScriptPhotoModeApplySink>> retired;
117};
118
119ScriptPhotoModeApplyProviders& scriptPhotoModeApplyProviders() {
120 static ScriptPhotoModeApplyProviders state;
121 return state;
122}
123
124} // namespace
125
127 ScriptPhotoModeApplyProviders& owned = scriptPhotoModeApplyProviders();
131 // 单实现槽位:`provide` 会顶掉当前实现,顶掉必须可见(否则原生提供方会"悄悄失效",
132 // 而症状只是照片模式的字段不再生效)。
133 if (cap::query<IPhotoModeApplySink>() != nullptr) {
134 recordLogEvent("warn", "script photo-mode provider '" + handle.name +
135 "' replaced the provider that owned IPhotoModeApplySink");
136 }
137 auto adapter = std::make_unique<ScriptPhotoModeApplySink>(handle);
138 ScriptPhotoModeApplySink* raw = adapter.get();
139 // 先注册再交所有权:从这一刻起能力槽位持有裸指针,`owned.live` 是让它有效的唯一原因。
140 cap::provide<IPhotoModeApplySink>(raw);
141 owned.live.push_back(std::move(adapter));
142 return Result<void>::success();
143 },
145 const auto match = std::find_if(owned.live.begin(), owned.live.end(),
146 [&handle](const std::unique_ptr<ScriptPhotoModeApplySink>& adapter) {
147 return adapter->handle() == handle;
148 });
149 if (match == owned.live.end()) {
150 // 幂等:后置条件是"这一代没有适配器",模块退出时可能已经撤过了。
151 return Result<void>::success();
152 }
153 cap::revoke<IPhotoModeApplySink>(match->get());
154 owned.retired.push_back(std::move(*match));
155 owned.live.erase(match);
156 return Result<void>::success();
157 });
158}
159
161 ScriptPhotoModeApplyProviders& owned = scriptPhotoModeApplyProviders();
162 for (auto& adapter : owned.live) {
163 cap::revoke<IPhotoModeApplySink>(adapter.get());
164 owned.retired.push_back(std::move(adapter));
165 }
166 owned.live.clear();
167
170 .ignore("module teardown may already have dropped this seam");
171}
172
173} // namespace eve::ui
ActiveSource owned
std::vector< std::unique_ptr< ScriptClimbingEventSink > > live
std::vector< std::unique_ptr< ScriptClimbingEventSink > > retired
Persistent, thread-safe record of engine crash and problem events.
std::string name
Definition ECS.cpp:154
double raw
#define EVENGINE_API_WORLD
Definition Export.h:109
让脚本成为 IPhotoModeApplySink 的提供方(决定③的第一个落地插座)。
PrimitiveHandle handle
Object::Ptr value
std::string domain
double number
std::string text
ScriptStructure out
std::string_view adapter
Owning, renderer-independent dynamic values.
static constexpr const char * capabilityName
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Definition Result.h:571
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
Result< void > removeCapability(std::string_view capability)
Withdraw a declared seam when its owning module goes away.
static ScriptProviderRegistry & instance()
Process-lifetime registry; clearAll is the only reset.
void declareCapability(std::string capability, ScriptCapabilityBinder binder, ScriptCapabilityUnbinder unbinder)
Declare that one native capability may be served by a script provider.
Result< void > call(const ScriptProviderHandle &handle, std::string_view method) const
Call method() on a published provider.
static Value number(double value)
Compatibility factory for a Double value.
Definition Value.h:117
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:121
static Value integer(std::int64_t value)
Compatibility factory for an Int64 value.
Definition Value.h:115
static Value boolean(bool value)
Compatibility factory for a boolean value.
Definition Value.h:119
static Value object(Object value)
Compatibility factory for an object value.
Definition Value.cpp:129
EVENGINE_API_WORLD void declareScriptPhotoModeApplySeam()
声明"`IPhotoModeApplySink` 可以由脚本提供"这条接缝。
@ Value
UINode.value (normalized HUD fill)
EVENGINE_API_WORLD void withdrawScriptPhotoModeApplySeam()
撤回接缝与仍然在位的脚本适配器。
void recordLogEvent(const std::string &level, const std::string &message)
Appends a timestamped event line to the crash/error log.
Definition CrashLog.cpp:70
std::variant< bool, std::int64_t, float, std::string, PhotoModeColor > PhotoModeValue
Typed payload accepted by authoritative photo-mode domain owners.
PhotoModeDomain
Authoritative owner selected for a Pcg photo-mode field.
static ScriptArg fromTable(Value::Object fields)
One object argument (a table on the script side). @cost One Value tree copy.
Generation-qualified identity of one published script provider.
glm::vec4 color