globalfuns.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*Software License
  2. Copyright(C) 2024[liuyingjie]
  3. License Terms
  4. Usage Rights
  5. Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code.
  6. If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license.
  7. Future Versions Updates
  8. From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version.
  9. Users must comply with the terms of the new license issued in future releases.
  10. Liability and Disclaimer
  11. This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
  12. Contact Information
  13. If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com.
  14. */
  15. #include "globalfuns.h"
  16. #include <thread>
  17. #include <sys/stat.h>
  18. #include <filesystem>
  19. #include <chrono>
  20. #include <ctime>
  21. #include <iostream>
  22. #include "util/counter.hpp"
  23. #include "util/codec.h"
  24. #include "util/time.h"
  25. #include "util/system.h"
  26. #include "core/global.h"
  27. #include "util/file.h"
  28. #include "core/app.h"
  29. #include "core/statemanager.h"
  30. static ylib::counter<uint64> s_counter_guid;
  31. std::string module::globalfuncs::website_dir(sol::this_state ts)
  32. {
  33. GET_APP;
  34. return app->config->website.dir;
  35. }
  36. void module::globalfuncs::create_env(const std::string& lua_filepath,sol::this_state ts)
  37. {
  38. GET_APP;
  39. std::thread t([](std::string lua_filepath,fastweb::app* a) {
  40. auto lua = a->state->get();
  41. std::string exception_string;
  42. try
  43. {
  44. sol::load_result script = lua->state->load_file(a->config->website.dir + lua_filepath);
  45. if (!script.valid()) {
  46. sol::error err = script;
  47. throw ylib::exception(err.what());
  48. }
  49. sol::protected_function_result result = script();
  50. if (!result.valid()) {
  51. sol::error err = result;
  52. throw ylib::exception(err.what());
  53. }
  54. }
  55. catch (const std::exception& e)
  56. {
  57. if (a->config->website.debug)
  58. a->log->error("[create_env][" + lua_filepath + "]: " + e.what(), __FILE__, __func__, __LINE__);
  59. }
  60. lua->state->collect_garbage();
  61. a->state->push(lua);
  62. },lua_filepath,app);
  63. t.detach();
  64. }
  65. std::optional<int64> module::globalfuncs::toint(const sol::object& obj)
  66. {
  67. sol::type t = obj.get_type();
  68. switch (t) {
  69. case sol::type::number:
  70. {
  71. double value = obj.as<double>();
  72. // 检查小数部分是否为零
  73. if (std::floor(value) == value) {
  74. return static_cast<int64>(value);
  75. }
  76. return std::nullopt;
  77. }
  78. case sol::type::string: {
  79. // 尝试将字符串转换为 int
  80. std::string s = obj.as<std::string>();
  81. try {
  82. return std::stoll(s);
  83. }
  84. catch (const std::exception&) {
  85. return std::nullopt;
  86. }
  87. }
  88. case sol::type::boolean:
  89. // 布尔值 true 转为 1, false 转为 0
  90. return obj.as<bool>() ? 1 : 0;
  91. default:
  92. // 其它类型返回空
  93. return std::nullopt;
  94. }
  95. }
  96. void module::globalfuncs::sleep_msec(int msec)
  97. {
  98. system::sleep_msec(msec);
  99. }
  100. int64 module::globalfuncs::now_msec()
  101. {
  102. return time::now_msec();
  103. }
  104. std::string file_last_modify_time(const std::string& filepath)
  105. {
  106. auto ftime = std::filesystem::last_write_time(filepath);
  107. // 把 file_time_type 转为 system_clock::time_point
  108. auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
  109. ftime - decltype(ftime)::clock::now() + std::chrono::system_clock::now()
  110. );
  111. // 转为 time_t(UNIX 时间戳)
  112. time_t last_modify_time = std::chrono::system_clock::to_time_t(sctp);
  113. std::string r;
  114. ylib::time::to_gmt(last_modify_time,r);
  115. return r
  116. }
  117. void module::globalfuncs::regist(sol::state* lua)
  118. {
  119. lua->set_function("fw_set_ptr", module::globalfuncs::set_ptr);
  120. lua->set_function("fw_get_str", module::globalfuncs::get_str);
  121. lua->set_function("fw_set_str", module::globalfuncs::set_str);
  122. lua->set_function("fw_make_software_guid", module::globalfuncs::make_software_guid);
  123. lua->set_function("fw_throw_string", module::globalfuncs::throw_string);
  124. lua->set_function("print", module::globalfuncs::print);
  125. lua->set_function("fw_website_dir", module::globalfuncs::website_dir);
  126. lua->set_function("fw_sleep_msec",ylib::system::sleep_msec);
  127. lua->set_function("fw_now_msec", ylib::time::now_msec);
  128. lua->set_function("fw_now_sec", ylib::time::now_sec);
  129. lua->set_function("fw_create_env", module::globalfuncs::create_env);
  130. lua->set_function("fw_toint", module::globalfuncs::toint);
  131. lua->set_function("fw_sleep_msec", module::globalfuncs::sleep_msec);
  132. lua->set_function("fw_now_msec", module::globalfuncs::now_msec);
  133. lua->set_function("fw_file_last_modify_time", module::globalfuncs::file_last_modify_time);
  134. }
  135. std::string module::globalfuncs::make_software_guid()
  136. {
  137. return codec::md5(std::to_string(time::now_msec()) + std::to_string(s_counter_guid.make()));
  138. }
  139. void module::globalfuncs::print(sol::variadic_args args, sol::this_state ts)
  140. {
  141. GET_APP;
  142. std::ostringstream oss;
  143. for (auto arg : args) {
  144. if (arg.get_type() == sol::type::string) {
  145. oss << arg.as<std::string>();
  146. }
  147. else if (arg.get_type() == sol::type::number) {
  148. oss << arg.as<double>(); // 可以处理整数和浮点数
  149. }
  150. else if (arg.get_type() == sol::type::boolean) {
  151. oss << (arg.as<bool>() ? "true" : "false");
  152. }
  153. else if (arg.get_type() == sol::type::nil) {
  154. oss << "nil";
  155. }
  156. else {
  157. oss << "unsupported type";
  158. }
  159. oss << " "; // 添加一个空格分隔符
  160. }
  161. app->log->lua(oss.str());
  162. }
  163. bool module::globalfuncs::set_ptr(const std::string& name, void* ptr, sol::this_state ts)
  164. {
  165. GET_APP;
  166. return app->global->set_ptr(name,ptr, ts);
  167. }
  168. void module::globalfuncs::set_str(const std::string& name, std::string value, sol::this_state ts)
  169. {
  170. GET_APP;
  171. return app->global->set_str(name,value);
  172. }
  173. sol::object module::globalfuncs::get_str(const std::string& name, sol::this_state ts)
  174. {
  175. GET_APP;
  176. return app->global->get_str(name, ts);
  177. }
  178. void module::globalfuncs::throw_string(const std::string& msg)
  179. {
  180. throw ylib::exception(msg);
  181. }