├── .gitattributes
├── .gitignore
├── Lua2Game
├── CMakeLists.txt
├── Game
│ ├── CMakeLists.txt
│ ├── Game.vcxproj
│ ├── Game.vcxproj.filters
│ ├── readme.txt
│ ├── script
│ │ ├── test.lua
│ │ └── test1.lua
│ └── src
│ │ ├── Api4Lua.cpp
│ │ ├── Api4Lua.h
│ │ ├── Core.cpp
│ │ ├── Core.h
│ │ ├── Global.cpp
│ │ ├── Global.h
│ │ ├── LuaManager.cpp
│ │ ├── LuaManager.h
│ │ ├── LuaScript.cpp
│ │ ├── LuaScript.h
│ │ └── main.cpp
├── Lua2Game.sln
└── LuaDll
│ ├── CMakeLists.txt
│ ├── LuaDll.vcxproj
│ ├── LuaDll.vcxproj.filters
│ ├── readme.txt
│ └── src
│ ├── Makefile
│ ├── lapi.c
│ ├── lapi.h
│ ├── lauxlib.c
│ ├── lauxlib.h
│ ├── lbaselib.c
│ ├── lbitlib.c
│ ├── lcode.c
│ ├── lcode.h
│ ├── lcorolib.c
│ ├── lctype.c
│ ├── lctype.h
│ ├── ldblib.c
│ ├── ldebug.c
│ ├── ldebug.h
│ ├── ldo.c
│ ├── ldo.h
│ ├── ldump.c
│ ├── lfunc.c
│ ├── lfunc.h
│ ├── lgc.c
│ ├── lgc.h
│ ├── linit.c
│ ├── liolib.c
│ ├── llex.c
│ ├── llex.h
│ ├── llimits.h
│ ├── lmathlib.c
│ ├── lmem.c
│ ├── lmem.h
│ ├── loadlib.c
│ ├── lobject.c
│ ├── lobject.h
│ ├── lopcodes.c
│ ├── lopcodes.h
│ ├── loslib.c
│ ├── lparser.c
│ ├── lparser.h
│ ├── lstate.c
│ ├── lstate.h
│ ├── lstring.c
│ ├── lstring.h
│ ├── lstrlib.c
│ ├── ltable.c
│ ├── ltable.h
│ ├── ltablib.c
│ ├── ltm.c
│ ├── ltm.h
│ ├── lua.c
│ ├── lua.h
│ ├── lua.hpp
│ ├── luaconf.h
│ ├── lualib.h
│ ├── lundump.c
│ ├── lundump.h
│ ├── lvm.c
│ ├── lvm.h
│ ├── lzio.c
│ └── lzio.h
└── ReadMe.txt
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.sln.docstates
8 |
9 | # Build results
10 | [Dd]ebug/
11 | [Dd]ebugPublic/
12 | [Rr]elease/
13 | x64/
14 | build/
15 | bld/
16 | [Bb]in/
17 | [Oo]bj/
18 |
19 | # MSTest test Results
20 | [Tt]est[Rr]esult*/
21 | [Bb]uild[Ll]og.*
22 |
23 | #NUNIT
24 | *.VisualState.xml
25 | TestResult.xml
26 |
27 | # Build Results of an ATL Project
28 | [Dd]ebugPS/
29 | [Rr]eleasePS/
30 | dlldata.c
31 |
32 | *_i.c
33 | *_p.c
34 | *_i.h
35 | *.ilk
36 | *.meta
37 | *.obj
38 | *.pch
39 | *.pdb
40 | *.pgc
41 | *.pgd
42 | *.rsp
43 | *.sbr
44 | *.tlb
45 | *.tli
46 | *.tlh
47 | *.tmp
48 | *.tmp_proj
49 | *.log
50 | *.vspscc
51 | *.vssscc
52 | .builds
53 | *.pidb
54 | *.svclog
55 | *.scc
56 |
57 | # Chutzpah Test files
58 | _Chutzpah*
59 |
60 | # Visual C++ cache files
61 | ipch/
62 | *.aps
63 | *.ncb
64 | *.opensdf
65 | *.sdf
66 | *.cachefile
67 |
68 | # Visual Studio profiler
69 | *.psess
70 | *.vsp
71 | *.vspx
72 |
73 | # TFS 2012 Local Workspace
74 | $tf/
75 |
76 | # Guidance Automation Toolkit
77 | *.gpState
78 |
79 | # ReSharper is a .NET coding add-in
80 | _ReSharper*/
81 | *.[Rr]e[Ss]harper
82 | *.DotSettings.user
83 |
84 | # JustCode is a .NET coding addin-in
85 | .JustCode
86 |
87 | # TeamCity is a build add-in
88 | _TeamCity*
89 |
90 | # DotCover is a Code Coverage Tool
91 | *.dotCover
92 |
93 | # NCrunch
94 | *.ncrunch*
95 | _NCrunch_*
96 | .*crunch*.local.xml
97 |
98 | # MightyMoose
99 | *.mm.*
100 | AutoTest.Net/
101 |
102 | # Web workbench (sass)
103 | .sass-cache/
104 |
105 | # Installshield output folder
106 | [Ee]xpress/
107 |
108 | # DocProject is a documentation generator add-in
109 | DocProject/buildhelp/
110 | DocProject/Help/*.HxT
111 | DocProject/Help/*.HxC
112 | DocProject/Help/*.hhc
113 | DocProject/Help/*.hhk
114 | DocProject/Help/*.hhp
115 | DocProject/Help/Html2
116 | DocProject/Help/html
117 |
118 | # Click-Once directory
119 | publish/
120 |
121 | # Publish Web Output
122 | *.[Pp]ublish.xml
123 | *.azurePubxml
124 |
125 | # NuGet Packages Directory
126 | packages/
127 | ## TODO: If the tool you use requires repositories.config uncomment the next line
128 | #!packages/repositories.config
129 |
130 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
131 | # This line needs to be after the ignore of the build folder (and the packages folder if the line above has been uncommented)
132 | !packages/build/
133 |
134 | # Windows Azure Build Output
135 | csx/
136 | *.build.csdef
137 |
138 | # Windows Store app package directory
139 | AppPackages/
140 |
141 | # Others
142 | sql/
143 | *.Cache
144 | ClientBin/
145 | [Ss]tyle[Cc]op.*
146 | ~$*
147 | *~
148 | *.dbmdl
149 | *.dbproj.schemaview
150 | *.pfx
151 | *.publishsettings
152 | node_modules/
153 |
154 | # RIA/Silverlight projects
155 | Generated_Code/
156 |
157 | # Backup & report files from converting an old project file to a newer
158 | # Visual Studio version. Backup files are not needed, because we have git ;-)
159 | _UpgradeReport_Files/
160 | Backup*/
161 | UpgradeLog*.XML
162 | UpgradeLog*.htm
163 |
164 | # SQL Server files
165 | *.mdf
166 | *.ldf
167 |
168 | # Business Intelligence projects
169 | *.rdl.data
170 | *.bim.layout
171 | *.bim_*.settings
172 |
173 | # Microsoft Fakes
174 | FakesAssemblies/
175 |
176 | # =========================
177 | # Operating System Files
178 | # =========================
179 |
180 | # OSX
181 | # =========================
182 |
183 | .DS_Store
184 | .AppleDouble
185 | .LSOverride
186 |
187 | # Icon must ends with two \r.
188 | Icon
189 |
190 | # Thumbnails
191 | ._*
192 |
193 | # Files that might appear on external disk
194 | .Spotlight-V100
195 | .Trashes
196 |
197 | # Windows
198 | # =========================
199 |
200 | # Windows image file caches
201 | Thumbs.db
202 | ehthumbs.db
203 |
204 | # Folder config file
205 | Desktop.ini
206 |
207 | # Recycle Bin used on file shares
208 | $RECYCLE.BIN/
209 |
210 | # Windows Installer files
211 | *.cab
212 | *.msi
213 | *.msm
214 | *.msp
215 |
--------------------------------------------------------------------------------
/Lua2Game/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8)
2 | PROJECT(Lua2Game)
3 | ADD_SUBDIRECTORY(LuaDll)
4 | ADD_SUBDIRECTORY(Game)
5 |
--------------------------------------------------------------------------------
/Lua2Game/Game/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required (VERSION 2.6)
2 | project (game)
3 | ADD_EXECUTABLE(game
4 | src/main.cpp
5 | src/Api4Lua.cpp
6 | src/Core.cpp
7 | src/Global.cpp
8 | src/LuaManager.cpp
9 | src/LuaScript.cpp
10 | )
11 | #find_path(header lua.h)
12 | #if(header)
13 | #include_directories(${header})
14 | #endif(header)
15 | #LINK_DIRECTORIES(/home/liubo5/Desktop/Lua2Game/Lua2Game/build/LuaDll)
16 | TARGET_LINK_LIBRARIES(game lualib)
17 |
18 |
--------------------------------------------------------------------------------
/Lua2Game/Game/Game.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {10E013E9-1C10-4F77-94A2-17E53C20D12B}
15 | MakeFileProj
16 |
17 |
18 |
19 | Application
20 | true
21 | v120
22 |
23 |
24 | Application
25 | false
26 | v120
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | Game.exe
40 | WIN32;_DEBUG;$(NMakePreprocessorDefinitions)
41 | $(IncludePath)
42 |
43 |
44 | Game.exe
45 | WIN32;NDEBUG;$(NMakePreprocessorDefinitions)
46 |
47 |
48 |
49 |
50 |
51 | ../Debug/LuaDll.lib;%(AdditionalDependencies)
52 | true
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/Lua2Game/Game/Game.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | 头文件
23 |
24 |
25 | 头文件
26 |
27 |
28 | 头文件
29 |
30 |
31 | 头文件
32 |
33 |
34 | 头文件
35 |
36 |
37 |
38 |
39 | 源文件
40 |
41 |
42 | 源文件
43 |
44 |
45 | 源文件
46 |
47 |
48 | 源文件
49 |
50 |
51 | 源文件
52 |
53 |
54 | 源文件
55 |
56 |
57 |
--------------------------------------------------------------------------------
/Lua2Game/Game/readme.txt:
--------------------------------------------------------------------------------
1 | ========================================================================
2 | 生成文件项目:Game 项目概述
3 | ========================================================================
4 |
5 | 应用程序向导已为您创建了此 Game 项目。
6 |
7 | 本文件概要介绍组成 Game 项目的每个文件的内容。
8 |
9 |
10 | Game.vcxproj
11 | 这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。
12 |
13 | Game.vcxproj.filters
14 | 这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。
15 |
16 | 此项目允许通过调用在向导中输入的命令在 Visual Studio 中进行生成/清除/重新生成。生成命令可以是 nmake 或任何其他所用的工具。
17 |
18 | 此项目不包含任何文件,因此在“解决方案资源管理器”没有任何显示。
19 |
20 | /////////////////////////////////////////////////////////////////////////////
21 |
--------------------------------------------------------------------------------
/Lua2Game/Game/script/test.lua:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xlplbo/Lua2Game/41d8715b42c2a51dcb889aff768bc27a31ffc150/Lua2Game/Game/script/test.lua
--------------------------------------------------------------------------------
/Lua2Game/Game/script/test1.lua:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xlplbo/Lua2Game/41d8715b42c2a51dcb889aff768bc27a31ffc150/Lua2Game/Game/script/test1.lua
--------------------------------------------------------------------------------
/Lua2Game/Game/src/Api4Lua.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xlplbo/Lua2Game/41d8715b42c2a51dcb889aff768bc27a31ffc150/Lua2Game/Game/src/Api4Lua.cpp
--------------------------------------------------------------------------------
/Lua2Game/Game/src/Api4Lua.h:
--------------------------------------------------------------------------------
1 | #ifndef __API_4_LUA__
2 | #define __API_4_LUA__
3 |
4 | extern "C"{
5 | #include "../../LuaDll/src/lua.h"
6 | #include "../../LuaDll/src/lauxlib.h"
7 | #include "../../LuaDll/src/lualib.h"
8 | }
9 |
10 |
11 | struct TLua_Funcs
12 | {
13 | const char *name;
14 | lua_CFunction func;
15 | };
16 |
17 | extern TLua_Funcs g_GameFunc[];
18 | extern int g_GetGameFuncSize();
19 |
20 | #endif
--------------------------------------------------------------------------------
/Lua2Game/Game/src/Core.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Core.h"
4 | #include "LuaManager.h"
5 | #ifdef _WIN32
6 | #include
7 | #else
8 | #include
9 | #endif
10 |
11 | CCore g_Core;
12 |
13 | CCore::CCore()
14 | {
15 | m_bIsRuning = true;
16 | }
17 |
18 | CCore::~CCore()
19 | {
20 |
21 | }
22 |
23 | bool CCore::Initialize()
24 | {
25 | //do something
26 | return true;
27 | }
28 |
29 | void CCore::Uninitialize()
30 | {
31 | //do something
32 | }
33 |
34 | void CCore::SetRunState(bool bRunning)
35 | {
36 | m_bIsRuning = bRunning;
37 | }
38 |
39 | bool CCore::Breathe()
40 | {
41 | if (!m_bIsRuning)
42 | return false;
43 | static size_t nCount = 0;
44 | if (nCount % 5 == 0)
45 | {
46 | size_t c = time(NULL);
47 | CLuaManager::GetInstance().ExecuteScript("\\script\\test.lua", "main", 1, "sdd", "luaer", c, c / 18);
48 | CLuaManager::GetInstance().ExecuteScript("\\script\\test1.lua", "test");
49 | printf("------------- %lds -----------\n", nCount);
50 | }
51 | ++ nCount;
52 | #ifdef _WIN32
53 | Sleep(1000);
54 | #else
55 | sleep(1);
56 | #endif
57 | return true;
58 | }
59 |
--------------------------------------------------------------------------------
/Lua2Game/Game/src/Core.h:
--------------------------------------------------------------------------------
1 | #ifndef __CORE_H__
2 | #define __CORE_H__
3 |
4 | #include "LuaScript.h"
5 |
6 | class CCore
7 | {
8 | public:
9 | CCore();
10 | ~CCore();
11 |
12 | public:
13 | bool Initialize();
14 | void Uninitialize();
15 | bool Breathe();
16 | void SetRunState(bool bRunning);
17 |
18 | private:
19 | bool m_bIsRuning;
20 | };
21 |
22 | extern CCore g_Core;
23 |
24 | #endif
--------------------------------------------------------------------------------
/Lua2Game/Game/src/Global.cpp:
--------------------------------------------------------------------------------
1 | #include "Global.h"
2 |
3 | unsigned g_FileNameHash(const char* pString)
4 | {
5 | unsigned int Id = 0;
6 | char c = 0;
7 | for (int i = 0; pString[i]; i++)
8 | {
9 | c = pString[i];
10 | if (c >= 'A' && c <= 'Z')
11 | c += 0x20;
12 | else if (c == '/')
13 | c = '\\';
14 | Id = (Id + (i + 1) * c) % 0x8000000b * 0xffffffef;
15 | }
16 | return (Id ^ 0x12345678);
17 | }
18 |
19 | char* g_StrReplace(char* src, char cs, char cd)
20 | {
21 | char* p = src;
22 | while (*p++)
23 | {
24 | if (*p == cs)
25 | *p = cd;
26 | }
27 | return src;
28 | }
--------------------------------------------------------------------------------
/Lua2Game/Game/src/Global.h:
--------------------------------------------------------------------------------
1 | #ifndef __GLOABAL_H__
2 | #define __GLOABAL_H__
3 |
4 | #define FILE_PATH_MAX 128
5 | extern unsigned g_FileNameHash(const char* pString);
6 | extern char* g_StrReplace(char* src, char cs, char cd);
7 |
8 | #endif
--------------------------------------------------------------------------------
/Lua2Game/Game/src/LuaManager.cpp:
--------------------------------------------------------------------------------
1 | #include "LuaManager.h"
2 |
3 | CLuaManager::CLuaManager()
4 | {
5 | UnInitialize();
6 | }
7 |
8 | CLuaManager::~CLuaManager()
9 | {
10 | UnInitialize();
11 | }
12 |
13 | CLuaManager& CLuaManager::GetInstance()
14 | {
15 | static CLuaManager ins;
16 | return ins;
17 | }
18 |
19 | void CLuaManager::UnInitialize()
20 | {
21 | for (MAP_LUASCRIPT_IT it = m_ScriptMap.begin(); it != m_ScriptMap.end(); ++it)
22 | {
23 | delete it->second.pLuaScript;
24 | }
25 | m_ScriptMap.clear();
26 | ClearIncludeSet();
27 | }
28 |
29 | CLuaScript* CLuaManager::GetScript(const char* szPath)
30 | {
31 | if (!szPath || szPath[0] == '\0')
32 | return NULL;
33 |
34 | TScriptNode tScriptNode = {0};
35 | CLuaScript* pLuaScript = NULL;
36 | unsigned nScriptId = g_FileNameHash(szPath);
37 | MAP_LUASCRIPT_IT it = m_ScriptMap.find(nScriptId);
38 | if (it != m_ScriptMap.end())
39 | {
40 | pLuaScript = it->second.pLuaScript;
41 | if (it->second.bNeedReload) /*need to reload*/
42 | {
43 | delete pLuaScript;
44 | m_ScriptMap.erase(it);
45 | goto POINT1;
46 | }
47 | return pLuaScript; /*is exsit*/
48 | }
49 |
50 | POINT1: /*new lua_state*/
51 | pLuaScript = new CLuaScript;
52 | if (!pLuaScript || !pLuaScript->Initialize())
53 | goto POINT2;
54 |
55 | ClearIncludeSet();
56 | AddIncludeSet(nScriptId);
57 | if (!pLuaScript->LoadScript(szPath))
58 | goto POINT2;
59 |
60 | tScriptNode.pLuaScript = pLuaScript;
61 | tScriptNode.bNeedReload = false;
62 | if (!m_ScriptMap.insert(MAP_LUASCRIPT::value_type(nScriptId, tScriptNode)).second)
63 | goto POINT2;
64 |
65 | return pLuaScript;
66 |
67 | POINT2: /*init failed*/
68 | delete pLuaScript;
69 | return NULL;
70 | }
71 |
72 | bool CLuaManager::ExecuteScript(const char* szPath, const char* szFuncName, int nResults /*= 0*/, const char* cFormat /*= NULL*/, ...)
73 | {
74 | if (!szPath || szPath[0] == '\0' || !szFuncName || szFuncName[0] == '\0')
75 | {
76 | printf("Param ERROR : %s : %s\n", szPath ? szPath : NULL, szFuncName ? szFuncName : NULL);
77 | return false;
78 | }
79 | CLuaScript* pLuaScript = GetScript(szPath);
80 | if (!pLuaScript)
81 | return false;
82 |
83 | bool bResult = false;
84 | va_list vlist;
85 | va_start(vlist, cFormat);
86 | bResult = pLuaScript->CallFunction((char*)szFuncName, nResults, cFormat, vlist);
87 | va_end(vlist);
88 |
89 | if (!bResult)
90 | {
91 | printf("Execute script [%s] [%s] failed!\n", szPath, szFuncName);
92 | }
93 |
94 | return bResult;
95 | }
96 |
97 | bool CLuaManager::AddIncludeSet(unsigned nScriptId)
98 | {
99 | return m_IncludeSet.insert(nScriptId).second;
100 | }
101 |
102 | void CLuaManager::ClearIncludeSet()
103 | {
104 | m_IncludeSet.clear();
105 | }
106 |
107 | void CLuaManager::ReloadAllScript()
108 | {
109 | int nCount = 0;
110 | for (MAP_LUASCRIPT_IT it = m_ScriptMap.begin(); it != m_ScriptMap.end(); ++it)
111 | {
112 | if (it->second.pLuaScript)
113 | {
114 | it->second.bNeedReload = true;
115 | nCount++;
116 | }
117 | }
118 | printf("Reload All Script Count = %d!\n", nCount);
119 | }
120 |
--------------------------------------------------------------------------------
/Lua2Game/Game/src/LuaManager.h:
--------------------------------------------------------------------------------
1 | #ifndef __LUA_MANAGER_H__
2 | #define __LUA_MANAGER_H__
3 |
4 | #include