153 lines
5.0 KiB
Lua
153 lines
5.0 KiB
Lua
local ____exports = __module.env
|
|
---
|
|
-- @noSelf
|
|
local function MapNativeStatus(nativeStatus)
|
|
if nativeStatus == UE.ELuaStorageStatus.Ok then
|
|
return "ok"
|
|
end
|
|
if nativeStatus == UE.ELuaStorageStatus.NotFound then
|
|
return "not_found"
|
|
end
|
|
if nativeStatus == UE.ELuaStorageStatus.Corrupted then
|
|
return "corrupted"
|
|
end
|
|
if nativeStatus == UE.ELuaStorageStatus.WriteFailed then
|
|
return "write_failed"
|
|
end
|
|
return "invalid_argument"
|
|
end
|
|
____exports.UEStorageBackend = __TS__DefineClass("UEStorageBackend", __module)
|
|
local UEStorageBackend = ____exports.UEStorageBackend
|
|
UEStorageBackend.name = "UEStorageBackend"
|
|
function UEStorageBackend.prototype.____constructor(self)
|
|
end
|
|
function UEStorageBackend.prototype.LoadSlotUtf8(self, slotName, userIndex)
|
|
if userIndex == nil then
|
|
userIndex = 0
|
|
end
|
|
local res = UE.ULuaStorageLibrary.LoadSlotUtf8(slotName, userIndex)
|
|
return {
|
|
status = MapNativeStatus(res.Status),
|
|
payloadUtf8 = res.PayloadUtf8 or "",
|
|
containerVersion = res.ContainerVersion or 0,
|
|
recoveredFromBackup = res.bRecoveredFromBackup == true,
|
|
errorMessage = res.ErrorMessage or ""
|
|
}
|
|
end
|
|
function UEStorageBackend.prototype.SaveSlotUtf8(self, slotName, payloadUtf8, userIndex)
|
|
if userIndex == nil then
|
|
userIndex = 0
|
|
end
|
|
return MapNativeStatus(UE.ULuaStorageLibrary.SaveSlotUtf8(slotName, payloadUtf8, userIndex))
|
|
end
|
|
function UEStorageBackend.prototype.DeleteSlot(self, slotName, userIndex)
|
|
if userIndex == nil then
|
|
userIndex = 0
|
|
end
|
|
return UE.ULuaStorageLibrary.DeleteSlot(slotName, userIndex, true)
|
|
end
|
|
function UEStorageBackend.prototype.DoesSlotExist(self, slotName, userIndex)
|
|
if userIndex == nil then
|
|
userIndex = 0
|
|
end
|
|
return UE.ULuaStorageLibrary.DoesSlotExist(slotName, userIndex)
|
|
end
|
|
____exports.MemoryStorageBackend = __TS__DefineClass("MemoryStorageBackend", __module)
|
|
local MemoryStorageBackend = ____exports.MemoryStorageBackend
|
|
MemoryStorageBackend.name = "MemoryStorageBackend"
|
|
function MemoryStorageBackend.prototype.____constructor(self)
|
|
self.slots = {}
|
|
self.backups = {}
|
|
self.corruptCopies = {}
|
|
self.corruptedPrimaryFlags = {}
|
|
self.writeCount = 0
|
|
self.failNextWrite = false
|
|
end
|
|
function MemoryStorageBackend.prototype.InjectCorruptPrimary(self, slotName, rawGarbage)
|
|
self.slots[slotName] = rawGarbage
|
|
self.corruptedPrimaryFlags[slotName] = true
|
|
end
|
|
function MemoryStorageBackend.prototype.GetCorruptCopy(self, slotName)
|
|
return self.corruptCopies[slotName]
|
|
end
|
|
function MemoryStorageBackend.prototype.LoadSlotUtf8(self, slotName, _userIndex)
|
|
if _userIndex == nil then
|
|
_userIndex = 0
|
|
end
|
|
local primary = self.slots[slotName]
|
|
local backup = self.backups[slotName]
|
|
if primary == nil and backup == nil then
|
|
return {
|
|
status = "not_found",
|
|
payloadUtf8 = "",
|
|
containerVersion = 0,
|
|
recoveredFromBackup = false,
|
|
errorMessage = ""
|
|
}
|
|
end
|
|
if primary ~= nil and not self.corruptedPrimaryFlags[slotName] then
|
|
return {
|
|
status = "ok",
|
|
payloadUtf8 = primary,
|
|
containerVersion = 1,
|
|
recoveredFromBackup = false,
|
|
errorMessage = ""
|
|
}
|
|
end
|
|
if primary ~= nil and self.corruptedPrimaryFlags[slotName] then
|
|
self.corruptCopies[slotName] = primary
|
|
end
|
|
if backup ~= nil then
|
|
return {
|
|
status = "ok",
|
|
payloadUtf8 = backup,
|
|
containerVersion = 1,
|
|
recoveredFromBackup = true,
|
|
errorMessage = "Primary slot corrupted; recovered from backup"
|
|
}
|
|
end
|
|
return {
|
|
status = "corrupted",
|
|
payloadUtf8 = "",
|
|
containerVersion = 0,
|
|
recoveredFromBackup = false,
|
|
errorMessage = "Primary slot corrupted and no valid backup exists"
|
|
}
|
|
end
|
|
function MemoryStorageBackend.prototype.SaveSlotUtf8(self, slotName, payloadUtf8, _userIndex)
|
|
if _userIndex == nil then
|
|
_userIndex = 0
|
|
end
|
|
self.writeCount = self.writeCount + 1
|
|
if self.failNextWrite then
|
|
self.failNextWrite = false
|
|
return "write_failed"
|
|
end
|
|
local prev = self.slots[slotName]
|
|
if prev ~= nil and not self.corruptedPrimaryFlags[slotName] then
|
|
self.backups[slotName] = prev
|
|
end
|
|
self.slots[slotName] = payloadUtf8
|
|
self.corruptedPrimaryFlags[slotName] = nil
|
|
return "ok"
|
|
end
|
|
function MemoryStorageBackend.prototype.DeleteSlot(self, slotName, _userIndex)
|
|
if _userIndex == nil then
|
|
_userIndex = 0
|
|
end
|
|
local existed = self.slots[slotName] ~= nil or self.backups[slotName] ~= nil
|
|
self.slots[slotName] = nil
|
|
self.backups[slotName] = nil
|
|
self.corruptCopies[slotName] = nil
|
|
self.corruptedPrimaryFlags[slotName] = nil
|
|
return existed
|
|
end
|
|
function MemoryStorageBackend.prototype.DoesSlotExist(self, slotName, _userIndex)
|
|
if _userIndex == nil then
|
|
_userIndex = 0
|
|
end
|
|
return self.slots[slotName] ~= nil
|
|
end
|
|
function __module.UpdateImports()
|
|
end
|