patch.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. --- AM2320 系统补丁
  2. -- @license MIT
  3. -- @copyright openLuat.com
  4. -- @release 2017.10.19
  5. --[[
  6. 模块名称:Lua自带接口补丁
  7. 模块功能:补丁某些Lua自带的接口,规避调用异常时死机
  8. 模块最后修改时间:2017.02.14
  9. ]]
  10. --保存Lua自带的os.time接口
  11. local oldostime = os.time
  12. --[[
  13. 函数名:safeostime
  14. 功能 :封装自定义的os.time接口
  15. 参数 :
  16. t:日期表,如果没有传入,使用系统当前时间
  17. 返回值:t时间距离1970年1月1日0时0分0秒所经过的秒数
  18. ]]
  19. function safeostime(t)
  20. return oldostime(t) or 0
  21. end
  22. --Lua自带的os.time接口指向自定义的safeostime接口
  23. os.time = safeostime
  24. --保存Lua自带的os.date接口
  25. local oldosdate = os.date
  26. --[[
  27. 函数名:safeosdate
  28. 功能 :封装自定义的os.date接口
  29. 参数 :
  30. s:输出格式
  31. t:距离1970年1月1日0时0分0秒所经过的秒数
  32. 返回值:参考Lua自带的os.date接口说明
  33. ]]
  34. function safeosdate(s, t)
  35. if s == "*t" then
  36. return oldosdate(s, t) or {year = 2012,
  37. month = 12,
  38. day = 11,
  39. hour = 10,
  40. min = 9,
  41. sec = 0}
  42. else
  43. return oldosdate(s, t)
  44. end
  45. end
  46. --Lua自带的os.date接口指向自定义的safeosdate接口
  47. os.date = safeosdate
  48. -- 对coroutine.resume加一个修饰器用于捕获协程错误
  49. local rawcoresume = coroutine.resume
  50. coroutine.resume = function(...)
  51. function wrapper(...)
  52. if not arg[1] then
  53. log.error("coroutine.resume", arg[2])
  54. end
  55. return unpack(arg)
  56. end
  57. return wrapper(rawcoresume(unpack(arg)))
  58. end
  59. --保存Lua自带的json.decode接口
  60. if json and json.decode then oldjsondecode = json.decode end
  61. --- 封装自定义的json.decode接口
  62. -- @string s,json格式的字符串
  63. -- @return table,第一个返回值为解析json字符串后的table
  64. -- @return boole,第二个返回值为解析结果(true表示成功,false失败)
  65. -- @return string,第三个返回值可选(只有第二个返回值为false时,才有意义),表示出错信息
  66. function safejsondecode(s)
  67. local result, info = pcall(oldjsondecode, s)
  68. if result then
  69. return info, true
  70. else
  71. return {}, false, info
  72. end
  73. end
  74. --Lua自带的json.decode接口指向自定义的safejsondecode接口
  75. if json and json.decode then json.decode = safejsondecode end