1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Civilization II 64-bit Editbox Patcher (MGE+ToT) (Also works on Win10 1709 Fall Creators Update)

Discussion in 'Civ2 - Technical Support' started by FoxAhead, Sep 7, 2017.

  1. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    Hello!

    Preamble


    I know that more than 10 years have passed since the Mastermind's patch was presented to run the game on the 64-bit systems. Nevertheless, I want to present you yet another patch.

    I was playing Civilization 2 MGE many years on my windows 7 64-bit. And always confusing about input fields not reacting on keyboard strokes like Enter, TAB or Esc. It was little annoying to press 'B' to build city and after that move mouse to hunt OK button rather than just press Enter. I remembered that sometime in the past everything worked fine. Suddenly I took debugger and dive deep in assembly code for a week. Finally I realized that problem was in Mastermind's patch. Yes it allows to run game on 64-bit but the normal key-processing for input fields was broken. And after a while I managed to make my own patch that works without any side effects.

    Description


    This is Civilization II 64-bit Editbox Patcher.

    It eliminates game and editor crashes on 64-bit systems when the game tries to display edit controls (input fields) for entering text (like city name, emperor name, world sizes etc.).

    Unlike the existing well known patch by Mastermind it will retain the full functionality of edit controls including the response to the Enter, TAB or Escape keys.

    So, the establishing city is now just 'B', 'Enter'. And customizing world size is like '100', 'TAB', '100', 'Enter'. No more mouse clicking.

    Supported game versions are:
    • Civilization II Multiplayer Gold Edition
    • Civilization II: Test of Time
    The patcher can even detect if Mastermind's patch has already been applied and will replace it.

    Download and source code


    https://github.com/FoxAhead/Civilization-II-64-bit-Editbox-Patcher/releases

    Technical details


    Spoiler :

    Like Mastermind mentioned the problem was in callback procedure for Editbox control.
    The pseudocode for place with error looks like this:
    Code:
    v17 = GetWindowLongA(hWnd, 0xA);
    nIndex = GetClassLongA(hWnd, GCL_CBWNDEXTRA) - 8;
    lpPrevWndFunc = GetWindowLongA(hWnd, nIndex);
    The problem was in hard-coded 0xA which is offset for parameter in extra window memory. Here we are getting two parameters: v17 - pointer to some Style data, and lpPrevWndFunc - pointer to parent callback. The second one is acquired correctly using offset in nIndex. But offset for the first one is hard-coded = 0xA. In 32-bit OS size of extra window memory for 'Edit' window class equals 6 bytes. So plus our two integer (4 bytes) parameters gives total: 6+4*2 = 14 bytes. Call for GetClassLongA(hWnd, GCL_CBWNDEXTRA) returns us this total bytes of extra memory used. To get offset for lpPrevWndFunc you have to subtract 8 from total (14) (which is done correctly). And to get offset for v17 you have to subtract 4 from total (14) which gives you exactly 10 ( = 0xA in hexadecimal). And that works well until 64-bit comes on scene where size of extra window memory for 'Edit' window class equals 8 bytes. So with our two additional integers total size equals 16. And with offset for lpPrevWndFunc being still calculated correctly (16-8) the offset for v17 must be 16 - 4 = 12 (0xC) and not 10 (0xA).
    The first simplest solution is to change this single byte from 0xA to 0xC and all will be working fine until... Well, for example you decide to copy this patched file back to 32-bit system (to play on vacation on old notebook with Windows 98 or XP x32). And this patched file will not work on 32-bit OS. So there is no backward compatibility.
    The correct way to patch is to achieve code something like this:
    Code:
    nIndex = GetClassLongA(hWnd, GCL_CBWNDEXTRA);
    v17           = GetWindowLongA(hWnd, nIndex - 4);
    lpPrevWndFunc = GetWindowLongA(hWnd, nIndex - 8);
    Here two first steps are swapped. Firstly we have to get total size of extra memory. And then we can calculate each offset correctly.
    And then comes sweet part of juggling assembly code to achieve desired in less opcodes changed. And you have to fit in fixed frame because changing size of EXE is unacceptable. After 6th iteration I managed to make patch changing only 10 bytes.
    Before:
    Code:
    005D2A01 55                  push   ebp
    005D2A02 8B EC               mov    ebp, esp
    005D2A04 83 EC 1C            sub    esp, 1Ch
    005D2A07 53                  push   ebx
    005D2A08 56                  push   esi
    005D2A09 57                  push   edi
    005D2A0A 6A 0A               push   0Ah             ; nIndex
    005D2A0C 8B 45 08            mov    eax, [ebp+hWnd]
    005D2A0F 50                  push   eax             ; hWnd
    005D2A10 FF 15 2C 7E 6E 00   call   ds:GetWindowLongA
    005D2A16 89 45 F8            mov    [ebp+var_8], eax
    005D2A19 6A EE               push   GCL_CBWNDEXTRA  ; nIndex
    005D2A1B 8B 45 08            mov    eax, [ebp+hWnd]
    005D2A1E 50                  push   eax             ; hWnd
    005D2A1F FF 15 9C 7E 6E 00   call   ds:GetClassLongA
    005D2A25 83 E8 08            sub    eax, 8
    005D2A28 89 45 F4            mov    [ebp+nIndex], eax
    005D2A2B 8B 45 F4            mov    eax, [ebp+nIndex]
    005D2A2E 50                  push   eax             ; nIndex
    005D2A2F 8B 45 08            mov    eax, [ebp+hWnd]
    005D2A32 50                  push   eax             ; hWnd
    005D2A33 FF 15 2C 7E 6E 00   call   ds:GetWindowLongA
    005D2A39 89 45 EC            mov    [ebp+lpPrevWndFunc], eax
    005D2A3C 8B 45 0C            mov    eax, [ebp+Msg]
    005D2A3F 89 45 E4            mov    [ebp+var_1C], eax
    005D2A42 E9 4D 02 00 00      jmp    loc_5D2C94
    
    After:
    Code:
    005D2A01 55                  push   ebp
    005D2A02 8B EC               mov    ebp, esp
    005D2A04 83 EC 1C            sub    esp, 1Ch
    005D2A07 53                  push   ebx
    005D2A08 56                  push   esi
    005D2A09 57                  push   edi
    005D2A0A 6A EE               push   GCL_CBWNDEXTRA  ; nIndex
    005D2A0C 8B 45 08            mov    eax, [ebp+hWnd]
    005D2A0F 50                  push   eax             ; hWnd
    005D2A10 FF 15 9C 7E 6E 00   call   ds:GetClassLongA
    005D2A16 89 C3               mov    ebx, eax
    005D2A18 2C 04               sub    al, 4
    005D2A1A 50                  push   eax             ; nIndex
    005D2A1B 8B 45 08            mov    eax, [ebp+hWnd]
    005D2A1E 50                  push   eax             ; hWnd
    005D2A1F FF 15 2C 7E 6E 00   call   ds:GetWindowLongA
    005D2A25 83 EB 08            sub    ebx, 8
    005D2A28 89 45 F8            mov    [ebp+var_8], eax
    005D2A2B 8B 45 F4            mov    eax, [ebp+var_C]
    005D2A2E 53                  push   ebx             ; nIndex
    005D2A2F 8B 45 08            mov    eax, [ebp+hWnd]
    005D2A32 50                  push   eax             ; hWnd
    005D2A33 FF 15 2C 7E 6E 00   call   ds:GetWindowLongA
    005D2A39 89 45 EC            mov    [ebp+lpPrevWndFunc], eax
    005D2A3C 8B 45 0C            mov    eax, [ebp+Msg]
    005D2A3F 89 45 E4            mov    [ebp+var_1C], eax
    005D2A42 E9 4D 02 00 00      jmp    loc_5D2C94
    
    P.S.
    After almost completing working on patch I found that someone named iunnamed already made another patch explaining exactly the same reason of crashes:
    https://www.old-games.ru/forum/threads/sid-meiers-civilization-2.7950/page-9#post-1013691
    I inspected it. That patch works good - input fields work correctly. The difference is just it for MGE 5.4.0f game only. Nor for Map Editor nor for ToT nor for other variations of MGE-like family. Also it lacks of GUI. And also it change 17 bytes of EXE.

    I made my patcher on Delphi 7 because I wanted:
    1) to make it possible run on old systems without external dependencies like .NET. (Tested on Windows 98 SE.);
    2) to make GUI;
    3) to develop it fast;
    Downside of this is size of 400kB. But who cares nowadays about 400kB?
     
    Civinator, Arrogant and ShadowFalls like this.
  2. Natalia

    Natalia Chieftain

    Joined:
    Dec 11, 2017
    Messages:
    3
    Gender:
    Female
    I've had a recent Windows 10 update and the mastermind patch doesn't work on civ2 anymore - will this work? The game crashes when settling cities even when the previous patch is applied. I'm pretty simple when it comes to computers so any simple help will be appreciated :)
     
  3. Natalia

    Natalia Chieftain

    Joined:
    Dec 11, 2017
    Messages:
    3
    Gender:
    Female
    Actually update - this works perfectly. Thank you so much! <3
     
  4. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    Yes, there are reports that this patch works even on latest Fall Creators Update (1709) for Windows 10, and still no issues reported.
    There are several ways of checking your Windows 10 version:
    https://www.howtogeek.com/236195/how-to-find-out-which-build-and-version-of-windows-10-you-have/

    You are welcome! Happy playing!
     
  5. Civ2isstillbest

    Civ2isstillbest Chieftain

    Joined:
    Dec 12, 2017
    Messages:
    1
    Gender:
    Male
    The update will not execute.

    When I click the link and download the patch Windows doesn't want me to run it. I override that. Then a small window pops up with "Civilization II 64-bit Editbox Patcher" at the top.

    There is a welcome message. At the bottom it says to click browse then select Civ2.exe or Civ2Map.exe and click patch. When I click browse what I see is:
    Civ2x64EditboxPatcher

    When I click that I get this message:
    No version info detected.
    Wrong file. Too much patterns found.
    CAN NOT PATCH!

    What am I doing wrong?
     
  6. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    Hi!
    It says "Just click 'Browse...', select Civ2.exe or Civ2Map.exe file and then click 'Patch!'."
    So you should select file named Civ2.exe (or Civ2Map.exe if you want to patch Mapeditor).
     
  7. Torres

    Torres Chieftain

    Joined:
    Dec 28, 2017
    Messages:
    4
    Gender:
    Male
    Hi and thanks very much for keeping my favourite game going.

    Unfortunately, with the latest Windows 10 build the failure on city build error seems to have cropped up again. I have Windows 10 Version 1709 OS Build 16299.125. Can you save me again?
     
  8. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    Hi, Torres!

    I updated my Windows 10 with latest KB4054517 (OS Build 16299.125) and Civ2 MGE 5.4.0f patched with this patch is working fine. Are you talking about MGE? Or may be you meant another game version? Test of Time?

    upload_2017-12-28_22-3-58.png
     
  9. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    Well. The same is with ToT 1.1.0. Text input fields are working fine.

    upload_2017-12-28_22-11-19.png
     
  10. Torres

    Torres Chieftain

    Joined:
    Dec 28, 2017
    Messages:
    4
    Gender:
    Male
    Hi FoxAhead. It's MGE
     
  11. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    Then are you sure you patched Civ2.exe?
    Run Civ2x64EditboxPatcher.exe, select desired Civ2.exe. What does it says? Is it:
    "Version '5.4.0f Multiplayer 26-March-99 Patch 3 ' detected.
    The file is already patched.
    CAN NOT PATCH!"?
    If it is, then Civ2.exe is patched and we should search another reason.
     
  12. Torres

    Torres Chieftain

    Joined:
    Dec 28, 2017
    Messages:
    4
    Gender:
    Male
    I've deleted and reinstalled. I'll see how it goes
     
  13. Torres

    Torres Chieftain

    Joined:
    Dec 28, 2017
    Messages:
    4
    Gender:
    Male
    That seems to have fixed it. Thanks for your help
     
  14. Cind13

    Cind13 Chieftain

    Joined:
    Dec 30, 2017
    Messages:
    1
    Gender:
    Male
    I registered here and confirmed my email just to tell you I love you. Any person who would dive into assembler to keep civilization II alive 21 years after it's release deserves some lovin.
    I can confirm that masterminds patch didn't work for me anymore (worked at first but crashed on city funding somewhere along the game) and yours does. Thank you.
     
  15. Civinator

    Civinator Blue Lion Supporter

    Joined:
    May 5, 2005
    Messages:
    5,412
    FoxAhead, thank you very much for your great work! :):thanx:

    Your 64-Bit patcher worked like charm for both my Civ 2 Gold Multiplayer and my Civ 2 ToT with Windows 10. :goodjob:

    As you showed, you have dived into the Civ 2 source code. May be you can give me a hint with the following problem concerning animated units in Civ 2 ToT: Some time ago, I converted some animated units from Civ 3 to Civ 2 ToT with MercatorĀ“s sprite tool. These units looked pretty well in Civ 2 ToT. You can find them there: https://forums.civfanatics.com/threads/animated-units-for-civ-2-tot.411816/#post-10217483

    The problem is, that the attack animations are only shown for the attacking unit. The defending unit stands only at its place until the death animation is started for the defending or the attacking unit. It is annoying that the defending unit does nothing when beeing attacked. Best would be, if in a battle, both units (the attacking and the defending) could switch to the attack animations.

    But it would be still better in a battle, than doing nothing as defender, if the old 'battle animation' of Civ 2 Gold Multiplayer could be activated, that showed hits in a battle mutually at the attacking or the defending unit.

    Do you have any idea what to do?
     
  16. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    You are welcome, Cind13!

    You are welcome too, Civinator!
    As for animation in ToT I believe it is no problem to fix that. But it has to deal with ToT which I really didn't like much and didn't played a lot. The real master of ToT-fixing is back - TheNamelessOne! :) I'm sure after some time he will make some fixing in his ToT Patch Project. ;)
     
  17. Civinator

    Civinator Blue Lion Supporter

    Joined:
    May 5, 2005
    Messages:
    5,412
    FoxAhead, thank you very much for your reply. :) I asked the TheNamelessOne and it seems that this is no easy task.
     
  18. bkwrm79

    bkwrm79 Chieftain

    Joined:
    Nov 17, 2003
    Messages:
    390
    Since the Windows update Mastermind's patch is no longer sufficient - I was still able to play, up to a certain point, then the game would crash on the next city every time. I installed your patch... wouldn't launch. Installed your addition. It launches! With enter key functionality! I've even been able to continue the game I was playing before the Win update (I had resigned myself to starting over, no big deal, but it's nice to be able to finish this one).

    Thank you!
     
  19. FNP

    FNP Chieftain

    Joined:
    Nov 2, 2009
    Messages:
    70
    Location:
    ashore
    Supported game versions are:
    • Civilization II Multiplayer Gold Edition
    • Civilization II: Test of Time
    Your patch works perfectly! Thanks. Have you considered patching the delevent.exe file so that many of the old event based scenario games like D Day and Red Front can be played? I think the batch files can be rewritten if the delevent file can be patched.
     
  20. FoxAhead

    FoxAhead Chieftain

    Joined:
    Sep 7, 2017
    Messages:
    73
    Gender:
    Male
    I did not know about DELEVENT.EXE and never used it. I searched and found it in Fantastic Worlds version of the game. So it seems that it is some sort of utility to repair old saves for using new macro language. Right?
    All I can say that it is 16-bit program (as all "old" versions of Civ2 before MGE). Therefore, there is no point in patching it because it will never run in 64-bit. It is not a bug. That is just how Windows works.
    https://en.wikipedia.org/wiki/64-bit_computing#Software_availability
    Another option is to completely rewrite this utility. But easier is to use some VM with 32-bit OS.
     

Share This Page