URLs, loading (downloading) and saving resources

1. Introduction

All methods in our engine take URL as the parameter, not just a filename. Although in most cases you can also pass a filename (absolute or relative to the current directory), and it will also work as expected.

All loading and saving routines (for models, images, sounds, and all other resources) automatically deal with URLs.

asynchronous_download example, downloading multiple URLs asynchronously (without blocking the UI)

2. Loading and saving any URL using TStream, synchronously or asynchronously

To directly load or save your own files using ObjectPascal TStream:

3. Loading and saving specialized resources

More higher-level loading and saving routines are available. They are all implemented on top of the above functions dealing with TStream, so they all support all registered URLs.

4. Case-sensitive filesystems and URLs

Often, URLs just refer to files on the filesystem. Like file:// URLs, or castle-data:/ URLs (on platforms where the application data is just a set of regular files). In this case, the underlying filesystem determines whether the names are case-sensitive (so e.g. foobar vs FooBar mean something else) or not.

  • On most operating systems (like Linux, FreeBSD, macOS; but also mobile and console filesystems) the filesystems are typically case-sensitive.

  • On Windows, the filesystems are typically not case-sensitive.

Note
To complicate matters, whether the filesystem is case sensitive or not is not actually determined by the OS. You can mount on Windows a case-sensitive filesystem like Ext4. You can mount on Linux a case-ignoring filesystem like NTFS. So the filesystems can be case-sensitive or not on any OS.

To make the application work on all platforms, be sure to always specify the same case in URLs as your files. So, assume that URLs are case-sensitive.

E.g. take care if you load castle-data:/FooBar.png or castle-data:/foobar.png. Using wrong letter case may be an easy mistake, because on Windows both versions will work, but on Linux only the version with correct case.

If you don’t want to care about the latter case (which makes sense if you develop mostly on Windows but want your application to also work on other platforms), a simple solution is to set global variable CastleDataIgnoreCase to true.

5. Supported protocols

5.1. Loading local files: file

To load normal files from disk, use a file URL.

Absolute file URLs look like this: file:///c:/windows/clock.avi (on Windows) or file:///etc/fstab (on Unix). You can also use normal absolute filenames with most engine routines, like c:\windows\clock.avi (on Windows; you can use slash or backslash) or /etc/fstab (on Unix).

In most cases hardcoding an absolute filename in your application is not very useful (since it would be specific to a particular system). Using relative URLs makes more sense, like textures/wood.png. Relative URLs should use slashes, and work naturally when used in other files (relative URL is then relative to the containing file) or code (relative URL is then relative to the current working directory). Note that the current working directory depends on how the user runs your application.

To reliably load game data from code you should use castle-data protocol, not file protocol.

CastleUriUtils contains routines to operate on URLs (and more general URIs), including converting between regular filenames and URLs with file: protocol.

  • Use this to convert a FileName (relative or absolute) to an absolute URL:

    URL := FilenameToUriSafe(FileName);
  • Use this to convert something that may be a FileName or URL to an URL. This is safer than FilenameToUriSafe(…​), in that it will never touch something that already is an URL.

    URL := AbsoluteUri(FileNameOrUrl);
  • Use this to convert URL back to a FileName. When the URL is a file: protocol, it will decode back the simple filename. Right now, URL without protocol is also returned back as a simple filename. When the URL uses a different protocol (like http), returns empty string.

    FileName := UriToFilenameSafe(URL);

On Android, you should use the read_external_storage service to be able to read storage files (e.g. from SD card) through the file protocol.

5.2. Loading data files: castle-data

This protocol should be used to load data files of your project. During development, on normal desktop systems (Windows, Linux etc.), the data files are simply files inside the data subdirectory of your project. You should place there all the files loaded at runtime by your application.

When the application is packaged for some systems, like Android or iOS, the data directory may be treated in a special way. If you access all the data files using the castle-data protocol (or using URLs relative to files loaded using the castle-data protocol) then your application will "just wok" on all systems.

Note that you can adjust ApplicationDataOverride to host your data files wherever you want. This way data files may even be loaded from http location. On desktop systems, the data location is by default just a regular directory on disk, but you can change it.

5.3. Downloading from the network: http and https

http and https work. You can download data from the Internet, and the TCastleDownload has a support for various HTTP methods (GET, POST). You can use this for simple downloading, or for full-featured communication with a REST server.

Asynchronous TCastleDownload supports http and https automatically. It is perfect to use with unreliable / slow network.

