##########################
#Registry Changer 2                       #
#An utility for Civ Evolution Game #
######################

;Script File compatible with NSIS 2.0b4

;Updated to source code for version 2.0.6

;By Diego Marcos
;<deguix@hotmail.com>

;----------------------------------------------

!define PRODUCT "Registry Changer"
!define VERSION_MAJOR "2"
!define VERSION_MEDIUM "0"
!define VERSION_MINOR "6"
!define VERSION "${VERSION_MAJOR}.${VERSION_MEDIUM}.${VERSION_MINOR}"

!define C-EVO "C-Evo"
!define C-EVO_PRODUCT "Civ Evolution"
!define C-EVO_VERSION_MAJOR "0"
!define C-EVO_VERSION_MEDIUM "12"
!define C-EVO_VERSION "${C-EVO_VERSION_MAJOR}.${C-EVO_VERSION_MEDIUM}"

XPStyle on

Caption "${PRODUCT} ${VERSION_MAJOR}"

BrandingText "${PRODUCT} v. ${VERSION}"

InstallDirRegKey HKLM "SOFTWARE\Civ Evolution\${PRODUCT}" "Install Dir"
ShowInstDetails nevershow

!ifndef NO_MUI

  !include "MUI.nsh"

  !define MUI_UI "RC.exe"

!endif

!macro ReadINIStr VAR FILE SECTION KEY

  !ifndef MUI_NOVERBOSE
    !ifndef MUI_MANUALVERBOSE
      !verbose 3
    !endif
  !endif

  ReadINIStr ${VAR} "$INSTDIR\Setup\${FILE}" "${SECTION}" "${KEY}"

  !ifndef MUI_NOVERBOSE
    !ifndef MUI_MANUALVERBOSE
      !verbose 4
    !endif
  !endif

!macroend

!macro WriteINIStr FILE SECTION KEY VALUE

  !ifndef MUI_NOVERBOSE
    !ifndef MUI_MANUALVERBOSE
      !verbose 3
    !endif
  !endif

  WriteIniStr "$INSTDIR\Setup\${FILE}" "${SECTION}" "${KEY}" "${VALUE}"

  !ifndef MUI_NOVERBOSE
    !ifndef MUI_MANUALVERBOSE
      !verbose 4
    !endif
  !endif

!macroend

Function .onInit

  ReadRegStr $LANGUAGE HKLM "SOFTWARE\Civ Evolution\${PRODUCT}" "Language"

  StrCmp $INSTDIR "" 0 +2
    Abort

  ReadRegStr $0 HKLM "SOFTWARE\Civ Evolution\${PRODUCT}" "Version"
  StrCmp ${VERSION} $0 +2
    Abort

FunctionEnd

!macro ReadString VAR NAME

  !insertmacro ReadINIStr ${VAR} "Strings.ini" "${PAGENAME}" "${NAME}"

!macroend

!macro ShowPage INIFILE

  Push $0
  Push $1

  !insertmacro ReadString $0 "#TITLE"
  !insertmacro ReadString $1 "#SUBTITLE"
  !insertmacro MUI_HEADER_TEXT "$0" "$1"

  Pop $1
  Pop $0

  InstallOptions::dialog /NOUNLOAD "$INSTDIR\Setup\${INIFILE}"
  Pop $R0

!macroend

!macro ShowPageAdv INIFILE

  Push $0
  Push $1

  !insertmacro ReadString $0 "#TITLE"
  !insertmacro ReadString $1 "#SUBTITLE"
  !insertmacro MUI_HEADER_TEXT "$0" "$1"

  Pop $1
  Pop $0

  InstallOptionsAdv::dialog /NOUNLOAD "$INSTDIR\Setup\${INIFILE}"
  Pop $R0
  InstallOptions::clean

!macroend

!macro ReservePage INIFILE

  Push $0
  Push $1

  !insertmacro ReadString $0 "#TITLE"
  !insertmacro ReadString $1 "#SUBTITLE"
  !insertmacro MUI_HEADER_TEXT "$0" "$1"

  Pop $1
  Pop $0

  InstallOptions::initDialog /NOUNLOAD "$INSTDIR\Setup\${INIFILE}"
  Pop $R0

!macroend

!macro ReservePageAdv INIFILE

  Push $0
  Push $1

  !insertmacro ReadString $0 "#TITLE"
  !insertmacro ReadString $1 "#SUBTITLE"
  !insertmacro MUI_HEADER_TEXT "$0" "$1"

  Pop $1
  Pop $0

  InstallOptionsAdv::initDialog /NOUNLOAD "$INSTDIR\Setup\${INIFILE}"
  Pop $R0

