Welcome to the Builder Academy

Question Patch File How-to

More
10 Jun 2012 22:46 - 11 Jun 2012 03:33 #9 by Rumble
Patch File How-to was created by Rumble

Attachment hand_patching.txt not found


By Juras, 2005

Lets take a quick look at a patch file, also known as a diff file.

I started out using cygwin as well, and there's very little difference between it and linux as far as a newbie coder is concerned.

I am going to be using the event_combat.patch file as an example, and see what we can do.

First off, I want to make sure that everyone knows what a patch file is. When someone is doing some code work that they think other people will want to have as well, they start off by doing something that everyone should do anyway. And that is making a backup of your current code and putting it somewhere safe.

If using linux, or using the cygwin shell, then you can do this by typing:

cp -r src ../backupsrc

This will copy your src file then paste it one level up in a new file called backupsrc. After making a backup, a coder will go about making his changes, perfecting them (lol yeah right :P ), debugging them, and making them work in a way that will work well with the mud.

After getting a good compilation, and the coder feels that it is ready for release, the coder will then copy the src once again, and use the diff command to create a text file that is just a written comparison of the backupsrc, and the changed src.

Now lets look at the beginning of a diff output. If instead of just patching in a diff file using the patch command, you open it up using wordpad, or vi, you will find that it is just an ordinary text file.
Code:
diff -BbuprN -x '*.o' oldsrc/act.wizard.c src/act.wizard.c --- oldsrc/act.wizard.c 2005-05-29 13:50:19.000000000 -0400 +++ src/act.wizard.c 2005-06-14 23:13:57.000000000 -0400 @@ -2777,6 +2777,7 @@ ACMD(do_show) { "maxki", ADMLVL_GRGOD, BOTH, NUMBER }, { "ki", ADMLVL_GRGOD, BOTH, NUMBER }, { "adminlevel", ADMLVL_IMPL, PC, NUMBER }, + { "speed", ADMLVL_GOD, BOTH, NUMBER }, { "\n", 0, BOTH, MISC } };

The first line is showing which files were used as a comparison. The diff command checks for differences in files, and when it finds a difference it writes it out like this. In this example, you can tell from the first line that whoever made this diff file was using oldsrc as his backup, and using src as his modified.

You can also tell from the first line that we will find the first set of changes in the act.wizard.c file, and on the fourth line you can see that the changes will be around line 2777.

Any place that has a plus sign is something that is going to need to be added in, and anything with a minus sign is something that will need to be removed. In this example we want to open up the act.wizard.c file either using vi, or using wordpad. Then scroll down to line 2777, and find the line that shows the adminlevel part. Then just copy the entire "spee" line, except for the plus sign, and put it one line below the adminlevel part.

