First C# Project

I've just gone through the Head First C# book. I've now started on my own project. What I want to do is make a pilot file editor for the old X-Wing game. I use to edit my pilot files with a hex editor. I figure this would be a small enough project to get my feet wet with.

I'm reading in the data from the pilot file with the following method. This works fine and I get the whole thing in a byte array.

void openToolStripButton_Click(object sender, EventArgs e) { openFileDialog1.FileName = ""; openFileDialog1.Filter = "Pilot Files (*.plt)|*.plt"; openFileDialog1.CheckFileExists = true; openFileDialog1.CheckPathExists = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { fileName = openFileDialog1.FileName; bytes = File.ReadAllBytes(fileName); pilot.GetData(fileName, bytes); } UpdateForm(); } 

I am now building a class called Pilot that I will map that data to so the user can change the values that they want to and then write that back to the file on disk. There are a lot of individual values available for editing. I'm mapping them in one by one. The code is getting rather long and repetitive and it's got me thinking their might be a more efficient way of doing the mapping. Any suggestions?

This is what my Pilot class looks like so far:

class Pilot { // Pilot data fields public string PltName { get; set; } public Health PltHealth { get; set; } public Rank PltRank { get; set; } public uint PltTODScore { get; set; } public uint PltRookieNum { get; set; } //experience level - rookie to top ace public int PltXWingTrainingCompleted { get; set; } public int PltYWingTrainingCompleted { get; set; } public int PltAWingTrainingCompleted { get; set; } public int PltBWingTrainingCompleted { get; set; } public uint PltXWingTrainingScore { get; set; } public uint PltYWingTrainingScore { get; set; } public uint PltAWingTrainingScore { get; set; } public uint PltBWingTrainingScore { get; set; } public bool PltXWingHistComplete1 { get; set; } public bool PltXWingHistComplete2 { get; set; } public bool PltXWingHistComplete3 { get; set; } public bool PltXWingHistComplete4 { get; set; } public bool PltXWingHistComplete5 { get; set; } public bool PltXWingHistComplete6 { get; set; } public bool PltYWingHistComplete1 { get; set; } public bool PltYWingHistComplete2 { get; set; } public bool PltYWingHistComplete3 { get; set; } public bool PltYWingHistComplete4 { get; set; } public bool PltYWingHistComplete5 { get; set; } public bool PltYWingHistComplete6 { get; set; } public bool PltAWingHistComplete1 { get; set; } public bool PltAWingHistComplete2 { get; set; } public bool PltAWingHistComplete3 { get; set; } public bool PltAWingHistComplete4 { get; set; } public bool PltAWingHistComplete5 { get; set; } public bool PltAWingHistComplete6 { get; set; } public bool PltBWingHistComplete1 { get; set; } public bool PltBWingHistComplete2 { get; set; } public bool PltBWingHistComplete3 { get; set; } public bool PltBWingHistComplete4 { get; set; } public bool PltBWingHistComplete5 { get; set; } public bool PltBWingHistComplete6 { get; set; } public Pilot() { PltName = "No Pilot File Loaded"; PltHealth = Health.Alive; PltRank = Rank.Cadet; PltTODScore = 0; PltRookieNum = 0; PltXWingTrainingCompleted = 0; PltYWingTrainingCompleted = 0; PltAWingTrainingCompleted = 0; PltBWingTrainingCompleted = 0; PltXWingTrainingScore = 0; PltYWingTrainingScore = 0; PltAWingTrainingScore = 0; PltBWingTrainingScore = 0; PltXWingHistComplete1 = false; PltXWingHistComplete2 = false; PltXWingHistComplete3 = false; PltXWingHistComplete4 = false; PltXWingHistComplete5 = false; PltXWingHistComplete6 = false; PltYWingHistComplete1 = false; PltYWingHistComplete2 = false; PltYWingHistComplete3 = false; PltYWingHistComplete4 = false; PltYWingHistComplete5 = false; PltYWingHistComplete6 = false; PltAWingHistComplete1 = false; PltAWingHistComplete2 = false; PltAWingHistComplete3 = false; PltAWingHistComplete4 = false; PltAWingHistComplete5 = false; PltAWingHistComplete6 = false; PltBWingHistComplete1 = false; PltBWingHistComplete2 = false; PltBWingHistComplete3 = false; PltBWingHistComplete4 = false; PltBWingHistComplete5 = false; PltBWingHistComplete6 = false; } public void GetData(string fileName, byte[] bytes) { PltName = Path.GetFileNameWithoutExtension(fileName); PltHealth = (Health)bytes[2]; PltRank = (Rank)bytes[3]; PltTODScore = BitConverter.ToUInt32(bytes,4); PltRookieNum = BitConverter.ToUInt16(bytes, 8); PltXWingTrainingCompleted = bytes[134]; PltYWingTrainingCompleted = bytes[135]; PltAWingTrainingCompleted = bytes[136]; PltBWingTrainingCompleted = bytes[137]; PltXWingTrainingScore = BitConverter.ToUInt32(bytes, 38); PltYWingTrainingScore = BitConverter.ToUInt32(bytes, 42); PltAWingTrainingScore = BitConverter.ToUInt32(bytes, 46); PltBWingTrainingScore = BitConverter.ToUInt32(bytes, 50); PltXWingHistComplete1 = Convert.ToBoolean(bytes[544]); PltXWingHistComplete2 = Convert.ToBoolean(bytes[545]); PltXWingHistComplete3 = Convert.ToBoolean(bytes[546]); PltXWingHistComplete4 = Convert.ToBoolean(bytes[547]); PltXWingHistComplete5 = Convert.ToBoolean(bytes[548]); PltXWingHistComplete6 = Convert.ToBoolean(bytes[549]); PltYWingHistComplete1 = Convert.ToBoolean(bytes[560]); PltYWingHistComplete2 = Convert.ToBoolean(bytes[561]); PltYWingHistComplete3 = Convert.ToBoolean(bytes[562]); PltYWingHistComplete4 = Convert.ToBoolean(bytes[563]); PltYWingHistComplete5 = Convert.ToBoolean(bytes[564]); PltYWingHistComplete6 = Convert.ToBoolean(bytes[565]); PltAWingHistComplete1 = Convert.ToBoolean(bytes[576]); PltAWingHistComplete2 = Convert.ToBoolean(bytes[577]); PltAWingHistComplete3 = Convert.ToBoolean(bytes[578]); PltAWingHistComplete4 = Convert.ToBoolean(bytes[579]); PltAWingHistComplete5 = Convert.ToBoolean(bytes[580]); PltAWingHistComplete6 = Convert.ToBoolean(bytes[581]); PltBWingHistComplete1 = Convert.ToBoolean(bytes[592]); PltBWingHistComplete2 = Convert.ToBoolean(bytes[593]); PltBWingHistComplete3 = Convert.ToBoolean(bytes[594]); PltBWingHistComplete4 = Convert.ToBoolean(bytes[595]); PltBWingHistComplete5 = Convert.ToBoolean(bytes[596]); PltBWingHistComplete6 = Convert.ToBoolean(bytes[597]); } } 

by LordDrakken via /r/csharp

Leave a Reply