Jump to content

Replacing functions with hex editing


Amphibious

Recommended Posts

I wanted to replace a redundant function (one that is responsible for OTS placement on easy and normal) at the end of the XGBase.GenerateTiles object by replacing it with couple of functions that are totaling the same size however it doesn't work.

 

This is the code I wanted to replace (bold part):

 

 

1B DB 2A 00 00 00 00 00 00 2C 09 25 26 16 //SetFacility(n,x,y)

07 BC 04 98 19 1B F5 10 00 00 00 00 00 00 16 //if Game()

0A 00 35 4F 00 00 00 1B CB 11 00 00 00 00 00 00 16 26 16 //.Get Difficulty()<=1

1B DB 2A 00 00 00 00 00 00 2C 0C 2C 06 26 16 //SetFacility(n,x,y)

0F 10 2C 0C 19 1B F5 10 00 00 00 00 00 00 16 //Game()

09 00 F5 4E 00 00 00 01 F5 4E 00 00 26 //m_arrFaciltyUnlocks

1B A2 34 00 00 00 00 00 00 16 //UpdateTiles()

04 0B 53 00 00 00 02 00 02 00 27 11 00 00 00 00 00 00 //object end

 

 

So I replaced bold part with 3 15-bit and 2 16-bit SetFacilty functionsto replace original 77-bit code however it doesn't work and I can't figure out why since think that I figured out the script right. Any thoughts?

Edited by Amphibious
Link to comment
Share on other sites

It should have not only the same file size, but also the same memory size. PatchUPK can recalculate memory and file sizes for you automatically if you use BEFORE_CODE/AFTER_CODE keys inside object scope:

OBJECT=XGBase.GenerateTiles:AUTO
[BEFORE_CODE]
before code written in pseudo-code
[AFTER_CODE]
after code written in pseudo-code
It's important that all object and name references, including null-reference, are written in pseudo-code: in this case patcher will calculate and adjust memory size correctly.

 

You can also adjust memory size manually, see UPK Format Document for details.

Edited by wghost81
Link to comment
Share on other sites

UE Explorer also displays both the memory and file sizes of various hex. UPKmodder includes a unreal hex parser in order to be able to display the same.

 

In case you're wondering how it works, each 4 byte export/import reference adds +4 bytes to the memory size. The filesize is just the count of hex bytes.

 

The original goal for UPKmodder was to update reference values to automate updating after patches (which worked quite well for the EW patch 1 to patch 2 and patch 2 to patch 3 updates). Since it is already computing where all the references are, it's pretty straightforward to dynamically recompute the memory/file positions of each line within a function. For UPKmodder I didn't get around to adding automated tools to compute changed size, since I figured we're doing grungy modding anyhow.

 

There's 2 ways I approach such changes. Either changing a small part of a function, in which case I have to arrange things so that both the memory and file sizes of the code match. Alternatively, resize the function object by altering the header values to adjust the memory and/or file size declared for the function object.

Link to comment
Share on other sites

It should have not only the same file size, but also the same memory size. PatchUPK can recalculate memory and file sizes for you automatically if you use BEFORE_CODE/AFTER_CODE keys inside object scope:

OBJECT=XGBase.GenerateTiles:AUTO
[BEFORE_CODE]
before code written in pseudo-code
[AFTER_CODE]
after code written in pseudo-code
It's important that all object and name references, including null-reference, are written in pseudo-code: in this case patcher will calculate and adjust memory size correctly.

 

You can also adjust memory size manually, see UPK Format Document for details.

 

 

I'm not sure if I understood you. I should write all jump and other similar tokens after // and Patch UPK will caluclate it by itself or?

 

This is the code I wanted to enable, basically it should remove OTS from lower level difficulty and enable anyone to design first level of the base to their liking by altering few entries:

 

 

UPK_FILE=xcomstrategygame.upk

 

[bEFORE_HEX]

F9 28 00 00 E0 25 00 00 00 00 00 00 F2 28 00 00 00 00 00 00 00 00 00 00 F9 28 00 00 00 00 00 00 D7 00 00 00 FA 16 00 00 C9 04 00 00 79 03 00 00 0F 00 F6 28 00 00 92 26 A7 12 20 2E FE FF FF 09 00 4D FB FF FF 00 02 4D FB FF FF 16 16 54 01 C9 28 00 00 90 2C 05 2C 07 16 16 54 01 CA 28 00 00 90 2C 05 2C 07 16 16 0F 00 F8 28 00 00 25 07 33 03 96 00 F8 28 00 00 2C 05 16 0F 00 F9 28 00 00 25 07 25 03 96 00 F9 28 00 00 2C 07 16 07 D3 00 9A 00 F8 28 00 00 25 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 03 06 91 02 07 81 01 9A 00 F8 28 00 00 26 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 03 07 7E 01 82 97 00 F9 28 00 00 91 2C 07 2C 02 16 16 18 0D 00 1B 36 2A 00 00 00 00 00 00 2C 4B 16 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 25 06 91 02 07 DF 01 1B 7B 17 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 03 06 91 02 07 2D 02 1B 36 2A 00 00 00 00 00 00 2C 14 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 02 06 91 02 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 25 55 00 F7 28 00 00 1D 00 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 16 0F 35 1B 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 00 F9 28 00 00 0F 35 1A 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 00 F8 28 00 00 A5 00 F9 28 00 00 16 06 75 00 A5 00 F8 28 00 00 16 06 5A 00 0F 00 F4 28 00 00 25 07 E0 03 96 00 F4 28 00 00 00 F6 28 00 00 16 0F 00 F5 28 00 00 A7 36 00 F7 28 00 00 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 10 00 F5 28 00 00 00 F7 28 00 00 01 C9 28 00 00 26 55 01 C8 28 00 00 14 00 10 00 F5 28 00 00 00 F7 28 00 00 16 40 00 F7 28 00 00 00 F5 28 00 00 26 16 A5 00 F4 28 00 00 16 06 3E 03 1B DB 2A 00 00 00 00 00 00 26 25 25 16 1B DB 2A 00 00 00 00 00 00 2C 03 2C 02 25 16 1B DB 2A 00 00 00 00 00 00 2C 02 2C 04 25 16 1B DB 2A 00 00 00 00 00 00 2C 04 2C 06 25 16 1B DB 2A 00 00 00 00 00 00 2C 06 2C 03 26 16 0F 35 17 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 2C 03 26 16 01 C9 28 00 00 26 1B DB 2A 00 00 00 00 00 00 2C 09 2C 02 26 16 07 BC 04 98 19 1B F5 10 00 00 00 00 00 00 16 0A 00 35 4F 00 00 00 1B CB 11 00 00 00 00 00 00 16 26 16 1B DB 2A 00 00 00 00 00 00 2C 0C 25 26 16 0F 10 2C 0C 19 1B F5 10 00 00 00 00 00 00 16 09 00 F5 4E 00 00 00 01 F5 4E 00 00 26 1B A2 34 00 00 00 00 00 00 16 04 0B 53 00 00 00 02 00 02 00 27 11 00 00 00 00 00 00

 

[AFTER_HEX]

F9 28 00 00 E0 25 00 00 00 00 00 00 F2 28 00 00 00 00 00 00 00 00 00 00 F9 28 00 00 00 00 00 00 D7 00 00 00 FA 16 00 00 C9 04 00 00 79 03 00 00 0F 00 F6 28 00 00 92 26 A7 12 20 2E FE FF FF 09 00 4D FB FF FF 00 02 4D FB FF FF 16 16 54 01 C9 28 00 00 90 2C 05 2C 07 16 16 54 01 CA 28 00 00 90 2C 05 2C 07 16 16 0F 00 F8 28 00 00 25 07 33 03 96 00 F8 28 00 00 2C 05 16 0F 00 F9 28 00 00 25 07 25 03 96 00 F9 28 00 00 2C 07 16 07 D3 00 98 00 F8 28 00 00 26 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 03 06 91 02 07 81 01 98 00 F8 28 00 00 2C 03 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 25 07 7E 01 82 97 00 F9 28 00 00 91 26 2C 02 16 16 18 0D 00 1B 36 2A 00 00 00 00 00 00 2C 14 16 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 02 06 91 02 07 DF 01 1B 7B 17 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 03 06 91 02 07 2D 02 1B 36 2A 00 00 00 00 00 00 2C 0A 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 2C 02 06 91 02 0F 35 19 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 25 55 00 F7 28 00 00 1D 00 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 16 0F 35 1B 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 00 F9 28 00 00 0F 35 1A 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 00 F9 28 00 00 00 F8 28 00 00 16 01 C9 28 00 00 00 F8 28 00 00 A5 00 F9 28 00 00 16 06 75 00 A5 00 F8 28 00 00 16 06 5A 00 0F 00 F4 28 00 00 25 07 E0 03 96 00 F4 28 00 00 00 F6 28 00 00 16 0F 00 F5 28 00 00 A7 36 00 F7 28 00 00 16 0F 35 19 05 00 00 1C 05 00 00 00 01 10 10 00 F5 28 00 00 00 F7 28 00 00 01 C9 28 00 00 26 55 01 C8 28 00 00 14 00 10 00 F5 28 00 00 00 F7 28 00 00 16 40 00 F7 28 00 00 00 F5 28 00 00 26 16 A5 00 F4 28 00 00 16 06 3E 03 1B DB 2A 00 00 00 00 00 00 26 25 25 16 1B DB 2A 00 00 00 00 00 00 2C 03 2C 02 25 16 1B DB 2A 00 00 00 00 00 00 2C 02 2C 04 25 16 1B DB 2A 00 00 00 00 00 00 2C 04 2C 06 25 16 1B DB 2A 00 00 00 00 00 00 2C 06 2C 03 26 16 0F 35 17 05 00 00 1C 05 00 00 00 01 10 1B E9 30 00 00 00 00 00 00 2C 03 26 16 01 C9 28 00 00 26

 

