It's weird learning a new language. Picking up on the basics of the language and getting things working is easy enough; it's usually just a matter of hunting through intellisense for a correct-sounding function and asking the occasional question. But, its a far cry from getting something working to actually utilizing the language. I know just about enough C# to realize that I am just writing C++ code with C# syntax, and that I'm not doing anything in a particularly clever, C#-friendly way.
Take my file parser for instance. I needed to save out and load in collision and waypoint info for the map editor. Now, I should be doing all this in XML, but I don't really know how to use the built-in XML parser in .NET, so I'm just using plain text and white-space separated values. This leads to one of the dirtiest, most unsafe file parsers I've ever written (and I've written some pretty terrible ones). Sure, it works (if the text file is not malformed), but this is some dailyWTF quality code, if you ask me.
Of course, I couldn't say that without showing you my code. That just wouldn't be fair, now would it? Hope you have a good laugh, at least:
private void ReadMapFromFile(string filename) { string delim = " "; char[] delims = delim.ToCharArray(); try { System.IO.StreamReader reader = new System.IO.StreamReader(filename); string s = reader.ReadLine(); System.Diagnostics.Debug.Assert(s == "MAP"); s = reader.ReadLine(); System.Diagnostics.Debug.Assert(s == m_FileName); s = reader.ReadLine(); System.Diagnostics.Debug.Assert(s == "RESOLUTION"); s = reader.ReadLine(); string[] words = s.Split(delims); int X = Convert.ToInt32(words[0]); int Y = Convert.ToInt32(words[1]); s = reader.ReadLine(); System.Diagnostics.Debug.Assert(s == "LINES"); while ((s = reader.ReadLine()) != "CIRCLES") { words = s.Split(delims); Point begin = new Point(Convert.ToInt32(words[0]), Convert.ToInt32(words[1])); Point end = new Point(Convert.ToInt32(words[2]), Convert.ToInt32(words[3])); System.Collections.ArrayList al = new System.Collections.ArrayList(); al.Add(begin); al.Add(end); completedShapes.Add(al); } while ((s = reader.ReadLine()) != "RECTANGLES") { words = s.Split(delims); Circle c = new Circle(new Point(Convert.ToInt32(words[0]), Convert.ToInt32(words[1])), Convert.ToInt32(words[2])); circs.Add(c); } while ((s = reader.ReadLine()) != "WAYPOINTS") { words = s.Split(delims); Rectangle r = new Rectangle(Convert.ToInt32(words[0]), Convert.ToInt32(words[1]), Convert.ToInt32(words[2]), Convert.ToInt32(words[3])); rects.Add(r); } while ((s = reader.ReadLine()) != null) { words = s.Split(delims); Waypoint wp = new Waypoint(new Point(Convert.ToInt32(words[0]), Convert.ToInt32(words[1]))); for (int i = 2; i < words.Length; ++i) { wp.links.Add(Convert.ToInt32(words)); } waypoints.Add(wp); } } catch (System.IO.IOException) { } }
And here is a sample text file it would read in:
MAPDock.jpgRESOLUTION1024 768LINES281 322 490 214490 214 362 520362 520 173 501173 501 281 322CIRCLES710 395 14199 98 45RECTANGLES160 709 715 50864 102 -282 -48WAYPOINTS57 572 3 1312 123 0 2602 188 3 1449 609 2 0
At this point, class is over in 2 weeks so I'm just crunching to get everything done. Clean, safe code be damned [razz].