Version

Goal:Access to the file system through mount points
Difficulty:Intermediate
Duration:15 minutes
Language:All

See The file system for a complete explanation on the Harfang filesystem layer and mount points.

Python

# Access to the file system through mount points

import os
import harfang as hg

# mount a standard file system as the 'std:' prefix
hg.MountFileDriver(hg.StdFileDriver(), "std:")

# mount a standard file system pointing to the current directory as the 'cwd:' prefix
hg.MountFileDriver(hg.StdFileDriver(os.path.dirname(__file__)), "cwd:")

# get the full path to this file
this_file_path = os.path.realpath(__file__)

# get this file name (ie. path relative to the current directory)
this_file_name = os.path.basename(__file__)

# the engine can access it relative to the current directory through the 'cwd:' mount point
cwd_path = "cwd:" + this_file_name
res_cwd_access = hg.GetFilesystem().Exists(cwd_path)

# and it can access it using its absolute path through the std: mount point
std_path = "std:" + this_file_path
res_std_access = hg.GetFilesystem().Exists(std_path)

# however, when no prefix is specified the engine has no way to access it from its absolute path
res_direct_access = hg.GetFilesystem().Exists(this_file_path)

print("Can access '%s': %s" % (this_file_path, str(res_direct_access)))
print("Can access '%s': %s" % (cwd_path, str(res_cwd_access)))
print("Can access '%s': %s" % (std_path, str(res_std_access)))

Lua

hg = require("harfang")

-- Access to the file system through mount points

-- mount a standard file system as the 'std:' prefix
hg.MountFileDriver(hg.StdFileDriver(), "std:")

-- mount a standard file system pointing to the data directory as the 'data:' prefix
hg.MountFileDriver(hg.StdFileDriver('../_data'), 'data:')

-- name and relative path to a data file
file_name = 'owl.jpg'
file_path = '../_data/'..file_name

-- the engine can access the file by name through the 'data:' mount point
data_path = 'data:'..file_name
res_data_access = hg.GetFilesystem():Exists(data_path)

print("Can access '"..data_path.."': "..tostring(res_data_access))

-- and it can access it using its relative path through the std: mount point
std_path = 'std:'..file_path
res_std_access = hg.GetFilesystem():Exists(std_path)

print("Can access '"..std_path.."': "..tostring(res_std_access))