From 1232eb1c587ffdc06c26a1c5b1b4fa5f22848754 Mon Sep 17 00:00:00 2001 From: Tleiades Hax Date: Sat, 13 Oct 2007 07:26:21 +0000 Subject: Asset server implementation. Again one of these "plumbing" releases, where no real functionality has been introduced, but ground work has been made, enabling the asset server, and preparing the sim server to query the asset server. Introduced an "IPlugin" interface, which plugins can inherit from. --- .../Communications/Cache/SQLAssetServer.cs | 68 +++++------ OpenSim/Framework/Data.MySQL/MySQLAssetData.cs | 112 +++++++++++++++++ OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs | 133 +++++++-------------- OpenSim/Framework/Data.MySQL/MySQLManager.cs | 92 +++++++++++++- .../Data.MySQL/Resources/CreateAssetsTable.sql | 11 ++ OpenSim/Framework/Data.SQLite/SQLiteAssetData.cs | 23 +++- .../Framework/General/Configuration/AssetConfig.cs | 54 +++++++++ .../Framework/General/Interfaces/IAssetProvider.cs | 3 +- OpenSim/Framework/General/Interfaces/IPlugin.cs | 29 +++++ 9 files changed, 392 insertions(+), 133 deletions(-) create mode 100644 OpenSim/Framework/Data.MySQL/MySQLAssetData.cs create mode 100644 OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql create mode 100644 OpenSim/Framework/General/Configuration/AssetConfig.cs create mode 100644 OpenSim/Framework/General/Interfaces/IPlugin.cs (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs b/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs index a64d195..2a38307 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 0000000..70e04b6 --- /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 804fd5f..6423f28 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 ea174b2..d3f6976 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; @@ -114,6 +118,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. /// /// The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y @@ -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 0000000..049a3a2 --- /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 4616275..922d714 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 0000000..e5f1c88 --- /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 daf9d6d..0b39d1f 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 0000000..ceb8b63 --- /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(); + } +} -- cgit v1.1