swift - Sharing code from an app as a framework for making plug-ins -
i experimenting loading classes bundles @ runtime. let me explain i'm trying do. question @ end please bear me.
suppose making game (as cocoa .app) , game contains classes can subclassed mods , registered game (as cocoa .bundle). whole infrastructure consist out of 3 main pairts:
game.app
the game @ least have 3 classes:
- a class represents object in game can subclassed;
- a class of subclass can used principal class of bundle;
- a static class called app delegate load bundles.
this code last two:
class principal { func register() { //for plugins register classes } } class loader { static func load() { let enumerator = filemanager.default.enumerator(at: bundle.main.builtinpluginsurl!, includingpropertiesforkeys: nil) while let element = enumerator?.nextobject() as? url { if element.pathextension == "bundle" { let bundle = bundle(url: element) if let mod = bundle?.principalclass as? principal.type { let instance:principal = mod.init() instance.register() } } } } } kit.framework
to used plugin bundles make subclasses of classes game.
plugin.bundle
links kit.framework , subclasses classes. registers classes in principal class so:
class plugin:principal { override func register() { //registers types (singleton) registry registry.registertype(something.type) } } i got of work putting code (except loader) in framework , having both app , bundle link framework. ideally keep code in game can't cast registered types corresponding types in game classes compiled separately in app , in framework , different entities.
now question if there way either link dynamically .app framework or way share code developing plugins without using framework , while keeping code in .app.
Comments
Post a Comment