The next part of the patch, or diff file, will show what we need to do next in the act.other.c file, so keep it open.
Code:
{ "adminlevel", ADMLVL_IMPL, PC, NUMBER }, + { "speed", ADMLVL_GOD, BOTH, NUMBER }, { "\n", 0, BOTH, MISC } }; @@ -3149,6 +3150,10 @@ int perform_set(struct char_data *ch, st admin_set(vict, value); break; + case 59: + GET_SPEED(vict) = RANGE(0, 50); + break; + default: send_to_char(ch, "Can't set that!\r\n"); return (0); @@ -3280,11 +3285,13 @@ ACMD(do_peace)

Right below the lines that we were just looking at to find our first change, you find this line:
@@ -3149,6 +3150,10 @@ int perform_set(struct char_data *ch, st

This line shows that we now need to go to lines 3149, and 3150 and take a look around. Somewhere around those lines, there is going to be a case 58, and these lines:

admin_set(vict, value);
break;

and right below it, we will need to add in the lines with the plus signs again. Make sure that you are not putting the plus signs themselves, they are just there to show you what needs to be added, or removed. Notice the bottom line is in the same format as well, showing that we will now need to go to lines 3280, to 3285, and check stuff out.
Code:
@@ -3280,11 +3285,13 @@ ACMD(do_peace) for (vict = world[IN_ROOM(ch)].people; vict; vict = next_v) { next_v = vict->next_in_room; - if (GET_ADMLEVEL(vict) >= GET_ADMLEVEL(ch)) - continue; +// if (GET_ADMLEVEL(vict) >= GET_ADMLEVEL(ch)) +// continue; + if (GET_FIGHTING(vict)) stop_fighting(vict); GET_POS(vict) = POS_SITTING; } + if (GET_FIGHTING(ch)) stop_fighting(ch); GET_POS(ch) = POS_STANDING;

See the minus sign? that is showing a line that needs to be removed. But, if you take a close look, you will notice that right below the minus signs are plus signs showing the same lines where the only addition, are two forward slashes commenting out the lines. What this tells you is that the person who wrote the code for this, commented out these lines, and the diff command cant write out changes to lines, it can only add or remove files. What this also means, is that if they went so far as to comment it out, then it is no longer needed, but that for some reason they felt that it might be needed later on in the future. So instead of removing the lines, and adding in whole new lines using the slashes, we could just put a "/*" at the beginning of the first line (without the quotes by the way), and put a "*/" at the end of the second line. This will also comment both lines, and in all actuality makes no difference at all, as long as the specified lines get commented out in some way.
Code:
GET_POS(vict) = POS_SITTING; } + if (GET_FIGHTING(ch)) stop_fighting(ch); GET_POS(ch) = POS_STANDING;

This set of lines might be a bit confusing, but it shows that in between the GET_POS lines, we need to add in an if check to see if someone is fighting or not. Pretty simple if you look at it by itself.
Code:
GET_POS(vict) = POS_SITTING; } + if (GET_FIGHTING(ch)) stop_fighting(ch); GET_POS(ch) = POS_STANDING; } diff -BbuprN -x '*.o' oldsrc/comm.c src/comm.c --- oldsrc/comm.c 2005-05-21 09:49:41.000000000 -0400 +++ src/comm.c 2005-06-14 23:12:33.000000000 -0400 @@ -169,7 +169,6 @@ void reboot_wizlists(void); void boot_world(void); void affect_update(void); /* In magic.c */ void mobile_activity(void); -void perform_violence(void);

This bit of the file is where we just added the if check, and what was right below it. Notice that it has another spot where it is showing diff and the format that the diff patch was made. Thats the "diff -BbuprN -x" part. but this shows that we are now done with act.wizard.c, and now need to move on to comm.c and go from there.

That's basically all there is to patch files, and handpatching. I highly suggest hand patching to everyone, as it makes you more familiar with the code that you are adding in, and also makes you more familiar with coding altogether if you are new to things. I hope this helps out a couple of people, and anyone that has any questions can feel free to ask me. Using cygwin is easy for newbies, since it stays on the computer that you are used to working on, and makes you familiar to the linux environment, before actually using linux remotely. Just remember that using cygwin, you won't be able to create other users that can log on remotely and help you out.

Thanks,
Shaun
Juras
Selorien@hotmail.com
Shaun@Mystic-Empire.com

Rumble
The Builder Academy
tbamud.com 9091
rumble@tbamud.com
Attachments:
Last edit: 11 Jun 2012 03:33 by Rumble.
The following user(s) said Thank You: Sascha

Please Log in or Create an account to join the conversation.

More
10 Jun 2012 22:50 - 10 Jun 2012 22:52 #10 by Rumble
Replied by Rumble on topic Re: Patch File How-to
By Maclir, 2004

For people providing various patch files for others to use, I would suggest the following format:

1) Use the "unified output" format.
2) Include the c function name.

This makes it much easier for those of us that have to hand patch your changes.

A simple way to ensure this is with:
Code:
diff -BbuprN -x *.o suntzu/src/ suntzu-test/src/ > my-wonderful-changes.patch

This assumes you are at the directory that contains both the (stock) suntzu distribution, and your test version (suntzu-test). What I normally do before working on a patch, is to create a copy of the latest stock code with the command:
Code:
cp -r suntzu suntzu-test

These are assuming you are using a unix / cygwin environment.

Thanks
Ken

Rumble
The Builder Academy
tbamud.com 9091
rumble@tbamud.com
Last edit: 10 Jun 2012 22:52 by Rumble.

Please Log in or Create an account to join the conversation.

More
11 Jan 2013 10:18 #1271 by Juras
Replied by Juras on topic Re: Patch File How-to
Seeing this makes me feel all warm and fuzzy inside.

Glad to see it's still helping people out.
The following user(s) said Thank You: zusuk, Sascha

Please Log in or Create an account to join the conversation.

Time to create page: 0.197 seconds