# Copyright (C) 2006-2012 Mortal Coil Games # See LICENSE for details. """ """ from twisted.internet import defer from mv3d.resource.asset import CodeAsset, SoundAsset from mv3d.util.classgen import ClassGenerator from mv3d.net.ha import withSlaveUpdate from mv3d.util.persist import Text def newLocalClassGeneratorAsset(conductor, assetgroup, modulename, classname, name="", depends=[]): """ Factory to create a new asset and add it to the specified group. """ tm = assetgroup.newAsset(conductor, ClassGenerator( "mv3d.resource.local.LocalClassGeneratorAsset")) def finishUp(tm, m, c, _n, d): """ Does the work of setting the attributes on the asset """ tm.setModule(m) tm.setClass(c) if name != "": tm.setName(name) else: tm.setName(c) for d in depends: if isinstance(d, tuple): tm.addDependency(d) else: tm.addDependency(d.getID()) for d in depends: if isinstance(d, tuple): tm.addDependency(d) else: tm.addDependency(d.getID()) return tm if isinstance(tm, defer.Deferred): tm.addCallback(finishUp, modulename, classname, name, depends) return tm return finishUp(tm, modulename, classname, name, depends) class LocalClassGeneratorAsset(CodeAsset): """ This asset type is a pointer to a class that is assumed to be installed locally. """ _schemaVersion = 2 allowedState = CodeAsset.allowedState + ["modulename", "classname"] modulename = Text(default="", autoSave=True, partialSave=True) classname = Text(default="", autoSave=True, partialSave=True) classModifiers = dict(web=[ ClassGenerator("mv3d.server.editor.LocalClassGeneratorAssetEditor"), ClassGenerator("mv3d.server.editor.DeleteAsset"), ]) def __init__(self, modulename=None, classname=None, server=None, datastore=None): CodeAsset.__init__(self) self.modulename = modulename self.classname = classname self.datastore = datastore self.server = server def getModule(self): """ Returns the module name of this asset """ return self.modulename @withSlaveUpdate def setModule(self, f): """ Sets the module name for the asset """ self.modulename = f def getClass(self): """ Returns the class name for the asset """ return self.classname @withSlaveUpdate def setClass(self, f): """ Sets the class name for the asset """ self.classname = f observe_setModule = setModule observe_setClass = setClass def haveAsset(self): """ Returns true if we have the asset locally """ return True def getCG(self): """ Returns this asset as a class generator """ return ClassGenerator(modulename=self.modulename, classname=self.classname, assetid=self.getID()) class LocalSoundAsset(SoundAsset): """ A sound that is stored on this computer """ _schemaVersion = 2 allowedState = SoundAsset.allowedState + ["filename"] filename = Text(default="", autoSave=True, partialSave=True) def __init__(self, filename=None, datastore=None): SoundAsset.__init__(self) self.filename = filename self.datastore = datastore def getFilename(self): return self.filename @withSlaveUpdate def setFilename(self, f): self.filename = f observe_setFilename = setFilename