Archive: nub needs help adding text


nub needs help adding text
I'm totally ignorant of this type of scripting from scratch. The closest thing I can claim is the DOS from 15 yrs ago ('mid 80's?), so please bear with me.
I'm trying to find out if I can add (and erase) text inside of a text document. I write scripts for Half-Life, not engine or mod scripts, just plain old alias scripts, and I need to find a way to add text so that the game will automatically execute them.
I'm going to give an example for ease of understanding:
I have a basic script pack and want to be able to offer "addons" to be downloaded seperately. To have them executed in the game, a text line has to be added to a file.
Example: valve.rc
exec script.cfg
exec buy.cfg
If 2 "addons" were downloaded seperately, I'd have to add an exec line into that file for each script.
I have a .nsi script that I use for the Pack install, which might be able to be modified further to suit my needs, but I don't know the commands to make it happen.
Here's the .nsi I'm using for the pack install (I only modified it to suit my specific files):
# DocPak3 NSIS Source Code
# Rev. 5/5/04 9:20pm cst
# Originally written by esky
# Modified by !Doc
# You may use this code for your own reference
# If you copy any of my functions, please give proper credit

Name "DocPak3"
OutFile "DocPak3.exe"
ShowInstDetails hide

# These are custom icons for the installer
# Delete the two lines to use default icons, or point to your own
Icon "C:\Program Files\Scripts\DP3\doc.ico"
UninstallIcon "C:\Program Files\Scripts\DP3\undoc.ico"

# Aux function to delete elements at the end of a path
# This function is taken from NSIS documentation
Function getParent
Push $8
Exch $0
Push $1
Push $2
StrCpy $1 -1
loop:
StrCpy $2 $0 1 $1
StrCmp $2 "" exit
StrCmp $2 "\" exit
IntOp $1 $1 - 1
Goto loop
exit:
StrCpy $0 $0 $1
Pop $2
Pop $1
Exch $0
FunctionEnd

# Find Steam via RegKey, then delete the half-life part, add Counter-Strike
Function findSteam
ReadRegStr $8 HKCU \
"Software\Valve\Steam" \
"ModInstallPath" ;These three lines find the key
Push $8
Call getParent ;delete 'half-life' from the end
Pop $8
StrCmp $8 "" skip addcs ;make sure a path was indeed copied from the registry
addcs:
StrCpy $INSTDIR "$8\Counter-Strike" ;add Counter-Strike on the end
GoTo skip2
skip:
StrCpy $INSTDIR "" ;this only happens if the path read from registry is empty or non-existant
skip2:
FunctionEnd

# Using path found in registry, make sure hl.exe exists in the counter-strike folder
# This is to verify that extracting giles to the *path*\cstrike will place script in correct place
Function doubleCheck
IfFileExists $INSTDIR\hl.exe skip notify
notify:
StrCpy $INSTDIR "Unable to find CS. Please browse manually..."
skip:
FunctionEnd

# Stuff to do when installer is started, before actual Install takes place
Function .onInit
Call findSteam ;attempt to find counter-strike path from registry
Call doubleCheck ;double check path is correct
FunctionEnd

# Make sure install path is correct
# This checks both the path auto-detected, and anything the user inputs
# Installer will not continue until the path has hl.exe
Function .onVerifyInstDir
IfFileExists $INSTDIR\hl.exe PathGood
Abort
PathGood:
FunctionEnd

DirText "Enter your Counter-Strike account location. Setup will try to detect the correct path. However, if path cannot be verified (hl.exe is not found), setup cannot continue."" "

