disable the flight stuff for now, we'll fix it after thanksgiving
[QuestHelper.git] / AstrolabeQH / DongleStub.lua
blobb5211a9015ab799ad4f3b5ba652a8ad4126cacba
1 --[[-------------------------------------------------------------------------
2 James Whitehead II grants anyone the right to use this work for any purpose,
3 without any conditions, unless such conditions are required by law.
4 ---------------------------------------------------------------------------]]
6 local major = "DongleStub"
7 local minor = tonumber(string.match("$Revision: 313 $", "(%d+)") or 1)
9 local g = getfenv(0)
11 if not g.DongleStub or g.DongleStub:IsNewerVersion(major, minor) then
12 local lib = setmetatable({}, {
13 __call = function(t,k)
14 if type(t.versions) == "table" and t.versions[k] then
15 return t.versions[k].instance
16 else
17 error("Cannot find a library with name '"..tostring(k).."'", 2)
18 end
19 end
22 function lib:IsNewerVersion(major, minor)
23 local versionData = self.versions and self.versions[major]
25 -- If DongleStub versions have differing major version names
26 -- such as DongleStub-Beta0 and DongleStub-1.0-RC2 then a second
27 -- instance will be loaded, with older logic. This code attempts
28 -- to compensate for that by matching the major version against
29 -- "^DongleStub", and handling the version check correctly.
31 if major:match("^DongleStub") then
32 local oldmajor,oldminor = self:GetVersion()
33 if self.versions and self.versions[oldmajor] then
34 return minor > oldminor
35 else
36 return true
37 end
38 end
40 if not versionData then return true end
41 local oldmajor,oldminor = versionData.instance:GetVersion()
42 return minor > oldminor
43 end
45 local function NilCopyTable(src, dest)
46 for k,v in pairs(dest) do dest[k] = nil end
47 for k,v in pairs(src) do dest[k] = v end
48 end
50 function lib:Register(newInstance, activate, deactivate)
51 assert(type(newInstance.GetVersion) == "function",
52 "Attempt to register a library with DongleStub that does not have a 'GetVersion' method.")
54 local major,minor = newInstance:GetVersion()
55 assert(type(major) == "string",
56 "Attempt to register a library with DongleStub that does not have a proper major version.")
57 assert(type(minor) == "number",
58 "Attempt to register a library with DongleStub that does not have a proper minor version.")
60 -- Generate a log of all library registrations
61 if not self.log then self.log = {} end
62 table.insert(self.log, string.format("Register: %s, %s", major, minor))
64 if not self:IsNewerVersion(major, minor) then return false end
65 if not self.versions then self.versions = {} end
67 local versionData = self.versions[major]
68 if not versionData then
69 -- New major version
70 versionData = {
71 ["instance"] = newInstance,
72 ["deactivate"] = deactivate,
75 self.versions[major] = versionData
76 if type(activate) == "function" then
77 table.insert(self.log, string.format("Activate: %s, %s", major, minor))
78 activate(newInstance)
79 end
80 return newInstance
81 end
83 local oldDeactivate = versionData.deactivate
84 local oldInstance = versionData.instance
86 versionData.deactivate = deactivate
88 local skipCopy
89 if type(activate) == "function" then
90 table.insert(self.log, string.format("Activate: %s, %s", major, minor))
91 skipCopy = activate(newInstance, oldInstance)
92 end
94 -- Deactivate the old libary if necessary
95 if type(oldDeactivate) == "function" then
96 local major, minor = oldInstance:GetVersion()
97 table.insert(self.log, string.format("Deactivate: %s, %s", major, minor))
98 oldDeactivate(oldInstance, newInstance)
99 end
101 -- Re-use the old table, and discard the new one
102 if not skipCopy then
103 NilCopyTable(newInstance, oldInstance)
105 return oldInstance
108 function lib:GetVersion() return major,minor end
110 local function Activate(new, old)
111 -- This code ensures that we'll move the versions table even
112 -- if the major version names are different, in the case of
113 -- DongleStub
114 if not old then old = g.DongleStub end
116 if old then
117 new.versions = old.versions
118 new.log = old.log
120 g.DongleStub = new
123 -- Actually trigger libary activation here
124 local stub = g.DongleStub or lib
125 lib = stub:Register(lib, Activate)