Skip to content

Commit

Permalink
working on database support
Browse files Browse the repository at this point in the history
  • Loading branch information
duchuule committed Jun 18, 2015
1 parent 0fce3a5 commit 4fadb32
Show file tree
Hide file tree
Showing 26 changed files with 1,498 additions and 77 deletions.
7 changes: 7 additions & 0 deletions App.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;

ROMDatabase^ VBA10::App::ROMDB = nullptr;

/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>

App::App()
{
InitializeComponent();
Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
Resuming += ref new EventHandler<Object^>(this, &App::OnResuming);

ROMDB = ref new ROMDatabase();
}

/// <summary>
Expand Down Expand Up @@ -58,6 +64,7 @@ void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEvent
}



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

Expand Down
6 changes: 6 additions & 0 deletions App.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "App.g.h"
#include "DirectXPage.xaml.h"
#include "Database\ROMDatabase.h"

namespace VBA10
{
Expand All @@ -19,9 +20,14 @@ namespace VBA10
App();
virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) override;

internal:
static ROMDatabase^ ROMDB;

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


};
}
124 changes: 70 additions & 54 deletions CheatPane.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,100 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:VBA10"
xmlns:ctl="using:VBA10.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="500"
d:DesignWidth="600">


<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Border Grid.Column="0" BorderThickness="0 0 1 0" BorderBrush="Black" />
<Border Grid.Column="1" BorderThickness="1 0 0 0" BorderBrush="Black" />
<TextBlock Grid.Column="1" Margin="10,20,0,0" Text="Cheat codes" FontSize="22" VerticalAlignment="Top" Height="28" HorizontalAlignment="Left"/>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Grid Grid.Column="0" Margin="10,20,10,10">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" MaxHeight="280"/>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Add new cheat code" Margin="0,0,0,20" FontSize="22" VerticalAlignment="Top" Height="28" HorizontalAlignment="Left"/>
<ctl:PageHeader>
<ctl:PageHeader.HeaderContent>
<TextBlock x:Name="title"
Style="{ThemeResource PageTitleTextBlockStyle}" Text="CHEATS"/>
</ctl:PageHeader.HeaderContent>
</ctl:PageHeader>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<TextBlock Grid.Row="1" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,20" MaxWidth="400"
Text="You can enter Gameshark and CodeBreaker codes. Using cheat codes can make your game unstable." />
<Border Grid.Column="0" BorderThickness="0 0 1 0" BorderBrush="Black" />
<Border Grid.Column="1" BorderThickness="1 0 0 0" BorderBrush="Black" />
<TextBlock Grid.Column="1" Margin="10,20,0,0" Text="Cheat codes" FontSize="22" VerticalAlignment="Top" Height="28" HorizontalAlignment="Left"/>

<Grid Grid.Row="2">
<Grid Grid.Column="0" Margin="10,20,10,10">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*" />
<RowDefinition Height="auto"/>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" MaxHeight="280"/>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" MinWidth="110"/>
<ColumnDefinition Width="*" MaxWidth="300"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Text="Add new cheat code" Margin="0,0,0,20" FontSize="22" VerticalAlignment="Top" Height="28" HorizontalAlignment="Left"/>

<TextBlock Grid.Row="0" Grid.Column="0" FontSize="20" Margin="0,0,0,10"
<TextBlock Grid.Row="1" TextWrapping="Wrap"
FontSize="20"
Margin="0,0,0,20" MaxWidth="400"
x:Name="descLabel"
Text="seecpp" />

<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*" />
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" MinWidth="110"/>
<ColumnDefinition Width="*" MaxWidth="300"/>
</Grid.ColumnDefinitions>

<TextBlock Grid.Row="0" Grid.Column="0" FontSize="20" Margin="0,0,0,10"
Text="Description:" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Grid.Column="0" FontSize="20"
<TextBlock Grid.Row="1" Grid.Column="0" FontSize="20"
Text="Code" VerticalAlignment="Top"/>
<TextBox Grid.Row="0" Grid.Column="1" Name="descBox" Margin="0,0,0,10" />
<TextBox Grid.Row="1" Grid.Column="1" Name="codeBox" AcceptsReturn="True" />
<TextBox Grid.Row="0" Grid.Column="1" Name="descBox" Margin="0,0,0,10" />
<TextBox Grid.Row="1" Grid.Column="1" Name="codeBox" AcceptsReturn="True" />

<Button Grid.Row="3" Content="add" HorizontalAlignment="Stretch"
<Button Grid.Row="3" Content="add" HorizontalAlignment="Stretch"
Margin="0,10,0,0" Name="addButton" Click="addButton_Click" />
</Grid>
</Grid>

</Grid>
</Grid>


