ScriptAsset.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef SCRIPT_ASSET_H
  23. #include "ScriptAsset.h"
  24. #endif
  25. #ifndef _ASSET_MANAGER_H_
  26. #include "assets/assetManager.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _TAML_
  32. #include "persistence/taml/taml.h"
  33. #endif
  34. #ifndef _ASSET_PTR_H_
  35. #include "assets/assetPtr.h"
  36. #endif
  37. // Debug Profiling.
  38. #include "platform/profiler.h"
  39. //-----------------------------------------------------------------------------
  40. IMPLEMENT_CONOBJECT(ScriptAsset);
  41. ConsoleType(ScriptAssetPtr, TypeScriptAssetPtr, ScriptAsset, ASSET_ID_FIELD_PREFIX)
  42. //-----------------------------------------------------------------------------
  43. ConsoleGetType(TypeScriptAssetPtr)
  44. {
  45. // Fetch asset Id.
  46. return (*((AssetPtr<ScriptAsset>*)dptr)).getAssetId();
  47. }
  48. //-----------------------------------------------------------------------------
  49. ConsoleSetType(TypeScriptAssetPtr)
  50. {
  51. // Was a single argument specified?
  52. if (argc == 1)
  53. {
  54. // Yes, so fetch field value.
  55. const char* pFieldValue = argv[0];
  56. // Fetch asset pointer.
  57. AssetPtr<ScriptAsset>* pAssetPtr = dynamic_cast<AssetPtr<ScriptAsset>*>((AssetPtrBase*)(dptr));
  58. // Is the asset pointer the correct type?
  59. if (pAssetPtr == NULL)
  60. {
  61. // No, so fail.
  62. //Con::warnf("(TypeScriptAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  63. return;
  64. }
  65. // Set asset.
  66. pAssetPtr->setAssetId(pFieldValue);
  67. return;
  68. }
  69. // Warn.
  70. Con::warnf("(TypeScriptAssetPtr) - Cannot set multiple args to a single asset.");
  71. }
  72. //-----------------------------------------------------------------------------
  73. IMPLEMENT_CALLBACK(ScriptAsset, onInitializeAsset, void, (), (),
  74. "@brief When the ScriptAsset is initialized(loaded) by the AssetManager.\n\n");
  75. IMPLEMENT_CALLBACK(ScriptAsset, onRefreshAsset, void, (), (),
  76. "@brief When the ScriptAsset is refreshed by the AssetManager.\n\n");
  77. IMPLEMENT_CALLBACK(ScriptAsset, onUnloadAsset, void, (), (),
  78. "@brief When the ScriptAsset is unloaded by the AssetManager.\n\n");
  79. ScriptAsset::ScriptAsset() : AssetBase(), mIsServerSide(true)
  80. {
  81. mScriptFile = StringTable->EmptyString();
  82. mScriptPath = StringTable->EmptyString();
  83. }
  84. //-----------------------------------------------------------------------------
  85. ScriptAsset::~ScriptAsset()
  86. {
  87. }
  88. //-----------------------------------------------------------------------------
  89. void ScriptAsset::initPersistFields()
  90. {
  91. docsURL;
  92. // Call parent.
  93. Parent::initPersistFields();
  94. addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, ScriptAsset),
  95. &setScriptFile, &getScriptFile, "Path to the script file.");
  96. }
  97. //------------------------------------------------------------------------------
  98. void ScriptAsset::copyTo(SimObject* object)
  99. {
  100. // Call to parent.
  101. Parent::copyTo(object);
  102. }
  103. void ScriptAsset::initializeAsset()
  104. {
  105. if (mpAssetDefinition->mAssetType != StringTable->insert("ScriptAsset"))
  106. {
  107. //if we've got a custom type, treat it as our namespace, too
  108. setClassNamespace(mpAssetDefinition->mAssetType);
  109. }
  110. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  111. if (Torque::FS::IsScriptFile(mScriptPath))
  112. {
  113. //We're initialized properly, so we'll go ahead and kick along any dependencies we may have as well
  114. AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
  115. // Does the asset have any dependencies?
  116. if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
  117. {
  118. // Iterate all dependencies.
  119. while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
  120. {
  121. AssetPtr<ScriptAsset> scriptAsset = assetDependenciesItr->value;
  122. mScriptAssetDependencies.push_front(scriptAsset);
  123. // Next dependency.
  124. assetDependenciesItr++;
  125. }
  126. }
  127. Con::executeFile(mScriptPath, false, false);
  128. }
  129. onInitializeAsset_callback();
  130. }
  131. void ScriptAsset::onAssetRefresh()
  132. {
  133. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  134. if (Torque::FS::IsScriptFile(mScriptPath))
  135. {
  136. //Refresh any dependencies we may have
  137. for (U32 i = 0; i < mScriptAssetDependencies.size(); i++)
  138. {
  139. mScriptAssetDependencies[i]->onAssetRefresh();
  140. }
  141. Con::executeFile(mScriptPath, false, false);
  142. }
  143. onRefreshAsset_callback();
  144. }
  145. void ScriptAsset::unloadAsset()
  146. {
  147. onUnloadAsset_callback();
  148. }
  149. void ScriptAsset::setScriptFile(const char* pScriptFile)
  150. {
  151. // Sanity!
  152. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  153. // Fetch image file.
  154. pScriptFile = StringTable->insert(pScriptFile, true);
  155. // Ignore no change,
  156. if (pScriptFile == mScriptFile)
  157. return;
  158. // Update.
  159. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  160. // Refresh the asset.
  161. refreshAsset();
  162. }
  163. bool ScriptAsset::execScript()
  164. {
  165. AssetBase* handle = mpOwningAssetManager->acquireAsset<AssetBase>(getAssetId());
  166. if (handle)
  167. return true;
  168. if (Torque::FS::IsScriptFile(mScriptPath))
  169. {
  170. return Con::executeFile(mScriptPath, false, false);
  171. }
  172. Con::errorf("ScriptAsset:execScript() - Script asset must have a valid file to exec");
  173. return false;
  174. }
  175. DefineEngineMethod(ScriptAsset, execScript, bool, (), ,
  176. "Executes the script file.\n"
  177. "@return The bool result of calling exec")
  178. {
  179. return object->execScript();
  180. }