NSIS使用目录中的随机文件

时间:2017-11-07 08:16:50

标签: nsis

我想拥有它,以便在启动过程中,我可以在安装过程中设置一个随机声音文件。

我有这个代码,所以它将所有声音文件放在一个下拉列表中,以便可以更改

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

因此,如何从列表中选择一个随机文件并将其保存到文件状态,而不仅仅是硬编码“默认”?

目录中的文件数量非常大,因此需要从每次运行中选择。

1 个答案:

答案 0 :(得分:0)

看起来我找到了办法。可能不是最好的,但它的工作原理。其他需要:

找到此代码并将其添加到我的代码中:

 !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

然后在我的原始代码中,我为计数器添加了一个变量,然后添加了另一个函数来检查与文件列表匹配的随机数。 (这是我的完整代码)

       !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文件

[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