OLD | NEW |
1 -- Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 -- Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 -- Use of this source code is governed by a BSD-style license that can be | 2 -- Use of this source code is governed by a BSD-style license that can be |
3 -- found in the LICENSE file. | 3 -- found in the LICENSE file. |
4 | 4 |
5 -- Functions for validating game data files (.def files). | 5 -- Functions for validating game data files (.def files). |
6 -- This module provides a single global called 'validate' which | 6 -- This module provides a single global called 'validate' which |
7 -- contains two function: | 7 -- contains two function: |
8 -- ValidateGameDef | 8 -- ValidateGameDef |
9 -- ValidateLevelDef | 9 -- ValidateLevelDef |
10 -- | 10 -- |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 -- @param the level.def lua table (result of dofile()) | 86 -- @param the level.def lua table (result of dofile()) |
87 validate.ValidateLevelDef = function(filename, gamedef, leveldef) | 87 validate.ValidateLevelDef = function(filename, gamedef, leveldef) |
88 local function Err(message) | 88 local function Err(message) |
89 Error(filename, message) | 89 Error(filename, message) |
90 end | 90 end |
91 | 91 |
92 if type(leveldef) ~= 'table' then | 92 if type(leveldef) ~= 'table' then |
93 return Err("file does not evaluate to an object of type 'table'") | 93 return Err("file does not evaluate to an object of type 'table'") |
94 end | 94 end |
95 | 95 |
96 CheckValidKeys(filename, leveldef, { 'num_stars', 'shapes', 'sprites', 'scri
pt' }) | 96 CheckValidKeys(filename, leveldef, { 'num_stars', 'shapes', 'script' }) |
97 | 97 |
98 if leveldef.shapes then | 98 if leveldef.shapes then |
99 local valid_keys = { 'start', 'finish', 'color', 'type', 'anchor', 'tag'
, 'dynamic' } | 99 local valid_keys = { 'script', 'pos', 'children', 'sensor', 'image', 'st
art', 'finish', 'color', 'type', 'anchor', 'tag', 'dynamic' } |
100 local valid_types = { 'line', 'edge' } | 100 local valid_types = { 'compound', 'line', 'edge', 'image' } |
101 local required_keys = { 'type' } | 101 local required_keys = { 'type' } |
102 for _, shape in pairs(leveldef.shapes) do | 102 |
103 CheckValidKeys(filename, shape, valid_keys) | 103 local function ValidateShapeList(shapes) |
104 CheckRequiredKeys(filename, shape, required_keys, 'shape') | 104 for _, shape in pairs(shapes) do |
105 if not ListContains(valid_types, shape.type) then | 105 if #shape > 0 then |
106 Err('invalid shape type: ' .. shape.type) | 106 ValidateShapeList(shape) |
| 107 else |
| 108 CheckValidKeys(filename, shape, valid_keys) |
| 109 CheckRequiredKeys(filename, shape, required_keys, 'shape') |
| 110 if not ListContains(valid_types, shape.type) then |
| 111 Err('invalid shape type: ' .. shape.type) |
| 112 end |
| 113 end |
107 end | 114 end |
108 end | 115 end |
| 116 |
| 117 ValidateShapeList(leveldef.shapes) |
109 end | 118 end |
110 | |
111 if leveldef.sprites then | |
112 local valid_keys = { 'pos', 'tag', 'image', 'script', 'sensor' } | |
113 local required_keys = { 'pos', 'image' } | |
114 for _, sprite in pairs(leveldef.sprites) do | |
115 CheckValidKeys(filename, sprite, valid_keys) | |
116 CheckRequiredKeys(filename, sprite, required_keys, 'sprite') | |
117 if gamedef.assets[sprite.image] == nil then | |
118 Err('invalid image asset: ' .. sprite.image) | |
119 end | |
120 end | |
121 end | |
122 | |
123 end | 119 end |
124 | 120 |
125 if arg and #arg >= 1 then | 121 if debug.getinfo(1).what == "main" and debug.getinfo(3) == nil then |
126 -- When run from the command line run validation on passed in game.def file. | 122 -- When run from the command line run validation on passed in game.def file. |
127 local filename = arg[1] | 123 local filename = arg[1] |
128 if string.sub(filename, -4) == '.def' then | 124 local gamedef = util.LoadYaml(filename) |
129 local gamedef = util.LoadYaml(filename) | 125 gamedef.root = path.dirname(filename) |
130 gamedef.root = path.dirname(filename) | 126 validate.ValidateGameDef(filename, gamedef) |
131 validate.ValidateGameDef(filename, gamedef) | |
132 | 127 |
133 -- Now validate all the levels in the game. | 128 -- Now validate all the levels in the game. |
134 for _, level in ipairs(gamedef.levels) do | 129 for _, level in ipairs(gamedef.levels) do |
135 filename = path.join(gamedef.root, level) | 130 filename = path.join(gamedef.root, level) |
136 level = util.LoadYaml(filename) | 131 level = util.LoadYaml(filename) |
137 validate.ValidateLevelDef(filename, gamedef, level) | 132 validate.ValidateLevelDef(filename, gamedef, level) |
138 end | 133 end |
139 | 134 |
140 print("Validation successful!") | 135 print("Validation successful!") |
141 end | |
142 end | 136 end |
143 | 137 |
144 return validate | 138 return validate |
OLD | NEW |