Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Introduction to Hexing, Programming, and General Game Modding

OP dbzfanatic

19th March 2012, 09:26 PM   |  #11  
dbzfanatic's Avatar
OP Senior Member
Thanks Meter: 282
 
1,063 posts
Join Date:Joined: Jun 2010
Donate to Me
More
Tying Code and GUI Together

Ok now that we have our code working it's time to make it look pretty. Some people can use a console fairly well,some can barely click a button lol. We want our programs to be as user-friendly as possible, thus we need a GUI. This should actually be the easiest part of the project since we already have our code. As I said before the code should be in functions so it's just a matter of tying those functions into events. Whenever a user does anything and I mean anything it triggers an event, be it clicking or moving a mouse, pressing a key, or sometimes even just sneezing on the screen (or maybe that's just me lol). We only need to capture certain events such as a button being clicked or a slider being moved usually so that's what I'll focus in terms of controls.

Designing the GUI
First thing we do is open up our favorite form designer. There are designers for most languages so if you don't have one just google, you're bound to find one. Design your GUI in this (place buttons, sliders, text boxes, labels, etc) then get the code for it. Make sure when you're designing your GUI anything that needs to be interacted with (buttons, sliders, etc) have an event set off when they're clicked/changed. Most form designers have an option to import the code directly into your editor/compiler so just do that if you can.

Integrating Functions and Events
Ok now that we have our code and our GUI code we can put the two together. Since all our important stuff is in functions and we can call it whenever we need all we need to do is put our function calls inside our event triggers. Here's an example in autoit of code that calls our custom function, called "HexEdit" when a button named "$btnChange" is clicked. Now it's named $btnChange but it will show Change on the button itself, don't get those confused it'll screw you up.
Code:
$nMsg = GUIGetMsg()
    Switch $nMsg
        Case $btnChange
            HexEdit($data)
    EndSwitch
$data is just a variable passed as a parameter to the function. Ok whose head just exploded, be honest. Raise your hand. Ok now go get a towel then talk to me after it grows back. For those of you still able to think all this code does is say "when a user clicks a button named $btnChange call the function HexEdit and send $data to that function."
Our function "HexEdit" should take the data it gets from $data and use that to do whatever it needs to do. Say it edits our gold, $data should be the value we want our gold at. We send $data (which has a value of, say, 10k) and in turn HexEdit() automatically edits our save file to give us 10k gold. Simple in principle yeah?


The most time-consuming portion of the GUI phase is designing the GUI. We all want our programs to look good so we'll probably spend a fair amount of time designing. I've found that doing a rough draft in the form designer then editing values in the code is a good way to make things look neat. I like to do things in increments of 5. If something has a value of 173 I'll make it 175, if it's 289 I'll make it 290, etc. Things line up well and it seems more professional, plus it's easier than trying to get it perfect in the form designer. Try to have an idea of how you want it to look before you start designing, that way you're not making it up and it looks even better. Really integration of the code and GUI is simple, once both portions are done. If you plan on adding features later you might want to add the appropriate controls to the editor and just disable them until their functions are working. It gives users a bit of a teaser of what's to come.
19th March 2012, 09:26 PM   |  #12  
dbzfanatic's Avatar
OP Senior Member
Thanks Meter: 282
 
1,063 posts
Join Date:Joined: Jun 2010
Donate to Me
More
Code Examples

Now most of these will be in AutoIt since it's my preferred language but I will post some code in other languages as well if I know the conversions off-hand. Most of these will be snippets (small pieces of code that do something specific).

For loop: *assuming you have an integer variable declared that's named iCount*
  • AutoIt
    Code:
    For $iCount = 1 to 6
    *do something here*
    Next
    This loops the code 6 times, each time doing what's between "For" and Next. It also adds 1 to $iCount.
  • C++
    Code:
    for(iCount = 1, iCount = 6, iCount++){
    *do something here*;
    }
    Same as above


While loop: *assuming you have an integer variable declared that's named iCount
  • AutoIt
    Code:
    While $iCount < 12
    *do something*
    WEnd
    Checks if $iCount is less than 12 and if so executes
  • C++
    Code:
    While(iCount < 12){
    *do something*;
    }
    Same as above


Do loop: *assuming you have an integer variable declared that's named iCount
Code:
Do
*do something*
Until $iCount = 48
Executes until $iCount equals 48

Open a file:
  • AutoIt
    Code:
    $fSave = FileOpen("*savegame*")
    Creates a filehandle to a file that can be used with other File* functions and opens in read-only mode
  • C++
    Code:
      FILE * fFile;
      fFile = fopen ("*savegame*","r");
    Same as above


Read a file: *uses the filehandle created earlier*
Code:
$sFileData = FileRead($fFile)
Reads the file's contents into a variable named sFileData

Write to a file: *uses the filehadle created earlier*
Code:
FileWrite($fFile,$sData)
Writes the string $sData to the file, either appending it (adding it to the end) or replacing the file's entire contents depending on the mode used to open the file.

Open a file in binary mode: *since we're modders we'll need the hex of the file so this will help a lot*
Code:
$fFile = FileOpen("*savegame*",16)
Opens the file in binary read-only mode. The binary is represented as hex. This will give us the hex data.

Read one byte from an offset: *uses filehandle created earlier*
Code:
FileSetPos($fFile,Dec(*offset*))
$sOffsetData = FileRead($fFile,1)
Goes to the offset (converted from hex to decimal with Dec) and reads one byte. Number of bytes read can be changed by changing the "1".

Will add more as needed/as I think of them
19th March 2012, 09:26 PM   |  #13  
dbzfanatic's Avatar
OP Senior Member
Thanks Meter: 282
 
1,063 posts
Join Date:Joined: Jun 2010
Donate to Me
More
More Code Examples:

Since I'm working on a project with another member here that requires me learning C# I thought I'd post some of my practice code. It's all functional.

C#
Common Functions:
Code:
using System.Windows.Forms;
using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;

namespace Common
{
    public class CommonFunctions{

        public static void MsgBox(string sMessage, string sCaption = "Error", MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon Icon = MessageBoxIcon.Exclamation, IWin32Window owner = null)
        {
            MessageBox.Show(owner, sMessage, sCaption, buttons, Icon);
        }

        public static void Test(params string[] sInput)
        {
            for (int i = 0; i < sInput.Length; i++)
            {
                Console.WriteLine(sInput[i]);
            }
        }

        public static void Sleep(int iMilliseconds)
        {
            Thread.Sleep(iMilliseconds);
        }

        public static string StringStripCR(string sInput)
        {
            string sNewString = sInput.Replace(Convert.ToString(Convert.ToChar(10)), "");
            sNewString = sNewString.Replace(Convert.ToString(Convert.ToChar(13)), "");
            return sNewString;
        }

        public static string[] DriveGetDrive(string sType){
            DriveInfo[] ListDrives = DriveInfo.GetDrives();
            List<string> lList = new List<string>();
            DriveType dType = new DriveType();
            int iDriveNum = 0;
            lList.Add(Convert.ToString(iDriveNum));

            if((sType == "removable") || (sType == "Removable")){
                dType = DriveType.Removable;
            }
            else if((sType == "network") || (sType == "Network")){
                dType = DriveType.Network;
            }
            else if ((sType == "Fixed") || (sType == "fixed"))
            {
                dType = DriveType.Fixed;
            }
            else if ((sType == "CDRom") || (sType == "cdrom") || (sType == "CDrom"))
            {
                dType = DriveType.CDRom;
            }

            foreach (DriveInfo sDrive in ListDrives)
            {
                if(sDrive.DriveType.Equals(dType))
                {
                    string sDriveString = Convert.ToString(sDrive);
                    lList.Add(sDriveString.Remove(sDriveString.Length - 1,1));//Add to RemovableDrive list
                }    
            }
            lList[0] = Convert.ToString(lList.Count - 1);
            string[] aDrives = lList.ToArray();
            
            return aDrives;
        }
    }
}
CWCheat Database Fixer:
Code:
using Common;
using System.IO;
using System;

public class MainRun:CommonFunctions
{

    static void Main()
    {
        string path = "";
        string[] aDrives = DriveGetDrive("removable");
        int iMax = Convert.ToInt16(aDrives[0]);

        for(int i = 2;i <= iMax; i++){
            if (Directory.Exists(aDrives[i] + "\\seplugins\\"))
            {
                path = aDrives[i] + "\\seplugins\\cwcheat\\cheat.db";
            }
            
        }

        if((path==""))
        {
            MsgBox("No database detected\n\nExiting...");
            Environment.Exit(0);
        }

        string sFileContents = File.ReadAllText(path);
        string sCleared = StringStripCR(sFileContents);
        sCleared = sCleared.Replace("_", "\r\n_");
        sCleared = sCleared.Remove(0, 1);
        File.WriteAllText(path, sCleared);
    }
}
Autoit
CWCheat Database Fixer:
Code:
#include <File.au3>

$path = ""

$drives = DriveGetDrive("removable")
for $i = 2 to $drives[0]
    if FileExists($drives[$i] & "\seplugins") Then
        $path = $drives[$i] & "\seplugins\cwcheat\cheat.db"
    EndIf
Next

if $path = "" Then
    MsgBox(48,"Error","No database detected." & @CRLF & @CRLF & "Exiting...")
    Exit
EndIf

$file = FileOpen($path,0)
$data = StringReplace(StringReplace(StringStripCR(FileRead($file)),"_", @CRLF & "_"),@cr,"",1)
$file = FileOpen($path,2)
FileWrite($file,$data)
Now the two DB fixers work exactly the same in exactly the same way with exactly the same result. Oddly enough the c# version was slower on my system than the autoit version, but considerably smaller. It may just be something quirky with my system but I thought I'd let you know. Since they work the same it's easy to look at the code and "translate" between the two.
19th March 2012, 09:28 PM   |  #14  
dbzfanatic's Avatar
OP Senior Member
Thanks Meter: 282
 
1,063 posts
Join Date:Joined: Jun 2010
Donate to Me
More
This was quoted from a buddy of mine.

Quote:
Originally Posted by Hipolipolopigus' timestamp='1295080083' post='7718

Just thought I'd put this up;

Instead of doing a whole bunch of if else if else... You can "cheat" :P

Original;

Code:
                        if((sType == "removable") || (sType == "Removable")){
                                dType = DriveType.Removable;
                        }
                        else if((sType == "network") || (sType == "Network")){
                                dType = DriveType.Network;
                        }
                        else if ((sType == "Fixed") || (sType == "fixed"))
                        {
                                dType = DriveType.Fixed;
                        }
                        else if ((sType == "CDRom") || (sType == "cdrom") || (sType == "CDrom"))
                        {
                                dType = DriveType.CDRom;
                        }
using "switch" and "case";
Code:
            switch (sType.ToLower())//use .ToLower() to handle the string as all lowercase
            {
                case "removable": dType = DriveType.Removeable; break;
                case "network": dType = DriveTypeNetwork; break;
                case "fixed": dType = DriveType.Fixed; break;
                case "cdrom": dType = DriveType.CDRom; break;
                default: throw new Exception("Invalid type!");
            }
As you can see, it saves code space and looks tidier

30th September 2012, 09:00 AM   |  #15  
dbzfanatic's Avatar
OP Senior Member
Thanks Meter: 282
 
1,063 posts
Join Date:Joined: Jun 2010
Donate to Me
More
In case you guys were afraid feel free to ask any questions or make requests for examples here the worst that'll happen is I'll say "I don't know how to do that :/" and it'll allow someone who does know how know they need to post an example
Kspalahyia
4th March 2013, 12:44 PM   |  #16  
Guest
Thanks Meter: 0
 
n/a posts
Re: Introduction to Hexing, Programming, and General Game Modding
Hi is this thread to mod an apk??

Sent from my GT-N7100 using xda premium
4th March 2013, 01:52 PM   |  #17  
Senior Member
Flag Nashik
Thanks Meter: 506
 
4,336 posts
Join Date:Joined: Aug 2012
More
Quote:
Originally Posted by Kspalahyia

Hi is this thread to mod an apk??

Sent from my GT-N7100 using xda premium

cant you read the OP? 10 poster. use search button for modding apk.

@OP

this is what i have always wanted. i tried learning hex-editing on .gba modifying sites for games but was never able to do it. will get back at this guide after exams. Thanks no thanks button in OT forum
Kspalahyia
4th March 2013, 01:55 PM   |  #18  
Guest
Thanks Meter: 0
 
n/a posts
Re: Introduction to Hexing, Programming, and General Game Modding
Just confirming... Must do this after my exams....

Sent from my GT-N7100 using xda premium
4th March 2013, 02:26 PM   |  #19  
Gogeta's Avatar
Senior Member
Flag Gingertown
Thanks Meter: 817
 
6,524 posts
Join Date:Joined: Oct 2012
More
Re: Introduction to Hexing, Programming, and General Game Modding
THIS program compiled successfully correctly but output is wrong ...
Can you help me??

Quote:

#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
float a,b;
char d;
printf("enter two variables");
scanf("%f%f",&a,&b);
printf("enter operator");
scanf("%c",&d);
switch (d)
{
case '+' : printf("%f",a+b); break;
case '-' : printf ("%f",a-b); break;
case '/': printf ("%f",a/b); break;
case '*' : printf ("%f",a*b); break;
default : printf ("try again!!!");
}
getch();
}

wanna thank xda? here
Kspalahyia
14th March 2013, 10:42 PM   |  #20  
Guest
Thanks Meter: 0
 
n/a posts
Re: Introduction to Hexing, Programming, and General Game Modding
Wowa I learned that thanks...

Sent from my GT-N7100 using xda premium

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes