Skip to content

Commit bd510c4

Browse files
committed
集成第三方库pblua\pbc\cjson\sproto等功能。
1 parent afb8451 commit bd510c4

File tree

17 files changed

+176
-46
lines changed

17 files changed

+176
-46
lines changed

Assets/LuaFramework/Lua/3rd/pblua/login_pb.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Generated By protoc-gen-lua Do not Edit
2-
local protobuf = require "protobuf"
2+
local protobuf = require "protobuf/protobuf"
33
module('login_pb')
44

55
local LOGINREQUEST = protobuf.Descriptor();

Assets/LuaFramework/Lua/3rd/pblua/person_pb.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Generated By protoc-gen-lua Do not Edit
2-
local protobuf = require "protobuf"
2+
local protobuf = require "protobuf/protobuf"
33
module('person_pb')
44

55

Assets/LuaFramework/Lua/Common/define.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ panelMgr = LuaHelper.GetPanelManager();
2929
soundMgr = LuaHelper.GetSoundManager();
3030
networkMgr = LuaHelper.GetNetManager();
3131

32-
GameObject = UnityEngine.GameObject;
32+
WWW = UnityEngine.WWW;
33+
GameObject = UnityEngine.GameObject;

Assets/LuaFramework/Lua/Controller/PromptCtrl.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
require "Common/define"
22

3-
--require "3rd/pblua/login_pb"
4-
--require "3rd/pbc/protobuf"
3+
require "3rd/pblua/login_pb"
4+
require "3rd/pbc/protobuf"
55

6-
--local sproto = require "3rd/sproto/sproto"
7-
--local core = require "sproto.core"
8-
--local print_r = require "3rd/sproto/print_r"
6+
local sproto = require "3rd/sproto/sproto"
7+
local core = require "sproto.core"
8+
local print_r = require "3rd/sproto/print_r"
99

1010
PromptCtrl = {};
1111
local this = PromptCtrl;

Assets/LuaFramework/Lua/Logic/Game.lua

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
--require "3rd/pblua/login_pb"
2-
--require "3rd/pbc/protobuf"
1+
require "3rd/pblua/login_pb"
2+
require "3rd/pbc/protobuf"
33

4-
--local lpeg = require "lpeg"
4+
local lpeg = require "lpeg"
55

6-
--local json = require "cjson"
7-
--local util = require "3rd/cjson.util"
6+
local json = require "cjson"
7+
local util = require "3rd/cjson.util"
88

9-
--local sproto = require "3rd/sproto/sproto"
10-
--local core = require "sproto.core"
11-
--local print_r = require "3rd/sproto/print_r"
9+
local sproto = require "3rd/sproto/sproto"
10+
local core = require "sproto.core"
11+
local print_r = require "3rd/sproto/print_r"
1212

1313
require "Common/define"
1414
require "Common/functions"
@@ -42,13 +42,14 @@ function Game.OnInitOK()
4242
--注册LuaView--
4343
this.InitViewPanels();
4444

45-
--this.test_class_func();
46-
--this.test_pblua_func();
47-
--this.test_cjson_func();
48-
--this.test_pbc_func();
49-
--this.test_lpeg_func();
50-
--this.test_sproto_func();
51-
--coroutine.start(this.test_coroutine);
45+
--测试第三方库功能--
46+
this.test_class_func();
47+
this.test_pblua_func();
48+
this.test_cjson_func();
49+
this.test_pbc_func();
50+
this.test_lpeg_func();
51+
this.test_sproto_func();
52+
coroutine.start(this.test_coroutine);
5253

5354
CtrlManager.Init();
5455
local ctrl = CtrlManager.GetCtrl(CtrlNames.Prompt);
@@ -64,7 +65,7 @@ function Game.test_coroutine()
6465
coroutine.wait(1);
6566
logWarn("2222");
6667

67-
local www = WWW("http://bbs.ulua.org/readme.txt");
68+
local www = WWW("http://doc.ulua.org/readme.txt");
6869
coroutine.www(www);
6970
logWarn(www.text);
7071
end

Assets/LuaFramework/Lua/Logic/Network.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ require "Common/protocal"
44
require "Common/functions"
55
Event = require 'events'
66

7-
--require "3rd/pblua/login_pb"
8-
--require "3rd/pbc/protobuf"
7+
require "3rd/pblua/login_pb"
8+
require "3rd/pbc/protobuf"
99

10-
--local sproto = require "3rd/sproto/sproto"
11-
--local core = require "sproto.core"
12-
--local print_r = require "3rd/sproto/print_r"
10+
local sproto = require "3rd/sproto/sproto"
11+
local core = require "sproto.core"
12+
local print_r = require "3rd/sproto/print_r"
1313

1414
Network = {};
1515
local this = Network;
-6 KB
Binary file not shown.

