in game load wallet function

This commit is contained in:
Crypto City 2019-08-06 15:02:34 +00:00
parent 28c4b17382
commit ee9cffc0a7
5 changed files with 109 additions and 55 deletions

View File

@ -8,7 +8,7 @@
#include <Urho3D/UI/Button.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/UIEvents.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/UI/MessageBox.h>
#include "wipeable_string.h"
#include "crypto/crypto.h"
#include "cryptonote_basic/cryptonote_basic.h"
@ -57,31 +57,38 @@ public:
}
if (load)
{
std::unique_ptr<tools::wallet2> w = std::move(tools::wallet2::make_from_file(*vm, false, wallet_file, [this](const char *prompt, bool verify) -> boost::optional<tools::password_container> {
MDEBUG("Need password");
boost::unique_lock<boost::mutex> lock(mutex);
windowVisible_ = WindowShow;
windowTitleText_ = prompt;
pass_req = true;
pass_req_cv.notify_one();
pass_res = false;
pass_res_cv.wait(lock);
windowVisible_ = WindowHide;
if (!pass_res)
return boost::none;
return tools::password_container(password);
}).first);
try
{
std::unique_ptr<tools::wallet2> w = std::move(tools::wallet2::make_from_file(*vm, false, wallet_file, [this](const char *prompt, bool verify) -> boost::optional<tools::password_container> {
MDEBUG("Need password");
boost::unique_lock<boost::mutex> lock(mutex);
windowVisible_ = WindowShow;
windowTitleText_ = prompt;
pass_req = true;
pass_req_cv.notify_one();
pass_res = false;
pass_res_cv.wait(lock);
windowVisible_ = WindowHide;
if (!pass_res)
return boost::none;
return tools::password_container(password);
}).first);
if (w)
{
MDEBUG("Load success");
boost::unique_lock<boost::mutex> lock(mutex);
on_loaded(std::shared_ptr<tools::wallet2>(w.release()), password);
if (w)
{
MDEBUG("Load success");
boost::unique_lock<boost::mutex> lock(mutex);
on_loaded(std::shared_ptr<tools::wallet2>(w.release()), password);
}
else
{
MERROR("Load failure");
on_loaded(nullptr, {});
}
}
else
catch (const std::exception &e)
{
MERROR("Load failure");
boost::unique_lock<boost::mutex> lock(mutex);
MERROR("Load exception");
on_loaded(nullptr, {});
}
}
@ -253,9 +260,11 @@ void WalletLoader::UpdateVisibility()
window_->SetVisible(true);
lineEdit_->SetFocus(true);
windowTitle_->SetText(windowTitleText_.c_str());
window_->SetModal(true);
windowVisible_ = WindowAsIs;
break;
case WindowHide:
window_->SetModal(false);
window_->SetEnabled(false);
window_->SetVisible(false);
windowVisible_ = WindowAsIs;
@ -277,6 +286,7 @@ public:
void refresh();
void update();
bool cancel_ui() { return loader.CancelUI(); }
void load(const char *wallet_file);
std::shared_ptr<tools::wallet2> wallet() { return w; }
@ -308,7 +318,7 @@ private:
GameWalletInternal::GameWalletInternal(Context *ctx, int argc, const char **argv):
Object(ctx),
loader(ctx),
walletChanged(true)
walletChanged(false)
{
boost::program_options::options_description desc_params(wallet_args::tr("Wallet options"));
tools::wallet2::init_options(desc_params);
@ -336,26 +346,7 @@ GameWalletInternal::GameWalletInternal(Context *ctx, int argc, const char **argv
loader.Run();
const std::string wallet_file = command_line::get_arg(*vm, wallet_args::arg_wallet_file());
if (!wallet_file.empty())
{
bool keys_file_exists, wallet_file_exists;
tools::wallet2::wallet_exists(wallet_file, keys_file_exists, wallet_file_exists);
if (keys_file_exists)
{
loader.Load(*vm, wallet_file, [this](std::shared_ptr<tools::wallet2> w, const epee::wipeable_string &password) {
boost::unique_lock<boost::mutex> lock(mutex);
this->w = w;
this->password = password;
if (w)
configure_wallet();
walletChanged = true;
});
}
else
{
URHO3D_LOGERRORF("Wallet keys file not found: %s", wallet_file.c_str());
}
}
load(wallet_file.c_str());
}
GameWalletInternal::~GameWalletInternal()
@ -393,6 +384,8 @@ void GameWalletInternal::update()
lock.unlock();
VariantMap eventData;
SendEvent(E_WALLET_NEW_WALLET, eventData);
if (!w)
new MessageBox(context_, "Error loading wallet");
lock.lock();
}
@ -408,6 +401,30 @@ void GameWalletInternal::update()
}
}
void GameWalletInternal::load(const char *wallet_file)
{
if (wallet_file && *wallet_file)
{
bool keys_file_exists, wallet_file_exists;
tools::wallet2::wallet_exists(wallet_file, keys_file_exists, wallet_file_exists);
if (keys_file_exists)
{
loader.Load(*vm, wallet_file, [this](std::shared_ptr<tools::wallet2> w, const epee::wipeable_string &password) {
boost::unique_lock<boost::mutex> lock(mutex);
this->w = w;
this->password = password;
if (w)
configure_wallet();
walletChanged = true;
});
}
else
{
new MessageBox(context_, String("Wallet keys file not found: %s") + wallet_file);
}
}
}
void GameWalletInternal::on_new_block(uint64_t height, const cryptonote::block& block, const std::vector<cryptonote::transaction> &transactions)
{
MDEBUG("New block");
@ -476,6 +493,11 @@ bool GameWallet::cancel_ui()
return internal->cancel_ui();
}
void GameWallet::load(const char *wallet_file)
{
return internal->load(wallet_file);
}
std::shared_ptr<tools::wallet2> GameWallet::wallet()
{
return internal->wallet();

View File

@ -26,6 +26,7 @@ public:
void refresh();
void update();
bool cancel_ui();
void load(const char *wallet_file);
std::shared_ptr<tools::wallet2> wallet();

View File

@ -19,6 +19,8 @@
#include <Urho3D/Engine/Console.h>
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Engine/DebugHud.h>
#include <Urho3D/UI/FileSelector.h>
#include <Urho3D/UI/UIEvents.h>
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "citymesh-urho3d.h"
#include "ui-urho3d.h"
@ -83,13 +85,14 @@ public:
void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
void HandleMouseMotion(StringHash eventType, VariantMap& eventData);
void HandleNextPlayer(StringHash eventType, VariantMap& eventData);
void HandleLoadWallet(StringHash eventType, VariantMap& eventData);
void HandleBuy(StringHash eventType, VariantMap& eventData);
void HandleBuildOne(StringHash eventType, VariantMap& eventData);
void HandleDestroyOne(StringHash eventType, VariantMap& eventData);
void HandleDisplayNormal(StringHash eventType, VariantMap& eventData);
void HandleDisplayLand(StringHash eventType, VariantMap& eventData);
void HandleMaterialSelected(StringHash eventType, VariantMap& eventData);
void HandleWalletFileSelected(StringHash eventType, VariantMap& eventData);
void HandleNewWallet(StringHash eventType, VariantMap& eventData);
void HandleNewBlock(StringHash eventType, VariantMap& eventData);
@ -144,6 +147,7 @@ private:
CityState cityState;
PlayerState playerState;
int currentMaterial;
SharedPtr<FileSelector> fileSelector;
std::shared_ptr<GameWallet> wallet;
WalletRefresher walletRefresher_;
@ -305,7 +309,7 @@ void CryptoCityUrho3D::SetupUI()
cursor->SetStyleAuto(style);
GetSubsystem<Urho3D::UI>()->SetCursor(cursor);
ui = new UIUrho3D(context_);
SubscribeToEvent(ui, E_CRYPTOCITY_NEXT_PLAYER, URHO3D_HANDLER(CryptoCityUrho3D, HandleNextPlayer));
SubscribeToEvent(ui, E_CRYPTOCITY_LOAD_WALLET, URHO3D_HANDLER(CryptoCityUrho3D, HandleLoadWallet));
SubscribeToEvent(ui, E_CRYPTOCITY_BUY, URHO3D_HANDLER(CryptoCityUrho3D, HandleBuy));
SubscribeToEvent(ui, E_CRYPTOCITY_BUILD_ONE, URHO3D_HANDLER(CryptoCityUrho3D, HandleBuildOne));
SubscribeToEvent(ui, E_CRYPTOCITY_DESTROY_ONE, URHO3D_HANDLER(CryptoCityUrho3D, HandleDestroyOne));
@ -698,9 +702,24 @@ void CryptoCityUrho3D::HandleSceneUpdate(StringHash /*eventType*/, VariantMap& e
}
}
void CryptoCityUrho3D::HandleNextPlayer(StringHash eventType, VariantMap& eventData)
void CryptoCityUrho3D::HandleLoadWallet(StringHash eventType, VariantMap& eventData)
{
printf("Obsolete\n");
printf("LOAD WALLET!\n");
auto root = context_->GetSubsystem<UI>()->GetRoot();
auto* cache = context_->GetSubsystem<ResourceCache>();
auto* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
auto* graphics = GetSubsystem<Graphics>();
XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
fileSelector = new FileSelector(context_);
fileSelector->SetTitle("Select wallet to load");
fileSelector->SetButtonTexts("Load", "Cancel");
fileSelector->SetFilters(Vector<String>(1, "*.keys"), 0);
fileSelector->SetDefaultStyle(xmlFile);
const IntVector2 size = fileSelector->GetWindow()->GetSize();
fileSelector->GetWindow()->SetPosition((root->GetWidth() - size.x_) / 2, (root->GetHeight() - size.y_) / 2);
SubscribeToEvent(fileSelector, E_FILESELECTED, URHO3D_HANDLER(CryptoCityUrho3D, HandleWalletFileSelected));
}
void CryptoCityUrho3D::HandleBuy(StringHash eventType, VariantMap& eventData)
@ -786,6 +805,18 @@ void CryptoCityUrho3D::HandleMaterialSelected(StringHash eventType, VariantMap&
currentMaterial = material;
}
void CryptoCityUrho3D::HandleWalletFileSelected(StringHash eventType, VariantMap& eventData)
{
const bool ok = eventData[FileSelected::P_OK].GetBool();
const String filename = eventData[FileSelected::P_FILENAME].GetString();
fileSelector = NULL;
if (ok)
{
wallet->load(filename.CString());
}
}
void CryptoCityUrho3D::HandleNewWallet(StringHash eventType, VariantMap& eventData)
{
MINFO("New wallet, rebuilding player state");

View File

@ -161,10 +161,10 @@ void UIUrho3D::CreatePlayerWindow()
indexToMaterial[i] = i;
}
nextPlayerButton = AddButton(playerWindow, "Next player");
loadWalletButton = AddButton(playerWindow, "Load wallet");
SubscribeToEvent(playerMaterialList, E_ITEMSELECTED, URHO3D_HANDLER(UIUrho3D, HandleMaterialSelected));
SubscribeToEvent(nextPlayerButton, E_RELEASED, URHO3D_HANDLER(UIUrho3D, HandleNextPlayer));
SubscribeToEvent(loadWalletButton, E_RELEASED, URHO3D_HANDLER(UIUrho3D, HandleLoadWallet));
playerWindow->SetPosition(0, selectionWindow->GetHeight());
}
@ -290,10 +290,10 @@ void UIUrho3D::UpdatePlayer(const PlayerState *player)
indexToMaterial[idx++] = -1;
}
void UIUrho3D::HandleNextPlayer(StringHash eventType, VariantMap& eventData)
void UIUrho3D::HandleLoadWallet(StringHash eventType, VariantMap& eventData)
{
VariantMap noEventData;
SendEvent(E_CRYPTOCITY_NEXT_PLAYER, noEventData);
SendEvent(E_CRYPTOCITY_LOAD_WALLET, noEventData);
}
void UIUrho3D::HandleBuy(StringHash eventType, VariantMap& eventData)

