diff options
Diffstat (limited to 'ClientHamr')
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 102 | ||||
-rw-r--r-- | ClientHamr/GuiLua/test.lua | 22 |
2 files changed, 67 insertions, 57 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index 24f3dbc..e332dbb 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -529,6 +529,23 @@ Thing.remove = function (self) -- Delete this Thing. | |||
529 | end | 529 | end |
530 | 530 | ||
531 | 531 | ||
532 | getStuffed = function (parent, key) | ||
533 | local metaMum = getmetatable(parent) | ||
534 | local thingy | ||
535 | |||
536 | if metaMum and metaMum.__self then | ||
537 | thingy = metaMum.__self.stuff[key] | ||
538 | |||
539 | if not thingy then | ||
540 | -- Deal with getting a table entry. | ||
541 | if metaMum.__values[key] then | ||
542 | thingy = getmetatable(metaMum.__values[key]).__self | ||
543 | end | ||
544 | end | ||
545 | end | ||
546 | return metaMum, thingy | ||
547 | end | ||
548 | |||
532 | local Mum = | 549 | local Mum = |
533 | { | 550 | { |
534 | __index = function (parent, key) | 551 | __index = function (parent, key) |
@@ -537,13 +554,10 @@ __index = function (parent, key) | |||
537 | -- Ah, it was doing isValid() on setStufflet(). | 554 | -- Ah, it was doing isValid() on setStufflet(). |
538 | 555 | ||
539 | -- First see if this is a Thing. | 556 | -- First see if this is a Thing. |
540 | local metaMum = getmetatable(parent) | 557 | local metaMum, thingy = getStuffed(parent, key) |
541 | 558 | ||
542 | if metaMum and metaMum.__self then | 559 | if thingy then |
543 | local thingy = metaMum.__self.stuff[key] | 560 | return metaMum.__values[thingy.names[1] ] or thingy.default |
544 | if thingy then | ||
545 | return metaMum.__values[thingy.names[1] ] or thingy.default | ||
546 | end | ||
547 | end | 561 | end |
548 | 562 | ||
549 | -- Then see if we can inherit it from Thing. | 563 | -- Then see if we can inherit it from Thing. |
@@ -554,56 +568,50 @@ __newindex = function (parent, key, value) | |||
554 | -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. | 568 | -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. |
555 | 569 | ||
556 | -- First see if this is a Thing. | 570 | -- First see if this is a Thing. |
557 | local metaMum = getmetatable(parent) | 571 | local metaMum, thingy = getStuffed(parent, key) |
558 | 572 | ||
559 | if metaMum and metaMum.__self then | 573 | if not thingy then |
560 | local thingy = metaMum.__self.stuff[key] | 574 | if metaMum.__self.isKeyed then |
561 | |||
562 | if not thingy then | ||
563 | -- Deal with setting a new Keyed table entry. | 575 | -- Deal with setting a new Keyed table entry. |
564 | if metaMum.__self.isKeyed and (nil == metaMum.__values[key]) then | 576 | local newThing = copy(parent, key) |
565 | local newThing = copy(parent, key) | 577 | rawset(metaMum.__values, key, newThing) |
566 | rawset(metaMum.__values, key, newThing) | 578 | thingy = {names={key}, types={'table'}, parent=newThing, stuff=getmetatable(newThing).__self.stuff, } |
567 | thingy = {names={key}, types={'table'}, parent=newThing, stuff=getmetatable(newThing).__self.stuff, } | 579 | setmetatable(thingy, {__index = Thing}) -- To pick up isValid, pattern, and the other stuff by default. |
568 | setmetatable(thingy, {__index = Thing}) -- To pick up isValid, pattern, and the other stuff by default. | ||
569 | metaMum.__self.stuff[key] = thingy | ||
570 | end | ||
571 | end | 580 | end |
581 | end | ||
572 | 582 | ||
573 | if thingy then | 583 | if thingy then |
574 | local name = thingy.names[1] | 584 | local name = thingy.names[1] |
575 | local oldMum | 585 | local oldMum |
576 | 586 | ||
577 | if 'table' == type(value) then | 587 | if 'table' == type(value) then |
578 | -- Coz setting it via metaMum screws with the __index stuff somehow. | 588 | -- Coz setting it via metaMum screws with the __index stuff somehow. |
579 | local oldValue = metaMum.__values[name] | 589 | local oldValue = metaMum.__values[name] |
580 | if 'table' == type(oldValue) then | 590 | if 'table' == type(oldValue) then |
581 | oldMum = getmetatable(oldValue) | 591 | oldMum = getmetatable(oldValue) |
582 | if oldMum then | 592 | if oldMum then |
583 | -- TODO - This SHOULD work, but doesn't. | 593 | -- TODO - This SHOULD work, but doesn't. |
584 | --setmetatable(value, oldMum) | 594 | --setmetatable(value, oldMum) |
585 | -- Instead we do this - | 595 | -- Instead we do this - |
586 | -- TODO - This wont clear out any values in the old table that are not in the new table. Should it? | 596 | -- TODO - This wont clear out any values in the old table that are not in the new table. Should it? |
587 | for k, v in pairs(value) do | 597 | for k, v in pairs(value) do |
588 | local newK = oldMum.__self.stuff[k] | 598 | local newK = oldMum.__self.stuff[k] |
589 | if newK then newK = newK.names[1] else newK = k end | 599 | if newK then newK = newK.names[1] else newK = k end |
590 | oldMum.__values[newK] = v | 600 | oldMum.__values[newK] = v |
591 | end | ||
592 | |||
593 | end | 601 | end |
594 | end | 602 | end |
595 | end | 603 | end |
596 | if nil == oldMum then metaMum.__values[name] = value end | 604 | end |
597 | -- NOTE - invalid values are still stored, this is by design. | 605 | if nil == oldMum then metaMum.__values[name] = value end |
598 | if not thingy:isValid(parent) then | 606 | -- NOTE - invalid values are still stored, this is by design. |
599 | for i, v in ipairs(thingy.errors) do | 607 | if not thingy:isValid(parent) then |
600 | print('ERROR - ' .. v) | 608 | for i, v in ipairs(thingy.errors) do |
601 | end | 609 | print('ERROR - ' .. v) |
602 | end | 610 | end |
603 | -- TODO - Go through it's linked things and set them to. | ||
604 | -- Done, don't fall through to the rawset() | ||
605 | return | ||
606 | end | 611 | end |
612 | -- TODO - Go through it's linked things and set them to. | ||
613 | -- Done, don't fall through to the rawset() | ||
614 | return | ||
607 | end | 615 | end |
608 | 616 | ||
609 | rawset(parent, key, value) -- Stuff it normally. | 617 | rawset(parent, key, value) -- Stuff it normally. |
diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index 9e3000a..da8af8a 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua | |||
@@ -161,15 +161,16 @@ test_c.bar = '7' | |||
161 | stuff.s.sa = true | 161 | stuff.s.sa = true |
162 | stuff.s.sb = 22 | 162 | stuff.s.sb = 22 |
163 | stuff.s.b = 33 | 163 | stuff.s.b = 33 |
164 | -- NOTE - This triggers isValid twice for each table element. | 164 | print('') |
165 | -- NOTE - This triggers isValid() twice for each table element. | ||
165 | stuff.s = {a=8, sb='9'} | 166 | stuff.s = {a=8, sb='9'} |
166 | stuff.s.sb = 99 | ||
167 | print('') | 167 | print('') |
168 | --stuff.S.record0 = {} | 168 | stuff.s.sb = 99 |
169 | -- NOTE - Yet this doesn't trigger isValid() twice. | ||
169 | stuff.S['record0'] = {field0=0, field1='zero'} | 170 | stuff.S['record0'] = {field0=0, field1='zero'} |
170 | stuff.S['record1'] = {field0='1', field1='one'} | 171 | stuff.S['record1'] = {field0='1', field1='one'} |
171 | -- TODO - adding a third makes isValid() crash. | 172 | -- TODO - adding a third makes isValid() crash. |
172 | --stuff.S['record2'] = {field0='2', field1='two'} | 173 | stuff.S['record2'] = {field0='2', field1='two'} |
173 | 174 | ||
174 | print('') | 175 | print('') |
175 | 176 | ||
@@ -200,18 +201,19 @@ print(test_c.c) | |||
200 | print(test_c.cfooble) | 201 | print(test_c.cfooble) |
201 | print(stuff.s.sa) | 202 | print(stuff.s.sa) |
202 | print(stuff.s.sb) | 203 | print(stuff.s.sb) |
203 | --skang.printTableStart(stuff.s, '', 'stuff.s') | ||
204 | print('') | 204 | print('') |
205 | 205 | ||
206 | skang.printTableStart(stuff.s, '', 'stuff.s') | ||
206 | skang.printTableStart(stuff.S, '', 'stuff.S') | 207 | skang.printTableStart(stuff.S, '', 'stuff.S') |
208 | --skang.printTableStart(getmetatable(stuff.S), '', 'stuff.S metatable') | ||
207 | 209 | ||
208 | print(stuff.S['record0'].field1) | 210 | print(stuff.S['record0'].field1) |
209 | print(stuff.S['record1'].field1) | 211 | print(stuff.S['record1'].field0) |
210 | --print(stuff.S['record2'].field0) | 212 | print(stuff.S['record2'].field1) |
211 | 213 | ||
212 | --skang.printTableStart(stuff.S['record0'], '', 'stuff.S[record0]') | 214 | --skang.printTableStart(getmetatable(stuff.S['record0']), '', 'metatable stuff.S[record0]') |
213 | --skang.printTableStart(stuff.S['record1'], '', 'stuff.S[record1]') | 215 | --skang.printTableStart(getmetatable(stuff.S['record1']), '', 'metatable stuff.S[record1]') |
214 | --skang.printTableStart(stuff.S['record2'], '', 'stuff.S[record2]') | 216 | --skang.printTableStart(getmetatable(stuff.S['record2']), '', 'metatable stuff.S[record2]') |
215 | 217 | ||
216 | --skang.printTableStart(getmetatable(stuff.s), '', 'stuff.s metatable') | 218 | --skang.printTableStart(getmetatable(stuff.s), '', 'stuff.s metatable') |
217 | --skang.printTableStart(getmetatable(stuff), '', 'stuff metatable') | 219 | --skang.printTableStart(getmetatable(stuff), '', 'stuff metatable') |