Quantcast

Better encapsulation of compile-related code into OvaleCompile module.

Johnny C. Lam [03-02-13 - 22:15]
Better encapsulation of compile-related code into OvaleCompile module.

The script needs to be recompiled if data that the script depends upon
changes.  In the other modules that flagged ``needCompile'', cause the
data changes due to Blizzard events to fire a new Ovale event after the
data is changed.

The new Ovale events are:

    Ovale_CheckBoxValueChanged
    Ovale_EquipmentChanged
    Ovale_GlyphsChanged
    Ovale_ListValueChanged
    Ovale_ScriptChanged
    Ovale_SpellsChanged
    Ovale_StanceChanged
    Ovale_TalentsChanged

Make OvaleCompile into a full-fledged Ace3 addon module that listens for
the new Ovale events that triggers script recompilation.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@714 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
Ovale.lua
OvaleCompile.lua
OvaleData.lua
OvaleEquipement.lua
OvaleFrame.lua
OvaleOptions.lua
OvaleStance.lua
diff --git a/Ovale.lua b/Ovale.lua
index daca869..da9cda0 100644
--- a/Ovale.lua
+++ b/Ovale.lua
@@ -32,8 +32,6 @@ Ovale.frame = nil
 Ovale.checkBoxes = {}
 --drop down GUI items
 Ovale.dropDowns = {}
---master nodes of the current script (one node for each icon)
-Ovale.masterNodes = nil
 --set it if there was a bug, traces will be enabled on next frame
 Ovale.bug = false
 Ovale.traced = false
@@ -46,10 +44,7 @@ Ovale.score = 0
 --maximal theoric score in current combat
 Ovale.maxScore = 0
 Ovale.refreshNeeded = {}
-Ovale.compileOnItems = false
-Ovale.compileOnStances = false
 Ovale.combatStartTime = nil
-Ovale.needCompile = false
 Ovale.listes = {}
 --</public-static-properties>

@@ -70,7 +65,7 @@ function Ovale:debugPrint(flag, ...)
 end

 function Ovale:Debug()
-	self:Print(OvaleCompile:DebugNode(self.masterNodes[1]))
+	self:Print(OvaleCompile:DebugNode(OvaleCompile.masterNodes[1]))
 end

 -- Print the auras matching the filter on the target in alphabetical order.
@@ -91,28 +86,13 @@ function Ovale:DebugListAura(target, filter)
 	end
 end

-function Ovale:CompileAll()
-	local code = OvaleOptions:GetProfile().code
-	if code then
-		if self.needCompile then
-			self:debugPrint("compile", "FULL compile")
-			self.masterNodes = OvaleCompile:Compile(code)
-		end
-		self.refreshNeeded.player = true
-		self:UpdateFrame()
-		self.needCompile = false
-	end
-end
-
 function Ovale:OnEnable()
     -- Called when the addon is enabled
 	RegisterAddonMessagePrefix("Ovale")
-	self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
 	self:RegisterEvent("PLAYER_REGEN_ENABLED");
 	self:RegisterEvent("PLAYER_REGEN_DISABLED");
 	self:RegisterEvent("PLAYER_TARGET_CHANGED")
 	self:RegisterEvent("CHAT_MSG_ADDON")
-	self:RegisterMessage("Ovale_UpdateShapeshiftForm")

 	local profile = OvaleOptions:GetProfile()
 	self.frame = LibStub("AceGUI-3.0"):Create("OvaleFrame")
@@ -122,33 +102,13 @@ end

 function Ovale:OnDisable()
     -- Called when the addon is disabled
-	self:UnregisterEvent("PLAYER_EQUIPMENT_CHANGED")
 	self:UnregisterEvent("PLAYER_REGEN_ENABLED")
 	self:UnregisterEvent("PLAYER_REGEN_DISABLED")
 	self:UnregisterEvent("PLAYER_TARGET_CHANGED")
 	self:UnregisterEvent("CHAT_MSG_ADDON")
-	self:UnregisterMessage("Ovale_UpdateShapeshiftForm")
 	self.frame:Hide()
 end

-function Ovale:PLAYER_EQUIPMENT_CHANGED(event, slot, hasItem)
-	if self.compileOnItems then
-		self:debugPrint("compile", event)
-		self.needCompile = true
-	else
-		self.refreshNeeded.player = true
-	end
-end
-
-function Ovale:Ovale_UpdateShapeshiftForm(event)
-	if Ovale.compileOnStances then
-		self:debugPrint("compile", event)
-		self.needCompile = true
-	else
-		self.refreshNeeded.player = true
-	end
-end
-
 --Called when the player target change
 --Used to update the visibility e.g. if the user chose
 --to hide Ovale if a friendly unit is targeted
