Quantcast

* Dongle-0.5.0-Alpha

James Whitehead II [01-01-07 - 22:33]
* Dongle-0.5.0-Alpha
Filename
Dongle.lua
DongleUtils.lua
diff --git a/Dongle.lua b/Dongle.lua
index d2cd929..0bc0e43 100644
--- a/Dongle.lua
+++ b/Dongle.lua
@@ -28,6 +28,67 @@
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ---------------------------------------------------------------------------]]
+local major = "DongleStub"
+local minor = tonumber(string.match("$Revision: 173 $", "(%d+)") or 1)
+
+local g = getfenv(0)
+
+if not g.DongleStub or g.DongleStub:IsNewerVersion(major, minor) then
+	local lib = setmetatable({}, {
+		__call = function(t,k)
+			if type(t.versions) == "table" and t.versions[k] then
+				return t.versions[k]
+			else
+				error("Cannot find a library with name '"..tostring(k).."'", 2)
+			end
+		end
+	})
+
+	function lib:IsNewerVersion(major, minor)
+		local entry = self.versions and self.versions[major]
+
+		if not entry then return true end
+		local oldmajor,oldminor = entry:GetVersion()
+
+		return minor > oldminor
+	end
+
+	function lib:Register(new)
+		local major,minor = new:GetVersion()
+		if not self:IsNewerVersion(major, minor) then return false end
+		local old = self.versions and self.versions[major]
+		-- Run the new libraries activation
+		if type(new.Activate) == "function" then
+			new:Activate(old)
+		end
+
+		-- Deactivate the old libary if necessary
+		if old and type(old.Deactivate) == "function" then
+			old:Deactivate(new)
+		end
+
+		self.versions[major] = new
+	end
+
+	function lib:GetVersion() return major,minor end
+
+	function lib:Activate(old)
+		if old then
+			self.versions = old.versions
+		else
+			self.versions = {}
+		end
+		g.DongleStub = self
+	end
+
+	-- Actually trigger libary activation here
+	local stub = g.DongleStub or lib
+	stub:Register(lib)
+end
+
+--[[-------------------------------------------------------------------------
+  Begin Library Implementation
+---------------------------------------------------------------------------]]

 local major = "Dongle"
 local minor = tonumber(string.match("$Revision: 188 $", "(%d+)") or 1)
diff --git a/DongleUtils.lua b/DongleUtils.lua
index a1c5ae0..89c06cc 100644
--- a/DongleUtils.lua
+++ b/DongleUtils.lua
@@ -28,6 +28,67 @@
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ---------------------------------------------------------------------------]]
+local major = "DongleStub"
+local minor = tonumber(string.match("$Revision: 173 $", "(%d+)") or 1)
+
+local g = getfenv(0)
+
+if not g.DongleStub or g.DongleStub:IsNewerVersion(major, minor) then
+	local lib = setmetatable({}, {
+		__call = function(t,k)
+			if type(t.versions) == "table" and t.versions[k] then
+				return t.versions[k]
+			else
+				error("Cannot find a library with name '"..tostring(k).."'", 2)
+			end
+		end
+	})
+
+	function lib:IsNewerVersion(major, minor)
+		local entry = self.versions and self.versions[major]
+
+		if not entry then return true end
+		local oldmajor,oldminor = entry:GetVersion()
+
+		return minor > oldminor
+	end
+
+	function lib:Register(new)
+		local major,minor = new:GetVersion()
+		if not self:IsNewerVersion(major, minor) then return false end
+		local old = self.versions and self.versions[major]
+		-- Run the new libraries activation
+		if type(new.Activate) == "function" then
+			new:Activate(old)
+		end
+
+		-- Deactivate the old libary if necessary
+		if old and type(old.Deactivate) == "function" then
+			old:Deactivate(new)
+		end
+
+		self.versions[major] = new
+	end
+
+	function lib:GetVersion() return major,minor end
+
+	function lib:Activate(old)
+		if old then
+			self.versions = old.versions
+		else
+			self.versions = {}
+		end
+		g.DongleStub = self
+	end
+
+	-- Actually trigger libary activation here
+	local stub = g.DongleStub or lib
+	stub:Register(lib)
+end
+
+--[[-------------------------------------------------------------------------
+  Begin Library Implementation
+---------------------------------------------------------------------------]]

 local majorUtil, majorGrat, majorMetro = "DongleUtils", "GratuityMini", "MetrognomeNano"
 local minor = tonumber(string.match("$Revision: 178 $", "(%d+)") or 1)