Last active
March 4, 2025 22:03
-
-
Save trezy/bd3041728897ef982940d75e99d1695a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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