MainPage.xaml.cpp 16 KB

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