@@ -207,16 +167,14 @@ end
 local function OnCheckBoxValueChanged(widget)
 	OvaleOptions:GetProfile().check[widget.userdata.k] = widget:GetValue()
 	if Ovale.casesACocher[widget.userdata.k].compile then
-		Ovale:debugPrint("compile", "checkbox value changed: " .. widget.userdata.k)
-		Ovale.needCompile = true
+		self:SendMessage("Ovale_CheckBoxValueChanged")
 	end
 end

 local function OnDropDownValueChanged(widget)
 	OvaleOptions:GetProfile().list[widget.userdata.k] = widget.value
 	if Ovale.listes[widget.userdata.k].compile then
-		Ovale:debugPrint("compile", "list value changed: " .. widget.userdata.k)
-		Ovale.needCompile = true
+		self:SendMessage("Ovale_ListValueChanged")
 	end
 end

diff --git a/OvaleCompile.lua b/OvaleCompile.lua
index e8c26c2..2587c5e 100644
--- a/OvaleCompile.lua
+++ b/OvaleCompile.lua
@@ -8,12 +8,12 @@
 ----------------------------------------------------------------------]]

 local _, Ovale = ...
-OvaleCompile = {}
+OvaleCompile = Ovale:NewModule("OvaleCompile", "AceEvent-3.0")

 --<private-static-properties>
 local L = Ovale.L

-local node={}
+local node = {}
 local defines = {}
 local customFunctions = {}
 local unknownSpellNodes = {}
@@ -24,6 +24,13 @@ local strfind, strgmatch, strgsub = string.find, string.gmatch, string.gsub
 local strlen, strlower, strmatch, strsub = string.len, string.lower, string.match, string.sub
 --</private-static-properties>

