TmEssentials 2.9.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TmEssentials --version 2.9.0
                    
NuGet\Install-Package TmEssentials -Version 2.9.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TmEssentials" Version="2.9.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TmEssentials" Version="2.9.0" />
                    
Directory.Packages.props
<PackageReference Include="TmEssentials" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TmEssentials --version 2.9.0
                    
#r "nuget: TmEssentials, 2.9.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TmEssentials@2.9.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TmEssentials&version=2.9.0
                    
Install as a Cake Addin
#tool nuget:?package=TmEssentials&version=2.9.0
                    
Install as a Cake Tool

TmEssentials

NuGet NuGet GitHub release (latest by date including pre-releases) Code Coverage

Welcome to TmEssentials!

A light-weight .NET library with the essentials of Trackmania and ManiaPlanet:

  • Race times with TimeInt32 and TimeSingle, formatted and parsed like in the game
  • Text formatting that reads $ codes the same way as the game, to plain text, HTML or ANSI
  • Account IDs and logins converted both ways
  • Map UIDs made and decoded like the game does
  • Fully managed, trimmable and NativeAOT compatible
  • Runs anywhere from .NET Framework 4.6.2 to .NET 11

Try the text formatting in the web sample.

Packages

Package Description
TmEssentials Times, text formatting, accounts and map UIDs
TmEssentials.Razor The FormattedText Blazor component, with a stylesheet for the game's icons
dotnet add package TmEssentials

Framework support

TmEssentials targets .NET 11, 10, 9 and 8, .NET Standard 2.1 and 2.0, and .NET Framework 4.6.2. TmEssentials.Razor targets .NET 11, 10, 9 and 8.

Feature .NET 8+ .NET Standard 2.0/2.1 .NET Framework 4.6.2+
Times, text formatting, accounts, map UIDs Yes Yes Yes
IParsable and ISpanParsable on times Yes No No
System.Text.Json converters Yes No No
HttpClient.HeadAsync Yes Yes No

Time

TimeInt32 and TimeSingle

The game stores race times as whole milliseconds in an int, and other times, like MediaTracker keys, as seconds in a float. TimeInt32 and TimeSingle hold exactly that in 4 bytes, half the size of TimeSpan. They have the familiar TimeSpan members (Days, TotalSeconds, Add, Duration, operators and more), share the ITime interface, and ToString() writes the time in the Trackmania format.

var time = new TimeInt32(TotalMilliseconds: 36217);

time.ToTmString();                    // 0:36.217 (ToString() does the same)
time.ToTmString(useHundredths: true); // 0:36.21
time.ToTmString(useApostrophe: true); // 0'36''217
time.ToTmString(compact: true);       // 36.217, handy for time differences

new TimeInt32(3_723_456).ToString();  // 1:02:03.456

TimeInt32? noTime = null;
noTime.ToTmString();                    // -:--.---
noTime.ToTmString(useHundredths: true); // -:--.--
noTime.ToTmString(useApostrophe: true); // -'--''---

TimeSingle works the same way. It rounds to the nearest millisecond when formatting, so float errors don't show:

var time = new TimeSingle(TotalSeconds: 23.51f);

time.ToTmString();                    // 0:23.510
time.ToTmString(useHundredths: true); // 0:23.51

new TimeSingle(0.251f).ToString();    // 0:00.251, even though the float is 0.25099998

Parsing

Both types parse the formats that ToString() writes, with milliseconds or hundredths and an optional minus sign. Parsing does not depend on the culture.

TimeInt32.Parse("1:23.456");                 // 83456 milliseconds
TimeInt32.Parse("36.217");                   // 36217 milliseconds
TimeInt32.TryParse("abc", out var result);   // false

TimeSingle.Parse("1:23.456");                // 83.456 seconds

A number without a colon or a dot is read as a fraction of a second, like TimeSpan.ParseExact with the FFFFFFF format. 5 is 500 milliseconds, 05 is 50 milliseconds and 500 is 500 milliseconds.

Conversions and overflow

TimeInt32 behaves like TimeSpan when a value doesn't fit: it throws OverflowException instead of wrapping around. Its range is about ±24.8 days.

TimeSpan timeSpan = new TimeInt32(1234);       // implicit
TimeSingle single = new TimeInt32(1234);       // implicit

var a = (TimeInt32)TimeSpan.FromSeconds(1.2345); // 1234 ms, truncated to whole milliseconds
var b = (TimeInt32)new TimeSingle(0.251f);       // 251 ms, rounded to the nearest millisecond
var c = TimeInt32.FromSeconds(0.251f);           // 251 ms, rounded to the nearest millisecond

