private MapObject GetMapObject() { MapObject toReturn = null; if (this.cbType.SelectedIndex == 1 && !string.IsNullOrEmpty(this.tbName.Text)) { toReturn = new MapObject(); toReturn.TileMap = null; toReturn.Name = this.tbName.Text; } return toReturn; }
/// <summary> /// Gets a map object by ID /// </summary> /// <param name="id">The ID of the map object to get</param> /// <returns>Returns the map object with the given ID. Null if none exists.</returns> public static MapObject GetMapObject(long id) { MapObject toReturn = null; DataTable dt = null; SqlCommand cmd = new SqlCommand(); string query = "SELECT * FROM [MapObject] WHERE ID = @ID"; cmd.CommandType = CommandType.Text; cmd.CommandText = query; cmd.Parameters.Add("@Parent", SqlDbType.BigInt).Value = id; dt = DatabaseHelper.ExecuteQuery(GetConnectionString(), cmd); if (dt != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { toReturn = new MapObject(); toReturn.ID = DatabaseHelper.GetValidValueFromObject(dr["ID"], (long)0); toReturn.Name = DatabaseHelper.GetValidValueFromObject(dr["Name"], string.Empty); if (dr["Structure"] != null) { toReturn.TileMap = TileMap.CreateTileMapFromByteArray((byte[])dr["Structure"]); } else { toReturn.TileMap = null; } } } return toReturn; }
/// <summary> /// Inserts or Updates a map object, based on the given map object's ID /// </summary> /// <param name="o">The map object to insert/update</param> /// <param name="parent">The parent of the map object</param> private static void InsertUpdateMapObject(MapObject o, long parent) { SqlCommand cmd = new SqlCommand(); string query = string.Empty; cmd.CommandType = CommandType.Text; cmd.CommandText = query; if (o != null) { //Update if (o.ID > 0) { query = "UPDATE [MapObject] SET Name = @Name, Structure = @Structure, Parent = @Parent WHERE ID = @ID"; cmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = o.ID; } //Insert else { query = "INSERT INTO [MapObject] (Name, Structure, Parent) VALUES (@Name, @Structure, @Parent)"; } cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = o.Name; cmd.Parameters.Add("@Structure", SqlDbType.VarBinary).Value = o.TileMap != null ? o.TileMap.GetBytesFromTileMap() : (object)DBNull.Value; cmd.Parameters.Add("@Parent", SqlDbType.BigInt).Value = parent; cmd.CommandText = query; DatabaseHelper.ExecuteCommand(GetConnectionString(), cmd); } }