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 /ClientHamr/GuiLua/skang.lua | |
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.
Diffstat (limited to '')
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 49 |
1 files changed, 34 insertions, 15 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 |