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