Source

nsis64 / Include / Memento.nsh

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
!verbose push
!verbose 3

!include LogicLib.nsh
!include Sections.nsh

!ifndef ___MEMENTO_NSH___
!define ___MEMENTO_NSH___

#####################################
### Memento                       ###
#####################################

/*

Memento is a set of macros that allow installers to remember user selection
across separate runs of the installer. Currently, it can remember the state
of sections and mark new sections as bold. In the future, it'll integrate
InstallOptions and maybe even the Modern UI.

A usage example can be found in `Examples\Memento.nsi`.

*/

#####################################
### Usage Instructions            ###
#####################################

/*

1. Declare usage of Memento by including Memento.nsh at the top of the script.

      !include Memento.nsh

2. Define MEMENTO_REGISTRY_ROOT and MEMENTO_REGISTRY_KEY with the a registry key
   where sections' state should be saved.

      !define MEMENTO_REGISTRY_ROOT HKLM
      !define MEMENTO_REGISTRY_KEY \
                Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProgram

3. Replace Section with ${MementoSection} and SectionEnd with ${MementoSectionEnd}
   for sections that whose state should be remembered by Memento.

   For sections that should be unselected by default, use ${MementoSection}'s
   brother - ${MementoUnselectedSection}.

   Sections that don't already have an identifier must be assigned one.

   Section identifiers must stay the same across different versions of the
   installer or their state will be forgotten.

4. Use ${MementoSectionDone} after the last ${MementoSection}.

5. Add a call to ${MementoSectionRestore} to .onInit to restore the state
   of all sections from the registry.

      Function .onInit

        ${MementoSectionRestore}

      FunctionEnd

6. Add a call to ${MementoSectionSave} to .onInstSuccess to save the state
   of all sections to the registry.

      Function .onInstSuccess

        ${MementoSectionSave}

      FunctionEnd

7. Tattoo the location of the chosen registry key on your arm.

*/

#####################################
### User API                      ###
#####################################

;
; ${MementoSection}
;
;   Defines a section whose state is remembered by Memento.
;
;   Usage is similar to Section.
;
;     ${MementoSection} "name" "some_id"
;

!define MementoSection "!insertmacro MementoSection"

;
; ${MementoSectionEnd}
;
;   Ends a section previously opened using ${MementoSection}.
;
;   Usage is similar to SectionEnd.
;
;     ${MementoSection} "name" "some_id"
;        # some code...
;     ${MementoSectionEnd}
;

;
; ${MementoUnselectedSection}
;
;   Defines a section whose state is remembered by Memento and is
;   unselected by default.
;
;   Usage is similar to Section with the /o switch.
;
;     ${MementoUnselectedSection} "name" "some_id"
;

!define MementoUnselectedSection "!insertmacro MementoUnselectedSection"

;
; ${MementoSectionEnd}
;
;   Ends a section previously opened using ${MementoSection}.
;
;   Usage is similar to SectionEnd.
;
;     ${MementoSection} "name" "some_id"
;        # some code...
;     ${MementoSectionEnd}
;

!define MementoSectionEnd "!insertmacro MementoSectionEnd"

;
; ${MementoSectionDone}
;
;   Used after all ${MementoSection} have been set.
;
;     ${MementoSection} "name1" "some_id1"
;        # some code...
;     ${MementoSectionEnd}
;
;     ${MementoSection} "name2" "some_id2"
;        # some code...
;     ${MementoSectionEnd}
;
;     ${MementoSection} "name3" "some_id3"
;        # some code...
;     ${MementoSectionEnd}
;
;     ${MementoSectionDone}
;

!define MementoSectionDone "!insertmacro MementoSectionDone"

;
; ${MementoSectionRestore}
;
;   Restores the state of all Memento sections from the registry.
;
;   Commonly used in .onInit.
;
;     Function .onInit
;
;       ${MementoSectionRestore}
;
;     FunctionEnd
;

!define MementoSectionRestore "!insertmacro MementoSectionRestore"

;
; ${MementoSectionSave}
;
;   Saves the state of all Memento sections to the registry.
;
;   Commonly used in .onInstSuccess.
;
;     Function .onInstSuccess
;
;       ${MementoSectionSave}
;
;     FunctionEnd
;

!define MementoSectionSave "!insertmacro MementoSectionSave"


#####################################
### Internal Defines              ###
#####################################

!define __MementoSectionIndex 1

#####################################
### Internal Macros               ###
#####################################

!macro __MementoCheckSettings

  !ifndef MEMENTO_REGISTRY_ROOT | MEMENTO_REGISTRY_KEY

    !error "MEMENTO_REGISTRY_ROOT and MEMENTO_REGISTRY_KEY must be defined before using any of Memento's macros"

  !endif

!macroend

!macro __MementoSection flags name id

  !insertmacro __MementoCheckSettings

  !ifndef __MementoSectionIndex

    !error "MementoSectionDone already used!"

  !endif

  !define __MementoSectionLastSectionId `${id}`

  !verbose pop

  Section ${flags} `${name}` `${id}`

  !verbose push
  !verbose 3

!macroend

#####################################
### User Macros                   ###
#####################################

!macro MementoSection name id

  !verbose push
  !verbose 3

  !insertmacro __MementoSection "" `${name}` `${id}`

  !verbose pop

!macroend

