Skip to content

Instantly share code, notes, and snippets.

@trezy
Last active March 4, 2025 22:03
Show Gist options
  • Save trezy/bd3041728897ef982940d75e99d1695a to your computer and use it in GitHub Desktop.
Save trezy/bd3041728897ef982940d75e99d1695a to your computer and use it in GitHub Desktop.
import {
Assets,
extensions,
} from 'pixi.js'
import { TauriFilesystemLoader } from './TauriFilesystemLoader'
extensions.add(TauriFilesystemLoader)
// Loads normally using the HTTP adapter
Assets.load('https://pixijs.com/bunny.png')
// Loads the file with the TauriFilesystemLoader since the path starts with `/fs`
Assets.load('/fs/images/bunny.png')
// Module imports
import {
ExtensionType,
type LoaderParser,
LoaderParserPriority,
} from 'pixi.js'
import {
BaseDirectory,
exists,
readFile,
readTextFile,
} from '@tauri-apps/plugin-fs'
const EXTENSION_NAME = 'TauriFilesystemLoader'
export function TauriFilesystemLoader(): LoaderParser {
return {
name: EXTENSION_NAME,
extension: {
name: EXTENSION_NAME,
priority: LoaderParserPriority.High,
type: ExtensionType.LoadParser,
},
/** Handle loading the asset if it uses the `/fs` root path. */
test(url: string) {
return /^\/fs/iu.test(url)
},
/** Load the file. */
async load(url: string) {
const readOptions = { baseDir: BaseDirectory.Resources }
const filePath = url.replace(/^\/fs/iu, '')
// Throw if the file doesn't exist
if (!exists(filePath, readOptions)) {
throw new Error(`File ${filePath} does not exist.`)
}
// Load text content files as strings.
if (/\.(json|txt)$/.test(filePath)) {
return readTextFile(filePath, readOptions)
}
// Assume it's a binary file.
return readFile(filePath, readOptions)
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment