Skip to content

Commit

Permalink
prevent duplicate rom entry when import rom from local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
duchuule committed Aug 28, 2015
1 parent 0f28184 commit 2263e67
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 109 deletions.
46 changes: 23 additions & 23 deletions App.xaml.cpp
Expand Up @@ -88,50 +88,50 @@ void App::CheckProductLicense()
/// <param name="e">Details about the launch request and process.</param>
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e)
{
#if _DEBUG
if (IsDebuggerPresent())
{
//DebugSettings->EnableFrameRateCounter = true;
}
#endif
LaunchApp(e->PreviousExecutionState, nullptr);

}


void App::LaunchApp(ApplicationExecutionState previousState,FileActivatedEventArgs^ args)
{
//change minimum suze to 320 px
Windows::Foundation::Size minsize = { 320.0f, 320.0f };
ApplicationView::GetForCurrentView()->SetPreferredMinSize(minsize);

//check license
CheckProductLicense();

App::ROMDB->Initialize().then([this, e] {
App::ROMDB->Initialize().then([this, previousState, args] {
if (m_directXPage == nullptr)
{
m_directXPage = ref new DirectXPage();
}



// Create a AppShell to act as the navigation context and navigate to the first page
//auto shell = ref new AppShell();

//if (shell->AppFrame->Content == nullptr)
//{
// // When the navigation stack isn't restored navigate to the first page,
// // suppressing the initial entrance animation and configuring the new
// // page by passing required information as a navigation parameter
// //shell->AppFrame->Navigate(TypeName(Views::LandingPage::typeid), e->Arguments, ref new Windows::UI::Xaml::Media::Animation::SuppressNavigationTransitionInfo());
//}

if (e->PreviousExecutionState == ApplicationExecutionState::Terminated)
if (previousState == ApplicationExecutionState::Terminated)
{
m_directXPage->LoadInternalState(ApplicationData::Current->LocalSettings->Values);
}

// Place the page in the current window and ensure that it is active.

Window::Current->Content = m_directXPage;
Window::Current->Activate();
});


//import file
if (args != nullptr)
{
m_directXPage->ImportRomFromFile(args);
}
}, task_continuation_context::use_current());
}

void App::OnFileActivated(FileActivatedEventArgs^ args)
{
LaunchApp(args->PreviousExecutionState, args);



}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions App.xaml.h
Expand Up @@ -30,11 +30,16 @@ namespace VBA10
static bool IsPremium;
static void CheckProductLicense();

protected:
virtual void OnFileActivated(Windows::ApplicationModel::Activation::FileActivatedEventArgs^ args) override;

private:
void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
void OnResuming(Platform::Object ^sender, Platform::Object ^args);

void LaunchApp(Windows::ApplicationModel::Activation::ApplicationExecutionState previousState,
Windows::ApplicationModel::Activation::FileActivatedEventArgs^ args);

DirectXPage^ m_directXPage;


Expand Down
89 changes: 46 additions & 43 deletions Database/ROMDatabase.cpp
Expand Up @@ -25,64 +25,67 @@ using namespace VBA10;



ROMDatabase::ROMDatabase()
ROMDatabase::ROMDatabase() : initialized(false)
{
}