new TimeInt32(251) == new TimeSingle(0.251f);    // true, compared by TotalSeconds

TimeInt32.MaxValue + new TimeInt32(1);           // OverflowException
TimeInt32.FromDays(30);                          // OverflowException

These are breaking changes from 2.8.0: conversions to TimeInt32 from TimeSpan and TimeSingle need a cast, the From methods round instead of truncating, and arithmetic throws on overflow. See the releases for the full list.

TimeSpan extensions

The same formatting is available on TimeSpan:

TimeSpan.FromMilliseconds(83456).ToTmString(); // 1:23.456

TimeSpan? noTime = null;
noTime.ToTmString();                           // -:--.---

TimeSpan.FromTicks(15_000).ToMilliseconds();   // 1, the total milliseconds truncated to an int

JSON and TypeConverter

On .NET 8+, JsonTimeInt32Converter and JsonTimeSingleConverter read and write the times as JSON numbers: TimeInt32 as milliseconds and TimeSingle as seconds. They use no reflection, so they also work in source-generated contexts.

var options = new JsonSerializerOptions
{
    Converters = { new JsonTimeInt32Converter(), new JsonTimeSingleConverter() }
};

JsonSerializer.Serialize(new { Time = new TimeInt32(36217), Key = new TimeSingle(1.5f) }, options);
// {"Time":36217,"Key":1.5}

Both types also have a TypeConverter, so configuration and input binding can read times from strings like 1:23.456.

Text formatting

TextFormatter reads formatting codes the same way as the formatting parser of ManiaPlanet:

  • Colors with 1 to 3 hex digits ($f is $f00)
  • $< and $> save and restore up to 7 styles, and $z and $g reset to the style saved by $<
  • $l, $h and $p links, with or without [url]
  • The $$, $[ and $] escapes
  • Carriage returns and a trailing $ are removed, like in the game

Deformat

TextFormatter.Deformat() removes the formatting codes:

TextFormatter.Deformat("$F00T$D01M$C13U$A14.$815K$727r$528a$329z$23By$03CC$03Co$04Bl$059o$068r$077s$085 $094v$0A30$0B1.$0C01");
// TMU.KrazyColors v0.1

TextFormatter.Deformat("$o$f00Big $<$iRed$> $$5 $l[https://trackmania.io]link$l");
// Big Red $5 link

HTML

TextFormatter.FormatHtml() turns the colors and styles into inline CSS, and the links into <a> elements when they are allowed:

TextFormatter.FormatHtml("$f00Red $l[https://trackmania.io]link$l", allowLinks: true);
// <span style="color:#f00">Red </span><a href="https://trackmania.io" style="color:#f00">link</a>

TextFormatter.FormatHtml("$o$iBold italic$z $sShadow");
// <span style="font-weight:bold;font-style:italic">Bold italic</span> <span style="text-shadow:1px 1px 2px black">Shadow</span>

TextFormatter.FormatHtml("$h[maniaplanet]Home$h", allowManialinks: true);
// <a href="maniaplanet:///:maniaplanet">Home</a>

Links are safe to show from untrusted text. Only http:// and https:// URLs become links, URLs without a scheme (like www.trackmania.com) get http:// in front, and URLs with any other scheme (like javascript:) are shown as plain text.

Icons

The game renders the private use characters U+F000 to U+F2E0 as Font Awesome 4.7 icons (see Text Styling). Pass a CSS class to wrap them in <span> elements, so that a page can display them with the icon font:

TextFormatter.FormatHtml(" Winner", allowLinks: false, allowManialinks: false, iconCssClass: "fa");
// <span class="fa"></span> Winner

fa works with the Font Awesome 4.7 stylesheet, or use tm-icon with the stylesheet of TmEssentials.Razor. TextFormatter.IsFontAwesomeIcon(char) tells whether a character is one of these icons.

ANSI

TextFormatter.FormatAnsi() turns the colors into the closest ANSI colors for terminals:

TextFormatter.FormatAnsi("$f00Red $0f0Green $zDefault");
// \x1B[1;31mRed \x1B[1;32mGreen \x1B[39m\x1B[22mDefault\x1B[39m\x1B[22m

Regex pattern

TextFormatter.FormattingCodeRegexPattern matches one formatting code, for when you need the codes themselves:

