Monday, October 6, 2014

New Pumpkin Templates

Gearing up for this halloween, here's a few templates I made.  Click the pic for a link to the pdf.

Enjoy!




Previous Years Pumpkin Templates

Click to see these lit up!
Despical Me:



Click to see this one lit up!
Man of Steel (See this post for more details)





Tuesday, April 29, 2014

Backreferences Visual Studio Find/Replace regex

Here's another post of soemthing nifty that I want to remember later.

In Unity, I was getting an error: "CompareBaseObjectsInternal can only be called from the main thread"

It turned out, if you try to do  something of the form
 if( instance == null)  
in anything that isn't the main thread (or called by the main thread) you get an error because unity implements its own comparison operation that is not thread safe (and not documented anywhere.)

 The solution, which is pointed out elsewhere was to use C#'s Object.Reference.Equals function. Instead of going through every file (at this point, there's around 100 source files), I did a find and replace with regex.

I always seem to forget about backreferences (hence this post).

I needed to find every instance of  if( instance == null ) and replace it with if( ReferenceEquals(instance,null)) but for any variable name. Backreferences  in regular expressions seek to let you do just that.

My find string was this:

 if[^\S\r\n]*([^\S\r\n]*(?<var>.*)[^\S\r\n]*==[^\S\r\n]*null[^\S\r\n]*)  

ignoring all the whitespace chars, the string is this

 if ( (?<var>.*) == null )  

which basically says to save any string of characters into a backreference named 'var'

My replace string:
 if( ReferenceEquals(${var}, null) )  

then uses that backreference.

Find and Replacing in files found 75 instances where I had compared a variable (or some reference) directly to null. I did scroll through the whole list first, before I actually allowed it to replace.

Annoying problem, simple solution.