# Segments

The PortableApps.com Launcher source code is divided up into lots of “segments”, each of which can run a number of “hooks”. This aids with code separation of different pieces of functionality, by grouping code by what it does rather than when it executes, providing a synergistically value-adding, mutually-beneficial strategic partnership between the developer and... and... well, whatever’s left. (A more useful workflow anyway. I’ll leave the marketing talk to the marketers.)

Why did you write that nonsense?

A few proposals have been put forward about such things as this, but as usual analysts have been unable to agree on the issue.

The simple answer is that we software developers have a quirky sense of humour. How else could you explain things like the recursive acronyms that all developers so love? At times it can get dull, just writing a program which does what it’s meant to do and that’s all, and so developers make time to put in what are commonly known as “easter eggs”: hidden functionality which they generally find amusing and which break the monotony of writing good software.

In particular here the point is writing documentation. Writing this documentation for the PortableApps.com Launcher is taking far longer than the actual writing of the code did in the first place (orders of magnitude longer). And so at times I decide to put strange things in for the fun of it.

It’s just one of those illogicalities of software developers.

## Hooks

Here is a list of the hooks which can be executed:

- `.onInit`: things which must go in the NSIS `.onInit` function (see the NSIS documentation for details about `.onInit`)
- `Init`: load data into variables, abort the launcher if necessary, and do anything else of a “starting up” nature”.
- `Pre`: do things to make the application portable which must always be done, whether the launcher is dealing with a primary or secondary instance of the application.
- `PrePrimary`: actions to make the application portable which should only be run with a primary instance of an application.
- `PreSecondary`: actions to make the application portable which should only be run with a secondary or subsequent instance of an application. I haven’t yet thought of an instance when this would be useful but there could be.
- `PreExec`: just before the program gets executed, there’s an opportunity to do something here. Try to use the `Pre` hook instead.
- `PreExecPrimary`: `PreExec` for primary instances.
- `PreExecSecondary`: `PreExec` for secondary and subsequent instances.
- `Post`: clean up the application and handle restoration of settings and related things in here.
- `PostPrimary`: `Post` for primary instances.
- `PostSecondary`: `Post` for secondary and subsequent instances.
- `Unload`: unload plug-ins and clean up traces from the launcher itself.

## Customisations

If you ever need to disable a segment or hook, you can do so. In general though if you can possibly avoid doing it you should; you can very easily break the PortableApps.com Launcher by disabling certain things. See Writing a custom segment for details.

## List of core segments

Here is the current list of segments included in the PortableApps.com Launcher:

- Core: various core functionality
- DirectoriesCleanup: `[DirectoriesCleanupIfEmpty]` and `[DirectoriesCleanupForce]`
- DirectoriesMove: `[DirectoriesMove]`
- DirectoryMoving: coping with moving the portable app package – `[Launch]:DirectoryMoveOK`
- DriveLetter: Drive variables
- Environment: `[Environment]`
- ExecString: constructing the string for execution
- FileWrite: `[FileWriteN]`
- FilesMove: `[FilesMove]`
- InstanceManagement: managing multiple instances of portable apps
- Java: guess
- Language: launcher language selection for message boxes and language switching (see Languages)
- OperatingSystem: `[Launch]:MinOS` and `[Launch]:MaxOS`
- Qt: `[QtKeysCleanup]` (see also Making Qt applications portable)
- RefreshShellIcons: `[Launch]:RefreshShellIcons`
- Registry: `[Activate]:Registry` and helper utilities for other Registry segments. See also Dealing with the registry for this and the other Registry segments listed here.
- RegistryCleanup: `[RegistryCleanupIfEmpty]` and `[RegistryCleanupForce]`
- RegistryKeys: `[RegistryKeys]`
- RegistryValueBackupDelete: `[RegistryValueBackupDelete]`
- RegistryValueWrite: `[RegistryValueWrite]`
- RunAsAdmin: `[Launch]:RunAsAdmin` et al.
- RunLocally: live mode support (mainly a user feature, but slightly configurable with `[LiveMode]`)
- Services: services (currently not functional and disabled)
- Settings: Management of `Data\settings` and copying default data from `App\DefaultData` to the `Data` directory
- SplashScreen: Splash screens
- Temp: management of the TEMP directory (mainly controlled by `[Launch]:CleanTemp`)
- Variables: internal functions for providing environment variables and most of the Directory variables
- WorkingDirectory: `[Launch]:WorkingDirectory`