Synchronous Download supports these protocols only if you set global variable EnableBlockingDownloads to true. We call them "blocking downloads" because the application has to simply wait for the download to finish and there’s no way to interrupt the download (without just killing the application) or even watch the progress.

We advise always using TCastleDownload to get data from the network — although it requires a bit more effort from code, but allows to observe and interrupt the download.

For the https (encrypted version of http) protocol to work:

  • For FPC: Use the OpenSSLSockets unit. Simply add this to the uses clause of one of your units (like GameInitialize):

    {$ifdef FPC} OpenSSLSockets, {$endif} // support HTTPS
  • Make sure users have the OpenSSL library installed.

    On Unix (Linux, FreeBSD, macOS…​), it is standard to have OpenSSL installed already system-wide. Developers and users likely don’t need to do anything.

    On Windows, use the appropriate DLLs. Our editor (or command-line build tool) will automatically place the proper DLLs alongside your EXE file on the first build. You only need to add <dependency name="Https" /> in your CastleEngineManifest.xml.

See an example like examples/network/asynchronous_download/ that does both things above, to make https work.

Note that we use URLs, not filenames, throughout the entire engine API. So to load something from the network, you can just pass e.g. https://…​; to TCastleSceneCore.Load.

Inside models (like X3D, glTF and other), you can also refer to network resources, and it will "just work". For example you can use X3D Inline node to inline a model from given URL, you can use X3D Anchor node to switch to given model on click, you can refer to textures and sounds and scripts and everything else from the network. Relative URLs are always resolved with respect to the containing document.

On Android, you should use the download_urls service to support http and https protocols.

5.4. Embedded data: data

data is a special protocol that doesn’t refer to an external resource. Instead, the complete data URI contains the contents. This allows to embed various resources (like textures, sounds, other 3D models) inside a parent file. For example instead of referring to the texture filename from 3D model — you can embed the actual texture contents inside 3D model file. This is sometimes a very nice feature (it makes the file easier to distribute).

See data: URI specification. Our engine includes a tool to-data-uri that can turn any file into a data URI, and you can use such data URI everywhere where we expect URL. to-data-uri is provided in the regular engine download in the bin/ subdirectory, also source code is here.

Wherever our engine, or X3D, says that it expects a URL — you can use data URI to provide the contents "right there", without using any additional file.

Demos of using data URI are inside our demo models, see in particular x3d/data_uri.x3dv.

5.5. Data from a zip

You can open and register an arbitrary ZIP file to read / write resources from it using a custom URL. See the TCastleZip.RegisterUrlProtocol docs.

See the examples/network/custom_url_handler, when USE_ZIP_URL_HANDLER is defined.

5.6. Registering your own protocol

You are not limited to the protocols documented here. You can register your own URL protocols, with custom reading and writing handlers, using RegisterUrlProtocol.

See the examples/network/custom_url_handler, when USE_ZIP_URL_HANDLER is undefined.

6. Dialog windows that support URLs

If you use TCastleWindow, it gives you a ready TCastleWindow.FileDialog that takes and returns URLs.

If you use Lazarus with TCastleControl, we advise to use our dialog components: TCastleOpenDialog, TCastleSaveDialog, TCastleOpenSceneDialog, TCastleOpenImageDialog, TCastleSaveImageDialog. They expose URL property which works naturally with CGE.

7. Notes about terminology: URI vs URL

URI is a more general term. URI uniquely identifies a resource but does not necessarily tell us how to load (download) or save (upload) it. We have many routines in CastleUriUtils unit that process URIs (strings), they use the more general term URI. They complement standard FPC URIParser routines.

URL is a specific type of URI that also tells you how to load or save the resource. For example http and file protocols define URLs. Most of our routines that load or save use the term URL.

Things get a little more cloudy when you realize there’s also data URI scheme. It’s not precisely an URL (it’s not an address of a resource), but you can load it (since the URI itself contains the resource). And we support it fully (our Download method loads it automatically). Admittedly, this means that our loading routines should rather use the term URL or data URI, but that’s just long and (for those who don’t use data URI) confusing, so for simplicity we just keep (over-)using the term URL. Also, other standards (like CSS and X3D) allow placing data URIs inside fields called url.

If you enjoy reading about Internet terminology, note that we use in our engine also URNs (another subtype of URI). They are used by X3D external prototypes.


To improve this documentation just edit this page and create a pull request to cge-www repository.