!macroend

!macro ShowReservedPage

  InstallOptions::show /NOUNLOAD
  Pop $R0

!macroend

!macro ShowReservedPageAdv

  InstallOptionsAdv::show /NOUNLOAD
  Pop $R0
  InstallOptions::clean

!macroend

!macro GetDlgItem VAR FIELD

  StrCpy ${VAR} ${FIELD}
  IntOp ${VAR} ${VAR} + 1199

  GetDlgItem ${VAR} $R0 ${VAR}

!macroend

!macro PathRequestPage

  Push $R0
  Push $1
  Push $0

  !insertmacro ReadINIStr $0 "Options.ini" "Field 12" "State"

  IfFileExists "$0\cEvo.exe" PathEnd 0

    !insertmacro PAGENAME "Buttons"

      !insertmacro ReadString $0 "#OK"
      !insertmacro WriteINIStr "Path.ini" "Settings" "NextButtonText" "$0"

      !insertmacro ReadString $0 "#CANCEL"
      !insertmacro WriteINIStr "Path.ini" "Settings" "CancelButtonText" "$0"

    !insertmacro PAGENAME "Options Page"

      !insertmacro ReadString $0 "#ENTER VALID PATH"
      !insertmacro WriteINIStr "Path.ini" "Field 1" "Text" "$0"

    !insertmacro PAGENAME "Path Request Page"

    !insertmacro ReadINIStr $0 "Options.ini" "Field 12" "State"
    !insertmacro WriteINIStr "Path.ini" "Field 2" "State" "$0"

    PathDialog:
    !insertmacro ShowPage "Path.ini"

    StrCmp $R0 "Success" 0 NoSuccess2

      !insertmacro ReadINIStr $1 "Path.ini" "Field 2" "State"

      IfFileExists "$1\cEvo.exe" +4 0
        !insertmacro ReadString $0 "#INVALID PATH"
        MessageBox MB_OK "$0"
        Goto PathDialog

      !insertmacro WriteINIStr "Options.ini" "Field 12" "State" "$1"
      Goto +2

    NoSuccess2:

    Quit

  PathEnd:

  Pop $0
  Pop $1
  Pop $R0

  !undef PAGENAME

!macroend

!macro PAGENAME PAGE

  !ifdef PAGENAME
    !undef PAGENAME
  !endif

  !define PAGENAME "${PAGE}"

!macroend

Outfile "..\Setup\Tribes.exe"

!include "RCDefault.nsi"

!define MUI_ICON "Icons\Tribes.ico"

Page custom Edit "" ""
Page custom Main "" ""

!insertmacro MUI_LANGUAGE "English"

Function Edit
FunctionEnd

