1-- This module checks the sprites of a quest.
2-- Only one function does the work: validator.check().
3
4-- TODO check the existence of mandatory sprites.
5
6local validator = {}
7
8local report = require("report")
9
10-- Checks an individual sprite.
11-- Returns a table animation_id -> animation.
12local function check_sprite(quest_path, sprite_id)
13
14  print("Checking sprite '" .. sprite_id .. "'")
15
16  local animations = {}
17
18  local env = {}
19  function env.animation(properties)
20
21    if properties.name == nil then
22      error("Animation without name", 2)
23    end
24
25    animations[properties.name] = properties
26
27    -- TODO check properties
28  end
29
30  local file = quest_path .. "sprites/" .. sprite_id .. ".dat"
31  local chunk, error = loadfile(file)
32  if chunk == nil then
33    report.error("Error in sprite '" .. sprite_id .. "': " .. error)
34  else
35    setfenv(chunk, env)
36    local success, error = pcall(chunk)
37
38    if not success then
39      report.error("Error in sprite '" .. sprite_id .. "': " .. error)
40    end
41  end
42
43  return animations
44end
45
46-- Checks all sprites.
47-- Returns a table of all sprite sheets: sprite_id -> animations.
48function validator.check(quest_path, resources)
49
50  print("Checking sprites")
51
52  local sprite_resource = resources.sprite
53  local sprites = {}
54
55  for _, sprite_element in ipairs(sprite_resource) do
56    local sprite_id = sprite_element.id
57    local sprite = check_sprite(quest_path, sprite_id)
58    sprites[sprite_id] = sprite
59  end
60
61  return sprites
62end
63
64return validator
65
66