Torque3D Documentation / _generateds / ScriptAsset.cpp

ScriptAsset.cpp

Engine/source/T3D/assets/ScriptAsset.cpp

More...

Public Functions

ConsoleSetType(TypeScriptAssetPtr )
ConsoleType(ScriptAssetPtr , TypeScriptAssetPtr , ScriptAsset , ASSET_ID_FIELD_PREFIX )
DefineEngineMethod(ScriptAsset , execScript , bool , () , "Executes the script <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n</a>" "@return The bool result of calling exec" )

Detailed Description

Public Functions

ConsoleSetType(TypeScriptAssetPtr )

ConsoleType(ScriptAssetPtr , TypeScriptAssetPtr , ScriptAsset , ASSET_ID_FIELD_PREFIX )

DefineEngineMethod(ScriptAsset , execScript , bool , () , "Executes the script <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n</a>" "@return The bool result of calling exec" )

IMPLEMENT_CONOBJECT(ScriptAsset )

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