Wednesday, July 13, 2016

C# Reflection: Setting a private enum-type member variable with a private inner enum type value

Today, while creating a C# unit test, I had a situation where I needed to set the value of a private variable on the class under test.  The variable was an enum-type variable, where the enum type itself was a private inner type defined in the class under test.

(I won’t in this post get into why I ended up landing on doing this, instead of some other solution such as refactoring the actual class under test in order to avoid the need to do so, as that would be a separate discussion.)

This post describes the technique using reflection that I ended up using to accomplish this.

For example, given a class defined like:

class Card
    private Suit _suit;

    private enum Suit

Given an instance of that class named card, I was able to set the value of that object's private _suit variable by doing:

using System.Reflection;


// Get a reference to the _suit member variable.
FieldInfo suitField = card.GetType().GetField("_suit", BindingFlags.NonPublic | BindingFlags.Instance);

// Get a reference to the Spades member of the Suit enum.
object spades = card.GetType().GetNestedType("Suit", BindingFlags.NonPublic).GetField("Spades").GetValue(card);

// Set the value of _suit on our card object to Spades.
suitField (card, spades);

This kind of reflection hackery probably isn’t a best practice in most situations! Still, I thought it might be helpful to record here as an aid for those rare situations where it does make sense to manipulate this kind of enum-type private variable for unit testing purposes.

Thursday, May 26, 2016

Solution: HTTP POST via curl has “data” value unexpectedly truncated


When using the curl command-line utility to manually sent an HTTP POST to a server, the “data” value specified in the message is unexpectedly truncated when the server receives it. 

For example, given this command line:

curl --request POST "" --header "Content-Length:115" --header "Accept-Language:en-us" --header "" --header "Accept:image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/, application/, application/msword, */*" --header "User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)" --header "Connection:Keep-Alive" --header "Cookie:ASP.NET_SessionId=some_sesson_token_here;" --header "Referer:" --header "Content-Type:application/x-www-form-urlencoded; Charset=UTF-8" --data "PrimaryID=719&SecondaryID=27483&"

The server (an ASP.NET MVC application) received the message, but the “message” parameter was assigned a value of “Visit my site at http” – the “://” portion of the value was missing.

The subsequent included parameters in the “--data” value, such as TertiaryID, were also completely missing their values, according to the server.


The problem is the Content-Length header value (from earlier in the command line).  As written, it has a value of 115, so the server is truncating the data value after 115 characters (which happened to be just after the “http” in the “message” parameter in this example).

The solution is to either set the Content-Length value to the actual length of the data value, or to just omit the Content-Length header entirely.

Thanks to my colleague Kevin for pointing that out and saving my sanity!  I had originally approached this problem assuming that the “://” was the problem – that curl wasn’t sending it correctly, and/or that the server was refusing it (possibly for security reasons) – but that turned out to be just a red herring, based on the fact that the incorrect Content-Length value just happened to fall near that substring’s position.

Friday, April 15, 2016

Vigil RPG (“Premium” iPhone Game) – Lifetime Sales Stats

Sometime in mid-2013, I had a hankering to play a particular kind of RPG on my iPhone. I wanted a game with these features:

  • Turn-based combat.
  • Portrait orientation, and thus playable with one hand. (e.g. while eating with the other hand.)
  • A single protagonist/hero. One thing I don't like about party-based RPGs is that typically, a couple of your party members need be KO’ed before you feel like the team is actually in any real danger. This doesn't tend to happen against non-boss enemies in most games, and thus those games often end up feeling uninteresting for long stretches.
  • Interesting decision-making in combat -- even vs. non-boss enemies -- something beyond the typical RPG trope of "do basic attacks / target enemy elemental weaknesses / heal self when injured / repeat."
  • No hard-to-use on-screen virtual D-pad for character movement. Give me a way to move my character that’s designed especially for a touchscreen, not one based on a traditional physical controller’s tactile D-pad!
  • A combat system built around LOW numbers and visible enemy HP / stats, so I can calculate that if, for example, that enemy has 9 HP left, then I can perfectly finish it off by doing my 4 and 5 HP attacks respectively over the next 2 rounds.
  • FAST combat. No waiting on long combat animations; no wading through multiple menus to kick off a combat round. This is my phone; let me whip it out when I’ve got 30 seconds, and actually accomplish something quickly.
  • No save points. Why not just always keep my game saved automatically?  (Even mid-combat!)
  • Game designed with a goal of fun, not of corporate revenue generation! Absolutely no IAPs or premium currencies or ads or stamina timers.

I couldn't find that game on the App Store.

So... I decided to write it myself!