Function Main

  !insertmacro PathRequestPage

  !insertmacro PAGENAME "Buttons"

    !insertmacro ReadString $0 "#EDIT"
    !insertmacro WriteINIStr "Tribes.ini" "Settings" "BackButtonText" "$0"

    !insertmacro ReadString $0 "#EXPORT"
    !insertmacro WriteINIStr "Tribes.ini" "Settings" "NextButtonText" "$0"

    !insertmacro ReadString $0 "#C-EVO MENU"
    !insertmacro WriteINIStr "Tribes.ini" "Settings" "CancelButtonText" "$0"

  !insertmacro PAGENAME "Tribes Page"

  !insertmacro ReadString $0 "#SELECT"
  !insertmacro WriteINIStr "Tribes.ini" "Field 1" "Text" "$0"

  !insertmacro ReadINIStr $R0 "Tribes.ini" "Field 2" "ListItems"
  !insertmacro ReadINIStr $R2 "Tribes.ini" "Field 2" "State"
  !insertmacro ReadINIStr $2 "Options.ini" "Field 12" "State"
  StrCpy $R8 0

  LoopingState1:
  StrCmp $R2 "" End1

  Push $R2
  Push "|"
  Push ">"
  Push "<"
  Push "0"
  Push "0"
    Call AdvancedStrStr
  Pop $R3

  Push $R2
  Push "|"
  Push ">"
  Push ">"
  Push "0"
  Push "0"
    Call AdvancedStrStr
  Pop $R2

    StrCmp $R3 "" End1 0

      LoopingListItems1:
      StrCmp $R0 "" LoopingState1

      Push $R0
      Push "|"
      Push ">"
      Push "<"
      Push "0"
      Push "0"
      Call AdvancedStrStr
      Pop $R1

      Push $R0
      Push "|"
      Push ">"
      Push ">"
      Push "0"
      Push "0"
      Call AdvancedStrStr
      Pop $R0

        StrCmp $R1 "" LoopingState1

          StrCmp $R1 $R3 +5
            !insertmacro ReadINIStr $R4 "TribesTemp.ini" "Temp2" "$R3"
            StrCmp $R4 "0" +2
              !insertmacro WriteINIStr "TribesTemp.ini" "Temp2" "$R3" "1"
            Goto LoopingListItems1

          !insertmacro WriteINIStr "TribesTemp.ini" "Temp2" "$R3" "0"
            Goto LoopingState1

  End1:

  !insertmacro ReadINIStr $R0 "Tribes.ini" "Field 2" "ListItems"

  SelectionDetection1:
  StrCmp $R0 "" SelectionDetectionEnd1

  Push $R0
  Push "|"
  Push ">"
  Push "<"
  Push "0"
  Push "0"
  Call AdvancedStrStr
  Pop $R1

  Push $R0
  Push "|"
  Push ">"
  Push ">"
  Push "0"
  Push "0"
  Call AdvancedStrStr
  Pop $R0

  !insertmacro ReadINIStr $0 "TribesTemp.ini" "Temp2" "$R1"

  StrCmp $0 "0" SelectionDetection1

    IfFileExists "$2\Tribes\$R1.tribe.bck" 0 +2
      Rename "$2\Tribes\$R1.tribe.bck" "$2\Tribes\$R1.tribe.txt"

    IntOp $R8 $R8 + 1

    !insertmacro WriteINIStr "TribesTemp.ini" "Temp" "$R8" "$R1"
    Goto SelectionDetection1

  SelectionDetectionEnd1:

  !insertmacro ShowPage "Tribes.ini"

  StrCmp $R0 "Back" 0 Cancel

    StrCpy $R2 0
    !insertmacro ReadINIStr $R2 "Tribes.ini" "Field 2" "State"
    !insertmacro ReadINIStr $2 "Options.ini" "Field 12" "State"

    StrCmp $R2 "" Fim2

    Push $R2
    Push "|"
    Push ">"
    Push "<"
    Push "0"
    Push "0"
    Call AdvancedStrStr
    Pop $R3

    StrCmp $R3 "" Fim2
      StrCpy $0 $R3

    Fim2:

    StrCmp $0 "" SelectionDetectionEnd1
  
    !insertmacro PAGENAME "Other Strings"

    !insertmacro ReadString $1 "#WAITING CLOSE PROCESS"
    Banner::show /NOUNLOAD /set 76 "${PRODUCT} ${VERSION_MAJOR}" "$1"

    !insertmacro PAGENAME "Tribes Page"

      ReadRegStr $1 HKCR ".txt" ""
      ReadRegStr $1 HKCR "$1\shell\open\command" ""

      Push $1
      Push "%1"

      IfFileExists "$2\Tribes\$0.tribe.txt" 0 +3
        Push '"$2\Tribes\$0.tribe.txt"'
        Goto +2
      Push '"$2\Tribes\$0.tribe.bck"'

      Call StrReplace

      Pop $3
      
      StrCmp $1 $3 0 +2
        StrCpy $3 '$3 "$2\Tribes\$0.tribe.txt"'
      
      ExecWait $3

    Banner::destroy /NOUNLOAD

    Goto SelectionDetectionEnd1

  Cancel:

  StrCmp $R0 "Cancel" +2 0
  StrCmp $R0 "Error" 0 Success

    StrCpy $R8 0

    Loop:

    IntOp $R8 $R8 + 1

    !insertmacro ReadINIStr $9 "TribesTemp.ini" "Temp" "$R8"
    !insertmacro ReadINIStr $2 "Options.ini" "Field 12" "State"

    IfErrors LoopEnd

    IfFileExists "$2\Tribes\$9.tribe.txt" 0 +2
      Rename "$2\Tribes\$9.tribe.txt" "$2\Tribes\$9.tribe.bck"

    Goto Loop

    LoopEnd:

    FlushINI "$INSTDIR\Setup\TribesTemp.ini"
    Delete "$INSTDIR\Setup\TribesTemp.ini"

    Exec cEvoMenu.exe
    Quit

  Success:

  StrCmp $R0 "success" 0 End

    FlushINI "$INSTDIR\Setup\TribesTemp.ini"
    Delete "$INSTDIR\Setup\TribesTemp.ini"

    !insertmacro ReadINIStr $R0 "Tribes.ini" "Field 2" "ListItems"
    !insertmacro ReadINIStr $R2 "Tribes.ini" "Field 2" "State"
    !insertmacro ReadINIStr $2 "Options.ini" "Field 12" "State"

    LoopingState2:
    StrCmp $R2 "" End2

    Push $R2
    Push "|"
    Push ">"
    Push "<"
    Push "0"
    Push "0"
    Call AdvancedStrStr
    Pop $R3

    Push $R2
    Push "|"
    Push ">"
    Push ">"
    Push "0"
    Push "0"
    Call AdvancedStrStr
    Pop $R2

    StrCmp $R3 "" End2 0

      LoopingListItems2:
      StrCmp $R0 "" LoopingState2

        Push $R0
        Push "|"
        Push ">"
        Push "<"
        Push "0"
        Push "0"
        Call AdvancedStrStr
        Pop $R1

        Push $R0
        Push "|"
        Push ">"
        Push ">"
        Push "0"
        Push "0"
        Call AdvancedStrStr
        Pop $R0

        StrCmp $R1 "" LoopingState2

          StrCmp $R1 $R3 +5
            !insertmacro ReadINIStr $R4 "TribesTemp.ini" "Temp2" "$R1"
            StrCmp $R4 "0" +2
              !insertmacro WriteINIStr "TribesTemp.ini" "Temp2" "$R1" "1"
            Goto LoopingListItems2

          !insertmacro WriteINIStr "TribesTemp.ini" "Temp2" "$R1" "0"
            Goto LoopingState2

    End2:

    !insertmacro ReadINIStr $R0 "Tribes.ini" "Field 2" "ListItems"

    SelectionDetection2:
    StrCmp $R0 "" SelectionDetectionEnd2

    Push $R0
    Push "|"
    Push ">"
    Push "<"
    Push "0"
    Push "0"
    Call AdvancedStrStr
    Pop $R1

    Push $R0
    Push "|"
    Push ">"
    Push ">"
    Push "0"
    Push "0"
    Call AdvancedStrStr
    Pop $R0

    !insertmacro ReadINIStr $0 "TribesTemp.ini" "Temp2" "$R1"

    StrCmp $0 "0" SelectionDetection2
      IfFileExists "$2\Tribes\$R1.tribe.txt" 0 +2
        Rename "$2\Tribes\$R1.tribe.txt" "$2\Tribes\$R1.tribe.bck"
      Goto SelectionDetection2

    SelectionDetectionEnd2:

    FlushINI "$INSTDIR\Setup\TribesTemp.ini"
    Delete "$INSTDIR\Setup\TribesTemp.ini"

    !insertmacro ReadINIStr $9 "Options.ini" "Field 2" "State"

    StrCmp $9 1 0 +5
      !insertmacro ReadINIStr $8 "Options.ini" "Field 12" "State"

      IfFileExists "$8\cEvo.exe" 0 +3
        Exec "$8\cEvo.exe"
        Quit

      Exec "cEvoMenu.exe"
      Quit

  End:

