【问题标题】:Reopening closed file: Lua重新打开关闭的文件:Lua
【发布时间】:2013-07-11 12:13:49
【问题描述】:

我有一个名为 backup.lua 的文件,程序应该经常写入该文件以备份其状态,以防万一发生故障。 问题是程序在第一次循环中完全正常地写入了 backup.lua 文件,但在其他任何时候它都拒绝写入该文件。

我尝试在程序仍处于打开状态时删除该文件,但 Windows 告诉我该文件正被程序“CrysisWarsDedicatedServer.exe”使用。我已经告诉宿主Lua函数关闭backup.lua文件,为什么关闭后不让我随意修改文件?

我在互联网上找不到任何东西(Google 实际上试图更正我的搜索),该项目的二级程序员也不知道。 所以我想知道你们中是否有人知道我们在这里做错了什么?

主机功能码:

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

谢谢大家:)。

编辑:

虽然文件现在已正常写入磁盘,但系统无法读取转储文件。

【问题讨论】:

    标签: windows lua crysis


    【解决方案1】:

    所以,现在的问题是“LoadScript”函数没有按照您的预期进行:

    因为我是通灵者,所以我猜到你正在编写一个Crysis 插件,并试图使用它是LoadScript API call

    (请不要假设这里的每个人都会猜到这一点,或者会费心去寻找它。这是您的问题中必须包含的重要信息)

    您正在编写的脚本尝试设置Backup - 但您的脚本,如所写的那样 - 不会用换行符分隔行。由于第一行是注释,整个脚本将被忽略。

    你写的脚本基本上是这样的,都被当成注释了。

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

    您需要在评论后写一个“\n”(我也建议在其他地方)以使其成为这样。事实上,你根本不需要块 cmets。

    --  comment
    Backup="Hello!"
    

    【讨论】:

    • 我认为 Script.LoadScript 是一个全局 Lua 函数,但事实证明我错了 :)。这行得通 - 我现在无法奖励赏金,因为我必须等待 24 小时,但我会在可以这样做的时候奖励它:)。
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多