deprecation.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. --[[
  2. font_api mod for Minetest - Library creating textures with fonts and text
  3. (c) Pierre-Yves Rollo
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. --]]
  15. -- Deprecation
  16. function deprecated_global_table(deprecated_global_name, replacement_global_name)
  17. assert(type(deprecated_global_name) == 'string', "deprecated_global_name should be a string.")
  18. assert(type(replacement_global_name) == 'string', "replacement_global_name should be a string.")
  19. assert(deprecated_global_name ~= '', "deprecated_global_name should not be empty.")
  20. assert(replacement_global_name ~= '', "replacement_global_name should not be empty.")
  21. assert(rawget(_G, deprecated_global_name) == nil, "deprecated global does not exist.")
  22. if _G[replacement_global_name] == nil then
  23. minetest.log('warning', string.format(
  24. 'Replacement global "%s" does not exists.', replacement_global_name))
  25. return
  26. end
  27. local meta = {
  28. deprecated = deprecated_global_name,
  29. replacement = replacement_global_name,
  30. __index = function(table, key)
  31. local meta = getmetatable(table)
  32. local dbg = debug.getinfo(2, "lS")
  33. minetest.log("warning", string.format(
  34. 'Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).',
  35. meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'),
  36. (dbg.currentline or 0)))
  37. return _G[meta.replacement][key]
  38. end,
  39. __newindex = function(table, key, value)
  40. local meta = getmetatable(table)
  41. local dbg = debug.getinfo(2, "lS")
  42. minetest.log("warning", string.format(
  43. 'Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).',
  44. meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'),
  45. (dbg.currentline or 0)))
  46. _G[meta.replacement][key]=value
  47. end,
  48. }
  49. rawset(_G, deprecated_global_name, {})
  50. setmetatable(_G[deprecated_global_name], meta)
  51. end
  52. -- deprecated(2) -- December 2018 - Deprecation of font_lib
  53. deprecated_global_table('font_lib', 'font_api')