simObject.cpp 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "platform/platformMemory.h"
  28. #include "console/simObject.h"
  29. #include "console/console.h"
  30. #include "console/consoleInternal.h"
  31. #include "console/engineAPI.h"
  32. #include "console/simFieldDictionary.h"
  33. #include "console/simPersistID.h"
  34. #include "console/typeValidators.h"
  35. #include "console/arrayObject.h"
  36. #include "console/codeBlock.h"
  37. #include "core/frameAllocator.h"
  38. #include "core/stream/fileStream.h"
  39. #include "core/fileObject.h"
  40. #include "persistence/taml/tamlCustom.h"
  41. #include "sim/netObject.h"
  42. #include "cinterface/cinterface.h"
  43. IMPLEMENT_CONOBJECT( SimObject );
  44. // See full description in the new CHM manual
  45. ConsoleDocClass( SimObject,
  46. "@brief Base class for almost all objects involved in the simulation.\n\n"
  47. "@ingroup Console\n"
  48. );
  49. bool SimObject::smForceId = false;
  50. SimObjectId SimObject::smForcedId = 0;
  51. bool SimObject::preventNameChanging = false;
  52. IMPLEMENT_CALLBACK(SimObject, onInspectPostApply, void, (SimObject* obj), (obj), "Generic callback for when an object is edited");
  53. namespace Sim
  54. {
  55. // Defined in simManager.cpp
  56. extern SimGroup *gRootGroup;
  57. extern SimManagerNameDictionary *gNameDictionary;
  58. extern SimIdDictionary *gIdDictionary;
  59. extern U32 gNextObjectId;
  60. }
  61. //-----------------------------------------------------------------------------
  62. SimObject::SimObject()
  63. {
  64. mObjectName = NULL;
  65. mOriginalName = NULL;
  66. mInternalName = NULL;
  67. mInheritFrom = NULL;
  68. nextNameObject = nullptr;
  69. nextManagerNameObject = nullptr;
  70. nextIdObject = NULL;
  71. mFilename = NULL;
  72. mDeclarationLine = -1;
  73. mId = 0;
  74. mIdString[ 0 ] = '\0';
  75. mGroup = 0;
  76. mNameSpace = NULL;
  77. mNotifyList = NULL;
  78. mFlags.set( ModStaticFields | ModDynamicFields );
  79. mProgenitorFile = StringTable->EmptyString();
  80. mFieldDictionary = NULL;
  81. mCanSaveFieldDictionary = true;
  82. mClassName = NULL;
  83. mSuperClassName = NULL;
  84. mCopySource = NULL;
  85. mPersistentId = NULL;
  86. is_temp_clone = false;
  87. }
  88. //-----------------------------------------------------------------------------
  89. SimObject::~SimObject()
  90. {
  91. // if this is a temp-clone, we don't delete any members that were shallow-copied
  92. // over from the source datablock.
  93. if (is_temp_clone)
  94. return;
  95. if( mFieldDictionary )
  96. {
  97. delete mFieldDictionary;
  98. mFieldDictionary = NULL;
  99. }
  100. // Release persistent ID.
  101. if( mPersistentId )
  102. {
  103. mPersistentId->unresolve();
  104. mPersistentId->decRefCount();
  105. mPersistentId = NULL;
  106. }
  107. if( mCopySource )
  108. mCopySource->unregisterReference( &mCopySource );
  109. AssertFatal(nextNameObject == nullptr,avar(
  110. "SimObject::~SimObject: Not removed from dictionary: name %s, id %i",
  111. mObjectName, mId));
  112. AssertFatal(nextManagerNameObject == nullptr,avar(
  113. "SimObject::~SimObject: Not removed from manager dictionary: name %s, id %i",
  114. mObjectName,mId));
  115. AssertFatal(mFlags.test(Added) == 0, "SimObject::object "
  116. "missing call to SimObject::onRemove");
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool SimObject::processArguments(S32 argc, ConsoleValue *argv)
  120. {
  121. return argc == 0;
  122. }
  123. //-----------------------------------------------------------------------------
  124. void SimObject::initPersistFields()
  125. {
  126. addGroup( "Ungrouped" );
  127. addProtectedField( "name", TypeName, Offset(mObjectName, SimObject), &setProtectedName, &defaultProtectedGetFn,
  128. "Optional global name of this object." );
  129. addProtectedField("inheritFrom", TypeString, Offset(mInheritFrom, SimObject), &setInheritFrom, &defaultProtectedGetFn,
  130. "Optional Name of object to inherit from as a parent.");
  131. endGroup( "Ungrouped" );
  132. addGroup( "Object" );
  133. addField( "internalName", TypeString, Offset(mInternalName, SimObject),
  134. "Optional name that may be used to lookup this object within a SimSet.");
  135. addProtectedField( "parentGroup", TYPEID< SimObject >(), Offset(mGroup, SimObject), &setProtectedParent, &defaultProtectedGetFn,
  136. "Group hierarchy parent of the object." );
  137. addProtectedField( "class", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn,
  138. "Script class of object." );
  139. addProtectedField( "superClass", TypeString, Offset(mSuperClassName, SimObject), &setSuperClass, &defaultProtectedGetFn,
  140. "Script super-class of object." );
  141. // For legacy support
  142. addProtectedField( "className", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn,
  143. "Script class of object.", AbstractClassRep::FIELD_HideInInspectors );
  144. endGroup( "Object" );
  145. addGroup( "Editing" );
  146. addProtectedField( "hidden", TypeBool, 0,
  147. &_setHidden, &_getHidden,
  148. "Whether the object is visible." );
  149. addProtectedField( "locked", TypeBool, 0,
  150. &_setLocked, &_getLocked,
  151. "Whether the object can be edited." );
  152. endGroup( "Editing" );
  153. addGroup( "Persistence" );
  154. addProtectedField( "canSave", TypeBool, Offset( mFlags, SimObject ),
  155. &_setCanSave, &_getCanSave,
  156. "Whether the object can be saved out. If false, the object is purely transient in nature." );
  157. addField( "canSaveDynamicFields", TypeBool, Offset(mCanSaveFieldDictionary, SimObject),
  158. "True if dynamic fields (added at runtime) should be saved. Defaults to true." );
  159. addProtectedField( "persistentId", TypePID, Offset( mPersistentId, SimObject ),
  160. &_setPersistentID, &defaultProtectedGetFn,
  161. "The universally unique identifier for the object." );
  162. endGroup( "Persistence" );
  163. Parent::initPersistFields();
  164. }
  165. //-----------------------------------------------------------------------------
  166. String SimObject::describeSelf() const
  167. {
  168. String desc = Parent::describeSelf();
  169. if( mId != 0 )
  170. desc = avar( "%s|id: %i", desc.c_str(), mId );
  171. if(mObjectName)
  172. desc = avar( "%s|name: %s", desc.c_str(), mObjectName);
  173. if( mInternalName )
  174. desc = avar( "%s|internal: %s", desc.c_str(), mInternalName );
  175. if( mNameSpace )
  176. desc = avar( "%s|nspace: %s", desc.c_str(), mNameSpace->mName );
  177. if( mGroup )
  178. desc = avar( "%s|group: %s", desc.c_str(), mGroup->getName() );
  179. if( mCopySource )
  180. desc = avar( "%s|copy: %s", desc.c_str(), mCopySource->getName() );
  181. if( mPersistentId )
  182. desc = avar( "%s|pid: %s", desc.c_str(), mPersistentId->getUUID().toString().c_str() );
  183. return desc;
  184. }
  185. // Copies dynamic fields from one object to another, optionally limited by the settings for
  186. // <filter> and <no_replace>. When true, <no_replace> prohibits the replacement of fields that
  187. // already have a value. When <filter> is specified, only fields with leading characters that
  188. // exactly match the characters in <filter> are copied.
  189. void SimObject::assignDynamicFieldsFrom(SimObject* from, const char* filter, bool no_replace)
  190. {
  191. if (from->mFieldDictionary)
  192. {
  193. if( mFieldDictionary == NULL )
  194. mFieldDictionary = new SimFieldDictionary;
  195. mFieldDictionary->assignFrom(from->mFieldDictionary, filter, no_replace);
  196. }
  197. }
  198. //=============================================================================
  199. // Persistence.
  200. //=============================================================================
  201. // MARK: ---- Persistence ----
  202. //-----------------------------------------------------------------------------
  203. bool SimObject::writeField(StringTableEntry fieldname, const char* value)
  204. {
  205. // Don't write empty fields.
  206. if (!value || !*value)
  207. return false;
  208. // Don't write owner field for components
  209. static StringTableEntry sOwner = StringTable->insert( "owner" );
  210. if( fieldname == sOwner )
  211. return false;
  212. // Don't write ParentGroup
  213. static StringTableEntry sParentGroup = StringTable->insert( "parentGroup" );
  214. if( fieldname == sParentGroup )
  215. return false;
  216. // Don't write name, is within the parenthesis already
  217. static StringTableEntry sName = StringTable->insert( "name" );
  218. if( fieldname == sName )
  219. return false;
  220. // Don't write className, it is read for legacy support but we
  221. // write it out as class.
  222. static StringTableEntry sClassName = StringTable->insert( "className" );
  223. if( fieldname == sClassName )
  224. return false;
  225. // Write persistent ID only if present.
  226. static StringTableEntry sPersistentId = StringTable->insert( "persistentId" );
  227. if( fieldname == sPersistentId && ( !value || !value[ 0 ] ) )
  228. return false;
  229. // Don't write hidden and locked flags if they are at their default value.
  230. static StringTableEntry sHidden = StringTable->insert( "hidden" );
  231. static StringTableEntry sLocked = StringTable->insert( "locked" );
  232. if( fieldname == sHidden && !dAtob( value ) )
  233. return false;
  234. if( fieldname == sLocked && !dAtob( value ) )
  235. return false;
  236. return true;
  237. }
  238. //-----------------------------------------------------------------------------
  239. void SimObject::writeFields(Stream &stream, U32 tabStop)
  240. {
  241. // Write static fields.
  242. const AbstractClassRep::FieldList &list = getFieldList();
  243. for(U32 i = 0; i < list.size(); i++)
  244. {
  245. const AbstractClassRep::Field* f = &list[i];
  246. // Skip the special field types.
  247. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  248. continue;
  249. for(U32 j = 0; S32(j) < f->elementCount; j++)
  250. {
  251. char array[8];
  252. dSprintf( array, 8, "%d", j );
  253. const char *val = getDataField(StringTable->insert( f->pFieldname ), array );
  254. // Make a copy for the field check.
  255. if (!val)
  256. continue;
  257. U32 nBufferSize = dStrlen( val ) + 1;
  258. FrameTemp<char> valCopy( nBufferSize );
  259. dStrcpy( (char *)valCopy, val, nBufferSize );
  260. if (!writeField(f->pFieldname, valCopy))
  261. continue;
  262. val = valCopy;
  263. U32 expandedBufferSize = ( nBufferSize * 2 ) + dStrlen(f->pFieldname) + 32;
  264. FrameTemp<char> expandedBuffer( expandedBufferSize );
  265. if(f->elementCount == 1)
  266. dSprintf(expandedBuffer, expandedBufferSize, "%s = \"", f->pFieldname);
  267. else
  268. dSprintf(expandedBuffer, expandedBufferSize, "%s[%d] = \"", f->pFieldname, j);
  269. // detect and collapse relative path information
  270. char fnBuf[1024];
  271. if (f->type == TypeFilename ||
  272. f->type == TypeStringFilename ||
  273. f->type == TypeImageFilename ||
  274. f->type == TypePrefabFilename ||
  275. f->type == TypeShapeFilename ||
  276. f->type == TypeSoundFilename )
  277. {
  278. Con::collapseScriptFilename(fnBuf, 1024, val);
  279. val = fnBuf;
  280. }
  281. expandEscape((char*)expandedBuffer + dStrlen(expandedBuffer), val);
  282. dStrcat(expandedBuffer, "\";\r\n", expandedBufferSize);
  283. stream.writeTabs(tabStop);
  284. stream.write(dStrlen(expandedBuffer),expandedBuffer);
  285. }
  286. }
  287. // Write dynamic fields, if enabled.
  288. if(mFieldDictionary && mCanSaveFieldDictionary)
  289. mFieldDictionary->writeFields(this, stream, tabStop);
  290. }
  291. //-----------------------------------------------------------------------------
  292. void SimObject::write(Stream &stream, U32 tabStop, U32 flags)
  293. {
  294. if( !getCanSave() && !( flags & IgnoreCanSave ) )
  295. return;
  296. // Only output selected objects if they want that.
  297. if((flags & SelectedOnly) && !isSelected())
  298. return;
  299. stream.writeTabs(tabStop);
  300. char buffer[1024];
  301. dSprintf(buffer, sizeof(buffer), "new %s(%s) {\r\n", getClassName(), getName() && !(flags & NoName) ? getName() : "");
  302. stream.write(dStrlen(buffer), buffer);
  303. writeFields(stream, tabStop + 1);
  304. stream.writeTabs(tabStop);
  305. stream.write(4, "};\r\n");
  306. }
  307. //-----------------------------------------------------------------------------
  308. bool SimObject::save(const char *pcFileName, bool bOnlySelected, const char *preappend)
  309. {
  310. static const char *beginMessage = "//--- OBJECT WRITE BEGIN ---";
  311. static const char *endMessage = "//--- OBJECT WRITE END ---";
  312. FileStream *stream;
  313. FileObject f;
  314. f.readMemory(pcFileName);
  315. // check for flags <selected, ...>
  316. U32 writeFlags = 0;
  317. if(bOnlySelected)
  318. writeFlags |= SimObject::SelectedOnly;
  319. if((stream = FileStream::createAndOpen( pcFileName, Torque::FS::File::Write )) == NULL)
  320. return false;
  321. char docRoot[256];
  322. char modRoot[256];
  323. dStrcpy(docRoot, pcFileName, 256);
  324. char *p = dStrrchr(docRoot, '/');
  325. if (p) *++p = '\0';
  326. else docRoot[0] = '\0';
  327. dStrcpy(modRoot, pcFileName, 256);
  328. p = dStrchr(modRoot, '/');
  329. if (p) *++p = '\0';
  330. else modRoot[0] = '\0';
  331. Con::setVariable("$DocRoot", docRoot);
  332. Con::setVariable("$ModRoot", modRoot);
  333. const char *buffer;
  334. while(!f.isEOF())
  335. {
  336. buffer = (const char *) f.readLine();
  337. if(!String::compare(buffer, beginMessage))
  338. break;
  339. stream->write(dStrlen(buffer), buffer);
  340. stream->write(2, "\r\n");
  341. }
  342. stream->write(dStrlen(beginMessage), beginMessage);
  343. stream->write(2, "\r\n");
  344. if ( preappend != NULL )
  345. stream->write(dStrlen(preappend),preappend);
  346. write(*stream, 0, writeFlags);
  347. stream->write(dStrlen(endMessage), endMessage);
  348. stream->write(2, "\r\n");
  349. while(!f.isEOF())
  350. {
  351. buffer = (const char *) f.readLine();
  352. if(!String::compare(buffer, endMessage))
  353. break;
  354. }
  355. while(!f.isEOF())
  356. {
  357. buffer = (const char *) f.readLine();
  358. stream->write(dStrlen(buffer), buffer);
  359. stream->write(2, "\r\n");
  360. }
  361. Con::setVariable("$DocRoot", NULL);
  362. Con::setVariable("$ModRoot", NULL);
  363. delete stream;
  364. return true;
  365. }
  366. //-----------------------------------------------------------------------------
  367. SimPersistID* SimObject::getOrCreatePersistentId()
  368. {
  369. if( !mPersistentId )
  370. {
  371. mPersistentId = SimPersistID::create( this );
  372. mPersistentId->incRefCount();
  373. }
  374. return mPersistentId;
  375. }
  376. void SimObject::onTamlCustomRead(TamlCustomNodes const& customNodes)
  377. {
  378. // Debug Profiling.
  379. //PROFILE_SCOPE(SimObject_OnTamlCustomRead);
  380. // Fetch field list.
  381. const AbstractClassRep::FieldList& fieldList = getFieldList();
  382. const U32 fieldCount = fieldList.size();
  383. for (U32 index = 0; index < fieldCount; ++index)
  384. {
  385. // Fetch field.
  386. const AbstractClassRep::Field* pField = &fieldList[index];
  387. // Ignore if field not appropriate.
  388. if (pField->type == AbstractClassRep::StartArrayFieldType || pField->elementCount > 1)
  389. {
  390. // Find cell custom node.
  391. const TamlCustomNode* pCustomCellNodes = NULL;
  392. if (pField->pGroupname != NULL)
  393. pCustomCellNodes = customNodes.findNode(pField->pGroupname);
  394. if (!pCustomCellNodes)
  395. {
  396. char* niceFieldName = const_cast<char *>(pField->pFieldname);
  397. niceFieldName[0] = dToupper(niceFieldName[0]);
  398. String str_niceFieldName = String(niceFieldName);
  399. pCustomCellNodes = customNodes.findNode(str_niceFieldName + "s");
  400. }
  401. // Continue if we have explicit cells.
  402. if (pCustomCellNodes != NULL)
  403. {
  404. // Fetch children cell nodes.
  405. const TamlCustomNodeVector& cellNodes = pCustomCellNodes->getChildren();
  406. U8 idx = 0;
  407. // Iterate cells.
  408. for (TamlCustomNodeVector::const_iterator cellNodeItr = cellNodes.begin(); cellNodeItr != cellNodes.end(); ++cellNodeItr)
  409. {
  410. char buf[5];
  411. dSprintf(buf, 5, "%d", idx);
  412. // Fetch cell node.
  413. TamlCustomNode* pCellNode = *cellNodeItr;
  414. // Fetch node name.
  415. StringTableEntry nodeName = pCellNode->getNodeName();
  416. // Is this a valid alias?
  417. if (nodeName != pField->pFieldname)
  418. {
  419. // No, so warn.
  420. Con::warnf("SimObject::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, pField->pFieldname);
  421. continue;
  422. }
  423. // Fetch fields.
  424. const TamlCustomFieldVector& fields = pCellNode->getFields();
  425. // Iterate property fields.
  426. for (TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr)
  427. {
  428. // Fetch field.
  429. const TamlCustomField* cField = *fieldItr;
  430. // Fetch field name.
  431. StringTableEntry fieldName = cField->getFieldName();
  432. const AbstractClassRep::Field* field = findField(fieldName);
  433. // Check common fields.
  434. if (field)
  435. {
  436. setDataField(fieldName, buf, cField->getFieldValue());
  437. }
  438. else
  439. {
  440. // Unknown name so warn.
  441. Con::warnf("SimObject::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName);
  442. continue;
  443. }
  444. }
  445. idx++;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. //-----------------------------------------------------------------------------
  452. bool SimObject::_setPersistentID( void* object, const char* index, const char* data )
  453. {
  454. SimObject* simObject = reinterpret_cast< SimObject* >( object );
  455. // Make sure we don't already have a PID.
  456. if( simObject->getPersistentId() )
  457. {
  458. Con::errorf( "SimObject::_setPersistentID - cannot set a persistent ID on an object that already has a persistent ID assigned." );
  459. return false;
  460. }
  461. SimPersistID* pid;
  462. Con::setData( TypePID, &pid, 0, 1, &data );
  463. if ( !pid )
  464. return false;
  465. // Make sure it's not already bound to an object.
  466. if( pid->getObject() )
  467. {
  468. AssertWarn( pid->getObject() != simObject, "Sim::_setPersistentID - PID is bound to this object yet not assigned to it!" );
  469. SimObject* otherObj = pid->getObject();
  470. Con::errorf( "SimObject::_setPersistentID - UUID is already used by another object: '%s' -> %i:%s (%s)",
  471. data, otherObj->getId(), otherObj->getClassName(), otherObj->getName() );
  472. return false;
  473. }
  474. pid->resolve( simObject );
  475. pid->incRefCount();
  476. simObject->mPersistentId = pid;
  477. return false;
  478. }
  479. //-----------------------------------------------------------------------------
  480. void SimObject::setFilename( const char* file )
  481. {
  482. if( file )
  483. mFilename = StringTable->insert( file );
  484. else
  485. mFilename = StringTable->EmptyString();
  486. }
  487. //-----------------------------------------------------------------------------
  488. void SimObject::setDeclarationLine(U32 lineNumber)
  489. {
  490. mDeclarationLine = lineNumber;
  491. }
  492. //=============================================================================
  493. // Management.
  494. //=============================================================================
  495. // MARK: ---- Management ----
  496. //-----------------------------------------------------------------------------
  497. bool SimObject::registerObject()
  498. {
  499. AssertFatal( !mFlags.test( Added ), "reigsterObject - Object already registered!");
  500. mFlags.clear(Deleted | Removed);
  501. if(smForceId)
  502. {
  503. setId(smForcedId);
  504. smForceId = false;
  505. }
  506. if( !mId )
  507. {
  508. mId = Sim::gNextObjectId++;
  509. dSprintf( mIdString, sizeof( mIdString ), "%u", mId );
  510. }
  511. AssertFatal(Sim::gIdDictionary && Sim::gNameDictionary,
  512. "SimObject::registerObject - tried to register an object before Sim::init()!");
  513. Sim::gIdDictionary->insert(this);
  514. Sim::gNameDictionary->insert(this);
  515. // Notify object
  516. bool ret = onAdd();
  517. if(!ret)
  518. unregisterObject();
  519. AssertFatal(!ret || isProperlyAdded(), "Object did not call SimObject::onAdd()");
  520. return ret;
  521. }
  522. //-----------------------------------------------------------------------------
  523. void SimObject::unregisterObject()
  524. {
  525. mFlags.set(Removed);
  526. // Notify object first
  527. onRemove();
  528. // Clear out any pending notifications before
  529. // we call our own, just in case they delete
  530. // something that we have referenced.
  531. clearAllNotifications();
  532. // Notify all objects that are waiting for delete
  533. // messages
  534. if (getGroup())
  535. getGroup()->removeObject(this);
  536. processDeleteNotifies();
  537. // Do removals from the Sim.
  538. Sim::gNameDictionary->remove(this);
  539. Sim::gIdDictionary->remove(this);
  540. Sim::cancelPendingEvents(this);
  541. }
  542. //-----------------------------------------------------------------------------
  543. void SimObject::deleteObject()
  544. {
  545. Parent::destroySelf();
  546. }
  547. //-----------------------------------------------------------------------------
  548. void SimObject::_destroySelf()
  549. {
  550. AssertFatal( !isDeleted(), "SimObject::destroySelf - Object has already been deleted" );
  551. AssertFatal( !isRemoved(), "SimObject::destroySelf - Object in the process of being removed" );
  552. mFlags.set( Deleted );
  553. if( mFlags.test( Added ) )
  554. unregisterObject();
  555. Parent::_destroySelf();
  556. }
  557. //-----------------------------------------------------------------------------
  558. void SimObject::destroySelf()
  559. {
  560. // When using the legacy console interop, we don't delete objects
  561. // when their reference count drops to zero but rather defer their
  562. // deletion until deleteObject() is called.
  563. if( engineAPI::gUseConsoleInterop )
  564. return;
  565. Parent::destroySelf();
  566. }
  567. //-----------------------------------------------------------------------------
  568. class SimObjectDeleteEvent : public SimEvent
  569. {
  570. public:
  571. void process(SimObject *object)
  572. {
  573. object->deleteObject();
  574. }
  575. };
  576. void SimObject::safeDeleteObject()
  577. {
  578. Sim::postEvent( this, new SimObjectDeleteEvent, Sim::getCurrentTime() + 1 );
  579. }
  580. //-----------------------------------------------------------------------------
  581. void SimObject::setId(SimObjectId newId)
  582. {
  583. if(!mFlags.test(Added))
  584. mId = newId;
  585. else
  586. {
  587. // get this object out of the id dictionary if it's in it
  588. Sim::gIdDictionary->remove(this);
  589. // Free current Id.
  590. // Assign new one.
  591. mId = newId ? newId : Sim::gNextObjectId++;
  592. Sim::gIdDictionary->insert(this);
  593. }
  594. dSprintf( mIdString, sizeof( mIdString ), "%u", mId );
  595. }
  596. //-----------------------------------------------------------------------------
  597. void SimObject::assignName(const char *name)
  598. {
  599. if(mObjectName && !isNameChangeAllowed() )
  600. {
  601. Con::errorf( "SimObject::assignName - not allowed to change name of object '%s'", mObjectName);
  602. return;
  603. }
  604. // Added this assert 3/30/2007 because it is dumb to try to name
  605. // a SimObject the same thing as it's class name -patw
  606. //AssertFatal( dStricmp( getClassName(), name ), "Attempted to assign a name to a SimObject which matches it's type name." );
  607. if( dStricmp( getClassName(), name ) == 0 )
  608. Con::errorf( "SimObject::assignName - Assigning name '%s' to instance of object with type '%s'."
  609. " This can cause namespace linking issues.", getClassName(), name );
  610. StringTableEntry newName = NULL;
  611. if(name[0])
  612. newName = StringTable->insert(name);
  613. onNameChange( newName );
  614. if( mGroup )
  615. mGroup->mNameDictionary.remove( this );
  616. if( isProperlyAdded() )
  617. {
  618. unlinkNamespaces();
  619. Sim::gNameDictionary->remove( this );
  620. }
  621. mObjectName = newName;
  622. if( mGroup )
  623. mGroup->mNameDictionary.insert( this );
  624. if( isProperlyAdded() )
  625. {
  626. Sim::gNameDictionary->insert( this );
  627. linkNamespaces();
  628. }
  629. }
  630. //-----------------------------------------------------------------------------
  631. bool SimObject::registerObject(U32 id)
  632. {
  633. setId(id);
  634. return registerObject();
  635. }
  636. //-----------------------------------------------------------------------------
  637. bool SimObject::registerObject(const char *name)
  638. {
  639. assignName(name);
  640. return registerObject();
  641. }
  642. //-----------------------------------------------------------------------------
  643. bool SimObject::registerObject(const char *name, U32 id)
  644. {
  645. setId(id);
  646. assignName(name);
  647. return registerObject();
  648. }
  649. //=============================================================================
  650. // Introspection.
  651. //=============================================================================
  652. // MARK: ---- Introspection ----
  653. //-----------------------------------------------------------------------------
  654. bool SimObject::isMethod( const char* methodName )
  655. {
  656. if( !methodName || !methodName[0] )
  657. return false;
  658. if (CInterface::isMethod(this->getName(), methodName) || CInterface::isMethod(this->getClassName(), methodName)) {
  659. return true;
  660. }
  661. StringTableEntry stname = StringTable->insert( methodName );
  662. if( getNamespace() )
  663. return ( getNamespace()->lookup( stname ) != NULL );
  664. return false;
  665. }
  666. //-----------------------------------------------------------------------------
  667. bool SimObject::isField( const char* fieldName, bool includeStatic, bool includeDynamic )
  668. {
  669. const char* strFieldName = StringTable->insert( fieldName );
  670. if( includeStatic && getClassRep()->findField( strFieldName ) )
  671. return true;
  672. if( includeDynamic && getFieldDictionary() && getFieldDictionary()->findDynamicField( strFieldName ) )
  673. return true;
  674. return false;
  675. }
  676. //-----------------------------------------------------------------------------
  677. void SimObject::assignDynamicFieldsFrom(SimObject* parent)
  678. {
  679. if(parent->mFieldDictionary)
  680. {
  681. if( mFieldDictionary == NULL )
  682. mFieldDictionary = new SimFieldDictionary;
  683. mFieldDictionary->assignFrom(parent->mFieldDictionary);
  684. }
  685. }
  686. //-----------------------------------------------------------------------------
  687. void SimObject::assignFieldsFrom(SimObject *parent)
  688. {
  689. // Only allow field assigns from objects of the same class or
  690. // a superclass.
  691. if( getClassRep()->isClass( parent->getClassRep() ) )
  692. {
  693. const AbstractClassRep::FieldList &list = parent->getFieldList();
  694. // copy out all the fields:
  695. for(U32 i = 0; i < list.size(); i++)
  696. {
  697. const AbstractClassRep::Field* f = &list[i];
  698. // Skip the special field types.
  699. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  700. continue;
  701. // Skip certain fields that we don't want to see copied so we don't
  702. // get error messages from their setters.
  703. static StringTableEntry sName = StringTable->insert( "name" );
  704. static StringTableEntry sPersistentId = StringTable->insert( "persistentId" );
  705. if( f->pFieldname == sName || f->pFieldname == sPersistentId )
  706. continue;
  707. S32 lastField = f->elementCount - 1;
  708. for(S32 j = 0; j <= lastField; j++)
  709. {
  710. const char* fieldVal = (*f->getDataFn)( parent, Con::getData(f->type, (void *) (((const char *)parent) + f->offset), j, f->table, f->flag));
  711. // Don't assign the field is the pointer is null or if
  712. // the field is not empty and writing it was disallowed.
  713. if ( !fieldVal || ( fieldVal[0] && !writeField( f->pFieldname, fieldVal ) ) )
  714. continue;
  715. // code copied from SimObject::setDataField().
  716. // TODO: paxorr: abstract this into a better setData / getData that considers prot fields.
  717. FrameTemp<char> buffer(2048);
  718. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  719. ConsoleBaseType *cbt = ConsoleBaseType::getType( f->type );
  720. const char* szBuffer = cbt->prepData( fieldVal, buffer, 2048 );
  721. dMemset( bufferSecure, 0, 2048 );
  722. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  723. //If we have an index worth mentioning, process it for pass-along as well to ensure we set stuff correctly
  724. char* elementIdxBuffer = nullptr;
  725. if (f->elementCount > 1)
  726. {
  727. elementIdxBuffer = Con::getArgBuffer(256);
  728. dSprintf(elementIdxBuffer, 256, "%i", j);
  729. }
  730. if((*f->setDataFn)( this, elementIdxBuffer, bufferSecure ) )
  731. Con::setData(f->type, (void *) (((const char *)this) + f->offset), j, 1, &fieldVal, f->table);
  732. if (f->networkMask != 0)
  733. {
  734. NetObject* netObj = static_cast<NetObject*>(this);
  735. netObj->setMaskBits(f->networkMask);
  736. }
  737. }
  738. }
  739. }
  740. else
  741. {
  742. Con::errorf( "SimObject::assignFieldsFrom() - cannot assigned fields from object of type '%s' to object of type '%s'",
  743. parent->getClassName(), getClassName()
  744. );
  745. }
  746. assignDynamicFieldsFrom(parent);
  747. }
  748. //-----------------------------------------------------------------------------
  749. void SimObject::setDataField(StringTableEntry slotName, const char *array, const char *value)
  750. {
  751. // first search the static fields if enabled
  752. if(mFlags.test(ModStaticFields))
  753. {
  754. const AbstractClassRep::Field *fld = findField(slotName);
  755. if(fld)
  756. {
  757. // Skip the special field types as they are not data.
  758. if ( fld->type >= AbstractClassRep::ARCFirstCustomField )
  759. return;
  760. S32 array1 = array ? dAtoi(array) : 0;
  761. // Here we check to see if <this> is a datablock and if <value>
  762. // starts with "$$". If both true than save value as a runtime substitution.
  763. if (dynamic_cast<SimDataBlock*>(this) && value[0] == '$' && value[1] == '$')
  764. {
  765. if (!this->allowSubstitutions())
  766. {
  767. Con::errorf("Substitution Error: %s datablocks do not allow \"$$\" field substitutions. [%s]",
  768. this->getClassName(), this->getName());
  769. return;
  770. }
  771. if (fld->doNotSubstitute)
  772. {
  773. Con::errorf("Substitution Error: field \"%s\" of datablock %s prohibits \"$$\" field substitutions. [%s]",
  774. slotName, this->getClassName(), this->getName());
  775. return;
  776. }
  777. // add the substitution
  778. ((SimDataBlock*)this)->addSubstitution(slotName, array1, value);
  779. return;
  780. }
  781. if(array1 >= 0 && array1 < fld->elementCount && fld->elementCount >= 1)
  782. {
  783. // If the set data notify callback returns true, then go ahead and
  784. // set the data, otherwise, assume the set notify callback has either
  785. // already set the data, or has deemed that the data should not
  786. // be set at all.
  787. FrameTemp<char> buffer(2048);
  788. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  789. // so that if the prep functions or any other functions use the string stack, the data
  790. // is not corrupted.
  791. ConsoleBaseType *cbt = ConsoleBaseType::getType( fld->type );
  792. AssertFatal( cbt != NULL, "Could not resolve Type Id." );
  793. const char* szBuffer = cbt->prepData( value, buffer, 2048 );
  794. dMemset( bufferSecure, 0, 2048 );
  795. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  796. if( (*fld->setDataFn)( this, array, bufferSecure ) )
  797. Con::setData(fld->type, (void *) (((const char *)this) + fld->offset), array1, 1, &value, fld->table);
  798. if(fld->validator)
  799. fld->validator->validateType(this, (void *) (((const char *)this) + fld->offset));
  800. if (fld->networkMask != 0)
  801. {
  802. NetObject* netObj = static_cast<NetObject*>(this);
  803. netObj->setMaskBits(fld->networkMask);
  804. }
  805. onStaticModified( slotName, value );
  806. return;
  807. }
  808. if(fld->validator)
  809. fld->validator->validateType(this, (void *) (((const char *)this) + fld->offset));
  810. onStaticModified( slotName, value );
  811. return;
  812. }
  813. }
  814. if(mFlags.test(ModDynamicFields))
  815. {
  816. if(!mFieldDictionary)
  817. mFieldDictionary = new SimFieldDictionary;
  818. if(!array)
  819. {
  820. mFieldDictionary->setFieldValue(slotName, value);
  821. onDynamicModified( slotName, value );
  822. }
  823. else
  824. {
  825. char buf[256];
  826. dStrcpy(buf, slotName, 256);
  827. dStrcat(buf, array, 256);
  828. StringTableEntry permanentSlotName = StringTable->insert(buf);
  829. mFieldDictionary->setFieldValue(permanentSlotName, value);
  830. onDynamicModified( permanentSlotName, value );
  831. }
  832. }
  833. }
  834. //-----------------------------------------------------------------------------
  835. const char *SimObject::getDataField(StringTableEntry slotName, const char *array)
  836. {
  837. if(mFlags.test(ModStaticFields))
  838. {
  839. S32 array1 = array ? dAtoi(array) : -1;
  840. const AbstractClassRep::Field *fld = findField(slotName);
  841. if(fld)
  842. {
  843. if(array1 == -1 && fld->elementCount == 1)
  844. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), 0, fld->table, fld->flag) );
  845. if(array1 >= 0 && array1 < fld->elementCount)
  846. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), array1, fld->table, fld->flag) );// + typeSizes[fld.type] * array1));
  847. return "";
  848. }
  849. }
  850. if(mFlags.test(ModDynamicFields))
  851. {
  852. if(!mFieldDictionary)
  853. return "";
  854. if(!array)
  855. {
  856. if (const char* val = mFieldDictionary->getFieldValue(slotName))
  857. return val;
  858. }
  859. else
  860. {
  861. static char buf[256];
  862. dStrcpy(buf, slotName, 256);
  863. dStrcat(buf, array, 256);
  864. if (const char* val = mFieldDictionary->getFieldValue(StringTable->insert(buf)))
  865. return val;
  866. }
  867. }
  868. return "";
  869. }
  870. const char *SimObject::getPrefixedDataField(StringTableEntry fieldName, const char *array)
  871. {
  872. // Sanity!
  873. AssertFatal(fieldName != NULL, "Cannot get field value with NULL field name.");
  874. // Fetch field value.
  875. const char* pFieldValue = getDataField(fieldName, array);
  876. // Sanity.
  877. //AssertFatal(pFieldValue != NULL, "Field value cannot be NULL.");
  878. if (!pFieldValue)
  879. return NULL;
  880. // Return without the prefix if there's no value.
  881. if (*pFieldValue == 0)
  882. return StringTable->EmptyString();
  883. // Fetch the field prefix.
  884. StringTableEntry fieldPrefix = getDataFieldPrefix(fieldName);
  885. // Sanity!
  886. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  887. // Calculate a buffer size including prefix.
  888. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  889. // Fetch a buffer.
  890. char* pValueBuffer = Con::getReturnBuffer(valueBufferSize);
  891. // Format the value buffer.
  892. dSprintf(pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue);
  893. return pValueBuffer;
  894. }
  895. //-----------------------------------------------------------------------------
  896. void SimObject::setPrefixedDataField(StringTableEntry fieldName, const char *_array, const char *value)
  897. {
  898. // Sanity!
  899. AssertFatal(fieldName != NULL, "Cannot set object field value with NULL field name.");
  900. AssertFatal(value != NULL, "Field value cannot be NULL.");
  901. // Set value without prefix if there's no value.
  902. if (*value == 0)
  903. {
  904. setDataField(fieldName, _array, value);
  905. return;
  906. }
  907. // Fetch the field prefix.
  908. StringTableEntry fieldPrefix = getDataFieldPrefix(fieldName);
  909. // Sanity.
  910. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  911. // Do we have a field prefix?
  912. if (fieldPrefix == StringTable->EmptyString())
  913. {
  914. // No, so set the data field in the usual way.
  915. setDataField(fieldName, _array, value);
  916. return;
  917. }
  918. // Yes, so fetch the length of the field prefix.
  919. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  920. // Yes, so does it start with the object field prefix?
  921. if (dStrnicmp(value, fieldPrefix, fieldPrefixLength) != 0)
  922. {
  923. // No, so set the data field in the usual way.
  924. setDataField(fieldName, _array, value);
  925. return;
  926. }
  927. // Yes, so set the data excluding the prefix.
  928. setDataField(fieldName, _array, value + fieldPrefixLength);
  929. }
  930. //-----------------------------------------------------------------------------
  931. const char *SimObject::getPrefixedDynamicDataField(StringTableEntry fieldName, const char *_array, const S32 fieldType)
  932. {
  933. // Sanity!
  934. AssertFatal(fieldName != NULL, "Cannot get field value with NULL field name.");
  935. // Fetch field value.
  936. const char* pFieldValue = getDataField(fieldName, _array);
  937. // Sanity.
  938. AssertFatal(pFieldValue != NULL, "Field value cannot be NULL.");
  939. // Return the field if no field type is specified.
  940. if (fieldType == -1)
  941. return pFieldValue;
  942. // Return without the prefix if there's no value.
  943. if (*pFieldValue == 0)
  944. return StringTable->EmptyString();
  945. // Fetch the console base type.
  946. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType(fieldType);
  947. // Did we find the console base type?
  948. if (pConsoleBaseType == NULL)
  949. {
  950. // No, so warn.
  951. Con::warnf("getPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  952. fieldType, fieldName, pFieldValue);
  953. }
  954. // Fetch the field prefix.
  955. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  956. // Sanity!
  957. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  958. // Calculate a buffer size including prefix.
  959. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  960. // Fetch a buffer.
  961. char* pValueBuffer = Con::getReturnBuffer(valueBufferSize);
  962. // Format the value buffer.
  963. dSprintf(pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue);
  964. return pValueBuffer;
  965. }
  966. //-----------------------------------------------------------------------------
  967. void SimObject::setPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const char *value, const S32 fieldType)
  968. {
  969. // Sanity!
  970. AssertFatal(fieldName != NULL, "Cannot set object field value with NULL field name.");
  971. AssertFatal(value != NULL, "Field value cannot be NULL.");
  972. // Set value without prefix if no field type was specified.
  973. if (fieldType == -1)
  974. {
  975. setDataField(fieldName, NULL, value);
  976. return;
  977. }
  978. // Fetch the console base type.
  979. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType(fieldType);
  980. // Did we find the console base type?
  981. if (pConsoleBaseType == NULL)
  982. {
  983. // No, so warn.
  984. Con::warnf("setPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  985. fieldType, fieldName, value);
  986. }
  987. // Set value without prefix if there's no value or we didn't find the console base type.
  988. if (*value == 0 || pConsoleBaseType == NULL)
  989. {
  990. setDataField(fieldName, NULL, value);
  991. return;
  992. }
  993. // Fetch the field prefix.
  994. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  995. // Sanity.
  996. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  997. // Do we have a field prefix?
  998. if (fieldPrefix == StringTable->EmptyString())
  999. {
  1000. // No, so set the data field in the usual way.
  1001. setDataField(fieldName, NULL, value);
  1002. return;
  1003. }
  1004. // Yes, so fetch the length of the field prefix.
  1005. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  1006. // Yes, so does it start with the object field prefix?
  1007. if (dStrnicmp(value, fieldPrefix, fieldPrefixLength) != 0)
  1008. {
  1009. // No, so set the data field in the usual way.
  1010. setDataField(fieldName, NULL, value);
  1011. return;
  1012. }
  1013. // Yes, so set the data excluding the prefix.
  1014. setDataField(fieldName, NULL, value + fieldPrefixLength);
  1015. }
  1016. //-----------------------------------------------------------------------------
  1017. StringTableEntry SimObject::getDataFieldPrefix(StringTableEntry fieldName)
  1018. {
  1019. // Sanity!
  1020. AssertFatal(fieldName != NULL, "Cannot get field prefix with NULL field name.");
  1021. // Find the field.
  1022. const AbstractClassRep::Field* pField = findField(fieldName);
  1023. // Return nothing if field was not found.
  1024. if (pField == NULL)
  1025. return StringTable->EmptyString();
  1026. // Yes, so fetch the console base type.
  1027. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType(pField->type);
  1028. // Fetch the type prefix.
  1029. return pConsoleBaseType->getTypePrefix();
  1030. }
  1031. //-----------------------------------------------------------------------------
  1032. U32 SimObject::getDataFieldType( StringTableEntry slotName, const char* array )
  1033. {
  1034. const AbstractClassRep::Field* field = findField( slotName );
  1035. if(field)
  1036. return field->type;
  1037. // Check dynamic fields
  1038. if(!mFieldDictionary)
  1039. return 0;
  1040. if(array == NULL || *array == 0)
  1041. return mFieldDictionary->getFieldType( slotName );
  1042. else
  1043. {
  1044. static char buf[256];
  1045. dStrcpy( buf, slotName, 256 );
  1046. dStrcat( buf, array, 256 );
  1047. return mFieldDictionary->getFieldType( StringTable->insert( buf ) );
  1048. }
  1049. }
  1050. //-----------------------------------------------------------------------------
  1051. void SimObject::setDataFieldType(const U32 fieldTypeId, StringTableEntry slotName, const char *array)
  1052. {
  1053. // This only works on dynamic fields, bail if we have no field dictionary
  1054. if(!mFieldDictionary)
  1055. return;
  1056. if(array == NULL || *array == 0)
  1057. {
  1058. mFieldDictionary->setFieldType( slotName, fieldTypeId );
  1059. onDynamicModified( slotName, mFieldDictionary->getFieldValue(slotName) );
  1060. }
  1061. else
  1062. {
  1063. static char buf[256];
  1064. dStrcpy( buf, slotName, 256 );
  1065. dStrcat( buf, array, 256 );
  1066. mFieldDictionary->setFieldType( StringTable->insert( buf ), fieldTypeId );
  1067. onDynamicModified( slotName, mFieldDictionary->getFieldValue(slotName) );
  1068. }
  1069. }
  1070. //-----------------------------------------------------------------------------
  1071. void SimObject::setDataFieldType(const char *typeName, StringTableEntry slotName, const char *array)
  1072. {
  1073. // This only works on dynamic fields, bail if we have no field dictionary
  1074. if(!mFieldDictionary)
  1075. return;
  1076. if(array == NULL || *array == 0)
  1077. mFieldDictionary->setFieldType( slotName, typeName );
  1078. else
  1079. {
  1080. static char buf[256];
  1081. dStrcpy( buf, slotName, 256 );
  1082. dStrcat( buf, array, 256 );
  1083. StringTableEntry permanentSlotName = StringTable->insert(buf);
  1084. mFieldDictionary->setFieldType( permanentSlotName, typeName );
  1085. onDynamicModified( permanentSlotName, mFieldDictionary->getFieldValue(permanentSlotName) );
  1086. }
  1087. }
  1088. // This is the copy-constructor used to create temporary datablock clones.
  1089. // The <temp_clone> argument is added to distinguish this copy-constructor
  1090. // from any general-purpose copy-constructor that might be needed in the
  1091. // future. <temp_clone> should always be true when creating temporary
  1092. // datablock clones.
  1093. //
  1094. SimObject::SimObject(const SimObject& other, bool temp_clone)
  1095. {
  1096. is_temp_clone = temp_clone;
  1097. mObjectName = other.mObjectName;
  1098. mOriginalName = other.mOriginalName;
  1099. nextNameObject = other.nextNameObject;
  1100. nextManagerNameObject = other.nextManagerNameObject;
  1101. nextIdObject = other.nextIdObject;
  1102. mGroup = other.mGroup;
  1103. mFlags = other.mFlags;
  1104. mProgenitorFile = other.mProgenitorFile;
  1105. mCopySource = other.mCopySource;
  1106. mFieldDictionary = other.mFieldDictionary;
  1107. //mIdString = other.mIdString; // special treatment (see below)
  1108. mFilename = other.mFilename;
  1109. mDeclarationLine = other.mDeclarationLine;
  1110. mNotifyList = other.mNotifyList;
  1111. mId = other.mId;
  1112. mInternalName = other.mInternalName;
  1113. mCanSaveFieldDictionary = other.mCanSaveFieldDictionary;
  1114. mPersistentId = other.mPersistentId;
  1115. mNameSpace = other.mNameSpace;
  1116. mClassName = other.mClassName;
  1117. mSuperClassName = other.mSuperClassName;
  1118. preventNameChanging = other.preventNameChanging;
  1119. if (mId)
  1120. dSprintf( mIdString, sizeof( mIdString ), "%u", mId );
  1121. else
  1122. mIdString[ 0 ] = '\0';
  1123. }
  1124. //-----------------------------------------------------------------------------
  1125. void SimObject::dumpClassHierarchy()
  1126. {
  1127. AbstractClassRep* pRep = getClassRep();
  1128. while(pRep)
  1129. {
  1130. Con::warnf("%s ->", pRep->getClassName());
  1131. pRep = pRep->getParentClass();
  1132. }
  1133. }
  1134. //-----------------------------------------------------------------------------
  1135. SimObject* SimObject::clone()
  1136. {
  1137. if( !getClassRep() )
  1138. return NULL;
  1139. ConsoleObject* conObject = getClassRep()->create();
  1140. if( !conObject )
  1141. return NULL;
  1142. SimObject* simObject = dynamic_cast< SimObject* >( conObject );
  1143. if( !simObject )
  1144. {
  1145. delete conObject;
  1146. return NULL;
  1147. }
  1148. simObject->assignFieldsFrom( this );
  1149. String name = Sim::getUniqueName( getName() );
  1150. if( !simObject->registerObject( name ) )
  1151. {
  1152. delete simObject;
  1153. return NULL;
  1154. }
  1155. if( getGroup() )
  1156. getGroup()->addObject( simObject );
  1157. return simObject;
  1158. }
  1159. //-----------------------------------------------------------------------------
  1160. SimObject* SimObject::deepClone()
  1161. {
  1162. return clone();
  1163. }
  1164. //=============================================================================
  1165. // Grouping.
  1166. //=============================================================================
  1167. // MARK: ---- Grouping ----
  1168. //-----------------------------------------------------------------------------
  1169. SimObject* SimObject::findObject( const char* )
  1170. {
  1171. return NULL;
  1172. }
  1173. //-----------------------------------------------------------------------------
  1174. bool SimObject::isChildOfGroup(SimGroup* pGroup)
  1175. {
  1176. if(!pGroup)
  1177. return false;
  1178. //if we *are* the group in question,
  1179. //return true:
  1180. if(pGroup == dynamic_cast<SimGroup*>(this))
  1181. return true;
  1182. SimGroup* temp = mGroup;
  1183. while(temp)
  1184. {
  1185. if(temp == pGroup)
  1186. return true;
  1187. temp = temp->mGroup;
  1188. }
  1189. return false;
  1190. }
  1191. //-----------------------------------------------------------------------------
  1192. bool SimObject::addToSet(SimObjectId spid)
  1193. {
  1194. if (mFlags.test(Added) == false)
  1195. return false;
  1196. SimObject* ptr = Sim::findObject(spid);
  1197. if (ptr)
  1198. {
  1199. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  1200. AssertFatal(sp != 0,
  1201. "SimObject::addToSet: "
  1202. "ObjectId does not refer to a set object");
  1203. sp->addObject(this);
  1204. return true;
  1205. }
  1206. return false;
  1207. }
  1208. //-----------------------------------------------------------------------------
  1209. bool SimObject::addToSet(const char *ObjectName)
  1210. {
  1211. if (mFlags.test(Added) == false)
  1212. return false;
  1213. SimObject* ptr = Sim::findObject(ObjectName);
  1214. if (ptr)
  1215. {
  1216. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  1217. AssertFatal(sp != 0,
  1218. "SimObject::addToSet: "
  1219. "ObjectName does not refer to a set object");
  1220. sp->addObject(this);
  1221. return true;
  1222. }
  1223. return false;
  1224. }
  1225. //-----------------------------------------------------------------------------
  1226. bool SimObject::removeFromSet(SimObjectId sid)
  1227. {
  1228. if (mFlags.test(Added) == false)
  1229. return false;
  1230. SimSet *set;
  1231. if(Sim::findObject(sid, set))
  1232. {
  1233. set->removeObject(this);
  1234. return true;
  1235. }
  1236. return false;
  1237. }
  1238. //-----------------------------------------------------------------------------
  1239. bool SimObject::removeFromSet(const char *objectName)
  1240. {
  1241. if (mFlags.test(Added) == false)
  1242. return false;
  1243. SimSet *set;
  1244. if(Sim::findObject(objectName, set))
  1245. {
  1246. set->removeObject(this);
  1247. return true;
  1248. }
  1249. return false;
  1250. }
  1251. //-----------------------------------------------------------------------------
  1252. void SimObject::dumpGroupHierarchy()
  1253. {
  1254. String className( getClassName() );
  1255. String objectName( getName() );
  1256. Con::warnf( "[%i] %s - %s ->", getId(), className.c_str(), objectName.c_str() );
  1257. if ( mGroup )
  1258. mGroup->dumpGroupHierarchy();
  1259. }
  1260. //=============================================================================
  1261. // Events.
  1262. //=============================================================================
  1263. // MARK: ---- Events ----
  1264. //-----------------------------------------------------------------------------
  1265. bool SimObject::onAdd()
  1266. {
  1267. mFlags.set(Added);
  1268. linkNamespaces();
  1269. return true;
  1270. }
  1271. //-----------------------------------------------------------------------------
  1272. void SimObject::onRemove()
  1273. {
  1274. mFlags.clear(Added);
  1275. unlinkNamespaces();
  1276. }
  1277. //-----------------------------------------------------------------------------
  1278. void SimObject::onGroupAdd()
  1279. {
  1280. }
  1281. //-----------------------------------------------------------------------------
  1282. void SimObject::onGroupRemove()
  1283. {
  1284. }
  1285. //-----------------------------------------------------------------------------
  1286. void SimObject::onDeleteNotify(SimObject*)
  1287. {
  1288. }
  1289. //-----------------------------------------------------------------------------
  1290. void SimObject::onNameChange(const char*)
  1291. {
  1292. }
  1293. //-----------------------------------------------------------------------------
  1294. void SimObject::onStaticModified(const char* slotName, const char* newValue)
  1295. {
  1296. }
  1297. //-----------------------------------------------------------------------------
  1298. void SimObject::onDynamicModified(const char* slotName, const char* newValue)
  1299. {
  1300. }
  1301. //=============================================================================
  1302. // Notifications.
  1303. //=============================================================================
  1304. // MARK: ---- Notifications ----
  1305. static Chunker<SimObject::Notify> notifyChunker(128000);
  1306. SimObject::Notify *SimObject::mNotifyFreeList = NULL;
  1307. //-----------------------------------------------------------------------------
  1308. SimObject::Notify *SimObject::allocNotify()
  1309. {
  1310. if(mNotifyFreeList)
  1311. {
  1312. SimObject::Notify *ret = mNotifyFreeList;
  1313. mNotifyFreeList = ret->next;
  1314. return ret;
  1315. }
  1316. return notifyChunker.alloc();
  1317. }
  1318. //-----------------------------------------------------------------------------
  1319. void SimObject::freeNotify(SimObject::Notify* note)
  1320. {
  1321. AssertFatal(note->type != SimObject::Notify::Invalid, "Invalid notify");
  1322. note->type = SimObject::Notify::Invalid;
  1323. note->next = mNotifyFreeList;
  1324. mNotifyFreeList = note;
  1325. }
  1326. //-----------------------------------------------------------------------------
  1327. SimObject::Notify* SimObject::removeNotify(void *ptr, SimObject::Notify::Type type)
  1328. {
  1329. Notify **list = &mNotifyList;
  1330. while(*list)
  1331. {
  1332. if((*list)->ptr == ptr && (*list)->type == type)
  1333. {
  1334. SimObject::Notify *ret = *list;
  1335. *list = ret->next;
  1336. return ret;
  1337. }
  1338. list = &((*list)->next);
  1339. }
  1340. return NULL;
  1341. }
  1342. //-----------------------------------------------------------------------------
  1343. void SimObject::deleteNotify(SimObject* obj)
  1344. {
  1345. AssertFatal(!obj->isDeleted(),
  1346. "SimManager::deleteNotify: Object is being deleted");
  1347. Notify *note = allocNotify();
  1348. note->ptr = (void *) this;
  1349. note->next = obj->mNotifyList;
  1350. note->type = Notify::DeleteNotify;
  1351. obj->mNotifyList = note;
  1352. note = allocNotify();
  1353. note->ptr = (void *) obj;
  1354. note->next = mNotifyList;
  1355. note->type = Notify::ClearNotify;
  1356. mNotifyList = note;
  1357. //obj->deleteNotifyList.pushBack(this);
  1358. //clearNotifyList.pushBack(obj);
  1359. }
  1360. //-----------------------------------------------------------------------------
  1361. void SimObject::registerReference(SimObject **ptr)
  1362. {
  1363. Notify *note = allocNotify();
  1364. note->ptr = (void *) ptr;
  1365. note->next = mNotifyList;
  1366. note->type = Notify::ObjectRef;
  1367. mNotifyList = note;
  1368. }
  1369. //-----------------------------------------------------------------------------
  1370. void SimObject::unregisterReference(SimObject **ptr)
  1371. {
  1372. Notify *note = removeNotify((void *) ptr, Notify::ObjectRef);
  1373. if(note)
  1374. {
  1375. freeNotify(note);
  1376. if( mFlags.test( AutoDelete ) )
  1377. {
  1378. for( Notify* n = mNotifyList; n != NULL; n = n->next )
  1379. if( n->type == Notify::ObjectRef )
  1380. return;
  1381. deleteObject();
  1382. }
  1383. }
  1384. }
  1385. //-----------------------------------------------------------------------------
  1386. void SimObject::clearNotify(SimObject* obj)
  1387. {
  1388. Notify *note = obj->removeNotify((void *) this, Notify::DeleteNotify);
  1389. if(note)
  1390. freeNotify(note);
  1391. note = removeNotify((void *) obj, Notify::ClearNotify);
  1392. if(note)
  1393. freeNotify(note);
  1394. }
  1395. //-----------------------------------------------------------------------------
  1396. void SimObject::processDeleteNotifies()
  1397. {
  1398. // clear out any delete notifies and
  1399. // object refs.
  1400. while(mNotifyList)
  1401. {
  1402. Notify *note = mNotifyList;
  1403. mNotifyList = note->next;
  1404. AssertFatal(note->type != Notify::ClearNotify, "Clear notes should be all gone.");
  1405. if(note->type == Notify::DeleteNotify)
  1406. {
  1407. SimObject *obj = (SimObject *) note->ptr;
  1408. Notify *cnote = obj->removeNotify((void *)this, Notify::ClearNotify);
  1409. obj->onDeleteNotify(this);
  1410. freeNotify(cnote);
  1411. }
  1412. else
  1413. {
  1414. // it must be an object ref - a pointer refs this object
  1415. *((SimObject **) note->ptr) = NULL;
  1416. }
  1417. freeNotify(note);
  1418. }
  1419. }
  1420. //-----------------------------------------------------------------------------
  1421. void SimObject::clearAllNotifications()
  1422. {
  1423. for(Notify **cnote = &mNotifyList; *cnote; )
  1424. {
  1425. Notify *temp = *cnote;
  1426. if(temp->type == Notify::ClearNotify)
  1427. {
  1428. *cnote = temp->next;
  1429. Notify *note = ((SimObject *) temp->ptr)->removeNotify((void *) this, Notify::DeleteNotify);
  1430. freeNotify(temp);
  1431. if ( note )
  1432. freeNotify(note);
  1433. }
  1434. else
  1435. cnote = &(temp->next);
  1436. }
  1437. }
  1438. //=============================================================================
  1439. // Namespaces.
  1440. //=============================================================================
  1441. // MARK: ---- Namespaces ----
  1442. //-----------------------------------------------------------------------------
  1443. void SimObject::linkNamespaces()
  1444. {
  1445. // Don't link if we already have a namespace linkage in place.
  1446. // If you want to change namespace linking, first call unlinkNamespaces()
  1447. // while still having the class namespace fields matching the current
  1448. // setup.
  1449. if (mNameSpace)
  1450. {
  1451. Con::warnf("SimObject::linkNamespaces -- Namespace linkage already in place %s", mNameSpace->getName());
  1452. return;
  1453. }
  1454. // Get the namespace for the C++ class.
  1455. Namespace* cppNamespace = getClassRep()->getNameSpace();
  1456. // Parent namespace defaults to namespace of C++ class.
  1457. Namespace* parentNamespace = cppNamespace;
  1458. // Perform superclass linking, if requested.
  1459. if( mSuperClassName && mSuperClassName[ 0 ] )
  1460. {
  1461. // Look up the superclass namespace.
  1462. Namespace* superClassNamespace = Con::lookupNamespace( mSuperClassName );
  1463. // If packages are active and adding to the superclass namespace, then we will
  1464. // have multiple packages in a parent chain that all have the same name.
  1465. // Con::lookupNamespace returns the bottom-most package in the chain to us so
  1466. // in order to properly link namespace here without conflicting with the package
  1467. // mechanism, we need to properly link child namespaces to the bottom-most namespace
  1468. // while linking parent namespaces to the topmost namespace. To find the latter
  1469. // one, we walk up the hierarchy here.
  1470. Namespace* superClassNamespacePackageRoot = superClassNamespace->getPackageRoot();
  1471. // Link the superclass namespace to the C++ class namespace.
  1472. if( superClassNamespacePackageRoot->getParent() == NULL )
  1473. {
  1474. // The superclass namespace isn't linked yet so we just
  1475. // link it to the C++ class namespace and make that our parent.
  1476. // No increasing parent reference counts is needed in this case.
  1477. bool ok = superClassNamespacePackageRoot->classLinkTo( cppNamespace );
  1478. AssertFatal( ok, "SimObject::linkNamespaces - failed to link new namespace to c++ class name" );
  1479. parentNamespace = superClassNamespace;
  1480. }
  1481. else
  1482. {
  1483. // In debug builds, make sure the namespace hierarchy that's been
  1484. // put into place actually makes sense and leads back to the C++
  1485. // class namespace.
  1486. #ifdef TORQUE_DEBUG
  1487. bool foundClassNameNS = false;
  1488. for( Namespace* linkWalk = superClassNamespacePackageRoot->getParent(); linkWalk != NULL;
  1489. linkWalk = linkWalk->getParent() )
  1490. {
  1491. if( linkWalk == cppNamespace )
  1492. {
  1493. foundClassNameNS = true;
  1494. break;
  1495. }
  1496. }
  1497. if( !foundClassNameNS )
  1498. {
  1499. // C++ class namespace not in parent link chain. Warn about it.
  1500. Con::errorf(
  1501. "SimObject::linkNamespaces - cannot link object to superclass %s because c++ class %s is not in the parent namespace chain. Linking object to c++ class.",
  1502. mSuperClassName,
  1503. getClassName()
  1504. );
  1505. // Clear out superclass name so we don't come across it during
  1506. // unlinking.
  1507. mSuperClassName = NULL;
  1508. }
  1509. else
  1510. #endif
  1511. {
  1512. // Super link is ok.
  1513. parentNamespace = superClassNamespace;
  1514. // Now increase the reference count of all namespaces in the parent hierarchy
  1515. // (up to the C++ class).
  1516. for( Namespace* linkWalk = parentNamespace;
  1517. linkWalk != NULL && linkWalk != cppNamespace && linkWalk->getParent() != NULL;
  1518. linkWalk = linkWalk->getParent() )
  1519. {
  1520. // Skip namespaces coming from packages.
  1521. if( linkWalk->getPackage() != NULL )
  1522. continue;
  1523. linkWalk->incRefCountToParent();
  1524. }
  1525. }
  1526. }
  1527. }
  1528. // If class name is set, link it in as the new parent
  1529. // which itself inherits from the current parent.
  1530. if( mClassName && mClassName[ 0 ] )
  1531. {
  1532. Namespace* classNamespace = Con::lookupNamespace( mClassName );
  1533. if( classNamespace && classNamespace->classLinkTo( parentNamespace ) )
  1534. {
  1535. parentNamespace = classNamespace;
  1536. }
  1537. else
  1538. {
  1539. // Clear out class name so we don't perform a bogus unlink
  1540. // in unlinkNamespaces().
  1541. mClassName = NULL;
  1542. }
  1543. }
  1544. // Finally, if we have an object name, link its namespace
  1545. // as the child to the current parent namespace and let it
  1546. // become the final namespace of this object.
  1547. StringTableEntry objectName = getName();
  1548. if( objectName && objectName[ 0 ] )
  1549. {
  1550. Namespace* objectNamespace = Con::lookupNamespace( objectName );
  1551. if( objectNamespace && objectNamespace->classLinkTo( parentNamespace ) )
  1552. {
  1553. parentNamespace = objectNamespace;
  1554. }
  1555. }
  1556. // Store our namespace.
  1557. mNameSpace = parentNamespace;
  1558. }
  1559. //-----------------------------------------------------------------------------
  1560. void SimObject::unlinkNamespaces()
  1561. {
  1562. if( !mNameSpace )
  1563. return;
  1564. Namespace* cppNamespace = getClassRep()->getNameSpace();
  1565. Namespace* parentNamespace = cppNamespace;
  1566. // Handle superclass.
  1567. if( mSuperClassName && mSuperClassName[ 0 ] )
  1568. {
  1569. // Get the superclass namespace.
  1570. Namespace* superClassNamespace = Con::lookupNamespace( mSuperClassName );
  1571. // Make it the parent namespace.
  1572. parentNamespace = superClassNamespace;
  1573. // Decrease parent refcounts on the superclass hierarchy.
  1574. for( Namespace* linkWalk = superClassNamespace;
  1575. linkWalk != NULL && linkWalk != cppNamespace && linkWalk->getParent() != NULL; )
  1576. {
  1577. // Store the parent link since it may disappear once we
  1578. // decrease the reference count.
  1579. Namespace* parent = linkWalk->getParent();
  1580. // Decrease the refcount.
  1581. if( linkWalk->getPackage() == NULL ) // Skip namespaces coming from packages.
  1582. linkWalk->decRefCountToParent();
  1583. // Walk up.
  1584. linkWalk = parent;
  1585. }
  1586. }
  1587. // Handle class.
  1588. if( mClassName && mClassName[ 0 ] )
  1589. {
  1590. Namespace* classNamespace = Con::lookupNamespace( mClassName );
  1591. if( classNamespace )
  1592. {
  1593. classNamespace->decRefCountToParent();
  1594. parentNamespace = classNamespace;
  1595. }
  1596. }
  1597. // Handle object name.
  1598. StringTableEntry objectName = getName();
  1599. if( objectName && objectName[ 0 ] )
  1600. mNameSpace->decRefCountToParent();
  1601. mNameSpace = NULL;
  1602. }
  1603. //-----------------------------------------------------------------------------
  1604. void SimObject::setClassNamespace( const char *classNamespace )
  1605. {
  1606. StringTableEntry oldClassNamespace = mClassName;
  1607. StringTableEntry newClassNamespace = StringTable->insert( classNamespace );
  1608. if( oldClassNamespace == newClassNamespace )
  1609. return;
  1610. if( isProperlyAdded() )
  1611. unlinkNamespaces();
  1612. mClassName = newClassNamespace;
  1613. if( isProperlyAdded() )
  1614. {
  1615. linkNamespaces();
  1616. // Restore old namespace setup if linkage failed.
  1617. if( mClassName != newClassNamespace )
  1618. {
  1619. mClassName = oldClassNamespace;
  1620. linkNamespaces();
  1621. }
  1622. }
  1623. }
  1624. //-----------------------------------------------------------------------------
  1625. void SimObject::setSuperClassNamespace( const char *superClassNamespace )
  1626. {
  1627. StringTableEntry oldSuperClassNamespace = mSuperClassName;
  1628. StringTableEntry newSuperClassNamespace = StringTable->insert( superClassNamespace );
  1629. if( oldSuperClassNamespace == newSuperClassNamespace )
  1630. return;
  1631. if( isProperlyAdded() )
  1632. unlinkNamespaces();
  1633. mSuperClassName = newSuperClassNamespace;
  1634. if( isProperlyAdded() )
  1635. {
  1636. linkNamespaces();
  1637. // Restore old setup if linkage failed.
  1638. if( mSuperClassName != newSuperClassNamespace )
  1639. {
  1640. mSuperClassName = oldSuperClassNamespace;
  1641. linkNamespaces();
  1642. }
  1643. }
  1644. }
  1645. //=============================================================================
  1646. // Misc.
  1647. //=============================================================================
  1648. // MARK: ---- Misc ----
  1649. //-----------------------------------------------------------------------------
  1650. void SimObject::setInternalName( const char* newname )
  1651. {
  1652. if( newname )
  1653. mInternalName = StringTable->insert( newname );
  1654. else
  1655. mInternalName = StringTable->EmptyString();
  1656. }
  1657. //-----------------------------------------------------------------------------
  1658. void SimObject::setOriginalName( const char* originalName )
  1659. {
  1660. if( originalName )
  1661. mOriginalName = StringTable->insert( originalName );
  1662. else
  1663. mOriginalName = StringTable->EmptyString();
  1664. }
  1665. //-----------------------------------------------------------------------------
  1666. const char *SimObject::tabComplete(const char *prevText, S32 baseLen, bool fForward)
  1667. {
  1668. return mNameSpace->tabComplete(prevText, baseLen, fForward);
  1669. }
  1670. //-----------------------------------------------------------------------------
  1671. void SimObject::setSelected( bool sel )
  1672. {
  1673. if( mFlags.test( Selected ) == sel )
  1674. return; // No change.
  1675. if( sel )
  1676. {
  1677. mFlags.set( Selected );
  1678. _onSelected();
  1679. }
  1680. else
  1681. {
  1682. mFlags.clear( Selected );
  1683. _onUnselected();
  1684. }
  1685. }
  1686. //-----------------------------------------------------------------------------
  1687. bool SimObject::isSelectedRecursive() const
  1688. {
  1689. const SimObject *walk = this;
  1690. while ( walk )
  1691. {
  1692. if ( walk->isSelected() )
  1693. return true;
  1694. walk = walk->getGroup();
  1695. }
  1696. return false;
  1697. }
  1698. //-----------------------------------------------------------------------------
  1699. void SimObject::setLocked( bool b )
  1700. {
  1701. if( b )
  1702. mFlags.set( Locked );
  1703. else
  1704. mFlags.clear( Locked );
  1705. }
  1706. //-----------------------------------------------------------------------------
  1707. void SimObject::setHidden( bool b )
  1708. {
  1709. if( b )
  1710. mFlags.set( Hidden );
  1711. else
  1712. mFlags.clear( Hidden );
  1713. }
  1714. //-----------------------------------------------------------------------------
  1715. void SimObject::setCopySource( SimObject* object )
  1716. {
  1717. if( mCopySource )
  1718. mCopySource->unregisterReference( &mCopySource );
  1719. mCopySource = object;
  1720. if( mCopySource )
  1721. mCopySource->registerReference( &mCopySource );
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. bool SimObject::_setCanSave( void* object, const char* index, const char* data )
  1725. {
  1726. SimObject* obj = reinterpret_cast< SimObject* >( object );
  1727. obj->setCanSave( dAtob( data ) );
  1728. return false;
  1729. }
  1730. //-----------------------------------------------------------------------------
  1731. const char* SimObject::_getCanSave( void* object, const char* data )
  1732. {
  1733. SimObject* obj = reinterpret_cast< SimObject* >( object );
  1734. if( obj->getCanSave() )
  1735. return "1";
  1736. else
  1737. return "0";
  1738. }
  1739. //---------------------------------------------------------------------------
  1740. // Copy SimObject to another SimObject (Originally designed for T2D).
  1741. void SimObject::copyTo( SimObject* object )
  1742. {
  1743. object->mClassName = mClassName;
  1744. object->mSuperClassName = mSuperClassName;
  1745. linkNamespaces();
  1746. }
  1747. //-----------------------------------------------------------------------------
  1748. bool SimObject::setProtectedParent( void *obj, const char *index, const char *data )
  1749. {
  1750. SimGroup *parent = NULL;
  1751. SimObject *object = static_cast<SimObject*>(obj);
  1752. if(Sim::findObject(data, parent))
  1753. parent->addObject(object);
  1754. // always return false, because we've set mGroup when we called addObject
  1755. return false;
  1756. }
  1757. //-----------------------------------------------------------------------------
  1758. bool SimObject::setProtectedName(void *obj, const char *index, const char *data)
  1759. {
  1760. if (preventNameChanging)
  1761. return false;
  1762. SimObject* object = static_cast<SimObject*>(obj);
  1763. if (object->isProperlyAdded())
  1764. object->assignName(data);
  1765. // always return false because we assign the name here
  1766. return false;
  1767. }
  1768. //-----------------------------------------------------------------------------
  1769. bool SimObject::setInheritFrom(void* obj, const char* index, const char* data)
  1770. {
  1771. SimObject* object = static_cast<SimObject*>(obj);
  1772. SimObject* parent;
  1773. if (Sim::findObject(data, parent))
  1774. {
  1775. object->setCopySource(parent);
  1776. object->assignFieldsFrom(parent);
  1777. // copy any substitution statements
  1778. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  1779. if (parent_db)
  1780. {
  1781. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(object);
  1782. if (currentNewObject_db)
  1783. currentNewObject_db->copySubstitutionsFrom(parent_db);
  1784. }
  1785. }
  1786. return true;
  1787. }
  1788. //-----------------------------------------------------------------------------
  1789. void SimObject::inspectPreApply()
  1790. {
  1791. }
  1792. //-----------------------------------------------------------------------------
  1793. void SimObject::inspectPostApply()
  1794. {
  1795. onInspectPostApply_callback(this);
  1796. }
  1797. //-----------------------------------------------------------------------------
  1798. String SimObject::_getLogMessage(const char* fmt, va_list args) const
  1799. {
  1800. String objClass = "UnknownClass";
  1801. if(getClassRep())
  1802. objClass = getClassRep()->getClassName();
  1803. String objName = getName();
  1804. if(objName.isEmpty())
  1805. objName = "Unnamed";
  1806. String formattedMessage = String::VToString(fmt, args);
  1807. return String::ToString("%s - %s(%i) - %s",
  1808. objClass.c_str(), objName.c_str(), getId(), formattedMessage.c_str());
  1809. }
  1810. //=============================================================================
  1811. // API.
  1812. //=============================================================================
  1813. // MARK: ---- API ----
  1814. //-----------------------------------------------------------------------------
  1815. DefineEngineMethod( SimObject, dumpGroupHierarchy, void, (),,
  1816. "Dump the hierarchy of this object up to RootGroup to the console." )
  1817. {
  1818. object->dumpGroupHierarchy();
  1819. }
  1820. //-----------------------------------------------------------------------------
  1821. DefineEngineMethod( SimObject, isMethod, bool, ( const char* methodName ),,
  1822. "Test whether the given method is defined on this object.\n"
  1823. "@param The name of the method.\n"
  1824. "@return True if the object implements the given method." )
  1825. {
  1826. return object->isMethod( methodName );
  1827. }
  1828. //-----------------------------------------------------------------------------
  1829. DefineEngineMethod( SimObject, isChildOfGroup, bool, ( SimGroup* group ),,
  1830. "Test whether the object belongs directly or indirectly to the given group.\n"
  1831. "@param group The SimGroup object.\n"
  1832. "@return True if the object is a child of the given group or a child of a group that the given group is directly or indirectly a child to." )
  1833. {
  1834. return object->isChildOfGroup( group );
  1835. }
  1836. //-----------------------------------------------------------------------------
  1837. DefineEngineMethod( SimObject, getClassNamespace, const char*, (),,
  1838. "Get the name of the class namespace assigned to this object.\n"
  1839. "@return The name of the 'class' namespace." )
  1840. {
  1841. return object->getClassNamespace();
  1842. }
  1843. //-----------------------------------------------------------------------------
  1844. DefineEngineMethod( SimObject, getSuperClassNamespace, const char*, (),,
  1845. "Get the name of the superclass namespace assigned to this object.\n"
  1846. "@return The name of the 'superClass' namespace." )
  1847. {
  1848. return object->getSuperClassNamespace();
  1849. }
  1850. //-----------------------------------------------------------------------------
  1851. DefineEngineMethod( SimObject, setClassNamespace, void, ( const char* name ),,
  1852. "Assign a class namespace to this object.\n"
  1853. "@param name The name of the 'class' namespace for this object." )
  1854. {
  1855. object->setClassNamespace( name );
  1856. }
  1857. //-----------------------------------------------------------------------------
  1858. DefineEngineMethod( SimObject, setSuperClassNamespace, void, ( const char* name ),,
  1859. "Assign a superclass namespace to this object.\n"
  1860. "@param name The name of the 'superClass' namespace for this object." )
  1861. {
  1862. object->setSuperClassNamespace( name );
  1863. }
  1864. //-----------------------------------------------------------------------------
  1865. DefineEngineMethod( SimObject, isSelected, bool, (),,
  1866. "Get whether the object has been marked as selected. (in editor)\n"
  1867. "@return True if the object is currently selected." )
  1868. {
  1869. return object->isSelected();
  1870. }
  1871. //-----------------------------------------------------------------------------
  1872. DefineEngineMethod( SimObject, setIsSelected, void, ( bool state ), ( true ),
  1873. "Set whether the object has been marked as selected. (in editor)\n"
  1874. "@param state True if object is to be marked selected; false if not." )
  1875. {
  1876. object->setSelected( state );
  1877. }
  1878. //-----------------------------------------------------------------------------
  1879. DefineEngineMethod( SimObject, isExpanded, bool, (),,
  1880. "Get whether the object has been marked as expanded. (in editor)\n"
  1881. "@return True if the object is marked expanded." )
  1882. {
  1883. return object->isExpanded();
  1884. }
  1885. //-----------------------------------------------------------------------------
  1886. DefineEngineMethod( SimObject, setIsExpanded, void, ( bool state ), ( true ),
  1887. "Set whether the object has been marked as expanded. (in editor)\n"
  1888. "@param state True if the object is to be marked expanded; false if not." )
  1889. {
  1890. object->setExpanded( state );
  1891. }
  1892. //-----------------------------------------------------------------------------
  1893. DefineEngineMethod( SimObject, getFilename, const char*, (),,
  1894. "Returns the filename the object is attached to.\n"
  1895. "@return The name of the file the object is associated with; usually the file the object was loaded from." )
  1896. {
  1897. return object->getFilename();
  1898. }
  1899. //-----------------------------------------------------------------------------
  1900. DefineEngineMethod( SimObject, setFilename, void, ( const char* fileName ),,
  1901. "Sets the object's file name and path\n"
  1902. "@param fileName The name of the file to associate this object with." )
  1903. {
  1904. return object->setFilename( fileName );
  1905. }
  1906. //-----------------------------------------------------------------------------
  1907. DefineEngineMethod( SimObject, getDeclarationLine, S32, (),,
  1908. "Get the line number at which the object is defined in its file.\n\n"
  1909. "@return The line number of the object's definition in script.\n"
  1910. "@see getFilename()")
  1911. {
  1912. return object->getDeclarationLine();
  1913. }
  1914. //-----------------------------------------------------------------------------
  1915. #ifdef TORQUE_DEBUG
  1916. static const char* sEnumCallbackFunction;
  1917. static void sEnumCallback( EngineObject* object )
  1918. {
  1919. SimObject* simObject = dynamic_cast< SimObject* >( object );
  1920. if( !simObject )
  1921. return;
  1922. Con::evaluatef( "%s( %i );", sEnumCallbackFunction, simObject->getId() );
  1923. }
  1924. DefineEngineFunction( debugEnumInstances, void, ( const char* className, const char* functionName ),,
  1925. "Call the given function for each instance of the given class.\n"
  1926. "@param className Name of the class for which to enumerate instances.\n"
  1927. "@param functionName Name of function to call and pass each instance of the given class.\n"
  1928. "@note This function is only available in debug builds and primarily meant as an aid in debugging."
  1929. "@ingroup Console")
  1930. {
  1931. sEnumCallbackFunction = functionName;
  1932. ConsoleObject::debugEnumInstances( className, sEnumCallback );
  1933. }
  1934. #endif
  1935. //-----------------------------------------------------------------------------
  1936. DefineEngineMethod( SimObject, assignFieldsFrom, void, ( SimObject* fromObject ),,
  1937. "Copy fields from another object onto this one. The objects must "
  1938. "be of same type. Everything from the object will overwrite what's "
  1939. "in this object; extra fields in this object will remain. This "
  1940. "includes dynamic fields.\n"
  1941. "@param fromObject The object from which to copy fields." )
  1942. {
  1943. if( fromObject )
  1944. object->assignFieldsFrom( fromObject );
  1945. }
  1946. //-----------------------------------------------------------------------------
  1947. DefineEngineMethod( SimObject, assignPersistentId, void, (),,
  1948. "Assign a persistent ID to the object if it does not already have one." )
  1949. {
  1950. object->getOrCreatePersistentId();
  1951. }
  1952. //-----------------------------------------------------------------------------
  1953. DefineEngineMethod( SimObject, getCanSave, bool, (),,
  1954. "Get whether the object will be included in saves.\n"
  1955. "@return True if the object will be saved; false otherwise." )
  1956. {
  1957. return object->getCanSave();
  1958. }
  1959. //-----------------------------------------------------------------------------
  1960. DefineEngineMethod( SimObject, setCanSave, void, ( bool value ), ( true ),
  1961. "Set whether the object will be included in saves.\n"
  1962. "@param value If true, the object will be included in saves; if false, it will be excluded." )
  1963. {
  1964. object->setCanSave( value );
  1965. }
  1966. //-----------------------------------------------------------------------------
  1967. DefineEngineMethod( SimObject, isEditorOnly, bool, (),,
  1968. "Return true if the object is only used by the editor.\n"
  1969. "@return True if this object exists only for the sake of editing." )
  1970. {
  1971. return object->isEditorOnly();
  1972. }
  1973. //-----------------------------------------------------------------------------
  1974. DefineEngineMethod( SimObject, setEditorOnly, void, ( bool value ), ( true ),
  1975. "Set/clear the editor-only flag on this object.\n"
  1976. "@param value If true, the object is marked as existing only for the editor." )
  1977. {
  1978. object->setEditorOnly( value );
  1979. }
  1980. //-----------------------------------------------------------------------------
  1981. DefineEngineMethod( SimObject, isNameChangeAllowed, bool, (),,
  1982. "Get whether this object may be renamed.\n"
  1983. "@return True if this object can be renamed; false otherwise." )
  1984. {
  1985. return object->isNameChangeAllowed();
  1986. }
  1987. //-----------------------------------------------------------------------------
  1988. DefineEngineMethod( SimObject, setNameChangeAllowed, void, ( bool value ), ( true ),
  1989. "Set whether this object can be renamed from its first name.\n"
  1990. "@param value If true, renaming is allowed for this object; if false, trying to change the name of the object will generate a console error." )
  1991. {
  1992. object->setNameChangeAllowed( value );
  1993. }
  1994. //-----------------------------------------------------------------------------
  1995. DefineEngineMethod( SimObject, clone, SimObject*, (),,
  1996. "Create a copy of this object.\n"
  1997. "@return An exact duplicate of this object." )
  1998. {
  1999. return object->clone();
  2000. }
  2001. //-----------------------------------------------------------------------------
  2002. DefineEngineMethod( SimObject, deepClone, SimObject*, (),,
  2003. "Create a copy of this object and all its subobjects.\n"
  2004. "@return An exact duplicate of this object and all objects it references." )
  2005. {
  2006. return object->deepClone();
  2007. }
  2008. //-----------------------------------------------------------------------------
  2009. DefineEngineMethod( SimObject, setLocked, void, ( bool value ), ( true ),
  2010. "Lock/unlock the object in the editor.\n"
  2011. "@param value If true, the object will be locked; if false, the object will be unlocked." )
  2012. {
  2013. object->setLocked( value );
  2014. }
  2015. //-----------------------------------------------------------------------------
  2016. DefineEngineMethod( SimObject, setHidden, void, ( bool value ), ( true ),
  2017. "Hide/unhide the object.\n"
  2018. "@param value If true, the object will be hidden; if false, the object will be unhidden." )
  2019. {
  2020. object->setHidden( value );
  2021. }
  2022. //-----------------------------------------------------------------------------
  2023. DefineEngineMethod( SimObject, dumpMethods, ArrayObject*, (),,
  2024. "List the methods defined on this object.\n\n"
  2025. "Each description is a newline-separated vector with the following elements:\n"
  2026. "- Minimum number of arguments.\n"
  2027. "- Maximum number of arguments.\n"
  2028. "- Prototype string.\n"
  2029. "- Full script file path (if script method).\n"
  2030. "- Line number of method definition in script (if script method).\n\n"
  2031. "- Documentation string (not including prototype). This takes up the remainder of the vector.\n"
  2032. "@return An ArrayObject populated with (name,description) pairs of all methods defined on the object." )
  2033. {
  2034. Namespace *ns = object->getNamespace();
  2035. if( !ns )
  2036. return 0;
  2037. ArrayObject* dictionary = new ArrayObject();
  2038. dictionary->registerObject();
  2039. VectorPtr<Namespace::Entry *> vec(__FILE__, __LINE__);
  2040. ns->getEntryList(&vec);
  2041. for(Vector< Namespace::Entry* >::iterator j = vec.begin(); j != vec.end(); j++)
  2042. {
  2043. Namespace::Entry* e = *j;
  2044. if( e->mType < 0 )
  2045. continue;
  2046. StringBuilder str;
  2047. str.append( String::ToString( e->mMinArgs ) );
  2048. str.append( '\n' );
  2049. str.append( String::ToString( e->mMaxArgs ) );
  2050. str.append( '\n' );
  2051. str.append( e->getPrototypeString() );
  2052. str.append( '\n' );
  2053. if( e->mCode && e->mCode->fullPath )
  2054. str.append( e->mCode->fullPath );
  2055. str.append( '\n' );
  2056. if( e->mCode )
  2057. str.append( String::ToString( e->mFunctionLineNumber ) );
  2058. str.append( '\n' );
  2059. String docs = e->getDocString();
  2060. if( !docs.isEmpty() )
  2061. str.append( docs );
  2062. dictionary->push_back( e->mFunctionName, str.end() );
  2063. }
  2064. return dictionary;
  2065. }
  2066. //-----------------------------------------------------------------------------
  2067. namespace {
  2068. S32 QSORT_CALLBACK compareFields( const void* a,const void* b )
  2069. {
  2070. const AbstractClassRep::Field* fa = *((const AbstractClassRep::Field**)a);
  2071. const AbstractClassRep::Field* fb = *((const AbstractClassRep::Field**)b);
  2072. return dStricmp(fa->pFieldname, fb->pFieldname);
  2073. }
  2074. struct DocString
  2075. {
  2076. char mPadding[ 8 ];
  2077. String mPrototype;
  2078. String mDescription;
  2079. const char* mReturnType;
  2080. DocString( Namespace::Entry* entry )
  2081. : mPrototype( entry->getArgumentsString() ),
  2082. mDescription( entry->getBriefDescription() )
  2083. {
  2084. mReturnType = " ";
  2085. mPadding[ 0 ] = 0;
  2086. if( entry->mType == -4 )
  2087. {
  2088. //TODO: need to have script callbacks set up proper return type info
  2089. }
  2090. else
  2091. {
  2092. switch( entry->mType )
  2093. {
  2094. case Namespace::Entry::StringCallbackType:
  2095. mReturnType = "string";
  2096. mPadding[ 0 ] = ' ';
  2097. mPadding[ 1 ] = ' ';
  2098. mPadding[ 2 ] = 0;
  2099. break;
  2100. case Namespace::Entry::IntCallbackType:
  2101. mReturnType = "int";
  2102. mPadding[ 0 ] = ' ';
  2103. mPadding[ 1 ] = ' ';
  2104. mPadding[ 2 ] = ' ';
  2105. mPadding[ 3 ] = ' ';
  2106. mPadding[ 4 ] = ' ';
  2107. mPadding[ 5 ] = 0;
  2108. break;
  2109. case Namespace::Entry::FloatCallbackType:
  2110. mReturnType = "float";
  2111. mPadding[ 0 ] = ' ';
  2112. mPadding[ 1 ] = ' ';
  2113. mPadding[ 2 ] = ' ';
  2114. mPadding[ 3 ] = 0;
  2115. break;
  2116. case Namespace::Entry::VoidCallbackType:
  2117. mReturnType = "void";
  2118. mPadding[ 0 ] = ' ';
  2119. mPadding[ 1 ] = ' ';
  2120. mPadding[ 2 ] = ' ';
  2121. mPadding[ 3 ] = ' ';
  2122. mPadding[ 4 ] = 0;
  2123. break;
  2124. case Namespace::Entry::BoolCallbackType:
  2125. mReturnType = "bool";
  2126. mPadding[ 0 ] = ' ';
  2127. mPadding[ 1 ] = ' ';
  2128. mPadding[ 2 ] = ' ';
  2129. mPadding[ 3 ] = ' ';
  2130. mPadding[ 4 ] = 0;
  2131. break;
  2132. }
  2133. }
  2134. }
  2135. };
  2136. }
  2137. DefineEngineMethod( SimObject, dump, void, ( bool detailed ), ( false ),
  2138. "Dump a description of all fields and methods defined on this object to the console.\n"
  2139. "@param detailed Whether to print detailed information about members." )
  2140. {
  2141. Con::printf( "Class: %s", object->getClassName() );
  2142. const AbstractClassRep::FieldList &list = object->getFieldList();
  2143. char expandedBuffer[4096];
  2144. Con::printf( "Static Fields:" );
  2145. Vector<const AbstractClassRep::Field *> flist(__FILE__, __LINE__);
  2146. for(U32 i = 0; i < list.size(); i++)
  2147. flist.push_back(&list[i]);
  2148. dQsort(flist.address(),flist.size(),sizeof(AbstractClassRep::Field *),compareFields);
  2149. for(Vector<const AbstractClassRep::Field *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  2150. {
  2151. const AbstractClassRep::Field* f = *itr;
  2152. // The special field types can be skipped.
  2153. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  2154. continue;
  2155. for(U32 j = 0; S32(j) < f->elementCount; j++)
  2156. {
  2157. // [neo, 07/05/2007 - #3000]
  2158. // Some objects use dummy vars and projected fields so make sure we call the get functions
  2159. //const char *val = Con::getData(f->type, (void *) (((const char *)object) + f->offset), j, f->table, f->flag);
  2160. const char *val = (*f->getDataFn)( object, Con::getData(f->type, (void *) (((const char *)object) + f->offset), j, f->table, f->flag) );// + typeSizes[fld.type] * array1));
  2161. ConsoleBaseType* conType = ConsoleBaseType::getType( f->type );
  2162. const char* conTypeName = "<unknown>";
  2163. if( conType )
  2164. conTypeName = conType->getTypeClassName();
  2165. if( !val /*|| !*val*/ )
  2166. continue;
  2167. if( f->elementCount == 1 )
  2168. dSprintf( expandedBuffer, sizeof( expandedBuffer ), " %s %s = \"", conTypeName, f->pFieldname );
  2169. else
  2170. dSprintf( expandedBuffer, sizeof( expandedBuffer ), " %s %s[ %d ] = \"", conTypeName, f->pFieldname, j );
  2171. expandEscape( expandedBuffer + dStrlen(expandedBuffer), val);
  2172. Con::printf( "%s\"", expandedBuffer );
  2173. if( detailed && f->pFieldDocs && f->pFieldDocs[ 0 ] )
  2174. Con::printf( " %s", f->pFieldDocs );
  2175. }
  2176. }
  2177. // If the object is a datablock with substitution statements,
  2178. // they get printed out as part of the dump.
  2179. if (dynamic_cast<SimDataBlock*>(object))
  2180. {
  2181. if (((SimDataBlock*)object)->getSubstitutionCount() > 0)
  2182. {
  2183. Con::printf("Substitution Fields:");
  2184. ((SimDataBlock*)object)->printSubstitutions();
  2185. }
  2186. }
  2187. Con::printf( "Dynamic Fields:" );
  2188. if(object->getFieldDictionary())
  2189. object->getFieldDictionary()->printFields(object);
  2190. Con::printf( "Methods:" );
  2191. Namespace *ns = object->getNamespace();
  2192. VectorPtr<Namespace::Entry *> vec(__FILE__, __LINE__);
  2193. if(ns)
  2194. ns->getEntryList(&vec);
  2195. bool sawCBs = false;
  2196. for(Vector<Namespace::Entry *>::iterator j = vec.begin(); j != vec.end(); j++)
  2197. {
  2198. Namespace::Entry *e = *j;
  2199. if(e->mType == Namespace::Entry::ScriptCallbackType)
  2200. sawCBs = true;
  2201. if(e->mType < 0)
  2202. continue;
  2203. DocString doc( e );
  2204. Con::printf( " %s%s%s%s", doc.mReturnType, doc.mPadding, e->mFunctionName, doc.mPrototype.c_str() );
  2205. if( detailed && !doc.mDescription.isEmpty() )
  2206. Con::printf( " %s", doc.mDescription.c_str() );
  2207. }
  2208. if( sawCBs )
  2209. {
  2210. Con::printf( "Callbacks:" );
  2211. for(Vector<Namespace::Entry *>::iterator j = vec.begin(); j != vec.end(); j++)
  2212. {
  2213. Namespace::Entry *e = *j;
  2214. if(e->mType != Namespace::Entry::ScriptCallbackType)
  2215. continue;
  2216. DocString doc( e );
  2217. Con::printf( " %s%s%s%s", doc.mReturnType, doc.mPadding, e->cb.mCallbackName, doc.mPrototype.c_str() );
  2218. if( detailed && !doc.mDescription.isEmpty() )
  2219. Con::printf( " %s", doc.mDescription.c_str() );
  2220. }
  2221. }
  2222. }
  2223. //-----------------------------------------------------------------------------
  2224. DefineEngineMethod( SimObject, save, bool, ( const char* fileName, bool selectedOnly, const char* preAppendString ), ( false, "" ),
  2225. "Save out the object to the given file.\n"
  2226. "@param fileName The name of the file to save to."
  2227. "@param selectedOnly If true, only objects marked as selected will be saved out.\n"
  2228. "@param preAppendString Text which will be preprended directly to the object serialization.\n"
  2229. "@param True on success, false on failure." )
  2230. {
  2231. return object->save( fileName, selectedOnly, preAppendString );
  2232. }
  2233. //-----------------------------------------------------------------------------
  2234. DefineEngineMethod( SimObject, setName, void, ( const char* newName ),,
  2235. "Set the global name of the object.\n"
  2236. "@param newName The new global name to assign to the object.\n"
  2237. "@note If name changing is disallowed on the object, the method will fail with a console error." )
  2238. {
  2239. object->assignName( newName );
  2240. }
  2241. //-----------------------------------------------------------------------------
  2242. DefineEngineMethod( SimObject, getName, const char*, (),,
  2243. "Get the global name of the object.\n"
  2244. "@return The global name assigned to the object." )
  2245. {
  2246. const char *ret = object->getName();
  2247. return ret ? ret : "";
  2248. }
  2249. //-----------------------------------------------------------------------------
  2250. DefineEngineMethod( SimObject, getClassName, const char*, (),,
  2251. "Get the name of the C++ class which the object is an instance of.\n"
  2252. "@return The name of the C++ class of the object." )
  2253. {
  2254. const char *ret = object->getClassName();
  2255. return ret ? ret : "";
  2256. }
  2257. //-----------------------------------------------------------------------------
  2258. DefineEngineMethod( SimObject, isField, bool, ( const char* fieldName ),,
  2259. "Test whether the given field is defined on this object.\n"
  2260. "@param fieldName The name of the field.\n"
  2261. "@return True if the object implements the given field." )
  2262. {
  2263. return object->isField( fieldName );
  2264. }
  2265. //-----------------------------------------------------------------------------
  2266. DefineEngineMethod( SimObject, getFieldValue, const char*, ( const char* fieldName, S32 index ), ( -1 ),
  2267. "Return the value of the given field on this object.\n"
  2268. "@param fieldName The name of the field. If it includes a field index, the index is parsed out.\n"
  2269. "@param index Optional parameter to specify the index of an array field separately.\n"
  2270. "@return The value of the given field or \"\" if undefined." )
  2271. {
  2272. const U32 nameLen = dStrlen( fieldName );
  2273. if (nameLen == 0)
  2274. return "";
  2275. char fieldNameBuffer[ 1024 ];
  2276. char arrayIndexBuffer[ 64 ];
  2277. // Parse out index if the field is given in the form of 'name[index]'.
  2278. const char* arrayIndex = NULL;
  2279. if( fieldName[ nameLen - 1 ] == ']' )
  2280. {
  2281. const char* leftBracket = dStrchr( fieldName, '[' );
  2282. const char* rightBracket = &fieldName[ nameLen - 1 ];
  2283. const U32 fieldNameLen = getMin( U32( leftBracket - fieldName ), sizeof( fieldNameBuffer ) - 1 );
  2284. const U32 arrayIndexLen = getMin( U32( rightBracket - leftBracket - 1 ), sizeof( arrayIndexBuffer ) - 1 );
  2285. dMemcpy( fieldNameBuffer, fieldName, fieldNameLen );
  2286. dMemcpy( arrayIndexBuffer, leftBracket + 1, arrayIndexLen );
  2287. fieldNameBuffer[ fieldNameLen ] = '\0';
  2288. arrayIndexBuffer[ arrayIndexLen ] = '\0';
  2289. fieldName = fieldNameBuffer;
  2290. arrayIndex = arrayIndexBuffer;
  2291. }
  2292. fieldName = StringTable->insert( fieldName );
  2293. if( index != -1 )
  2294. {
  2295. dSprintf( arrayIndexBuffer, sizeof( arrayIndexBuffer ), "%i", index );
  2296. arrayIndex = arrayIndexBuffer;
  2297. }
  2298. return object->getDataField( fieldName, arrayIndex );
  2299. }
  2300. //-----------------------------------------------------------------------------
  2301. DefineEngineMethod( SimObject, setFieldValue, bool, ( const char* fieldName, const char* value, S32 index ), ( -1 ),
  2302. "Set the value of the given field on this object.\n"
  2303. "@param fieldName The name of the field to assign to. If it includes an array index, the index will be parsed out.\n"
  2304. "@param value The new value to assign to the field.\n"
  2305. "@param index Optional argument to specify an index for an array field.\n"
  2306. "@return True." )
  2307. {
  2308. char fieldNameBuffer[ 1024 ];
  2309. char arrayIndexBuffer[ 64 ];
  2310. // Parse out index if the field is given in the form of 'name[index]'.
  2311. const char* arrayIndex = NULL;
  2312. const U32 nameLen = dStrlen( fieldName );
  2313. if( fieldName[ nameLen - 1 ] == ']' )
  2314. {
  2315. const char* leftBracket = dStrchr( fieldName, '[' );
  2316. const char* rightBracket = &fieldName[ nameLen - 1 ];
  2317. const U32 fieldNameLen = getMin( U32( leftBracket - fieldName ), sizeof( fieldNameBuffer ) - 1 );
  2318. const U32 arrayIndexLen = getMin( U32( rightBracket - leftBracket - 1 ), sizeof( arrayIndexBuffer ) - 1 );
  2319. dMemcpy( fieldNameBuffer, fieldName, fieldNameLen );
  2320. dMemcpy( arrayIndexBuffer, leftBracket + 1, arrayIndexLen );
  2321. fieldNameBuffer[ fieldNameLen ] = '\0';
  2322. arrayIndexBuffer[ arrayIndexLen ] = '\0';
  2323. fieldName = fieldNameBuffer;
  2324. arrayIndex = arrayIndexBuffer;
  2325. }
  2326. fieldName = StringTable->insert( fieldName );
  2327. if( index != -1 )
  2328. {
  2329. dSprintf( arrayIndexBuffer, sizeof( arrayIndexBuffer ), "%i", index );
  2330. arrayIndex = arrayIndexBuffer;
  2331. }
  2332. object->setDataField( fieldName, arrayIndex, value );
  2333. return true;
  2334. }
  2335. //-----------------------------------------------------------------------------
  2336. DefineEngineMethod( SimObject, getFieldType, const char*, ( const char* fieldName ),,
  2337. "Get the console type code of the given field.\n"
  2338. "@return The numeric type code for the underlying console type of the given field." )
  2339. {
  2340. U32 typeID = object->getDataFieldType( StringTable->insert( fieldName ), NULL );
  2341. ConsoleBaseType* type = ConsoleBaseType::getType( typeID );
  2342. if( type )
  2343. return type->getTypeName();
  2344. return "";
  2345. }
  2346. //-----------------------------------------------------------------------------
  2347. DefineEngineMethod( SimObject, setFieldType, void, ( const char* fieldName, const char* type ),,
  2348. "Set the console type code for the given field.\n"
  2349. "@param fieldName The name of the dynamic field to change to type for.\n"
  2350. "@param type The name of the console type.\n"
  2351. "@note This only works for dynamic fields. Types of static fields cannot be changed." )
  2352. {
  2353. object->setDataFieldType( type, StringTable->insert( fieldName ), NULL );
  2354. }
  2355. //-----------------------------------------------------------------------------
  2356. DefineEngineStringlyVariadicMethod( SimObject, call, const char*, 3, 0, "( string method, string args... ) Dynamically call a method on an object.\n"
  2357. "@param method Name of method to call.\n"
  2358. "@param args Zero or more arguments for the method.\n"
  2359. "@return The result of the method call." )
  2360. {
  2361. argv[1].setString(argv[2].getString());
  2362. ConsoleValue returnValue = Con::execute(object, argc - 1, argv + 1);
  2363. return Con::getReturnBuffer(returnValue.getString());
  2364. }
  2365. //-----------------------------------------------------------------------------
  2366. DefineEngineMethod( SimObject, setInternalName, void, ( const char* newInternalName ),,
  2367. "Set the internal name of the object.\n"
  2368. "@param newInternalName The new internal name for the object." )
  2369. {
  2370. object->setInternalName( newInternalName );
  2371. }
  2372. //-----------------------------------------------------------------------------
  2373. DefineEngineMethod( SimObject, getInternalName, const char*, (),,
  2374. "Get the internal name of the object.\n"
  2375. "@return The internal name of the object." )
  2376. {
  2377. return object->getInternalName();
  2378. }
  2379. //-----------------------------------------------------------------------------
  2380. DefineEngineMethod( SimObject, dumpClassHierarchy, void, (),,
  2381. "Dump the native C++ class hierarchy of this object's C++ class to the console." )
  2382. {
  2383. object->dumpClassHierarchy();
  2384. }
  2385. //-----------------------------------------------------------------------------
  2386. DefineEngineMethod( SimObject, isMemberOfClass, bool, ( const char* className ),,
  2387. "Test whether this object is a member of the specified class.\n"
  2388. "@param className Name of a native C++ class.\n"
  2389. "@return True if this object is an instance of the given C++ class or any of its super classes." )
  2390. {
  2391. AbstractClassRep* pRep = object->getClassRep();
  2392. while(pRep)
  2393. {
  2394. if( !dStricmp(pRep->getClassName(), className ) )
  2395. {
  2396. //matches
  2397. return true;
  2398. }
  2399. pRep = pRep->getParentClass();
  2400. }
  2401. return false;
  2402. }
  2403. //-----------------------------------------------------------------------------
  2404. DefineEngineMethod( SimObject, isInNamespaceHierarchy, bool, ( const char* name ),,
  2405. "Test whether the namespace of this object is a direct or indirect child to the given namespace.\n"
  2406. "@param name The name of a namespace.\n"
  2407. "@return True if the given namespace name is within the namespace hierarchy of this object." )
  2408. {
  2409. Namespace* nspace = object->getNamespace();
  2410. while( nspace && dStricmp( nspace->mName, name ) != 0 )
  2411. nspace = nspace->mParent;
  2412. return ( nspace != NULL );
  2413. }
  2414. //-----------------------------------------------------------------------------
  2415. DefineEngineMethod( SimObject, getId, S32, (),,
  2416. "Get the underlying unique numeric ID of the object.\n"
  2417. "@note Object IDs are unique only during single engine runs.\n"
  2418. "@return The unique numeric ID of the object." )
  2419. {
  2420. return object->getId();
  2421. }
  2422. //-----------------------------------------------------------------------------
  2423. DefineEngineMethod( SimObject, getGroup, SimGroup*, (),,
  2424. "Get the group that this object is contained in.\n"
  2425. "@note If not assigned to particular SimGroup, an object belongs to RootGroup.\n"
  2426. "@return The SimGroup object to which the object belongs." )
  2427. {
  2428. return object->getGroup();
  2429. }
  2430. //-----------------------------------------------------------------------------
  2431. DefineEngineMethod( SimObject, delete, void, (),,
  2432. "Delete and remove the object." )
  2433. {
  2434. object->deleteObject();
  2435. }
  2436. //-----------------------------------------------------------------------------
  2437. DefineEngineStringlyVariadicMethod( SimObject,schedule, S32, 4, 0, "( float time, string method, string args... ) Delay an invocation of a method.\n"
  2438. "@param time The number of milliseconds after which to invoke the method. This is a soft limit.\n"
  2439. "@param method The method to call.\n"
  2440. "@param args The arguments with which to call the method.\n"
  2441. "@return The numeric ID of the created schedule. Can be used to cancel the call.\n" )
  2442. {
  2443. U32 timeDelta = U32(argv[2].getFloat());
  2444. argv[2].setString(argv[3].getString());
  2445. argv[3].setString(argv[1].getString());
  2446. SimConsoleEvent *evt = new SimConsoleEvent(argc - 2, argv + 2, true);
  2447. S32 ret = Sim::postEvent(object, evt, Sim::getCurrentTime() + timeDelta);
  2448. // #ifdef DEBUG
  2449. // Con::printf("obj %s schedule(%s) = %d", argv[3], argv[2], ret);
  2450. // Con::executef( "backtrace");
  2451. // #endif
  2452. return ret;
  2453. }
  2454. //-----------------------------------------------------------------------------
  2455. DefineEngineMethod( SimObject, getDynamicFieldCount, S32, (),,
  2456. "Get the number of dynamic fields defined on the object.\n"
  2457. "@return The number of dynamic fields defined on the object." )
  2458. {
  2459. S32 count = 0;
  2460. SimFieldDictionary* fieldDictionary = object->getFieldDictionary();
  2461. for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
  2462. count++;
  2463. return count;
  2464. }
  2465. //-----------------------------------------------------------------------------
  2466. DefineEngineMethod( SimObject, getDynamicField, const char*, ( S32 index ),,
  2467. "Get a value of a dynamic field by index.\n"
  2468. "@param index The index of the dynamic field.\n"
  2469. "@return The value of the dynamic field at the given index or \"\"." )
  2470. {
  2471. SimFieldDictionary* fieldDictionary = object->getFieldDictionary();
  2472. SimFieldDictionaryIterator itr(fieldDictionary);
  2473. for (S32 i = 0; i < index; i++)
  2474. {
  2475. if (!(*itr))
  2476. {
  2477. Con::warnf("Invalid dynamic field index passed to SimObject::getDynamicField!");
  2478. return NULL;
  2479. }
  2480. ++itr;
  2481. }
  2482. static const U32 bufSize = 256;
  2483. char* buffer = Con::getReturnBuffer(bufSize);
  2484. if (*itr)
  2485. {
  2486. SimFieldDictionary::Entry* entry = *itr;
  2487. dSprintf(buffer, bufSize, "%s\t%s", entry->slotName, entry->value);
  2488. return buffer;
  2489. }
  2490. Con::warnf("Invalid dynamic field index passed to SimObject::getDynamicField!");
  2491. return NULL;
  2492. }
  2493. //-----------------------------------------------------------------------------
  2494. DefineEngineMethod( SimObject, getFieldCount, S32, (),,
  2495. "Get the number of static fields on the object.\n"
  2496. "@return The number of static fields defined on the object." )
  2497. {
  2498. const AbstractClassRep::FieldList &list = object->getFieldList();
  2499. const AbstractClassRep::Field* f;
  2500. U32 numDummyEntries = 0;
  2501. for(S32 i = 0; i < list.size(); i++)
  2502. {
  2503. f = &list[i];
  2504. // The special field types do not need to be counted.
  2505. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  2506. numDummyEntries++;
  2507. }
  2508. return list.size() - numDummyEntries;
  2509. }
  2510. //-----------------------------------------------------------------------------
  2511. DefineEngineMethod( SimObject, getField, const char*, ( S32 index ),,
  2512. "Retrieve the value of a static field by index.\n"
  2513. "@param index The index of the static field.\n"
  2514. "@return The value of the static field with the given index or \"\"." )
  2515. {
  2516. const AbstractClassRep::FieldList &list = object->getFieldList();
  2517. if( ( index < 0 ) || ( index >= list.size() ) )
  2518. return "";
  2519. const AbstractClassRep::Field* f;
  2520. S32 currentField = 0;
  2521. for ( U32 i = 0; i < list.size() && currentField <= index; i++ )
  2522. {
  2523. f = &list[i];
  2524. // The special field types can be skipped.
  2525. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  2526. continue;
  2527. if(currentField == index)
  2528. return f->pFieldname;
  2529. currentField++;
  2530. }
  2531. // if we found nada, return nada.
  2532. return "";
  2533. }
  2534. DefineEngineFunction(getClassHierarchy, const char*, (const char* name), ,
  2535. "Returns the inheritance hierarchy for a given class.")
  2536. {
  2537. AbstractClassRep* pRep = AbstractClassRep::findClassRep(name);
  2538. if (!pRep)
  2539. {
  2540. //Con::errorf("%s does not exist", name);
  2541. return StringTable->EmptyString();
  2542. }
  2543. StringBuilder buffer;
  2544. while (pRep != NULL)
  2545. {
  2546. StringTableEntry className = pRep->getClassName();
  2547. buffer.append(className);
  2548. buffer.append(" ");
  2549. pRep = pRep->getParentClass();
  2550. }
  2551. String result = buffer.end().trim();
  2552. //Con::printf("getClassHierarchy for %s=%s", name, result.c_str());
  2553. return Con::getReturnBuffer(result.c_str());
  2554. }
  2555. //-----------------------------------------------------------------------------
  2556. #ifdef TORQUE_DEBUG
  2557. DefineEngineMethod( SimObject, getDebugInfo, ArrayObject*, (),,
  2558. "Return some behind-the-scenes information on the object.\n"
  2559. "@return An ArrayObject filled with internal information about the object." )
  2560. {
  2561. ArrayObject* array = new ArrayObject();
  2562. array->registerObject();
  2563. array->push_back( "C++|Address", String::ToString( "0x%x", object ) );
  2564. array->push_back( "C++|Size", String::ToString( object->getClassRep()->getSizeof() ) );
  2565. array->push_back( "Object|Description", object->describeSelf() );
  2566. array->push_back( "Object|FileName", object->getFilename() );
  2567. array->push_back( "Object|DeclarationLine", String::ToString( object->getDeclarationLine() ) );
  2568. array->push_back( "Object|CopySource", object->getCopySource() ?
  2569. String::ToString( "%i:%s (%s)", object->getCopySource()->getId(), object->getCopySource()->getClassName(), object->getCopySource()->getName() ) : "" );
  2570. array->push_back( "Flag|EditorOnly", object->isEditorOnly() ? "true" : "false" );
  2571. array->push_back( "Flag|NameChangeAllowed", object->isNameChangeAllowed() ? "true" : "false" );
  2572. array->push_back( "Flag|AutoDelete", object->isAutoDeleted() ? "true" : "false" );
  2573. array->push_back( "Flag|Selected", object->isSelected() ? "true" : "false" );
  2574. array->push_back( "Flag|Expanded", object->isExpanded() ? "true" : "false" );
  2575. array->push_back( "Flag|ModStaticFields", object->canModStaticFields() ? "true" : "false" );
  2576. array->push_back( "Flag|ModDynamicFields", object->canModDynamicFields() ? "true" : "false" );
  2577. array->push_back( "Flag|CanSave", object->getCanSave() ? "true" : "false" );
  2578. #ifndef TORQUE_DISABLE_MEMORY_MANAGER
  2579. Memory::Info memInfo;
  2580. Memory::getMemoryInfo( object, memInfo );
  2581. array->push_back( "Memory|AllocNumber", String::ToString( memInfo.mAllocNumber ) );
  2582. array->push_back( "Memory|AllocSize", String::ToString( memInfo.mAllocSize ) );
  2583. array->push_back( "Memory|AllocFile", memInfo.mFileName );
  2584. array->push_back( "Memory|AllocLine", String::ToString( memInfo.mLineNumber ) );
  2585. array->push_back( "Memory|IsGlobal", memInfo.mIsGlobal ? "true" : "false" );
  2586. array->push_back( "Memory|IsStatic", memInfo.mIsStatic ? "true" : "false" );
  2587. #endif
  2588. return array;
  2589. }
  2590. #endif