+--<public-static-properties>
+--master nodes of the current script (one node for each icon)
+OvaleCompile.masterNodes = {}
+OvaleCompile.compileOnItems = false
+OvaleCompile.compileOnStances = false
+--</public-static-properties>
+
 --<private-static-methods>
 local function AddNode(newNode)
 	node[#node+1] = newNode
@@ -81,7 +88,7 @@ local function TestConditions(paramList)
 		return false
 	end
 	if paramList.if_stance then
-		Ovale.compileOnStances = true
+		OvaleCompile.compileOnStances = true
 		if not OvaleStance:IsStance(paramList.if_stance) then
 			return false
 		end
@@ -125,7 +132,7 @@ local function TestConditions(paramList)
 	end
 	if paramList.itemset and paramList.itemcount then
 		local equippedCount = OvaleEquipement:GetArmorSetCount(paramList.itemset)
-		Ovale.compileOnItems = true
+		OvaleCompile.compileOnItems = true
 		if equippedCount < paramList.itemcount then
 			return false
 		end
@@ -537,16 +544,16 @@ end
 local function ParseL(text)
 	return '"'..L[text]..'"'
 end
---</private-static-methods>

---<public-static-methods>
-function OvaleCompile:CompileComments(text)
+-- Suppression des commentaires
+local function CompileComments(text)
 	text = strgsub(text, "#.-\n","")
 	text = strgsub(text, "#.*$","")
 	return text
 end

-function OvaleCompile:CompileInputs(text)
+-- On compile les AddCheckBox et AddListItem
+local function CompileInputs(text)
 	Ovale.casesACocher = {}
 	Ovale.listes = {}

@@ -555,7 +562,8 @@ function OvaleCompile:CompileInputs(text)
 	return text
 end

-function OvaleCompile:CompileDeclarations(text)
+-- Compile non-function and non-icon declarations.
+local function CompileDeclarations(text)
 	-- Define(CONSTANTE valeur)
 	text = strgsub(text, "Define%s*%(%s*([%w_]+)%s+(%w+)%s*%)", ParseDefine)

@@ -585,23 +593,19 @@ function OvaleCompile:CompileDeclarations(text)
 	return text
 end

-function OvaleCompile:Compile(text)
-	Ovale.compileOnItems = false
-	Ovale.compileOnStances = false
+local function CompileScript(text)
+	OvaleCompile.compileOnItems = false
+	OvaleCompile.compileOnStances = false
 	Ovale.bug = false
-	node = {}
-	defines = {}
-	unknownSpellNodes = {}
-	missingSpellList = {}

-	-- Suppression des commentaires
-	text = self:CompileComments(text)
+	wipe(node)
+	wipe(defines)
+	wipe(unknownSpellNodes)
+	wipe(missingSpellList)

-	-- Compile non-function and non-icon declarations.
-	text = self:CompileDeclarations(text)
-
-	-- On compile les AddCheckBox et AddListItem
-	text = self:CompileInputs(text)
+	text = CompileComments(text)
+	text = CompileDeclarations(text)
+	text = CompileInputs(text)

 	OvaleData:ResetSpellFilter()

@@ -612,8 +616,9 @@ function OvaleCompile:Compile(text)
 		end
 	end

-	local masterNodes ={}
-
+	local masterNodes = OvaleCompile.masterNodes
+	wipe(masterNodes)
+
 	-- On compile les AddIcon
 	for p,t in strgmatch(text, "AddActionIcon%s*(.-)%s*(%b{})") do
 		local newNode = ParseAddIcon(p,t,true)
@@ -633,8 +638,58 @@ function OvaleCompile:Compile(text)
 	for k, v in pairs(missingSpellList) do
 		OvaleData.spellList[k] = v
 	end
+end
+--</private-static-methods>
+
+--<public-static-methods>
+function OvaleCompile:OnEnable()
+	self:RegisterMessage("Ovale_CheckBoxValueChanged", "EventHandler")
+	self:RegisterMessage("Ovale_EquipmentChanged")
+	self:RegisterMessage("Ovale_GlyphsChanged", "EventHandler")
+	self:RegisterMessage("Ovale_ListValueChanged", "EventHandler")
+	self:RegisterMessage("Ovale_ScriptChanged", "EventHandler")
+	self:RegisterMessage("Ovale_SpellsChanged", "EventHandler")
+	self:RegisterMessage("Ovale_StanceChanged")
+	self:RegisterMessage("Ovale_TalentsChanged", "EventHandler")
+end
+
+function OvaleCompile:OnDisable()
+	self:UnregisterMessage("Ovale_CheckBoxValueChanged")
+	self:UnregisterMessage("Ovale_EquipmentChanged")
+	self:UnregisterMessage("Ovale_GlyphsChanged")
+	self:UnregisterMessage("Ovale_ListValueChanged")
+	self:UnregisterMessage("Ovale_ScriptChanged")
+	self:UnregisterMessage("Ovale_SpellsChanged")
+	self:UnregisterMessage("Ovale_StanceChanged")
+	self:UnregisterMessage("Ovale_TalentsChanged")
+end
+
+function OvaleCompile:EventHandler(event)
+	Ovale:debugPrint("compile", event)
+	self:Compile()
+end
+
+function OvaleCompile:Ovale_EquipmentChanged(event)
+	if OvaleCompile.compileOnItems then
+		self:EventHandler(event)
+	end
+	Ovale.refreshNeeded.player = true
+end

-	return masterNodes
+function OvaleCompile:Ovale_StanceChanged(event)
+	if OvaleCompile.compileOnStances then
+		self:EventHandler(event)
+	end
+	Ovale.refreshNeeded.player = true
+end
+
+function OvaleCompile:Compile()
+	local code = OvaleOptions:GetProfile().code
+	if code then
+		CompileScript(code)
+		Ovale.refreshNeeded.player = true
+		Ovale:UpdateFrame()
+	end
 end

 function OvaleCompile:DebugNode(node)
diff --git a/OvaleData.lua b/OvaleData.lua
index 6f2270e..a3d7289 100644
--- a/OvaleData.lua
+++ b/OvaleData.lua
@@ -300,19 +300,17 @@ function OvaleData:OnEnable()
 	self.className = select(2, UnitClass("player"))
 	self.level = UnitLevel("player")

-	self:RemplirListeTalents()
-	self:FillSpellList()
-
-	self:RegisterEvent("CHARACTER_POINTS_CHANGED")
-	self:RegisterEvent("GLYPH_ADDED")
-	self:RegisterEvent("GLYPH_DISABLED")
-	self:RegisterEvent("GLYPH_ENABLED")
-	self:RegisterEvent("GLYPH_REMOVED")
-	self:RegisterEvent("GLYPH_UPDATED")
+	self:RegisterEvent("CHARACTER_POINTS_CHANGED", "RemplirListeTalents")
+	self:RegisterEvent("GLYPH_ADDED", "UpdateGlyphs")
+	self:RegisterEvent("GLYPH_DISABLED", "UpdateGlyphs")
+	self:RegisterEvent("GLYPH_ENABLED", "UpdateGlyphs")
+	self:RegisterEvent("GLYPH_REMOVED", "UpdateGlyphs")
+	self:RegisterEvent("GLYPH_UPDATED", "UpdateGlyphs")
+	self:RegisterEvent("PLAYER_ALIVE")
 	self:RegisterEvent("PLAYER_LEVEL_UP")
-	self:RegisterEvent("PLAYER_TALENT_UPDATE")
-	self:RegisterEvent("SPELLS_CHANGED")
-	self:RegisterEvent("UNIT_PET")
+	self:RegisterEvent("PLAYER_TALENT_UPDATE", "RemplirListeTalents")
+	self:RegisterEvent("SPELLS_CHANGED", "FillSpellList")
+	self:RegisterEvent("UNIT_PET", "FillPetSpellList")
 end

 function OvaleData:OnDisable()
@@ -322,35 +320,18 @@ function OvaleData:OnDisable()
 	self:UnregisterEvent("GLYPH_ENABLED")
 	self:UnregisterEvent("GLYPH_REMOVED")
 	self:UnregisterEvent("GLYPH_UPDATED")
+	self:UnregisterEvent("PLAYER_ALIVE")
 	self:UnregisterEvent("PLAYER_LEVEL_UP")
 	self:UnregisterEvent("PLAYER_TALENT_UPDATE")
 	self:UnregisterEvent("SPELLS_CHANGED")
 	self:UnregisterEvent("UNIT_PET")
 end

-function OvaleData:CHARACTER_POINTS_CHANGED()
+-- Talent information is available to the UI when PLAYER_ALIVE fires.
+function OvaleData:PLAYER_ALIVE(event)
 	self:RemplirListeTalents()
---	Ovale:Print("CHARACTER_POINTS_CHANGED")
-end
-
-function OvaleData:GLYPH_ADDED(event)
-	self:UpdateGlyphs()
-end
-
-function OvaleData:GLYPH_DISABLED(event)
-	self:UpdateGlyphs()
-end
-
-function OvaleData:GLYPH_ENABLED(event)
-	self:UpdateGlyphs()
-end
-
-function OvaleData:GLYPH_REMOVED(event)
-	self:UpdateGlyphs()
-end
-
-function OvaleData:GLYPH_UPDATED(event)
 	self:UpdateGlyphs()
+	self:FillSpellList()
 end

 function OvaleData:PLAYER_LEVEL_UP(event, level, ...)
@@ -362,23 +343,6 @@ function OvaleData:PLAYER_LEVEL_UP(event, level, ...)
 	end
 end

-function OvaleData:PLAYER_TALENT_UPDATE(event)
-	Ovale:debugPrint("compile", event)
-	self:RemplirListeTalents()
-	Ovale.needCompile = true
-end
-
-function OvaleData:UNIT_PET()
-	self:FillPetSpellList()
-end
-
---The user learnt a new spell
-function OvaleData:SPELLS_CHANGED(event)
-	Ovale:debugPrint("compile", event)
-	self:FillSpellList()
-	Ovale.needCompile = true
-end
-
 function OvaleData:GetRootSpellList()
 	if rootSpellList then
 		return rootSpellList
@@ -467,11 +431,12 @@ function OvaleData:FillSpellList()
 		i = i + 1
 	end
 	self:FillPetSpellList()
+	self:SendMessage("Ovale_SpellsChanged")
 end

 function OvaleData:RemplirListeTalents()
 	local talentId = 1
-	local needCompile = false
+	local talentsChanged = false
 	while true do
 		local name, texture, tier, column, selected, available = GetTalentInfo(talentId)
 		if not name then
@@ -486,12 +451,11 @@ function OvaleData:RemplirListeTalents()
 			self.pointsTalent[talentId] = 0
 		end
 		self.listeTalentsRemplie = true
-		needCompile = true
+		talentsChanged = true
 		talentId = talentId + 1
 	end
-	if needCompile then
-		Ovale:debugPrint("compile", "filling talent list")
-		Ovale.needCompile = needCompile
+	if talentsChanged then
+		self:SendMessage("Ovale_TalentsChanged")
 	end
 end

@@ -521,8 +485,7 @@ function OvaleData:UpdateGlyphs()
 			self.glyphs[glyphSpell] = true
 		end
 	end
-	Ovale:debugPrint("compile", event)
-	Ovale.needCompile = true
+	self:SendMessage("Ovale_GlyphsChanged")
 end

 function OvaleData:DebugGlyphs()
diff --git a/OvaleEquipement.lua b/OvaleEquipement.lua
index 42f0e76..a871a0c 100644
--- a/OvaleEquipement.lua
+++ b/OvaleEquipement.lua
@@ -554,5 +554,6 @@ function OvaleEquipement:Refresh()
 			end
 		end
 	end
+	self:SendMessage("Ovale_EquipmentChanged")
 end
 --</public-static-methods>
diff --git a/OvaleFrame.lua b/OvaleFrame.lua
index 7765f98..e310c70 100644
--- a/OvaleFrame.lua
+++ b/OvaleFrame.lua
@@ -146,14 +146,6 @@ do
 	end

 	local function OnUpdate(self)
-		if not OvaleData.listeTalentsRemplie then
-			OvaleData:RemplirListeTalents()
-		end
-		if Ovale.needCompile then
-			Ovale:CompileAll()
-			return
-		end
-
 		local now = GetTime()
 		local profile = OvaleOptions:GetProfile()
 		local forceRefresh = not self.lastUpdate or (now > self.lastUpdate + profile.apparence.updateInterval)
@@ -161,12 +153,12 @@ do
 		if not next(Ovale.refreshNeeded) and not forceRefresh then
 			return
 		end
-		if not Ovale.masterNodes then return end
+		if not OvaleCompile.masterNodes then return end

 		self.lastUpdate = now

 		OvaleState:StartNewFrame()
-		for k,node in pairs(Ovale.masterNodes) do
+		for k,node in pairs(OvaleCompile.masterNodes) do
 			local target = node.params.target or "target"
 			OvaleCondition.defaultTarget = target

@@ -311,7 +303,7 @@ do
 		local maxWidth = 0
 		local top = 0

-		if (not Ovale.masterNodes) then
+		if (not OvaleCompile.masterNodes) then
 			return;
 		end

@@ -319,7 +311,7 @@ do

 		local margin = profile.apparence.margin

-		for k,node in pairs(Ovale.masterNodes) do
+		for k,node in pairs(OvaleCompile.masterNodes) do
 			if not self.actions[k] then
 				self.actions[k] = {icons={}, secureIcons={}}
 			end
diff --git a/OvaleOptions.lua b/OvaleOptions.lua
index df3955e..4f842d7 100644
--- a/OvaleOptions.lua
+++ b/OvaleOptions.lua
@@ -10,7 +10,7 @@
 -- Ovale options and UI

 local _, Ovale = ...
-OvaleOptions = Ovale:NewModule("OvaleOptions", "AceConsole-3.0")
+OvaleOptions = Ovale:NewModule("OvaleOptions", "AceConsole-3.0", "AceEvent-3.0")

 --<private-static-properties>
 local strgmatch, strgsub, tostring = string.gmatch, string.gsub, tostring
@@ -286,8 +286,7 @@ local options =
 					end,
 					set = function(info,v)
 						OvaleOptions.db.profile.code = v
-						Ovale:debugPrint("compile", "accepting script")
-						Ovale.needCompile = true
+						self:SendMessage("Ovale_ScriptChanged")
 					end,
 					width = "full"
 				},
