主题
MapArchive
MapArchive 地图存档服务。通过 editor:GetService("MapArchive") 获取,查询/手动创建/回档/删除本地与云端的地图存档点。
概览
地图存档服务把编辑器自动生成的地图存档点开放给插件使用,支持列出槽位、手动打档、回档到指定存档点、删除手动档,以及读写自动保存配置。存档按存档位置与来源类型两个维度组织,自动存档、阶梯下沉档与退出档受轮转机制保护,只有手动档可删除。回档属于高副作用操作,会覆盖当前地图工程并重建场景,插件侧状态随之失效。
Get by:
lua
-- @runtime editor
local service = editor:GetService("MapArchive")函数
List
查询存档列表。返回存档点数组,每项为 MapArchiveInfo。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
options | Table | 可选。Location:Local / Cloud / All,默认 All;MapKey:默认当前打开的地图;SortType:TimeDesc(默认)/ TimeAsc;IncludeEmpty:是否包含空槽位,默认 true。 |
返回值
| 类型 | 说明 |
|---|---|
Array | 存档点列表。每项为 MapArchiveInfo,含 MapKey、Location、SlotName、Slot、VersionId、SourceType、SaveTime、SaveTimeStr、Desc、IsEmpty、CanRollback。空槽位也会返回(IsEmpty=true、SaveTime=-1),便于直接按槽位画格子。 |
列出当前地图的全部存档槽位
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
local slots = mapArchive:List()
print("共获取到 " .. tostring(#slots) .. " 个存档槽位")
for index, info in ipairs(slots) do
print(string.format("[%d] 槽位=%s | 位置=%s | 来源=%s | 可回档=%s", index, tostring(info.SlotName), tostring(info.Location), tostring(info.SourceType), tostring(info.CanRollback)))
endCreate
MapArchive 的手动打存档点接口,用于在当前地图中主动写入一个存档点。调用时可选择传入 options 表进行配置,返回一个结果表:成功时结果表的 Success 为 true,并带有地图标识、存档点名称、位置与存档时间等信息;失败时 Success 为 false,并给出 ErrorCode 与 Error 说明。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
options | Table | 可选。Location:Local(默认)/ Cloud;Desc:存档描述,默认自动命名;SlotName:指定槽位,不传则自动挑选。 |
返回值
| 类型 | 说明 |
|---|---|
Table | 成功 {Success=true, MapKey, SlotName, Location, SaveTime, VersionId(云端)};失败 {Success=false, ErrorCode, Error},ErrorCode ∈ no_map | not_cobuild_owner | busy | slot_unknown | create_failed | no_avatar。 |
创建当前地图的手动存档点
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
local result = mapArchive:Create({})
if type(result) == "table" and result.Success == true then
print("手动打档成功:", tostring(result.SlotName), tostring(result.Location))
else
print("手动打档未成功:", tostring(result))
endRestore
回档到指定存档点。按槽位名发起回档请求,并返回一个表示请求是否被受理的 Table。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
slotName | String | 来自 List 返回项的 SlotName,原样传回。 |
options | Table | 可选。CreateBackup:回档前是否先备份当前状态到本地手动档,默认 true。 |
返回值
| 类型 | 说明 |
|---|---|
Table | 已受理 {Accepted=true, SlotName, Location};未受理 {Accepted=false, ErrorCode, Error},ErrorCode ∈ no_map | not_cobuild_owner | unsupported_in_ogc_cobuild | busy | slot_empty | slot_unknown | restore_failed。 |
监听回档完成事件并请求回档
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
mapArchive.OnRestoreFinished:Once(function(info)
print("收到回档完成事件:", tostring(info))
end)
local accepted = mapArchive:Restore("manual_slot_1")
if type(accepted) == "table" and accepted.Accepted == true then
print("回档请求已受理:", tostring(accepted.SlotName))
else
print("回档请求未受理:", tostring(accepted))
endGetRestoreState
查询回档进度。返回 MapArchiveRestoreState 类型的对象,描述当前回档状态与最近一次回档结果。
返回值
| 类型 | 说明 |
|---|---|
MapArchiveRestoreState | 当前回档状态与最近一次回档结果,与 OnRestoreFinished 事件同源;用于事件因换图丢失后的兜底查询。 |
查询当前回档进度状态
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
local state = mapArchive:GetRestoreState()
if state ~= nil then
print("当前回档状态:", state.State, "是否回档中:", state.IsRestoring, "槽位:", state.SlotName)
else
print("未取得有效的回档状态对象")
endDelete
删除存档点。按槽位名删除指定存档点,并返回描述本次操作结果的 Table。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
slotName | String | 来自 List 返回项的 SlotName。 |
返回值
| 类型 | 说明 |
|---|---|
Table | 成功 {Success=true};失败 {Success=false, ErrorCode, Error},ErrorCode ∈ no_map | not_cobuild_owner | slot_unknown | not_deletable | delete_failed。 |
删除指定手动存档槽位
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
local result = mapArchive:Delete("manual_slot_1")
if type(result) == "table" and result.Success == true then
print("已删除手动存档点: manual_slot_1")
else
print("删除存档点结果:", tostring(result))
endGetCapacity
查询存档容量。返回一个 Table,包含 Local 与 Cloud 两组容量信息,每组分别有 UsedCount、SlotCount、UsedBytes 三个字段。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
location | String | Local / Cloud / All,默认 All。 |
返回值
| 类型 | 说明 |
|---|---|
Table | {Local{UsedCount, SlotCount, UsedBytes}, Cloud{UsedCount, SlotCount, UsedBytes}}。 |
查询本地与云端存档容量
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
local capacity = mapArchive:GetCapacity()
if type(capacity) == "table" then
local localInfo = capacity.Local
local cloudInfo = capacity.Cloud
if type(localInfo) == "table" then
print("本地存档已用槽位:", tostring(localInfo.UsedCount), "/", tostring(localInfo.SlotCount))
end
if type(cloudInfo) == "table" then
print("云端存档已用槽位:", tostring(cloudInfo.UsedCount), "/", tostring(cloudInfo.SlotCount))
end
endGetAutoSaveConfig
读取自动保存配置。返回 MapArchiveAutoSaveConfig 类型的自动保存配置对象。该配置与编辑器「保存设置」界面读写的是同一份数据,因此从界面改过的值这里能读到,反之亦然。
返回值
| 类型 | 说明 |
|---|---|
MapArchiveAutoSaveConfig | 自动保存配置。与编辑器「保存设置」界面读写同一份数据,因此从 UI 改过的值这里能读到,反之亦然。 |
读取自动保存配置
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
local config = mapArchive:GetAutoSaveConfig()
if config ~= nil then
print("自动保存配置:", "Enabled=", tostring(config.Enabled), "SaveIntervalSeconds=", tostring(config.SaveIntervalSeconds), "SnapshotIntervalSeconds=", tostring(config.SnapshotIntervalSeconds))
else
print("未取得自动保存配置")
endSetAutoSaveConfig
写入自动保存配置。将传入的 MapArchiveAutoSaveConfig 配置写入,并返回描述写入结果的 Table。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
config | MapArchiveAutoSaveConfig | 要写入的配置,只需给出要改的字段:Enabled 是否开启自动保存;SaveIntervalSeconds 自动保存间隔(秒,最小 60)。SnapshotIntervalSeconds 只读,传入无效。 |
返回值
| 类型 | 说明 |
|---|---|
Table | 成功 {Success=true, Applied 写入后的完整配置};失败 {Success=false, ErrorCode, Error},ErrorCode ∈ invalid_config | apply_failed。 |
读取并回写自动保存配置
lua
-- @runtime editor
local mapArchive = editor:GetService("MapArchive")
local config = mapArchive:GetAutoSaveConfig()
if config ~= nil then
local result = mapArchive:SetAutoSaveConfig({ Enabled = config.Enabled, SaveIntervalSeconds = config.SaveIntervalSeconds })
print("自动保存配置写入结果:", tostring(result))
end属性
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Location | Table | - | 存档位置枚举表。以 Table 形式提供存档位置的枚举取值,供存档相关的查询结果与调用结果中标识存档所在位置。 |
SourceType | Table | - | 存档来源枚举表。以 Table 形式提供存档来源的枚举取值,用于标识某个存档点是由何种来源产生的。 |
事件
OnRestoreFinished
签名:OnRestoreFinished(info: MapArchiveRestoreResult)
回档流程结束时派发(成功或失败)。回档会重建场景重新进图,换图时会丢弃未完成的方法回调,因此本事件是插件侧拿到回档结果的唯一可靠信号,请以此为准,不要依赖 Restore 的同步返回判断成功。
| 参数 | 类型 | 说明 |
|---|---|---|
info | MapArchiveRestoreResult | 本次回档的最终结果,含 Success 是否成功、SlotName 回档到的槽位名、Location 存档位置(Local | Cloud)、ErrorInfo 失败原因(成功时为空串)。 |
