我有一个名为 backup.lua 的文件,程序应该经常写入该文件以备份其状态,以防出现故障。 问题是程序在第一轮完全没问题地写入 backup.lua 文件,但在其他任何时候它都拒绝写入该文件。
我尝试在该程序仍处于打开状态时删除该文件,但 Windows 告诉我该文件正被“CrysisWarsDedicatedServer.exe”(即该程序)使用。我已经告诉宿主Lua函数关闭backup.lua文件,为什么它关闭后不让我随意修改呢?
我在互联网上找不到任何东西(谷歌实际上试图纠正我的搜索)并且该项目的二级程序员也不知道。 所以我想知道你们中是否有人知道我们在这里做错了什么?
宿主函数代码:
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
谢谢大家:)。
编辑:
虽然文件现在已经写入磁盘,但系统无法读取转储的文件。
最佳答案
所以,现在的问题是“LoadScript”函数没有按照您的预期进行:
因为我通灵,所以我猜到你在写 Crysis插件,并试图使用它的 LoadScript API call .
(请不要假设这里的每个人都会猜到这个,或者懒得去寻找它。这是必须构成您问题的一部分的重要信息)
您正在编写的脚本尝试设置 Backup
- 但您编写的脚本不会使用换行符分隔行。由于第一行是注释,整个脚本将被忽略。
基本上,您编写的脚本看起来像这样,都被视为注释。
--[[ comment ]]--Backup="Hello!"
你需要在评论后面写一个“\n”(我也建议在其他地方也这样)才能做到这一点。事实上,您根本不需要 block 注释。
-- comment
Backup="Hello!"
关于windows - 重新打开关闭的文件 : Lua,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17623612/