flair (FreeLancer Augmentation and Inspection at Runtime) is a client-side hook for the 2003 space sim Freelancer which requires no changes to the game files and no code to be injected into the game's process. It supports Freelancer running natively on Windows and also on Linux under WINE.
flair achieves this through a combination of hooking user input, reading process memory and window interaction using the Win32 API or Linux syscalls, and an understanding of Freelancer's static data provided by flint. Through these means flair allows the game's state to be inspected in real-time. It also allows the game client to be augmented, for example adding clipboard access to the game's chat box or implementing a custom command line interface.
flair is on PyPI:
pip install fl-flair
Alternatively you can install straight from this repository:
pip install https://github.com/biqqles/flair/archive/master.zip -U
Built wheels are also available under Releases, as is a changelog. flair requires Python 3.6 or higher.
flair includes a testing mode. To access it run python -m flair <path_to_freelancer>
. In this mode flair will load all built-in augmentations, print all events, and begin polling and printing the state of the game to the terminal. More on all of those later.
Basic usage of flair relies on two main principles:
- a state object, which allows access to the game's state in real time (
flair.state
) - a simple events system inspired by Qt's signals and slots mechanism (
flair.events
)
To use flair in your own programs, first call flair.set_install_dir
to bind flair to the Freelancer installation it is to hook. This creates a FreelancerState
instance at flair.state
. This will automatically begin polling the game (by default every 1 second). The polling frequency only affects when events are emitted - accesses to FreelancerState
will cause the immediate state of the game to be read (in most cases).
If you want to do more than read the state of the game and receive events, you should use the hook directly. An example of this situation is creating augmentations augmentations.
All these concepts are discussed in detail below.
Source: flair/inspect/state.py
As mentioned earlier, flair.FreelancerState
allows various parameters of the game's state to be accessed. Instances of this class have the following methods and properties:
Methods | Type | Notes |
---|---|---|
begin_polling(period) |
Begin polling the game's state and emitting events. Called upon instantiation by default. | |
stop_polling() |
Stop polling the game's state and emitting events. | |
refresh() |
Cause state variables to refresh themselves | |
Properties | Type | Notes |
running |
bool |
Whether an instance of the game is running |
foreground |
bool |
Whether an instance of the game is in the foreground and accepting input |
chat_box |
bool |
Whether the chat box is open |
account |
str |
The "name" (hash) of the currently active account, taken from the registry |
mouseover |
str |
See documentation in hook/process. |
character_loaded |
bool |
Whether a character is currently loaded (i.e. the player is logged in), either in SP or MP |
name |
Optional[str] |
The name of the currently active character if there is one, otherwise None |
credits |
Optional[int] |
The number of credits the active character has if there is one, otherwise None |
system |
Optional[str] |
The display name (e.g. "New London") of the system the active character is in if there is one, otherwise None |
base |
Optional[str] |
The display name (e.g. "The Ring") of the base the active character last docked at if there is one, otherwise None |
pos |
Optional[PosVector] |
The position vector of the active character if there is one, otherwise None |
docked |
Optional[bool] |
Whether the active character is presently docked at a base if there is one, otherwise None |
A FreelancerState
instance at flair.state
will be created when you call flair.set_install_dir
. You should not normally need to initialise FreelancerState
yourself. If for some reason you wanted to hook two instances of Freelancer running simultaneously, you should use two different Python processes.
Source: flair/inspect/events.py
Events are used by "connecting" them to functions (or vice-versa). flair automatically "emits" these events when necessary. For example flair.events.message_sent.connect(lambda message: print(message))
causes that lambda to be called every time flair emits the message_sent
signal, thereby printing the contents of the message to the terminal.
The connected function should take a keyword argument with the name specified in the schema column.
Event | Emitted when | Parameter schema |
---|---|---|
character_changed |
New character loaded | name : new character name |
account_changed |
Active (registry) account changed | account : new account code |
system_changed |
New system entered | system : New system display name |
docked |
Docked base (respawn point) changed | base : new base display name |
undocked |
Undocked from base | N/A |
credits_changed |
Credit balance changed | balance : new credit balance |
message_sent |
New message sent by player | message : message text |
chat_box_opened |
Chat box opened | message : message text |
chat_box_closed |
Chat box closed | message_sent : whether message sent |
freelancer_started |
Freelancer process launched | N/A |
freelancer_stopped |
Freelancer process closed | N/A |
switched_to_foreground |
Freelancer switched to foreground | N/A |
switched_to_background |
Freelancer switched to background | N/A |
flair/hook
contains the hook itself. It is separated into the following modules:
Source: flair/hook/input
Adds a hotkey which is only active when Freelancer is in the foreground.
Removes a hotkey that has been bound in the Freelancer window.
Queue text to be displayed to the user. If Freelancer is in the foreground and the chat box is closed, this will be shown immediately. Otherwise, the text will be shown as soon as both these conditions are true.
Inserts message
into the chat box and sends it. If private
is true, send to the Console.
Inject a key combination into the Freelancer window.
Inject text into the chat box.
Initialise hotkey hooks for Freelancer. Should be run when, and only when, the Freelancer window is brought into the foreground.
Release hotkey hooks for Freelancer. Should be run when, and only when, the Freelancer window is put into the background.
Handle the user opening the chat box. Emits the chat_box_opened
signal.
Handle the user closing the chat box. Emits the chat_box_closed
signal.
Handle a keyboard event while the chat box is open. Todo: handle arrow keys, copy and paste
Return (our best guess at) the current contents of the chat box. If it is closed, returns a blank string.
Return the hotkey configured to open the chat box.
Source: flair/hook/process
Return a handle to Freelancer's process.
Reads Freelancer's process memory.
Just as with string resources, strings are stored as UTF-16 meaning that the end of a string is marked by two consecutive null bytes. However, other bytes will be present in the buffer after these two nulls since it is of arbitrary size, and these confuse Python's builtin .decode and result in UnicodeDecodeError. So we can't use it.
Read a value from memory. key
refers to the key of an address in READ_ADDRESSES
Read a UTF-16 string from memory.
Read the name of the active character from memory.
Read the credit balance of the active character from memory.
Read the position of the active character from memory.
This is a really interesting address. It seems to store random, unconnected pieces of text that have been recently displayed or interacted with in the game. These range from console outputs to the names of bases and planets immediately upon jumping in or docking, to the prices of commodities in the trader screen, to mission "popups" messages, to the name of some solars and NPCs that are moused over while in space. With some imagination this can probably be put to some use...
Similar to mouseover, but usually contains tooltip text.
Read the last message sent by the player from memory
Read the state of the chat box from memory.
Read whether a character is loaded (whether in SP or MP).
Read whether the active character is docked.
Source: flair/hook/window
Returns a non-zero window handle to Freelancer if a window exists, otherwise, returns zero.
Reports whether Freelancer is running.
Reports whether Freelancer is in the foreground and accepting input.
Return the screen coordinates for the contents ("client"; excludes window decorations) of a Freelancer window.
Remove the borders and titlebar from the game while running in windowed mode.
Bring Freelancer's window into the foreground and make it the active window.
Source: flair/hook/storage
Returns the currently active account's code ("name") from the registry. Note that Freelancer reads the account from the registry at the time of server connect.
Get the name of a key from its VK (virtual key) code.
Get Freelancer's current key map as defined in UserKeyMap.ini, in a format understood by the keyboard
module.
Source: flair/augment
"Augmentations" are modules that augment the game client. flair includes several such examples built-in.
To create an augmentation, subclass flair.augment.Augmentation
. Simply override the methods load()
and unload()
. These are run when augmentations are "loaded" into the game client and "unloaded" respectively. Connect up the events you need to use and add any other setup in these methods.
Adds clipboard access to the chat box. Use Ctrl+Shift+C to copy the contents of the chat box and Ctrl+Shift+V to paste text to it.
Adds a basic command-line interface to the game.
The following commands are implemented:
..date
: print the local date and time..sector
: print the current sector and system..eval
: evaluate the given expression with Python..quit
: quit the game..help
: show this help message
Adds proper screenshot functionality to the game, similar to that found in games like World of Warcraft. Screenshots are automatically named with a timestamp and the system name and saved to My Games/Freelancer/Screenshots
with the character name as the directory. Screenshots are taken using Ctrl+PrintScreen
.
- Reimplementing Wizou's multiplayer code
- Increasing the robustness of determining the chat box contents - currently it does not handle arrow keys
- Getting system and base is currently pretty hacky, and it often requires a dock and undock to set both after loading a character