Regex.Matches("$f00Red $l[https://trackmania.io]link$l", TextFormatter.FormattingCodeRegexPattern);
// $f00, $l[https://trackmania.io], $l

A regex cannot know whether a link is open, so inside a link it can read more than the formatters do. Prefer the TextFormatter methods when you only need the result.

Blazor

TmEssentials.Razor provides the FormattedText component. Add @using TmEssentials.Razor to _Imports.razor, then:

<FormattedText Text="$f00Red $l[https://trackmania.io]link$l" AllowLinks="true" />

To display the icons, add the stylesheet that comes with the package (it includes the Font Awesome 4.7 font, licensed under the SIL OFL 1.1):

<link rel="stylesheet" href="_content/TmEssentials.Razor/tm-essentials.css" />

Set IconCssClass="fa" if the page uses the Font Awesome 4.7 stylesheet instead, or IconCssClass="@null" to not wrap the icons.

Accounts

AccountUtils converts between account IDs and logins:

AccountUtils.ToAccountId("v89i_w-eQKq5JBG5xwuKCQ");                     // bfcf62ff-0f9e-40aa-b924-11b9c70b8a09
AccountUtils.ToLogin(Guid.Parse("bfcf62ff-0f9e-40aa-b924-11b9c70b8a09")); // v89i_w-eQKq5JBG5xwuKCQ

ToAccountId throws FormatException when the login does not decode to exactly 16 bytes.

Map UIDs

MapUtils makes map UIDs the same way as the game: a new UUID and a CRC32 (20 bytes) written as one number in base 63, with _0-9a-zA-Z as the digits. They are most often 27 characters long, sometimes shorter.

MapUtils.GenerateMapUid();             // random, like az8D4K6OKOuyGtUjHV_ui39x4xi
MapUtils.GenerateMapUid(seed: 1112);   // OQNeAcbA0Uh7ftCm_tRbW7wezdb, the same for the same seed
MapUtils.GenerateMapUid(uuid, crc32);  // exactly what the game makes from these

MapUtils.TryDecodeMapUid("az8D4K6OKOuyGtUjHV_ui39x4xi", out var uuid, out var crc32);
// true, eabd691c-a87f-428c-9e56-608e21bc91dd, 0xCF9B930D

The game uses the CRC32 of the map's serialized data, which needs the whole map, so the random overloads use a random number in its place.

HTTP HEAD requests

HttpClientExtensions.HeadAsync() sends a HEAD request, which is useful for checking a file's size or last modified date without downloading it:

using var response = await http.HeadAsync("https://example.com/map.Map.Gbx");
var lastModified = response.Content.Headers.LastModified;

Benchmarks

The benchmarks are in Tests/TmEssentials.Benchmarks. Run them with:

dotnet run -c Release --project Tests/TmEssentials.Benchmarks/TmEssentials.Benchmarks.csproj

Build

Make sure you have the .NET 11 SDK installed, then:

git clone https://github.com/BigBang1112/tm-essentials.git
cd tm-essentials
dotnet build
dotnet test --solution TmEssentials.slnx

The browser tests of TmEssentials.Razor use the installed Google Chrome, and the .NET Framework tests only run on Windows.

License

  • TmEssentials and TmEssentials.Razor are licensed under the MIT License.
  • The Font Awesome 4.7 font included in TmEssentials.Razor is licensed under the SIL OFL 1.1.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.2

  • .NETStandard 2.0

  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on TmEssentials:

Package Downloads
GBX.NET

General purpose library for Gbx files - data from Nadeo games like Trackmania or Shootmania. It supports high performance serialization and deserialization of 500+ Gbx classes.

ManiaAPI.NadeoAPI

Wrapper for the official in-game Nadeo API from Trackmania released in 2020. Part of the ManiaAPI.NET library set.

ManiaAPI.TMX

Wrapper for the TMX API, currently only on tm-exchange.com. Part of the ManiaAPI.NET library set.

ManiaAPI.TrackmaniaIO

Wrapper for the Trackmania.io API. Part of the ManiaAPI.NET library set.

ManiaAPI.TrackmaniaWS

Wrapper for the old TMF web API (ws.trackmania.com). Part of the ManiaAPI.NET library set.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on TmEssentials:

