Unity3D and Raycasts in a 2D Scene

Note to self: When using RaycastHit2D in Unity make sure that the GameObjects you’re attempting to collide with have a Rigidbody component or those rays are going to shoot right on through!  In a 2D scene where you’re not bothered about gravity its helpful to make those Rigid bodies kinematic as well else they’ll fall to nowhere. #stilllearning

An example detecting a GameObject located below a touch event:

 

Touch touch = Input.touches [0];

Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit2D hit = Physics2D.GetRayIntersection(ray);

if (hit) {
    Debug.Log ("Hit: " + hit.transform.gameObject.name);
    // Do something with the GameObject...
}

Roku 3 Error 013 or Error 014 with a Virgin Media SuperHub 2

After grabbing a Roku 3 today I found myself in the unfortunate situation where it would not connect to my home network, which is driven by a SuperHub 2 from Virgin Media.  I would receive ‘Error 013′ when trying to connect via ethernet and ‘Error 014′ connecting via wireless.  Fortunately with a bit of faffing this can be fixed.  To get it sorted you’ll need to connect your Roku to a alternate network with internet access, run a firmware update and then change a top secret option.

The Fix

This is all obviously at your own risk.  I’d assume the menus are top secret for a reason!

  1. Turn on your Roku
  2. Connect your Roku to an alternate network. I tethered mine to my phone. This is for a firmware update so keep in mind any data usage limits.
  3. Press the home button 5 times, fast forward button 3 times and rewind button twice
  4. Run the software update (even if your Roku already thinks it is up to date)
  5. Wait for the Roku to reboot then access another top secret menu by pressing the home button 5 times, fast forward button once, play button once, rewind button once, play button once, followed lastly by pressing the fast forward button once.
  6. Disable network pings in the menu and select ‘Back’
  7. Hook the Roku up to your SuperHub

Your Roku will now connect to your enternet or wireless Virgin SuperHub’ed network allowing to continue with the set up wizard. Good times!

Updating WordPress via SSH (Who knew it was possible?!)

I’ve been running WordPress on a VPS for a few years now. Because I didn’t have an FTPd installed I ran updates manually as I assumed this was the only way in for the WordPress auto-update function.

Turns out WordPress supports updating via SSH – you just need to have the libraries installed for PHP. To enable auto-update using SSH on Debian-like systems follow the instructions below.

Install the SSH2 extension for PHP:

sudo apt-get install libssh2-1-dev libssh2-php

Restart your web server:

sudo service apache2 restart

Next time you log in to your WordPress admin panel you’ll be able to update WordPress and any installed plugins with a couple of button clicks. Handy!

C# “The server committed a protocol violation. Section=ResponseStatusLine”

This error had me stumped for a few minutes today. When crafting a POST request to an API I found that every other HttpWebRequest during one session would fail throwing the error:

The server committed a protocol violation. Section=ResponseStatusLine

Which was pretty annoying. My tests didn’t pick up the bug earlier as the application only depends on one API function which in the typical use case is called once per session. After checking that all was well with the request and API I headed for Google. Googling the above error turns up a couple of solutions that didn’t work for me.

Solution 1

Add the below to your app.config:

<system.net>
    <settings>
        <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
</system.net>

This disables the validation performed on HTTP requests. Although I trust the server providing the API this didn’t really sit right with me.

Solution 2

Set the following property on your request:

request.ServicePoint.Expect100Continue = false;

This again didn’t work for me. Reading the MSDN entry it doesn’t even seem relevant.

What worked

Set the following property on your request:

request.KeepAlive = false;

Issue goes away! When KeepAlive is true keep-alive is sent in the request’s header to the server. This indicates a preference to keep connections alive saving having to tear them down every request.

As I am sending one API request per session my users and the server have little to gain by keeping the connection alive. But I do wonder, this should work magically, so where is the issue arising? The API host is still running IIS6, so perhaps its a legacy thing? Anyway, problem solved for now! I hope this proves useful for someone else.

You can read more about keep-alive and persistant connections over at Wikipedia and more about HttpWebRequest’s KeepAlive property on MSDN.

Visual Studio 11 beta installer on Windows 8 on Bootcamp on a MacBook Air locking up

Just a quick note for anyone that is running this combination and running into the same problem as me – I found that the Visual Studio 11 beta installation would lock up my Macbook Air.

The fix for me was running the available Windows updates. After a reboot the Visual Studio 11 beta installer ran smoothly and all was well. In fact, Windows 8 runs beautifully on a MacBook Air!

How to fix CodeIgniter not accepting RTF mimetype in Internet Explorer

While working with the CodeIgniter file upload library I discovered that I was able to upload RTF files in Chrome but not in Internet Explorer.

When executing:

$this->upload->do_upload()

I would get:

The filetype you are attempting to upload is not allowed

from:

$this->upload->display_errors()

After some research I found that this was due to the fact that sometimes different web browser describe a file with the same extension with a different mime type upon upload.

Luckily the fix is simple. Internet Explorer sets the mime type of an RTF to ‘application/msword’. All we need to do is add this mime type to our mimes.php config file. CodeIgniter accepts an array of mime types per file extension, so all we need to do to fix is open up application/config/mimes.php and replace the line:

'rtf' => 'text/rtf'

with:

rtf => array('text/rtf', 'application/msword')

You will now be able to upload RTF files in your CodeIgniter application via Internet Explorer. I hope that helps somebody!

SQL COALESCE function

Today I discovered the SQL function ‘COALESCE’.  The function returns the value with the highest precedence datatype from a comma separated list of expressions.  For example:

SELECT COALESCE (site_settings.VALUE, settings.VALUE) AS VALUE
FROM site_settings
LEFT JOIN settings ON settings.id = site_settings.setting_id

In the above example the first non NULL value passed to the COALESCE function is selected. if site_settings.value is NULL then settings.value will be selected. If settings.value is NULL, then the site_settings.value will be selected. If both values are NULL, then NULL will be returned.

Here is the MySQL reference page for the function: COALESCE

Worth keeping in mind. Came in handy for simplifying some SQL at work.

Testing out the iPhone WordPress client: Homemade lasagne

Recipe from http://www.bbc.co.uk/food/recipes/simons_mums_lasagne_62589

Didn’t soak the pasta sheets enough so was a little dry. Other than that, yum!

20120204-182416.jpg

Motivation

I’ve decided to spend this snowy day setting up a new WordPress install for my blog.  I’ve had an idea for a project I would like to work on so rather than diving into that, I’m doing this!  I could be accused of procrastinating…

The real reason I’m re-setting up WordPress is to help me focus.  By writing down and thinking about the ideas and the resulting code I can make it the best it can be.  If it helps someone else along the way, great!  I’m likely to focus on PHP and Python as day to day thats what I’m work with.  I’m likely to plan out my project on paper over the next couple of weeks.  When I am focusing on something such as an API or a Python library I’ll use this blog to explain what I’m up to and why I’m approaching a problem from that angle.

I’ve not done much web work with Python before, so when I get to it thats where I’ll start.