aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-03-30 09:03:02 +1000
committerDavid Walter Seikel2014-03-30 09:03:02 +1000
commitb0aa1bd6b016c6f0bc054df19a2b73c95ea27300 (patch)
tree696b7b7c4a4bc66b503d88cf9fbcd762e3c37918
parentAdd fixNames() to sort out all but lowest level Thing names. (diff)
downloadSledjHamr-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.lua49
-rw-r--r--ClientHamr/GuiLua/test.lua25
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)
167end 167end
168 168
169function printTable(table, space) 169function 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
337On the other hand, might be better to now think of modules as a type?
338How much of OO are we willing to go with here? lol
339In Lua 5.1 and LuaJIT (?) metatables have no __type.
340What 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
437end 441end
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
105test.fooble = true 105test.fooble = true
106test.f = 42 106test.f = 42
107test.f = nil 107test.f = nil
108test.bar = 123
108print('') 109print('')
109 110
110skang.set(test, 'f', 'required', false) 111skang.set(test, 'f', 'required', false)
@@ -123,6 +124,10 @@ stuff.t = {}
123skang.thing{'a', module=stuff, help = 'A test stufflet'} 124skang.thing{'a', module=stuff, help = 'A test stufflet'}
124skang.thing{'b', module=stuff.t, help = 'A sub stufflet'} 125skang.thing{'b', module=stuff.t, help = 'A sub stufflet'}
125skang.thing{'c', module=stuff.t, help = 'Another sub stufflet'} 126skang.thing{'c', module=stuff.t, help = 'Another sub stufflet'}
127skang.thing{'s', module=stuff, help = 'A Stuff', types='table'}
128skang.thing{'sa', module=stuff.s, help = 'A stufflet in a Stuff'}
129skang.thing{'sb', module=stuff.s, help = 'Another stufflet in a Stuff'}
130
126print('*********************************') 131print('*********************************')
127skang.fixNames(skang, 'skang') 132skang.fixNames(skang, 'skang')
128skang.fixNames(test, 'test') 133skang.fixNames(test, 'test')
@@ -134,10 +139,14 @@ print('*********************************')
134print(skang.get(stuff, 'a', 'help')) 139print(skang.get(stuff, 'a', 'help'))
135print(skang.get(stuff.t, 'b', 'help')) 140print(skang.get(stuff.t, 'b', 'help'))
136print(skang.get(stuff.t, 'c', 'help')) 141print(skang.get(stuff.t, 'c', 'help'))
142print(skang.get(stuff, 's', 'help'))
143print(skang.get(stuff.s, 'sa,a', 'help'))
144print(skang.get(stuff.s, 'sb,b', 'help'))
137skang.thing{'baz,b', module=test, help = 'A test stufflet for test'} 145skang.thing{'baz,b', module=test, help = 'A test stufflet for test'}
138print(skang.get(test, 'b', 'help')) 146print(skang.get(test, 'b', 'help'))
139print(skang.get(test, 'f', 'help')) 147print(skang.get(test, 'f', 'help'))
140stuff.a = '1' 148-- Should fail isValid()
149stuff.a = 1
141stuff.t.b = '2' 150stuff.t.b = '2'
142stuff.t.c = '3' 151stuff.t.c = '3'
143test.b = '422222' 152test.b = '422222'
@@ -145,6 +154,14 @@ test.f = 5
145test_c.cbar = '666' 154test_c.cbar = '666'
146-- This one doesn't actually exist. 155-- This one doesn't actually exist.
147test_c.bar = '7' 156test_c.bar = '7'
157-- The sa should fail isValid()
158stuff.s.sa = true
159stuff.s.sb = 22
160-- TODO - This one should fail, but doesn't. It goes through to the real stuff.s table.
161stuff.s.b = 33
162-- TODO - And the 'a' one just gets dropped.
163stuff.s = {a=8, sb='9'}
164stuff.s.sb = 44
148print('') 165print('')
149 166
150print(skang.get(stuff, 'a')) 167print(skang.get(stuff, 'a'))
@@ -156,6 +173,9 @@ print(skang.get(test, 'f'))
156print(skang.get(test, 'fooble')) 173print(skang.get(test, 'fooble'))
157print(skang.get(test_c, 'cbar')) 174print(skang.get(test_c, 'cbar'))
158print(skang.get(test_c, 'bar')) 175print(skang.get(test_c, 'bar'))
176print(type(skang.get(stuff, 's')))
177print(skang.get(stuff.s, 'sa'))
178print(skang.get(stuff.s, 'sb'))
159print('') 179print('')
160 180
161print(stuff.a) 181print(stuff.a)
@@ -169,3 +189,6 @@ print(test_c.cbar)
169print(test_c.bar) 189print(test_c.bar)
170print(test_c.c) 190print(test_c.c)
171print(test_c.cfooble) 191print(test_c.cfooble)
192print(stuff.s.sa)
193print(stuff.s.sb)
194skang.printTableStart(stuff.s, '', 'stuff.s')