diff --git a/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs b/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs index a64d1959f6..2a383079f6 100644 --- a/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs +++ b/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs @@ -44,16 +44,16 @@ namespace OpenSim.Framework.Communications.Caches private IAssetReceiver _receiver; private BlockingQueue _assetRequests; private Thread _localAssetServerThread; - protected IAssetProvider m_plugin; + protected IAssetProvider m_assetProviderPlugin; private object syncLock = new object(); - public SQLAssetServer() + public SQLAssetServer(string pluginName) { - System.Console.WriteLine("Starting sqlite asset storage system"); - _assetRequests = new BlockingQueue(); - AddPlugin("OpenSim.Framework.Data.SQLite.dll"); - this.SetUpAssetDatabase(); + _assetRequests = new BlockingQueue(); + AddPlugin(pluginName); + + SetUpAssetDatabase(); this._localAssetServerThread = new Thread(new ThreadStart(RunRequests)); this._localAssetServerThread.IsBackground = true; @@ -63,7 +63,7 @@ namespace OpenSim.Framework.Communications.Caches public void AddPlugin(string FileName) { - //MainLog.Instance.Verbose("SQLAssetServer", "AssetStorage: Attempting to load " + FileName); + MainLog.Instance.Verbose("SQLAssetServer", "AssetStorage: Attempting to load " + FileName); Assembly pluginAssembly = Assembly.LoadFrom(FileName); foreach (Type pluginType in pluginAssembly.GetTypes()) @@ -75,10 +75,10 @@ namespace OpenSim.Framework.Communications.Caches if (typeInterface != null) { IAssetProvider plug = (IAssetProvider)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); - m_plugin = plug; - m_plugin.Initialise("AssetStorage.db", ""); - - //MainLog.Instance.Verbose("AssetStorage: Added IAssetProvider Interface"); + m_assetProviderPlugin = plug; + m_assetProviderPlugin.Initialise(); + + MainLog.Instance.Verbose("AssetStorage: Added " + m_assetProviderPlugin.Name + " " + m_assetProviderPlugin.Version); } typeInterface = null; @@ -105,8 +105,8 @@ namespace OpenSim.Framework.Communications.Caches { lock (syncLock) { - m_plugin.UpdateAsset(asset); - m_plugin.CommitAssets(); + m_assetProviderPlugin.UpdateAsset(asset); + m_assetProviderPlugin.CommitAssets(); } } @@ -114,8 +114,8 @@ namespace OpenSim.Framework.Communications.Caches { lock (syncLock) { - m_plugin.CreateAsset(asset); - m_plugin.CommitAssets(); + m_assetProviderPlugin.CreateAsset(asset); + m_assetProviderPlugin.CommitAssets(); } } @@ -125,7 +125,7 @@ namespace OpenSim.Framework.Communications.Caches } public void Close() { - m_plugin.CommitAssets(); + m_assetProviderPlugin.CommitAssets(); } private void RunRequests() @@ -140,7 +140,7 @@ namespace OpenSim.Framework.Communications.Caches AssetBase asset = null; lock (syncLock) { - asset = m_plugin.FetchAsset(req.AssetID); + asset = m_assetProviderPlugin.FetchAsset(req.AssetID); } if (asset != null) { @@ -163,67 +163,67 @@ namespace OpenSim.Framework.Communications.Caches Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001"); Image.Name = "Bricks"; this.LoadAsset(Image, true, "bricks.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002"); Image.Name = "Plywood"; this.LoadAsset(Image, true, "plywood.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003"); Image.Name = "Rocks"; this.LoadAsset(Image, true, "rocks.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004"); Image.Name = "Granite"; this.LoadAsset(Image, true, "granite.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005"); Image.Name = "Hardwood"; this.LoadAsset(Image, true, "hardwood.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005"); Image.Name = "Prim Base Texture"; this.LoadAsset(Image, true, "plywood.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000006"); Image.Name = "Map Base Texture"; this.LoadAsset(Image, true, "map_base.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000007"); Image.Name = "Map Texture"; this.LoadAsset(Image, true, "map1.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-1111-9999-000000000010"); Image.Name = "Female Body Texture"; this.LoadAsset(Image, true, "femalebody.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-1111-9999-000000000011"); Image.Name = "Female Bottom Texture"; this.LoadAsset(Image, true, "femalebottom.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-0000-1111-9999-000000000012"); Image.Name = "Female Face Texture"; this.LoadAsset(Image, true, "femaleface.jp2"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("77c41e39-38f9-f75a-024e-585989bbabbb"); @@ -231,7 +231,7 @@ namespace OpenSim.Framework.Communications.Caches Image.Type = 13; Image.InvType = 13; this.LoadAsset(Image, false, "base_skin.dat"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); @@ -239,7 +239,7 @@ namespace OpenSim.Framework.Communications.Caches Image.Type = 13; Image.InvType = 13; this.LoadAsset(Image, false, "base_shape.dat"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-38f9-1111-024e-222222111110"); @@ -247,7 +247,7 @@ namespace OpenSim.Framework.Communications.Caches Image.Type = 5; Image.InvType = 18; this.LoadAsset(Image, false, "newshirt.dat"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); Image = new AssetBase(); Image.FullID = new LLUUID("00000000-38f9-1111-024e-222222111120"); @@ -255,7 +255,7 @@ namespace OpenSim.Framework.Communications.Caches Image.Type = 5; Image.InvType = 18; this.LoadAsset(Image, false, "newpants.dat"); - m_plugin.CreateAsset(Image); + m_assetProviderPlugin.CreateAsset(Image); string filePath = Path.Combine(Util.configDir(), "OpenSimAssetSet.xml"); if (File.Exists(filePath)) @@ -264,7 +264,7 @@ namespace OpenSim.Framework.Communications.Caches ReadAssetDetails(source); } - m_plugin.CommitAssets(); + m_assetProviderPlugin.CommitAssets(); } protected void ReadAssetDetails(IConfigSource source) @@ -282,7 +282,7 @@ namespace OpenSim.Framework.Communications.Caches { MainLog.Instance.Verbose("Creating new asset: " + newAsset.Name); this.LoadAsset(newAsset, false, fileName); - m_plugin.CreateAsset(newAsset); + m_assetProviderPlugin.CreateAsset(newAsset); } } } diff --git a/OpenSim/Framework/Data.MySQL/MySQLAssetData.cs b/OpenSim/Framework/Data.MySQL/MySQLAssetData.cs new file mode 100644 index 0000000000..70e04b6fac --- /dev/null +++ b/OpenSim/Framework/Data.MySQL/MySQLAssetData.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using MySql.Data.MySqlClient; + +using libsecondlife; +using OpenSim.Framework.Console; +using OpenSim.Framework.Interfaces; +using OpenSim.Framework.Types; + +namespace OpenSim.Framework.Data.MySQL +{ + class MySQLAssetData : IAssetProvider + { + MySQLManager _dbConnection; + #region IAssetProvider Members + + private void UpgradeAssetsTable(string oldVersion) + { + // null as the version, indicates that the table didn't exist + if (oldVersion == null) + { + MainLog.Instance.Notice("ASSETS", "Creating new database tables"); + _dbConnection.ExecuteResourceSql("CreateAssetsTable.sql"); + return; + } + } + + /// + /// Ensure that the assets related tables exists and are at the latest version + /// + private void TestTables() + { + + Dictionary tableList = new Dictionary(); + + tableList["assets"] = null; + _dbConnection.GetTableVersion(tableList); + + UpgradeAssetsTable(tableList["assets"]); + + } + + public AssetBase FetchAsset(LLUUID uuid) + { + throw new Exception("The method or operation is not implemented."); + } + + public void CreateAsset(AssetBase asset) + { + MySqlCommand cmd = new MySqlCommand("REPLACE INTO assets(id, name, description, assetType, invType, local, temporary, data)" + + "VALUES(?id, ?name, ?description, ?assetType, ?invType, ?local, ?temporary, ?data)", _dbConnection.Connection); + MySqlParameter p = cmd.Parameters.Add("?id", MySqlDbType.Binary, 16); + p.Value = asset.FullID.GetBytes(); + cmd.Parameters.AddWithValue("?name", asset.Name); + cmd.Parameters.AddWithValue("?description", asset.Description); + cmd.Parameters.AddWithValue("?assetType", asset.Type); + cmd.Parameters.AddWithValue("?invType", asset.InvType); + cmd.Parameters.AddWithValue("?local", asset.Local); + cmd.Parameters.AddWithValue("?temporary", asset.Temporary); + cmd.Parameters.AddWithValue("?data", asset.Data); + cmd.ExecuteNonQuery(); + } + + public void UpdateAsset(AssetBase asset) + { + CreateAsset(asset); + } + + public bool ExistsAsset(LLUUID uuid) + { + throw new Exception("The method or operation is not implemented."); + } + + /// + /// All writes are immediately commited to the database, so this is a no-op + /// + public void CommitAssets() + { + } + + #endregion + + #region IPlugin Members + + public void Initialise() + { + IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini"); + string hostname = GridDataMySqlFile.ParseFileReadValue("hostname"); + string database = GridDataMySqlFile.ParseFileReadValue("database"); + string username = GridDataMySqlFile.ParseFileReadValue("username"); + string password = GridDataMySqlFile.ParseFileReadValue("password"); + string pooling = GridDataMySqlFile.ParseFileReadValue("pooling"); + string port = GridDataMySqlFile.ParseFileReadValue("port"); + + _dbConnection = new MySQLManager(hostname, database, username, password, pooling, port); + + TestTables(); + } + + public string Version + { + get { return _dbConnection.getVersion(); } + } + + public string Name + { + get { return "MySQL Asset storage engine"; } + } + + #endregion + } +} diff --git a/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs b/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs index 804fd5f8ce..6423f280a7 100644 --- a/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs +++ b/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs @@ -28,7 +28,6 @@ using System; using System.IO; using System.Data; -using System.Reflection; using System.Collections.Generic; using libsecondlife; using OpenSim.Framework.Types; @@ -65,41 +64,13 @@ namespace OpenSim.Framework.Data.MySQL } #region Test and initialization code - /// - /// Extract a named string resource from the embedded resources - /// - /// name of embedded resource - /// string contained within the embedded resource - private string getResourceString(string name) - { - Assembly assem = this.GetType().Assembly; - string[] names = assem.GetManifestResourceNames(); - foreach(string s in names) - if(s.EndsWith(name)) - using (Stream resource = assem.GetManifestResourceStream(s)) - { - using (StreamReader resourceReader = new StreamReader(resource)) - { - string resourceString = resourceReader.ReadToEnd(); - return resourceString; - } - } - throw new Exception(string.Format("Resource '{0}' was not found", name)); - } - - private void ExecuteResourceSql(MySqlConnection conn, string name) - { - MySqlCommand cmd = new MySqlCommand(getResourceString(name), conn); - cmd.ExecuteNonQuery(); - } - - private void UpgradeFoldersTable(MySqlConnection conn, string oldVersion) + private void UpgradeFoldersTable(string oldVersion) { // null as the version, indicates that the table didn't exist if (oldVersion == null) { - ExecuteResourceSql(conn, "CreateFoldersTable.sql"); + database.ExecuteResourceSql("CreateFoldersTable.sql"); return; } @@ -107,15 +78,15 @@ namespace OpenSim.Framework.Data.MySQL if (oldVersion == "Rev. 2") return; - ExecuteResourceSql(conn, "UpgradeFoldersTableToVersion2.sql"); + database.ExecuteResourceSql("UpgradeFoldersTableToVersion2.sql"); } - private void UpgradeItemsTable(MySqlConnection conn, string oldVersion) + private void UpgradeItemsTable(string oldVersion) { // null as the version, indicates that the table didn't exist if (oldVersion == null) { - ExecuteResourceSql(conn, "CreateItemsTable.sql"); + database.ExecuteResourceSql("CreateItemsTable.sql"); return; } @@ -123,7 +94,7 @@ namespace OpenSim.Framework.Data.MySQL if (oldVersion == "Rev. 2") return; - ExecuteResourceSql(conn, "UpgradeItemsTableToVersion2.sql"); + database.ExecuteResourceSql("UpgradeItemsTableToVersion2.sql"); } private void TestTables(MySqlConnection conn) @@ -134,25 +105,10 @@ namespace OpenSim.Framework.Data.MySQL tableList["inventoryfolders"] = null; tableList["inventoryitems"] = null; - MySqlCommand tablesCmd = new MySqlCommand("SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='opensim'", conn); - MySqlDataReader tables = tablesCmd.ExecuteReader(); - while (tables.Read()) - { - try - { - string tableName = (string)tables["TABLE_NAME"]; - string comment = (string)tables["TABLE_COMMENT"]; - tableList[tableName] = comment; - } - catch (Exception e) - { - MainLog.Instance.Error(e.ToString()); - } - } - tables.Close(); + database.GetTableVersion(tableList); - UpgradeFoldersTable(conn, tableList["inventoryfolders"]); - UpgradeItemsTable(conn, tableList["inventoryitems"]); + UpgradeFoldersTable(tableList["inventoryfolders"]); + UpgradeItemsTable(tableList["inventoryitems"]); } #endregion @@ -179,12 +135,7 @@ namespace OpenSim.Framework.Data.MySQL /// A string containing the DB provider public string getVersion() { - System.Reflection.Module module = this.GetType().Module; - string dllName = module.Assembly.ManifestModule.Name; - Version dllVersion = module.Assembly.GetName().Version; - - - return string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, dllVersion.Revision); + return database.getVersion(); } /// @@ -201,7 +152,7 @@ namespace OpenSim.Framework.Data.MySQL List items = new List(); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); while(reader.Read()) @@ -233,8 +184,8 @@ namespace OpenSim.Framework.Data.MySQL lock (database) { MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", user.ToStringHyphenated()); - result.Parameters.Add("?zero", LLUUID.Zero.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", user.ToStringHyphenated()); + result.Parameters.AddWithValue("?zero", LLUUID.Zero.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); List items = new List(); @@ -267,13 +218,9 @@ namespace OpenSim.Framework.Data.MySQL { lock (database) { - Dictionary param = new Dictionary(); - param["?uuid"] = user.ToStringHyphenated(); - param["?zero"] = LLUUID.Zero.ToStringHyphenated(); - MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", user.ToStringHyphenated()); - result.Parameters.Add("?zero", LLUUID.Zero.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", user.ToStringHyphenated()); + result.Parameters.AddWithValue("?zero", LLUUID.Zero.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); @@ -308,7 +255,7 @@ namespace OpenSim.Framework.Data.MySQL lock (database) { MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", parentID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", parentID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); List items = new List(); @@ -378,7 +325,7 @@ namespace OpenSim.Framework.Data.MySQL Dictionary param = new Dictionary(); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", itemID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", itemID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); InventoryItemBase item = null; @@ -438,7 +385,7 @@ namespace OpenSim.Framework.Data.MySQL lock (database) { MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); reader.Read(); @@ -469,19 +416,19 @@ namespace OpenSim.Framework.Data.MySQL try { MySqlCommand result = new MySqlCommand(sql, database.Connection); - result.Parameters.Add("?inventoryID", item.inventoryID.ToStringHyphenated()); - result.Parameters.Add("?assetID", item.assetID.ToStringHyphenated()); - result.Parameters.Add("?assetType", item.assetType.ToString()); - result.Parameters.Add("?parentFolderID", item.parentFolderID.ToStringHyphenated()); - result.Parameters.Add("?avatarID", item.avatarID.ToStringHyphenated()); - result.Parameters.Add("?inventoryName", item.inventoryName); - result.Parameters.Add("?inventoryDescription", item.inventoryDescription); - result.Parameters.Add("?inventoryNextPermissions", item.inventoryNextPermissions.ToString()); - result.Parameters.Add("?inventoryCurrentPermissions", item.inventoryCurrentPermissions.ToString()); - result.Parameters.Add("?invType", item.invType); - result.Parameters.Add("?creatorID", item.creatorsID.ToStringHyphenated()); - result.Parameters.Add("?inventoryBasePermissions", item.inventoryBasePermissions); - result.Parameters.Add("?inventoryEveryOnePermissions", item.inventoryEveryOnePermissions); + result.Parameters.AddWithValue("?inventoryID", item.inventoryID.ToStringHyphenated()); + result.Parameters.AddWithValue("?assetID", item.assetID.ToStringHyphenated()); + result.Parameters.AddWithValue("?assetType", item.assetType.ToString()); + result.Parameters.AddWithValue("?parentFolderID", item.parentFolderID.ToStringHyphenated()); + result.Parameters.AddWithValue("?avatarID", item.avatarID.ToStringHyphenated()); + result.Parameters.AddWithValue("?inventoryName", item.inventoryName); + result.Parameters.AddWithValue("?inventoryDescription", item.inventoryDescription); + result.Parameters.AddWithValue("?inventoryNextPermissions", item.inventoryNextPermissions.ToString()); + result.Parameters.AddWithValue("?inventoryCurrentPermissions", item.inventoryCurrentPermissions.ToString()); + result.Parameters.AddWithValue("?invType", item.invType); + result.Parameters.AddWithValue("?creatorID", item.creatorsID.ToStringHyphenated()); + result.Parameters.AddWithValue("?inventoryBasePermissions", item.inventoryBasePermissions); + result.Parameters.AddWithValue("?inventoryEveryOnePermissions", item.inventoryEveryOnePermissions); result.ExecuteNonQuery(); result.Dispose(); } @@ -509,7 +456,7 @@ namespace OpenSim.Framework.Data.MySQL try { MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", database.Connection); - cmd.Parameters.Add("?uuid", itemID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?uuid", itemID.ToStringHyphenated()); cmd.ExecuteNonQuery(); } catch (MySqlException e) @@ -529,12 +476,12 @@ namespace OpenSim.Framework.Data.MySQL sql += "(?folderID, ?agentID, ?parentFolderID, ?folderName, ?type, ?version)"; MySqlCommand cmd = new MySqlCommand(sql, database.Connection); - cmd.Parameters.Add("?folderID", folder.folderID.ToStringHyphenated()); - cmd.Parameters.Add("?agentID", folder.agentID.ToStringHyphenated()); - cmd.Parameters.Add("?parentFolderID", folder.parentID.ToStringHyphenated()); - cmd.Parameters.Add("?folderName", folder.name); - cmd.Parameters.Add("?type", (short)folder.type); - cmd.Parameters.Add("?version", folder.version); + cmd.Parameters.AddWithValue("?folderID", folder.folderID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?agentID", folder.agentID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?parentFolderID", folder.parentID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?folderName", folder.name); + cmd.Parameters.AddWithValue("?type", (short)folder.type); + cmd.Parameters.AddWithValue("?version", folder.version); try { @@ -590,7 +537,7 @@ namespace OpenSim.Framework.Data.MySQL try { MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryfolders WHERE folderID=?uuid", database.Connection); - cmd.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); cmd.ExecuteNonQuery(); } catch (MySqlException e) @@ -605,7 +552,7 @@ namespace OpenSim.Framework.Data.MySQL try { MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE parentFolderID=?uuid", database.Connection); - cmd.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); cmd.ExecuteNonQuery(); } catch (MySqlException e) diff --git a/OpenSim/Framework/Data.MySQL/MySQLManager.cs b/OpenSim/Framework/Data.MySQL/MySQLManager.cs index ea174b2dca..d3f6976337 100644 --- a/OpenSim/Framework/Data.MySQL/MySQLManager.cs +++ b/OpenSim/Framework/Data.MySQL/MySQLManager.cs @@ -26,10 +26,14 @@ * */ using System; -using System.Collections.Generic; +using System.IO; using System.Data; +using System.Reflection; +using System.Collections.Generic; using libsecondlife; + using MySql.Data.MySqlClient; + using OpenSim.Framework.Types; using OpenSim.Framework.Console; @@ -113,6 +117,88 @@ namespace OpenSim.Framework.Data.MySQL } } + /// + /// Returns the version of this DB provider + /// + /// A string containing the DB provider + public string getVersion() + { + System.Reflection.Module module = this.GetType().Module; + string dllName = module.Assembly.ManifestModule.Name; + Version dllVersion = module.Assembly.GetName().Version; + + + return string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, dllVersion.Revision); + } + + + /// + /// Extract a named string resource from the embedded resources + /// + /// name of embedded resource + /// string contained within the embedded resource + private string getResourceString(string name) + { + Assembly assem = this.GetType().Assembly; + string[] names = assem.GetManifestResourceNames(); + + foreach (string s in names) + if (s.EndsWith(name)) + using (Stream resource = assem.GetManifestResourceStream(s)) + { + using (StreamReader resourceReader = new StreamReader(resource)) + { + string resourceString = resourceReader.ReadToEnd(); + return resourceString; + } + } + throw new Exception(string.Format("Resource '{0}' was not found", name)); + } + + /// + /// Execute a SQL statement stored in a resource, as a string + /// + /// + public void ExecuteResourceSql(string name) + { + MySqlCommand cmd = new MySqlCommand(getResourceString(name), dbcon); + cmd.ExecuteNonQuery(); + } + + /// + /// Given a list of tables, return the version of the tables, as seen in the database + /// + /// + public void GetTableVersion(Dictionary tableList) + { + lock (dbcon) + { + MySqlCommand tablesCmd = new MySqlCommand("SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?dbname", dbcon); + tablesCmd.Parameters.AddWithValue("?dbname", dbcon.Database); + using (MySqlDataReader tables = tablesCmd.ExecuteReader()) + { + while (tables.Read()) + { + try + { + string tableName = (string)tables["TABLE_NAME"]; + string comment = (string)tables["TABLE_COMMENT"]; + if(tableList.ContainsKey(tableName)) + tableList[tableName] = comment; + } + catch (Exception e) + { + MainLog.Instance.Error(e.ToString()); + } + } + tables.Close(); + } + } + } + + + // at some time this code should be cleaned up + /// /// Runs a query with protection against SQL Injection by using parameterised input. /// @@ -127,7 +213,7 @@ namespace OpenSim.Framework.Data.MySQL dbcommand.CommandText = sql; foreach (KeyValuePair param in parameters) { - dbcommand.Parameters.Add(param.Key, param.Value); + dbcommand.Parameters.AddWithValue(param.Key, param.Value); } return (IDbCommand)dbcommand; @@ -161,7 +247,7 @@ namespace OpenSim.Framework.Data.MySQL dbcommand.CommandText = sql; foreach (KeyValuePair param in parameters) { - dbcommand.Parameters.Add(param.Key, param.Value); + dbcommand.Parameters.AddWithValue(param.Key, param.Value); } return (IDbCommand)dbcommand; diff --git a/OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql b/OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql new file mode 100644 index 0000000000..049a3a26bb --- /dev/null +++ b/OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql @@ -0,0 +1,11 @@ +CREATE TABLE `assets` ( + `id` binary(16) NOT NULL, + `name` varchar(64) NOT NULL, + `description` varchar(64) NOT NULL, + `assetType` smallint(5) unsigned NOT NULL, + `invType` smallint(5) unsigned NOT NULL, + `local` tinyint(1) NOT NULL, + `temporary` tinyint(1) NOT NULL, + `data` longblob NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Rev. 1'; \ No newline at end of file diff --git a/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs b/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs index 4616275cbe..922d7145a6 100644 --- a/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs +++ b/OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs @@ -53,7 +53,7 @@ namespace OpenSim.Framework.Data.SQLite private const string assetSelect = "select * from assets"; private DataSet ds; private SqliteDataAdapter da; - + public void Initialise(string dbfile, string dbname) { SqliteConnection conn = new SqliteConnection("URI=file:" + dbfile + ",version=3"); @@ -275,5 +275,26 @@ namespace OpenSim.Framework.Data.SQLite return true; } + #region IPlugin interface + public string Version { + get + { + System.Reflection.Module module = this.GetType().Module; + string dllName = module.Assembly.ManifestModule.Name; + Version dllVersion = module.Assembly.GetName().Version; + + return string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, dllVersion.Revision); + } + } + + public void Initialise() + { + Initialise("AssetStorage.db", ""); + } + + public string Name { + get { return "SQLite Asset storage engine"; } + } + #endregion } } diff --git a/OpenSim/Framework/General/Configuration/AssetConfig.cs b/OpenSim/Framework/General/Configuration/AssetConfig.cs new file mode 100644 index 0000000000..e5f1c88b66 --- /dev/null +++ b/OpenSim/Framework/General/Configuration/AssetConfig.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenSim.Framework.Configuration +{ + /// + /// UserConfig -- For User Server Configuration + /// + public class AssetConfig + { + public string DefaultStartupMsg = ""; + + public string DatabaseProvider = ""; + + public uint HttpPort = 8003; + + private ConfigurationMember configMember; + + public AssetConfig(string description, string filename) + { + configMember = new ConfigurationMember(filename, description, this.loadConfigurationOptions, this.handleIncomingConfiguration); + configMember.performConfigurationRetrieve(); + } + + public void loadConfigurationOptions() + { + configMember.addConfigurationOption("default_startup_message", ConfigurationOption.ConfigurationTypes.TYPE_STRING_NOT_EMPTY, "Default Startup Message", "Welcome to OGS", false); + + configMember.addConfigurationOption("database_provider", ConfigurationOption.ConfigurationTypes.TYPE_STRING, "DLL for database provider", "OpenSim.Framework.Data.MySQL.dll", false); + + configMember.addConfigurationOption("http_port", ConfigurationOption.ConfigurationTypes.TYPE_UINT32, "Http Listener port", "8003", false); + + } + + public bool handleIncomingConfiguration(string configuration_key, object configuration_result) + { + switch (configuration_key) + { + case "default_startup_message": + this.DefaultStartupMsg = (string)configuration_result; + break; + case "database_provider": + this.DatabaseProvider = (string)configuration_result; + break; + case "http_port": + HttpPort = (uint)configuration_result; + break; + } + + return true; + } + } +} diff --git a/OpenSim/Framework/General/Interfaces/IAssetProvider.cs b/OpenSim/Framework/General/Interfaces/IAssetProvider.cs index daf9d6d395..0b39d1fd3f 100644 --- a/OpenSim/Framework/General/Interfaces/IAssetProvider.cs +++ b/OpenSim/Framework/General/Interfaces/IAssetProvider.cs @@ -6,9 +6,8 @@ using libsecondlife; namespace OpenSim.Framework.Interfaces { - public interface IAssetProvider + public interface IAssetProvider : IPlugin { - void Initialise(string dbfile, string dbname); AssetBase FetchAsset(LLUUID uuid); void CreateAsset(AssetBase asset); void UpdateAsset(AssetBase asset); diff --git a/OpenSim/Framework/General/Interfaces/IPlugin.cs b/OpenSim/Framework/General/Interfaces/IPlugin.cs new file mode 100644 index 0000000000..ceb8b63dff --- /dev/null +++ b/OpenSim/Framework/General/Interfaces/IPlugin.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenSim.Framework.Interfaces +{ + /// + /// This interface, describes a generic plugin + /// + public interface IPlugin + { + /// + /// Returns the plugin version + /// + /// Plugin version in MAJOR.MINOR.REVISION.BUILD format + string Version { get; } + + /// + /// Returns the plugin name + /// + /// Plugin name, eg MySQL User Provider + string Name { get; } + + /// + /// Initialises the plugin (artificial constructor) + /// + void Initialise(); + } +} diff --git a/OpenSim/Grid/AssetServer/Main.cs b/OpenSim/Grid/AssetServer/Main.cs index 6bdb9da69b..9272f24359 100644 --- a/OpenSim/Grid/AssetServer/Main.cs +++ b/OpenSim/Grid/AssetServer/Main.cs @@ -28,14 +28,21 @@ using System; using System.IO; -using System.Text; -using Db4objects.Db4o; + using libsecondlife; using OpenSim.Framework.Console; -using OpenSim.Framework.Types; using OpenSim.Framework.Servers; +using OpenSim.Framework.Configuration; +using OpenSim.Framework.Interfaces; using OpenSim.Framework.Utilities; - +using OpenSim.Framework.Communications.Caches; +/* +using System.Text; +using Db4objects.Db4o; +using OpenSim.Framework.Types; +using OpenSim.Framework.Utilities; +using OpenSim.Framework.Communications.Caches; +*/ namespace OpenSim.Grid.AssetServer { /// @@ -43,11 +50,11 @@ namespace OpenSim.Grid.AssetServer /// public class OpenAsset_Main : conscmd_callback { - private IObjectContainer db; - public static OpenAsset_Main assetserver; private LogBase m_console; + private AssetConfig m_config; + private IAssetServer m_assetServer; [STAThread] public static void Main(string[] args) @@ -82,8 +89,10 @@ namespace OpenSim.Grid.AssetServer public void Startup() { + m_config = new AssetConfig("ASSET SERVER", (Path.Combine(Util.configDir(), "AssetServer_Config.xml"))); + m_console.Verbose("ASSET", "Setting up asset DB"); - setupDB(); + setupDB(m_config); m_console.Verbose("ASSET", "Starting HTTP process"); BaseHttpServer httpServer = new BaseHttpServer(8003); @@ -97,180 +106,22 @@ namespace OpenSim.Grid.AssetServer public byte[] GetAssetData(LLUUID assetID, bool isTexture) { - bool found = false; - AssetStorage foundAsset = null; - - IObjectSet result = db.Get(new AssetStorage(assetID)); - if (result.Count > 0) - { - foundAsset = (AssetStorage)result.Next(); - found = true; - } - - if (found) - { - return foundAsset.Data; - } - else - { - return null; - } + return null; } - public void setupDB() - { - string yappath=(Path.Combine(Util.dataDir(),"gridassets.yap")); - bool yapfile = File.Exists(yappath); - try - { - db = Db4oFactory.OpenFile(yappath); - MainLog.Instance.Verbose("storage", "Main.cs:setupDB() - creation"); - } - catch (Exception e) - { - db.Close(); - MainLog.Instance.Warn("storage", "Main.cs:setupDB() - Exception occured"); - MainLog.Instance.Warn("storage", e.ToString()); - } - if (!yapfile) - { - this.LoadDB(); - } - } - - public void LoadDB() + public void setupDB(AssetConfig config) { try { - - Console.WriteLine("setting up Asset database"); - - AssetBase Image = new AssetBase(); - Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001"); - Image.Name = "Bricks"; - this.LoadAsset(Image, true, "bricks.jp2"); - AssetStorage store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); - - Image = new AssetBase(); - Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002"); - Image.Name = "Plywood"; - this.LoadAsset(Image, true, "plywood.jp2"); - store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); - - Image = new AssetBase(); - Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003"); - Image.Name = "Rocks"; - this.LoadAsset(Image, true, "rocks.jp2"); - store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); - - Image = new AssetBase(); - Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004"); - Image.Name = "Granite"; - this.LoadAsset(Image, true, "granite.jp2"); - store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); - - Image = new AssetBase(); - Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005"); - Image.Name = "Hardwood"; - this.LoadAsset(Image, true, "hardwood.jp2"); - store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); - - Image = new AssetBase(); - Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005"); - Image.Name = "Prim Base Texture"; - this.LoadAsset(Image, true, "plywood.jp2"); - store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); - - Image = new AssetBase(); - Image.FullID = new LLUUID("13371337-1337-1337-1337-133713371337"); - Image.Name = "Peaches"; - this.LoadAsset(Image, true, "peaches.jp2"); - store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); - - Image = new AssetBase(); - Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); - Image.Name = "Shape"; - this.LoadAsset(Image, false, "base_shape.dat"); - store = new AssetStorage(); - store.Data = Image.Data; - store.Name = Image.Name; - store.UUID = Image.FullID; - db.Set(store); - db.Commit(); + m_assetServer = new SQLAssetServer(config.DatabaseProvider); } catch (Exception e) { - Console.WriteLine(e.Message); + MainLog.Instance.Warn("ASSET", "setupDB() - Exception occured"); + MainLog.Instance.Warn("ASSET", e.ToString()); } } - private void LoadAsset(AssetBase info, bool image, string filename) - { - - - string dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; - string fileName = Path.Combine(dataPath, filename); - FileInfo fInfo = new FileInfo(fileName); - long numBytes = fInfo.Length; - FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); - byte[] idata = new byte[numBytes]; - BinaryReader br = new BinaryReader(fStream); - idata = br.ReadBytes((int)numBytes); - br.Close(); - fStream.Close(); - info.Data = idata; - //info.loaded=true; - } - - public void CreateAsset(LLUUID assetId, byte[] assetData) - { - AssetBase asset = new AssetBase(); - asset.Name = ""; - asset.FullID = assetId; - asset.Data = assetData; - - AssetStorage store = new AssetStorage(); - store.Data = asset.Data; - store.Name = asset.Name; - store.UUID = asset.FullID; - db.Set(store); - db.Commit(); - } public void RunCmd(string cmd, string[] cmdparams) { @@ -339,7 +190,7 @@ namespace OpenSim.Grid.AssetServer byte[] assetData = memoryStream.ToArray(); - m_assetManager.CreateAsset(assetId, assetData); +// m_assetManager.CreateAsset(assetId, assetData); } } diff --git a/OpenSim/Region/Application/OpenSimMain.cs b/OpenSim/Region/Application/OpenSimMain.cs index 08b499d328..20152d8674 100644 --- a/OpenSim/Region/Application/OpenSimMain.cs +++ b/OpenSim/Region/Application/OpenSimMain.cs @@ -283,7 +283,7 @@ namespace OpenSim } else { - assetServer = new SQLAssetServer(); + assetServer = new SQLAssetServer("OpenSim.Framework.Data.SQLite.dll"); } assetServer.SetServerInfo(m_networkServersInfo.AssetURL, m_networkServersInfo.AssetSendKey); m_assetCache = new AssetCache(assetServer); diff --git a/OpenSim/Region/Physics/OdePlugin/OdePlugin.cs b/OpenSim/Region/Physics/OdePlugin/OdePlugin.cs index e9ac642598..e0661f82fc 100644 --- a/OpenSim/Region/Physics/OdePlugin/OdePlugin.cs +++ b/OpenSim/Region/Physics/OdePlugin/OdePlugin.cs @@ -30,7 +30,6 @@ using System.Threading; using System.Collections.Generic; using libsecondlife; -using libsecondlife.Utilities; using Axiom.Math; using Ode.NET; diff --git a/prebuild.xml b/prebuild.xml index 8bd52a9ab2..e5046e2115 100644 --- a/prebuild.xml +++ b/prebuild.xml @@ -804,6 +804,7 @@ +