task<void> ROMDatabase::Initialize(void)
{

return create_task(Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync(ROMDB_FILE_NAME, CreationCollisionOption::OpenIfExists))
if (initialized)
return create_task([] {});
else
return create_task(Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync(ROMDB_FILE_NAME, CreationCollisionOption::OpenIfExists))
.then([this](StorageFile^ file)
{
db = ref new SQLiteWinRT::Database(file);
{
db = ref new SQLiteWinRT::Database(file);

//open database
return db->OpenAsync(SQLiteWinRT::SqliteOpenMode::OpenReadWrite);
//open database
return db->OpenAsync(SQLiteWinRT::SqliteOpenMode::OpenReadWrite);

}).then([this]
{
//create table if not exist
return db->ExecuteStatementAsync("CREATE TABLE IF NOT EXISTS ROMTABLE ( "\
"LOCATIONTYPE INT NOT NULL,"\
"DISPLAYNAME TEXT NOT NULL,"\
"FILENAME TEXT NOT NULL,"\
"FOLDERPATH TEXT NOT NULL,"\
"TOKEN TEXT NOT NULL,"\
"LASTPLAYED INT NOT NULL,"\
"LASTSAVEINDEX INT NOT NULL, "\
"AUTOSAVEINDEX INT NOT NULL, "\
"SNAPSHOTURI TEXT NOT NULL );");
}).then([this]
{
return db->PrepareStatementAsync("SELECT * FROM ROMTABLE ORDER BY DISPLAYNAME ASC;");
}).then([this]
{
//create table if not exist
return db->ExecuteStatementAsync("CREATE TABLE IF NOT EXISTS ROMTABLE ( "\
"LOCATIONTYPE INT NOT NULL,"\
"DISPLAYNAME TEXT NOT NULL,"\
"FILENAME TEXT NOT NULL,"\
"FOLDERPATH TEXT NOT NULL,"\
"TOKEN TEXT NOT NULL,"\
"LASTPLAYED INT NOT NULL,"\
"LASTSAVEINDEX INT NOT NULL, "\
"AUTOSAVEINDEX INT NOT NULL, "\
"SNAPSHOTURI TEXT NOT NULL );");
}).then([this]
{
return db->PrepareStatementAsync("SELECT * FROM ROMTABLE ORDER BY DISPLAYNAME ASC;");

}).then([this](SQLiteWinRT::Statement^ stmt)
{
statement = stmt;
return RetrieveQuerry();
}).then([this](SQLiteWinRT::Statement^ stmt)
{
statement = stmt;
return RetrieveQuerry();

}).then([this] (Vector<ROMDBEntry^>^ ret)
{
_allROMDBEntries = ret;
}).then([this] (Vector<ROMDBEntry^>^ ret)
{
_allROMDBEntries = ret;

//load rom entry
return LoadSnapshotImage();
}).then([this](task<void> t)
{
//check to see if any entry failed to load (due to deleted folder)
for (int i = 0; i < _allROMDBEntries->Size; i++)
//load rom entry
return LoadSnapshotImage();
}).then([this](task<void> t)
{
auto entry = _allROMDBEntries->GetAt(i);
if (!entry->Folder)
//check to see if any entry failed to load (due to deleted folder)
for (int i = 0; i < _allROMDBEntries->Size; i++)
{
_allROMDBEntries->RemoveAt(i);
i--;
auto entry = _allROMDBEntries->GetAt(i);
if (!entry->Folder)
{
_allROMDBEntries->RemoveAt(i);
i--;
}
}
}
return t.get();
});
initialized = true;
return t.get();
});



Expand Down
1 change: 1 addition & 0 deletions Database/ROMDatabase.h
Expand Up @@ -53,6 +53,7 @@ namespace VBA10
PC::Vector<ROMDBEntry^>^ _allROMDBEntries;
task<void> LoadSnapshotImage();
Platform::String^ ReplaceSingleQuote(Platform::String^ pstr);
bool initialized;

};

Expand Down
2 changes: 1 addition & 1 deletion DirectXPage.xaml
Expand Up @@ -103,7 +103,7 @@
<!-- OnNavigatingToPage we synchronize the selected item in the nav menu with the current page.
OnNavigatedToPage we move keyboard focus to the first item on the page after it's loaded. -->
<SplitView.Content>
<Frame x:Name="frame" Margin="-1,0,0,0">
<Frame x:Name="contentFrame" Margin="-1,0,0,0">
<!--Navigating="OnNavigatingToPage"
Navigated="OnNavigatedToPage"-->

Expand Down
44 changes: 34 additions & 10 deletions DirectXPage.xaml.cpp
Expand Up @@ -50,6 +50,7 @@ using namespace Windows::UI::ViewManagement;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Globalization; //to get date time
using namespace Windows::System::Display;
using namespace Windows::ApplicationModel::Activation;


using namespace std;
Expand All @@ -61,10 +62,6 @@ using namespace VBA10::Controls;

DirectXPage^ DirectXPage::_current;

Frame^ DirectXPage::AppFrame::get()
{
return frame;
}

DirectXPage::DirectXPage():
m_windowVisible(true),
Expand Down Expand Up @@ -229,17 +226,18 @@ DirectXPage::DirectXPage():
CopyDemoROMAsync()
.then([this] //NOTE: this let CopyDemonROM to run on UI thread
{
//open menu, need dispatcher to move it to UI thread, otherwise exception
this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
{
RootSplitView->IsPaneOpen = true;
}));
RootSplitView->IsPaneOpen = true;

});
}, task_continuation_context::use_current());


}


void DirectXPage::OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)
{
}

void DirectXPage::OnHardwareBackButtonPressed(Platform::Object^ sender,
Windows::Phone::UI::Input::BackPressedEventArgs ^args )
{
Expand Down Expand Up @@ -890,4 +888,30 @@ task<void> DirectXPage::UpdateDBEntry()

}

void DirectXPage::ImportRomFromFile(FileActivatedEventArgs^ args)
{
StorageFile^ file = (StorageFile^)(args->Files->GetAt(0));

//calculate folder path and snapshot name
Platform::String ^file_path = file->Path;
wstring wfilepath(file_path->Begin(), file_path->End());

wstring folderpath;
wstring filename;
wstring filenamenoext;
wstring ext;
splitFilePath(wfilepath, folderpath, filename, filenamenoext, ext);

wstring snapshotname = filenamenoext + L".jpg";
Platform::String^ psnapshotname = ref new Platform::String(snapshotname.c_str());
Platform::String^ pfilenamenoext = ref new Platform::String(filenamenoext.c_str());

//calculate token
replace(folderpath.begin(), folderpath.end(), ':', '_');
replace(folderpath.begin(), folderpath.end(), '/', '_');
replace(folderpath.begin(), folderpath.end(), '\\', '_');
Platform::String^ ptoken = ref new Platform::String(folderpath.c_str());



}
29 changes: 19 additions & 10 deletions DirectXPage.xaml.h
Expand Up @@ -37,13 +37,16 @@ namespace VBA10
DirectXPage();
virtual ~DirectXPage();


void LoadInternalState(Windows::Foundation::Collections::IPropertySet^ state);

//from AppShell
property Windows::UI::Xaml::Controls::Frame^ AppFrame
{
Windows::UI::Xaml::Controls::Frame^ get();
Windows::UI::Xaml::Controls::Frame^ get()
{
return this->contentFrame; //contentFrame is defined in xaml
};
}
void GoToPage(int pageindex);

Expand All @@ -52,10 +55,10 @@ namespace VBA10
void LoadState();
void Reset();
void SelectSaveState(int slot);



//from AppShell


//from AppShell
internal:

static property DirectXPage^ Current
Expand All @@ -73,7 +76,12 @@ namespace VBA10
task<void> SaveSnapshot();
task<void> UpdateDBEntry();
task<void> SaveBeforeStop();
void ImportRomFromFile(Windows::ApplicationModel::Activation::FileActivatedEventArgs^ args);


protected:
virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;

private:
//variables
bool loadingDialogOpen;
Expand All @@ -82,7 +90,7 @@ namespace VBA10

//function
task<void> CopyDemoROMAsync(void);


// XAML low-level rendering event handler.
void OnRendering(Platform::Object^ sender, Platform::Object^ args);
Expand Down Expand Up @@ -110,7 +118,7 @@ namespace VBA10
Windows::Foundation::EventRegistrationToken m_eventToken;

//back button
void OnHardwareBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs ^args);
void OnHardwareBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs ^args);

// Independent input handling functions.
void OnPointerPressed(CoreWindow ^window, PointerEventArgs ^args);
Expand All @@ -119,7 +127,7 @@ namespace VBA10

// Resources used to render the DirectX content in the XAML page background.
std::shared_ptr<DX::DeviceResources> m_deviceResources;
std::unique_ptr<VBA10Main> m_main;
std::unique_ptr<VBA10Main> m_main;
bool m_windowVisible;

//from AppShell
Expand All @@ -132,10 +140,11 @@ namespace VBA10

static DirectXPage^ _current;
Windows::System::Display::DisplayRequest^ g_DisplayRequest;



void TogglePaneButton_UnChecked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void TogglePaneButton_Checked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void CloseMenu();
};
};
}

0 comments on commit 2263e67

Please sign in to comment.