Skip to content

Commit

Permalink
Extract cloud APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
hokein committed Sep 2, 2016
1 parent d3e29a7 commit 7dfbd5f
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 140 deletions.
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
'target_name': '<(project_name)',
'sources': [
'src/api/steam_api_auth.cc',
'src/api/steam_api_cloud.cc',
'src/api/steam_api_friends.cc',
'src/api/steam_api_registry.h',
'src/greenworks_api.cc',
Expand Down
163 changes: 163 additions & 0 deletions src/api/steam_api_cloud.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright (c) 2016 Greenheart Games Pty. Ltd. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include <string>

#include "nan.h"
#include "v8.h"

#include "greenworks_async_workers.h"
#include "steam/steam_api.h"
#include "steam_api_registry.h"

namespace greenworks {
namespace api {
namespace {

NAN_METHOD(SaveTextToFile) {
Nan::HandleScope scope;

if (info.Length() < 3 || !info[0]->IsString() || !info[1]->IsString() ||
!info[2]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}

std::string file_name(*(v8::String::Utf8Value(info[0])));
std::string content(*(v8::String::Utf8Value(info[1])));
Nan::Callback* success_callback =
new Nan::Callback(info[2].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 3 && info[3]->IsFunction())
error_callback = new Nan::Callback(info[3].As<v8::Function>());

Nan::AsyncQueueWorker(new greenworks::FileContentSaveWorker(success_callback,
error_callback,
file_name,
content));
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(SaveFilesToCloud) {
Nan::HandleScope scope;
if (info.Length() < 2 || !info[0]->IsArray() || !info[1]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}
v8::Local<v8::Array> files = info[0].As<v8::Array>();
std::vector<std::string> files_path;
for (uint32_t i = 0; i < files->Length(); ++i) {
if (!files->Get(i)->IsString())
THROW_BAD_ARGS("Bad arguments");
v8::String::Utf8Value string_array(files->Get(i));
// Ignore empty path.
if (string_array.length() > 0)
files_path.push_back(*string_array);
}

Nan::Callback* success_callback =
new Nan::Callback(info[1].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 2 && info[2]->IsFunction())
error_callback = new Nan::Callback(info[2].As<v8::Function>());
Nan::AsyncQueueWorker(new greenworks::FilesSaveWorker(success_callback,
error_callback,
files_path));
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(ReadTextFromFile) {
Nan::HandleScope scope;

if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}

std::string file_name(*(v8::String::Utf8Value(info[0])));
Nan::Callback* success_callback =
new Nan::Callback(info[1].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 2 && info[2]->IsFunction())
error_callback = new Nan::Callback(info[2].As<v8::Function>());

Nan::AsyncQueueWorker(new greenworks::FileReadWorker(success_callback,
error_callback,
file_name));
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(IsCloudEnabled) {
Nan::HandleScope scope;
ISteamRemoteStorage* steam_remote_storage = SteamRemoteStorage();
info.GetReturnValue().Set(Nan::New<v8::Boolean>(
steam_remote_storage->IsCloudEnabledForApp()));
}

NAN_METHOD(IsCloudEnabledForUser) {
Nan::HandleScope scope;
ISteamRemoteStorage* steam_remote_storage = SteamRemoteStorage();
info.GetReturnValue().Set(Nan::New<v8::Boolean>(
steam_remote_storage->IsCloudEnabledForAccount()));
}

NAN_METHOD(EnableCloud) {
Nan::HandleScope scope;

if (info.Length() < 1) {
THROW_BAD_ARGS("Bad arguments");
}
bool enable_flag = info[0]->BooleanValue();
SteamRemoteStorage()->SetCloudEnabledForApp(enable_flag);
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(GetCloudQuota) {
Nan::HandleScope scope;

if (info.Length() < 1 || !info[0]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}
Nan::Callback* success_callback =
new Nan::Callback(info[0].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 2 && info[1]->IsFunction())
error_callback = new Nan::Callback(info[1].As<v8::Function>());

Nan::AsyncQueueWorker(new greenworks::CloudQuotaGetWorker(success_callback,
error_callback));
info.GetReturnValue().Set(Nan::Undefined());
}

void RegisterAPIs(v8::Handle<v8::Object> target) {
Nan::Set(target,
Nan::New("saveTextToFile").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(SaveTextToFile)->GetFunction());
Nan::Set(target,
Nan::New("readTextFromFile").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ReadTextFromFile)->GetFunction());
Nan::Set(target,
Nan::New("saveFilesToCloud").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(SaveFilesToCloud)->GetFunction());
Nan::Set(target,
Nan::New("isCloudEnabled").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(IsCloudEnabled)->GetFunction());
Nan::Set(target,
Nan::New("isCloudEnabledForUser").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(
IsCloudEnabledForUser)->GetFunction());
Nan::Set(target,
Nan::New("enableCloud").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(EnableCloud)->GetFunction());
Nan::Set(target,
Nan::New("getCloudQuota").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(GetCloudQuota)->GetFunction());
}

SteamAPIRegistry::Add X(RegisterAPIs);

} // namespace
} // namespace api
} // namespace greenworks
140 changes: 0 additions & 140 deletions src/greenworks_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,122 +206,6 @@ NAN_METHOD(GetImageRGBA) {
.ToLocalChecked());
}

NAN_METHOD(SaveTextToFile) {
Nan::HandleScope scope;

if (info.Length() < 3 || !info[0]->IsString() || !info[1]->IsString() ||
!info[2]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}

std::string file_name(*(v8::String::Utf8Value(info[0])));
std::string content(*(v8::String::Utf8Value(info[1])));
Nan::Callback* success_callback =
new Nan::Callback(info[2].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 3 && info[3]->IsFunction())
error_callback = new Nan::Callback(info[3].As<v8::Function>());

Nan::AsyncQueueWorker(new greenworks::FileContentSaveWorker(success_callback,
error_callback,
file_name,
content));
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(SaveFilesToCloud) {
Nan::HandleScope scope;
if (info.Length() < 2 || !info[0]->IsArray() || !info[1]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}
v8::Local<v8::Array> files = info[0].As<v8::Array>();
std::vector<std::string> files_path;
for (uint32_t i = 0; i < files->Length(); ++i) {
if (!files->Get(i)->IsString())
THROW_BAD_ARGS("Bad arguments");
v8::String::Utf8Value string_array(files->Get(i));
// Ignore empty path.
if (string_array.length() > 0)
files_path.push_back(*string_array);
}

Nan::Callback* success_callback =
new Nan::Callback(info[1].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 2 && info[2]->IsFunction())
error_callback = new Nan::Callback(info[2].As<v8::Function>());
Nan::AsyncQueueWorker(new greenworks::FilesSaveWorker(success_callback,
error_callback,
files_path));
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(ReadTextFromFile) {
Nan::HandleScope scope;

if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}

std::string file_name(*(v8::String::Utf8Value(info[0])));
Nan::Callback* success_callback =
new Nan::Callback(info[1].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 2 && info[2]->IsFunction())
error_callback = new Nan::Callback(info[2].As<v8::Function>());

Nan::AsyncQueueWorker(new greenworks::FileReadWorker(success_callback,
error_callback,
file_name));
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(IsCloudEnabled) {
Nan::HandleScope scope;
ISteamRemoteStorage* steam_remote_storage = SteamRemoteStorage();
info.GetReturnValue().Set(Nan::New<v8::Boolean>(
steam_remote_storage->IsCloudEnabledForApp()));
}

NAN_METHOD(IsCloudEnabledForUser) {
Nan::HandleScope scope;
ISteamRemoteStorage* steam_remote_storage = SteamRemoteStorage();
info.GetReturnValue().Set(Nan::New<v8::Boolean>(
steam_remote_storage->IsCloudEnabledForAccount()));
}

NAN_METHOD(EnableCloud) {
Nan::HandleScope scope;

if (info.Length() < 1) {
THROW_BAD_ARGS("Bad arguments");
}
bool enable_flag = info[0]->BooleanValue();
SteamRemoteStorage()->SetCloudEnabledForApp(enable_flag);
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(GetCloudQuota) {
Nan::HandleScope scope;

if (info.Length() < 1 || !info[0]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments");
}
Nan::Callback* success_callback =
new Nan::Callback(info[0].As<v8::Function>());
Nan::Callback* error_callback = NULL;

if (info.Length() > 2 && info[1]->IsFunction())
error_callback = new Nan::Callback(info[1].As<v8::Function>());

Nan::AsyncQueueWorker(new greenworks::CloudQuotaGetWorker(success_callback,
error_callback));
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(ActivateAchievement) {
Nan::HandleScope scope;

Expand Down Expand Up @@ -761,30 +645,6 @@ NAN_MODULE_INIT(init) {
Nan::Set(target,
Nan::New("getImageRGBA").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(GetImageRGBA)->GetFunction());
// File related APIs.
Nan::Set(target,
Nan::New("saveTextToFile").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(SaveTextToFile)->GetFunction());
Nan::Set(target,
Nan::New("readTextFromFile").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ReadTextFromFile)->GetFunction());
Nan::Set(target,
Nan::New("saveFilesToCloud").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(SaveFilesToCloud)->GetFunction());
// Cloud related APIs.
Nan::Set(target,
Nan::New("isCloudEnabled").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(IsCloudEnabled)->GetFunction());
Nan::Set(target,
Nan::New("isCloudEnabledForUser").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(
IsCloudEnabledForUser)->GetFunction());
Nan::Set(target,
Nan::New("enableCloud").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(EnableCloud)->GetFunction());
Nan::Set(target,
Nan::New("getCloudQuota").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(GetCloudQuota)->GetFunction());
// Achievement related APIs.
Nan::Set(target,
Nan::New("activateAchievement").ToLocalChecked(),
Expand Down

0 comments on commit 7dfbd5f

Please sign in to comment.