AStopher
AStopher

Reputation: 4551

Reopening closed file: Lua

I have a file called backup.lua, which the program should write to every so often in order to backup its status, in case of a failure. The problem is that the program writes the backup.lua file completely fine first-time round, but any other times it refuses to write to the file.

I tried removing the file while the program was still open but Windows told me that the file was in use by 'CrysisWarsDedicatedServer.exe', which is the program. I have told the host Lua function to close the backup.lua file, so why isn't it letting me modify the file at will after it has been closed?

I can't find anything on the internet (Google actually tried to correct my search) and the secondary programmer on the project doesn't know either. So I'm wondering if any of you folks know what we are doing wrong here?

Host function code:

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

Thanks all :).

Edit:

Although the file is now written to the disk fine, the system fails to read the dumped file.

Upvotes: 3

Views: 426

Answers (1)

Roddy
Roddy

Reputation: 68064

So, now the problem is that the "LoadScript" function isn't doing what you expect:

Because I'm psychic, i have divined that you're writing a Crysis plugin, and are attempting to use it's LoadScript API call.

(Please don't assume everyone here would guess this, or be bothered to look for it. It's vital information that must form part of your questions)

The script you're writing attempts to set Backup - but your script, as written - does not separate lines with newline characters. As the first line is a comment, the entire script will be ignored.

Basicallty the script you've written looks like this, which is all treated as a comment.

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

You need to write a "\n" after the comment (and, I'd recommend in other places too) to make it like this. In fact, you don't really need block comments at all.

--  comment
Backup="Hello!"

Upvotes: 2

Related Questions