View File

@ -12,7 +12,7 @@
#include "game-state.h"
#include "map.h"
URHO3D_EVENT(E_CRYPTOCITY_NEXT_PLAYER, NextPlayer) {}
URHO3D_EVENT(E_CRYPTOCITY_LOAD_WALLET, LoadWallet) {}
URHO3D_EVENT(E_CRYPTOCITY_BUY, Buy) {}
URHO3D_EVENT(E_CRYPTOCITY_BUILD_ONE, BuildOne) {}
URHO3D_EVENT(E_CRYPTOCITY_DESTROY_ONE, DestroyOne) {}
@ -36,7 +36,7 @@ private:
void UpdateSelection(const Map *map, const CityState *city, int mouse_x, int mouse_y, uint32_t sx0, uint32_t sy0, uint32_t sx1, uint32_t sy1);
void UpdatePlayer(const PlayerState *player);
void HandleNextPlayer(Urho3D::StringHash eventType, Urho3D::VariantMap& eventData);
void HandleLoadWallet(Urho3D::StringHash eventType, Urho3D::VariantMap& eventData);
void HandleBuy(Urho3D::StringHash eventType, Urho3D::VariantMap& eventData);
void HandleBuildOne(Urho3D::StringHash eventType, Urho3D::VariantMap& eventData);
void HandleDestroyOne(Urho3D::StringHash eventType, Urho3D::VariantMap& eventData);
@ -60,7 +60,7 @@ private:
Urho3D::SharedPtr<Urho3D::ListView> playerMaterialList;
Urho3D::SharedPtr<Urho3D::UIElement> playerMaterialItem[256];
Urho3D::SharedPtr<Urho3D::Text> playerMaterialText[256];
Urho3D::SharedPtr<Urho3D::Button> nextPlayerButton;
Urho3D::SharedPtr<Urho3D::Button> loadWalletButton;
int indexToMaterial[256];
Urho3D::SharedPtr<ShadableWindow> displayWindow;