diff options
author | David Walter Seikel | 2014-03-30 09:03:02 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-03-30 09:03:02 +1000 |
commit | b0aa1bd6b016c6f0bc054df19a2b73c95ea27300 (patch) | |
tree | 696b7b7c4a4bc66b503d88cf9fbcd762e3c37918 | |
parent | Add fixNames() to sort out all but lowest level Thing names. (diff) | |
download | SledjHamr-b0aa1bd6b016c6f0bc054df19a2b73c95ea27300.zip SledjHamr-b0aa1bd6b016c6f0bc054df19a2b73c95ea27300.tar.gz SledjHamr-b0aa1bd6b016c6f0bc054df19a2b73c95ea27300.tar.bz2 SledjHamr-b0aa1bd6b016c6f0bc054df19a2b73c95ea27300.tar.xz |
Various fixes, mostly to do with stufflets, which are still slightly broken.
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 49 | ||||
-rw-r--r-- | ClientHamr/GuiLua/test.lua | 25 |
2 files changed, 58 insertions, 16 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index 7333e54..a742845 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -167,6 +167,7 @@ function printTableStart(table, space, name) | |||
167 | end | 167 | end |
168 | 168 | ||
169 | function printTable(table, space) | 169 | function printTable(table, space) |
170 | if nil == table then return end | ||
170 | for k, v in pairs(table) do | 171 | for k, v in pairs(table) do |
171 | if type(v) == "table" then | 172 | if type(v) == "table" then |
172 | if v._NAME then | 173 | if v._NAME then |
@@ -334,12 +335,6 @@ Other Thing things are - | |||
334 | quitter:action('quit') | 335 | quitter:action('quit') |
335 | 336 | ||
336 | 337 | ||
337 | On the other hand, might be better to now think of modules as a type? | ||
338 | How much of OO are we willing to go with here? lol | ||
339 | In Lua 5.1 and LuaJIT (?) metatables have no __type. | ||
340 | What about userdata? We could hijack the type() function, and dig deeper if it's a userdata, or even if it's a Thing? | ||
341 | |||
342 | |||
343 | Things as a metatable field - | 338 | Things as a metatable field - |
344 | In this plan test.__something is short for metatable(test).__something. | 339 | In this plan test.__something is short for metatable(test).__something. |
345 | 340 | ||
@@ -363,8 +358,6 @@ What about userdata? We could hijack the type() function, and dig deeper if it' | |||
363 | If module.foo is a table, it can have it's own metatable(foo). | 358 | If module.foo is a table, it can have it's own metatable(foo). |
364 | Such a table will use Thing as a proxy table via __index and __newindex, as it does now. | 359 | Such a table will use Thing as a proxy table via __index and __newindex, as it does now. |
365 | 360 | ||
366 | thing(test, 'foo', 'table' ...) -> setmetatable(thing, {__index=Thing}); thing stuff goes into thing; setmetatable(foo, thing); test.__stuff[foo] = thing; test.__values[foo] = {}; | ||
367 | thing(test.foo, 'bar', ...) -> setmetatable(thing, {__index=Thing}); setmetatable(foo, thing); foo.__stuff[bar] = thing; foo.__values[bar] = thing.default | ||
368 | test.foo -> test.__index(test, 'foo') -> test.__values[foo]; if that's nil, and test.__stuff[foo], then return an empty table instead? | 361 | test.foo -> test.__index(test, 'foo') -> test.__values[foo]; if that's nil, and test.__stuff[foo], then return an empty table instead? |
369 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) | 362 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) |
370 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) (but it's not a function) -> test.__values[foo].__call(test.__values[foo], a) | 363 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) (but it's not a function) -> test.__values[foo].__call(test.__values[foo], a) |
@@ -418,13 +411,13 @@ Thing.isValid = function (self, module) -- Check if this Thing is valid, return | |||
418 | local key = thingy.names[1]; | 411 | local key = thingy.names[1]; |
419 | local value = modThing.__values[key] | 412 | local value = modThing.__values[key] |
420 | 413 | ||
421 | local t = type(value) | 414 | local t = type(value) or 'nil' |
422 | self.errors = {} | 415 | self.errors = {} |
423 | -- TODO - Naturally there should be formatting functions for stuffing Thing stuff into strings, and overridable output functions. | 416 | -- TODO - Naturally there should be formatting functions for stuffing Thing stuff into strings, and overridable output functions. |
424 | if 'nil' == t then | 417 | if 'nil' == t then |
425 | if self.required then table.insert(self.errors, modThing.names[1] .. '.' .. name .. ' is required!') end | 418 | if self.required then table.insert(self.errors, modThing.names[1] .. '.' .. name .. ' is required!') end |
426 | else | 419 | else |
427 | if self.types[1] ~= t then table.insert(self.errors, modThing.names[1] .. '.' .. name .. ' should be a ' .. self.types[1] .. ', but it is a ' .. type(value) .. '!') | 420 | if self.types[1] ~= t then table.insert(self.errors, modThing.names[1] .. '.' .. name .. ' should be a ' .. self.types[1] .. ', but it is a ' .. t .. '!') |
428 | else | 421 | else |
429 | if 'number' == t then value = '' .. value end | 422 | if 'number' == t then value = '' .. value end |
430 | if ('number' == t) or ('string' == t) then | 423 | if ('number' == t) or ('string' == t) then |
@@ -432,7 +425,18 @@ Thing.isValid = function (self, module) -- Check if this Thing is valid, return | |||
432 | end | 425 | end |
433 | end | 426 | end |
434 | end | 427 | end |
435 | -- TODO - Should go through self.__stuff[*]:isValid(self) as well. | 428 | |
429 | local stuff = getmetatable(value) | ||
430 | if stuff and stuff.__stuff then | ||
431 | for k, v in pairs(stuff.__stuff) do | ||
432 | if not v:isValid(value) then | ||
433 | for i, w in ipairs(v.errors) do | ||
434 | table.insert(self.errors, w) | ||
435 | end | ||
436 | end | ||
437 | end | ||
438 | end | ||
439 | |||
436 | return #(self.errors) == 0 | 440 | return #(self.errors) == 0 |
437 | end | 441 | end |
438 | 442 | ||
@@ -468,11 +472,21 @@ Thing.__newindex = function (module, key, value) | |||
468 | local modThing = getmetatable(module) | 472 | local modThing = getmetatable(module) |
469 | 473 | ||
470 | if modThing then | 474 | if modThing then |
471 | -- This is a proxy table, the values never exist in the real table. | 475 | -- This is a proxy table, the values never exist in the real table. In theory. |
472 | local thingy = modThing.__stuff[key] | 476 | local thingy = modThing.__stuff[key] |
473 | if thingy then | 477 | if thingy then |
474 | local name = thingy.names[1] | 478 | local name = thingy.names[1] |
475 | modThing.__values[name] = value | 479 | local valueMeta |
480 | if 'table' == type(value) then | ||
481 | -- Coz setting it via modThing screws with the __index stuff somehow. | ||
482 | valueMeta = getmetatable(modThing.__values[name]) | ||
483 | if valueMeta then | ||
484 | for k, v in pairs(value) do | ||
485 | valueMeta.__values[k] = v | ||
486 | end | ||
487 | end | ||
488 | end | ||
489 | if nil == valueMeta then modThing.__values[name] = value end | ||
476 | if 'function' == type(value) then | 490 | if 'function' == type(value) then |
477 | thingy.func = value | 491 | thingy.func = value |
478 | else | 492 | else |
@@ -527,7 +541,6 @@ thing = function (names, ...) | |||
527 | local modThing = getmetatable(module) | 541 | local modThing = getmetatable(module) |
528 | if nil == modThing then | 542 | if nil == modThing then |
529 | modThing = {} | 543 | modThing = {} |
530 | modThing.names = {module._NAME} | ||
531 | -- This is how the Thing is stored with the module. | 544 | -- This is how the Thing is stored with the module. |
532 | setmetatable(module, modThing) | 545 | setmetatable(module, modThing) |
533 | end | 546 | end |
@@ -536,6 +549,7 @@ thing = function (names, ...) | |||
536 | if nil == modThing.__stuff then | 549 | if nil == modThing.__stuff then |
537 | -- Seems this does not deal with __index and __newindex, and also screws up __stuff somehow. | 550 | -- Seems this does not deal with __index and __newindex, and also screws up __stuff somehow. |
538 | -- setmetatable(modThing, {__index = Thing}) | 551 | -- setmetatable(modThing, {__index = Thing}) |
552 | modThing.names = {module._NAME or 'NoName'} | ||
539 | modThing.__stuff = {} | 553 | modThing.__stuff = {} |
540 | modThing.__values = {} | 554 | modThing.__values = {} |
541 | modThing.__index = Thing.__index | 555 | modThing.__index = Thing.__index |
@@ -578,9 +592,14 @@ thing = function (names, ...) | |||
578 | -- Find type, default to string, then break out the other types. | 592 | -- Find type, default to string, then break out the other types. |
579 | local typ = type(thingy.default) | 593 | local typ = type(thingy.default) |
580 | if 'nil' == typ then typ = 'string' end | 594 | if 'nil' == typ then typ = 'string' end |
581 | if types then types = typ .. ',' .. types else types = typ end | 595 | if 'function' == typ then types = typ .. ',' .. types end |
596 | if '' == types then types = typ end | ||
582 | thingy.types = csv2table(types) | 597 | thingy.types = csv2table(types) |
583 | 598 | ||
599 | if 'table' == thingy.types[1] then | ||
600 | if '' == thingy.default then thingy.default = {} end | ||
601 | end | ||
602 | |||
584 | -- Remove old names, then stash the Thing under all of it's new names. | 603 | -- Remove old names, then stash the Thing under all of it's new names. |
585 | for i, v in ipairs(oldNames) do | 604 | for i, v in ipairs(oldNames) do |
586 | modThing.__stuff[v] = nil | 605 | modThing.__stuff[v] = nil |
diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index 73cacee..d74e22c 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua | |||
@@ -105,6 +105,7 @@ test.fooble = 42 | |||
105 | test.fooble = true | 105 | test.fooble = true |
106 | test.f = 42 | 106 | test.f = 42 |
107 | test.f = nil | 107 | test.f = nil |
108 | test.bar = 123 | ||
108 | print('') | 109 | print('') |
109 | 110 | ||
110 | skang.set(test, 'f', 'required', false) | 111 | skang.set(test, 'f', 'required', false) |
@@ -123,6 +124,10 @@ stuff.t = {} | |||
123 | skang.thing{'a', module=stuff, help = 'A test stufflet'} | 124 | skang.thing{'a', module=stuff, help = 'A test stufflet'} |
124 | skang.thing{'b', module=stuff.t, help = 'A sub stufflet'} | 125 | skang.thing{'b', module=stuff.t, help = 'A sub stufflet'} |
125 | skang.thing{'c', module=stuff.t, help = 'Another sub stufflet'} | 126 | skang.thing{'c', module=stuff.t, help = 'Another sub stufflet'} |
127 | skang.thing{'s', module=stuff, help = 'A Stuff', types='table'} | ||
128 | skang.thing{'sa', module=stuff.s, help = 'A stufflet in a Stuff'} | ||
129 | skang.thing{'sb', module=stuff.s, help = 'Another stufflet in a Stuff'} | ||
130 | |||
126 | print('*********************************') | 131 | print('*********************************') |
127 | skang.fixNames(skang, 'skang') | 132 | skang.fixNames(skang, 'skang') |
128 | skang.fixNames(test, 'test') | 133 | skang.fixNames(test, 'test') |
@@ -134,10 +139,14 @@ print('*********************************') | |||
134 | print(skang.get(stuff, 'a', 'help')) | 139 | print(skang.get(stuff, 'a', 'help')) |
135 | print(skang.get(stuff.t, 'b', 'help')) | 140 | print(skang.get(stuff.t, 'b', 'help')) |
136 | print(skang.get(stuff.t, 'c', 'help')) | 141 | print(skang.get(stuff.t, 'c', 'help')) |
142 | print(skang.get(stuff, 's', 'help')) | ||
143 | print(skang.get(stuff.s, 'sa,a', 'help')) | ||
144 | print(skang.get(stuff.s, 'sb,b', 'help')) | ||
137 | skang.thing{'baz,b', module=test, help = 'A test stufflet for test'} | 145 | skang.thing{'baz,b', module=test, help = 'A test stufflet for test'} |
138 | print(skang.get(test, 'b', 'help')) | 146 | print(skang.get(test, 'b', 'help')) |
139 | print(skang.get(test, 'f', 'help')) | 147 | print(skang.get(test, 'f', 'help')) |
140 | stuff.a = '1' | 148 | -- Should fail isValid() |
149 | stuff.a = 1 | ||
141 | stuff.t.b = '2' | 150 | stuff.t.b = '2' |
142 | stuff.t.c = '3' | 151 | stuff.t.c = '3' |
143 | test.b = '422222' | 152 | test.b = '422222' |
@@ -145,6 +154,14 @@ test.f = 5 | |||
145 | test_c.cbar = '666' | 154 | test_c.cbar = '666' |
146 | -- This one doesn't actually exist. | 155 | -- This one doesn't actually exist. |
147 | test_c.bar = '7' | 156 | test_c.bar = '7' |
157 | -- The sa should fail isValid() | ||
158 | stuff.s.sa = true | ||
159 | stuff.s.sb = 22 | ||
160 | -- TODO - This one should fail, but doesn't. It goes through to the real stuff.s table. | ||
161 | stuff.s.b = 33 | ||
162 | -- TODO - And the 'a' one just gets dropped. | ||
163 | stuff.s = {a=8, sb='9'} | ||
164 | stuff.s.sb = 44 | ||
148 | print('') | 165 | print('') |
149 | 166 | ||
150 | print(skang.get(stuff, 'a')) | 167 | print(skang.get(stuff, 'a')) |
@@ -156,6 +173,9 @@ print(skang.get(test, 'f')) | |||
156 | print(skang.get(test, 'fooble')) | 173 | print(skang.get(test, 'fooble')) |
157 | print(skang.get(test_c, 'cbar')) | 174 | print(skang.get(test_c, 'cbar')) |
158 | print(skang.get(test_c, 'bar')) | 175 | print(skang.get(test_c, 'bar')) |
176 | print(type(skang.get(stuff, 's'))) | ||
177 | print(skang.get(stuff.s, 'sa')) | ||
178 | print(skang.get(stuff.s, 'sb')) | ||
159 | print('') | 179 | print('') |
160 | 180 | ||
161 | print(stuff.a) | 181 | print(stuff.a) |
@@ -169,3 +189,6 @@ print(test_c.cbar) | |||
169 | print(test_c.bar) | 189 | print(test_c.bar) |
170 | print(test_c.c) | 190 | print(test_c.c) |
171 | print(test_c.cfooble) | 191 | print(test_c.cfooble) |
192 | print(stuff.s.sa) | ||
193 | print(stuff.s.sb) | ||
194 | skang.printTableStart(stuff.s, '', 'stuff.s') | ||