0
votes

I would like to have it so that during boot, I can set a random sound file to play during install.

I have this code so it puts all the sound files in a drop down so it can be changed

Function SetUserOptionsPage
 ${Locate} "$InstDrvRE\Media\SoundFiles" "/L=F /M=*.mp3" "listsoundfiles"
  StrCpy $IVO "$IVO|Random|None"
  WriteINIStr "$PLUGINSDIR\settings.ini" "Field 6" "ListItems" "$IVO"
  WriteINIStr "$PLUGINSDIR\XYfYk7xQ.dat" "Field 6" "State" "Default"
  InstallOptions::initDialog "$PLUGINSDIR\settings.ini"
  Pop $0
  InstallOptions::show
  Pop $0
  Abort 
Function End

Function listsoundfiles
  ${StrStrip}  ".mp3" $R7 $R0
  ${If} $R7 == "Default.mp3"
    StrCpy $LSF "$R0"
  ${Else}
    StrCpy $LSF "$LSF|$R0"
  ${EndIf}
    Push $0 
FunctionEnd


Example Drop Down:
Default
Jazz
Rock
Metal
Random
None

So instead of just hard coding "Default" how can I select a random file from the list and save that to the file state?

The amount of files in the dir can very so it will need to be a selection from all available each run.

1

1 Answers

0
votes

Looks like I found a way. Might not be the best but it works. Incase other need:

Found this code and added it to mine:

 !define Rnd "!insertmacro _Rnd"
    !macro _Rnd _RetVal_ _Min_ _Max_
       Push "${_Max_}"
       Push "${_Min_}"
       Call Rnd
       Pop ${_RetVal_}
    !macroend
    Function Rnd
       Exch $0  ;; Min / return value
       Exch
       Exch $1  ;; Max / random value
       Push "$3"  ;; Max - Min range
       Push "$4"  ;; random value buffer

       IntOp $3 $1 - $0 ;; calculate range
       IntOp $3 $3 + 1
       System::Call '*(l) i .r4'
       System::Call 'advapi32::SystemFunction036(i r4, i 4)'  ;; RtlGenRandom
       System::Call '*$4(l .r1)'
       System::Free $4
       ;; fit value within range
       System::Int64Op $1 * $3
       Pop $3
       System::Int64Op $3 / 0xFFFFFFFF
       Pop $3
       IntOp $0 $3 + $0  ;; index with minimum value

       Pop $4
       Pop $3
       Pop $1
       Exch $0
    FunctionEnd

then in my original code I added a vars for a counters, then added another function to check for the random number that matched the listing of file. (Here is my complete code)

       !include WinVer.nsh
!include LogicLib.nsh
!include x64.nsh
!include FileFunc.nsh
!include MUI2.nsh
!include WinMessages.nsh
!include InstallOptions.nsh
!include Sections.nsh
!include nsDialogs.nsh

;Request application privileges
RequestExecutionLevel Admin

Page custom SetUserOptionsPage SetUserOptionsPagenext
!insertmacro MUI_PAGE_INSTFILES

;Languages
  !insertmacro MUI_LANGUAGE "English"

Function .int
    Var /GlOBAL InstDrvRE
    Var /GlOBAL numsounds
    Var /Global randomsound
    Var /Global soundnum
    Var /Global randomsoundsel
    Var /Global IVO 
    Var /Global LSF

Push $EXEPATH
Call GetRoot
Pop $0
StrCpy $InstDrvRE "$0"
CreateDirectory $PLUGINSDIR
    FunctionEnd

Function GetRoot
  Exch $0
  Push $1
  Push $2
  Push $3
  Push $4

  StrCpy $1 $0 2
  StrCmp $1 "\\" UNC
    StrCpy $0 $1
    Goto done

UNC:
  StrCpy $2 3
  StrLen $3 $0
  loop:
    IntCmp $2 $3 "" "" loopend
    StrCpy $1 $0 1 $2
    IntOp $2 $2 + 1
    StrCmp $1 "\" loopend loop
  loopend:
    StrCmp $4 "1" +3
      StrCpy $4 1
      Goto loop
    IntOp $2 $2 - 1
    StrCpy $0 $0 $2

