YSTest: Environment.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

Environment.cpp
浏览该文件的文档.
1 /*
2 © 2013-2016 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
30 #include YFM_Helper_Initialization // for InitializeEnvironment,
31 // ShowInitializedLog, LoadComponents, ExtractInitException;
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_YCLib_Video // for platform::ColorSpace;
37 #elif YCL_Win32
38 # include YFM_Win32_YCLib_MinGW32 // for platform_ex::FixConsoleHandler,
39 // 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 std::set_terminate(terminate);
55 #if YCL_DS
56
57 using namespace platform;
58
59 ::powerOn(POWER_ALL);
60 ::defaultExceptionHandler();
61 DSConsoleInit(true, ColorSpace::Lime);
62 FetchCommonLogger().SetSender([&](Logger::Level lv, Logger&,
63 const char* str) YB_NONNULL(4) ynothrowv{
64 if(ShowInitializedLog || lv <= Descriptions::Alert)
65 {
66 if(!ShowInitializedLog)
67 {
68 static struct Init
69 {
70 Init()
71 {
72 DSConsoleInit({}, ColorSpace::White, ColorSpace::Blue);
73 }
74 } init;
75 }
76 std::fprintf(stderr, "%s\n", Nonnull(str));
77 std::fflush(stderr);
78 }
79 });
80 TryExpr(app.AddExit(ystdex::any(ystdex::any_ops::in_place_t<FileSystem>())))
81 // TODO: Use 'std::throw_with_nested'?
82 CatchThrow(..., FatalError(
83 " LibFAT Failure ",
84 " An error is preventing the\n"
85 " program from accessing\n"
86 " external files.\n"
87 "\n"
88 " If you're using an emulator,\n"
89 " make sure it supports DLDI\n"
90 " and that it's activated.\n"
91 "\n"
92 " In case you're seeing this\n"
93 " screen on a real DS, make sure\n"
94 " you've applied the correct\n"
95 " DLDI patch (most modern\n"
96 " flashcards do this\n"
97 " automatically).\n"
98 "\n"
99 " Note: Some cards only\n"
100 " autopatch .nds files stored in\n"
101 " the root folder of the card.\n"))
102 #elif YCL_Win32
103 TryExpr(FixConsoleHandler())
104 CatchExpr(Win32Exception&,
105 YTraceDe(Warning, "Console handler setup failed."))
106
107 string env_str;
108
109 // TODO: Extract as %YCoreUtilities functions?
110 if(FetchEnvironmentVariable(env_str, "YF_DEBUG_OUTPUT"))
111 FilterExceptions([&]{
112 if(env_str == "1")
113 FetchCommonLogger().SetSender(
114 platform_ex::SendDebugString);
115 });
116 #endif
117 #if 0
118 // TODO: Review locale APIs compatibility.
119 static yconstexpr const char locale_str[]{"zh_CN.GBK"};
120
121 if(!std::setlocale(LC_ALL, locale_str))
122 throw GeneralEvent("Call of std::setlocale() with %s failed.\n",
123 locale_str);
124 #endif
125 // NOTE: Ensure root node is initialized before lifetime of environment
126 // begins.
127 string res;
128
129 YTraceDe(Notice, "Checking installation...");
130 try
131 {
132 Root = LoadConfiguration(true);
133 if(Root.GetName() == "YFramework")
134 Root = PackNodes(string(), std::move(Root));
135 LoadComponents(app, AccessNode(Root, "YFramework"));
136 YTraceDe(Notice, "Check of installation succeeded.");
137 YCL_Trace(Debug, "Environment lifetime began.");
138 return;
139 }
140 CatchExpr(std::exception& e, ExtractInitException(e, res))
141 CatchExpr(..., res += "Unknown exception @ InitializeInstalled.\n")
142 throw FatalError(" Invalid Installation ",
143 " Please make sure the data is\n"
144 " stored in correct directory.\n" + res);
145 }
146 Environment::~Environment()
147 {
148 YCL_Trace(Debug, "Environment lifetime ended.");
149 }
150
151 } // namespace YSLib;
152
YSLib::lv
RecordLevel lv
Definition: YException.cpp:58
YSLib::FatalError
致命错误。
Definition: YException.h:89
YSLib::Consoles::Blue
Definition: YConsole.h:62
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:653
platform::Nonnull
_type && Nonnull(_type &&p) ynothrow
断言并返回非空指针。
Definition: Debug.h:411
platform::Descriptions::Warning
Definition: YCommon.h:157
ystdex::any_ops::in_place_t
Definition: any.h:83
YSLib::FetchEnvironmentVariable
bool FetchEnvironmentVariable(string &res, const char *var)
Definition: YCoreUtilities.cpp:36
YSLib::GeneralEvent
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: YException.h:45
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
platform::Logger::SetSender
void SetSender(Sender)
设置发送器。
Definition: Debug.cpp:98
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:202
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
YSLib::AccessNode
name name name name YF_API ValueNode & AccessNode(ValueNode &, size_t)
Definition: ValueNode.cpp:81
YSLib::Environment::Environment
Environment(Application &)
构造：初始化环境。
Definition: Environment.cpp:52
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:305
YSLib::PackNodes
ValueNode PackNodes(_tString &&name, _tParams &&...args)
取以指定分量为参数对应初始化得到的值类型节点为子节点的值类型节点。
Definition: ValueNode.h:819
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:823
YSLib::Application
程序实例。
Definition: YApplication.h:46
platform
默认平台命名空间。
Definition: Container.h:47
platform::FetchCommonLogger
YF_API Logger & FetchCommonLogger()
取公共日志记录器。
Definition: Debug.cpp:223
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
YSLib::Environment::~Environment
~Environment()
Definition: Environment.cpp:146
YCL_Trace
#define YCL_Trace(_lv,...)
YCLib 默认调试跟踪。
Definition: Debug.h:286
YSLib::Application::AddExit
void AddExit(_tParam &&arg)
Definition: YApplication.h:103
platform_ex
平台扩展命名空间。
Definition: Android.h:45
YSLib::Text::str
const char const char * str
Definition: TextFile.h:77
YSLib::LoadComponents
YF_API void LoadComponents(Application &, const ValueNode &)
初始化应用程序组件。
Definition: Initialization.cpp:343
YSLib::Consoles::White
Definition: YConsole.h:68
platform::Logger
日志记录器。
Definition: Debug.h:86
YSLib::YB_NONNULL
YF_API YB_NONNULL(1, 2) ValueNode LoadNPLA1File(const char *disp
载入 NPLA1 配置文件。
platform::Descriptions::Debug
Definition: YCommon.h:160
YModules.h
platform::terminate
YB_NORETURN YF_API void terminate() ynothrow
异常终止函数。
Definition: YCommon.cpp:42
YSLib::ExtractInitException
YF_API void ExtractInitException(const std::exception &, string &) ynothrow
展开初始化异常。
Definition: Initialization.cpp:277
YSLib::LoadConfiguration
YF_API ValueNode LoadConfiguration(bool={})
载入默认配置。
Definition: Initialization.cpp:411
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:151
platform::Descriptions::Notice
Definition: YCommon.h:158
platform::Descriptions::Alert
Definition: YCommon.h:154
CatchThrow
#define CatchThrow(_ex,...)
指定抛出指定异常作为异常处理器。
Definition: YBaseMacro.h:141