MainPage.xaml.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //
  2. // MainPage.xaml.cpp
  3. // MainPage 类的实现。
  4. //
  5. #include "pch.h"
  6. #include "MainPage.xaml.h"
  7. #include "DownloadDialog.xaml.h"
  8. #include "StringHelper.h"
  9. #include "AsyncHelper.h"
  10. #include "global.h"
  11. #include "palcfg.h"
  12. #include "util.h"
  13. #include "generated.h"
  14. using namespace SDLPal;
  15. using namespace Platform;
  16. using namespace Windows::Foundation;
  17. using namespace Windows::Foundation::Collections;
  18. using namespace Windows::UI::Core;
  19. using namespace Windows::UI::Popups;
  20. using namespace Windows::UI::Xaml;
  21. using namespace Windows::UI::Xaml::Controls;
  22. using namespace Windows::UI::Xaml::Controls::Primitives;
  23. using namespace Windows::UI::Xaml::Data;
  24. using namespace Windows::UI::Xaml::Input;
  25. using namespace Windows::UI::Xaml::Media;
  26. using namespace Windows::UI::Xaml::Navigation;
  27. static Platform::String^ msg_file_exts[] = { ".msg" };
  28. static Platform::String^ font_file_exts[] = { ".bdf" };
  29. static Platform::String^ log_file_exts[] = { ".log" };
  30. MainPage^ MainPage::Current = nullptr;
  31. MainPage::MainPage()
  32. : m_dlg(nullptr)
  33. {
  34. InitializeComponent();
  35. Current = this;
  36. m_controls = ref new Platform::Collections::Map<Platform::String^, ButtonAttribute^>();
  37. m_controls->Insert(btnBrowseMsgFile->Name, ref new ButtonAttribute(tbMsgFile, ref new Platform::Array<Platform::String^>(msg_file_exts, sizeof(msg_file_exts) / sizeof(msg_file_exts[0]))));
  38. m_controls->Insert(btnBrowseFontFile->Name, ref new ButtonAttribute(tbFontFile, ref new Platform::Array<Platform::String^>(font_file_exts, sizeof(font_file_exts) / sizeof(font_file_exts[0]))));
  39. m_controls->Insert(btnBrowseLogFile->Name, ref new ButtonAttribute(tbLogFile, ref new Platform::Array<Platform::String^>(log_file_exts, sizeof(log_file_exts) / sizeof(log_file_exts[0]))));
  40. m_controls->Insert(cbUseMsgFile->Name, ref new ButtonAttribute(gridMsgFile, nullptr));
  41. m_controls->Insert(cbUseFontFile->Name, ref new ButtonAttribute(gridFontFile, nullptr));
  42. m_controls->Insert(cbUseLogFile->Name, ref new ButtonAttribute(gridLogFile, nullptr));
  43. m_acl[PALCFG_GAMEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_GAMEPATH)));
  44. m_acl[PALCFG_SAVEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_SAVEPATH)));
  45. m_acl[PALCFG_MESSAGEFILE] = ref new AccessListEntry(tbMsgFile, cbUseMsgFile, ConvertString(PAL_ConfigName(PALCFG_MESSAGEFILE)));
  46. m_acl[PALCFG_FONTFILE] = ref new AccessListEntry(tbFontFile, cbUseFontFile, ConvertString(PAL_ConfigName(PALCFG_FONTFILE)));
  47. m_acl[PALCFG_LOGFILE] = ref new AccessListEntry(tbLogFile, cbUseLogFile, ConvertString(PAL_ConfigName(PALCFG_LOGFILE)));
  48. tbGitRevision->Text = " " PAL_GIT_REVISION;
  49. LoadControlContents(false);
  50. btnDownloadGame->IsEnabled = (tbGamePath->Text->Length() > 0);
  51. m_resLdr = Windows::ApplicationModel::Resources::ResourceLoader::GetForCurrentView();
  52. if (static_cast<App^>(Application::Current)->LastCrashed)
  53. {
  54. (ref new MessageDialog(m_resLdr->GetString("MBCrashContent")))->ShowAsync();
  55. }
  56. try
  57. {
  58. delete AWait(Windows::Storage::ApplicationData::Current->LocalFolder->GetFileAsync("sdlpal.cfg"));
  59. }
  60. catch (Exception^)
  61. {
  62. (ref new MessageDialog(m_resLdr->GetString("MBStartupMessage"), m_resLdr->GetString("MBStartupTitle")))->ShowAsync();
  63. }
  64. }
  65. void SDLPal::MainPage::LoadControlContents(bool loadDefault)
  66. {
  67. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  68. {
  69. auto item = i->second;
  70. item->text->Text = "";
  71. item->text->Tag = nullptr;
  72. if (item->check)
  73. {
  74. item->check->IsChecked = false;
  75. m_controls->Lookup(item->check->Name)->Object->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  76. }
  77. }
  78. if (!loadDefault)
  79. {
  80. // Always load folder/files from FutureAccessList
  81. std::list<Platform::String^> invalid_tokens;
  82. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  83. for each (auto entry in fal->Entries)
  84. {
  85. auto& ace = m_acl[PAL_ConfigIndex(ConvertString(entry.Token).c_str())];
  86. ace->text->Tag = AWait(fal->GetItemAsync(entry.Token), g_eventHandle);
  87. if (ace->text->Tag)
  88. ace->text->Text = entry.Metadata;
  89. else
  90. invalid_tokens.push_back(entry.Token);
  91. if (ace->check)
  92. {
  93. auto grid = m_controls->Lookup(ace->check->Name)->Object;
  94. ace->check->IsChecked = (ace->text->Tag != nullptr);
  95. grid->Visibility = ace->check->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  96. }
  97. }
  98. for (auto i = invalid_tokens.begin(); i != invalid_tokens.end(); fal->Remove(*i++));
  99. }
  100. tsKeepAspect->IsOn = (gConfig.fKeepAspectRatio == TRUE);
  101. tsStereo->IsOn = (gConfig.iAudioChannels == 2);
  102. tsSurroundOPL->IsOn = (gConfig.fUseSurroundOPL == TRUE);
  103. tsTouchOverlay->IsOn = (gConfig.fUseTouchOverlay == TRUE);
  104. tsEnableAVI->IsOn = (gConfig.fEnableAviPlay == TRUE);
  105. slMusicVolume->Value = gConfig.iMusicVolume;
  106. slSoundVolume->Value = gConfig.iSoundVolume;
  107. slQuality->Value = gConfig.iResampleQuality;
  108. cbLogLevel->SelectedIndex = (int)gConfig.iLogLevel;
  109. cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
  110. cbBGM->SelectedIndex = (gConfig.eMusicType <= MUSIC_OGG) ? gConfig.eMusicType : MUSIC_RIX;
  111. cbOPL->SelectedIndex = (int)gConfig.eOPLType;
  112. if (gConfig.iSampleRate <= 11025)
  113. cbSampleRate->SelectedIndex = 0;
  114. else if (gConfig.iSampleRate <= 22050)
  115. cbSampleRate->SelectedIndex = 1;
  116. else
  117. cbSampleRate->SelectedIndex = 2;
  118. auto wValue = gConfig.wAudioBufferSize >> 10;
  119. unsigned int index = 0;
  120. while (wValue) { index++; wValue >>= 1; }
  121. if (index >= cbAudioBuffer->Items->Size)
  122. cbAudioBuffer->SelectedIndex = cbAudioBuffer->Items->Size - 1;
  123. else
  124. cbAudioBuffer->SelectedIndex = index;
  125. if (gConfig.iOPLSampleRate <= 12429)
  126. cbOPLSR->SelectedIndex = 0;
  127. else if (gConfig.iSampleRate <= 24858)
  128. cbOPLSR->SelectedIndex = 1;
  129. else
  130. cbOPLSR->SelectedIndex = 2;
  131. }
  132. void SDLPal::MainPage::SaveControlContents()
  133. {
  134. // All folders/files are not stored in config file, as they are store in FutureAcessList
  135. if (gConfig.pszGamePath) { free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr; }
  136. if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr; }
  137. if (gConfig.pszFontFile) { free(gConfig.pszFontFile); gConfig.pszFontFile = nullptr; }
  138. if (gConfig.pszLogFile) { free(gConfig.pszLogFile); gConfig.pszLogFile = nullptr; }
  139. gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
  140. gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
  141. gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
  142. gConfig.fUseTouchOverlay = tsTouchOverlay->IsOn ? TRUE : FALSE;
  143. gConfig.fEnableAviPlay = tsEnableAVI->IsOn ? TRUE : FALSE;
  144. gConfig.iMusicVolume = (int)slMusicVolume->Value;
  145. gConfig.iSoundVolume = (int)slSoundVolume->Value;
  146. gConfig.iResampleQuality = (int)slQuality->Value;
  147. gConfig.iLogLevel = (LOGLEVEL)cbLogLevel->SelectedIndex;
  148. gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
  149. gConfig.eMusicType = (MUSICTYPE)cbBGM->SelectedIndex;
  150. gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
  151. gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
  152. gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
  153. gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
  154. }
  155. void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  156. {
  157. auto visibility = (cbBGM->SelectedIndex == MUSIC_RIX) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  158. cbOPL->Visibility = visibility;
  159. cbOPLSR->Visibility = visibility;
  160. tsSurroundOPL->Visibility = visibility;
  161. }
  162. void SDLPal::MainPage::btnDefault_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  163. {
  164. PAL_LoadConfig(FALSE);
  165. LoadControlContents(true);
  166. }
  167. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  168. {
  169. PAL_LoadConfig(TRUE);
  170. LoadControlContents(false);
  171. }
  172. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  173. {
  174. if (tbGamePath->Text->Length() > 0)
  175. {
  176. if (PAL_MISSING_REQUIRED(UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str())))
  177. {
  178. auto msg = std::wstring(m_resLdr->GetString("MBRequired")->Data());
  179. msg.replace(msg.find(L"{0}", 0), 3, tbGamePath->Text->Data());
  180. (ref new MessageDialog(ref new Platform::String(msg.c_str())))->ShowAsync();
  181. tbGamePath->Focus(Windows::UI::Xaml::FocusState::Programmatic);
  182. return;
  183. }
  184. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  185. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  186. {
  187. auto item = i->second;
  188. auto check = item->check ? item->check->IsChecked->Value : true;
  189. if (check && item->text->Tag)
  190. fal->AddOrReplace(item->token, safe_cast<Windows::Storage::IStorageItem^>(item->text->Tag), item->text->Text);
  191. else if (fal->ContainsItem(item->token))
  192. fal->Remove(item->token);
  193. }
  194. SaveControlContents();
  195. gConfig.fLaunchSetting = FALSE;
  196. PAL_SaveConfig();
  197. concurrency::create_task((ref new MessageDialog(m_resLdr->GetString("MBExitContent"), m_resLdr->GetString("MBExitTitle")))->ShowAsync()).then([] (IUICommand^ command) {
  198. Application::Current->Exit();
  199. });
  200. }
  201. else
  202. {
  203. (ref new MessageDialog(m_resLdr->GetString("MBEmptyContent")))->ShowAsync();
  204. }
  205. }
  206. void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  207. {
  208. tbMsgFile->Text = "";
  209. tbMsgFile->Tag = nullptr;
  210. }
  211. void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
  212. {
  213. if (folder)
  214. {
  215. tbGamePath->Text = folder->Path;
  216. tbGamePath->Tag = folder;
  217. btnDownloadGame->IsEnabled = true;
  218. }
  219. }
  220. void SDLPal::MainPage::SetFile(Windows::UI::Xaml::Controls::TextBox^ target, Windows::Storage::StorageFile^ file)
  221. {
  222. if (target && file)
  223. {
  224. target->Text = file->Path;
  225. target->Tag = file;
  226. }
  227. }
  228. void SDLPal::MainPage::btnBrowseFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  229. {
  230. auto picker = ref new Windows::Storage::Pickers::FolderPicker();
  231. picker->FileTypeFilter->Append("*");
  232. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  233. picker->PickFolderAndContinue();
  234. #else
  235. concurrency::create_task(picker->PickSingleFolderAsync()).then([this](Windows::Storage::StorageFolder^ folder) { SetPath(folder); });
  236. #endif
  237. }
  238. void SDLPal::MainPage::btnBrowseFileOpen_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  239. {
  240. auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  241. auto target = m_controls->Lookup(button->Name);
  242. auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
  243. picker->FileTypeFilter->ReplaceAll(target->Filter);
  244. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  245. picker->ContinuationData->Insert("Target", button->Name);
  246. picker->PickSingleFileAndContinue();
  247. #else
  248. concurrency::create_task(picker->PickSingleFileAsync()).then(
  249. [this, target](Windows::Storage::StorageFile^ file) {
  250. SetFile(static_cast<Windows::UI::Xaml::Controls::TextBox^>(target->Object), file);
  251. }
  252. );
  253. #endif
  254. }
  255. void SDLPal::MainPage::btnBrowseFileSave_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  256. {
  257. auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  258. auto target = m_controls->Lookup(button->Name);
  259. auto picker = ref new Windows::Storage::Pickers::FileSavePicker();
  260. picker->FileTypeChoices->Insert(m_resLdr->GetString("LogFileType"), ref new Platform::Collections::Vector<Platform::String^>(target->Filter));
  261. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  262. picker->ContinuationData->Insert("Target", button->Name);
  263. picker->PickSaveFileAndContinue();
  264. #else
  265. concurrency::create_task(picker->PickSaveFileAsync()).then(
  266. [this, target](Windows::Storage::StorageFile^ file) {
  267. SetFile(static_cast<Windows::UI::Xaml::Controls::TextBox^>(target->Object), file);
  268. }
  269. );
  270. #endif
  271. }
  272. void SDLPal::MainPage::cbUseFile_CheckChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  273. {
  274. auto checker = static_cast<Windows::UI::Xaml::Controls::CheckBox^>(sender);
  275. auto attr = m_controls->Lookup(checker->Name);
  276. attr->Object->Visibility = checker->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  277. }
  278. void SDLPal::MainPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  279. {
  280. #if NTDDI_VERSION >= NTDDI_WIN10
  281. if (!Windows::Foundation::Metadata::ApiInformation::IsTypePresent("Windows.UI.ViewManagement.StatusBar")) return;
  282. #endif
  283. #if NTDDI_VERSION >= NTDDI_WIN10 || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  284. auto statusBar = Windows::UI::ViewManagement::StatusBar::GetForCurrentView();
  285. concurrency::create_task(statusBar->ShowAsync()).then([statusBar]() { statusBar->BackgroundOpacity = 1.0; });
  286. #endif
  287. }
  288. void SDLPal::MainPage::btnDownloadGame_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  289. {
  290. auto folder = dynamic_cast<Windows::Storage::StorageFolder^>(tbGamePath->Tag);
  291. auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadMessage"), m_resLdr->GetString("MBDownloadTitle"));
  292. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonOK"), nullptr, 1));
  293. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonCancel"), nullptr, nullptr));
  294. msgbox->DefaultCommandIndex = 0;
  295. msgbox->CancelCommandIndex = 1;
  296. concurrency::create_task(msgbox->ShowAsync()).then([this](IUICommand^ command)->IAsyncOperation<IUICommand^>^ {
  297. if (command->Id != nullptr)
  298. {
  299. if (UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str()) != PALFILE_ALL_ORIGIN)
  300. {
  301. auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadOverwrite"), m_resLdr->GetString("MBDownloadTitle"));
  302. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonYes"), nullptr, 1));
  303. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonNo"), nullptr, nullptr));
  304. msgbox->DefaultCommandIndex = 0;
  305. msgbox->CancelCommandIndex = 1;
  306. return msgbox->ShowAsync();
  307. }
  308. else
  309. {
  310. return concurrency::create_async([command]()->IUICommand^ { return command; });
  311. }
  312. }
  313. else
  314. {
  315. return concurrency::create_async([command]()->IUICommand^ { return command; });
  316. }
  317. }).then([this, folder](IUICommand^ command) {
  318. if (command->Id != nullptr)
  319. {
  320. HANDLE hEvent = CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS);
  321. try
  322. {
  323. auto file = AWait(folder->CreateFileAsync("pal98.zip", Windows::Storage::CreationCollisionOption::ReplaceExisting), hEvent);
  324. auto stream = AWait(file->OpenAsync(Windows::Storage::FileAccessMode::ReadWrite), hEvent);
  325. concurrency::create_task(this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, folder, file, stream]() {
  326. m_dlg = ref new DownloadDialog(m_resLdr, folder, stream, ActualWidth, ActualHeight);
  327. }))).then([this]()->IAsyncOperation<ContentDialogResult>^{
  328. return m_dlg->ShowAsync();
  329. }).then([this, file, stream, hEvent](ContentDialogResult result) {
  330. delete stream;
  331. AWait(file->DeleteAsync(), hEvent);
  332. delete file;
  333. CloseHandle(hEvent);
  334. });
  335. }
  336. catch (Exception^ e)
  337. {
  338. (ref new MessageDialog(String::Concat(m_resLdr->GetString("MBDownloadError"), e)))->ShowAsync();
  339. CloseHandle(hEvent);
  340. }
  341. }
  342. });
  343. }
  344. void SDLPal::MainPage::OnSizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e)
  345. {
  346. if (m_dlg)
  347. {
  348. m_dlg->MaxWidth = e->NewSize.Width;
  349. if (m_dlg->MaxHeight == e->PreviousSize.Height)
  350. m_dlg->MaxHeight = e->NewSize.Height;
  351. m_dlg->UpdateLayout();
  352. }
  353. }