MySQLManager.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.IO;
  32. using System.Reflection;
  33. using log4net;
  34. using MySql.Data.MySqlClient;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. namespace OpenSim.Data.MySQL
  38. {
  39. /// <summary>
  40. /// A MySQL Database manager
  41. /// </summary>
  42. public class MySQLManager
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// The database connection object
  47. /// </summary>
  48. private MySqlConnection dbcon;
  49. /// <summary>
  50. /// Connection string for ADO.net
  51. /// </summary>
  52. private string connectionString;
  53. private const string m_waitTimeoutSelect = "select @@wait_timeout";
  54. /// <summary>
  55. /// Wait timeout for our connection in ticks.
  56. /// </summary>
  57. private long m_waitTimeout;
  58. /// <summary>
  59. /// Make our storage of the timeout this amount smaller than it actually is, to give us a margin on long
  60. /// running database operations.
  61. /// </summary>
  62. private long m_waitTimeoutLeeway = 60 * TimeSpan.TicksPerSecond;
  63. /// <summary>
  64. /// Holds the last tick time that the connection was used.
  65. /// </summary>
  66. private long m_lastConnectionUse;
  67. /// <summary>
  68. /// Initialises and creates a new MySQL connection and maintains it.
  69. /// </summary>
  70. /// <param name="hostname">The MySQL server being connected to</param>
  71. /// <param name="database">The name of the MySQL database being used</param>
  72. /// <param name="username">The username logging into the database</param>
  73. /// <param name="password">The password for the user logging in</param>
  74. /// <param name="cpooling">Whether to use connection pooling or not, can be one of the following: 'yes', 'true', 'no' or 'false', if unsure use 'false'.</param>
  75. /// <param name="port">The MySQL server port</param>
  76. public MySQLManager(string hostname, string database, string username, string password, string cpooling,
  77. string port)
  78. {
  79. string s = "Server=" + hostname + ";Port=" + port + ";Database=" + database + ";User ID=" +
  80. username + ";Password=" + password + ";Pooling=" + cpooling + ";";
  81. Initialise(s);
  82. }
  83. /// <summary>
  84. /// Initialises and creates a new MySQL connection and maintains it.
  85. /// </summary>
  86. /// <param name="connect">connectionString</param>
  87. public MySQLManager(String connect)
  88. {
  89. Initialise(connect);
  90. }
  91. /// <summary>
  92. /// Initialises and creates a new MySQL connection and maintains it.
  93. /// </summary>
  94. /// <param name="connect">connectionString</param>
  95. public void Initialise(String connect)
  96. {
  97. try
  98. {
  99. connectionString = connect;
  100. dbcon = new MySqlConnection(connectionString);
  101. try
  102. {
  103. dbcon.Open();
  104. }
  105. catch(Exception e)
  106. {
  107. throw new Exception("Connection error while using connection string ["+connectionString+"]", e);
  108. }
  109. m_log.Info("[MYSQL]: Connection established");
  110. GetWaitTimeout();
  111. }
  112. catch (Exception e)
  113. {
  114. throw new Exception("Error initialising MySql Database: " + e.ToString());
  115. }
  116. }
  117. /// <summary>
  118. /// Get the wait_timeout value for our connection
  119. /// </summary>
  120. protected void GetWaitTimeout()
  121. {
  122. MySqlCommand cmd = new MySqlCommand(m_waitTimeoutSelect, dbcon);
  123. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  124. {
  125. if (dbReader.Read())
  126. {
  127. m_waitTimeout
  128. = Convert.ToInt32(dbReader["@@wait_timeout"]) * TimeSpan.TicksPerSecond + m_waitTimeoutLeeway;
  129. }
  130. dbReader.Close();
  131. cmd.Dispose();
  132. }
  133. m_lastConnectionUse = DateTime.Now.Ticks;
  134. m_log.DebugFormat(
  135. "[REGION DB]: Connection wait timeout {0} seconds", m_waitTimeout / TimeSpan.TicksPerSecond);
  136. }
  137. /// <summary>
  138. /// Should be called before any db operation. This checks to see if the connection has not timed out
  139. /// </summary>
  140. public void CheckConnection()
  141. {
  142. //m_log.Debug("[REGION DB]: Checking connection");
  143. long timeNow = DateTime.Now.Ticks;
  144. if (timeNow - m_lastConnectionUse > m_waitTimeout || dbcon.State != ConnectionState.Open)
  145. {
  146. m_log.DebugFormat("[REGION DB]: Database connection has gone away - reconnecting");
  147. Reconnect();
  148. }
  149. // Strictly, we should set this after the actual db operation. But it's more convenient to set here rather
  150. // than require the code to call another method - the timeout leeway should be large enough to cover the
  151. // inaccuracy.
  152. m_lastConnectionUse = timeNow;
  153. }
  154. /// <summary>
  155. /// Get the connection being used
  156. /// </summary>
  157. /// <returns>MySqlConnection Object</returns>
  158. public MySqlConnection Connection
  159. {
  160. get { return dbcon; }
  161. }
  162. /// <summary>
  163. /// Shuts down the database connection
  164. /// </summary>
  165. public void Close()
  166. {
  167. dbcon.Close();
  168. dbcon = null;
  169. }
  170. /// <summary>
  171. /// Reconnects to the database
  172. /// </summary>
  173. public void Reconnect()
  174. {
  175. m_log.Info("[REGION DB] Reconnecting database");
  176. lock (dbcon)
  177. {
  178. try
  179. {
  180. // Close the DB connection
  181. dbcon.Close();
  182. // Try reopen it
  183. dbcon = new MySqlConnection(connectionString);
  184. dbcon.Open();
  185. }
  186. catch (Exception e)
  187. {
  188. m_log.Error("Unable to reconnect to database " + e.ToString());
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// Returns the version of this DB provider
  194. /// </summary>
  195. /// <returns>A string containing the DB provider</returns>
  196. public string getVersion()
  197. {
  198. Module module = GetType().Module;
  199. // string dllName = module.Assembly.ManifestModule.Name;
  200. Version dllVersion = module.Assembly.GetName().Version;
  201. return
  202. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  203. dllVersion.Revision);
  204. }
  205. /// <summary>
  206. /// Extract a named string resource from the embedded resources
  207. /// </summary>
  208. /// <param name="name">name of embedded resource</param>
  209. /// <returns>string contained within the embedded resource</returns>
  210. private string getResourceString(string name)
  211. {
  212. Assembly assem = GetType().Assembly;
  213. string[] names = assem.GetManifestResourceNames();
  214. foreach (string s in names)
  215. {
  216. if (s.EndsWith(name))
  217. {
  218. using (Stream resource = assem.GetManifestResourceStream(s))
  219. {
  220. using (StreamReader resourceReader = new StreamReader(resource))
  221. {
  222. string resourceString = resourceReader.ReadToEnd();
  223. return resourceString;
  224. }
  225. }
  226. }
  227. }
  228. throw new Exception(string.Format("Resource '{0}' was not found", name));
  229. }
  230. /// <summary>
  231. /// Execute a SQL statement stored in a resource, as a string
  232. /// </summary>
  233. /// <param name="name">name of embedded resource</param>
  234. public void ExecuteResourceSql(string name)
  235. {
  236. CheckConnection();
  237. MySqlCommand cmd = new MySqlCommand(getResourceString(name), dbcon);
  238. cmd.ExecuteNonQuery();
  239. }
  240. /// <summary>
  241. /// Execute a MySqlCommand
  242. /// </summary>
  243. /// <param name="sql">sql string to execute</param>
  244. public void ExecuteSql(string sql)
  245. {
  246. CheckConnection();
  247. MySqlCommand cmd = new MySqlCommand(sql, dbcon);
  248. cmd.ExecuteNonQuery();
  249. }
  250. public void ExecuteParameterizedSql(string sql, Dictionary<string, string> parameters)
  251. {
  252. CheckConnection();
  253. MySqlCommand cmd = (MySqlCommand)dbcon.CreateCommand();
  254. cmd.CommandText = sql;
  255. foreach (KeyValuePair<string, string> param in parameters)
  256. {
  257. cmd.Parameters.AddWithValue(param.Key, param.Value);
  258. }
  259. cmd.ExecuteNonQuery();
  260. }
  261. /// <summary>
  262. /// Given a list of tables, return the version of the tables, as seen in the database
  263. /// </summary>
  264. /// <param name="tableList"></param>
  265. public void GetTableVersion(Dictionary<string, string> tableList)
  266. {
  267. lock (dbcon)
  268. {
  269. CheckConnection();
  270. MySqlCommand tablesCmd =
  271. new MySqlCommand(
  272. "SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?dbname",
  273. dbcon);
  274. tablesCmd.Parameters.AddWithValue("?dbname", dbcon.Database);
  275. using (MySqlDataReader tables = tablesCmd.ExecuteReader())
  276. {
  277. while (tables.Read())
  278. {
  279. try
  280. {
  281. string tableName = (string) tables["TABLE_NAME"];
  282. string comment = (string) tables["TABLE_COMMENT"];
  283. if (tableList.ContainsKey(tableName))
  284. {
  285. tableList[tableName] = comment;
  286. }
  287. }
  288. catch (Exception e)
  289. {
  290. m_log.Error(e.ToString());
  291. }
  292. }
  293. tables.Close();
  294. }
  295. }
  296. }
  297. // TODO: at some time this code should be cleaned up
  298. /// <summary>
  299. /// Runs a query with protection against SQL Injection by using parameterised input.
  300. /// </summary>
  301. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  302. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  303. /// <returns>A MySQL DB Command</returns>
  304. public IDbCommand Query(string sql, Dictionary<string, object> parameters)
  305. {
  306. try
  307. {
  308. CheckConnection(); // Not sure if this one is necessary
  309. MySqlCommand dbcommand = (MySqlCommand) dbcon.CreateCommand();
  310. dbcommand.CommandText = sql;
  311. foreach (KeyValuePair<string, object> param in parameters)
  312. {
  313. dbcommand.Parameters.AddWithValue(param.Key, param.Value);
  314. }
  315. return (IDbCommand) dbcommand;
  316. }
  317. catch (Exception e)
  318. {
  319. // Return null if it fails.
  320. m_log.Error("Failed during Query generation: " + e.ToString());
  321. return null;
  322. }
  323. }
  324. /// <summary>
  325. /// Reads a region row from a database reader
  326. /// </summary>
  327. /// <param name="reader">An active database reader</param>
  328. /// <returns>A region profile</returns>
  329. public RegionProfileData readSimRow(IDataReader reader)
  330. {
  331. RegionProfileData retval = new RegionProfileData();
  332. if (reader.Read())
  333. {
  334. // Region Main gotta-have-or-we-return-null parts
  335. UInt64 tmp64;
  336. if (!UInt64.TryParse(reader["regionHandle"].ToString(), out tmp64))
  337. {
  338. return null;
  339. }
  340. else
  341. {
  342. retval.regionHandle = tmp64;
  343. }
  344. UUID tmp_uuid;
  345. if (!UUID.TryParse((string)reader["uuid"], out tmp_uuid))
  346. {
  347. return null;
  348. }
  349. else
  350. {
  351. retval.UUID = tmp_uuid;
  352. }
  353. // non-critical parts
  354. retval.regionName = (string)reader["regionName"];
  355. retval.originUUID = new UUID((string) reader["originUUID"]);
  356. // Secrets
  357. retval.regionRecvKey = (string) reader["regionRecvKey"];
  358. retval.regionSecret = (string) reader["regionSecret"];
  359. retval.regionSendKey = (string) reader["regionSendKey"];
  360. // Region Server
  361. retval.regionDataURI = (string) reader["regionDataURI"];
  362. retval.regionOnline = false; // Needs to be pinged before this can be set.
  363. retval.serverIP = (string) reader["serverIP"];
  364. retval.serverPort = (uint) reader["serverPort"];
  365. retval.serverURI = (string) reader["serverURI"];
  366. retval.httpPort = Convert.ToUInt32(reader["serverHttpPort"].ToString());
  367. retval.remotingPort = Convert.ToUInt32(reader["serverRemotingPort"].ToString());
  368. // Location
  369. retval.regionLocX = Convert.ToUInt32(reader["locX"].ToString());
  370. retval.regionLocY = Convert.ToUInt32(reader["locY"].ToString());
  371. retval.regionLocZ = Convert.ToUInt32(reader["locZ"].ToString());
  372. // Neighbours - 0 = No Override
  373. retval.regionEastOverrideHandle = Convert.ToUInt64(reader["eastOverrideHandle"].ToString());
  374. retval.regionWestOverrideHandle = Convert.ToUInt64(reader["westOverrideHandle"].ToString());
  375. retval.regionSouthOverrideHandle = Convert.ToUInt64(reader["southOverrideHandle"].ToString());
  376. retval.regionNorthOverrideHandle = Convert.ToUInt64(reader["northOverrideHandle"].ToString());
  377. // Assets
  378. retval.regionAssetURI = (string) reader["regionAssetURI"];
  379. retval.regionAssetRecvKey = (string) reader["regionAssetRecvKey"];
  380. retval.regionAssetSendKey = (string) reader["regionAssetSendKey"];
  381. // Userserver
  382. retval.regionUserURI = (string) reader["regionUserURI"];
  383. retval.regionUserRecvKey = (string) reader["regionUserRecvKey"];
  384. retval.regionUserSendKey = (string) reader["regionUserSendKey"];
  385. // World Map Addition
  386. UUID.TryParse((string)reader["regionMapTexture"], out retval.regionMapTextureID);
  387. UUID.TryParse((string)reader["owner_uuid"], out retval.owner_uuid);
  388. }
  389. else
  390. {
  391. return null;
  392. }
  393. return retval;
  394. }
  395. /// <summary>
  396. /// Reads a reservation row from a database reader
  397. /// </summary>
  398. /// <param name="reader">An active database reader</param>
  399. /// <returns>A reservation data object</returns>
  400. public ReservationData readReservationRow(IDataReader reader)
  401. {
  402. ReservationData retval = new ReservationData();
  403. if (reader.Read())
  404. {
  405. retval.gridRecvKey = (string) reader["gridRecvKey"];
  406. retval.gridSendKey = (string) reader["gridSendKey"];
  407. retval.reservationCompany = (string) reader["resCompany"];
  408. retval.reservationMaxX = Convert.ToInt32(reader["resXMax"].ToString());
  409. retval.reservationMaxY = Convert.ToInt32(reader["resYMax"].ToString());
  410. retval.reservationMinX = Convert.ToInt32(reader["resXMin"].ToString());
  411. retval.reservationMinY = Convert.ToInt32(reader["resYMin"].ToString());
  412. retval.reservationName = (string) reader["resName"];
  413. retval.status = Convert.ToInt32(reader["status"].ToString()) == 1;
  414. UUID tmp;
  415. UUID.TryParse((string) reader["userUUID"], out tmp);
  416. retval.userUUID = tmp;
  417. }
  418. else
  419. {
  420. return null;
  421. }
  422. return retval;
  423. }
  424. /// <summary>
  425. /// Reads an agent row from a database reader
  426. /// </summary>
  427. /// <param name="reader">An active database reader</param>
  428. /// <returns>A user session agent</returns>
  429. public UserAgentData readAgentRow(IDataReader reader)
  430. {
  431. UserAgentData retval = new UserAgentData();
  432. if (reader.Read())
  433. {
  434. // Agent IDs
  435. UUID tmp;
  436. if (!UUID.TryParse((string)reader["UUID"], out tmp))
  437. return null;
  438. retval.ProfileID = tmp;
  439. UUID.TryParse((string) reader["sessionID"], out tmp);
  440. retval.SessionID = tmp;
  441. UUID.TryParse((string)reader["secureSessionID"], out tmp);
  442. retval.SecureSessionID = tmp;
  443. // Agent Who?
  444. retval.AgentIP = (string) reader["agentIP"];
  445. retval.AgentPort = Convert.ToUInt32(reader["agentPort"].ToString());
  446. retval.AgentOnline = Convert.ToBoolean(Convert.ToInt16(reader["agentOnline"].ToString()));
  447. // Login/Logout times (UNIX Epoch)
  448. retval.LoginTime = Convert.ToInt32(reader["loginTime"].ToString());
  449. retval.LogoutTime = Convert.ToInt32(reader["logoutTime"].ToString());
  450. // Current position
  451. retval.Region = new UUID((string)reader["currentRegion"]);
  452. retval.Handle = Convert.ToUInt64(reader["currentHandle"].ToString());
  453. Vector3 tmp_v;
  454. Vector3.TryParse((string) reader["currentPos"], out tmp_v);
  455. retval.Position = tmp_v;
  456. Vector3.TryParse((string)reader["currentLookAt"], out tmp_v);
  457. retval.LookAt = tmp_v;
  458. }
  459. else
  460. {
  461. return null;
  462. }
  463. return retval;
  464. }
  465. /// <summary>
  466. /// Reads a user profile from an active data reader
  467. /// </summary>
  468. /// <param name="reader">An active database reader</param>
  469. /// <returns>A user profile</returns>
  470. public UserProfileData readUserRow(IDataReader reader)
  471. {
  472. UserProfileData retval = new UserProfileData();
  473. if (reader.Read())
  474. {
  475. UUID id;
  476. if (!UUID.TryParse((string)reader["UUID"], out id))
  477. return null;
  478. retval.ID = id;
  479. retval.FirstName = (string) reader["username"];
  480. retval.SurName = (string) reader["lastname"];
  481. retval.Email = (reader.IsDBNull(reader.GetOrdinal("email"))) ? "" : (string) reader["email"];
  482. retval.PasswordHash = (string) reader["passwordHash"];
  483. retval.PasswordSalt = (string) reader["passwordSalt"];
  484. retval.HomeRegion = Convert.ToUInt64(reader["homeRegion"].ToString());
  485. retval.HomeLocation = new Vector3(
  486. Convert.ToSingle(reader["homeLocationX"].ToString()),
  487. Convert.ToSingle(reader["homeLocationY"].ToString()),
  488. Convert.ToSingle(reader["homeLocationZ"].ToString()));
  489. retval.HomeLookAt = new Vector3(
  490. Convert.ToSingle(reader["homeLookAtX"].ToString()),
  491. Convert.ToSingle(reader["homeLookAtY"].ToString()),
  492. Convert.ToSingle(reader["homeLookAtZ"].ToString()));
  493. UUID regionID = UUID.Zero;
  494. UUID.TryParse(reader["homeRegionID"].ToString(), out regionID); // it's ok if it doesn't work; just use UUID.Zero
  495. retval.HomeRegionID = regionID;
  496. retval.Created = Convert.ToInt32(reader["created"].ToString());
  497. retval.LastLogin = Convert.ToInt32(reader["lastLogin"].ToString());
  498. retval.UserInventoryURI = (string) reader["userInventoryURI"];
  499. retval.UserAssetURI = (string) reader["userAssetURI"];
  500. retval.CanDoMask = Convert.ToUInt32(reader["profileCanDoMask"].ToString());
  501. retval.WantDoMask = Convert.ToUInt32(reader["profileWantDoMask"].ToString());
  502. if (reader.IsDBNull(reader.GetOrdinal("profileAboutText")))
  503. retval.AboutText = "";
  504. else
  505. retval.AboutText = (string) reader["profileAboutText"];
  506. if (reader.IsDBNull(reader.GetOrdinal("profileFirstText")))
  507. retval.FirstLifeAboutText = "";
  508. else
  509. retval.FirstLifeAboutText = (string)reader["profileFirstText"];
  510. if (reader.IsDBNull(reader.GetOrdinal("profileImage")))
  511. retval.Image = UUID.Zero;
  512. else {
  513. UUID tmp;
  514. UUID.TryParse((string)reader["profileImage"], out tmp);
  515. retval.Image = tmp;
  516. }
  517. if (reader.IsDBNull(reader.GetOrdinal("profileFirstImage")))
  518. retval.FirstLifeImage = UUID.Zero;
  519. else {
  520. UUID tmp;
  521. UUID.TryParse((string)reader["profileFirstImage"], out tmp);
  522. retval.FirstLifeImage = tmp;
  523. }
  524. if (reader.IsDBNull(reader.GetOrdinal("webLoginKey")))
  525. {
  526. retval.WebLoginKey = UUID.Zero;
  527. }
  528. else
  529. {
  530. UUID tmp;
  531. UUID.TryParse((string)reader["webLoginKey"], out tmp);
  532. retval.WebLoginKey = tmp;
  533. }
  534. retval.UserFlags = Convert.ToInt32(reader["userFlags"].ToString());
  535. retval.GodLevel = Convert.ToInt32(reader["godLevel"].ToString());
  536. if (reader.IsDBNull(reader.GetOrdinal("customType")))
  537. retval.CustomType = "";
  538. else
  539. retval.CustomType = reader["customType"].ToString();
  540. if (reader.IsDBNull(reader.GetOrdinal("partner")))
  541. {
  542. retval.Partner = UUID.Zero;
  543. }
  544. else
  545. {
  546. UUID tmp;
  547. UUID.TryParse((string)reader["partner"], out tmp);
  548. retval.Partner = tmp;
  549. }
  550. }
  551. else
  552. {
  553. return null;
  554. }
  555. return retval;
  556. }
  557. /// <summary>
  558. /// Reads an avatar appearence from an active data reader
  559. /// </summary>
  560. /// <param name="reader">An active database reader</param>
  561. /// <returns>An avatar appearence</returns>
  562. public AvatarAppearance readAppearanceRow(IDataReader reader)
  563. {
  564. AvatarAppearance appearance = null;
  565. if (reader.Read())
  566. {
  567. appearance = new AvatarAppearance();
  568. appearance.Owner = new UUID((string)reader["owner"]);
  569. appearance.Serial = Convert.ToInt32(reader["serial"]);
  570. appearance.VisualParams = (byte[])reader["visual_params"];
  571. appearance.Texture = new Primitive.TextureEntry((byte[])reader["texture"], 0, ((byte[])reader["texture"]).Length);
  572. appearance.AvatarHeight = (float)Convert.ToDouble(reader["avatar_height"]);
  573. appearance.BodyItem = new UUID((string)reader["body_item"]);
  574. appearance.BodyAsset = new UUID((string)reader["body_asset"]);
  575. appearance.SkinItem = new UUID((string)reader["skin_item"]);
  576. appearance.SkinAsset = new UUID((string)reader["skin_asset"]);
  577. appearance.HairItem = new UUID((string)reader["hair_item"]);
  578. appearance.HairAsset = new UUID((string)reader["hair_asset"]);
  579. appearance.EyesItem = new UUID((string)reader["eyes_item"]);
  580. appearance.EyesAsset = new UUID((string)reader["eyes_asset"]);
  581. appearance.ShirtItem = new UUID((string)reader["shirt_item"]);
  582. appearance.ShirtAsset = new UUID((string)reader["shirt_asset"]);
  583. appearance.PantsItem = new UUID((string)reader["pants_item"]);
  584. appearance.PantsAsset = new UUID((string)reader["pants_asset"]);
  585. appearance.ShoesItem = new UUID((string)reader["shoes_item"]);
  586. appearance.ShoesAsset = new UUID((string)reader["shoes_asset"]);
  587. appearance.SocksItem = new UUID((string)reader["socks_item"]);
  588. appearance.SocksAsset = new UUID((string)reader["socks_asset"]);
  589. appearance.JacketItem = new UUID((string)reader["jacket_item"]);
  590. appearance.JacketAsset = new UUID((string)reader["jacket_asset"]);
  591. appearance.GlovesItem = new UUID((string)reader["gloves_item"]);
  592. appearance.GlovesAsset = new UUID((string)reader["gloves_asset"]);
  593. appearance.UnderShirtItem = new UUID((string)reader["undershirt_item"]);
  594. appearance.UnderShirtAsset = new UUID((string)reader["undershirt_asset"]);
  595. appearance.UnderPantsItem = new UUID((string)reader["underpants_item"]);
  596. appearance.UnderPantsAsset = new UUID((string)reader["underpants_asset"]);
  597. appearance.SkirtItem = new UUID((string)reader["skirt_item"]);
  598. appearance.SkirtAsset = new UUID((string)reader["skirt_asset"]);
  599. }
  600. return appearance;
  601. }
  602. // Read attachment list from data reader
  603. public Hashtable readAttachments(IDataReader r)
  604. {
  605. Hashtable ret = new Hashtable();
  606. while (r.Read())
  607. {
  608. int attachpoint = Convert.ToInt32(r["attachpoint"]);
  609. if (ret.ContainsKey(attachpoint))
  610. continue;
  611. Hashtable item = new Hashtable();
  612. item.Add("item", r["item"].ToString());
  613. item.Add("asset", r["asset"].ToString());
  614. ret.Add(attachpoint, item);
  615. }
  616. r.Close();
  617. return ret;
  618. }
  619. /// <summary>
  620. /// Inserts a new row into the log database
  621. /// </summary>
  622. /// <param name="serverDaemon">The daemon which triggered this event</param>
  623. /// <param name="target">Who were we operating on when this occured (region UUID, user UUID, etc)</param>
  624. /// <param name="methodCall">The method call where the problem occured</param>
  625. /// <param name="arguments">The arguments passed to the method</param>
  626. /// <param name="priority">How critical is this?</param>
  627. /// <param name="logMessage">Extra message info</param>
  628. /// <returns>Saved successfully?</returns>
  629. public bool insertLogRow(string serverDaemon, string target, string methodCall, string arguments, int priority,
  630. string logMessage)
  631. {
  632. string sql = "INSERT INTO logs (`target`, `server`, `method`, `arguments`, `priority`, `message`) VALUES ";
  633. sql += "(?target, ?server, ?method, ?arguments, ?priority, ?message)";
  634. Dictionary<string, object> parameters = new Dictionary<string, object>();
  635. parameters["?server"] = serverDaemon;
  636. parameters["?target"] = target;
  637. parameters["?method"] = methodCall;
  638. parameters["?arguments"] = arguments;
  639. parameters["?priority"] = priority.ToString();
  640. parameters["?message"] = logMessage;
  641. bool returnval = false;
  642. try
  643. {
  644. IDbCommand result = Query(sql, parameters);
  645. if (result.ExecuteNonQuery() == 1)
  646. returnval = true;
  647. result.Dispose();
  648. }
  649. catch (Exception e)
  650. {
  651. m_log.Error(e.ToString());
  652. return false;
  653. }
  654. return returnval;
  655. }
  656. /// <summary>
  657. /// Creates a new user and inserts it into the database
  658. /// </summary>
  659. /// <param name="uuid">User ID</param>
  660. /// <param name="username">First part of the login</param>
  661. /// <param name="lastname">Second part of the login</param>
  662. /// <param name="passwordHash">A salted hash of the users password</param>
  663. /// <param name="passwordSalt">The salt used for the password hash</param>
  664. /// <param name="homeRegion">A regionHandle of the users home region</param>
  665. /// <param name="homeRegionID"> The UUID of the user's home region</param>
  666. /// <param name="homeLocX">Home region position vector</param>
  667. /// <param name="homeLocY">Home region position vector</param>
  668. /// <param name="homeLocZ">Home region position vector</param>
  669. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  670. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  671. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  672. /// <param name="created">Account created (unix timestamp)</param>
  673. /// <param name="lastlogin">Last login (unix timestamp)</param>
  674. /// <param name="inventoryURI">Users inventory URI</param>
  675. /// <param name="assetURI">Users asset URI</param>
  676. /// <param name="canDoMask">I can do mask</param>
  677. /// <param name="wantDoMask">I want to do mask</param>
  678. /// <param name="aboutText">Profile text</param>
  679. /// <param name="firstText">Firstlife text</param>
  680. /// <param name="profileImage">UUID for profile image</param>
  681. /// <param name="firstImage">UUID for firstlife image</param>
  682. /// <param name="webLoginKey">Ignored</param>
  683. /// <returns>Success?</returns>
  684. public bool insertUserRow(UUID uuid, string username, string lastname, string email, string passwordHash,
  685. string passwordSalt, UInt64 homeRegion, UUID homeRegionID, float homeLocX, float homeLocY, float homeLocZ,
  686. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  687. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  688. string aboutText, string firstText,
  689. UUID profileImage, UUID firstImage, UUID webLoginKey, int userFlags, int godLevel, string customType, UUID partner)
  690. {
  691. m_log.Debug("[MySQLManager]: Fetching profile for " + uuid.ToString());
  692. string sql =
  693. "INSERT INTO users (`UUID`, `username`, `lastname`, `email`, `passwordHash`, `passwordSalt`, `homeRegion`, `homeRegionID`, ";
  694. sql +=
  695. "`homeLocationX`, `homeLocationY`, `homeLocationZ`, `homeLookAtX`, `homeLookAtY`, `homeLookAtZ`, `created`, ";
  696. sql +=
  697. "`lastLogin`, `userInventoryURI`, `userAssetURI`, `profileCanDoMask`, `profileWantDoMask`, `profileAboutText`, ";
  698. sql += "`profileFirstText`, `profileImage`, `profileFirstImage`, `webLoginKey`, `userFlags`, `godLevel`, `customType`, `partner`) VALUES ";
  699. sql += "(?UUID, ?username, ?lastname, ?email, ?passwordHash, ?passwordSalt, ?homeRegion, ?homeRegionID, ";
  700. sql +=
  701. "?homeLocationX, ?homeLocationY, ?homeLocationZ, ?homeLookAtX, ?homeLookAtY, ?homeLookAtZ, ?created, ";
  702. sql +=
  703. "?lastLogin, ?userInventoryURI, ?userAssetURI, ?profileCanDoMask, ?profileWantDoMask, ?profileAboutText, ";
  704. sql += "?profileFirstText, ?profileImage, ?profileFirstImage, ?webLoginKey, ?userFlags, ?godLevel, ?customType, ?partner)";
  705. Dictionary<string, object> parameters = new Dictionary<string, object>();
  706. parameters["?UUID"] = uuid.ToString();
  707. parameters["?username"] = username;
  708. parameters["?lastname"] = lastname;
  709. parameters["?email"] = email;
  710. parameters["?passwordHash"] = passwordHash;
  711. parameters["?passwordSalt"] = passwordSalt;
  712. parameters["?homeRegion"] = homeRegion;
  713. parameters["?homeRegionID"] = homeRegionID.ToString();
  714. parameters["?homeLocationX"] = homeLocX;
  715. parameters["?homeLocationY"] = homeLocY;
  716. parameters["?homeLocationZ"] = homeLocZ;
  717. parameters["?homeLookAtX"] = homeLookAtX;
  718. parameters["?homeLookAtY"] = homeLookAtY;
  719. parameters["?homeLookAtZ"] = homeLookAtZ;
  720. parameters["?created"] = created;
  721. parameters["?lastLogin"] = lastlogin;
  722. parameters["?userInventoryURI"] = inventoryURI;
  723. parameters["?userAssetURI"] = assetURI;
  724. parameters["?profileCanDoMask"] = canDoMask;
  725. parameters["?profileWantDoMask"] = wantDoMask;
  726. parameters["?profileAboutText"] = aboutText;
  727. parameters["?profileFirstText"] = firstText;
  728. parameters["?profileImage"] = profileImage.ToString();
  729. parameters["?profileFirstImage"] = firstImage.ToString();
  730. parameters["?webLoginKey"] = webLoginKey.ToString();
  731. parameters["?userFlags"] = userFlags;
  732. parameters["?godLevel"] = godLevel;
  733. parameters["?customType"] = customType == null ? "" : customType;
  734. parameters["?partner"] = partner.ToString();
  735. bool returnval = false;
  736. try
  737. {
  738. IDbCommand result = Query(sql, parameters);
  739. if (result.ExecuteNonQuery() == 1)
  740. returnval = true;
  741. result.Dispose();
  742. }
  743. catch (Exception e)
  744. {
  745. m_log.Error(e.ToString());
  746. return false;
  747. }
  748. //m_log.Debug("[MySQLManager]: Fetch user retval == " + returnval.ToString());
  749. return returnval;
  750. }
  751. /// <summary>
  752. /// Update user data into the database where User ID = uuid
  753. /// </summary>
  754. /// <param name="uuid">User ID</param>
  755. /// <param name="username">First part of the login</param>
  756. /// <param name="lastname">Second part of the login</param>
  757. /// <param name="passwordHash">A salted hash of the users password</param>
  758. /// <param name="passwordSalt">The salt used for the password hash</param>
  759. /// <param name="homeRegion">A regionHandle of the users home region</param>
  760. /// <param name="homeLocX">Home region position vector</param>
  761. /// <param name="homeLocY">Home region position vector</param>
  762. /// <param name="homeLocZ">Home region position vector</param>
  763. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  764. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  765. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  766. /// <param name="created">Account created (unix timestamp)</param>
  767. /// <param name="lastlogin">Last login (unix timestamp)</param>
  768. /// <param name="inventoryURI">Users inventory URI</param>
  769. /// <param name="assetURI">Users asset URI</param>
  770. /// <param name="canDoMask">I can do mask</param>
  771. /// <param name="wantDoMask">I want to do mask</param>
  772. /// <param name="aboutText">Profile text</param>
  773. /// <param name="firstText">Firstlife text</param>
  774. /// <param name="profileImage">UUID for profile image</param>
  775. /// <param name="firstImage">UUID for firstlife image</param>
  776. /// <param name="webLoginKey">UUID for weblogin Key</param>
  777. /// <returns>Success?</returns>
  778. public bool updateUserRow(UUID uuid, string username, string lastname, string email, string passwordHash,
  779. string passwordSalt, UInt64 homeRegion, UUID homeRegionID, float homeLocX, float homeLocY, float homeLocZ,
  780. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  781. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  782. string aboutText, string firstText,
  783. UUID profileImage, UUID firstImage, UUID webLoginKey, int userFlags, int godLevel, string customType, UUID partner)
  784. {
  785. string sql = "UPDATE users SET `username` = ?username , `lastname` = ?lastname, `email` = ?email ";
  786. sql += ", `passwordHash` = ?passwordHash , `passwordSalt` = ?passwordSalt , ";
  787. sql += "`homeRegion` = ?homeRegion , `homeRegionID` = ?homeRegionID, `homeLocationX` = ?homeLocationX , ";
  788. sql += "`homeLocationY` = ?homeLocationY , `homeLocationZ` = ?homeLocationZ , ";
  789. sql += "`homeLookAtX` = ?homeLookAtX , `homeLookAtY` = ?homeLookAtY , ";
  790. sql += "`homeLookAtZ` = ?homeLookAtZ , `created` = ?created , `lastLogin` = ?lastLogin , ";
  791. sql += "`userInventoryURI` = ?userInventoryURI , `userAssetURI` = ?userAssetURI , ";
  792. sql += "`profileCanDoMask` = ?profileCanDoMask , `profileWantDoMask` = ?profileWantDoMask , ";
  793. sql += "`profileAboutText` = ?profileAboutText , `profileFirstText` = ?profileFirstText, ";
  794. sql += "`profileImage` = ?profileImage , `profileFirstImage` = ?profileFirstImage , ";
  795. sql += "`userFlags` = ?userFlags , `godLevel` = ?godLevel , ";
  796. sql += "`customType` = ?customType , `partner` = ?partner , ";
  797. sql += "`webLoginKey` = ?webLoginKey WHERE UUID = ?UUID";
  798. Dictionary<string, object> parameters = new Dictionary<string, object>();
  799. parameters["?UUID"] = uuid.ToString();
  800. parameters["?username"] = username;
  801. parameters["?lastname"] = lastname;
  802. parameters["?email"] = email;
  803. parameters["?passwordHash"] = passwordHash;
  804. parameters["?passwordSalt"] = passwordSalt;
  805. parameters["?homeRegion"] = homeRegion;
  806. parameters["?homeRegionID"] = homeRegionID.ToString();
  807. parameters["?homeLocationX"] = homeLocX;
  808. parameters["?homeLocationY"] = homeLocY;
  809. parameters["?homeLocationZ"] = homeLocZ;
  810. parameters["?homeLookAtX"] = homeLookAtX;
  811. parameters["?homeLookAtY"] = homeLookAtY;
  812. parameters["?homeLookAtZ"] = homeLookAtZ;
  813. parameters["?created"] = created;
  814. parameters["?lastLogin"] = lastlogin;
  815. parameters["?userInventoryURI"] = inventoryURI;
  816. parameters["?userAssetURI"] = assetURI;
  817. parameters["?profileCanDoMask"] = canDoMask;
  818. parameters["?profileWantDoMask"] = wantDoMask;
  819. parameters["?profileAboutText"] = aboutText;
  820. parameters["?profileFirstText"] = firstText;
  821. parameters["?profileImage"] = profileImage.ToString();
  822. parameters["?profileFirstImage"] = firstImage.ToString();
  823. parameters["?webLoginKey"] = webLoginKey.ToString();
  824. parameters["?userFlags"] = userFlags;
  825. parameters["?godLevel"] = godLevel;
  826. parameters["?customType"] = customType == null ? "" : customType;
  827. parameters["?partner"] = partner.ToString();
  828. bool returnval = false;
  829. try
  830. {
  831. IDbCommand result = Query(sql, parameters);
  832. if (result.ExecuteNonQuery() == 1)
  833. returnval = true;
  834. result.Dispose();
  835. }
  836. catch (Exception e)
  837. {
  838. m_log.Error(e.ToString());
  839. return false;
  840. }
  841. //m_log.Debug("[MySQLManager]: update user retval == " + returnval.ToString());
  842. return returnval;
  843. }
  844. /// <summary>
  845. /// Inserts a new region into the database
  846. /// </summary>
  847. /// <param name="regiondata">The region to insert</param>
  848. /// <returns>Success?</returns>
  849. public bool insertRegion(RegionProfileData regiondata)
  850. {
  851. bool GRID_ONLY_UPDATE_NECESSARY_DATA = false;
  852. string sql = String.Empty;
  853. if (GRID_ONLY_UPDATE_NECESSARY_DATA)
  854. {
  855. sql += "INSERT INTO ";
  856. }
  857. else
  858. {
  859. sql += "REPLACE INTO ";
  860. }
  861. sql += "regions (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  862. sql +=
  863. "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  864. // part of an initial brutish effort to provide accurate information (as per the xml region spec)
  865. // wrt the ownership of a given region
  866. // the (very bad) assumption is that this value is being read and handled inconsistently or
  867. // not at all. Current strategy is to put the code in place to support the validity of this information
  868. // and to roll forward debugging any issues from that point
  869. //
  870. // this particular section of the mod attempts to implement the commit of a supplied value
  871. // server for the UUID of the region's owner (master avatar). It consists of the addition of the column and value to the relevant sql,
  872. // as well as the related parameterization
  873. sql +=
  874. "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey, regionMapTexture, serverHttpPort, serverRemotingPort, owner_uuid, originUUID) VALUES ";
  875. sql += "(?regionHandle, ?regionName, ?uuid, ?regionRecvKey, ?regionSecret, ?regionSendKey, ?regionDataURI, ";
  876. sql +=
  877. "?serverIP, ?serverPort, ?serverURI, ?locX, ?locY, ?locZ, ?eastOverrideHandle, ?westOverrideHandle, ?southOverrideHandle, ?northOverrideHandle, ?regionAssetURI, ?regionAssetRecvKey, ";
  878. sql +=
  879. "?regionAssetSendKey, ?regionUserURI, ?regionUserRecvKey, ?regionUserSendKey, ?regionMapTexture, ?serverHttpPort, ?serverRemotingPort, ?owner_uuid, ?originUUID)";
  880. if (GRID_ONLY_UPDATE_NECESSARY_DATA)
  881. {
  882. sql += "ON DUPLICATE KEY UPDATE serverIP = ?serverIP, serverPort = ?serverPort, serverURI = ?serverURI, owner_uuid - ?owner_uuid;";
  883. }
  884. else
  885. {
  886. sql += ";";
  887. }
  888. Dictionary<string, object> parameters = new Dictionary<string, object>();
  889. parameters["?regionHandle"] = regiondata.regionHandle.ToString();
  890. parameters["?regionName"] = regiondata.regionName.ToString();
  891. parameters["?uuid"] = regiondata.UUID.ToString();
  892. parameters["?regionRecvKey"] = regiondata.regionRecvKey.ToString();
  893. parameters["?regionSecret"] = regiondata.regionSecret.ToString();
  894. parameters["?regionSendKey"] = regiondata.regionSendKey.ToString();
  895. parameters["?regionDataURI"] = regiondata.regionDataURI.ToString();
  896. parameters["?serverIP"] = regiondata.serverIP.ToString();
  897. parameters["?serverPort"] = regiondata.serverPort.ToString();
  898. parameters["?serverURI"] = regiondata.serverURI.ToString();
  899. parameters["?locX"] = regiondata.regionLocX.ToString();
  900. parameters["?locY"] = regiondata.regionLocY.ToString();
  901. parameters["?locZ"] = regiondata.regionLocZ.ToString();
  902. parameters["?eastOverrideHandle"] = regiondata.regionEastOverrideHandle.ToString();
  903. parameters["?westOverrideHandle"] = regiondata.regionWestOverrideHandle.ToString();
  904. parameters["?northOverrideHandle"] = regiondata.regionNorthOverrideHandle.ToString();
  905. parameters["?southOverrideHandle"] = regiondata.regionSouthOverrideHandle.ToString();
  906. parameters["?regionAssetURI"] = regiondata.regionAssetURI.ToString();
  907. parameters["?regionAssetRecvKey"] = regiondata.regionAssetRecvKey.ToString();
  908. parameters["?regionAssetSendKey"] = regiondata.regionAssetSendKey.ToString();
  909. parameters["?regionUserURI"] = regiondata.regionUserURI.ToString();
  910. parameters["?regionUserRecvKey"] = regiondata.regionUserRecvKey.ToString();
  911. parameters["?regionUserSendKey"] = regiondata.regionUserSendKey.ToString();
  912. parameters["?regionMapTexture"] = regiondata.regionMapTextureID.ToString();
  913. parameters["?serverHttpPort"] = regiondata.httpPort.ToString();
  914. parameters["?serverRemotingPort"] = regiondata.remotingPort.ToString();
  915. parameters["?owner_uuid"] = regiondata.owner_uuid.ToString();
  916. parameters["?originUUID"] = regiondata.originUUID.ToString();
  917. bool returnval = false;
  918. try
  919. {
  920. IDbCommand result = Query(sql, parameters);
  921. // int x;
  922. // if ((x = result.ExecuteNonQuery()) > 0)
  923. // {
  924. // returnval = true;
  925. // }
  926. if (result.ExecuteNonQuery() > 0)
  927. {
  928. returnval = true;
  929. }
  930. result.Dispose();
  931. }
  932. catch (Exception e)
  933. {
  934. m_log.Error(e.ToString());
  935. return false;
  936. }
  937. return returnval;
  938. }
  939. /// <summary>
  940. /// Delete a region from the database
  941. /// </summary>
  942. /// <param name="uuid">The region to delete</param>
  943. /// <returns>Success?</returns>
  944. //public bool deleteRegion(RegionProfileData regiondata)
  945. public bool deleteRegion(string uuid)
  946. {
  947. bool returnval = false;
  948. string sql = "DELETE FROM regions WHERE uuid = ?uuid;";
  949. Dictionary<string, object> parameters = new Dictionary<string, object>();
  950. try
  951. {
  952. parameters["?uuid"] = uuid;
  953. IDbCommand result = Query(sql, parameters);
  954. // int x;
  955. // if ((x = result.ExecuteNonQuery()) > 0)
  956. // {
  957. // returnval = true;
  958. // }
  959. if (result.ExecuteNonQuery() > 0)
  960. {
  961. returnval = true;
  962. }
  963. result.Dispose();
  964. }
  965. catch (Exception e)
  966. {
  967. m_log.Error(e.ToString());
  968. return false;
  969. }
  970. return returnval;
  971. }
  972. /// <summary>
  973. /// Creates a new agent and inserts it into the database
  974. /// </summary>
  975. /// <param name="agentdata">The agent data to be inserted</param>
  976. /// <returns>Success?</returns>
  977. public bool insertAgentRow(UserAgentData agentdata)
  978. {
  979. string sql = String.Empty;
  980. sql += "REPLACE INTO ";
  981. sql += "agents (UUID, sessionID, secureSessionID, agentIP, agentPort, agentOnline, loginTime, logoutTime, currentRegion, currentHandle, currentPos, currentLookAt) VALUES ";
  982. sql += "(?UUID, ?sessionID, ?secureSessionID, ?agentIP, ?agentPort, ?agentOnline, ?loginTime, ?logoutTime, ?currentRegion, ?currentHandle, ?currentPos, ?currentLookAt);";
  983. Dictionary<string, object> parameters = new Dictionary<string, object>();
  984. parameters["?UUID"] = agentdata.ProfileID.ToString();
  985. parameters["?sessionID"] = agentdata.SessionID.ToString();
  986. parameters["?secureSessionID"] = agentdata.SecureSessionID.ToString();
  987. parameters["?agentIP"] = agentdata.AgentIP.ToString();
  988. parameters["?agentPort"] = agentdata.AgentPort.ToString();
  989. parameters["?agentOnline"] = (agentdata.AgentOnline == true) ? "1" : "0";
  990. parameters["?loginTime"] = agentdata.LoginTime.ToString();
  991. parameters["?logoutTime"] = agentdata.LogoutTime.ToString();
  992. parameters["?currentRegion"] = agentdata.Region.ToString();
  993. parameters["?currentHandle"] = agentdata.Handle.ToString();
  994. parameters["?currentPos"] = "<" + (agentdata.Position.X).ToString().Replace(",", ".") + "," + (agentdata.Position.Y).ToString().Replace(",", ".") + "," + (agentdata.Position.Z).ToString().Replace(",", ".") + ">";
  995. parameters["?currentLookAt"] = "<" + (agentdata.LookAt.X).ToString().Replace(",", ".") + "," + (agentdata.LookAt.Y).ToString().Replace(",", ".") + "," + (agentdata.LookAt.Z).ToString().Replace(",", ".") + ">";
  996. bool returnval = false;
  997. try
  998. {
  999. IDbCommand result = Query(sql, parameters);
  1000. // int x;
  1001. // if ((x = result.ExecuteNonQuery()) > 0)
  1002. // {
  1003. // returnval = true;
  1004. // }
  1005. if (result.ExecuteNonQuery() > 0)
  1006. {
  1007. returnval = true;
  1008. }
  1009. result.Dispose();
  1010. }
  1011. catch (Exception e)
  1012. {
  1013. m_log.Error(e.ToString());
  1014. return false;
  1015. }
  1016. return returnval;
  1017. }
  1018. /// <summary>
  1019. /// Create (or replace if existing) an avatar appearence
  1020. /// </summary>
  1021. /// <param name="appearance"></param>
  1022. /// <returns>Succes?</returns>
  1023. public bool insertAppearanceRow(AvatarAppearance appearance)
  1024. {
  1025. string sql = String.Empty;
  1026. sql += "REPLACE INTO ";
  1027. sql += "avatarappearance (owner, serial, visual_params, texture, avatar_height, ";
  1028. sql += "body_item, body_asset, skin_item, skin_asset, hair_item, hair_asset, eyes_item, eyes_asset, ";
  1029. sql += "shirt_item, shirt_asset, pants_item, pants_asset, shoes_item, shoes_asset, socks_item, socks_asset, ";
  1030. sql += "jacket_item, jacket_asset, gloves_item, gloves_asset, undershirt_item, undershirt_asset, underpants_item, underpants_asset, ";
  1031. sql += "skirt_item, skirt_asset) values (";
  1032. sql += "?owner, ?serial, ?visual_params, ?texture, ?avatar_height, ";
  1033. sql += "?body_item, ?body_asset, ?skin_item, ?skin_asset, ?hair_item, ?hair_asset, ?eyes_item, ?eyes_asset, ";
  1034. sql += "?shirt_item, ?shirt_asset, ?pants_item, ?pants_asset, ?shoes_item, ?shoes_asset, ?socks_item, ?socks_asset, ";
  1035. sql += "?jacket_item, ?jacket_asset, ?gloves_item, ?gloves_asset, ?undershirt_item, ?undershirt_asset, ?underpants_item, ?underpants_asset, ";
  1036. sql += "?skirt_item, ?skirt_asset)";
  1037. bool returnval = false;
  1038. // we want to send in byte data, which means we can't just pass down strings
  1039. try {
  1040. MySqlCommand cmd = (MySqlCommand) dbcon.CreateCommand();
  1041. cmd.CommandText = sql;
  1042. cmd.Parameters.AddWithValue("?owner", appearance.Owner.ToString());
  1043. cmd.Parameters.AddWithValue("?serial", appearance.Serial);
  1044. cmd.Parameters.AddWithValue("?visual_params", appearance.VisualParams);
  1045. cmd.Parameters.AddWithValue("?texture", appearance.Texture.ToBytes());
  1046. cmd.Parameters.AddWithValue("?avatar_height", appearance.AvatarHeight);
  1047. cmd.Parameters.AddWithValue("?body_item", appearance.BodyItem.ToString());
  1048. cmd.Parameters.AddWithValue("?body_asset", appearance.BodyAsset.ToString());
  1049. cmd.Parameters.AddWithValue("?skin_item", appearance.SkinItem.ToString());
  1050. cmd.Parameters.AddWithValue("?skin_asset", appearance.SkinAsset.ToString());
  1051. cmd.Parameters.AddWithValue("?hair_item", appearance.HairItem.ToString());
  1052. cmd.Parameters.AddWithValue("?hair_asset", appearance.HairAsset.ToString());
  1053. cmd.Parameters.AddWithValue("?eyes_item", appearance.EyesItem.ToString());
  1054. cmd.Parameters.AddWithValue("?eyes_asset", appearance.EyesAsset.ToString());
  1055. cmd.Parameters.AddWithValue("?shirt_item", appearance.ShirtItem.ToString());
  1056. cmd.Parameters.AddWithValue("?shirt_asset", appearance.ShirtAsset.ToString());
  1057. cmd.Parameters.AddWithValue("?pants_item", appearance.PantsItem.ToString());
  1058. cmd.Parameters.AddWithValue("?pants_asset", appearance.PantsAsset.ToString());
  1059. cmd.Parameters.AddWithValue("?shoes_item", appearance.ShoesItem.ToString());
  1060. cmd.Parameters.AddWithValue("?shoes_asset", appearance.ShoesAsset.ToString());
  1061. cmd.Parameters.AddWithValue("?socks_item", appearance.SocksItem.ToString());
  1062. cmd.Parameters.AddWithValue("?socks_asset", appearance.SocksAsset.ToString());
  1063. cmd.Parameters.AddWithValue("?jacket_item", appearance.JacketItem.ToString());
  1064. cmd.Parameters.AddWithValue("?jacket_asset", appearance.JacketAsset.ToString());
  1065. cmd.Parameters.AddWithValue("?gloves_item", appearance.GlovesItem.ToString());
  1066. cmd.Parameters.AddWithValue("?gloves_asset", appearance.GlovesAsset.ToString());
  1067. cmd.Parameters.AddWithValue("?undershirt_item", appearance.UnderShirtItem.ToString());
  1068. cmd.Parameters.AddWithValue("?undershirt_asset", appearance.UnderShirtAsset.ToString());
  1069. cmd.Parameters.AddWithValue("?underpants_item", appearance.UnderPantsItem.ToString());
  1070. cmd.Parameters.AddWithValue("?underpants_asset", appearance.UnderPantsAsset.ToString());
  1071. cmd.Parameters.AddWithValue("?skirt_item", appearance.SkirtItem.ToString());
  1072. cmd.Parameters.AddWithValue("?skirt_asset", appearance.SkirtAsset.ToString());
  1073. if (cmd.ExecuteNonQuery() > 0)
  1074. returnval = true;
  1075. cmd.Dispose();
  1076. }
  1077. catch (Exception e)
  1078. {
  1079. m_log.Error(e.ToString());
  1080. return false;
  1081. }
  1082. return returnval;
  1083. }
  1084. public void writeAttachments(UUID agentID, Hashtable data)
  1085. {
  1086. string sql = "delete from avatarattachments where UUID = ?uuid";
  1087. MySqlCommand cmd = (MySqlCommand) dbcon.CreateCommand();
  1088. cmd.CommandText = sql;
  1089. cmd.Parameters.AddWithValue("?uuid", agentID.ToString());
  1090. cmd.ExecuteNonQuery();
  1091. if (data == null)
  1092. return;
  1093. sql = "insert into avatarattachments (UUID, attachpoint, item, asset) values (?uuid, ?attachpoint, ?item, ?asset)";
  1094. cmd = (MySqlCommand) dbcon.CreateCommand();
  1095. cmd.CommandText = sql;
  1096. foreach (DictionaryEntry e in data)
  1097. {
  1098. int attachpoint = Convert.ToInt32(e.Key);
  1099. Hashtable item = (Hashtable)e.Value;
  1100. cmd.Parameters.Clear();
  1101. cmd.Parameters.AddWithValue("?uuid", agentID.ToString());
  1102. cmd.Parameters.AddWithValue("?attachpoint", attachpoint);
  1103. cmd.Parameters.AddWithValue("?item", item["item"]);
  1104. cmd.Parameters.AddWithValue("?asset", item["asset"]);
  1105. cmd.ExecuteNonQuery();
  1106. }
  1107. }
  1108. }
  1109. }