diff --git a/sql/characters.sql b/sql/characters.sql
index 82ae0855d..10442ba6c 100644
--- a/sql/characters.sql
+++ b/sql/characters.sql
@@ -21,7 +21,7 @@
DROP TABLE IF EXISTS `character_db_version`;
CREATE TABLE `character_db_version` (
- `required_9767_03_characters_characters` bit(1) default NULL
+ `required_9849_01_characters_saved_variables` bit(1) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED COMMENT='Last applied sql update to DB';
--
@@ -1567,7 +1567,8 @@ DROP TABLE IF EXISTS `saved_variables`;
CREATE TABLE `saved_variables` (
`NextArenaPointDistributionTime` bigint(40) UNSIGNED NOT NULL DEFAULT '0',
`NextDailyQuestResetTime` bigint(40) unsigned NOT NULL default '0',
- `NextWeeklyQuestResetTime` bigint(40) unsigned NOT NULL default '0'
+ `NextWeeklyQuestResetTime` bigint(40) unsigned NOT NULL default '0',
+ `cleaning_flags` int(11) unsigned NOT NULL default '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED COMMENT='Variable Saves';
--
diff --git a/sql/updates/9849_01_characters_saved_variables.sql b/sql/updates/9849_01_characters_saved_variables.sql
new file mode 100644
index 000000000..4f1e91925
--- /dev/null
+++ b/sql/updates/9849_01_characters_saved_variables.sql
@@ -0,0 +1,4 @@
+ALTER TABLE character_db_version CHANGE COLUMN required_9767_03_characters_characters required_9849_01_characters_saved_variables bit;
+
+ALTER TABLE saved_variables ADD cleaning_flags int(11) unsigned NOT NULL default '0' AFTER NextWeeklyQuestResetTime;
+UPDATE saved_variables SET cleaning_flags = 0xF;
diff --git a/sql/updates/Makefile.am b/sql/updates/Makefile.am
index 18066bda4..3ad678e55 100644
--- a/sql/updates/Makefile.am
+++ b/sql/updates/Makefile.am
@@ -56,6 +56,7 @@ pkgdata_DATA = \
9794_02_mangos_command.sql \
9803_01_mangos_spell_bonus_data.sql \
9826_01_mangos_spell_script_target.sql \
+ 9849_01_characters_saved_variables.sql \
README
## Additional files to include when running 'make dist'
@@ -92,4 +93,5 @@ EXTRA_DIST = \
9794_02_mangos_command.sql \
9803_01_mangos_spell_bonus_data.sql \
9826_01_mangos_spell_script_target.sql \
+ 9849_01_characters_saved_variables.sql \
README
diff --git a/src/game/CharacterDatabaseCleaner.cpp b/src/game/CharacterDatabaseCleaner.cpp
new file mode 100644
index 000000000..193d595f9
--- /dev/null
+++ b/src/game/CharacterDatabaseCleaner.cpp
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2005-2010 MaNGOS
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "Common.h"
+#include "CharacterDatabaseCleaner.h"
+#include "World.h"
+#include "Database/DatabaseEnv.h"
+#include "DBCStores.h"
+#include "ProgressBar.h"
+
+void CharacterDatabaseCleaner::CleanDatabase()
+{
+ // config to disable
+ if(!sWorld.getConfig(CONFIG_BOOL_CLEAN_CHARACTER_DB))
+ return;
+
+ sLog.outString("Cleaning character database...");
+
+ // check flags which clean ups are necessary
+ QueryResult* result = CharacterDatabase.PQuery("SELECT cleaning_flags FROM saved_variables");
+ if(!result)
+ return;
+ uint32 flags = (*result)[0].GetUInt32();
+ delete result;
+
+ // clean up
+ if(flags & CLEANING_FLAG_ACHIEVEMENT_PROGRESS)
+ CleanCharacterAchievementProgress();
+ if(flags & CLEANING_FLAG_SKILLS)
+ CleanCharacterSkills();
+ if(flags & CLEANING_FLAG_SPELLS)
+ CleanCharacterSpell();
+ if(flags & CLEANING_FLAG_TALENTS)
+ CleanCharacterTalent();
+ CharacterDatabase.Execute("UPDATE saved_variables SET cleaning_flags = 0");
+}
+
+void CharacterDatabaseCleaner::CheckUnique(const char* column, const char* table, bool (*check)(uint32))
+{
+ QueryResult* result = CharacterDatabase.PQuery("SELECT DISTINCT %s FROM %s", column, table);
+ if(!result)
+ {
+ sLog.outString( "Table %s is empty.", table );
+ return;
+ }
+
+ bool found = false;
+ std::ostringstream ss;
+ barGoLink bar( (int)result->GetRowCount() );
+ do
+ {
+ bar.step();
+
+ Field *fields = result->Fetch();
+
+ uint32 id = fields[0].GetUInt32();
+
+ if(!check(id))
+ {
+ if(!found)
+ {
+ ss << "DELETE FROM " << table << " WHERE " << column << " IN (";
+ found = true;
+ }
+ else
+ ss << ",";
+ ss << id;
+ }
+ }
+ while( result->NextRow() );
+ delete result;
+
+ if (found)
+ {
+ ss << ")";
+ CharacterDatabase.Execute( ss.str().c_str() );
+ }
+}
+
+bool CharacterDatabaseCleaner::AchievementProgressCheck(uint32 criteria)
+{
+ return sAchievementCriteriaStore.LookupEntry(criteria);
+}
+
+void CharacterDatabaseCleaner::CleanCharacterAchievementProgress()
+{
+ CheckUnique("criteria", "character_achievement_progress", &AchievementProgressCheck);
+}
+
+bool CharacterDatabaseCleaner::SkillCheck(uint32 skill)
+{
+ return sSkillLineStore.LookupEntry(skill);
+}
+
+void CharacterDatabaseCleaner::CleanCharacterSkills()
+{
+ CheckUnique("skill", "character_skills", &SkillCheck);
+}
+
+bool CharacterDatabaseCleaner::SpellCheck(uint32 spell_id)
+{
+ return sSpellStore.LookupEntry(spell_id) && !GetTalentSpellPos(spell_id);
+}
+
+void CharacterDatabaseCleaner::CleanCharacterSpell()
+{
+ CheckUnique("spell", "character_spell", &SpellCheck);
+}
+
+bool CharacterDatabaseCleaner::TalentCheck(uint32 talent_id)
+{
+ TalentEntry const *talentInfo = sTalentStore.LookupEntry( talent_id );
+ if(!talentInfo)
+ return false;
+
+ return sTalentTabStore.LookupEntry( talentInfo->TalentTab );
+}
+
+void CharacterDatabaseCleaner::CleanCharacterTalent()
+{
+ CharacterDatabase.DirectPExecute("DELETE FROM character_talent WHERE spec > %u OR current_rank > %u", MAX_TALENT_SPEC_COUNT, MAX_TALENT_RANK);
+
+ CheckUnique("talent_id", "character_talent", &TalentCheck);
+}
diff --git a/src/game/CharacterDatabaseCleaner.h b/src/game/CharacterDatabaseCleaner.h
new file mode 100644
index 000000000..3fc504e9d
--- /dev/null
+++ b/src/game/CharacterDatabaseCleaner.h
@@ -0,0 +1,48 @@
+/*
+* Copyright (C) 2005-2010 MaNGOS
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef CHARACTERDATABASECLEANER_H
+#define CHARACTERDATABASECLEANER_H
+
+namespace CharacterDatabaseCleaner
+{
+ enum CleaningFlags
+ {
+ CLEANING_FLAG_ACHIEVEMENT_PROGRESS = 0x1,
+ CLEANING_FLAG_SKILLS = 0x2,
+ CLEANING_FLAG_SPELLS = 0x4,
+ CLEANING_FLAG_TALENTS = 0x8
+ };
+
+
+ void CleanDatabase();
+
+ void CheckUnique(const char* column, const char* table, bool (*check)(uint32));
+
+ bool AchievementProgressCheck(uint32 criteria);
+ bool SkillCheck(uint32 skill);
+ bool SpellCheck(uint32 spell_id);
+ bool TalentCheck(uint32 talent_id);
+
+ void CleanCharacterAchievementProgress();
+ void CleanCharacterSkills();
+ void CleanCharacterSpell();
+ void CleanCharacterTalent();
+}
+
+#endif
diff --git a/src/game/Makefile.am b/src/game/Makefile.am
index e1a85d939..b9022daf0 100644
--- a/src/game/Makefile.am
+++ b/src/game/Makefile.am
@@ -83,6 +83,8 @@ libmangosgame_a_SOURCES = \
ChannelHandler.cpp \
ChannelMgr.cpp \
ChannelMgr.h \
+ CharacterDatabaseCleaner.cpp \
+ CharacterDatabaseCleaner.h \
CharacterHandler.cpp \
Chat.cpp \
Chat.h \
diff --git a/src/game/World.cpp b/src/game/World.cpp
index 8b5120437..87d4a1591 100644
--- a/src/game/World.cpp
+++ b/src/game/World.cpp
@@ -60,6 +60,7 @@
#include "WaypointManager.h"
#include "GMTicketMgr.h"
#include "Util.h"
+#include "CharacterDatabaseCleaner.h"
INSTANTIATE_SINGLETON_1( World );
@@ -516,6 +517,7 @@ void World::LoadConfigSettings(bool reload)
///- Read other configuration items from the config file
setConfigMinMax(CONFIG_UINT32_COMPRESSION, "Compression", 1, 1, 9);
setConfig(CONFIG_BOOL_ADDON_CHANNEL, "AddonChannel", true);
+ setConfig(CONFIG_BOOL_CLEAN_CHARACTER_DB, "CleanCharacterDB", true);
setConfig(CONFIG_BOOL_GRID_UNLOAD, "GridUnload", true);
setConfigPos(CONFIG_UINT32_INTERVAL_SAVE, "PlayerSave.Interval", 15 * MINUTE * IN_MILLISECONDS);
setConfigMinMax(CONFIG_UINT32_MIN_LEVEL_STAT_SAVE, "PlayerSave.Stats.MinLevel", 0, 0, MAX_LEVEL);
@@ -1093,6 +1095,8 @@ void World::SetInitialWorldSettings()
sLog.outString( "Loading Pet Name Parts..." );
sObjectMgr.LoadPetNames();
+ CharacterDatabaseCleaner::CleanDatabase();
+
sLog.outString( "Loading the max pet number..." );
sObjectMgr.LoadPetNumber();
diff --git a/src/game/World.h b/src/game/World.h
index 2cbaa4037..ca241781b 100644
--- a/src/game/World.h
+++ b/src/game/World.h
@@ -311,6 +311,7 @@ enum eConfigBoolValues
CONFIG_BOOL_ARENA_QUEUE_ANNOUNCER_EXIT,
CONFIG_BOOL_KICK_PLAYER_ON_BAD_PACKET,
CONFIG_BOOL_STATS_SAVE_ONLY_ON_LOGOUT,
+ CONFIG_BOOL_CLEAN_CHARACTER_DB,
CONFIG_BOOL_VALUE_COUNT
};
diff --git a/src/mangosd/mangosd.conf.dist.in b/src/mangosd/mangosd.conf.dist.in
index 3f9bd73b5..fbf9a2176 100644
--- a/src/mangosd/mangosd.conf.dist.in
+++ b/src/mangosd/mangosd.conf.dist.in
@@ -169,6 +169,10 @@ BindIP = "0.0.0.0"
# Default: 1 (permit addon channel)
# 0 (do not permit addon channel)
#
+# CleanCharacterDB
+# Perform character db clean ups on start up
+# Default: 1 (Enable)
+# 0 (Disabled)
#
###################################################################################################################
@@ -195,6 +199,7 @@ TargetPosRecalculateRange = 1.5
UpdateUptimeInterval = 10
MaxCoreStuckTime = 0
AddonChannel = 1
+CleanCharacterDB = 1
###################################################################################################################
# SERVER LOGGING
diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h
index 1da34a4f6..a9cdef1cd 100644
--- a/src/shared/revision_nr.h
+++ b/src/shared/revision_nr.h
@@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
- #define REVISION_NR "9848"
+ #define REVISION_NR "9849"
#endif // __REVISION_NR_H__
diff --git a/src/shared/revision_sql.h b/src/shared/revision_sql.h
index e5dd67826..d184f4b20 100644
--- a/src/shared/revision_sql.h
+++ b/src/shared/revision_sql.h
@@ -1,6 +1,6 @@
#ifndef __REVISION_SQL_H__
#define __REVISION_SQL_H__
- #define REVISION_DB_CHARACTERS "required_9767_03_characters_characters"
+ #define REVISION_DB_CHARACTERS "required_9849_01_characters_saved_variables"
#define REVISION_DB_MANGOS "required_9826_01_mangos_spell_script_target"
#define REVISION_DB_REALMD "required_9748_01_realmd_realmlist"
#endif // __REVISION_SQL_H__
diff --git a/win/VC100/game.vcxproj b/win/VC100/game.vcxproj
index 1b4bbe148..4be936d3e 100644
--- a/win/VC100/game.vcxproj
+++ b/win/VC100/game.vcxproj
@@ -373,6 +373,7 @@
+
@@ -519,6 +520,7 @@
+
diff --git a/win/VC80/game.vcproj b/win/VC80/game.vcproj
index 0493d90dd..006d2d084 100644
--- a/win/VC80/game.vcproj
+++ b/win/VC80/game.vcproj
@@ -697,6 +697,14 @@
RelativePath="..\..\src\game\ChannelMgr.h"
>
+
+
+
+
diff --git a/win/VC90/game.vcproj b/win/VC90/game.vcproj
index f37f92487..dcad9f1b5 100644
--- a/win/VC90/game.vcproj
+++ b/win/VC90/game.vcproj
@@ -698,6 +698,14 @@
RelativePath="..\..\src\game\ChannelMgr.h"
>
+
+
+
+