_warpzone = 2147483648 -- start storing strings here (THIS IS THE ABSOLUTE MAXIMUM VALUE FOR THIS)
_maxlength = 1024 -- multiply by 3 to get the true length.
setPlayerStorageInteger = setPlayerStorageValue
getPlayerStorageInteger = getPlayerStorageValue
function setPlayerStorageString(cid, key, value)
if #value > (_maxlength-1) * 3 - 1 then -- Last word is reserved for 0 termination of the string.
error("Storage string is too long")
end
if key > _warpzone / _maxlength then
error("Storage string key is too large (" .. key .. ")")
end
key = _warpzone + key * _maxlength
local word = 0
local wordwrap = 0
local wordcount = 0
local i = 1
while i <= #value do
local byte = string.byte(string.sub(value, i, i))
word = bit.bor(word, bit.lshift(byte, wordwrap))
wordwrap = wordwrap + 8
if wordwrap == 24 then
--[[
In the ideal world we would be able to store 4 characters per word,
however, as the default return value for getPlayerStorageValue is
-1, we can't use the last bit.
]]--
setPlayerStorageInteger(cid, key + wordcount, word)
word = 0
wordwrap = 0
wordcount = wordcount + 1
end
i = i + 1
end
-- store the last word
setPlayerStorageInteger(cid, key + wordcount, word)
end
function getPlayerStorageString(cid, key)
if key > _warpzone / _maxlength then
error("Storage string key is too large (" .. key .. ")")
end
key = _warpzone + key * _maxlength
local wordcount = 0
local str = ""
while true do
if wordcount >= _maxlength then
break
end
local word = getPlayerStorageInteger(cid, key + wordcount)
if word == -1 then
-- end of string
break
else
-- Extract the 3 characters from the value
byte = bit.band(word, 255)
if byte == 0 then break else str = str .. string.char(byte) end
byte = bit.rshift(bit.band(word, 65280), 8)
if byte == 0 then break else str = str .. string.char(byte) end
byte = bit.rshift(bit.band(word, 16711680), 16)
if byte == 0 then break else str = str .. string.char(byte) end
end
wordcount = wordcount + 1
end
return str
end