done:
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd

Function StrStrip
Exch $R0 #string
Exch
Exch $R1 #in string
Push $R2
Push $R3
Push $R4
Push $R5
 StrLen $R5 $R0
 StrCpy $R2 -1
 IntOp $R2 $R2 + 1
 StrCpy $R3 $R1 $R5 $R2
 StrCmp $R3 "" +9
 StrCmp $R3 $R0 0 -3
  StrCpy $R3 $R1 $R2
  IntOp $R2 $R2 + $R5
  StrCpy $R4 $R1 "" $R2
  StrCpy $R1 $R3$R4
  IntOp $R2 $R2 - $R5
  IntOp $R2 $R2 - 1
  Goto -10
  StrCpy $R0 $R1
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
!macro StrStrip Str InStr OutVar
 Push '${InStr}'
 Push '${Str}'
  Call StrStrip
 Pop '${OutVar}'
!macroend
!define StrStrip '!insertmacro StrStrip'

!define Rnd "!insertmacro _Rnd"
!macro _Rnd _RetVal_ _Min_ _Max_
   Push "${_Max_}"
   Push "${_Min_}"
   Call Rnd
   Pop ${_RetVal_}
!macroend
Function Rnd
   Exch $0  ;; Min / return value
   Exch
   Exch $1  ;; Max / random value
   Push "$3"  ;; Max - Min range
   Push "$4"  ;; random value buffer

   IntOp $3 $1 - $0 ;; calculate range
   IntOp $3 $3 + 1
   System::Call '*(l) i .r4'
   System::Call 'advapi32::SystemFunction036(i r4, i 4)'  ;; RtlGenRandom
   System::Call '*$4(l .r1)'
   System::Free $4
   ;; fit value within range
   System::Int64Op $1 * $3
   Pop $3
   System::Int64Op $3 / 0xFFFFFFFF
   Pop $3
   IntOp $0 $3 + $0  ;; index with minimum value

   Pop $4
   Pop $3
   Pop $1
   Exch $0
FunctionEnd


Function SetUserOptionsPage
${Locate} "$InstDrvRE\Media\SoundFiles" "/L=F /M=*.mp3" "listsoundfiles"
  StrCpy $IVO "$IVO|Random|None"
  WriteINIStr ".\settings.ini" "Field 2" "ListItems" "$IVO"
  InstallOptions::initDialog ".\settings.ini"
  Pop $0
  InstallOptions::show
  Pop $0
  Abort 
FunctionEnd

Function SetUserOptionsPagenext
FunctionEnd


Function listsoundfiles
IntOp $numsounds $numsounds + 1
  ${StrStrip}  ".mp3" $R7 $R0
  ${If} $numsounds == 1
    StrCpy $IVO "$R0"
  ${Else}
    StrCpy $IVO "$IVO|$R0"
  ${EndIf}
  Push $0 
FunctionEnd

Function randomsound
IntOp $soundnum $soundnum + 1
${if} $randomsound == $soundnum
    StrCpy $randomsoundsel "$R7"
${EndIf}
  Push $0 
FunctionEnd

Section
 ${Rnd} $randomsound 1 $numsounds
   StrCpy $soundnum 0
 ${Locate} "$InstDrvRE\Media\SoundFiles" "/L=F /M=*.mp3" "randomsound"
MessageBox MB_OK|MB_ICONSTOP|MB_TOPMOST "File Selected=$randomsoundsel,$numsounds"

SectionEnd

ini file

[Settings]
NumFields=2
State=0


[Field 1]
Type=Label
Left=75
Right=145
Top=21
Bottom=30
Text="Intro"
txtAlign=Center


[Field 2]
Type=DROPLIST
ListItems=Random|None
State="Sound_Screen_Click"
Flags=
Left=75
Right=145
Top=30
Bottom=40