Thursday, August 14, 2014

MongoDB Sorting

Mongo DB command line:
db.collection.find().sort( { score: -1 } )

Mongo DB Java driver:
DBCursor cursor = collection.find();
cursor.sort(new BasicDBObject("score", -1));

Note that "-1" has to be an int, not a String

Reference: http://docs.mongodb.org/manual/reference/operator/meta/orderby/

MongoDB Index

Single field index: http://docs.mongodb.org/manual/core/index-single/

Compound index: http://docs.mongodb.org/manual/core/index-compound/

Thursday, August 7, 2014

Bitnami Wordpress Root

Here it is: "/opt/bitnami/apps/wordpress/"

Wordpress Pingback Attack

This attack makes use of the pingback/trackback feature to generate a lot of traffic to a target via many third-party uncompromised Wordpress servers. This is because when this request is made (via xmlrpc.php) to a server, it will attempt to download data from the target server.

These links describe the attack:
Here are some suggested solutions:

Cocos2D-X HttpClient: Adding HTTP Headers

It's done like this:
std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8");
request->setHeaders(headers);

Reference: http://www.snip2code.com/Snippet/15942/HTTP-Client-for-Cocos2dx

Cocos2D-X: Basic Guide to Using HttpClient

This guide is pretty much covers the basics: http://www.cocos2d-x.org/wiki/How_to_use_CCHttpClient

The only part lacking is the signature of the callback, which needs to have 2 arguments. Example:
void onHttpRequestCompleted(cocos2d::network::HttpClient* client, cocos2d::network::HttpResponse* response);

Blog Post Comparing JSON Libraries in C++

Link: http://www.thomaswhitton.com/blog/2013/06/28/json-c-plus-plus-examples/

Covers:

  • jsoncpp
  • rapidjson
  • jansson

Using Rapidjson in Cocos2D-X: Creating a JSON Document in Code and Serializing it

This is based on Cocos2D-X version 3.2

This import is necessary for creating a JSON document and manipulating it:
#include "external/json/document.h"

These imports are necessary for Serialization:
#include "external/json/writer.h"
#include "external/json/stringbuffer.h"

Example code:
rapidjson::Document jsonDoc; // create the object
jsonDoc.SetObject(); // flag it as an object rather than an array,a necessary step for Serialization

// to set a value (using Cocos2D-X String type in this example here)
String* key = “key”;
String* value = “value”
jsonDoc.AddMember(key->getCString(),value->getCString(),jsonDoc.GetAllocator());

// to Serialize to JSON String
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
jsonDoc.Accept(writer);
String* jsonString = String::create(sb.GetString());

References:

Enabling HttpClient in Cocos2D-X in Android

To do this, you'll need to uncomment the following lines in Android.mk

LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
and
$(call import-module,network)

Otherwise you'll get error messages saying HttpClient cannot be found.

Android Error "Binary XML file line #xxx: Error inflating class "

There seems to be many causes of it but one of them seem to be out of memory error, just bad error handling and unclear error message:

References:

Cocos2D-X 9 Patch Technique with Scale9Sprite

Links:

The key thing step here is the "setCapInsets" function.

Cocos2D-X 3.0: What's New

As summarised in this blog post http://www.cocos2d-x.org/news/216 :

  • A lot of refactoring with "CC" removed from many class names, the API design being more C++ than just a straight port of Cocos2D etc
  • Huge performance improvements (or so they claim)
  • New UI widget collection
  • Many many more

Cocos2D-X 3.0 Changes Part 1

A lot of refactoring was done moving to Cocos2D-X 3.0, with a lot of renaming being done, making it difficult to map old classes to new ones. Here are some observations and notes:

  • CCObject has been removed, if you wish to create an object that supports auto-release and reference counting, use "Ref" instead.
  • CCLOG doesn't seem to work anymore (nothing showed up in Android logcat), but switching to use "log" worked fine.
  • CCString is now just "String" (this is a bit confusing as you don't exactly find a "String" class in the API references)
  • CCPoint is now Vec2
  • Generally CC has been removed from many class names, but some classes (such as those above), have been completely renamed

Wednesday, August 6, 2014

Mac OS SVN

No longer installed by default (Mac OS 10.8.X). To install, open XCode, then Preferences, go to the Downloads tab and install "Command Line Tools".

Reference: http://blog.grapii.com/2012/08/svn-missing-in-mac-os-x-10-8-mountain-lion/

Monday, August 4, 2014

Cocos2D-X Creating Solid Color Rectangular Sprite

There are times where instead of overriding the "draw" method, you need to create a sprite that is a solid rectangle. There are a few ways to do it, but the easiest for me is to create a 1x1px PNG file. The color can be changed during runtime as needed.

Reference: http://stackoverflow.com/questions/3339955/cocos2d-solid-color-rectangular-sprite

Android "gravity" vs "layout_gravity"

"gravity": affects layout of children within the view

"layout_gravity": affects the layout of the view within its parent

Reference: http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity

Cocos2D-X Android: "Resources" vs "assets" Folders

In Cocos2D-X, the art assets are placed in a "Resources" folder that is above the project folder for every project. However, in Android, the folder designated for this is "assets", and it is inside the Android project folder.

How Cocos2D-X manages this (tested on Android ADT), is that the "Resources" folder should contain the master copy for all the assets, and that all assets should be managed from there. When the "assets" folder is refreshed, it is automatically synced with the "Resources" folder.

Cocos2D-X Android: Cocos Extension cocos-ext.h

To enable the "cocos-ext.h" Cocos Extension library, you'll need to edit the Android.mk file by uncommenting (or adding) the following lines:

LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
and
$(call import-module,extensions)

In your source files, include it like this:
#include "extensions/cocos-ext.h"

This is for Cocos2D-X 3.2.

Friday, August 1, 2014

Cocos2D-X Device Orientation

The configuration for this is platform-specific: http://www.cocos2d-x.org/wiki/Device_Orientation

Building a Cocos2D-X Android App

The steps here are generally correct though I had to work through some version-specific issues:
http://www.cocos2d-x.org/wiki/How_to_Build_an_Android_Project_with_Eclipse

The above link is for Cocos2D-X version 3.0 while I was using version 3.2.

Versions:

  • As for Eclipse & Android SDK it's the Android ADT 20140702 bundle
  • NDK r10
Issues:

The first issue I ran into was NDK_ROOT not found in the environment (even though it has been defined in .bash_profile). I hardcoded it into the build_native.py and it solved the problem.


NDK_ROOT="path_to_ndk"

The next issue is a CPP compile issue in the file "cocos2d/cocos/3d/CCBundleReader.cpp". I changed the return type in the header to "ssize_t" and it worked.


   /**
    * Returns the position of the file pointer.
    */
   ssize_t tell();

Lastly, the Java compiler was missing libcocos2dx.jar

I had to open the project at "cocos2d-x-3.2/cocos/platform/android/java", compile the library project and copy the libcocos2dx.jar from the "bin" folder to my project's "lib" folder.

Note: I later found out that this is because I imported the Android project only in Eclipse by selecting the Eclipse project file. If I were to select the project at the top-level folder instead, I would have been given the option of importing the libcocos2dx library as well. In which case the project would have compiled without the steps above.

After these I could build the app. The CPP compile error was a bit of a surprise thought. It shouldn't be happening.