2013-07-12 150 views
3

我有一个名为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 

感谢所有:)。

编辑:

尽管文件正在写入到磁盘精细,系统无法读取倾倒文件。

回答

2

所以,现在的问题是,“LoadScript”功能是不是做你所期望的:

因为我的心灵,我已经猜到你正在写一个Crysis插件,并试图使用它的LoadScript API call

(请不要以为这里大家会猜这个,还是懒得看它这是一个必须形成你的问题一部分的重要信息。)

你写尝试设置Backup脚本 - 但是您的脚本,如同书面的 - 不会将行与换行符分开。由于第一行是注释,整个脚本将被忽略。

Basicallty您写的脚本看起来像这样,它被视为评论。

--[[ comment ]]--Backup="Hello!" 

你需要写一个“\ n”的评论之后(和,我在其他地方也推荐),使它像这样。实际上,你根本不需要阻止评论。

-- comment 
Backup="Hello!" 
+0

我以为Script.LoadScript是一个全球Lua功能,我已被证明是错误的:)。这工作 - 我现在不能奖励赏金,因为我必须等待24小时,但当我可以这样做时我会奖励它。 – cybermonkey