Trouv / bevy_ecs_ldtk

ECS-friendly ldtk plugin for bevy, leveraging bevy_ecs_tilemap
Other
627 stars 73 forks source link

Allow custom deserialization of ldtk files #304

Open StratusFearMe21 opened 4 months ago

StratusFearMe21 commented 4 months ago

This PR makes it possible to load Ldtk files that you have preprocessed, for example by re-serializing the file using bincode and compressing it.

Trouv commented 3 months ago

So this seems to me like it would actually be a good use-case for bevy's relatively new asset preprocessing features. I'm not an expert on it but I think basically what could work for this plugin is a new asset settings type. something like...

enum LdtkProjectFormat {
    Json,
    Bincode,
}

struct LdtkProjectLoaderSettings {
    format: LdtkProjectFormat
}

Then that would be the associated Settings type in LdtkProjectLoader's AssetLoader implementation.

Then we could write an AssetSaver implementation for some new type like LdtkProjectBincodeSaver, with associated types Asset = LdtkProject, OutputLoader = LdtkProjectLoader. It would write the bincode formatted bytes and return Ok(LdtkProjectLoaderSettings { format: LdtkProjecFormat::Bincode }). Then we register it as a preprocessor and bevy will transform the source json in assets/ to bincode bytes in imported_assets/ automatically if processed assets are enabled.

At least, I think that's the necessary steps to implementing a custom pre-processor. It seems like being able to use an enum of formats in a Settings type would be a good starting point at least that doesn't necessarily require the AssetSaver stuff. Thoughts?

StratusFearMe21 commented 3 months ago

Ooooo. I like that idea! Instead of an enum tho, could you pass a function pointer to the LdtkProjectLoaderSettings? Something like fn(Vec<u8>) -> LdtkJson? That way any deserializer could work with this plugin