/* Next six lines represent places on XCOM base first level starting from left to right. Substitute the first 00 value after 2C token with one of the following to design base of your liking.

 

00 = Empty

07 = Lab

08 = Workshop

09 = Satellite uplink

0A = Power generator

0B = Foundry

0C = OTS */

 

1B DB 2A 00 00 00 00 00 00 2C 00 2C 00 2C 01 16

 

1B DB 2A 00 00 00 00 00 00 2C 00 2C 01 26 16

 

1B DB 2A 00 00 00 00 00 00 2C 00 2C 02 26 16

 

1B DB 2A 00 00 00 00 00 00 2C 00 2C 04 26 16

 

1B DB 2A 00 00 00 00 00 00 2C 00 2C 05 26 16

 

1B DB 2A 00 00 00 00 00 00 2C 00 2C 06 26 16

 

1B A2 34 00 00 00 00 00 00 16 04 0B 53 00 00 00 02 00 02 00 27 11 00 00 00 00 00 00

 

Link to comment
Share on other sites

Alternatively, resize the function object by altering the header values to adjust the memory and/or file size declared for the function object.

 

Would you please look at the code and tell me what value exactly should i alter in this case.

Edited by Amphibious
Link to comment
Share on other sites

Could you please read the document I linked. :wink: Since you work with raw hex data understanding it's format will answer a lot of questions. :wink:

 

Memory size is an integer at relative offset 0x28:

OBJECT=XGBase.GenerateTiles
REL_OFFSET=0x28
UNSIGNED=0x12345 // script memory size
You still using absolute package search, which is limiting, IMO. You can specify an object with OBJECT and work with that object. You can use relative offsets and code replacement if you work with objects. And you can keep inter-patch and inter-mods compatibility that way.

 

If you're rewriting the whole function, you can use pseudo-code to recalculate object memory and file size and auto-calculate jump offsets. If you're rewriting a part of a code, you can't use jump labels, but you can use named references and patcher will calculate new memory/file sizes for you. See PatchUPK_Readme.txt (inside "doc" folder in UPKUtils github repo) for more info and usage examples.

Link to comment
Share on other sites

Could you please read the document I linked. :wink: Since you work with raw hex data understanding it's format will answer a lot of questions. :wink:

 

I did skim through it, and was like wtf is this. It looks like aramaic to me. Honestly I'm a layman at this I need things simplified.

 

This hex editing of upks reminds me of messsing with DNA introns and exons. YOu can mess with the coding parts to get variations of final product but can't touch the non coding jumps and regulators or stuff won't work anymore if you dont know precisely what you are doing.

 

Back to subject:

 

OK so I figured out that my PS is 0x379 and VS is 0x4c9. Now I just need to figure out how each one is determined or at least how did my changes affect VS to know how to alter it.

 

SO basically I know everything up to 5 new "AddFacility" lines that replaced "if(Game()etc...". didn't changed a thing in respect to PS and VS since if only those parts are modded thing works.

 

Edit: I figured of PS it's = overall funct size - header size - footer size

 

in this case 0x3b7 (951) - (48) - (14) = 0x379 (889).

Edited by Amphibious
Link to comment
Share on other sites

The trustworthy method of trial'n'error combined with lifetime experience of pokin-around-blindly yielded good results.

 

SO basically I played around with VS paramater decreasing it by 4 gradually and testing until I got positive result since I couldn't figure out how exactly VS is counted.

 

Expect release soon...

 

Thx for advices gals, bye

Edited by Amphibious
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...