|
| 1 | +--[[ Vindar, 11/2010 |
| 2 | + A patch for the Turbine.PluginData bug for europeen clients. It's dirty but it seems to works :) |
| 3 | +]]-- |
| 4 | + |
| 5 | + |
| 6 | +-- return a copy of the object obj in a safe format |
| 7 | +local function _convSafe(obj) |
| 8 | + if type(obj)=="number" then return(tostring(obj)) end |
| 9 | + if type(obj)~="string" then return(obj) end |
| 10 | + local res = "" |
| 11 | + for i,v in ipairs({obj:byte(1,-1)}) do |
| 12 | + if (v < 32) or (v > 125) then res = res .. "#" .. tonumber(v) .. "#" |
| 13 | + else |
| 14 | + res = res .. string.char(v) |
| 15 | + if v == 35 then res = res .. "#" end |
| 16 | + end |
| 17 | + end |
| 18 | + if res:byte(1,1) == 35 then return("#" .. res) end |
| 19 | + if (tonumber(res)~= nil) then return("#" .. res) end |
| 20 | + return res |
| 21 | +end |
| 22 | +local function _copySafe(obj) |
| 23 | + if type(obj) ~= "table" then return _convSafe(obj) elseif lookupSafe[obj] then return lookupSafe[obj] end |
| 24 | + local newt = {} |
| 25 | + lookupSafe[obj] = newt |
| 26 | + for i, v in pairs(obj) do newt[_copySafe(i)] = _copySafe(v) end |
| 27 | + return setmetatable(newt, getmetatable(obj)) |
| 28 | +end |
| 29 | +function convSafe(obj) |
| 30 | + lookupSafe = {} |
| 31 | + local retVal=_copySafe(obj) |
| 32 | + lookupSafe=nil; |
| 33 | + return retVal; |
| 34 | +end |
| 35 | + |
| 36 | + |
| 37 | +-- the inverse operation |
| 38 | +local function _convBack(obj) |
| 39 | + if type(obj) ~= "string" then return obj end |
| 40 | + if obj == "" then return "" end |
| 41 | + local num = tonumber(obj) |
| 42 | + if num ~= nil then return num end |
| 43 | + local res = "" |
| 44 | + if obj:byte(1,1) == 35 then obj = string.sub(obj,2,-1) end |
| 45 | + while string.len(obj)~=0 do |
| 46 | + if obj:byte(1,1) ~= 35 then res = res .. string.char(obj:byte(1,1)); obj = string.sub(obj,2,-1) |
| 47 | + elseif obj:byte(2,2) == 35 then res = res .. "#"; obj = string.sub(obj,3,-1) |
| 48 | + else |
| 49 | + obj = string.sub(obj,2,-1) |
| 50 | + local k = 1; |
| 51 | + while (obj:byte(k,k) ~= nil) and (obj:byte(k,k) ~= 35) do k = k + 1 end |
| 52 | + if obj:byte(k,k) == nil then error("convBack : parse error 1 !") end |
| 53 | + local v = tonumber(string.sub(obj,1,k-1)) |
| 54 | + if v == nil then error("convBack : parse error 2 !") end |
| 55 | + res = res .. string.char(v) |
| 56 | + obj = string.sub(obj,k+1,-1) |
| 57 | + end |
| 58 | + end |
| 59 | + return res; |
| 60 | +end |
| 61 | +local function _copyBack(obj) |
| 62 | + if type(obj) ~= "table" then return _convBack(obj) elseif lookupBack[obj] then return lookupBack[obj] end |
| 63 | + local newt = {} |
| 64 | + lookupBack[obj] = newt |
| 65 | + for i, v in pairs(obj) do newt[_copyBack(i)] = _copyBack(v) end |
| 66 | + return setmetatable(newt, getmetatable(obj)) |
| 67 | +end |
| 68 | +function convBack(obj) |
| 69 | + lookupBack = {} |
| 70 | + local retVal=_copyBack(obj) |
| 71 | + lookupBack=nil; |
| 72 | + return retVal |
| 73 | +end |
| 74 | + |
| 75 | +-- Replacement for Turbine.PluginData.Load |
| 76 | +function PatchDataLoad(a,b,c) |
| 77 | + local success,results; |
| 78 | + success,results=pcall(Turbine.PluginData.Load,a,b,c); |
| 79 | + if success then |
| 80 | + success,results=pcall(convBack,results); |
| 81 | + if success then |
| 82 | + return results; |
| 83 | + else |
| 84 | + Turbine.Shell.WriteLine("Error:"..tostring(results)) |
| 85 | + Turbine.Shell.WriteLine("While processing convBack(Turbine.PluginData.Load("..tostring(a)..", "..tostring(b)..", "..tostring(c).."))") |
| 86 | + return nil; |
| 87 | + end |
| 88 | + else |
| 89 | + Turbine.Shell.WriteLine("Error:"..tostring(results)) |
| 90 | + Turbine.Shell.WriteLine("While processing Turbine.PluginData.Load("..tostring(a)..", "..tostring(b)..", "..tostring(c)..")") |
| 91 | + return nil; |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +-- Replacement for Turbine.PluginData.Save |
| 96 | +function PatchDataSave(a,b,c,d) |
| 97 | + return Turbine.PluginData.Save(a,b,convSafe(c),d) |
| 98 | +end |
| 99 | + |
| 100 | +-- end of file |
0 commit comments