123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- //
- // MainPage.xaml.cpp
- // MainPage 类的实现。
- //
- #include "pch.h"
- #include "MainPage.xaml.h"
- #include "DownloadDialog.xaml.h"
- #include "StringHelper.h"
- #include "AsyncHelper.h"
- #include "global.h"
- #include "palcfg.h"
- #include "util.h"
- #include "generated.h"
- using namespace SDLPal;
- using namespace Platform;
- using namespace Windows::Foundation;
- using namespace Windows::Foundation::Collections;
- using namespace Windows::UI::Core;
- using namespace Windows::UI::Popups;
- using namespace Windows::UI::Xaml;
- using namespace Windows::UI::Xaml::Controls;
- using namespace Windows::UI::Xaml::Controls::Primitives;
- using namespace Windows::UI::Xaml::Data;
- using namespace Windows::UI::Xaml::Input;
- using namespace Windows::UI::Xaml::Media;
- using namespace Windows::UI::Xaml::Navigation;
- static Platform::String^ msg_file_exts[] = { ".msg" };
- static Platform::String^ font_file_exts[] = { ".bdf" };
- static Platform::String^ log_file_exts[] = { ".log" };
- MainPage^ MainPage::Current = nullptr;
- MainPage::MainPage()
- : m_dlg(nullptr)
- {
- InitializeComponent();
- Current = this;
- m_controls = ref new Platform::Collections::Map<Platform::String^, ButtonAttribute^>();
- 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]))));
- 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]))));
- 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]))));
- m_controls->Insert(cbUseMsgFile->Name, ref new ButtonAttribute(gridMsgFile, nullptr));
- m_controls->Insert(cbUseFontFile->Name, ref new ButtonAttribute(gridFontFile, nullptr));
- m_controls->Insert(cbUseLogFile->Name, ref new ButtonAttribute(gridLogFile, nullptr));
- m_acl[PALCFG_GAMEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_GAMEPATH)));
- m_acl[PALCFG_SAVEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_SAVEPATH)));
- m_acl[PALCFG_MESSAGEFILE] = ref new AccessListEntry(tbMsgFile, cbUseMsgFile, ConvertString(PAL_ConfigName(PALCFG_MESSAGEFILE)));
- m_acl[PALCFG_FONTFILE] = ref new AccessListEntry(tbFontFile, cbUseFontFile, ConvertString(PAL_ConfigName(PALCFG_FONTFILE)));
- m_acl[PALCFG_LOGFILE] = ref new AccessListEntry(tbLogFile, cbUseLogFile, ConvertString(PAL_ConfigName(PALCFG_LOGFILE)));
- tbGitRevision->Text = " " PAL_GIT_REVISION;
- LoadControlContents(false);
- btnDownloadGame->IsEnabled = (tbGamePath->Text->Length() > 0);
- m_resLdr = Windows::ApplicationModel::Resources::ResourceLoader::GetForCurrentView();
- if (static_cast<App^>(Application::Current)->LastCrashed)
- {
- (ref new MessageDialog(m_resLdr->GetString("MBCrashContent")))->ShowAsync();
- }
- try
- {
- delete AWait(Windows::Storage::ApplicationData::Current->LocalFolder->GetFileAsync("sdlpal.cfg"));
- }
- catch (Exception^)
- {
- (ref new MessageDialog(m_resLdr->GetString("MBStartupMessage"), m_resLdr->GetString("MBStartupTitle")))->ShowAsync();
- }
- }
- void SDLPal::MainPage::LoadControlContents(bool loadDefault)
- {
- for (auto i = m_acl.begin(); i != m_acl.end(); i++)
- {
- auto item = i->second;
- item->text->Text = "";
- item->text->Tag = nullptr;
- if (item->check)
- {
- item->check->IsChecked = false;
- m_controls->Lookup(item->check->Name)->Object->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
- }
- }
- if (!loadDefault)
- {
- // Always load folder/files from FutureAccessList
- std::list<Platform::String^> invalid_tokens;
- auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
- for each (auto entry in fal->Entries)
- {
- auto& ace = m_acl[PAL_ConfigIndex(ConvertString(entry.Token).c_str())];
- ace->text->Tag = AWait(fal->GetItemAsync(entry.Token), g_eventHandle);
- if (ace->text->Tag)
- ace->text->Text = entry.Metadata;
- else
- invalid_tokens.push_back(entry.Token);
- if (ace->check)
- {
- auto grid = m_controls->Lookup(ace->check->Name)->Object;
- ace->check->IsChecked = (ace->text->Tag != nullptr);
- grid->Visibility = ace->check->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
- }
- }
- for (auto i = invalid_tokens.begin(); i != invalid_tokens.end(); fal->Remove(*i++));
- }
- tsKeepAspect->IsOn = (gConfig.fKeepAspectRatio == TRUE);
- tsStereo->IsOn = (gConfig.iAudioChannels == 2);
- tsSurroundOPL->IsOn = (gConfig.fUseSurroundOPL == TRUE);
- tsTouchOverlay->IsOn = (gConfig.fUseTouchOverlay == TRUE);
- tsEnableAVI->IsOn = (gConfig.fEnableAviPlay == TRUE);
- slMusicVolume->Value = gConfig.iMusicVolume;
- slSoundVolume->Value = gConfig.iSoundVolume;
- slQuality->Value = gConfig.iResampleQuality;
- cbLogLevel->SelectedIndex = (int)gConfig.iLogLevel;
- cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
- cbBGM->SelectedIndex = (gConfig.eMusicType <= MUSIC_OGG) ? gConfig.eMusicType : MUSIC_RIX;
- cbOPL->SelectedIndex = (int)gConfig.eOPLType;
- if (gConfig.iSampleRate <= 11025)
- cbSampleRate->SelectedIndex = 0;
- else if (gConfig.iSampleRate <= 22050)
- cbSampleRate->SelectedIndex = 1;
- else
- cbSampleRate->SelectedIndex = 2;
- auto wValue = gConfig.wAudioBufferSize >> 10;
- unsigned int index = 0;
- while (wValue) { index++; wValue >>= 1; }
- if (index >= cbAudioBuffer->Items->Size)
- cbAudioBuffer->SelectedIndex = cbAudioBuffer->Items->Size - 1;
- else
- cbAudioBuffer->SelectedIndex = index;
- if (gConfig.iOPLSampleRate <= 12429)
- cbOPLSR->SelectedIndex = 0;
- else if (gConfig.iSampleRate <= 24858)
- cbOPLSR->SelectedIndex = 1;
- else
- cbOPLSR->SelectedIndex = 2;
- }
- void SDLPal::MainPage::SaveControlContents()
- {
- // All folders/files are not stored in config file, as they are store in FutureAcessList
- if (gConfig.pszGamePath) { free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr; }
- if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr; }
- if (gConfig.pszFontFile) { free(gConfig.pszFontFile); gConfig.pszFontFile = nullptr; }
- if (gConfig.pszLogFile) { free(gConfig.pszLogFile); gConfig.pszLogFile = nullptr; }
- gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
- gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
- gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
- gConfig.fUseTouchOverlay = tsTouchOverlay->IsOn ? TRUE : FALSE;
- gConfig.fEnableAviPlay = tsEnableAVI->IsOn ? TRUE : FALSE;
- gConfig.iMusicVolume = (int)slMusicVolume->Value;
- gConfig.iSoundVolume = (int)slSoundVolume->Value;
- gConfig.iResampleQuality = (int)slQuality->Value;
- gConfig.iLogLevel = (LOGLEVEL)cbLogLevel->SelectedIndex;
- gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
- gConfig.eMusicType = (MUSICTYPE)cbBGM->SelectedIndex;
- gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
- gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
- gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
- gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
- }
- void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
- {
- auto visibility = (cbBGM->SelectedIndex == MUSIC_RIX) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
- cbOPL->Visibility = visibility;
- cbOPLSR->Visibility = visibility;
- tsSurroundOPL->Visibility = visibility;
- }
- void SDLPal::MainPage::btnDefault_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- PAL_LoadConfig(FALSE);
- LoadControlContents(true);
- }
- void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- PAL_LoadConfig(TRUE);
- LoadControlContents(false);
- }
- void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- if (tbGamePath->Text->Length() > 0)
- {
- if (PAL_MISSING_REQUIRED(UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str())))
- {
- auto msg = std::wstring(m_resLdr->GetString("MBRequired")->Data());
- msg.replace(msg.find(L"{0}", 0), 3, tbGamePath->Text->Data());
- (ref new MessageDialog(ref new Platform::String(msg.c_str())))->ShowAsync();
- tbGamePath->Focus(Windows::UI::Xaml::FocusState::Programmatic);
- return;
- }
- auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
- for (auto i = m_acl.begin(); i != m_acl.end(); i++)
- {
- auto item = i->second;
- auto check = item->check ? item->check->IsChecked->Value : true;
- if (check && item->text->Tag)
- fal->AddOrReplace(item->token, safe_cast<Windows::Storage::IStorageItem^>(item->text->Tag), item->text->Text);
- else if (fal->ContainsItem(item->token))
- fal->Remove(item->token);
- }
- SaveControlContents();
- gConfig.fLaunchSetting = FALSE;
- PAL_SaveConfig();
- concurrency::create_task((ref new MessageDialog(m_resLdr->GetString("MBExitContent"), m_resLdr->GetString("MBExitTitle")))->ShowAsync()).then([] (IUICommand^ command) {
- Application::Current->Exit();
- });
- }
- else
- {
- (ref new MessageDialog(m_resLdr->GetString("MBEmptyContent")))->ShowAsync();
- }
- }
- void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- tbMsgFile->Text = "";
- tbMsgFile->Tag = nullptr;
- }
- void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
- {
- if (folder)
- {
- tbGamePath->Text = folder->Path;
- tbGamePath->Tag = folder;
- btnDownloadGame->IsEnabled = true;
- }
- }
- void SDLPal::MainPage::SetFile(Windows::UI::Xaml::Controls::TextBox^ target, Windows::Storage::StorageFile^ file)
- {
- if (target && file)
- {
- target->Text = file->Path;
- target->Tag = file;
- }
- }
- void SDLPal::MainPage::btnBrowseFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- auto picker = ref new Windows::Storage::Pickers::FolderPicker();
- picker->FileTypeFilter->Append("*");
- #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
- picker->PickFolderAndContinue();
- #else
- concurrency::create_task(picker->PickSingleFolderAsync()).then([this](Windows::Storage::StorageFolder^ folder) { SetPath(folder); });
- #endif
- }
- void SDLPal::MainPage::btnBrowseFileOpen_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
- auto target = m_controls->Lookup(button->Name);
- auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
- picker->FileTypeFilter->ReplaceAll(target->Filter);
- #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
- picker->ContinuationData->Insert("Target", button->Name);
- picker->PickSingleFileAndContinue();
- #else
- concurrency::create_task(picker->PickSingleFileAsync()).then(
- [this, target](Windows::Storage::StorageFile^ file) {
- SetFile(static_cast<Windows::UI::Xaml::Controls::TextBox^>(target->Object), file);
- }
- );
- #endif
- }
- void SDLPal::MainPage::btnBrowseFileSave_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
- auto target = m_controls->Lookup(button->Name);
- auto picker = ref new Windows::Storage::Pickers::FileSavePicker();
- picker->FileTypeChoices->Insert(m_resLdr->GetString("LogFileType"), ref new Platform::Collections::Vector<Platform::String^>(target->Filter));
- #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
- picker->ContinuationData->Insert("Target", button->Name);
- picker->PickSaveFileAndContinue();
- #else
- concurrency::create_task(picker->PickSaveFileAsync()).then(
- [this, target](Windows::Storage::StorageFile^ file) {
- SetFile(static_cast<Windows::UI::Xaml::Controls::TextBox^>(target->Object), file);
- }
- );
- #endif
- }
- void SDLPal::MainPage::cbUseFile_CheckChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- auto checker = static_cast<Windows::UI::Xaml::Controls::CheckBox^>(sender);
- auto attr = m_controls->Lookup(checker->Name);
- attr->Object->Visibility = checker->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
- }
- void SDLPal::MainPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- #if NTDDI_VERSION >= NTDDI_WIN10
- if (!Windows::Foundation::Metadata::ApiInformation::IsTypePresent("Windows.UI.ViewManagement.StatusBar")) return;
- #endif
- #if NTDDI_VERSION >= NTDDI_WIN10 || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
- auto statusBar = Windows::UI::ViewManagement::StatusBar::GetForCurrentView();
- concurrency::create_task(statusBar->ShowAsync()).then([statusBar]() { statusBar->BackgroundOpacity = 1.0; });
- #endif
- }
- void SDLPal::MainPage::btnDownloadGame_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
- {
- auto folder = dynamic_cast<Windows::Storage::StorageFolder^>(tbGamePath->Tag);
- auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadMessage"), m_resLdr->GetString("MBDownloadTitle"));
- msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonOK"), nullptr, 1));
- msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonCancel"), nullptr, nullptr));
- msgbox->DefaultCommandIndex = 0;
- msgbox->CancelCommandIndex = 1;
- concurrency::create_task(msgbox->ShowAsync()).then([this](IUICommand^ command)->IAsyncOperation<IUICommand^>^ {
- if (command->Id != nullptr)
- {
- if (UTIL_CheckResourceFiles(ConvertString(tbGamePath->Text).c_str(), ConvertString(tbMsgFile->Text).c_str()) != PALFILE_ALL_ORIGIN)
- {
- auto msgbox = ref new MessageDialog(m_resLdr->GetString("MBDownloadOverwrite"), m_resLdr->GetString("MBDownloadTitle"));
- msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonYes"), nullptr, 1));
- msgbox->Commands->Append(ref new UICommand(m_resLdr->GetString("MBButtonNo"), nullptr, nullptr));
- msgbox->DefaultCommandIndex = 0;
- msgbox->CancelCommandIndex = 1;
- return msgbox->ShowAsync();
- }
- else
- {
- return concurrency::create_async([command]()->IUICommand^ { return command; });
- }
- }
- else
- {
- return concurrency::create_async([command]()->IUICommand^ { return command; });
- }
- }).then([this, folder](IUICommand^ command) {
- if (command->Id != nullptr)
- {
- HANDLE hEvent = CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS);
- try
- {
- auto file = AWait(folder->CreateFileAsync("pal98.zip", Windows::Storage::CreationCollisionOption::ReplaceExisting), hEvent);
- auto stream = AWait(file->OpenAsync(Windows::Storage::FileAccessMode::ReadWrite), hEvent);
- concurrency::create_task(this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, folder, file, stream]() {
- m_dlg = ref new DownloadDialog(m_resLdr, folder, stream, ActualWidth, ActualHeight);
- }))).then([this]()->IAsyncOperation<ContentDialogResult>^{
- return m_dlg->ShowAsync();
- }).then([this, file, stream, hEvent](ContentDialogResult result) {
- delete stream;
- AWait(file->DeleteAsync(), hEvent);
- delete file;
- CloseHandle(hEvent);
- });
- }
- catch (Exception^ e)
- {
- (ref new MessageDialog(String::Concat(m_resLdr->GetString("MBDownloadError"), e)))->ShowAsync();
- CloseHandle(hEvent);
- }
- }
- });
- }
- void SDLPal::MainPage::OnSizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e)
- {
- if (m_dlg)
- {
- m_dlg->MaxWidth = e->NewSize.Width;
- if (m_dlg->MaxHeight == e->PreviousSize.Height)
- m_dlg->MaxHeight = e->NewSize.Height;
- m_dlg->UpdateLayout();
- }
- }
|