!macro MementoUnselectedSection name id

  !verbose push
  !verbose 3

  !insertmacro __MementoSection /o `${name}` `${id}`

  !define __MementoSectionUnselected

  !verbose pop

!macroend

!macro MementoSectionEnd

  SectionEnd

  !verbose push
  !verbose 3

  !insertmacro __MementoCheckSettings

  !ifndef __MementoSectionIndex

    !error "MementoSectionDone already used!"

  !endif

  !define /MATH __MementoSectionIndexNext \
      ${__MementoSectionIndex} + 1

  Function __MementoSectionMarkNew${__MementoSectionIndex}

    ClearErrors
    ReadRegDWORD $0 ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}`

    ${If} ${Errors}

      !insertmacro SetSectionFlag `${${__MementoSectionLastSectionId}}` ${SF_BOLD}

    ${EndIf}

    GetFunctionAddress $0 __MementoSectionMarkNew${__MementoSectionIndexNext}
    Goto $0

  FunctionEnd

  Function __MementoSectionRestoreStatus${__MementoSectionIndex}

    ClearErrors
    ReadRegDWORD $0 ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}`

    !ifndef __MementoSectionUnselected

      ${If} ${Errors}
      ${OrIf} $0 != 0

        !insertmacro SelectSection `${${__MementoSectionLastSectionId}}`

      ${Else}

        !insertmacro UnselectSection `${${__MementoSectionLastSectionId}}`

      ${EndIf}

    !else

      !undef __MementoSectionUnselected

      ${If} ${Errors}
      ${OrIf} $0 == 0

        !insertmacro UnselectSection `${${__MementoSectionLastSectionId}}`

      ${Else}

        !insertmacro SelectSection `${${__MementoSectionLastSectionId}}`

      ${EndIf}

    !endif

    GetFunctionAddress $0 __MementoSectionRestoreStatus${__MementoSectionIndexNext}
    Goto $0

  FunctionEnd

  Function __MementoSectionSaveStatus${__MementoSectionIndex}

    ${If} ${SectionIsSelected} `${${__MementoSectionLastSectionId}}`

      WriteRegDWORD ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}` 1

    ${Else}

      WriteRegDWORD ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}` 0

    ${EndIf}

    GetFunctionAddress $0 __MementoSectionSaveStatus${__MementoSectionIndexNext}
    Goto $0

  FunctionEnd

  !undef __MementoSectionIndex
  !define __MementoSectionIndex ${__MementoSectionIndexNext}
  !undef __MementoSectionIndexNext

  !undef __MementoSectionLastSectionId

  !verbose pop

!macroend

!macro MementoSectionDone

  !verbose push
  !verbose 3

  !insertmacro __MementoCheckSettings

  Function __MementoSectionMarkNew${__MementoSectionIndex}
  FunctionEnd

  Function __MementoSectionRestoreStatus${__MementoSectionIndex}
  FunctionEnd

  Function __MementoSectionSaveStatus${__MementoSectionIndex}
  FunctionEnd

  !undef __MementoSectionIndex

  !verbose pop

!macroend

!macro MementoSectionRestore

  !verbose push
  !verbose 3

  !insertmacro __MementoCheckSettings

  Push $0
  Push $1
  Push $2
  Push $3

    # check for first usage

    ClearErrors

    ReadRegStr $0 ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` MementoSectionUsed

    ${If} ${Errors}

      # use script defaults on first run
      Goto done

    ${EndIf}

    # mark new components in bold
    
    Call __MementoSectionMarkNew1

    # mark section groups in bold

    StrCpy $0 0
    StrCpy $1 ""
    StrCpy $2 ""
    StrCpy $3 ""

    loop:

      ClearErrors

      ${If} ${SectionIsBold} $0

        ${If} $1 != ""

          !insertmacro SetSectionFlag $1 ${SF_BOLD}

        ${EndIf}

        ${If} $2 != ""

          !insertmacro SetSectionFlag $2 ${SF_BOLD}

        ${EndIf}

        ${If} $3 != ""

          !insertmacro SetSectionFlag $3 ${SF_BOLD}

        ${EndIf}

      ${ElseIf} ${Errors}

        Goto loop_end

      ${EndIf}

      ${If} ${SectionIsSectionGroup} $0

        ${If} $1 == ""

          StrCpy $1 $0

        ${ElseIf} $2 == ""

          StrCpy $2 $0

        ${ElseIf} $3 == ""

          StrCpy $3 $0

        ${EndIf}

      ${EndIf}

      ${If} ${SectionIsSectionGroupEnd} $0

        ${If} $3 != ""

          StrCpy $3 ""

        ${ElseIf} $2 != ""

          StrCpy $2 ""

        ${ElseIf} $1 != ""

          StrCpy $1 ""

        ${EndIf}

      ${EndIf}

      IntOp $0 $0 + 1

    Goto loop
    loop_end:

    # restore sections' status

    Call __MementoSectionRestoreStatus1

  # all done

  done:

  Pop $3
  Pop $2
  Pop $1
  Pop $0

  !verbose pop

!macroend

!macro MementoSectionSave

  !verbose push
  !verbose 3

  !insertmacro __MementoCheckSettings

  Push $0

    WriteRegStr ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` MementoSectionUsed ""
  
    Call __MementoSectionSaveStatus1

  Pop $0

  !verbose pop

!macroend



!endif # ___MEMENTO_NSH___

!verbose pop
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.