MainPage.xaml.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. 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::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  168. {
  169. auto visibility = (cbBGM->SelectedIndex == MUSIC_RIX) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  170. cbOPL->Visibility = visibility;
  171. cbOPLSR->Visibility = visibility;
  172. tsSurroundOPL->Visibility = visibility;
  173. }
  174. void SDLPal::MainPage::btnDefault_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  175. {
  176. PAL_LoadConfig(FALSE);
  177. LoadControlContents(true);
  178. }
  179. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  180. {
  181. PAL_LoadConfig(TRUE);
  182. LoadControlContents(false);
  183. }
  184. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  185. {
  186. if (tbGamePath->Text->Length() > 0)
  187. {
  188. if (PAL_MISSING_REQUIRED(UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str())))
  189. {
  190. auto msg = std::wstring(m_resLdr->GetString("MBRequired")->Data());
  191. msg.replace(msg.find(L"{0}", 0), 3, tbGamePath->Text->Data());
  192. (ref new MessageDialog(ref new Platform::String(msg.c_str())))->ShowAsync();
  193. tbGamePath->Focus(Windows::UI::Xaml::FocusState::Programmatic);
  194. return;
  195. }
  196. auto fal = StorageApplicationPermissions::FutureAccessList;
  197. auto mru = StorageApplicationPermissions::MostRecentlyUsedList;
  198. fal->Clear();
  199. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  200. {
  201. auto entry = i->second;
  202. try
  203. {
  204. auto item = AWait(mru->GetItemAsync(entry->token), g_eventHandle);
  205. if ((!entry->check || entry->check->IsChecked->Value) && item)
  206. {
  207. fal->AddOrReplace(entry->token, item, entry->text->Text);
  208. }
  209. }
  210. catch (Exception ^)
  211. {
  212. }
  213. }
  214. mru->Clear();
  215. SaveControlContents();
  216. gConfig.fLaunchSetting = FALSE;
  217. PAL_SaveConfig();
  218. concurrency::create_task((ref new MessageDialog(m_resLdr->GetString("MBExitContent"), m_resLdr->GetString("MBExitTitle")))->ShowAsync()).then([] (IUICommand^ command) {
  219. Application::Current->Exit();
  220. });
  221. }
  222. else
  223. {
  224. (ref new MessageDialog(m_resLdr->GetString("MBEmptyContent")))->ShowAsync();
  225. }
  226. }
  227. void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  228. {
  229. tbMsgFile->Text = "";
  230. }
  231. void SDLPal::MainPage::SetPath(StorageFolder^ folder)
  232. {
  233. if (folder)
  234. {
  235. tbGamePath->Text = folder->Path;
  236. tbGamePath->Tag = folder;
  237. btnDownloadGame->IsEnabled = true;
  238. StorageApplicationPermissions::MostRecentlyUsedList->AddOrReplace(m_acl[PALCFG_GAMEPATH]->token, folder, folder->Path);
  239. StorageApplicationPermissions::MostRecentlyUsedList->AddOrReplace(m_acl[PALCFG_SAVEPATH]->token, folder, folder->Path);
  240. }
  241. }
  242. void SDLPal::MainPage::SetFile(Windows::UI::Xaml::Controls::TextBox^ target, StorageFile^ file)
  243. {
  244. if (target && file)
  245. {
  246. target->Text = file->Path;
  247. StorageApplicationPermissions::MostRecentlyUsedList->AddOrReplace(static_cast<String^>(target->Tag), file, file->Path);
  248. }
  249. }
  250. void SDLPal::MainPage::btnBrowseFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  251. {
  252. auto picker = ref new Pickers::FolderPicker();
  253. picker->FileTypeFilter->Append("*");
  254. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  255. picker->PickFolderAndContinue();
  256. #else
  257. concurrency::create_task(picker->PickSingleFolderAsync()).then([this](StorageFolder^ folder) { SetPath(folder); });
  258. #endif
  259. }
  260. void SDLPal::MainPage::btnBrowseFileOpen_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  261. {
  262. auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  263. auto target = m_controls->Lookup(button->Name);
  264. auto picker = ref new Pickers::FileOpenPicker();
  265. picker->FileTypeFilter->Append("*");
  266. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  267. picker->ContinuationData->Insert("Target", button->Name);
  268. picker->PickSingleFileAndContinue();
  269. #else
  270. concurrency::create_task(picker->PickSingleFileAsync()).then([this, target](StorageFile^ file) { SetFile(static_cast<TextBox^>(target), file); });
  271. #endif
  272. }
  273. void SDLPal::MainPage::btnBrowseFileSave_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  274. {
  275. auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  276. auto target = m_controls->Lookup(button->Name);
  277. auto picker = ref new Pickers::FileSavePicker();
  278. picker->FileTypeChoices->Insert(m_resLdr->GetString("LogFileType"), ref new Vector<String^>(1, ref new String(L".log")));
  279. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  280. picker->ContinuationData->Insert("Target", button->Name);
  281. picker->PickSaveFileAndContinue();
  282. #else
  283. concurrency::create_task(picker->PickSaveFileAsync()).then([this, target](StorageFile^ file) { SetFile(static_cast<TextBox^>(target), file); });
  284. #endif
  285. }
  286. void SDLPal::MainPage::cbUseFile_CheckChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  287. {
  288. auto checker = static_cast<Windows::UI::Xaml::Controls::CheckBox^>(sender);
  289. m_controls->Lookup(checker->Name)->Visibility = checker->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  290. }
  291. void SDLPal::MainPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  292. {
  293. #if NTDDI_VERSION >= NTDDI_WIN10
  294. if (!Windows::Foundation::Metadata::ApiInformation::IsTypePresent("Windows.UI.ViewManagement.StatusBar")) return;
  295. #endif
  296. #if NTDDI_VERSION >= NTDDI_WIN10 || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  297. auto statusBar = Windows::UI::ViewManagement::StatusBar::GetForCurrentView();
  298. concurrency::create_task(statusBar->ShowAsync()).then([statusBar]() { statusBar->BackgroundOpacity = 1.0; });
  299. #endif
  300. }
  301. void SDLPal::MainPage::btnDownloadGame_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  302. {
  303. auto folder = dynamic_cast<StorageFolder^>(tbGamePath->Tag);
  304. auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadMessage"), m_resLdr->GetString("MBDownloadTitle"));
  305. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonOK"), nullptr, 1));
  306. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonCancel"), nullptr, nullptr));
  307. msgbox->DefaultCommandIndex = 0;
  308. msgbox->CancelCommandIndex = 1;
  309. concurrency::create_task(msgbox->ShowAsync()).then([this](IUICommand^ command)->IAsyncOperation<IUICommand^>^ {
  310. if (command->Id != nullptr)
  311. {
  312. if (UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str()) != PALFILE_ALL_ORIGIN)
  313. {
  314. auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadOverwrite"), m_resLdr->GetString("MBDownloadTitle"));
  315. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonYes"), nullptr, 1));
  316. msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonNo"), nullptr, nullptr));
  317. msgbox->DefaultCommandIndex = 0;
  318. msgbox->CancelCommandIndex = 1;
  319. return msgbox->ShowAsync();
  320. }
  321. else
  322. {
  323. return concurrency::create_async([command]()->IUICommand^ { return command; });
  324. }
  325. }
  326. else
  327. {
  328. return concurrency::create_async([command]()->IUICommand^ { return command; });
  329. }
  330. }).then([this, folder](IUICommand^ command) {
  331. if (command->Id != nullptr)
  332. {
  333. try
  334. {
  335. auto file = AWait(folder->CreateFileAsync("pal98.zip", CreationCollisionOption::ReplaceExisting), g_eventHandle);
  336. auto stream = AWait(file->OpenAsync(FileAccessMode::ReadWrite), g_eventHandle);
  337. concurrency::create_task(this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, folder, file, stream]() {
  338. m_dlg = ref new DownloadDialog(m_resLdr, folder, stream, ActualWidth, ActualHeight);
  339. }))).then([this]()->IAsyncOperation<ContentDialogResult>^{
  340. return m_dlg->ShowAsync();
  341. }).then([this, file, stream](ContentDialogResult result) {
  342. delete stream;
  343. AWait(file->DeleteAsync(), g_eventHandle);
  344. delete file;
  345. });
  346. }
  347. catch (Exception^ e)
  348. {
  349. (ref new MessageDialog(String::Concat(m_resLdr->GetString("MBDownloadError"), e)))->ShowAsync();
  350. }
  351. }
  352. });
  353. }
  354. void SDLPal::MainPage::OnSizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e)
  355. {
  356. if (m_dlg)
  357. {
  358. m_dlg->MaxWidth = e->NewSize.Width;
  359. if (m_dlg->MaxHeight == e->PreviousSize.Height)
  360. m_dlg->MaxHeight = e->NewSize.Height;
  361. m_dlg->UpdateLayout();
  362. }
  363. }