Tuesday 7 June 2016

Converting Venture to C#

As I am between jobs, I thought I would set myself a project to work on.  Simplest one I fancied doing was converting my old iOS adventure game library to C#.  The theory being, write it once in .Net Core 1.0 and then use Xamarin forms to port it to Win10, Android and iOS.

So, I have got the basics working.  First task was reading the XML config for the game into some sort of data structure.  This was done by creating classes for each "element" and then using XmlType and XmlAttribute to map it onto the XML.  Then you can use

XmlSerializer deserializer = new XmlSerializer(typeof(GameDetails));
Stream s = GetResourceStream(xmlFileName);
using (TextReader textReader = new StreamReader(s))
{
     gd = (GameDetails)deserializer.Deserialize(textReader);
}

to read the xml file into a complete data structure.  Much easier than the iOS way.  Only slight hitch with Core 1.0 is that you have to pass a Stream to the XmlSerializer and not simply the file name.  Also, Core makes it harder to access files as it is multi platform and they all treat files differently.

Next I added NCalc which is a library for evaluating expressions.  So it can take the string "2+2" and return 4 as the answer.

Then i set up some unit tests and used those to get the engine working.  One test to check the XML loaded correctly and another to check the Conditions and Expressions evaluated correctly.  More to test the PageText and Buttons were evaluated correctly.