other.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #pragma once
  2. // Thanks hatf0
  3. // https://github.com/hatf0/TraceEnabler
  4. // ripped out structs from Tork
  5. typedef unsigned int U32;
  6. typedef float F32;
  7. typedef signed int S32;
  8. typedef const char* StringTableEntry;
  9. typedef const char* (*StringCallback)(DWORD* obj, int argc, const char* argv[]);
  10. typedef int(*IntCallback)(DWORD* obj, int argc, const char* argv[]);
  11. typedef float(*FloatCallback)(DWORD* obj, int argc, const char* argv[]);
  12. typedef void(*VoidCallback)(DWORD* obj, int argc, const char* argv[]);
  13. typedef bool(*BoolCallback)(DWORD* obj, int argc, const char* argv[]);
  14. struct Namespace
  15. {
  16. const char* mName;
  17. const char* mPackage;
  18. Namespace* mParent;
  19. Namespace* mNext;
  20. void* mClassRep;
  21. U32 mRefCountToParent;
  22. struct Entry
  23. {
  24. enum
  25. {
  26. GroupMarker = -3,
  27. OverloadMarker = -2,
  28. InvalidFunctionType = -1,
  29. ScriptFunctionType,
  30. StringCallbackType,
  31. IntCallbackType,
  32. FloatCallbackType,
  33. VoidCallbackType,
  34. BoolCallbackType
  35. };
  36. Namespace* mNamespace;
  37. //char _padding1[4];
  38. Entry* mNext;
  39. const char* mFunctionName;
  40. S32 mType;
  41. S32 mMinArgs;
  42. S32 mMaxArgs;
  43. const char* mUsage;
  44. const char* mPackage;
  45. void* mCode; // CodeBlock *mCode;
  46. U32 mFunctionOffset;
  47. union {
  48. StringCallback mStringCallbackFunc;
  49. IntCallback mIntCallbackFunc;
  50. VoidCallback mVoidCallbackFunc;
  51. FloatCallback mFloatCallbackFunc;
  52. BoolCallback mBoolCallbackFunc;
  53. const char* mGroupName;
  54. } cb;
  55. };
  56. Entry* mEntryList;
  57. Entry** mHashTable;
  58. U32 mHashSize;
  59. U32 mHashSequence; ///< @note The hash sequence is used by the autodoc console facility
  60. /// as a means of testing reference state.
  61. char* lastUsage;
  62. };
  63. //static Namespace* mGlobalNamespace;
  64. struct CodeBlock
  65. {
  66. const char* name;
  67. int unk_1;
  68. void* globalStrings;
  69. void* functionStrings;
  70. void* globalFloats;
  71. void* functionFloats;
  72. int codeSize;
  73. int unk_2;
  74. char* code;
  75. int refCount;
  76. int lineBreakPairCount;
  77. int* lineBreakPairs;
  78. int breakListSize;
  79. int* breakList;
  80. CodeBlock* nextFile;
  81. const char* mRoot;
  82. };
  83. struct Dictionary
  84. {
  85. struct Entry {
  86. enum
  87. {
  88. TypeInternalInt = -3,
  89. TypeInternalFloat = -2,
  90. TypeInternalString = -1,
  91. };
  92. const char* name;
  93. Entry* next;
  94. S32 type;
  95. char* sval;
  96. U32 ival; // doubles as strlen
  97. F32 fval;
  98. U32 bufferLen;
  99. void* dataPtr;
  100. };
  101. struct HashTableData
  102. {
  103. int size;
  104. int count;
  105. Entry** data;
  106. void* owner;
  107. };
  108. HashTableData table;
  109. const char* scopeName;
  110. const char* scopeNamespace;
  111. CodeBlock* block;
  112. int ip;
  113. };
  114. struct ExprEvalState
  115. {
  116. void* thisObject;
  117. Dictionary::Entry* currentVariable;
  118. bool traceOn;
  119. Dictionary globalVars;
  120. Vector<Dictionary> stack;
  121. };
  122. //static ExprEvalState* gEvalState;
  123. struct DataChunker
  124. {
  125. struct DataBlock
  126. {
  127. DataBlock* next;
  128. char* data;
  129. int curIndex;
  130. };
  131. DataBlock* curBlock;
  132. signed int chunkSize;
  133. };
  134. struct StringStack
  135. {
  136. char* mBuffer;
  137. int mBufferSize;
  138. const char* mArgv[20];
  139. int mFrameOffsets[1024];
  140. int mStartOffsets[1024];
  141. int mNumFrames;
  142. int mArgc;
  143. int mStart;
  144. int mLen;
  145. int mStartStackSize;
  146. int mFunctionOffset;
  147. int mArgBufferSize;
  148. char* mArgBuffer;
  149. };
  150. struct StringTable {
  151. struct Node {
  152. char* val;
  153. Node* next;
  154. };
  155. Node** buckets;
  156. U32 numBuckets;
  157. U32 itemCount;
  158. DataChunker memPool;
  159. };
  160. //New types not from TraceEnabler
  161. struct Notify
  162. {
  163. enum Type
  164. {
  165. ClearNotify, ///< Notified when the object is cleared.
  166. DeleteNotify, ///< Notified when the object is deleted.
  167. ObjectRef, ///< Cleverness to allow tracking of references.
  168. Invalid ///< Mark this notification as unused (used in freeNotify).
  169. } type;
  170. void* ptr; ///< Data (typically referencing or interested object).
  171. Notify* next; ///< Next notification in the linked list.
  172. };
  173. //Implementation of a Point3F class and Torque's MatrixF
  174. //Borrowed from Valcle's VehicleWalking and touched up a little
  175. class Point3F
  176. {
  177. public:
  178. float pX, pY, pZ;
  179. Point3F(float x, float y, float z)
  180. {
  181. pX = x;
  182. pY = y;
  183. pZ = z;
  184. }
  185. Point3F() {};
  186. Point3F operator+(Point3F& other)
  187. {
  188. return Point3F(pX + other[0],
  189. pY + other[1],
  190. pZ + other[2]);
  191. }
  192. Point3F operator+(Point3F&& other)
  193. {
  194. return operator+(other);
  195. }
  196. Point3F operator-(Point3F& other)
  197. {
  198. return Point3F(pX - other[0],
  199. pY - other[1],
  200. pZ - other[2]);
  201. }
  202. Point3F operator-(Point3F&& other)
  203. {
  204. return operator-(other);
  205. }
  206. Point3F operator*(float fScalar)
  207. {
  208. return Point3F(pX * fScalar,
  209. pY * fScalar,
  210. pZ * fScalar);
  211. }
  212. Point3F operator/(float fScalar)
  213. {
  214. return Point3F(pX / fScalar,
  215. pY / fScalar,
  216. pZ / fScalar);
  217. }
  218. Point3F ptNorm()
  219. {
  220. float fLn = fLen();
  221. return Point3F(pX / fLn,
  222. pY / fLn,
  223. pZ / fLn);
  224. }
  225. float& operator[](unsigned int i)
  226. {
  227. if(i == 0)
  228. return pX;
  229. if(i == 1)
  230. return pY;
  231. return pZ;
  232. }
  233. float fLen()
  234. {
  235. return sqrtf((pX * pX) +
  236. (pY * pY) +
  237. (pZ * pZ));
  238. }
  239. };
  240. class MatrixF
  241. {
  242. public:
  243. float fM[16];
  244. Point3F ptGetPosition()
  245. {
  246. return Point3F(fM[3], fM[7], fM[11]);
  247. }
  248. Point3F ptGetForwardVector()
  249. {
  250. return Point3F(fM[1], fM[5], fM[9]);
  251. }
  252. void GetColumn(int iCol, Point3F* ptRet)
  253. {
  254. ptRet[0][0] = fM[iCol];
  255. ptRet[0][1] = fM[iCol + 4];
  256. ptRet[0][2] = fM[iCol + 8];
  257. }
  258. void SetColumn(int iCol, Point3F* ptSet)
  259. {
  260. fM[iCol] = ptSet[0][0];
  261. fM[iCol + 4] = ptSet[0][1];
  262. fM[iCol + 8] = ptSet[0][2];
  263. }
  264. float Determinate()
  265. {
  266. return fM[0] * (fM[5] * fM[10] - fM[6] * fM[9]) +
  267. fM[4] * (fM[2] * fM[9] - fM[1] * fM[10]) +
  268. fM[8] * (fM[1] * fM[6] - fM[2] * fM[5]);
  269. }
  270. void Invert()
  271. {
  272. float inv[16], det;
  273. int i;
  274. inv[0] = fM[5] * fM[10] * fM[15] -
  275. fM[5] * fM[11] * fM[14] -
  276. fM[9] * fM[6] * fM[15] +
  277. fM[9] * fM[7] * fM[14] +
  278. fM[13] * fM[6] * fM[11] -
  279. fM[13] * fM[7] * fM[10];
  280. inv[4] = -fM[4] * fM[10] * fM[15] +
  281. fM[4] * fM[11] * fM[14] +
  282. fM[8] * fM[6] * fM[15] -
  283. fM[8] * fM[7] * fM[14] -
  284. fM[12] * fM[6] * fM[11] +
  285. fM[12] * fM[7] * fM[10];
  286. inv[8] = fM[4] * fM[9] * fM[15] -
  287. fM[4] * fM[11] * fM[13] -
  288. fM[8] * fM[5] * fM[15] +
  289. fM[8] * fM[7] * fM[13] +
  290. fM[12] * fM[5] * fM[11] -
  291. fM[12] * fM[7] * fM[9];
  292. inv[12] = -fM[4] * fM[9] * fM[14] +
  293. fM[4] * fM[10] * fM[13] +
  294. fM[8] * fM[5] * fM[14] -
  295. fM[8] * fM[6] * fM[13] -
  296. fM[12] * fM[5] * fM[10] +
  297. fM[12] * fM[6] * fM[9];
  298. inv[1] = -fM[1] * fM[10] * fM[15] +
  299. fM[1] * fM[11] * fM[14] +
  300. fM[9] * fM[2] * fM[15] -
  301. fM[9] * fM[3] * fM[14] -
  302. fM[13] * fM[2] * fM[11] +
  303. fM[13] * fM[3] * fM[10];
  304. inv[5] = fM[0] * fM[10] * fM[15] -
  305. fM[0] * fM[11] * fM[14] -
  306. fM[8] * fM[2] * fM[15] +
  307. fM[8] * fM[3] * fM[14] +
  308. fM[12] * fM[2] * fM[11] -
  309. fM[12] * fM[3] * fM[10];
  310. inv[9] = -fM[0] * fM[9] * fM[15] +
  311. fM[0] * fM[11] * fM[13] +
  312. fM[8] * fM[1] * fM[15] -
  313. fM[8] * fM[3] * fM[13] -
  314. fM[12] * fM[1] * fM[11] +
  315. fM[12] * fM[3] * fM[9];
  316. inv[13] = fM[0] * fM[9] * fM[14] -
  317. fM[0] * fM[10] * fM[13] -
  318. fM[8] * fM[1] * fM[14] +
  319. fM[8] * fM[2] * fM[13] +
  320. fM[12] * fM[1] * fM[10] -
  321. fM[12] * fM[2] * fM[9];
  322. inv[2] = fM[1] * fM[6] * fM[15] -
  323. fM[1] * fM[7] * fM[14] -
  324. fM[5] * fM[2] * fM[15] +
  325. fM[5] * fM[3] * fM[14] +
  326. fM[13] * fM[2] * fM[7] -
  327. fM[13] * fM[3] * fM[6];
  328. inv[6] = -fM[0] * fM[6] * fM[15] +
  329. fM[0] * fM[7] * fM[14] +
  330. fM[4] * fM[2] * fM[15] -
  331. fM[4] * fM[3] * fM[14] -
  332. fM[12] * fM[2] * fM[7] +
  333. fM[12] * fM[3] * fM[6];
  334. inv[10] = fM[0] * fM[5] * fM[15] -
  335. fM[0] * fM[7] * fM[13] -
  336. fM[4] * fM[1] * fM[15] +
  337. fM[4] * fM[3] * fM[13] +
  338. fM[12] * fM[1] * fM[7] -
  339. fM[12] * fM[3] * fM[5];
  340. inv[14] = -fM[0] * fM[5] * fM[14] +
  341. fM[0] * fM[6] * fM[13] +
  342. fM[4] * fM[1] * fM[14] -
  343. fM[4] * fM[2] * fM[13] -
  344. fM[12] * fM[1] * fM[6] +
  345. fM[12] * fM[2] * fM[5];
  346. inv[3] = -fM[1] * fM[6] * fM[11] +
  347. fM[1] * fM[7] * fM[10] +
  348. fM[5] * fM[2] * fM[11] -
  349. fM[5] * fM[3] * fM[10] -
  350. fM[9] * fM[2] * fM[7] +
  351. fM[9] * fM[3] * fM[6];
  352. inv[7] = fM[0] * fM[6] * fM[11] -
  353. fM[0] * fM[7] * fM[10] -
  354. fM[4] * fM[2] * fM[11] +
  355. fM[4] * fM[3] * fM[10] +
  356. fM[8] * fM[2] * fM[7] -
  357. fM[8] * fM[3] * fM[6];
  358. inv[11] = -fM[0] * fM[5] * fM[11] +
  359. fM[0] * fM[7] * fM[9] +
  360. fM[4] * fM[1] * fM[11] -
  361. fM[4] * fM[3] * fM[9] -
  362. fM[8] * fM[1] * fM[7] +
  363. fM[8] * fM[3] * fM[5];
  364. inv[15] = fM[0] * fM[5] * fM[10] -
  365. fM[0] * fM[6] * fM[9] -
  366. fM[4] * fM[1] * fM[10] +
  367. fM[4] * fM[2] * fM[9] +
  368. fM[8] * fM[1] * fM[6] -
  369. fM[8] * fM[2] * fM[5];
  370. det = fM[0] * inv[0] + fM[1] * inv[4] + fM[2] * inv[8] + fM[3] * inv[12];
  371. det = 1.0f / det;
  372. for (i = 0; i < 16; i++)
  373. fM[i] = inv[i] * det;
  374. }
  375. void MulVector(Point3F& ptVec, Point3F* ptOut)
  376. {
  377. ptOut[0][0] = fM[0] * ptVec[0] + fM[1] * ptVec[1] + fM[2] * ptVec[2];
  378. ptOut[0][1] = fM[4] * ptVec[0] + fM[5] * ptVec[1] + fM[6] * ptVec[2];
  379. ptOut[0][2] = fM[8] * ptVec[0] + fM[9] * ptVec[1] + fM[10] * ptVec[2];
  380. }
  381. void MulPoint(Point3F& ptVec, Point3F* ptOut)
  382. {
  383. ptOut[0][0] = fM[0] * ptVec[0] + fM[1] * ptVec[1] + fM[2] * ptVec[2] + fM[3];
  384. ptOut[0][1] = fM[4] * ptVec[0] + fM[5] * ptVec[1] + fM[6] * ptVec[2] + fM[7];
  385. ptOut[0][2] = fM[8] * ptVec[0] + fM[9] * ptVec[1] + fM[10] * ptVec[2] + fM[11];
  386. }
  387. };