YSTest: Environment.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Environment.cpp
浏览该文件的文档.
1 /*
2 © 2013-2016, 2018-2020 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "Helper/YModules.h"
29 #include YFM_Helper_Environment // for in_place_type, any;
30 #include YFM_Helper_Initialization // for InitializeEnvironment,
31 // ShowInitializedLog, PerformKeyAction, LoadComponents;
32 #if YCL_DS
33 # include YFM_YCLib_NativeAPI // for ::powerOn, ::defaultExceptionHandler,
34 // platform_ex::InitializeFileSystem;
35 # include YFM_DS_YCLib_DSVideo // for platform_ex::DSConsoleInit;
36 # include YFM_YSLib_Core_YGDIBase // for Drawing::ColorSpace;
37 #elif YCL_Win32
38 # include YFM_Win32_YCLib_Consoles // for platform_ex::FixConsoleHandler;
39 # include YFM_Win32_YCLib_MinGW32 // for platform_ex::Win32Exception;
40 # include YFM_YSLib_Core_YCoreUtilities // for FetchEnvironmentVariable;
41 # include YFM_YCLib_Debug // for platform_ex::SendDebugString;
42 #endif
43
44 namespace YSLib
45 {
46
47 using namespace platform_ex;
48 #if YF_Hosted
49 using namespace Host;
50 #endif
51
52 Environment::Environment(Application& app)
53 {
54 #if !YF_Hosted
55 // NOTE: This only effects freestanding implementations now, which may need
56 // different behavior than the default implementation. Hosted
57 // implemenations are expected to have termination handlers friendly to
58 // debugging, which can be overriden before the construction of
59 // %Environment if needed.
60 std::set_terminate(terminate);
61 #endif
62 #if YCL_DS
63
64 using namespace platform;
65 namespace ColorSpace = Drawing::ColorSpace;
66
67 ::powerOn(POWER_ALL);
68 ::defaultExceptionHandler();
69 DSConsoleInit(true, ColorSpace::Lime, ColorSpace::Black);
70 FetchCommonLogger().SetSender([&](Logger::Level lv, Logger&,
71 const char* str) YB_ATTR_LAMBDA_QUAL(ynothrowv, YB_NONNULL(4)){
72 if(ShowInitializedLog || lv <= Descriptions::Alert)
73 {
74 if(!ShowInitializedLog)
75 {
76 static const struct Init
77 {
78 Init()
79 {
80 DSConsoleInit({}, ColorSpace::White, ColorSpace::Blue);
81 }
82 } init;
83 }
84 // XXX: Error is ignored.
85 std::fprintf(stderr, "%s\n", Nonnull(str));
86 std::fflush(stderr);
87 }
88 });
89 PerformKeyAction([&]{
90 app.AddExit(any(in_place_type<FileSystem>));
91 }, yfsig, " LibFAT Failure ",
92 " An error is preventing the\n"
93 " program from accessing\n"
94 " external files.\n"
95 "\n"
96 " If you're using an emulator,\n"
97 " make sure it supports DLDI\n"
98 " and that it's activated.\n"
99 "\n"
100 " In case you're seeing this\n"
101 " screen on a real DS, make sure\n"
102 " you've applied the correct\n"
103 " DLDI patch (most modern\n"
104 " flashcards do this\n"
105 " automatically).\n"
106 "\n"
107 " Note: Some cards only\n"
108 " autopatch .nds files stored in\n"
109 " the root folder of the card.\n");
110 #elif YCL_Win32
111 TryExpr(FixConsoleHandler())
112 CatchExpr(Win32Exception&,
113 YTraceDe(Warning, "Console handler setup failed."))
114
115 string env_str;
116
117 // TODO: Extract as %YCoreUtilities functions?
118 if(FetchEnvironmentVariable(env_str, "YF_DEBUG_OUTPUT"))
119 FilterExceptions([&]{
120 if(env_str == "1")
121 FetchCommonLogger().SetSender(SendDebugString);
122 });
123 #endif
124 #if false
125 // TODO: Review locale APIs compatibility.
126 static yconstexpr const char locale_str[]{"zh_CN.GBK"};
127
128 if(!std::setlocale(LC_ALL, locale_str))
129 throw GeneralEvent("Call of std::setlocale() with %s failed.\n",
130 locale_str);
131 #endif
132 // NOTE: Ensure root node is initialized before lifetime of environment
133 // begins.
134 string res;
135
136 YTraceDe(Notice, "Checking installation...");
137 PerformKeyAction([&]{
138 Root = LoadConfiguration(true);
139 if(Root.GetName() == "YFramework")
140 Root = PackNodes(string(), std::move(Root));
141 LoadComponents(app, AccessNode(Root, "YFramework"));
142 YTraceDe(Notice, "Check of installation succeeded.");
143 }, yfsig, " Invalid Installation ",
144 " Please make sure the data is\n stored in correct directory.\n");
145 YF_Trace(Debug, "Environment lifetime began.");
146 }
147 Environment::~Environment()
148 {
149 YF_Trace(Debug, "Environment lifetime ended.");
150 }
151
152 } // namespace YSLib;
153
YSLib::Environment::~Environment
~Environment()
Definition: Environment.cpp:147
platform::Nonnull
YB_PURE _type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:518
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:1072
platform_ex
平台扩展命名空间。
Definition: Android.h:46
YB_ATTR_LAMBDA_QUAL
#define YB_ATTR_LAMBDA_QUAL(_q,...)
允许在 lambda 表达式的参数列表后和限定符共用的属性。
Definition: ydef.h:619
YSLib::Application::AddExit
void AddExit(_tParam &&arg)
Definition: YApplication.h:109
YSLib::Consoles::White
@ White
Definition: YConsole.h:68
platform::Logger::SetSender
PDefHOp(Logger &,=, const Logger &logger) ImplRet(ystdex void SetSender(Sender)
设置过滤器。
Definition: Debug.h:183
yfsig
#define yfsig
展开为表示函数签名的预定义变量的宏。
Definition: ydef.h:1133
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:178
YF_Trace
#define YF_Trace(_lv,...)
YCLib 默认调试日志追踪。
Definition: Debug.h:388
YSLib::Consoles::Blue
@ Blue
Definition: YConsole.h:62
YSLib::PerformKeyAction
YF_API void PerformKeyAction(function< void()>, const char *, const char *, string_view)
执行关键动作。
Definition: YCoreUtilities.cpp:38
YSLib::GeneralEvent
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: YException.h:46
platform_ex::Windows::FixConsoleHandler
YF_API void FixConsoleHandler(int(WINAPI *)(unsigned long)={}, bool=true)
安装控制台处理器。
YModules.h
Helper 库模块配置文件。
platform::terminate
YB_NORETURN YF_API void terminate() ynothrow
终止函数。
Definition: YCommon.cpp:48
YSLib::lv
const char RecordLevel lv
Definition: YException.cpp:61
platform::FetchCommonLogger
YF_API Logger & FetchCommonLogger()
取公共日志记录器。
platform::Descriptions::Alert
@ Alert
Definition: YCommon.h:180
YSLib::FetchEnvironmentVariable
bool FetchEnvironmentVariable(string &res, const char *var)
Definition: YCoreUtilities.cpp:77
YSLib::Application
程序实例。
Definition: YApplication.h:47
YSLib::PackNodes
YB_ATTR_nodiscard YB_PURE ValueNode PackNodes(_tString &&name, _tParams &&... args)
取以指定分量为参数对应初始化得到的值类型节点为子节点的值类型节点。
Definition: ValueNode.h:1408
platform::Descriptions::Notice
@ Notice
Definition: YCommon.h:184
YSLib::Consoles::Black
@ Black
Definition: YConsole.h:53
YSLib::Drawing::ColorSpace::Lime
@ Lime
Definition: YGDIBase.h:267
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:407
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:225
platform::Descriptions::Warning
@ Warning
Definition: YCommon.h:183
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
YSLib::LoadComponents
YF_API void LoadComponents(Application &, const ValueNode &)
初始化应用程序组件。
Definition: Initialization.cpp:707
platform
默认平台命名空间。
Definition: Container.h:56
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
YSLib::LoadConfiguration
YF_API ValueNode LoadConfiguration(bool={})
载入默认配置。
Definition: Initialization.cpp:730
YSLib::str
const char * str
Definition: YException.cpp:60
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:1219
YSLib::YB_NONNULL
YB_ATTR_nodiscard YF_API YB_NONNULL(1, 2) ValueNode LoadNPLA1File(const char *disp
载入 NPLA1 配置文件。
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:1634
platform::Logger
日志记录器。
Definition: Debug.h:97
YSLib::Environment::Environment
Environment(Application &)
构造：初始化环境。
Definition: Environment.cpp:52
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
YSLib::AccessNode
YB_ATTR_nodiscard YF_API YB_PURE ValueNode & AccessNode(ValueNode::Container *, const string &)
访问节点。
Definition: ValueNode.cpp:102
platform::Descriptions::Debug
@ Debug
Definition: YCommon.h:186