xx 2 vuotta sitten
vanhempi
sitoutus
a76df472e7
3 muutettua tiedostoa jossa 35 lisäystä ja 16 poistoa
  1. 1 1
      src/core/config.cpp
  2. 31 15
      src/module/timer.cpp
  3. 3 0
      src/module/timer.h

+ 1 - 1
src/core/config.cpp

@@ -22,7 +22,7 @@ bool config::open(const std::string& ini_filepath)
 	// 配置文件目录
 	{
 		std::string ini_dir = ylib::file::parent_dir(ini_filepath);
-		src_content = strutils::replace(src_content, "${config_dir}", ini_dir);
+		src_content = strutils::replace(strutils::replace(src_content, "${config_dir}", ini_dir), '\\', '/');
 	}
 	
 

+ 31 - 15
src/module/timer.cpp

@@ -16,7 +16,7 @@ std::string module::timer::add(const std::string& name, const std::string& filep
         filepath2 = sConfig->scripts.app_dir + "/" + filepath;
     else
     {
-        std::string filepath2 = module_manager::getInstance()->search(filepath);
+        filepath2 = module_manager::getInstance()->search(filepath);
         if (filepath2.empty())
         {
             std::string result;
@@ -41,6 +41,7 @@ std::string module::timer::add(const std::string& name, const std::string& filep
     ti.loop = loop;
     ti.msec = msec;
     ti.exec_msec = time::now_msec() + msec;
+
     module::timer::getInstance()->m_list.emplace(name, ti);
     module::timer::getInstance()->m_insert = true;
     return "";
@@ -82,22 +83,29 @@ bool module::timer::run()
             return 1000;
         return wait_msec;
     };
-    auto exec_func = [&](const std::string& filepath,const std::string& funname)->bool {
-        auto lua = sStateMgr->get();
+    auto exec_func = [&](timer_info& ti)->bool {
         std::string exception_string;
         try
         {
-            auto lbResult = lua->state->load_file(filepath);
-            if (lbResult.valid() == false)
+            // 加载LUA文件
+            if (ti.lua == nullptr)
             {
-                sol::error err = lbResult;
-                throw ylib::exception("Failed to load script, " + std::string(err.what()));
+                ti.lua = sStateMgr->get();
+                auto lbResult = ti.lua->state->load_file(ti.filepath);
+                if (lbResult.valid() == false)
+                {
+                    sol::error err = lbResult;
+                    throw ylib::exception("Failed to load script, " + std::string(err.what()));
+                }
+                lbResult();
             }
-            lbResult();
-
-            if (funname.empty() == false)
+            // 加载函数
+            if (ti.funname != "" && ti.function.valid() == false)
+                ti.function = (*ti.lua->state)[ti.funname];
+            // 执行函数
+            if (ti.function.valid())
             {
-                auto result = (*lua->state)[funname]();
+                auto result = ti.function();
                 if (!result.valid()) {
                     sol::error err = result;
                     throw ylib::exception(err.what());
@@ -110,7 +118,7 @@ bool module::timer::run()
         {
             exception_string = e.what();
             if (sConfig->website.debug)
-                LOG_ERROR("[timer][" + filepath + "]: " + e.what());
+                LOG_ERROR("[timer][" + ti.filepath + "]: " + e.what());
             return false;
         }
         return false;
@@ -132,8 +140,7 @@ bool module::timer::run()
         if (iter->second.exec_msec <= now_msec)
         {
             bool ready_remove = false;
-            auto ti = iter->second;
-            if (exec_func(ti.filepath,ti.funname) == false)
+            if (exec_func(iter->second) == false)
                 ready_remove = true;
             if (ready_remove == false)
             {
@@ -154,7 +161,16 @@ bool module::timer::run()
     }
     std::string name;
     while (m_removed.pop(name))
-        m_list.erase(name);
+    {
+        auto iter = m_list.find(name);
+        if (iter != m_list.end())
+        {
+            iter->second.lua->state->collect_garbage();
+            sStateMgr->push(iter->second.lua);
+            m_list.erase(iter);
+        }
+    }
+        
 
     return true;
 }

+ 3 - 0
src/module/timer.h

@@ -1,6 +1,7 @@
 #pragma once
 #include "sol/sol.hpp"
 #include "imodule.h"
+#include "core/structs.h"
 #include "base/singleton.hpp"
 #include "util/thread.h"
 #include "util/queue.hpp"
@@ -15,6 +16,8 @@ namespace module
 		bool loop = false;
 		timestamp exec_msec = 0;
 		timestamp msec = 0;
+		luastate* lua = nullptr;
+		sol::function function;
 	};
 	/// <summary>
 	/// 定时器