Browse Source

[WinRT]: Automatically launch the setting page after crash.

louyihua 8 years ago
parent
commit
b0fa6ba0f0

+ 24 - 19
winrt/SDLPal.Common/MainPage.xaml.cpp

@@ -25,8 +25,14 @@ using namespace Windows::UI::Xaml::Navigation;
 MainPage::MainPage()
 {
 	InitializeComponent();
-	static_cast<App^>(Application::Current)->SetMainPage(this);
 	LoadControlContents();
+
+	auto app = static_cast<App^>(Application::Current);
+#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
+	app->Page = this;
+#endif
+	if (app->LastCrashed)
+		(ref new Windows::UI::Popups::MessageDialog((ref new Windows::ApplicationModel::Resources::ResourceLoader())->GetString("MBCrashContent")))->ShowAsync();
 }
 
 void SDLPal::MainPage::LoadControlContents()
@@ -177,43 +183,42 @@ void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI:
 	tbMsgFile->Tag = nullptr;
 }
 
-#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
 {
-	tbGamePath->Text = folder->Path;
-	tbGamePath->Tag = folder;
+	if (folder)
+	{
+		tbGamePath->Text = folder->Path;
+		tbGamePath->Tag = folder;
+	}
 }
 
 void SDLPal::MainPage::SetFile(Windows::Storage::StorageFile^ file)
 {
-	tbMsgFile->Text = file->Path;
-	tbMsgFile->Tag = file;
+	if (file)
+	{
+		tbMsgFile->Text = file->Path;
+		tbMsgFile->Tag = file;
+	}
 }
 
 void SDLPal::MainPage::btnBrowse_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::btnBrowseFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
 {
 	auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
 	picker->FileTypeFilter->Append("*");
+#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 	picker->PickSingleFileAndContinue();
-}
-
 #else
-
-void SDLPal::MainPage::btnBrowse_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
-{
-	auto picker = ref new Windows::Storage::Pickers::FolderPicker();
-	picker->FileTypeFilter->Append("*");
-	concurrency::create_task(picker->PickSingleFolderAsync()).then([this](Windows::Storage::StorageFolder^ folder) {
-		tbGamePath->Text = folder->Path;
-		tbGamePath->Tag = folder;
-	});
-}
-
+	concurrency::create_task(picker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) { SetFile(file); });
 #endif
+}

+ 0 - 2
winrt/SDLPal.Common/MainPage.xaml.h

@@ -17,10 +17,8 @@ namespace SDLPal
 	public:
 		MainPage();
 
-#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
 		void SetPath(Windows::Storage::StorageFolder^ folder);
 		void SetFile(Windows::Storage::StorageFile^ file);
-#endif
 
 	protected:
 		void LoadControlContents();

+ 8 - 7
winrt/SDLPal.Common/SDLPal.cpp

@@ -6,7 +6,7 @@
 #include "../../global.h"
 #include "App.xaml.h"
 
-HANDLE g_eventHandle;
+HANDLE g_eventHandle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
 
 int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
 {
@@ -14,27 +14,28 @@ int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
 		return 1;
 	}
 
-	g_eventHandle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
-
 	PAL_LoadConfig(TRUE);
 
+	bool last_crashed = false;
+
 	try
 	{
 		// Check the 'running' file to determine whether the program is abnormally terminated last time.
 		// If so, force to launch the setting page.
-		auto file = AWait(Windows::Storage::ApplicationData::Current->LocalCacheFolder->GetFileAsync("running"), g_eventHandle);
-		gConfig.fLaunchSetting = TRUE;
+		auto file = AWait(Windows::Storage::ApplicationData::Current->LocalFolder->GetFileAsync("running"), g_eventHandle);
+		gConfig.fLaunchSetting = TRUE; last_crashed = true;
 		AWait(file->DeleteAsync(), g_eventHandle);
 	}
 	catch(Platform::Exception^)
 	{ }
 