@@ -301,8 +300,7 @@ local options =
 					end,
 					func = function()
 						OvaleOptions.db.profile.code = OvaleOptions.db.defaults.profile.code
-						Ovale:debugPrint("compile", "restoring default script")
-						Ovale.needCompile = true
+						self:SendMessage("Ovale_ScriptChanged")
 					end,
 				}
 			}
@@ -519,16 +517,12 @@ function OvaleOptions:OnInitialize()
 	self.db.RegisterCallback( self, "OnProfileChanged", "HandleProfileChanges" )
 	self.db.RegisterCallback( self, "OnProfileCopied", "HandleProfileChanges" )

-	if self.db.profile.code then
-		Ovale:debugPrint("compile", "setting script during addon initialization")
-		Ovale.needCompile = true
-	end
+	self:HandleProfileChanges()
 end

 function OvaleOptions:HandleProfileChanges()
-	if (self.db.profile.code) then
-		Ovale:debugPrint("compile", "changing profiles")
-		Ovale.needCompile = true
+	if self.db.profile.code then
+		self:SendMessage("Ovale_ScriptChanged")
 	end
 end

diff --git a/OvaleStance.lua b/OvaleStance.lua
index 100ed59..c305074 100644
--- a/OvaleStance.lua
+++ b/OvaleStance.lua
@@ -165,7 +165,7 @@ function OvaleStance:ShapeshiftEventHandler()
 	local newStance = GetShapeshiftForm()
 	if stance ~= newStance then
 		stance = newStance
-		self:SendMessage("Ovale_UpdateShapeshiftForm")
+		self:SendMessage("Ovale_StanceChanged")
 	end
 end
 --</public-static-methods>