MainPage.xaml.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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::Popups;
  19. using namespace Windows::UI::Xaml;
  20. using namespace Windows::UI::Xaml::Controls;
  21. using namespace Windows::UI::Xaml::Controls::Primitives;
  22. using namespace Windows::UI::Xaml::Data;
  23. using namespace Windows::UI::Xaml::Input;
  24. using namespace Windows::UI::Xaml::Media;
  25. using namespace Windows::UI::Xaml::Navigation;
  26. static Platform::String^ msg_file_exts[] = { ".msg" };
  27. static Platform::String^ font_file_exts[] = { ".bdf" };
  28. static Platform::String^ log_file_exts[] = { ".log" };
  29. MainPage^ MainPage::Current = nullptr;
  30. MainPage::MainPage()
  31. {
  32. InitializeComponent();
  33. Current = this;
  34. m_controls = ref new Platform::Collections::Map<Platform::String^, ButtonAttribute^>();
  35. 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]))));
  36. 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]))));
  37. 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]))));
  38. m_controls->Insert(cbUseMsgFile->Name, ref new ButtonAttribute(gridMsgFile, nullptr));
  39. m_controls->Insert(cbUseFontFile->Name, ref new ButtonAttribute(gridFontFile, nullptr));
  40. m_controls->Insert(cbUseLogFile->Name, ref new ButtonAttribute(gridLogFile, nullptr));
  41. m_acl[PALCFG_GAMEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_GAMEPATH)));
  42. m_acl[PALCFG_SAVEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_SAVEPATH)));
  43. m_acl[PALCFG_MESSAGEFILE] = ref new AccessListEntry(tbMsgFile, cbUseMsgFile, ConvertString(PAL_ConfigName(PALCFG_MESSAGEFILE)));
  44. m_acl[PALCFG_FONTFILE] = ref new AccessListEntry(tbFontFile, cbUseFontFile, ConvertString(PAL_ConfigName(PALCFG_FONTFILE)));
  45. m_acl[PALCFG_LOGFILE] = ref new AccessListEntry(tbLogFile, cbUseLogFile, ConvertString(PAL_ConfigName(PALCFG_LOGFILE)));
  46. tbGitRevision->Text = " " PAL_GIT_REVISION;
  47. LoadControlContents(false);
  48. btnDownloadGame->IsEnabled = (tbGamePath->Text->Length() > 0);
  49. RadioButton^ links[] = { rbDownloadLink1, rbDownloadLink2, rbDownloadLink3 };
  50. srand(time(NULL));
  51. links[rand() % 3]->IsChecked = true;
  52. m_resLdr = Windows::ApplicationModel::Resources::ResourceLoader::GetForCurrentView();
  53. if (static_cast<App^>(Application::Current)->LastCrashed)
  54. {
  55. (ref new MessageDialog(m_resLdr->GetString("MBCrashContent")))->ShowAsync();
  56. }
  57. try
  58. {
  59. delete AWait(Windows::Storage::ApplicationData::Current->LocalFolder->GetFileAsync("sdlpal.cfg"));
  60. }
  61. catch (Exception^)
  62. {
  63. (ref new MessageDialog(m_resLdr->GetString("MBStartupMessage"), m_resLdr->GetString("MBStartupTitle")))->ShowAsync();
  64. }
  65. }
  66. void SDLPal::MainPage::LoadControlContents(bool loadDefault)
  67. {
  68. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  69. {
  70. auto item = i->second;
  71. item->text->Text = "";
  72. item->text->Tag = nullptr;
  73. if (item->check)
  74. {
  75. item->check->IsChecked = false;
  76. m_controls->Lookup(item->check->Name)->Object->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  77. }
  78. }
  79. if (!loadDefault)
  80. {
  81. // Always load folder/files from FutureAccessList
  82. std::list<Platform::String^> invalid_tokens;
  83. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  84. for each (auto entry in fal->Entries)
  85. {
  86. auto& ace = m_acl[PAL_ConfigIndex(ConvertString(entry.Token).c_str())];
  87. ace->text->Tag = AWait(fal->GetItemAsync(entry.Token), g_eventHandle);
  88. if (ace->text->Tag)
  89. ace->text->Text = entry.Metadata;
  90. else
  91. invalid_tokens.push_back(entry.Token);
  92. if (ace->check)
  93. {
  94. auto grid = m_controls->Lookup(ace->check->Name)->Object;
  95. ace->check->IsChecked = (ace->text->Tag != nullptr);
  96. grid->Visibility = ace->check->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  97. }
  98. }
  99. for (auto i = invalid_tokens.begin(); i != invalid_tokens.end(); fal->Remove(*i++));
  100. }
  101. tsKeepAspect->IsOn = (gConfig.fKeepAspectRatio == TRUE);
  102. tsStereo->IsOn = (gConfig.iAudioChannels == 2);
  103. tsSurroundOPL->IsOn = (gConfig.fUseSurroundOPL == TRUE);
  104. tsTouchOverlay->IsOn = (gConfig.fUseTouchOverlay == TRUE);
  105. tsEnableAVI->IsOn = (gConfig.fEnableAviPlay == TRUE);
  106. slMusicVolume->Value = gConfig.iMusicVolume;
  107. slSoundVolume->Value = gConfig.iSoundVolume;
  108. slQuality->Value = gConfig.iResampleQuality;
  109. cbLogLevel->SelectedIndex = (int)gConfig.iLogLevel;
  110. cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
  111. cbBGM->SelectedIndex = (gConfig.eMusicType <= MUSIC_OGG) ? gConfig.eMusicType : MUSIC_RIX;
  112. cbOPL->SelectedIndex = (int)gConfig.eOPLType;
  113. if (gConfig.iSampleRate <= 11025)
  114. cbSampleRate->SelectedIndex = 0;
  115. else if (gConfig.iSampleRate <= 22050)
  116. cbSampleRate->SelectedIndex = 1;
  117. else
  118. cbSampleRate->SelectedIndex = 2;
  119. auto wValue = gConfig.wAudioBufferSize >> 10;
  120. unsigned int index = 0;
  121. while (wValue) { index++; wValue >>= 1; }
  122. if (index >= cbAudioBuffer->Items->Size)
  123. cbAudioBuffer->SelectedIndex = cbAudioBuffer->Items->Size - 1;
  124. else
  125. cbAudioBuffer->SelectedIndex = index;
  126. if (gConfig.iOPLSampleRate <= 12429)
  127. cbOPLSR->SelectedIndex = 0;
  128. else if (gConfig.iSampleRate <= 24858)
  129. cbOPLSR->SelectedIndex = 1;
  130. else
  131. cbOPLSR->SelectedIndex = 2;
  132. }
  133. void SDLPal::MainPage::SaveControlContents()
  134. {
  135. // All folders/files are not stored in config file, as they are store in FutureAcessList
  136. if (gConfig.pszGamePath) { free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr; }
  137. if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr; }
  138. if (gConfig.pszFontFile) { free(gConfig.pszFontFile); gConfig.pszFontFile = nullptr; }
  139. if (gConfig.pszLogFile) { free(gConfig.pszLogFile); gConfig.pszLogFile = nullptr; }
  140. gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
  141. gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
  142. gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
  143. gConfig.fUseTouchOverlay = tsTouchOverlay->IsOn ? TRUE : FALSE;
  144. gConfig.fEnableAviPlay = tsEnableAVI->IsOn ? TRUE : FALSE;
  145. gConfig.iMusicVolume = (int)slMusicVolume->Value;
  146. gConfig.iSoundVolume = (int)slSoundVolume->Value;
  147. gConfig.iResampleQuality = (int)slQuality->Value;
  148. gConfig.iLogLevel = (LOGLEVEL)cbLogLevel->SelectedIndex;
  149. gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
  150. gConfig.eMusicType = (MUSICTYPE)cbBGM->SelectedIndex;
  151. gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
  152. gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
  153. gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
  154. gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
  155. }
  156. void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  157. {
  158. auto visibility = (cbBGM->SelectedIndex == MUSIC_RIX) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  159. cbOPL->Visibility = visibility;
  160. cbOPLSR->Visibility = visibility;
  161. tsSurroundOPL->Visibility = visibility;
  162. }
  163. void SDLPal::MainPage::btnDefault_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  164. {
  165. PAL_LoadConfig(FALSE);
  166. LoadControlContents(true);
  167. }
  168. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  169. {
  170. PAL_LoadConfig(TRUE);
  171. LoadControlContents(false);
  172. }
  173. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  174. {
  175. if (tbGamePath->Text->Length() > 0)
  176. {
  177. if (PAL_MISSING_REQUIRED(UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str())))
  178. {
  179. auto msg = std::wstring(m_resLdr->GetString("MBRequired")->Data());
  180. msg.replace(msg.find(L"{0}", 0), 3, tbGamePath->Text->Data());
  181. (ref new MessageDialog(ref new Platform::String(msg.c_str())))->ShowAsync();
  182. tbGamePath->Focus(Windows::UI::Xaml::FocusState::Programmatic);
  183. return;
  184. }
  185. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  186. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  187. {
  188. auto item = i->second;
  189. auto check = item->check ? item->check->IsChecked->Value : true;
  190. if (check && item->text->Tag)
  191. fal->AddOrReplace(item->token, safe_cast<Windows::Storage::IStorageItem^>(item->text->Tag), item->text->Text);
  192. else if (fal->ContainsItem(item->token))
  193. fal->Remove(item->token);
  194. }
  195. SaveControlContents();
  196. gConfig.fLaunchSetting = FALSE;
  197. PAL_SaveConfig();
  198. concurrency::create_task((ref new MessageDialog(m_resLdr->GetString("MBExitContent"), m_resLdr->GetString("MBExitTitle")))->ShowAsync()).then([] (IUICommand^ command) {
  199. Application::Current->Exit();
  200. });
  201. }
  202. else
  203. {
  204. (ref new MessageDialog(m_resLdr->GetString("MBEmptyContent")))->ShowAsync();
  205. }
  206. }
  207. void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  208. {
  209. tbMsgFile->Text = "";
  210. tbMsgFile->Tag = nullptr;
  211. }
  212. void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
  213. {
  214. if (folder)
  215. {
  216. tbGamePath->Text = folder->Path;
  217. tbGamePath->Tag = folder;
  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. Platform::String^ link = nullptr;
  291. Windows::UI::Xaml::Controls::RadioButton^ controls[] = {
  292. this->rbDownloadLink1, this->rbDownloadLink2, this->rbDownloadLink3
  293. };
  294. for (int i = 0; i < 3; i++)
  295. {
  296. if (controls[i]->IsChecked->Value)
  297. {
  298. link = static_cast<Platform::String^>(controls[i]->Tag);
  299. break;
  300. }
  301. }
  302. auto folder = dynamic_cast<Windows::Storage::StorageFolder^>(tbGamePath->Tag);
  303. auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadMessage"), m_resLdr->GetString("MBDownloadTitle"));
  304. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonOK"), nullptr, 1));
  305. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonCancel"), nullptr, nullptr));
  306. msgbox->DefaultCommandIndex = 0;
  307. msgbox->CancelCommandIndex = 1;
  308. concurrency::create_task(msgbox->ShowAsync()).then([this](IUICommand^ command)->IAsyncOperation<IUICommand^>^ {
  309. if (command->Id != nullptr)
  310. {
  311. if (UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str()) != PALFILE_ALL_ORIGIN)
  312. {
  313. auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadOverwrite"), m_resLdr->GetString("MBDownloadTitle"));
  314. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonYes"), nullptr, 1));
  315. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonNo"), nullptr, nullptr));
  316. msgbox->DefaultCommandIndex = 0;
  317. msgbox->CancelCommandIndex = 1;
  318. return msgbox->ShowAsync();
  319. }
  320. else
  321. {
  322. return concurrency::create_async([command]()->IUICommand^ { return command; });
  323. }
  324. }
  325. else
  326. {
  327. return concurrency::create_async([command]()->IUICommand^ { return command; });
  328. }
  329. }).then([this, folder, link](IUICommand^ command) {
  330. if (command->Id != nullptr)
  331. {
  332. HANDLE hEvent = CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS);
  333. try
  334. {
  335. auto file = AWait(folder->CreateFileAsync("pal98.zip", Windows::Storage::CreationCollisionOption::ReplaceExisting), hEvent);
  336. auto stream = AWait(file->OpenAsync(Windows::Storage::FileAccessMode::ReadWrite), hEvent);
  337. concurrency::create_task((ref new DownloadDialog(link, m_resLdr, folder, stream))->ShowAsync()).then(
  338. [this, file, stream, hEvent](ContentDialogResult result) {
  339. delete stream;
  340. AWait(file->DeleteAsync(), hEvent);
  341. delete file;
  342. CloseHandle(hEvent);
  343. });
  344. }
  345. catch (Exception^ e)
  346. {
  347. (ref new MessageDialog(String::Concat(m_resLdr->GetString("MBDownloadError"), e)))->ShowAsync();
  348. CloseHandle(hEvent);
  349. }
  350. }
  351. });
  352. }