Debian Codegraph @

Include/Memento.nsh

raw
1 !verbose push
2 !verbose 3
3
4 !include LogicLib.nsh
5 !include Sections.nsh
6
7 !ifndef ___MEMENTO_NSH___
8 !define ___MEMENTO_NSH___
9
10 #####################################
11 ### Memento ###
12 #####################################
13
14 /*
15
16 Memento is a set of macros that allow installers to remember user selection
17 across separate runs of the installer. Currently, it can remember the state
18 of sections and mark new sections as bold. In the future, it'll integrate
19 InstallOptions and maybe even the Modern UI.
20
21 A usage example can be found in `Examples\Memento.nsi`.
22
23 */
24
25 #####################################
26 ### Usage Instructions ###
27 #####################################
28
29 /*
30
31 1. Declare usage of Memento by including Memento.nsh at the top of the script.
32
33 !include Memento.nsh
34
35 2. Define MEMENTO_REGISTRY_ROOT and MEMENTO_REGISTRY_KEY with the a registry key
36 where sections' state should be saved.
37
38 !define MEMENTO_REGISTRY_ROOT HKLM
39 !define MEMENTO_REGISTRY_KEY \
40 Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProgram
41
42 3. Replace Section with ${MementoSection} and SectionEnd with ${MementoSectionEnd}
43 for sections that whose state should be remembered by Memento.
44
45 For sections that should be unselected by default, use ${MementoSection}'s
46 brother - ${MementoUnselectedSection}.
47
48 Sections that don't already have an identifier must be assigned one.
49
50 Section identifiers must stay the same across different versions of the
51 installer or their state will be forgotten.
52
53 4. Use ${MementoSectionDone} after the last ${MementoSection}.
54
55 5. Add a call to ${MementoSectionRestore} to .onInit to restore the state
56 of all sections from the registry.
57
58 Function .onInit
59
60 ${MementoSectionRestore}
61
62 FunctionEnd
63
64 6. Add a call to ${MementoSectionSave} to .onInstSuccess to save the state
65 of all sections to the registry.
66
67 Function .onInstSuccess
68
69 ${MementoSectionSave}
70
71 FunctionEnd
72
73 7. Tattoo the location of the chosen registry key on your arm.
74
75 */
76
77 #####################################
78 ### User API ###
79 #####################################
80
81 ;
82 ; ${MementoSection}
83 ;
84 ; Defines a section whose state is remembered by Memento.
85 ;
86 ; Usage is similar to Section.
87 ;
88 ; ${MementoSection} "name" "id"
89 ;
90 ; ${MementoSectionEx} "[flags]" "name" "memento_id" "section_id"
91 ;
92
93 !define MementoSection "!insertmacro MementoSection"
94 !define MementoSectionEx "!insertmacro MementoSectionEx"
95
96 ;
97 ; ${MementoSectionEnd}
98 ;
99 ; Ends a section previously opened using ${MementoSection}.
100 ;
101 ; Usage is similar to SectionEnd.
102 ;
103 ; ${MementoSection} "name" "id"
104 ; # some code...
105 ; ${MementoSectionEnd}
106 ;
107
108 ;
109 ; ${MementoUnselectedSection}
110 ;
111 ; Defines a section whose state is remembered by Memento and is
112 ; unselected by default.
113 ;
114 ; Usage is similar to Section with the /o switch.
115 ;
116 ; ${MementoUnselectedSection} "name" "id"
117 ;
118
119 !define MementoUnselectedSection "!insertmacro MementoUnselectedSection"
120
121 ;
122 ; ${MementoSectionEnd}
123 ;
124 ; Ends a section previously opened using ${MementoSection}.
125 ;
126 ; Usage is similar to SectionEnd.
127 ;
128 ; ${MementoSection} "name" "id"
129 ; # some code...
130 ; ${MementoSectionEnd}
131 ;
132
133 !define MementoSectionEnd "!insertmacro MementoSectionEnd"
134
135 ;
136 ; ${MementoSectionDone}
137 ;
138 ; Used after all ${MementoSection} have been set.
139 ;
140 ; ${MementoSection} "name1" "some_id1"
141 ; # some code...
142 ; ${MementoSectionEnd}
143 ;
144 ; ${MementoSection} "name2" "some_id2"
145 ; # some code...
146 ; ${MementoSectionEnd}
147 ;
148 ; ${MementoSection} "name3" "some_id3"
149 ; # some code...
150 ; ${MementoSectionEnd}
151 ;
152 ; ${MementoSectionDone}
153 ;
154
155 !define MementoSectionDone "!insertmacro MementoSectionDone"
156
157 ;
158 ; ${MementoSectionRestore}
159 ;
160 ; Restores the state of all Memento sections from the registry.
161 ;
162 ; Commonly used in .onInit.
163 ;
164 ; Function .onInit
165 ;
166 ; ${MementoSectionRestore}
167 ;
168 ; FunctionEnd
169 ;
170
171 !define MementoSectionRestore "!insertmacro MementoSectionRestore"
172
173 ;
174 ; ${MementoSectionSave}
175 ;
176 ; Saves the state of all Memento sections to the registry.
177 ;
178 ; Commonly used in .onInstSuccess.
179 ;
180 ; Function .onInstSuccess
181 ;
182 ; ${MementoSectionSave}
183 ;
184 ; FunctionEnd
185 ;
186
187 !define MementoSectionSave "!insertmacro MementoSectionSave"
188
189 ;
190 ; MementoSection<ReadWrite><Int|Marker>
191 ;
192 ; Replaceable macros that allow custom storage methods to be used.
193 ;
194 !define /IfNDef MementoSectionStorageSeparator '_'
195
196 !ifmacrondef MementoSectionReadInt
197 !define __MementoSectionStdRegReadWrite
198 !define /IfNDef MementoSectionStoragePrefix "MementoSection"
199
200 !macro MementoSectionReadInt outvar name
201 ReadRegDWord ${outvar} ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `${MementoSectionStoragePrefix}${name}`
202 !macroend
203
204 !macro MementoSectionWriteInt name val
205 WriteRegDWord ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `${MementoSectionStoragePrefix}${name}` `${val}`
206 !macroend
207
208 !macro MementoSectionReadMarker outvar name
209 ReadRegStr ${outvar} ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `${MementoSectionStoragePrefix}${name}`
210 !macroend
211
212 !macro MementoSectionWriteMarker name
213 WriteRegStr ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `${MementoSectionStoragePrefix}${name}` ``
214 !macroend
215
216 !endif
217
218 #####################################
219 ### Internal Defines ###
220 #####################################
221
222 !define __MementoSectionIndex 1
223
224 #####################################
225 ### Internal Macros ###
226 #####################################
227
228 !macro __MementoCheckSettings
229
230 !ifdef __MementoSectionStdRegReadWrite
231 !ifndef MEMENTO_REGISTRY_ROOT | MEMENTO_REGISTRY_KEY
232
233 !error "MEMENTO_REGISTRY_ROOT and MEMENTO_REGISTRY_KEY must be defined before using any of Memento's macros"
234
235 !endif
236 !endif
237
238 !macroend
239
240 !macro __MementoSection flags name mementoid sectionid
241
242 !insertmacro __MementoCheckSettings
243
244 !ifndef __MementoSectionIndex
245
246 !error "MementoSectionDone already used!"
247
248 !endif
249
250 !define __MementoSectionLastMementoId `${mementoid}`
251 !define __MementoSectionLastSectionId `${sectionid}`
252 !define __MementoSectionLastFlags `${flags}`
253
254 !verbose pop
255
256 Section ${flags} `${name}` `${sectionid}`
257
258 !verbose push
259 !verbose 3
260
261 !macroend
262
263 #####################################
264 ### User Macros ###
265 #####################################
266
267 !macro MementoSectionEx flags name mid sid
268
269 !verbose push 3
270
271 !insertmacro __MementoSection `${flags}` `${name}` `${mid}` `${sid}`
272
273 !verbose pop
274
275 !macroend
276
277 !macro MementoSection name id
278
279 !verbose push 3
280
281 !insertmacro __MementoSection "" `${name}` `${id}` `${id}`
282
283 !verbose pop
284
285 !macroend
286
287 !macro MementoUnselectedSection name id
288
289 !verbose push 3
290
291 !insertmacro __MementoSection /o `${name}` `${id}` `${id}`
292
293 !verbose pop
294
295 !macroend
296
297 !macro MementoSectionEnd
298
299 SectionEnd
300
301 !verbose push
302 !verbose 3
303
304 !insertmacro __MementoCheckSettings
305
306 !ifndef __MementoSectionIndex
307
308 !error "MementoSectionDone already used!"
309
310 !endif
311
312 !define /MATH __MementoSectionIndexNext \
313 ${__MementoSectionIndex} + 1
314
315 Function __MementoSectionMarkNew${__MementoSectionIndex}
316
317 ClearErrors
318 !insertmacro MementoSectionReadInt $0 `${MementoSectionStorageSeparator}${__MementoSectionLastMementoId}`
319
320 ${If} ${Errors}
321
322 !insertmacro SetSectionFlag `${${__MementoSectionLastSectionId}}` ${SF_BOLD}
323
324 ${EndIf}
325
326 GetFunctionAddress $0 __MementoSectionMarkNew${__MementoSectionIndexNext}
327 Goto $0
328
329 FunctionEnd
330
331 Function __MementoSectionRestoreStatus${__MementoSectionIndex}
332
333 ClearErrors
334 !insertmacro MementoSectionReadInt $0 `${MementoSectionStorageSeparator}${__MementoSectionLastMementoId}`
335
336 !searchparse /ignorecase /noerrors `${__MementoSectionLastFlags}-` `/o` __MementoSectionUnselected
337
338 !ifndef __MementoSectionUnselected
339
340 ${If} ${Errors}
341 ${OrIf} $0 != 0
342
343 !insertmacro SelectSection `${${__MementoSectionLastSectionId}}`
344
345 ${Else}
346
347 !insertmacro UnselectSection `${${__MementoSectionLastSectionId}}`
348
349 ${EndIf}
350
351 !else
352
353 !undef __MementoSectionUnselected
354
355 ${If} ${Errors}
356 ${OrIf} $0 == 0
357
358 !insertmacro UnselectSection `${${__MementoSectionLastSectionId}}`
359
360 ${Else}
361
362 !insertmacro SelectSection `${${__MementoSectionLastSectionId}}`
363
364 ${EndIf}
365
366 !endif
367
368 GetFunctionAddress $0 __MementoSectionRestoreStatus${__MementoSectionIndexNext}
369 Goto $0
370
371 FunctionEnd
372
373 Function __MementoSectionSaveStatus${__MementoSectionIndex}
374
375 ${If} ${SectionIsSelected} `${${__MementoSectionLastSectionId}}`
376
377 !insertmacro MementoSectionWriteInt `${MementoSectionStorageSeparator}${__MementoSectionLastMementoId}` 1
378
379 ${Else}
380
381 !insertmacro MementoSectionWriteInt `${MementoSectionStorageSeparator}${__MementoSectionLastMementoId}` 0
382
383 ${EndIf}
384
385 GetFunctionAddress $0 __MementoSectionSaveStatus${__MementoSectionIndexNext}
386 Goto $0
387
388 FunctionEnd
389
390 !undef __MementoSectionIndex
391 !define __MementoSectionIndex ${__MementoSectionIndexNext}
392 !undef __MementoSectionIndexNext
393
394 !undef __MementoSectionLastMementoId
395 !undef __MementoSectionLastSectionId
396 !undef __MementoSectionLastFlags
397
398 !verbose pop
399
400 !macroend
401
402 !macro MementoSectionDone
403
404 !verbose push
405 !verbose 3
406
407 !insertmacro __MementoCheckSettings
408
409 Function __MementoSectionMarkNew${__MementoSectionIndex}
410 FunctionEnd
411
412 Function __MementoSectionRestoreStatus${__MementoSectionIndex}
413 FunctionEnd
414
415 Function __MementoSectionSaveStatus${__MementoSectionIndex}
416 FunctionEnd
417
418 !undef __MementoSectionIndex
419
420 !verbose pop
421
422 !macroend
423
424 !macro MementoSectionRestore
425
426 !verbose push
427 !verbose 3
428
429 !insertmacro __MementoCheckSettings
430
431 Push $0
432 Push $1
433 Push $2
434 Push $3
435
436 # check for first usage
437
438 ClearErrors
439 !insertmacro MementoSectionReadMarker $0 `Used`
440
441 ${If} ${Errors}
442
443 # use script defaults on first run
444 Goto done
445
446 ${EndIf}
447
448 # mark new components in bold
449
450 Call __MementoSectionMarkNew1
451
452 # mark section groups in bold
453
454 StrCpy $0 0
455 StrCpy $1 ""
456 StrCpy $2 ""
457 StrCpy $3 ""
458
459 loop:
460
461 ClearErrors
462
463 ${If} ${SectionIsBold} $0
464
465 ${If} $1 != ""
466
467 !insertmacro SetSectionFlag $1 ${SF_BOLD}
468
469 ${EndIf}
470
471 ${If} $2 != ""
472
473 !insertmacro SetSectionFlag $2 ${SF_BOLD}
474
475 ${EndIf}
476
477 ${If} $3 != ""
478
479 !insertmacro SetSectionFlag $3 ${SF_BOLD}
480
481 ${EndIf}
482
483 ${ElseIf} ${Errors}
484
485 Goto loop_end
486
487 ${EndIf}
488
489 ${If} ${SectionIsSectionGroup} $0
490
491 ${If} $1 == ""
492
493 StrCpy $1 $0
494
495 ${ElseIf} $2 == ""
496
497 StrCpy $2 $0
498
499 ${ElseIf} $3 == ""
500
501 StrCpy $3 $0
502
503 ${EndIf}
504
505 ${EndIf}
506
507 ${If} ${SectionIsSectionGroupEnd} $0
508
509 ${If} $3 != ""
510
511 StrCpy $3 ""
512
513 ${ElseIf} $2 != ""
514
515 StrCpy $2 ""
516
517 ${ElseIf} $1 != ""
518
519 StrCpy $1 ""
520
521 ${EndIf}
522
523 ${EndIf}
524
525 IntOp $0 $0 + 1
526
527 Goto loop
528 loop_end:
529
530 # restore sections' status
531
532 Call __MementoSectionRestoreStatus1
533
534 # all done
535
536 done:
537
538 Pop $3
539 Pop $2
540 Pop $1
541 Pop $0
542
543 !verbose pop
544
545 !macroend
546
547 !macro MementoSectionSave
548
549 !verbose push
550 !verbose 3
551
552 !insertmacro __MementoCheckSettings
553
554 Push $0
555
556 !insertmacro MementoSectionWriteMarker `Used`
557
558 Call __MementoSectionSaveStatus1
559
560 Pop $0
561
562 !verbose pop
563
564 !macroend
565
566
567
568 !endif # ___MEMENTO_NSH___
569
570 !verbose pop