YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Initialization.cpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2009 - 2013.
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/Initialization.h"
29 #include "YSLib/Core/yapp.h"
30 #include "Helper/GUIApplication.h"
31 #include "YCLib/Debug.h"
32 #include "CHRLib/MapEx.h"
33 #include "YCLib/MemoryMapping.h"
34 #include <cstring> // for std::strcmp;
35 //#include <clocale>
36 
37 using namespace ystdex;
38 using namespace platform;
39 using std::puts;
40 
42 
43 using namespace Drawing;
44 using namespace IO;
45 
46 namespace
47 {
48 
49 #if !CHRLIB_NODYNAMIC_MAPPING
50 
51 platform::MappedFile* p_mapped;
52 #endif
53 
54 #if YCL_DS
55 # define ROOTW
56 # define DATA_DIRECTORY ROOTW "/Data/"
57  //const char* DEF_FONT_NAME = ROOTW "方正姚体";
58  //const_path_t DEF_FONT_PATH = ROOTW "/Font/FZYTK.TTF";
59 # define DEF_FONT_PATH ROOTW "/Font/FZYTK.TTF"
60 # define DEF_FONT_DIRECTORY ROOTW "/Font/"
61 #else
62 # define ROOTW "H:\\NDS\\EFSRoot"
63 # define DATA_DIRECTORY ROOTW "\\Data\\"
64  //const char* DEF_FONT_NAME = "方正姚体";
65  //const_path_t DEF_FONT_PATH = ROOTW "\\Font\\FZYTK.TTF";
66 # define DEF_FONT_PATH ROOTW "\\Font\\FZYTK.TTF"
67 # define DEF_FONT_DIRECTORY ROOTW "\\Font\\"
68 #endif
69 #define CONF_PATH "yconf.txt"
70 
71 void
72 LoadComponents(const ValueNode& node)
73 {
74  const auto& data_dir(AccessChild<string>(node, "DataDirectory"));
75  const auto& font_path(AccessChild<string>(node, "FontFile"));
76  const auto& font_dir(AccessChild<string>(node, "FontDirectory"));
77 
78  if(!data_dir.empty() && !font_path.empty() && !font_dir.empty())
79  std::printf("Loaded default directory:\n%s\n"
80  "Loaded default font path:\n%s\n"
81  "Loaded default font directory:\n%s\n",
82  data_dir.c_str(), font_path.c_str(), font_dir.c_str());
83  else
84  throw LoggedEvent("Empty path loaded.");
85 #if !CHRLIB_NODYNAMIC_MAPPING
86  puts("Load character mapping file...");
87  p_mapped = new MappedFile(data_dir + "cp113.bin");
88  if(p_mapped->GetSize() != 0)
89  CHRLib::cp113 = p_mapped->GetPtr();
90  else
91  throw LoggedEvent("CHRMapEx loading fail.");
92  puts("CHRMapEx loaded successfully.");
93 #endif
94  std::printf("Trying entering directory %s ...\n", data_dir.c_str());
95  if(!udirexists(data_dir))
96  throw LoggedEvent("Invalid default data directory found.");
97  if(!(ufexists(font_path) || udirexists(font_dir)))
98  throw LoggedEvent("Invalid default font file path found.");
99 }
100 
101 } // unnamed namespace;
102 
103 void
104 HandleFatalError(const FatalError& e) ynothrow
105 {
106  YDebugSetStatus();
107  YDebugBegin();
108 
109  const char* line("--------------------------------");
110 
111  std::printf("%s%s%s\n%s\n%s",
112  line, e.GetTitle(), line, e.GetContent(), line);
113  terminate();
114 }
115 
116 
117 ValueNode
119 {
120  if(YB_LIKELY(tf))
121  {
123  throw LoggedEvent("Wrong encoding of configuration file.");
124 
125  NPL::Configuration conf;
126 
127  tf >> conf;
128  if(conf.GetNodeRRef().GetSize() != 0)
129  return conf.GetNodeRRef();
130  }
131  throw LoggedEvent("Invalid file found when reading configuration.");
132 }
133 
134 void
136 {
137  if(YB_UNLIKELY(!tf))
138  throw LoggedEvent("Invalid file found when writing configuration.");
139  tf << NPL::Configuration("", node.Value);
140 }
141 
142 ValueNode
143 LoadConfiguration(bool bInfo)
144 {
145  if(!ufexists(CONF_PATH))
146  {
147  if(bInfo)
148  std::printf("Creating configuration file '%s'...\n", CONF_PATH);
149  if(TextFile tf{CONF_PATH, std::ios_base::out | std::ios_base::trunc})
150  tf << NPL::Configuration(PackNodes("YFramework", MakeNode(
151  "DataDirectory", string(DATA_DIRECTORY)), MakeNode("FontFile",
152  string(DEF_FONT_PATH)),
153  MakeNode("FontDirectory", string(DEF_FONT_DIRECTORY))));
154  else
155  throw LoggedEvent("Cannot create file.");
156  }
157 
158  TextFile tf(CONF_PATH);
159 
160  if(bInfo)
161  std::printf("Found configuration file '%s'.\n", CONF_PATH);
162  return ReadConfiguration(tf);
163 }
164 
165 void
167 {
168  TextFile tf(CONF_PATH, std::ios_base::out | std::ios_base::trunc);
169 
170  WriteConfiguration(tf, node);
171 }
172 
173 
174 void
176 {
177  //设置默认异常终止函数。
178  std::set_terminate(terminate);
179 #if YCL_DS
180  //启用设备。
181  ::powerOn(POWER_ALL);
182 
183  //启用 LibNDS 默认异常处理。
184  ::defaultExceptionHandler();
185 
186  //初始化主控制台。
188 
189  //初始化文件系统。
190  //初始化 EFSLib 和 LibFAT 。
191  //当 .nds 文件大于32MB时, EFS 行为异常。
192 # ifdef USE_EFS
193  if(!::EFS_Init(EFS_AND_FAT | EFS_DEFAULT_DEVICE, nullptr))
194  {
195  //如果初始化 EFS 失败则初始化 FAT 。
196 # endif
197  if(!::fatInitDefault())
198  throw FatalError(" LibFAT Failure ",
199  " An error is preventing the\n"
200  " program from accessing\n"
201  " external files.\n"
202  "\n"
203  " If you're using an emulator,\n"
204  " make sure it supports DLDI\n"
205  " and that it's activated.\n"
206  "\n"
207  " In case you're seeing this\n"
208  " screen on a real DS, make sure\n"
209  " you've applied the correct\n"
210  " DLDI patch (most modern\n"
211  " flashcards do this\n"
212  " automatically).\n"
213  "\n"
214  " Note: Some cards only\n"
215  " autopatch .nds files stored in\n"
216  " the root folder of the card.\n");
217 # ifdef USE_EFS
218  }
219 # endif
220 #endif
221 #if 0
222  // TODO: Review locale APIs compatibility.
223  static yconstexpr char locale_str[]{"zh_CN.GBK"};
224 
225  if(!std::setlocale(LC_ALL, locale_str))
226  throw LoggedEvent("Call of std::setlocale() with %s failed.\n",
227  locale_str);
228 #endif
229 }
230 
231 ValueNode
233 {
234  puts("Checking installation...");
235  try
236  {
237  auto node(LoadConfiguration(true));
238 
239  if(node.GetName() == "YFramework")
240  node = PackNodes("", std::move(node));
241  LoadComponents(node.GetNode("YFramework"));
242  puts("OK!");
243  return std::move(node);
244  }
245  catch(std::exception& e)
246  {
247  std::printf("Error occurred: %s\n", e.what());
248  }
249  throw FatalError(" Invalid Installation ",
250  " Please make sure the data is\n"
251  " stored in correct directory.\n");
252 }
253 
254 void
256  const string& fong_file, const string& font_dir)
257 {
258  puts("Loading font files...");
259  try
260  {
261  size_t nFileLoaded(fc.LoadTypefaces(fong_file) != 0);
262 
263  if(!font_dir.empty())
264  //读取字体文件目录并载入目录下指定后缀名的字体文件。
265  if(HFileNode dir{font_dir.c_str()})
266  while((++dir).LastError == 0)
267  if(std::strcmp(dir.GetName(), FS_Now) != 0
268  && !dir.IsDirectory()
269  /*&& IsExtensionOf(ext, dir.GetName())*/)
270  {
271  FontPath path(font_dir + dir.GetName());
272 
273  if(path != fong_file)
274  nFileLoaded += fc.LoadTypefaces(path) != 0;
275  }
276  fc.InitializeDefaultTypeface();
277  if(const auto nFaces = fc.GetFaces().size())
278  std::printf("%u face(s) in %u font file(s)"
279  " are loaded\nsuccessfully.\n", nFaces, nFileLoaded);
280  else
281  throw LoggedEvent("No fonts found.");
282  puts("Setting default font face...");
283  if(const auto* const pf = fc.GetDefaultTypefacePtr())
284  std::printf("\"%s\":\"%s\",\nsuccessfully.\n",
285  pf->GetFamilyName().c_str(), pf->GetStyleName().c_str());
286  else
287  throw LoggedEvent("Setting default font face failed.");
288  return;
289  }
290  // TODO: Use %std::nested_exception.
291  catch(std::exception& e)
292  {
293  puts(e.what());
294  }
295  throw FatalError(" Font Caching Failure ",
296  " Please make sure the fonts are\n"
297  " stored in correct path.\n");
298 }
299 
300 void
302 {
303 #if !CHRLIB_NODYNAMIC_MAPPING
304  delete p_mapped;
305 #endif
306 }
307 
308 YSL_END
309