After spending most of my evenings between 10:00pm and midnight (after my day job, spending time with my family, getting the kids into bed, and daily chores) for about 18 months designing and writing the game – learning the Objective-C programming language and the whole MacOS / iOS development ecosystem along the way – Vigil RPG was released in November 2014!

Here’s Vigil RPG’s combat screen, which illustrates the realization a lot of the points noted above that I wanted to achieve with the game.  You can check out more screen shots and info about the game at the Vigil RPG website!


Lifetime App Store Sales Stats

I don't really have any reason to keep them private, and I thought it might be insightful for other #indiedev folks and industry observers, so without further ado, here are the lifetime sales statistics to date for Vigil RPG (iOS)!  According to my developer account at iTunes Connect:

2016-04-15 12_27_17-iTunes Connect

  • Released November 2014 at a price of US $2.99
  • 354 paid copies sold, almost entirely at $2.99, with a few at $1.99 in a "birthday sale" in November 2015
  • Total gross sales: US $1004
  • About 70% of the lifetime sales of Vigil RPG came in the first 30 days after release.
  • Vigil RPG got about ten 5-out-of-5-star community reviews on the App Store (and no 0-through-4-star reviews) immediately after release; it’s gotten zero community reviews since then.  (Vigil RPG has no “review nag” prompts, which was an intentional design decision.)
  • The second big spike in sales was after the 4-out-of-5-star TouchArcade review (which I was thrilled with, and found to be extremely on-point and fair – much respect to the reviewer, Shaun Musgrave). TouchArcade was the only major site to do a review.
  • The little spike in November 2015 was the beginning of the $1.99 sale.  Sales dropped off again rapidly even though I left the price at $1.99 for a while.
  • Outside of the initial release and $1.99 sale periods, Vigil RPG sold at a rate of roughly 1 copy per week.
  • Net proceeds after Apple's cut: US $707
  • 3 x $US 99 of Apple annual developer licenses to develop the game and keep it live on the App Store = $297. Net proceeds after Apple dev license fees: $410
  • Other misc. operating costs -- State of Michigan incorporation fees for Aggro Magnet Games LLC, web hosting for -- of around $100 to date.  Bottom line proceeds to date: About $310
  • 122 free copies redeemed (promo codes sent to review sites; a few free giveaways to try and drum up visibility and community interest)
  • I didn’t bother trying to keep any stats on piracy rates, but at least one site out there (fairly readily findable via Google search) has the binary of the game posted for free download.

Given a very very rough estimate of about 600 hours spent creating the game, $310 in net profit works out to a wage of about $0.50/hour.  Not exactly enough to quit the ol’ day job!  (Fortunately, I already have a day job which I love!)

I am, however, honestly totally fine with that performance. I made an intentional decision up front for my goal for the Vigil RPG project to be to "make the game I wanted to play" – with no design compromises being made for the sake of monetization.  So no IAPs, no ads, no other typical "freemium" features (or “anti-features,” as the case may be) such as premium currencies or stamina timers.


$0.99 Sale

Consistent with my initial goal for Vigil RPG of prioritizing fun over profits, as of today, for the first time ever, the App Store price for Vigil RPG is reduced to $0.99!  I’m hopeful that this will allow more people to enjoy the game – assuming there’s a segment of folks out there who are interested in iPhone RPGs, and are unwilling or unable to buy the game at the $2.99 price point, but will go ahead and pick it up for $0.99.

The main reason I didn't just cut the price all the way down to $0.00 (free) was that admittedly there's somewhat more cachet in being able to say "The game I made is for sale on the App Store!" than "I made a game and I'm giving it away on the App Store since no one was really buying it!" 

It would also be nice if Vigil RPG’s proceeds would at least cover the annual $99 that Apple requires to keep it listed on the App Store.  To that end, I might bump the price back to the original $2.99 at some point if sales at the $0.99 price point don’t generate much increased volume relative to the 1 sale/week or so of the $2.99 price.


“Buy It Now!”

Hopefully this detailed peek into one game’s iOS App Store performance was helpful, or at least mildly interesting!

If you’d like read more about the gameplay of Vigil RPG, you can do so on the Vigil RPG website.  Or, you can check out the full 5-to-10-hour adventure firsthand via Vigil RPG on the App Store if you’ve got an iOS device, and can scrape together enough loose change to join the exclusive club of premium iOS game owners!

You can also hit me up with any questions you’ve got on Twitter at @AggroMagnetGame, or below in the comments!

Wednesday, April 06, 2016

New PC 2016: Things were not cool for a while there

