diff options
author | David Walter Seikel | 2014-03-30 04:25:29 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-03-30 04:25:29 +1000 |
commit | aaf78bdc4fe8d0f6a0dd99334b1f5c73697e8a2f (patch) | |
tree | c79437315164a199adebf06e00bfbb6c7f44baaa /ClientHamr | |
parent | No doubling up on the default types. (diff) | |
download | SledjHamr-aaf78bdc4fe8d0f6a0dd99334b1f5c73697e8a2f.zip SledjHamr-aaf78bdc4fe8d0f6a0dd99334b1f5c73697e8a2f.tar.gz SledjHamr-aaf78bdc4fe8d0f6a0dd99334b1f5c73697e8a2f.tar.bz2 SledjHamr-aaf78bdc4fe8d0f6a0dd99334b1f5c73697e8a2f.tar.xz |
Local thing -> thingy, plus a few minor cleanups around that.
Diffstat (limited to 'ClientHamr')
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 135 |
1 files changed, 66 insertions, 69 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index 2ab9f94..6588c1d 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -418,8 +418,8 @@ Thing.isValid = function (self, module) -- Check if this Thing is valid, return | |||
418 | -- Anything that overrides this method, should call this super method first. | 418 | -- Anything that overrides this method, should call this super method first. |
419 | local name = self.names[1] | 419 | local name = self.names[1] |
420 | local modThing = getmetatable(module) | 420 | local modThing = getmetatable(module) |
421 | local thing = modThing.__stuff[name] | 421 | local thingy = modThing.__stuff[name] |
422 | local key = thing.names[1]; | 422 | local key = thingy.names[1]; |
423 | local value = modThing.__values[key] | 423 | local value = modThing.__values[key] |
424 | 424 | ||
425 | local t = type(value) | 425 | local t = type(value) |
@@ -448,26 +448,23 @@ Thing.__index = function (module, key) | |||
448 | -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. | 448 | -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. |
449 | -- TODO - Java skang called isValid() on get(). On the other hand, doesn't seem to call it on set(), but calls it on append(). | 449 | -- TODO - Java skang called isValid() on get(). On the other hand, doesn't seem to call it on set(), but calls it on append(). |
450 | -- Ah, it was doing isValid() on setStufflet(). | 450 | -- Ah, it was doing isValid() on setStufflet(). |
451 | -- TODO - Call thing.func() if it exists. | 451 | -- TODO - Call thingy.func() if it exists. |
452 | 452 | ||
453 | -- First see if this is a Thing. | 453 | -- First see if this is a Thing. |
454 | local modThing = getmetatable(module) | 454 | local modThing = getmetatable(module) |
455 | local thingy | ||
455 | 456 | ||
456 | if modThing then | 457 | if modThing then |
457 | local thing = modThing.__stuff[key] | 458 | thingy = modThing.__stuff[key] |
458 | 459 | if thingy then | |
459 | if thing then | 460 | local name = thingy.names[1]; |
460 | local name = thing.names[1]; | 461 | return modThing.__values[name] or thingy.default |
461 | return modThing.__values[name] or thing.__stuff[name].default | ||
462 | end | 462 | end |
463 | end | 463 | end |
464 | 464 | ||
465 | -- Then see if we can inherit it from Thing. | 465 | -- Then see if we can inherit it from Thing. |
466 | thing = Thing[key] | 466 | thingy = Thing[key] |
467 | if thing then return thing end | 467 | return thingy |
468 | |||
469 | -- If all else fails, return nil. | ||
470 | return nil | ||
471 | end | 468 | end |
472 | 469 | ||
473 | Thing.__newindex = function (module, key, value) | 470 | Thing.__newindex = function (module, key, value) |
@@ -476,20 +473,21 @@ Thing.__newindex = function (module, key, value) | |||
476 | 473 | ||
477 | if modThing then | 474 | if modThing then |
478 | -- 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. |
479 | local thing = modThing.__stuff[key] | 476 | local thingy = modThing.__stuff[key] |
480 | local name = thing.names[1] | 477 | if thingy then |
481 | modThing.__values[name] = value | 478 | local name = thingy.names[1] |
482 | if 'function' == type(value) then | 479 | modThing.__values[name] = value |
483 | thing.func = value | 480 | if 'function' == type(value) then |
484 | local types = '' | 481 | thingy.func = value |
485 | else | 482 | else |
486 | -- NOTE - invalid values are still stored, this is by design. | 483 | -- NOTE - invalid values are still stored, this is by design. |
487 | if not thing:isValid(module) then | 484 | if not thingy:isValid(module) then |
488 | for i, v in ipairs(thing.errors) do | 485 | for i, v in ipairs(thingy.errors) do |
489 | print('ERROR - ' .. v) | 486 | print('ERROR - ' .. v) |
490 | end | 487 | end |
488 | end | ||
489 | -- TODO - Go through it's linked things and set them to. | ||
491 | end | 490 | end |
492 | -- TODO - Go through it's linked things and set them to. | ||
493 | end | 491 | end |
494 | else | 492 | else |
495 | rawset(module, key, value) -- Stuff it normally. | 493 | rawset(module, key, value) -- Stuff it normally. |
@@ -538,77 +536,76 @@ thing = function (names, ...) | |||
538 | modThing[k] = modThing[k] or v | 536 | modThing[k] = modThing[k] or v |
539 | end | 537 | end |
540 | end | 538 | end |
541 | local thing = modThing.__stuff[name] | 539 | |
542 | if not thing then -- This is a new Thing. | 540 | local thingy = modThing.__stuff[name] |
541 | if not thingy then -- This is a new Thing. | ||
543 | new = true | 542 | new = true |
544 | thing = {} | 543 | thingy = {} |
545 | -- Grab the environment of the calling function, so this new thing automatically becomes a global in it. | 544 | thingy.module = module |
546 | thing.module = module | 545 | thingy.names = names |
547 | thing.names = names | 546 | setmetatable(thingy, {__index = Thing}) |
548 | setmetatable(thing, {__index = Thing}) | ||
549 | end | 547 | end |
550 | 548 | ||
551 | -- Pull out positional arguments. | 549 | -- Pull out positional arguments. |
552 | thing.help = params[1] or thing.help | 550 | thingy.help = params[1] or thingy.help |
553 | thing.default = params[2] or thing.default | 551 | thingy.default = params[2] or thingy.default |
554 | local types = params[3] or table.concat(thing.types or {}, ',') | 552 | local types = params[3] or table.concat(thingy.types or {}, ',') |
555 | thing.widget = params[4] or thing.widget | 553 | thingy.widget = params[4] or thingy.widget |
556 | thing.required = params[5] or thing.required | 554 | thingy.required = params[5] or thingy.required |
557 | thing.acl = params[6] or thing.acl | 555 | thingy.acl = params[6] or thingy.acl |
558 | thing.boss = params[7] or thing.boss | 556 | thingy.boss = params[7] or thingy.boss |
559 | 557 | ||
560 | -- Pull out named arguments. | 558 | -- Pull out named arguments. |
561 | for k, v in pairs(params) do | 559 | for k, v in pairs(params) do |
562 | if 'string' == type(k) then | 560 | if 'string' == type(k) then |
563 | if 'types' == k then types = v | 561 | if 'types' == k then types = v |
564 | elseif 'names' == k then | 562 | elseif 'names' == k then |
565 | oldNames = thing.names | 563 | oldNames = thingy.names |
566 | thing.names = cvs2table(v) | 564 | thingy.names = cvs2table(v) |
567 | else thing[k] = v | 565 | else thingy[k] = v |
568 | end | 566 | end |
569 | end | 567 | end |
570 | end | 568 | end |
571 | 569 | ||
572 | thing.required = isBoolean(thing.required) | 570 | thingy.required = isBoolean(thingy.required) |
573 | 571 | ||
574 | -- Find type, default to string, then break out the other types. | 572 | -- Find type, default to string, then break out the other types. |
575 | local typ = type(thing.default) | 573 | local typ = type(thingy.default) |
576 | if 'nil' == typ then typ = 'string' end | 574 | if 'nil' == typ then typ = 'string' end |
577 | thing.types = {} | ||
578 | if types then types = typ .. ',' .. types else types = typ end | 575 | if types then types = typ .. ',' .. types else types = typ end |
579 | thing.types = csv2table(types) | 576 | thingy.types = csv2table(types) |
580 | 577 | ||
581 | -- Remove old names, then stash the Thing under all of it's new names. | 578 | -- Remove old names, then stash the Thing under all of it's new names. |
582 | for i, v in ipairs(oldNames) do | 579 | for i, v in ipairs(oldNames) do |
583 | modThing.__stuff[v] = nil | 580 | modThing.__stuff[v] = nil |
584 | end | 581 | end |
585 | for i, v in ipairs(thing.names) do | 582 | for i, v in ipairs(thingy.names) do |
586 | modThing.__stuff[v] = thing | 583 | modThing.__stuff[v] = thingy |
587 | end | 584 | end |
588 | 585 | ||
589 | -- This triggers the Thing.__newindex metamethod above. If nothing else, it triggers thing.isValid() | 586 | -- This triggers the Thing.__newindex metamethod above. If nothing else, it triggers thingy.isValid() |
590 | if new then module[name] = thing.default end | 587 | if new then module[name] = thingy.default end |
591 | end | 588 | end |
592 | 589 | ||
593 | 590 | ||
594 | copy = function (module, name) | 591 | copy = function (module, name) |
595 | local result = {} | 592 | local result = {} |
596 | local thing = {} | 593 | local thingy = {} |
597 | local modThing = getmetatable(module) | 594 | local modThing = getmetatable(module) |
598 | 595 | ||
599 | for k, v in pairs(Thing) do | 596 | for k, v in pairs(Thing) do |
600 | thing[k] = v | 597 | thingy[k] = v |
601 | end | 598 | end |
602 | 599 | ||
603 | thing.__values = {} | 600 | thingy.__values = {} |
604 | for k, v in pairs(modThing.__values) do | 601 | for k, v in pairs(modThing.__values) do |
605 | thing.__values[k] = v | 602 | thingy.__values[k] = v |
606 | end | 603 | end |
607 | 604 | ||
608 | thing.__stuff = modThing.__stuff | 605 | thingy.__stuff = modThing.__stuff |
609 | thing.names = {name} | 606 | thingy.names = {name} |
610 | setmetatable(thing, {__index = Thing}) | 607 | setmetatable(thingy, {__index = Thing}) |
611 | setmetatable(result, thing) | 608 | setmetatable(result, thingy) |
612 | 609 | ||
613 | return result | 610 | return result |
614 | end | 611 | end |
@@ -617,9 +614,9 @@ end | |||
617 | get = function (stuff, key, name) | 614 | get = function (stuff, key, name) |
618 | local result | 615 | local result |
619 | if name then | 616 | if name then |
620 | local thing = getmetatable(stuff) | 617 | local thingy = getmetatable(stuff) |
621 | if thing then | 618 | if thingy then |
622 | local this = thing.__stuff[key] | 619 | local this = thingy.__stuff[key] |
623 | if this then result = this[name] end | 620 | if this then result = this[name] end |
624 | end | 621 | end |
625 | else | 622 | else |
@@ -631,9 +628,9 @@ end | |||
631 | 628 | ||
632 | reset = function (stuff, key, name) | 629 | reset = function (stuff, key, name) |
633 | if name then | 630 | if name then |
634 | local thing = getmetatable(stuff) | 631 | local thingy = getmetatable(stuff) |
635 | if thing then | 632 | if thingy then |
636 | local this = thing.__stuff[key] | 633 | local this = thingy.__stuff[key] |
637 | if this then this[name] = nil end | 634 | if this then this[name] = nil end |
638 | end | 635 | end |
639 | else | 636 | else |
@@ -643,10 +640,10 @@ end | |||
643 | 640 | ||
644 | 641 | ||
645 | set = function (stuff, key, name, value) | 642 | set = function (stuff, key, name, value) |
646 | if value then | 643 | if 'nil' ~= type(value) then |
647 | local thing = getmetatable(stuff) | 644 | local thingy = getmetatable(stuff) |
648 | if thing then | 645 | if thingy then |
649 | local this = thing.__stuff[key] | 646 | local this = thingy.__stuff[key] |
650 | if this then this[name] = value end | 647 | if this then this[name] = value end |
651 | end | 648 | end |
652 | else | 649 | else |