<ListBox Grid.Column="1" Name="cheatList" Margin="0,70,0,0"
<ListBox Grid.Column="1" Name="cheatList" Margin="0,70,0,0"
Background="Transparent" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="84" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal">
<CheckBox Name="enableCheatBox" IsChecked="{Binding Enabled}" Margin="0,0,15,0" Checked="enableCheatBox_Checked" Unchecked="enableCheatBox_Checked" />
<StackPanel Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Left">
<TextBlock Foreground="White" Text="{Binding CheatCode}" Width="auto" Height="auto" FontSize="26"/>
<TextBlock Foreground="White" Text="{Binding Description}" Width="auto" Height="auto" FontSize="14" />
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="84" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal">
<CheckBox Name="enableCheatBox" IsChecked="{Binding Enabled}" Margin="0,0,15,0" Checked="enableCheatBox_Checked" Unchecked="enableCheatBox_Checked" />
<StackPanel Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Left">
<TextBlock Foreground="White" Text="{Binding CheatCode}" Width="auto" Height="auto" FontSize="26"/>
<TextBlock Foreground="White" Text="{Binding Description}" Width="auto" Height="auto" FontSize="14" />
</StackPanel>
</StackPanel>
</StackPanel>
<Button Grid.Column="1"
<Button Grid.Column="1"
Tag="Delete" Click="DeleteCheatButton_Click" Name="DeleteCheatButton"
AutomationProperties.AutomationId="DeleteFolderAppBarButton" Content="&#xE107;"
RenderTransformOrigin="1.23,0.758" HorizontalAlignment="Right" Width="84" Height="66" VerticalAlignment="Top"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</Page>
5 changes: 5 additions & 0 deletions CheatPane.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ CheatPane::CheatPane()
if (IsROMLoaded())
{
addButton->IsEnabled = true;
descLabel->Text = "You can enter Gameshark and CodeBreaker codes. Using cheat codes can make your game unstable.";

create_task([this]()
{
return LoadCheats();
Expand Down Expand Up @@ -68,7 +70,10 @@ CheatPane::CheatPane()
});
}
else
{
addButton->IsEnabled = false;
descLabel->Text = "You need to load a ROM first before you can enter cheats.";
}
}


Expand Down
134 changes: 134 additions & 0 deletions Database/Database.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "Database.h"
#include <ppltasks.h>

using namespace SQLiteWinRT;
using namespace Platform;
using namespace concurrency;
using namespace Windows::Storage;
using namespace std;

std::unique_ptr<char[]> PlatformStringToCharArray(Platform::String^ string)
{
auto wideData = string->Data();
int bufferSize = string->Length() + 1;
std::unique_ptr<char[]> ansi(new char[bufferSize]);
if (0 == WideCharToMultiByte(CP_UTF8, 0, wideData, -1, ansi.get(), bufferSize, NULL, NULL))
throw ref new FailureException(L"Can't convert string to UTF8");

return ansi;
}

IAsyncAction^ Database::OpenAsync()
{
return OpenAsync(SqliteOpenMode::Default);
}

IAsyncAction^ Database::OpenAsync(SqliteOpenMode openMode)
{
LONG result = InterlockedCompareExchange(&m_opened, 1, 0);
if (result == 1)
throw ref new Platform::FailureException(L"Database is already open (or is in the process of being opened)");

return create_async([this, openMode]()
{
return create_task([this, openMode]()
{
SimulateSlowOperation();

auto buffer(PlatformStringToCharArray(m_path));
auto actualOpenMode = (openMode == SqliteOpenMode::Default) ? SqliteOpenMode::OpenOrCreateReadWrite : openMode;

auto rc = (SqliteReturnCode)sqlite3_open_v2(buffer.get(), &m_database, (int)actualOpenMode, nullptr);

switch(rc)
{
case SqliteReturnCode::Ok:
break;

case SqliteReturnCode::AccessDenied:
case SqliteReturnCode::AuthorizationDenied:
throw ref new Platform::AccessDeniedException(L"Access denied trying to open database");

case SqliteReturnCode::BadDatabaseFormat:
case SqliteReturnCode::DatabaseCorrupt:
case SqliteReturnCode::DatabaseFull:
case SqliteReturnCode::DatabaseEmpty:
case SqliteReturnCode::DatabaseInUse:
case SqliteReturnCode::DatabaseOpenError:
case SqliteReturnCode::InvalidFileFormat:
throw ref new Platform::Exception(MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, rc), L"Database file is full / corrupt / empty / etc.");

default:
throw ref new Platform::Exception(MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, rc), L"Unknonwn error opening file");
}
});
});
}

IAsyncOperation<Statement^>^ Database::PrepareStatementAsync(String^ cmd)
{
return create_async([this, cmd]()
{
SimulateSlowOperation();
return ref new Statement(this, cmd);
} );
}

IAsyncAction^ Database::ExecuteStatementAsync(String^ cmd)
{
return create_async([this, cmd]()
{
SimulateSlowOperation();

auto utf8cmd = PlatformStringToCharArray(cmd);

auto result = (SqliteReturnCode)sqlite3_exec(m_database, utf8cmd.get(), NULL, NULL, NULL);

Statement::ThrowIfStepFailed(result);
});
}

int64 Database::GetLastInsertedRowId()
{
return sqlite3_last_insert_rowid(m_database);
}

IAsyncOperation<Database^>^ Database::FromApplicationUriAsync(Uri^ path)
{
return create_async([path]()
{
try
{
return create_task(StorageFile::GetFileFromApplicationUriAsync(path))
.then([](StorageFile^ file)
{
SimulateSlowOperation();
return ref new Database(file);
}
);
}
catch (...)
{
throw ref new Platform::InvalidArgumentException("Path invalid");
}
});
}

Database::Database(StorageFile^ file) :
m_path(file->Path)
{
}

Database::Database(StorageFolder^ folder, String^ name) :
m_path(String::Concat(folder->Path, String::Concat(L"\\", name)))
{
}

Database::~Database()
{
if (m_database != nullptr)
{
sqlite3_close(m_database);
m_database = nullptr;
}
}
Loading

0 comments on commit 4fadb32

Please sign in to comment.