Skip to content

Commit

Permalink
Allow Yafc to tolerate requires of nonexistent files.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleStan committed Jan 8, 2025
1 parent c951e46 commit 9c0fe76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
8 changes: 7 additions & 1 deletion Yafc.Parser/FactorioDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ public static bool ModPathExists(string modName, string path) {
}

public static byte[] ReadModFile(string modName, string path) {
var info = allMods[modName];
if (!allMods.TryGetValue(modName, out var info)) {
// Treat nonexistent files as empty.
// This allows sandbox.lua to load __core__'s definition of data.extend, without breaking the tests (which completely replace data)
// Also, this may allow us to tolerate mods that `pcall(require("__other_mod__/something"))` without checking if an appropriate
// version of other_mod is loaded. (see https://github.com/ShadowTheAge/yafc/issues/199)
return [];
}

if (info.zipArchive != null) {
var entry = info.zipArchive.GetEntry(info.folder + path);
Expand Down
24 changes: 5 additions & 19 deletions Yafc/Data/Sandbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@ for defineType,typeTable in pairs(defines.prototypes) do
end
end

local function dataAdd(type, name, obj)
if not data.raw[type] then data.raw[type] = {} end;
data.raw[type][name] = obj;
--[[if parentTypes[type] then
dataAdd(parentTypes[type], name, obj);
end]]
end

data = {raw = {}, is_demo=false}
function data:extend(t)
for i=1,#t do
local prototype = t[i];
dataAdd(prototype.type, prototype.name, prototype);
end
end

require("__core__/lualib/dataloader.lua")

local raw_log = _G.raw_log;
Expand All @@ -53,9 +37,11 @@ table_size = function(t)
end
return count
end

data.data_crawler = "yafc "..yafc_version;

log(data.data_crawler);
if data then
-- If data isn't set, we couldn't load __core__/lualib/dataloader, which means we're running tests. They replace the entire data table.
data.data_crawler = "yafc "..yafc_version;
log(data.data_crawler);
end

size=32;

0 comments on commit 9c0fe76

Please sign in to comment.