Writing Code
Before we get into the actual code writing I want to discuss a few things. Most of them are just tips to make things easier and more readable, especially if you're not the only one working on it.
Naming Conventions
This is simply how you name things. Function names should be pretty obvious and easy to read. If a function reads hex data call it something like ReadHex or HexRead. Variable names should be just as obvious and should tell you what type of variable it is just by the name.
- Arrays - start with a (aArray)
- Strings - start with s (sString)
- Integer - start with i (iInteger)
- File - start with f(no floats) or fi(floats)(fFile, fiFile)
- Pointers - start with p (pPointer)
- Double - start with d (dDouble)
- Edit Boxes - start with edt (edtEdit)
- Text boxes/Inputs - start with txt (txtInput)
- Buttons - start with btn (btnButton)
- Sliders - start with sld (sldSlider)
- Combo box(dropdown menu) - start with cmb (cbmCombo)
- Menu - start with mnu (mnuMenu)
- List boxes/TreeViews - start with lst (lstTreeview)
- Checkboxes - start with chk (chkCheckbox)
- Radio button - start with rdo (rdoRadio)
- Labels - start with lbl (lblLabel)
- Groups - start with grp (grpGroup)
This will make your code more readable to an extent, and if you break things apart properly that will make it even easier.
Readability:
You always want your code to be readable, if for no other reason than you don't want to forget what something does. If you are working with another coder you definitely want to have readable code, otherwise there will be a lot of time wasted trying to figure out what portions of code do when it would be obvious if it were more readable.
Comments are a good way to improve readability of code, as is breaking code into chunks. Breaking code up should have a negligible effect on speed, if any at all. Here's an example to illustrate my point.
Originally Posted by bunched
Autoit:
Code:C++:Code:$sData = Bin(Hex(Asc("a")))
Code:Code:binaryWriter->Write(binaryReader->ReadByte());I know these examples aren't great but they do illustrate my point. In the first it's a bit jumbled and somewhat hard to understand what's going on immediately. In the readable version it's clear as to what is happening in the code and in what order. Order does play a key role in coding so it's important to always know what order things happen in. In the autoit example we are taking the ascii value of "a", converting that to hex, then converting that to binary. The example is fairly easy to read but there will be times when things are nested (get to that in a moment) in such a way that it's almost impossible to read. That's why you want to break the code apart, so as to avoid confusion for yourself and anyone else reading your code.Originally Posted by readable
Autoit:
Code:C++:Code:$sData = Asc("a") $sData = Hex($sData) $sData = Bin($sData)
Code:Code:sByte = binaryReader->ReadByte(); binaryWriter->Write(sByte);
Nested Statements:
Nested statements are basically just statements within other statements. A for loop inside a while loop, a function calling another function (this one happens a lot), etc. More than likely you will use nested statements at some point. This is fine as long as you maintain readability. Always make sure you keep your indentations consistent, a nested statement should be indented one place more than the previous statement. If you have a statement that has 3 parts, 2 nested, there should be 2 indentations. This (broken record :P) maintains readability and makes it simpler to find the portion of code you're looking for. Most compilers/editors do this for you but sometimes not, just make sure everything is there.
Writing Functions:
As I've said before you want your functions to do only one thing, and do it well. Sometimes it's a bit difficult to separate different portions of code into functions but it helps. If you just need part of the code later on it's much better to have it in its own function than to have to run through an entire other function to get to the part you need. This improves speed and efficiency. Take your time, plan out how you want things to flow, and if nothing else wing it. Try different ideas and see what works and what doesn't. I know there are others out there who do their best work when semi-conscious or fully asleep (yes I dream about coding sometimes -_-) but that allows you to think of solutions you normally never would. If you can't get past a roadblock try taking a nap or just plain sleeping. A fresh set of eyes will help with projects, sometimes you need to ask a new coder to look over it and sometimes you need a veteran. Since new coders don't know much yet they often only find obvious errors that we overlook such as an error in syntax or a typo. Veteran coders know a lot and they can help us think of new solutions as well as possibly tell us why what we try doesn't work. When you finally get a function working, look it over. See if there's a way to break it down into even smaller functions.
more soon
Last edited by flyers2114; 03-23-2012 at 04:34 PM.
Galaxy S2 Skyrocket
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:
$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."Code:$nMsg = GUIGetMsg() Switch $nMsg Case $btnChange HexEdit($data) EndSwitch
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.
Last edited by flyers2114; 03-23-2012 at 04:39 PM.
Galaxy S2 Skyrocket
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* NextThis 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* WEndChecks 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 iCountExecutes until $iCount equals 48Code:Do *do something* Until $iCount = 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*Reads the file's contents into a variable named sFileDataCode:$sFileData = FileRead($fFile)
Write to a file: *uses the filehadle created earlier*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.Code:FileWrite($fFile,$sData)
Open a file in binary mode: *since we're modders we'll need the hex of the file so this will help a lot*Opens the file in binary read-only mode. The binary is represented as hex. This will give us the hex data.Code:$fFile = FileOpen("*savegame*",16)
Read one byte from an offset: *uses filehandle created earlier*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".Code:FileSetPos($fFile,Dec(*offset*)) $sOffsetData = FileRead($fFile,1)
Last edited by flyers2114; 03-23-2012 at 09:42 PM.
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:CWCheat Database Fixer: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; } } }AutoitCode: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); } }
CWCheat Database Fixer: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.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)
Last edited by VenumX; 03-25-2012 at 02:27 AM.
reserved in case
cause I need this also