Add Profiles.lua file that supplies profile management functions and callbacks.
[Rifter.git] / Profiles.lua
blob097d3f5e0b352f15d762736873921ccdd6eacd3b
1 -- Initialize function
2 local characterID = ("%s - %s"):format(GetRealmName(), UnitName("player"));
4 local function Initialize()
5 -- Make sure the required tables exist
6 if(type(RifterDB) ~= "table") then
7 RifterDB = {};
8 end
9 if(type(RifterDB.CurrentProfile) ~= "table") then
10 RifterDB.CurrentProfile = {};
11 end
12 if(type(RifterDB.Profiles) ~= "table") then
13 RifterDB.Profiles = {};
14 end
15 -- If the current character has no profile assigned to it, assign it a profile equal to his CharacterID (RealmName - CharName)
16 Rifter:SetProfile(RifterDB.CurrentProfile[characterID] or characterID);
17 Rifter.events:Fire("DatabaseReady");
18 end
20 -- Return the current profile, has a valid response only after DatabaseReady has triggered.
21 function Rifter:GetProfile()
22 return RifterDB.CurrentProfile[characterID];
23 end
25 -- Set the current profile to the passed profileName
26 function Rifter:SetProfile(profileName)
27 if(type(RifterDB.Profiles[profileName]) ~= "table") then
28 RifterDB.Profiles[profileName] = {};
29 end
30 Rifter.CurrentProfile = RifterDB.Profiles[profileName];
31 local oldProfile = RifterDB.CurrentProfile[characterID];
32 RifterDB.CurrentProfile[characterID] = profileName;
33 if(oldProfile ~= profileName) then
34 Rifter.events:Fire("ProfileChanged", oldProfile, profileName);
35 end
36 end
38 -- Register the InitializeCore callback on which we'll call our initializer function
39 Rifter.RegisterCallback("Profiles", "InitializeCore", Initialize);