PageRenderTime 58ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/lscript/lscript_library.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 427 lines | 365 code | 35 blank | 27 comment | 53 complexity | c82908a3bba2507ba111c25228bc3f9f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lscript_library.h
  3. * @brief External library interface
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LSCRIPT_LIBRARY_H
  27. #define LL_LSCRIPT_LIBRARY_H
  28. #include "lscript_byteformat.h"
  29. #include "v3math.h"
  30. #include "llquaternion.h"
  31. #include "lluuid.h"
  32. #include "lscript_byteconvert.h"
  33. class LLScriptLibData;
  34. class LLScriptLibraryFunction
  35. {
  36. public:
  37. LLScriptLibraryFunction(F32 eu, F32 st, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &), const char *name, const char *ret_type, const char *args, BOOL god_only = FALSE);
  38. ~LLScriptLibraryFunction();
  39. F32 mEnergyUse;
  40. F32 mSleepTime;
  41. void (*mExecFunc)(LLScriptLibData *, LLScriptLibData *, const LLUUID &);
  42. const char *mName;
  43. const char *mReturnType;
  44. const char *mArgs;
  45. BOOL mGodOnly;
  46. };
  47. class LLScriptLibrary
  48. {
  49. public:
  50. LLScriptLibrary();
  51. ~LLScriptLibrary();
  52. void init();
  53. void addFunction(F32 eu, F32 st, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &), const char *name, const char *ret_type, const char *args, BOOL god_only = FALSE);
  54. void assignExec(const char *name, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &));
  55. std::vector<LLScriptLibraryFunction> mFunctions;
  56. };
  57. class LLScriptLibData
  58. {
  59. public:
  60. // TODO: Change this to a union
  61. LSCRIPTType mType;
  62. S32 mInteger;
  63. F32 mFP;
  64. char *mKey;
  65. char *mString;
  66. LLVector3 mVec;
  67. LLQuaternion mQuat;
  68. LLScriptLibData *mListp;
  69. friend bool operator<=(const LLScriptLibData &a, const LLScriptLibData &b)
  70. {
  71. if (a.mType == b.mType)
  72. {
  73. if (a.mType == LST_INTEGER)
  74. {
  75. return a.mInteger <= b.mInteger;
  76. }
  77. if (a.mType == LST_FLOATINGPOINT)
  78. {
  79. return a.mFP <= b.mFP;
  80. }
  81. if (a.mType == LST_STRING)
  82. {
  83. return strcmp(a.mString, b.mString) <= 0;
  84. }
  85. if (a.mType == LST_KEY)
  86. {
  87. return strcmp(a.mKey, b.mKey) <= 0;
  88. }
  89. if (a.mType == LST_VECTOR)
  90. {
  91. return a.mVec.magVecSquared() <= b.mVec.magVecSquared();
  92. }
  93. }
  94. return TRUE;
  95. }
  96. friend bool operator==(const LLScriptLibData &a, const LLScriptLibData &b)
  97. {
  98. if (a.mType == b.mType)
  99. {
  100. if (a.mType == LST_INTEGER)
  101. {
  102. return a.mInteger == b.mInteger;
  103. }
  104. if (a.mType == LST_FLOATINGPOINT)
  105. {
  106. return a.mFP == b.mFP;
  107. }
  108. if (a.mType == LST_STRING)
  109. {
  110. return !strcmp(a.mString, b.mString);
  111. }
  112. if (a.mType == LST_KEY)
  113. {
  114. return !strcmp(a.mKey, b.mKey);
  115. }
  116. if (a.mType == LST_VECTOR)
  117. {
  118. return a.mVec == b.mVec;
  119. }
  120. if (a.mType == LST_QUATERNION)
  121. {
  122. return a.mQuat == b.mQuat;
  123. }
  124. }
  125. return FALSE;
  126. }
  127. S32 getListLength() const
  128. {
  129. const LLScriptLibData *data = this;
  130. S32 retval = 0;
  131. while (data->mListp)
  132. {
  133. retval++;
  134. data = data->mListp;
  135. }
  136. return retval;
  137. }
  138. BOOL checkForMultipleLists()
  139. {
  140. LLScriptLibData *data = this;
  141. while (data->mListp)
  142. {
  143. data = data->mListp;
  144. if (data->mType == LST_LIST)
  145. return TRUE;
  146. }
  147. return FALSE;
  148. }
  149. S32 getSavedSize()
  150. {
  151. S32 size = 0;
  152. // mType
  153. size += 4;
  154. switch(mType)
  155. {
  156. case LST_INTEGER:
  157. size += 4;
  158. break;
  159. case LST_FLOATINGPOINT:
  160. size += 4;
  161. break;
  162. case LST_KEY:
  163. size += (S32)strlen(mKey) + 1; /*Flawfinder: ignore*/
  164. break;
  165. case LST_STRING:
  166. size += (S32)strlen(mString) + 1; /*Flawfinder: ignore*/
  167. break;
  168. case LST_LIST:
  169. break;
  170. case LST_VECTOR:
  171. size += 12;
  172. break;
  173. case LST_QUATERNION:
  174. size += 16;
  175. break;
  176. default:
  177. break;
  178. }
  179. return size;
  180. }
  181. S32 write2bytestream(U8 *dest)
  182. {
  183. S32 offset = 0;
  184. integer2bytestream(dest, offset, mType);
  185. switch(mType)
  186. {
  187. case LST_INTEGER:
  188. integer2bytestream(dest, offset, mInteger);
  189. break;
  190. case LST_FLOATINGPOINT:
  191. float2bytestream(dest, offset, mFP);
  192. break;
  193. case LST_KEY:
  194. char2bytestream(dest, offset, mKey);
  195. break;
  196. case LST_STRING:
  197. char2bytestream(dest, offset, mString);
  198. break;
  199. case LST_LIST:
  200. break;
  201. case LST_VECTOR:
  202. vector2bytestream(dest, offset, mVec);
  203. break;
  204. case LST_QUATERNION:
  205. quaternion2bytestream(dest, offset, mQuat);
  206. break;
  207. default:
  208. break;
  209. }
  210. return offset;
  211. }
  212. LLScriptLibData() : mType(LST_NULL), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
  213. {
  214. }
  215. LLScriptLibData(const LLScriptLibData &data) : mType(data.mType), mInteger(data.mInteger), mFP(data.mFP), mKey(NULL), mString(NULL), mVec(data.mVec), mQuat(data.mQuat), mListp(NULL)
  216. {
  217. if (data.mKey)
  218. {
  219. mKey = new char[strlen(data.mKey) + 1]; /* Flawfinder: ignore */
  220. if (mKey == NULL)
  221. {
  222. llerrs << "Memory Allocation Failed" << llendl;
  223. return;
  224. }
  225. strcpy(mKey, data.mKey); /* Flawfinder: ignore */
  226. }
  227. if (data.mString)
  228. {
  229. mString = new char[strlen(data.mString) + 1]; /* Flawfinder: ignore */
  230. if (mString == NULL)
  231. {
  232. llerrs << "Memory Allocation Failed" << llendl;
  233. return;
  234. }
  235. strcpy(mString, data.mString); /* Flawfinder: ignore */
  236. }
  237. }
  238. LLScriptLibData(U8 *src, S32 &offset) : mListp(NULL)
  239. {
  240. static char temp[TOP_OF_MEMORY]; /* Flawfinder: ignore */
  241. mType = (LSCRIPTType)bytestream2integer(src, offset);
  242. switch(mType)
  243. {
  244. case LST_INTEGER:
  245. mInteger = bytestream2integer(src, offset);
  246. break;
  247. case LST_FLOATINGPOINT:
  248. mFP = bytestream2float(src, offset);
  249. break;
  250. case LST_KEY:
  251. {
  252. bytestream2char(temp, src, offset, sizeof(temp));
  253. mKey = new char[strlen(temp) + 1]; /* Flawfinder: ignore */
  254. if (mKey == NULL)
  255. {
  256. llerrs << "Memory Allocation Failed" << llendl;
  257. return;
  258. }
  259. strcpy(mKey, temp); /* Flawfinder: ignore */
  260. }
  261. break;
  262. case LST_STRING:
  263. {
  264. bytestream2char(temp, src, offset, sizeof(temp));
  265. mString = new char[strlen(temp) + 1]; /* Flawfinder: ignore */
  266. if (mString == NULL)
  267. {
  268. llerrs << "Memory Allocation Failed" << llendl;
  269. return;
  270. }
  271. strcpy(mString, temp); /* Flawfinder: ignore */
  272. }
  273. break;
  274. case LST_LIST:
  275. break;
  276. case LST_VECTOR:
  277. bytestream2vector(mVec, src, offset);
  278. break;
  279. case LST_QUATERNION:
  280. bytestream2quaternion(mQuat, src, offset);
  281. break;
  282. default:
  283. break;
  284. }
  285. }
  286. void set(U8 *src, S32 &offset)
  287. {
  288. static char temp[TOP_OF_MEMORY]; /* Flawfinder: ignore */
  289. mType = (LSCRIPTType)bytestream2integer(src, offset);
  290. switch(mType)
  291. {
  292. case LST_INTEGER:
  293. mInteger = bytestream2integer(src, offset);
  294. break;
  295. case LST_FLOATINGPOINT:
  296. mFP = bytestream2float(src, offset);
  297. break;
  298. case LST_KEY:
  299. {
  300. bytestream2char(temp, src, offset, sizeof(temp));
  301. mKey = new char[strlen(temp) + 1]; /* Flawfinder: ignore */
  302. if (mKey == NULL)
  303. {
  304. llerrs << "Memory Allocation Failed" << llendl;
  305. return;
  306. }
  307. strcpy(mKey, temp); /* Flawfinder: ignore */
  308. }
  309. break;
  310. case LST_STRING:
  311. {
  312. bytestream2char(temp, src, offset, sizeof(temp));
  313. mString = new char[strlen(temp) + 1]; /* Flawfinder: ignore */
  314. if (mString == NULL)
  315. {
  316. llerrs << "Memory Allocation Failed" << llendl;
  317. return;
  318. }
  319. strcpy(mString, temp); /* Flawfinder: ignore */
  320. }
  321. break;
  322. case LST_LIST:
  323. break;
  324. case LST_VECTOR:
  325. bytestream2vector(mVec, src, offset);
  326. break;
  327. case LST_QUATERNION:
  328. bytestream2quaternion(mQuat, src, offset);
  329. break;
  330. default:
  331. break;
  332. }
  333. }
  334. void print(std::ostream &s, BOOL b_prepend_comma);
  335. void print_separator(std::ostream& ostr, BOOL b_prepend_sep, char* sep);
  336. void setFromCSV(const char *src)
  337. {
  338. mType = LST_STRING;
  339. mString = new char[strlen(src) + 1]; /* Flawfinder: ignore */
  340. if (mString == NULL)
  341. {
  342. llerrs << "Memory Allocation Failed" << llendl;
  343. return;
  344. }
  345. strcpy(mString, src); /* Flawfinder: ignore */
  346. }
  347. LLScriptLibData(S32 integer) : mType(LST_INTEGER), mInteger(integer), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
  348. {
  349. }
  350. LLScriptLibData(F32 fp) : mType(LST_FLOATINGPOINT), mInteger(0), mFP(fp), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
  351. {
  352. }
  353. LLScriptLibData(const LLUUID &id) : mType(LST_KEY), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
  354. {
  355. std::string idstr;
  356. id.toString(idstr);
  357. mKey = new char[idstr.length()+1];
  358. LLStringUtil::copy(mKey,idstr.c_str(),idstr.length()+1);
  359. }
  360. LLScriptLibData(const char *string) : mType(LST_STRING), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
  361. {
  362. if (!string)
  363. {
  364. mString = new char[1];
  365. mString[0] = 0;
  366. }
  367. else
  368. {
  369. mString = new char[strlen(string) + 1]; /* Flawfinder: ignore */
  370. if (mString == NULL)
  371. {
  372. llerrs << "Memory Allocation Failed" << llendl;
  373. return;
  374. }
  375. strcpy(mString, string); /* Flawfinder: ignore */
  376. }
  377. }
  378. LLScriptLibData(const LLVector3 &vec) : mType(LST_VECTOR), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(vec), mQuat(), mListp(NULL)
  379. {
  380. }
  381. LLScriptLibData(const LLQuaternion &quat) : mType(LST_QUATERNION), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(quat), mListp(NULL)
  382. {
  383. }
  384. ~LLScriptLibData()
  385. {
  386. delete mListp;
  387. delete [] mKey;
  388. delete [] mString;
  389. }
  390. };
  391. extern LLScriptLibrary gScriptLibrary;
  392. #endif