MatthewC529

CC Config API Demo v1.0.0

Jul 24th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. os.loadAPI('/configuration')
  2.  
  3. -- Initializes debugging for CC Config
  4. -- this sets the log tag to 'demo':upper()
  5. -- Location: /.config-tmp/VERSION/log.txt
  6. cc_config_init_debug('demo')
  7.  
  8. -- Creates a Config in /.config-tmp/VERSION called 'hiddencfg.cfg'
  9. local hidden = configuration.new('hiddencfg', 'A Config File Hidden in a Hidden Directory')
  10.  
  11. -- Creates a Config in / (the root directory) called 'knowncfg.cfg'
  12. local known = configuration.new('knowncfg', '/', 'A Config Not Hidden')
  13.  
  14. -- This is exclusively logged to the CC Config Log File
  15. -- iff cc_config_init_debug was called previously
  16. -- LogLevels tell what the message should be tagged with.
  17. -- Supports INFO, WARN, and ERROR ('INFO', 'WARN', 'ERR' tags respectively)
  18. -- Also has aliases in lowercase (INFO==info) and expanded (WARN==warning)
  19. cc_config_debug(LogLevels.WARN, 'Successfully initialized configs...')
  20. cc_config_debug(configuration.LogLevels.INFO, 'We can also %s messages. More than %4d :)', 'format', 256)
  21. cc_config_debug(LogLevels.err, 'For when you don\'t want to THROW an error')
  22.  
  23. -- Error messages will actually throw an error and will log to the log file
  24. -- cc_config_error('You can error too')
  25. -- cc_config_error('Even with %s messages', 'format')
  26.  
  27. -- You can GET Strings, Numbers, Booleans and Lists (tables)
  28. -- If these values do not exist, they create them in the cfg and return the default value
  29. -- If they do exist, the cfg value is used but min, max and valid values are overwritten
  30. local exampleString = hidden:getString('exampleStr', 'category', 'defaultValue', 'Element Comments', {'validValues', 'defaultValue'})
  31. local exampleNumber = hidden:getNumber('exampleNum', 'category', 0.00612354, '', 0, 1)
  32. local exampleBoolean = hidden:getBoolean('exampleBool', 'othercategory', true, '')
  33. local exampleList = hidden:getList('exampleList', 'category.subcategory', {1, 2, 'wow', true}, '', {1, 2, 'wow', true})
  34. local exampleList2 = hidden:getList('exampleList2', 'category.subcategory', {'one', 90.214, false})
  35.  
  36. print(table.concat(exampleList2, ', '))
  37.  
  38. -- You can even get the ConfigValue objects themselves
  39. local configValueObj = known:get('category', 'testCV', 'someValue', 'Comments')
  40. local configValueObj2 = known:get('othercategory.sub', 'testCV', 256, '', 0, 256)
  41.  
  42. configValueObj:set('someOtherValue')
  43. configValueObj:setValids({'someValue', 'someOtherValue'})
  44. configValueObj2:set(512)
  45. configValueObj2:setMax(512)
  46.  
  47. print(configValueObj:getRaw())
  48. print(configValueObj:getString())
  49. -- configValueObj:getNumber() will error
  50. print(configValueObj2:getNumber())
  51.  
  52. -- You can set category commens by file or retrieve the ConfigCategory object and set it there
  53. hidden:setCategoryComment('category', 'Example of Setting Category Comments')
  54. hidden:getCategory('category.subcategory'):setComment('Another Way to set Category Comments')
  55.  
  56.  
  57. -- You MUST save to write out any changes to the cfg file
  58. hidden:save()
  59. known:save()
  60.  
  61. -- Not required necessarily, but recommended
  62. -- handles cleaning _G of CC Config functions
  63. os.unloadAPI('configuration')
Advertisement
Add Comment
Please, Sign In to add comment