Repository Stars
BigBang1112/gbx-net
General purpose library for Gbx files - data from Nadeo games like Trackmania or Shootmania, written in C#/.NET. It supports high performance serialization and deserialization of 500+ Gbx classes.
Version Downloads Last Updated
2.9.1 328 11/17/2026
2.9.0 265 11/17/2026
2.8.0 2,440 5/9/2026
2.7.0 6,122 4/11/2026
2.6.2 1,501 3/15/2026
2.6.1 241 2/7/2026
2.6.0 4,556 1/8/2026
2.5.3 1,700 10/16/2025
2.5.2 2,654 3/28/2025
2.5.0 6,794 7/20/2024
2.4.2 3,443 4/14/2024
2.4.1 1,959 1/24/2024
2.4.0 3,046 12/21/2023
2.3.3 675 7/5/2023
2.3.2 7,872 8/30/2022
2.3.1 3,181 8/10/2022
2.3.0 1,955 8/4/2022
2.2.0 2,223 5/30/2022
2.1.3 4,152 4/2/2022
2.1.2 2,908 2/19/2022
Loading failed

- **Breaking:** `TimeInt32` conversions from `TimeSpan` and `TimeSingle` are now explicit, so they need a cast
 - `(TimeInt32)timeSpan` truncates to whole milliseconds and throws `OverflowException` outside the `TimeInt32` range (about ±24.8 days)
 - `(TimeInt32)timeSingle` rounds to the nearest millisecond, like `TimeSingle.ToTimeInt32()` and `TimeInt32.Add(ITime)` with a `TimeSingle`
- **Breaking:** `TimeInt32` arithmetic throws `OverflowException` instead of wrapping around, like `TimeSpan`
 - Applies to the `+`, `-` and `*` operators, `Add`, `Multiply(int)`, `Negate`, `Duration` and the constructors with days, hours, minutes and seconds
- **Breaking:** `TimeInt32.FromDays`, `FromHours`, `FromMinutes`, `FromSeconds` and `FromMilliseconds` round to the nearest millisecond, like `TimeSpan` on .NET Framework, instead of truncating
 - For example, `TimeInt32.FromSeconds(0.251f)` is now 251 milliseconds instead of 250
 - They and `FromTicks` throw `OverflowException` outside the `TimeInt32` range instead of returning a wrong time, and `ArgumentException` for NaN
- **Breaking:** `TextFormatter.Deformat`, `FormatAnsi` and `FormatHtml` now read formatting codes the same way as ManiaPlanet, so some results differ from 2.8.0
 - Added 1 and 2 digit colors (`$f` is `$f00`), `$p` manialinks, the `$[` and `$]` escapes, and `$m` for normal width
 - `$<` and `$>` save and restore up to 7 styles, and `$z` and `$g` reset to the style saved by `$<`
 - Carriage returns and a trailing `$` are removed
- **Breaking:** `MapUtils.GenerateMapUid` overloads now generate UIDs the same way as the game, so UIDs from a seed differ from 2.8.0 and some UIDs are shorter than 27 characters
- **Added `MapUtils.GenerateMapUid(Guid, uint)` and `MapUtils.TryDecodeMapUid`** to make a map UID from a UUID and a CRC32 like the game, and to read them back from a UID
- **Added Font Awesome 4.7 icon support** to `TextFormatter.FormatHtml` with an `iconCssClass` parameter that wraps icon characters in `span` elements of that class
 - Added `TextFormatter.IsFontAwesomeIcon`
- Added `TextFormatter.FormattingCodeRegexPattern` that matches one formatting code the same way as the formatters when no link is open
- **Fixed `TextFormatter.FormatHtml` links that could run scripts.** `$l` links keep `http` and `https` URLs, add `http://` to URLs without a scheme, and are not clickable with any other scheme like `javascript:`
 - Whitespace around link URLs is trimmed like in the game
- Fixed `TextFormatter.FormatAnsi` ignoring 1 and 2 digit color codes, `$<`, `$>` and `$t`
- Fixed `TimeSingle.ToString` writing some times one millisecond lower, for example `0:00.250` for 0.251 seconds
- Fixed `TimeInt32.Days`, `Hours`, `Minutes`, `Seconds` and `ToString` losing precision near the end of a day or an hour
- Changed `TimeInt32` equality and comparison with `TimeSingle` and other `ITime` values to use `TotalSeconds`, so `new TimeInt32(251) == new TimeSingle(0.251f)`
- Fixed `AccountUtils.ToAccountId` returning an account ID for logins that do not decode to exactly 16 bytes. It now throws `FormatException`
- Improved performance and reduced allocations, for example time parsing is up to 12 times faster, map UIDs up to 14 times faster and `TextFormatter.FormatHtml` up to 19 times faster
- Added .NET 11 support
- Removed the `totmstring` native entry point