One of the things I did to verify that my newly-built home PC was working well was to download and run a CPU temperature monitoring program, and leave that open in the secondary monitor while running programs in the primary monitor.

Unfortunately, this pretty quickly turned up problems. The CPU, an Intel Core i7-4790K, would get dangerously hot when running certain games. The game Cities: Skylines exhibited the worst symptoms: After running the game for just a minute or two, although the game itself would run great, the CPU temperature (as reported by the temperature monitor program) would shoot up to nearly 100 degrees C!  That’s close to the point where the PC will shut itself off to avoid damage, and much hotter than I would expect.

I thought the problem might be due to my having done a poor job applying the thermal paste to my CPU and/or installing the stock heatsink incorrectly, so I removed the heatsink, carefully cleaned off the old thermal paste, applied new thermal paste, and reinstalled the heatsink.  After doing that, though, the CPU temperatures while playing Cities were still extremely hot.

At this point, on the advice of some of the friendly folks at the Gamers With Jobs community, I decided to throw some hardware at the problem, in the form of a US $29 Cooler Master Hyper 212 EVO heatsink! 

I’d previously never bothered with “premium” heatsinks, since I don’t overclock my systems (valuing rock-solid stability over an incremental speed boost).  In this case, though, it seemed like the best option to protect the $340 investment I’d made in my nice CPU.

I’m very happy to report that it worked perfectly!  With the 212 EVO installed (replacing the stock Intel heatsink, and with another fresh application of thermal paste), CPU temperatures while playing Cities: Skylines dropped from nearly 100 C down to the mid-40s C!

One caveat that I discovered with the 212 EVO though is that it fastens to the motherboard from both sides, effectively pressure-clamping itself down onto the CPU.  Therefore, I needed to unscrew my motherboard from the case to install the new heatsink.  If you’re doing a build that includes a premium heatsink, I suggest installing the heatsink and CPU onto the motherboard before screwing the motherboard into the case!

A final additional purchase I made was a cheap ($8) case exhaust fan, since the case I used didn’t come with one.  I didn’t want the air inside my case warming up over periods of long computer use. 

CPU cases evidently come with quite a few variants of screw mounting hole spacings.  The distance between screw holes is not, as it turns out, the size of fan that you should order!  I found the chart on this page very useful (and accurate!) in translating the space between mounting holes that I measured on the back of my case to the size of fan that I needed to order.

Monday, February 15, 2016

2016 New Primary Home PC Build!

With my family's primary home PC having been built in 2008 and showing its age, it was time earlier this month to build my first general-use home PC in 8 years!

Here's the parts list I put together and built, with a somewhat-flexible budget of around $1200:

Type Item Price
CPU Intel Core i7-4790K 4.0GHz Quad-Core Processor $339.99 @ Newegg
Motherboard MSI Z97-GAMING 5 ATX LGA1150 Motherboard $153.98 @ Newegg
Memory G.Skill Ripjaws X Series 16GB (2 x 8GB) DDR3-2133 Memory $74.99 @ Newegg
Storage (OS) Samsung 850 EVO-Series 250GB 2.5" Solid State Drive $87.99 @ Newegg
Storage (Apps) Western Digital 1TB 3.5" 7200RPM Internal Hard Drive $59.99 @ Office Depot
Storage (Data) Western Digital Blue 4TB 3.5" 5400RPM Internal Hard Drive $133.99 @ Newegg
Video Card EVGA GeForce GTX 970 4GB SSC ACX 2.0+ Video Card $333.98 @ Newegg
Power Supply EVGA 500W 80+ Bronze Certified ATX Power Supply $29.99 @ Newegg
Optical Drive Asus DRW-24B1ST/BLK/B/AS DVD/CD Writer $15.99 @ Newegg
Operating System Microsoft Windows 10 Home OEM (64-bit) $102.98 @ Newegg
Total   $1333.87

I had a spare case and existing mouse / keyboard / monitor / speakers to use with this build, so I didn't need to factor those in.

The website (the target of all of the links in the parts list above) was a particularly helpful tool in keeping track of the parts for this build as I was researching and selecting them!  It was a nice upgrade over the text file and/or spreadsheet-based systems I’ve used for this in the past.


In chronological order of how I executed the build!

The goods, prior to the build.  (Note: The LEGO blocks pictured were not actually included in the build.)


The empty case – with its circa 2006 350W power supply with only IDE power cables, no SATA, removed – ready for components!


Motherboard in place and screwed down!


The Intel Core i7 CPU, still in packaging.  That’s a lot of power packed into a small package!


Close-up of CPU installed in motherboard.


CPU locked into place (via the lock included as part of the motherboard).


