Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.39 KB | None | 0 0
  1. // Name : Avlis Persistence System include
  2. // Purpose : Various APS/NWNX2 related functions
  3. // Authors : Ingmar Stieger, Adam Colon, Josh Simon
  4. // Modified : December 21, 2003
  5.  
  6. // This file is licensed under the terms of the
  7. // GNU GENERAL PUBLIC LICENSE (GPL) Version 2
  8.  
  9. #include nwnx_sql
  10.  
  11.  
  12. /************************************/
  13. /* Return codes */
  14. /************************************/
  15.  
  16. int SQL_ERROR = 0;
  17. int SQL_SUCCESS = 1;
  18.  
  19. /************************************/
  20. /* Function prototypes */
  21. /************************************/
  22.  
  23. // Setup placeholders for ODBC requests and responses
  24. void SQLInit();
  25.  
  26. // Execute statement in sSQL
  27. void SQLExecDirect(string sSQL);
  28.  
  29. // Position cursor on next row of the resultset
  30. // Call this before using SQLGetData().
  31. // returns: SQL_SUCCESS if there is a row
  32. // SQL_ERROR if there are no more rows
  33. int SQLFetch();
  34.  
  35. // * deprecated. Use SQLFetch instead.
  36. // Position cursor on first row of the resultset and name it sResultSetName
  37. // Call this before using SQLNextRow() and SQLGetData().
  38. // returns: SQL_SUCCESS if result set is not empty
  39. // SQL_ERROR is result set is empty
  40. int SQLFirstRow();
  41.  
  42. // * deprecated. Use SQLFetch instead.
  43. // Position cursor on next row of the result set sResultSetName
  44. // returns: SQL_SUCCESS if cursor could be advanced to next row
  45. // SQL_ERROR if there was no next row
  46. int SQLNextRow();
  47.  
  48. // Return value of column iCol in the current row of result set sResultSetName
  49. string SQLGetData(int iCol);
  50.  
  51. // Return a string value when given a location
  52. string APSLocationToString(location lLocation);
  53.  
  54. // Return a location value when given the string form of the location
  55. location APSStringToLocation(string sLocation);
  56.  
  57. // Return a string value when given a vector
  58. string APSVectorToString(vector vVector);
  59.  
  60. // Return a vector value when given the string form of the vector
  61. vector APSStringToVector(string sVector);
  62.  
  63. // Set oObject's persistent string variable sVarName to sValue
  64. // Optional parameters:
  65. // iExpiration: Number of days the persistent variable should be kept in database (default: 0=forever)
  66. // sTable: Name of the table where variable should be stored (default: pwdata)
  67. void SetPersistentString(object oObject, string sVarName, string sValue, int iExpiration = 0, string sTable = "pwdata");
  68.  
  69. // Set oObject's persistent integer variable sVarName to iValue
  70. // Optional parameters:
  71. // iExpiration: Number of days the persistent variable should be kept in database (default: 0=forever)
  72. // sTable: Name of the table where variable should be stored (default: pwdata)
  73. void SetPersistentInt(object oObject, string sVarName, int iValue, int iExpiration = 0, string sTable = "pwdata");
  74.  
  75. // Set oObject's persistent float variable sVarName to fValue
  76. // Optional parameters:
  77. // iExpiration: Number of days the persistent variable should be kept in database (default: 0=forever)
  78. // sTable: Name of the table where variable should be stored (default: pwdata)
  79. void SetPersistentFloat(object oObject, string sVarName, float fValue, int iExpiration = 0, string sTable = "pwdata");
  80.  
  81. // Set oObject's persistent location variable sVarName to lLocation
  82. // Optional parameters:
  83. // iExpiration: Number of days the persistent variable should be kept in database (default: 0=forever)
  84. // sTable: Name of the table where variable should be stored (default: pwdata)
  85. // This function converts location to a string for storage in the database.
  86. void SetPersistentLocation(object oObject, string sVarName, location lLocation, int iExpiration = 0, string sTable = "pwdata");
  87.  
  88. // Set oObject's persistent vector variable sVarName to vVector
  89. // Optional parameters:
  90. // iExpiration: Number of days the persistent variable should be kept in database (default: 0=forever)
  91. // sTable: Name of the table where variable should be stored (default: pwdata)
  92. // This function converts vector to a string for storage in the database.
  93. void SetPersistentVector(object oObject, string sVarName, vector vVector, int iExpiration = 0, string sTable = "pwdata");
  94.  
  95. // Get oObject's persistent string variable sVarName
  96. // Optional parameters:
  97. // sTable: Name of the table where variable is stored (default: pwdata)
  98. // * Return value on error: ""
  99. string GetPersistentString(object oObject, string sVarName, string sTable = "pwdata");
  100.  
  101. // Get oObject's persistent integer variable sVarName
  102. // Optional parameters:
  103. // sTable: Name of the table where variable is stored (default: pwdata)
  104. // * Return value on error: 0
  105. int GetPersistentInt(object oObject, string sVarName, string sTable = "pwdata");
  106.  
  107. // Get oObject's persistent float variable sVarName
  108. // Optional parameters:
  109. // sTable: Name of the table where variable is stored (default: pwdata)
  110. // * Return value on error: 0
  111. float GetPersistentFloat(object oObject, string sVarName, string sTable = "pwdata");
  112.  
  113. // Get oObject's persistent location variable sVarName
  114. // Optional parameters:
  115. // sTable: Name of the table where variable is stored (default: pwdata)
  116. // * Return value on error: 0
  117. location GetPersistentLocation(object oObject, string sVarname, string sTable = "pwdata");
  118.  
  119. // Get oObject's persistent vector variable sVarName
  120. // Optional parameters:
  121. // sTable: Name of the table where variable is stored (default: pwdata)
  122. // * Return value on error: 0
  123. vector GetPersistentVector(object oObject, string sVarName, string sTable = "pwdata");
  124.  
  125. // Delete persistent variable sVarName stored on oObject
  126. // Optional parameters:
  127. // sTable: Name of the table where variable is stored (default: pwdata)
  128. void DeletePersistentVariable(object oObject, string sVarName, string sTable = "pwdata");
  129.  
  130. // (private function) Replace special character ' with ~
  131. string SQLEncodeSpecialChars(string sString);
  132.  
  133. // (private function)Replace special character ' with ~
  134. string SQLDecodeSpecialChars(string sString);
  135.  
  136. /************************************/
  137. /* Implementation */
  138. /************************************/
  139.  
  140. // Functions for initializing APS and working with result sets
  141.  
  142. void SQLInit()
  143. {
  144. int i;
  145.  
  146. // Placeholder for ODBC persistence
  147. string sMemory;
  148.  
  149. for (i = 0; i < 8; i++) // reserve 8*128 bytes
  150. sMemory +=
  151. "................................................................................................................................";
  152.  
  153. SetLocalString(GetModule(), "NWNX!ODBC!SPACER", sMemory);
  154. }
  155.  
  156. void SQLExecDirect(string sSQL)
  157. {
  158. SetLocalString(GetModule(), "NWNX!ODBC!EXEC", sSQL);
  159. }
  160.  
  161. int SQLFetch()
  162. {
  163. string sRow;
  164. object oModule = GetModule();
  165.  
  166. SetLocalString(oModule, "NWNX!ODBC!FETCH", GetLocalString(oModule, "NWNX!ODBC!SPACER"));
  167. sRow = GetLocalString(oModule, "NWNX!ODBC!FETCH");
  168. if (GetStringLength(sRow) > 0)
  169. {
  170. SetLocalString(oModule, "NWNX_ODBC_CurrentRow", sRow);
  171. return SQL_SUCCESS;
  172. }
  173. else
  174. {
  175. SetLocalString(oModule, "NWNX_ODBC_CurrentRow", "");
  176. return SQL_ERROR;
  177. }
  178. }
  179.  
  180. // deprecated. use SQLFetch().
  181. int SQLFirstRow()
  182. {
  183. return SQLFetch();
  184. }
  185.  
  186. // deprecated. use SQLFetch().
  187. int SQLNextRow()
  188. {
  189. return SQLFetch();
  190. }
  191.  
  192. string SQLGetData(int iCol)
  193. {
  194. int iPos;
  195. string sResultSet = GetLocalString(GetModule(), "NWNX_ODBC_CurrentRow");
  196.  
  197. // find column in current row
  198. int iCount = 0;
  199. string sColValue = "";
  200.  
  201. iPos = FindSubString(sResultSet, "¬");
  202. if ((iPos == -1) && (iCol == 1))
  203. {
  204. // only one column, return value immediately
  205. sColValue = sResultSet;
  206. }
  207. else if (iPos == -1)
  208. {
  209. // only one column but requested column > 1
  210. sColValue = "";
  211. }
  212. else
  213. {
  214. // loop through columns until found
  215. while (iCount != iCol)
  216. {
  217. iCount++;
  218. if (iCount == iCol)
  219. sColValue = GetStringLeft(sResultSet, iPos);
  220. else
  221. {
  222. sResultSet = GetStringRight(sResultSet, GetStringLength(sResultSet) - iPos - 1);
  223. iPos = FindSubString(sResultSet, "¬");
  224. }
  225.  
  226. // special case: last column in row
  227. if (iPos == -1)
  228. iPos = GetStringLength(sResultSet);
  229. }
  230. }
  231.  
  232. return sColValue;
  233. }
  234.  
  235. // These functions deal with various data types. Ultimately, all information
  236. // must be stored in the database as strings, and converted back to the proper
  237. // form when retrieved.
  238.  
  239. string APSVectorToString(vector vVector)
  240. {
  241. return "#POSITION_X#" + FloatToString(vVector.x) + "#POSITION_Y#" + FloatToString(vVector.y) +
  242. "#POSITION_Z#" + FloatToString(vVector.z) + "#END#";
  243. }
  244.  
  245. vector APSStringToVector(string sVector)
  246. {
  247. float fX, fY, fZ;
  248. int iPos, iCount;
  249. int iLen = GetStringLength(sVector);
  250.  
  251. if (iLen > 0)
  252. {
  253. iPos = FindSubString(sVector, "#POSITION_X#") + 12;
  254. iCount = FindSubString(GetSubString(sVector, iPos, iLen - iPos), "#");
  255. fX = StringToFloat(GetSubString(sVector, iPos, iCount));
  256.  
  257. iPos = FindSubString(sVector, "#POSITION_Y#") + 12;
  258. iCount = FindSubString(GetSubString(sVector, iPos, iLen - iPos), "#");
  259. fY = StringToFloat(GetSubString(sVector, iPos, iCount));
  260.  
  261. iPos = FindSubString(sVector, "#POSITION_Z#") + 12;
  262. iCount = FindSubString(GetSubString(sVector, iPos, iLen - iPos), "#");
  263. fZ = StringToFloat(GetSubString(sVector, iPos, iCount));
  264. }
  265.  
  266. return Vector(fX, fY, fZ);
  267. }
  268.  
  269. string APSLocationToString(location lLocation)
  270. {
  271. object oArea = GetAreaFromLocation(lLocation);
  272. vector vPosition = GetPositionFromLocation(lLocation);
  273. float fOrientation = GetFacingFromLocation(lLocation);
  274. string sReturnValue;
  275.  
  276. if (GetIsObjectValid(oArea))
  277. sReturnValue =
  278. "#AREA#" + GetTag(oArea) + "#POSITION_X#" + FloatToString(vPosition.x) +
  279. "#POSITION_Y#" + FloatToString(vPosition.y) + "#POSITION_Z#" +
  280. FloatToString(vPosition.z) + "#ORIENTATION#" + FloatToString(fOrientation) + "#END#";
  281.  
  282. return sReturnValue;
  283. }
  284.  
  285. location APSStringToLocation(string sLocation)
  286. {
  287. location lReturnValue;
  288. object oArea;
  289. vector vPosition;
  290. float fOrientation, fX, fY, fZ;
  291.  
  292. int iPos, iCount;
  293. int iLen = GetStringLength(sLocation);
  294.  
  295. if (iLen > 0)
  296. {
  297. iPos = FindSubString(sLocation, "#AREA#") + 6;
  298. iCount = FindSubString(GetSubString(sLocation, iPos, iLen - iPos), "#");
  299. oArea = GetObjectByTag(GetSubString(sLocation, iPos, iCount));
  300.  
  301. iPos = FindSubString(sLocation, "#POSITION_X#") + 12;
  302. iCount = FindSubString(GetSubString(sLocation, iPos, iLen - iPos), "#");
  303. fX = StringToFloat(GetSubString(sLocation, iPos, iCount));
  304.  
  305. iPos = FindSubString(sLocation, "#POSITION_Y#") + 12;
  306. iCount = FindSubString(GetSubString(sLocation, iPos, iLen - iPos), "#");
  307. fY = StringToFloat(GetSubString(sLocation, iPos, iCount));
  308.  
  309. iPos = FindSubString(sLocation, "#POSITION_Z#") + 12;
  310. iCount = FindSubString(GetSubString(sLocation, iPos, iLen - iPos), "#");
  311. fZ = StringToFloat(GetSubString(sLocation, iPos, iCount));
  312.  
  313. vPosition = Vector(fX, fY, fZ);
  314.  
  315. iPos = FindSubString(sLocation, "#ORIENTATION#") + 13;
  316. iCount = FindSubString(GetSubString(sLocation, iPos, iLen - iPos), "#");
  317. fOrientation = StringToFloat(GetSubString(sLocation, iPos, iCount));
  318.  
  319. lReturnValue = Location(oArea, vPosition, fOrientation);
  320. }
  321.  
  322. return lReturnValue;
  323. }
  324.  
  325. // These functions are responsible for transporting the various data types back
  326. // and forth to the database.
  327.  
  328. void SetPersistentString(object oObject, string sVarName, string sValue, int iExpiration =
  329. 0, string sTable = "pwdata")
  330. {
  331. string sPlayer;
  332. string sTag;
  333.  
  334. if (GetIsPC(oObject))
  335. {
  336. sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oObject));
  337. sTag = SQLEncodeSpecialChars(GetName(oObject));
  338. }
  339. else
  340. {
  341. sPlayer = "~";
  342. sTag = GetTag(oObject);
  343. }
  344.  
  345. sVarName = SQLEncodeSpecialChars(sVarName);
  346. sValue = SQLEncodeSpecialChars(sValue);
  347.  
  348. string sSQL = "SELECT player FROM " + sTable + " WHERE player='" + sPlayer +
  349. "' AND tag='" + sTag + "' AND name='" + sVarName + "'";
  350. SQLExecDirect(sSQL);
  351.  
  352. if (SQLFirstRow() == SQL_SUCCESS)
  353. {
  354. // row exists
  355. sSQL = "UPDATE " + sTable + " SET val='" + sValue +
  356. "',expire=" + IntToString(iExpiration) + " WHERE player='" + sPlayer +
  357. "' AND tag='" + sTag + "' AND name='" + sVarName + "'";
  358. SQLExecDirect(sSQL);
  359. }
  360. else
  361. {
  362. // row doesn't exist
  363. sSQL = "INSERT INTO " + sTable + " (player,tag,name,val,expire) VALUES" +
  364. "('" + sPlayer + "','" + sTag + "','" + sVarName + "','" +
  365. sValue + "'," + IntToString(iExpiration) + ")";
  366. SQLExecDirect(sSQL);
  367. }
  368. }
  369.  
  370. string GetPersistentString(object oObject, string sVarName, string sTable = "pwdata")
  371. {
  372. string sPlayer;
  373. string sTag;
  374.  
  375. if (GetIsPC(oObject))
  376. {
  377. sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oObject));
  378. sTag = SQLEncodeSpecialChars(GetName(oObject));
  379. }
  380. else
  381. {
  382. sPlayer = "~";
  383. sTag = GetTag(oObject);
  384. }
  385.  
  386. sVarName = SQLEncodeSpecialChars(sVarName);
  387.  
  388. string sSQL = "SELECT val FROM " + sTable + " WHERE player='" + sPlayer +
  389. "' AND tag='" + sTag + "' AND name='" + sVarName + "'";
  390. SQLExecDirect(sSQL);
  391.  
  392. if (SQLFirstRow() == SQL_SUCCESS)
  393. return SQLDecodeSpecialChars(SQLGetData(1));
  394. else
  395. {
  396. return "";
  397. // If you want to convert your existing persistent data to APS, this
  398. // would be the place to do it. The requested variable was not found
  399. // in the database, you should
  400. // 1) query it's value using your existing persistence functions
  401. // 2) save the value to the database using SetPersistentString()
  402. // 3) return the string value here.
  403. }
  404. }
  405.  
  406. void SetPersistentInt(object oObject, string sVarName, int iValue, int iExpiration =
  407. 0, string sTable = "pwdata")
  408. {
  409. SetPersistentString(oObject, sVarName, IntToString(iValue), iExpiration, sTable);
  410. }
  411.  
  412. int GetPersistentInt(object oObject, string sVarName, string sTable = "pwdata")
  413. {
  414. return StringToInt(GetPersistentString(oObject, sVarName, sTable));
  415. }
  416.  
  417. void SetPersistentFloat(object oObject, string sVarName, float fValue, int iExpiration =
  418. 0, string sTable = "pwdata")
  419. {
  420. SetPersistentString(oObject, sVarName, FloatToString(fValue), iExpiration, sTable);
  421. }
  422.  
  423. float GetPersistentFloat(object oObject, string sVarName, string sTable = "pwdata")
  424. {
  425. return StringToFloat(GetPersistentString(oObject, sVarName, sTable));
  426. }
  427.  
  428. void SetPersistentLocation(object oObject, string sVarName, location lLocation, int iExpiration =
  429. 0, string sTable = "pwdata")
  430. {
  431. SetPersistentString(oObject, sVarName, APSLocationToString(lLocation), iExpiration, sTable);
  432. }
  433.  
  434. location GetPersistentLocation(object oObject, string sVarName, string sTable = "pwdata")
  435. {
  436. return APSStringToLocation(GetPersistentString(oObject, sVarName, sTable));
  437. }
  438.  
  439. void SetPersistentVector(object oObject, string sVarName, vector vVector, int iExpiration =
  440. 0, string sTable = "pwdata")
  441. {
  442. SetPersistentString(oObject, sVarName, APSVectorToString(vVector), iExpiration, sTable);
  443. }
  444.  
  445. vector GetPersistentVector(object oObject, string sVarName, string sTable = "pwdata")
  446. {
  447. return APSStringToVector(GetPersistentString(oObject, sVarName, sTable));
  448. }
  449.  
  450. void DeletePersistentVariable(object oObject, string sVarName, string sTable = "pwdata")
  451. {
  452. string sPlayer;
  453. string sTag;
  454.  
  455. if (GetIsPC(oObject))
  456. {
  457. sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oObject));
  458. sTag = SQLEncodeSpecialChars(GetName(oObject));
  459. }
  460. else
  461. {
  462. sPlayer = "~";
  463. sTag = GetTag(oObject);
  464. }
  465.  
  466. sVarName = SQLEncodeSpecialChars(sVarName);
  467. string sSQL = "DELETE FROM " + sTable + " WHERE player='" + sPlayer +
  468. "' AND tag='" + sTag + "' AND name='" + sVarName + "'";
  469. SQLExecDirect(sSQL);
  470. }
  471.  
  472. // Problems can arise with SQL commands if variables or values have single quotes
  473. // in their names. These functions are a replace these quote with the tilde character
  474.  
  475. string SQLEncodeSpecialChars(string sString)
  476. {
  477. if (FindSubString(sString, "'") == -1) // not found
  478. return sString;
  479.  
  480. int i;
  481. string sReturn = "";
  482. string sChar;
  483.  
  484. // Loop over every character and replace special characters
  485. for (i = 0; i < GetStringLength(sString); i++)
  486. {
  487. sChar = GetSubString(sString, i, 1);
  488. if (sChar == "'")
  489. sReturn += "~";
  490. else
  491. sReturn += sChar;
  492. }
  493. return sReturn;
  494. }
  495.  
  496. string SQLDecodeSpecialChars(string sString)
  497. {
  498. if (FindSubString(sString, "~") == -1) // not found
  499. return sString;
  500.  
  501. int i;
  502. string sReturn = "";
  503. string sChar;
  504.  
  505. // Loop over every character and replace special characters
  506. for (i = 0; i < GetStringLength(sString); i++)
  507. {
  508. sChar = GetSubString(sString, i, 1);
  509. if (sChar == "~")
  510. sReturn += "'";
  511. else
  512. sReturn += sChar;
  513. }
  514. return sReturn;
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement