MainPage.xaml.cpp 15 KB

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