# This section actually extracts all my stuff, also deletes previous installation if found
Section
Delete "$INSTDIR\cstrike\DocPak\*.*"
RMDir "$INSTDIR\cstrike\DocPak"
Rename $INSTDIR\cstrike\valve.rc $INSTDIR\cstrike\valve.rc.old
Rename $INSTDIR\cstrike\titles.txt $INSTDIR\cstrike\titles.txt.old
Rename $INSTDIR\cstrike\commandmenu.txt $INSTDIR\cstrike\commandmenu.txt.old
Rename $INSTDIR\cstrike\gfx\shell\kb_act.lst $INSTDIR\cstrike\gfx\shell\kb_act.lst.old
Rename $INSTDIR\cstrike\gfx\shell\kb_def.lst $INSTDIR\cstrike\gfx\shell\kb_def.lst.old
SetOutPath $INSTDIR\cstrike\DocPak
File "C:\Program Files\Scripts\DP3\cstrike\DocPak\buy.cfg"
File "C:\Program Files\Scripts\DP3\cstrike\DocPak\common.cfg"
File "C:\Program Files\Scripts\DP3\cstrike\DocPak\default.cfg"
File "C:\Program Files\Scripts\DP3\cstrike\DocPak\names.cfg"
File "C:\Program Files\Scripts\DP3\cstrike\DocPak\says.cfg"
File "C:\Program Files\Scripts\DP3\cstrike\DocPak\scripts.cfg"
File "C:\Program Files\Scripts\DP3\cstrike\DocPak\DP3.readme.rtf"
SetOutPath $INSTDIR\cstrike\gfx\shell
File "C:\Program Files\Scripts\DP3\cstrike\gfx\shell\kb_act.lst"
File "C:\Program Files\Scripts\DP3\cstrike\gfx\shell\kb_def.lst"
SetOutPath $INSTDIR\cstrike
File "C:\Program Files\Scripts\DP3\cstrike\valve.rc"
File "C:\Program Files\Scripts\DP3\cstrike\titles.txt"
File "C:\Program Files\Scripts\DP3\cstrike\commandmenu.txt"
WriteUninstaller "$INSTDIR\UninstallDocPak3.exe"
SetOutPath $SMPROGRAMS\DocPak3
CreateShortCut "$SMPROGRAMS\DocPak3\DP3.readme.lnk" "$INSTDIR\cstrike\DocPak\DP3.readme.rtf"
CreateShortCut "$SMPROGRAMS\DocPak3\Uninstall.lnk" "$INSTDIR\UninstallDocPak3.exe"
SectionEnd

UninstallText "This will uninstall DocPak3. Hit next to continue."

# Restore the valve.rc and commandmenu.txt the person had before installing
Function un.Rename
Rename $INSTDIR\cstrike\valve.rc.old $INSTDIR\cstrike\valve.rc
Rename $INSTDIR\cstrike\titles.txt.old $INSTDIR\cstrike\titles.txt
Rename $INSTDIR\cstrike\commandmenu.txt.old $INSTDIR\cstrike\commandmenu.txt
Rename $INSTDIR\cstrike\gfx\shell\kb_act.lst.old $INSTDIR\cstrike\gfx\shell\kb_act.lst
Rename $INSTDIR\cstrike\gfx\shell\kb_def.lst.old $INSTDIR\cstrike\gfx\shell\kb_def.lst
FunctionEnd

Section "Uninstall"
Delete "$INSTDIR\cstrike\valve.rc"
Delete "$INSTDIR\cstrike\titles.txt"
Delete "$INSTDIR\cstrike\commandmenu.txt"
Delete "$INSTDIR\UninstallDocPak3.exe"
Delete "$INSTDIR\cstrike\gfx\shell\kb_act.lst"
Delete "$INSTDIR\cstrike\gfx\shell\kb_def.lst"
Delete "$INSTDIR\cstrike\DocPak\*.*"
RMDir "$INSTDIR\cstrike\DocPak"
Delete "$SMPROGRAMS\DocPak3\*.*"
RMDir "$SMPROGRAMS\DocPak3"
Call un.Rename
SectionEnd

# If install is success, it will ask the user to show the readme.html
Function .onInstSuccess
MessageBox MB_YESNO|MB_ICONQUESTION \
"All Done! You can bind controls in Options. View ReadMe now?" \
IDNO NoReadme
ExecShell "open" $INSTDIR\cstrike\DocPak\DP3.readme.rtf
NoReadme:
FunctionEnd
;eof

I am curious about adding text into a text document from the installer. The reason I'm not putting all the scripts into one installer is that there are about 30-40 of them, and putting in a components option would be a bit much (to be honest, I wouldn't know how to start that anyway).
Thanks for any help you can give me.

ps. the adding text would be for installing and deleting text would be for uninstalling, wasn't sure if that was clear. Thanx again.


NSIS has build-in commands for text file modification. There are several examples available in the archive.

The easiest way is to read the text file, append or remove the data and write everything back.