我有一個名爲backup.lua的文件,程序應該每隔一段時間寫一次文件以備份其狀態,以防出現故障。 問題是該程序首次將backup.lua文件寫入完全正確,但其他任何時候它拒絕寫入該文件。重新打開關閉的文件:Lua
我試圖在程序仍然打開時刪除文件,但Windows告訴我文件正在被'CrysisWarsDedicatedServer.exe'使用,這是該程序。我已經告訴主機Lua函數關閉了backup.lua文件,爲什麼不讓它在關閉後隨意修改文件?
我在互聯網上找不到任何東西(谷歌實際上試圖糾正我的搜索),項目的輔助程序員也不知道。 所以我想知道你們中的任何一個人是否知道我們在這裏做錯了什麼?
Host功能代碼:
function ServerBackup(todo)
local write, read;
if todo=="write" then
write = true;
else
read = true;
end
if (write) then
local source = io.open(Root().."Mods/Infinity/System/Read/backup.lua", "w");
System.Log(TeamInstantAction:GetTeamScore(2).." for 2, and for 1: "..TeamInstantAction:GetTeamScore(1))
System.LogAlways("[System] Backing up serverdata to file 'backup.lua'");
source:write("--[[ The server is dependent on this file; editing it will lead to serious problems.If there is a problem with this file, please re-write it by accessing the backup system ingame.--]]");
source:write("Backup = {};Backup.Time = '"..os.date("%H:%M").."';Backup.Date = '"..os.date("%d/%m/%Y").."';");
source:write(XFormat("TeamInstantAction:SetTeamScore(2, %d);TeamInstantAction:SetTeamScore(1, %d);TeamInstantAction:UpdateScores();",TeamInstantAction:GetTeamScore(2), TeamInstantAction:GetTeamScore(1)));
source:close();
for i,player in pairs(g_gameRules.game:GetPlayers() or {}) do
if (IsModerator(player)) then
CMPlayer(player, "[!backup] Completed server backup.");
end
end
end
--local source = io.open(Root().."Mods/Infinity/System/Read/backup.lua", "r"); Can the file be open here and by the Lua scriptloader too?
if (read) then
System.LogAlways("[System] Restoring serverdata from file 'backup.lua'");
--source:close();
Backup = {};
Script.LoadScript(Root().."Mods/Infinity/System/Read/backup.lua");
if not Backup or #Backup < 1 then
System.LogAlways("[System] Error restoring serverdata from file 'backup.lua'");
end
end
end
感謝所有:)。
編輯:
儘管文件正在寫入到磁盤精細,系統無法讀取傾倒文件。
我以爲Script.LoadScript是一個全球Lua功能,我已被證明是錯誤的:)。這工作 - 我現在不能獎勵賞金,因爲我必須等待24小時,但當我可以這樣做時我會獎勵它。 – cybermonkey