Programmers Guide to Learning Hex(Part 2) - Programming

Discussion in 'Programming & Scripting' started by GoldBl4d3, Dec 28, 2008 with 0 replies and 334 views.

  1. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    In this tutorial I will be teaching you some of the different ways to program with hex. What I mean by this is, reading a value such as an int32 or a string from a file. Like I said in the previous tutorial. Files are made up of bytes, hex editors are just a simple way of viewing those bytes.




    Now lets start. For this tutorial you will need ALL of the following complete.
    Next, add the following controls to the designer surface of your form.
    • 1 Button - rename to "Open Map"
    • 2 TextBoxs
    • 1 Label
    Configure the controls as seen in the image below:

    [Click here to view the link]

    Next, double click the button. If done correctly, you should have a screen simular to this:

    [Click here to view the link]

    Now time to add some code in.

    Before we can start, we need to add a line of code to the very top, this line of code lets us know that we will be using a certain system. Add the code that is boxed in the image below. Make sure that that boxed code is where it appears in the image.

    [Click here to view the link]

    This line of code tells C# that we will be using the Input/Output System.
    -IO is used to read and write data

    Now lets begin,

    First we have to make an open-file-dialog so we can find the path of the map we want to mod.

    We must first declare a new OpenFileDialog named "ofd"

    [Click here to view the link]

    Next we must set some properties about that dialogBox that will be displayed

    First we want to set the filename to be a default value of blank

    [Click here to view the link]

    Next we want to set the filter, so that only .map files can be seen

    [Click here to view the link]

    Finally, we want to show this dialog. But if the user clicks 'OK' then the code inside the curly braces will be executed.

    [Click here to view the link]

    Now time to get into the juicy stuff. From this point on we will be reading values from the map file. In this tutorial, I will show you have to successfully read the Version, scenario, and internal map name of a map file.

    What do we use to read a value? To answer, we use BinaryReaders. As explained in the previous tutorial. Binary looks somthing like 00101001010101100010101.

    But lets start by setting it up, first we need to declare a new binaryreader named "br"

    [Click here to view the link]

    Next we need to let the new BinaryReader know were the map file is. The map file is read by the binaryreader through a filestream. The binaryreader must also know what kind of access permission is allowed.

    [Click here to view the link]


    **For now on I will only be showing the code inside the button1_click event**

    Now were accessing the map file, the next thing to do is point where the first thing we are going to read is. The first thing to read is the map version. In the hex editor its a position 4 as an Int32.

    [Click here to view the link]

    To goto position 4, we tell the BinaryReaders basestream, to goto Position 4.

    [Click here to view the link]

    next we assign textBox1's text property to the version of the map by reading its int32 value. We MUST use the .ToString() method when doing this to convert a non text object into a text object.

    [Click here to view the link]

    Next we need to know where the positions are for the internal name and the scenario. The internal name is at position 408. The scenario is at position 444.

    So lets start by setting the basestreams position to 408.

    [Click here to view the link]

    Reading is alittle different. We have to read the characters(like strings). But every internal name has a different number of chars in it. So we loop through, reading every char untill we get to a null/blank character.

    [Click here to view the link]

    Yes, looks like we skipped a big step. Well actually we didnt. We read each individual char of the internal name untill be got to a null/blank char. Then we set label1's text property to a local string variable were the chars were stored to when they were being read.

    Next we goto position 444, witch is the scenarios position.

    [Click here to view the link]

    Time to read the scenario. To read the scenario name, its just like the internal name.
    But this time we will set textBox2's text property to the local string variable that the scenario's chars were stored to.

    [Click here to view the link]

    Finally, we need to close the BinaryReader so that other applications can have access to the same file without having to close the application.

    [Click here to view the link]

    There you go, you are finished. Open a map in this program and see the results yourself. If your having problems, then download my source project below.

    The next tutorial will go into reading hex in several different ways.

    Source Project: [Click here to view the link]