CPU with thermal paste applied and then fan installed on top.


New power supply installed in case and wired up to motherboard.  It isn’t very visible in this photo, but the CPU wires for the power switch, reset switch, and the front USB 2.0 ports are also wired to the motherboard in this photo, on the bottom edge.


The solid state drive (SSD) and traditional hard disk drive (HDD) drive side-by-side.  Even though this wasn’t my first SSD install, I was surprised anew just how small, thin and light that drive is compared to the traditional HDD.


Drives installed in case.  This older case didn’t have a spot designed to accommodate the SSD, but that drive was small and light enough that I was comfortable with just screwing one side of it into the 3.5” HDD bay (at an angle to get the screw holes to line up!).  The optical (CD/DVD) drive is also installed in the top 5.25” slot.


The G.Skill Ripjaws 2 x 8GB RAM installed (just to the right of the big CPU fan).  Not sure how much faster the fancy red trim makes it, but it does look cool!


The GeForce GTX 970 video card, just out of its packaging.  This thing is a beast, size-wise!  Clearly EVGA wants it to look nice out of the box, since it came with clear plastic wrap over the entire thing (a couple of pieces of which are still on and visible in this photo, such as the piece over the “GeForce GTX 970” logo on the bottom edge).


Video card installed.  It turned out to be juuust big enough in this case that I couldn’t quite install a full size hard drive directly across from it (even using a 90-degree SATA cable).


Finally, everything assembled, with the cover on over the rear-facing motherboard ports.  Both the motherboard and video card came with caps installed over their video ports (as shown here), which I appreciated.


Mishaps and Mistakes


So with everything assembled and monitor, keyboard, mouse, power, network, and speakers all plugged in, I hit the power button for the first time… and immediately noticed two obviously “unhappy” sounds:

  1. A buzzing-type sound coming from the bottom portion of the case;
  2. A repetitive squealing / grinding sound coming from the front of the case.

The first sound turned out to be easily solvable; one of the case wires at the bottom of the case was contacting the spinning fan on the underside of the video card.  Getting those wires out of the way solved that.

Unfortunately, the second noise turned out to be the sound of a dead hard drive.  The noise was coming from the 2TB HDD.  It wouldn’t stop making the noise, and the drive wasn’t recognized by the BIOS (whereas the SSD and the optical drive were recognized with no issues).

This was my first DOA (“dead on arrival”) part among the five PC builds I’ve done, so I suppose I was due. I got it returned and refunded with no issues… and ended up breaking my budget a bit by replacing it with both a fast 7200 RPM 1 TB drive for installing programs, and a 5600 RPM 4 TB drive for storing all the great photos my wife takes.

Forgotten Thermal Paste

Fortunately, I didn’t forget to apply thermal paste, which might have resulted in a cooked CPU.  Rather, I forgot to order thermal paste.  None came with the CPU I bought, and I only do PC builds infrequently (every few years) and so didn’t have any on hand.

Also fortunately, my town has a little PC repair shop, and so one quick car trip and $1 later I was good to go with a single-use tube of thermal paste.

Don’t Leave the Motherboard Backplate For Last

With this build, I made the embarrassing mistake of leaving the install of the motherboard backplate (which ends up situated over all of the ports on the back of the PC) for last, thinking for whatever reason that I could pop it into place from the rear.

However, I realized the hard way that the backplate does not install from the rear; rather, it needs to go on from the inside of the case, before the motherboard gets fastened into place.

So, to my chagrin, I ended up unscrewing the motherboard from the case (leaving everything else connected), shifting it slightly to allow the backplate to be installed, and then positioning it back into place and screwing it back in.  And so, oddly enough, this build came full circle, in that affixing the motherboard to the case was both the first and the last step!

So How’s it Working?

It’s working great!  I haven’t measured it yet, but Windows 10 boots incredibly fast. 

The app where I’ve seen the most difference relative to my old PC is the game Cities: Skylines (the latest and greatest take on the city-builder genre pioneered by SimCity).  On the old PC, saved games would take a very long time to load, and the game itself was quite playable but it would “chug” noticeably, particularly when trying to rapidly scroll around the map.  On the new PC, by contrast, it’s super fast and smooth as silk!

After doing “main home PC” replacements every 4 years (in 2000, 2004, and 2008), that 2008 model lasted for a solid 8 years.  (It’s actually still running, although there are signs it might be on its last legs, which helped prompt this upgrade).

I’m hoping this new PC has a nice long life as well!  I’d like it to still be running in 8 years… at which point my 10-year-old will be headed off to college.  Now there’s an interesting thought!