Assets/LuaFramework/Scripts/ConstDefine/AppConst.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ public class AppConst {
2222
public const bool LuaByteMode = false; //Lua字节码模式-默认关闭
2323
public const bool LuaBundleMode = false; //Lua代码AssetBundle模式-默认关闭
2424

25-
public const bool UsePbc = true; //PBC
26-
public const bool UseLpeg = true; //lpeg
27-
public const bool UsePbLua = true; //Protobuff-lua-gen
28-
public const bool UseCJson = true; //CJson
29-
public const bool UseSproto = true; //Sproto
30-
3125
public const int TimerInterval = 1;
3226
public const int GameFrameRate = 30; //游戏帧频
3327

Assets/LuaFramework/Scripts/Manager/GameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public void OnResourceInited() {
243243
LuaManager.DoFile("Logic/Network"); //加载网络
244244
NetManager.OnInit(); //初始化网络
245245

246-
Util.CallMethod(name, "OnInitOK"); //初始化完成
246+
Util.CallMethod("Game", "OnInitOK"); //初始化完成
247247
initialize = true; //初始化完
248248
}
249249

Assets/LuaFramework/Scripts/Manager/LuaManager.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,72 @@ public class LuaManager : Manager {
77
private LuaState lua;
88
private LuaLoader loader;
99

10+
private LuaFunction updateFunc = null;
11+
private LuaFunction lateUpdateFunc = null;
12+
private LuaFunction fixedUpdateFunc = null;
13+
14+
public LuaEvent UpdateEvent {
15+
get;
16+
private set;
17+
}
18+
19+
public LuaEvent LateUpdateEvent {
20+
get;
21+
private set;
22+
}
23+
24+
public LuaEvent FixedUpdateEvent {
25+
get;
26+
private set;
27+
}
28+
1029
// Use this for initialization
1130
void Awake() {
1231
loader = new LuaLoader();
1332
lua = new LuaState();
33+
this.InitLuaLibrary();
1434
LuaBinder.Bind(lua);
35+
LuaCoroutine.Register(lua, this);
1536
}
1637

1738
public void InitStart() {
1839
InitLuaPath();
1940
InitLuaBundle();
2041
this.lua.Start(); //启动LUAVM
42+
43+
lua.DoFile("Main.lua");
44+
45+
updateFunc = lua.GetFunction("Update");
46+
lateUpdateFunc = lua.GetFunction("LateUpdate");
47+
fixedUpdateFunc = lua.GetFunction("FixedUpdate");
48+
49+
LuaFunction main = lua.GetFunction("Main");
50+
main.Call();
51+
main.Dispose();
52+
main = null;
53+
54+
UpdateEvent = GetEvent("UpdateBeat");
55+
LateUpdateEvent = GetEvent("LateUpdateBeat");
56+
FixedUpdateEvent = GetEvent("FixedUpdateBeat");
57+
}
58+
59+
/// <summary>
60+
/// 初始化加载第三方库
61+
/// </summary>
62+
void InitLuaLibrary() {
63+
lua.OpenLibs(LuaDLL.luaopen_pb);
64+
lua.OpenLibs(LuaDLL.luaopen_sproto_core);
65+
lua.OpenLibs(LuaDLL.luaopen_protobuf_c);
66+
lua.OpenLibs(LuaDLL.luaopen_lpeg);
67+
lua.OpenLibs(LuaDLL.luaopen_cjson);
68+
lua.OpenLibs(LuaDLL.luaopen_cjson_safe);
69+
lua.OpenLibs(LuaDLL.luaopen_bit);
70+
lua.OpenLibs(LuaDLL.luaopen_socket_core);
2171
}
2272

73+
/// <summary>
74+
/// 初始化Lua代码加载路径
75+
/// </summary>
2376
void InitLuaPath() {
2477
if (AppConst.DebugMode) {
2578
string rootPath = AppConst.FrameworkRoot;
@@ -47,6 +100,52 @@ void InitLuaBundle() {
47100
}
48101
}
49102

103+
void Update() {
104+
if (updateFunc != null) {
105+
updateFunc.BeginPCall(TracePCall.Ignore);
106+
updateFunc.Push(Time.deltaTime);
107+
updateFunc.Push(Time.unscaledDeltaTime);
108+
updateFunc.PCall();
109+
updateFunc.EndPCall();
110+
}
111+
lua.Collect();
112+
#if UNITY_EDITOR
113+
lua.CheckTop();
114+
#endif
115+
}
116+
117+
void LateUpdate() {
118+
if (lateUpdateFunc != null) {
119+
lateUpdateFunc.BeginPCall(TracePCall.Ignore);
120+
lateUpdateFunc.PCall();
121+
lateUpdateFunc.EndPCall();
122+
}
123+
}
124+
125+
void FixedUpdate() {
126+
if (fixedUpdateFunc != null) {
127+
fixedUpdateFunc.BeginPCall(TracePCall.Ignore);
128+
fixedUpdateFunc.Push(Time.fixedDeltaTime);
129+
fixedUpdateFunc.PCall();
130+
fixedUpdateFunc.EndPCall();
131+
}
132+
}
133+
134+
LuaEvent GetEvent(string name) {
135+
LuaTable table = lua.GetTable(name);
136+
LuaEvent e = new LuaEvent(table);
137+
table.Dispose();
138+
table = null;
139+
return e;
140+
}
141+
142+
void SafeRelease(ref LuaFunction luaRef) {
143+
if (luaRef != null) {
144+
luaRef.Dispose();
145+
luaRef = null;
146+
}
147+
}
148+
50149
public object[] DoFile(string filename) {
51150
return lua.DoFile(filename);
52151
}
@@ -65,7 +164,26 @@ public void LuaGC() {
65164
}
66165

67166
public void Close() {
167+
SafeRelease(ref updateFunc);
168+
SafeRelease(ref lateUpdateFunc);
169+
SafeRelease(ref fixedUpdateFunc);
170+
171+
if (UpdateEvent != null) {
172+
UpdateEvent.Dispose();
173+
UpdateEvent = null;
174+
}
175+
176+
if (LateUpdateEvent != null) {
177+
LateUpdateEvent.Dispose();
178+
LateUpdateEvent = null;
179+
}
180+
181+
if (FixedUpdateEvent != null) {
182+
FixedUpdateEvent.Dispose();
183+
FixedUpdateEvent = null;
184+
}
68185
lua.Dispose();
186+
lua = null;
69187
loader = null;
70188
}
71189
}

0 commit comments

Comments
 (0)