-	if (gConfig.fLaunchSetting)
+	if (gConfig.fLaunchSetting || last_crashed)
 	{
 		Windows::UI::Xaml::Application::Start(
 			ref new Windows::UI::Xaml::ApplicationInitializationCallback(
-				[](Windows::UI::Xaml::ApplicationInitializationCallbackParams^ p) {
+				[last_crashed](Windows::UI::Xaml::ApplicationInitializationCallbackParams^ p) {
 					auto app = ref new SDLPal::App();
+					app->LastCrashed = last_crashed;
 				}
 			)
 		);

+ 3 - 0
winrt/SDLPal.Common/Strings/en/Resources.resw

@@ -192,6 +192,9 @@
   <data name="Language.OnContent" xml:space="preserve">
     <value>Simplified Chinese</value>
   </data>
+  <data name="MBCrashContent" xml:space="preserve">
+    <value>The program is abnormally terminated last time. The setting page has been launched for you. Please check if there are any incorrect settings.</value>
+  </data>
   <data name="MBEmptyContent" xml:space="preserve">
     <value>The game resource folder must be specified!</value>
   </data>

+ 3 - 0
winrt/SDLPal.Common/Strings/zh-hans/Resources.resw

@@ -192,6 +192,9 @@
   <data name="Language.OnContent" xml:space="preserve">
     <value>简体中文</value>
   </data>
+  <data name="MBCrashContent" xml:space="preserve">
+    <value>上次程序异常退出,已显示设置页供您检查设置是否正确。</value>
+  </data>
   <data name="MBEmptyContent" xml:space="preserve">
     <value>必须指定游戏资源文件夹!</value>
   </data>

+ 3 - 0
winrt/SDLPal.Common/Strings/zh-hant/Resources.resw

@@ -192,6 +192,9 @@
   <data name="Language.OnContent" xml:space="preserve">
     <value>簡體中文</value>
   </data>
+  <data name="MBCrashContent" xml:space="preserve">
+    <value>上次程式異常退出,已顯示設定頁供您檢查設定是否正確。</value>
+  </data>
   <data name="MBEmptyContent" xml:space="preserve">
     <value>必須指定遊戲資源檔夾!</value>
   </data>

+ 1 - 1
winrt/SDLPal.Common/WinRTUtil.cpp

@@ -117,7 +117,7 @@ extern "C"
 INT UTIL_Platform_Init(int argc, char* argv[])
 {
 	// Create the 'running' file for crash detection.
-	try { g_running_file = AWait(Windows::Storage::ApplicationData::Current->LocalCacheFolder->CreateFileAsync("running", Windows::Storage::CreationCollisionOption::OpenIfExists), g_eventHandle); }
+	try { g_running_file = AWait(Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync("running", Windows::Storage::CreationCollisionOption::OpenIfExists), g_eventHandle); }
 	catch (Platform::Exception^) {}
 
 	SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");

+ 1 - 3
winrt/SDLPal.UWP/App.xaml.h

@@ -20,11 +20,9 @@ namespace SDLPal
 	internal:
 		App();
 
-		void SetMainPage(Windows::UI::Xaml::Controls::Page^ page) { _main_page = page; }
+		property bool LastCrashed;
 
 	private:
-		Windows::UI::Xaml::Controls::Page^ _main_page;
-
 		void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
 		void OnNavigationFailed(Platform::Object ^sender, Windows::UI::Xaml::Navigation::NavigationFailedEventArgs ^e);
 	};

+ 1 - 3
winrt/SDLPal.Windows/App.xaml.h

@@ -20,11 +20,9 @@ namespace SDLPal
 	internal:
 		App();
 
-		void SetMainPage(Windows::UI::Xaml::Controls::Page^ page) { _main_page = page; }
+		property bool LastCrashed;
 
 	private:
-		Windows::UI::Xaml::Controls::Page^ _main_page;
-
 		void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
 		void OnNavigationFailed(Platform::Object ^sender, Windows::UI::Xaml::Navigation::NavigationFailedEventArgs ^e);
 	};

+ 3 - 4
winrt/SDLPal.WindowsPhone/App.xaml.cpp

@@ -132,14 +132,13 @@ void SDLPal::App::OnActivated(Windows::ApplicationModel::Activation::IActivatedE
 	{
 	case ActivationKind::PickFolderContinuation:
 	{
-		auto folder = safe_cast<IFolderPickerContinuationEventArgs^>(args)->Folder;
-		if (folder) static_cast<MainPage^>(_main_page)->SetPath(folder);
+		static_cast<SDLPal::MainPage^>(Page)->SetPath(safe_cast<IFolderPickerContinuationEventArgs^>(args)->Folder);
 		break;
 	}
 	case ActivationKind::PickFileContinuation:
 	{
-		auto file = safe_cast<IFileOpenPickerContinuationEventArgs^>(args)->Files->First()->Current;
-		if (file) static_cast<MainPage^>(_main_page)->SetFile(file);
+		auto files = safe_cast<IFileOpenPickerContinuationEventArgs^>(args)->Files;
+		if (files->Size > 0) static_cast<SDLPal::MainPage^>(Page)->SetFile(files->GetAt(0));
 		break;
 	}
 	}

+ 4 - 5
winrt/SDLPal.WindowsPhone/App.xaml.h

@@ -14,20 +14,19 @@ namespace SDLPal
 	/// </summary>
 	ref class App sealed
 	{
-	public:
+	internal:
 		App();
 
-		virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) override;
-
-		void SetMainPage(Windows::UI::Xaml::Controls::Page^ page) { _main_page = page; }
+		property Windows::UI::Xaml::Controls::Page^ Page;
+		property bool LastCrashed;
 
 	protected:
+		virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) override;
 		virtual void OnActivated(Windows::ApplicationModel::Activation::IActivatedEventArgs^ args) override;
 
 	private:
 		Windows::UI::Xaml::Media::Animation::TransitionCollection^ _transitions;
 		Windows::Foundation::EventRegistrationToken _firstNavigatedToken;
-		Windows::UI::Xaml::Controls::Page^ _main_page;
 
 		void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
 		void RootFrame_FirstNavigated(Platform::Object^ sender, Windows::UI::Xaml::Navigation::NavigationEventArgs^ e);