FunctionEnd

Function AdvancedStrStr

 # Preparing Variables

 Exch $R9
 Exch
 Exch $R8
 Exch
 Exch 2
 Exch $R7
 Exch 2
 Exch 3
 Exch $R6
 Exch 3
 Exch 4
 Exch $R5
 Exch 4
 Exch 5
 Exch $R4
 Exch 5
 Push $R3
 Push $R2
 Push $R1
 Push $R0
 Push $9
 Push $8
 Push $7
 Push $6
 StrCpy $R2 $R4
 StrCpy $R1 $R5
 StrCpy $R4 ""
 StrCpy $R5 ""
 StrCpy $7 $R2

 # Detect Empty Input

 StrCmp $R1 "" 0 +3
   SetErrors
   Goto granddone

 StrCmp $R2 "" 0 +3
   SetErrors
   Goto granddone

 StrCmp $R6 "" 0 +2
   StrCpy $R6 >

 StrCmp $R7 "" 0 +2
   StrCpy $R7 >

 # Preparing StrStr

 StrCpy $R0 0

 IntCmp $R9 1 +2 0 +2
   StrCpy $R9 0

 IntOp $R9 $R9 + 1

 # Loops and more loops if you want...

   grandloop:

   # Detect if the loops number given by user = code runs...

   StrCpy $R4 0
   StrLen $R3 $R1
   StrCpy $6 $R3
   StrCmp $9 1 0 +4
     StrCmp $R6 "<" 0 +2
     IntOp $R3 $R3 + 1
     IntOp $R4 $R4 + 1

   StrCmp $R6 "<" 0 +6
     IntOp $9 $R3 * 2
     IntOp $R3 $R3 - $9
     StrCpy $6 $R3
     IntCmp $R0 0 +2 0 0
       IntOp $6 $6 + 1

   # Loop to search the str

     loop:

     # If you search in the backward direction...

     StrCmp $R6 "<" 0 EndBack

       IntOp $9 $R4 * 2
       IntOp $9 $R4 - $9
       StrCmp $9 0 0 +3
         StrCpy $R5 $R2 "" $R3
           Goto +2
       StrCpy $R5 $R2 $9 $R3
         Goto +2

     EndBack:

     # Normal search (foward direction)...

     StrCpy $R5 $R2 $R3 $R4

     # Detect if the value returned is the searched...

     StrCmp $R5 $R1 done

     StrCmp $R5 "" granddone

         # If not, make a loop...

         IntOp $R4 $R4 + 1
         StrCmp $R6 "<" 0 +2
           IntOp $R3 $R3 - 1

     Goto loop

   done:

   StrCmp $R6 "<" 0 +3
     IntOp $8 $9 + $8
       Goto +2
     IntOp $8 $R4 + $8

   # Looping Calculation...

    IntOp $R0 $R0 + 1

   IntCmp $R0 $R9 0 continueloop 0

   # If the calc is backward...

     StrCmp $R6 "<" 0 EndBackward
       StrCmp $R7 ">" 0 +4
         StrCpy $R2 $7 "" $8
         StrCpy $R2 $R1$R2
         Goto +2
       StrCpy $R2 $R2 $9
       StrCmp $R8 1 EndForward 0
         StrCmp $R7 ">" 0 End>
           Push $6
           Push $7
           IntOp $7 $6 * 2
           IntOp $6 $6 - $7
           StrCpy $R2 $R2 "" $6
           Pop $7
           Pop $6
             Goto +2
         End>:
         StrCpy $R2 $R2 $6
           Goto EndForward
     EndBackward:

     # If the calc is forward...

     StrCmp $R7 "<" 0 +4
       StrCpy $R2 $7 $8
       StrCpy $R2 $R2$R1
       Goto +2
     StrCpy $R2 $R2 "" $R4
     StrCmp $R8 1 EndForward 0
       StrCmp $R7 "<" 0 End<
         Push $6
         Push $7
         IntOp $7 $6 * 2
         IntOp $6 $6 - $7
         StrCpy $R2 $R2 $6
         Pop $7
         Pop $6
           Goto +2
       End<:
       StrCpy $R2 $R2 "" $R3
     EndForward:

       Goto stoploop

   continueloop:

   # If the calc is backward...

   StrCmp $R6 "<" 0 +3
     StrCpy $R2 $R2 $9
       Goto +2

   # If the calc is forward...

   StrCpy $R2 $R2 "" $R4

   stoploop:

   # Return to grandloop init...

   StrCpy $9 1

   IntCmp $R0 $R9 0 grandloop 0

 StrCpy $R4 $R2

 Goto +2

 granddone:

 # Return the result to user

 StrCpy $R4 ""

 Pop $6
 Pop $7
 Pop $8
 Pop $9
 Pop $R0
 Pop $R1
 Pop $R2
 Pop $R3
 Pop $R9
 Pop $R8
 Pop $R7
 Pop $R6
 Pop $R5
 Exch $R4

FunctionEnd

function StrReplace
  Exch $0 ;this will replace wrong characters
  Exch
  Exch $1 ;needs to be replaced
  Exch
  Exch 2
  Exch $2 ;the orginal string
  Push $3 ;counter
  Push $4 ;temp character
  Push $5 ;temp string
  Push $6 ;length of string that need to be replaced
  Push $7 ;length of string that will replace
  Push $R0 ;tempstring
  Push $R1 ;tempstring
  Push $R2 ;tempstring
  StrCpy $3 "-1"
  StrCpy $5 ""
  StrLen $6 $1
  StrLen $7 $0
  Loop:
  IntOp $3 $3 + 1
  StrCpy $4 $2 $6 $3
  StrCmp $4 "" ExitLoop
  StrCmp $4 $1 Replace
  Goto Loop
  Replace:
  StrCpy $R0 $2 $3
  IntOp $R2 $3 + $6
  StrCpy $R1 $2 "" $R2
  StrCpy $2 $R0$0$R1
  IntOp $3 $3 + $7
  Goto Loop
  ExitLoop:
  StrCpy $0 $2
  Pop $R2
  Pop $R1
  Pop $R0
  Pop $7
  Pop $6
  Pop $5
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd

Section
SectionEnd
