Skip to content

Commit 3120d4e

Browse files
committed
Dostring uses Env 、LuaTable output optimization
1 parent 9aac826 commit 3120d4e

9 files changed

+196
-56
lines changed

Assets/GS/GSProtoBufTool.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.IO;
3+
using Google.Protobuf;
4+
5+
public class GSProtoBufTool
6+
{
7+
public static bool load(string filePath,IMessage message)
8+
{
9+
try
10+
{
11+
FileStream fs = new FileStream(filePath,FileMode.Open);
12+
// CodedInputStream ctx = new CodedInputStream(fs);
13+
message.MergeFrom(fs);
14+
}
15+
catch (Exception e)
16+
{
17+
GSLogTool.exception("GSProtoBuffTool.init",e,string.Format("load file:{0} failed",filePath));
18+
message = null;
19+
}
20+
21+
if (message == null)
22+
{
23+
GSLogTool.eFormat("GSProtoBuffTool.init","filePath:{0} failed",filePath);
24+
return false;
25+
}
26+
else
27+
{
28+
GSLogTool.dFormat("GSProtoBuffTool.init","filePath:{0} success",filePath);
29+
return true;
30+
}
31+
}
32+
33+
public static void save(string fileName, IMessage message)
34+
{
35+
if (GSFileTool.getFileExit(fileName))
36+
{
37+
GSFileTool.deleteFile(fileName);
38+
}
39+
using (var output = File.Create(fileName))
40+
{
41+
message.WriteTo(output);
42+
}
43+
44+
GSLogTool.dFormat("GSProtoBuffTool.save","保存成功:{0}", fileName);
45+
}
46+
}

Assets/Scipts/GSUnityLuaShellConst.cs

+3
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ public static class GSUnityLuaShellConst
1616

1717
// 输入框高度
1818
public const float InputFiledHeight = 100;
19+
20+
// Lua Depth
21+
public const int LuaTableDepth = 3;
1922
}
2023
}

Assets/Scipts/GSUnityLuaShellHistory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private GSUnityLuaShellHistory()
2626
CreateDirectoryIfNotExit();
2727
if (File.Exists(GSUnityLuaShellConst.HistoryFilePath))
2828
{
29-
GSProtoBuffTool.load(GSUnityLuaShellConst.HistoryFilePath, mCommands);
29+
GSProtoBufTool.load(GSUnityLuaShellConst.HistoryFilePath, mCommands);
3030
}
3131
}
3232

Assets/Scipts/GSUnityLuaShellTip.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace GSUnityLuaShell
4+
{
5+
public class GSUnityLuaShellTip
6+
{
7+
private string[] Macros = new string[]
8+
{
9+
"and",
10+
"or",
11+
"not",
12+
"true",
13+
"false",
14+
"nil"
15+
};
16+
17+
private static GSUnityLuaShellTip mInstance;
18+
public static GSUnityLuaShellTip GetInstance()
19+
{
20+
if (mInstance == null)
21+
{
22+
mInstance = new GSUnityLuaShellTip();
23+
}
24+
25+
return mInstance;
26+
}
27+
28+
public string[] getTips(string text)
29+
{
30+
if (string.IsNullOrEmpty(text))
31+
{
32+
return null;
33+
}
34+
if (text.StartsWith("$"))
35+
{
36+
return null;
37+
}
38+
return null;
39+
}
40+
}
41+
}

Assets/Scipts/GSUnityLuaShellTip.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scipts/GSUnityLuaShellWindow.cs

+63-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
using System.Collections.Generic;
12
using System.Runtime.InteropServices;
23
using GS.Editor;
34
using UnityEditor;
45
using UnityEditor.IMGUI.Controls;
56
using UnityEngine;
67
using UnityEngine.Serialization;
8+
using XLua;
9+
using XLua.LuaDLL;
710

811
namespace GSUnityLuaShell
912
{
@@ -65,9 +68,14 @@ private void OnGUI()
6568
EditorGUILayout.EndHorizontal();
6669

6770
EditorGUILayout.BeginScrollView(mScrollPos);
68-
treeView.OnGUI(new Rect(0,0, position.width, position.height-GSUnityLuaShellConst.InputFiledHeight));
71+
treeView.OnGUI(new Rect(0,0, position.width, position.height-GSUnityLuaShellConst.InputFiledHeight - 50));
6972
EditorGUILayout.EndScrollView();
7073

74+
//TODO 修复回车导致对于\n
75+
if (Text.StartsWith("\n"))
76+
{
77+
Text = Text.Substring(1, Text.Length - 1);
78+
}
7179
mTextEditor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
7280
mTextEditor.text = Text;
7381
GUI.SetNextControlName(GSUnityLuaShellConst.InputTextAreaControlName);
@@ -87,19 +95,30 @@ public void ParseResult()
8795
}
8896
else
8997
{
90-
foreach (var obj in objects)
98+
if (objects.Length == 1)
9199
{
92-
if (obj == null)
100+
object result = objects[0];
101+
if (result is LuaTable)
93102
{
94-
treeView.AddChild("nil");
95-
continue;
103+
treeView.AddChild(result.ToString());
104+
ParseResultLuaTable(result as LuaTable,1);
105+
}
106+
else
107+
{
108+
treeView.AddChild( result == null ? "nil" : result.ToString());
109+
}
110+
}
111+
else
112+
{
113+
foreach (var obj in objects)
114+
{
115+
treeView.AddChild( obj == null ? "nil" : obj.ToString());
96116
}
97-
98-
treeView.AddChild(obj.ToString());
99117
}
100118
}
101119

102120
treeView.Reload();
121+
treeView.SetSelection(new List<int>(){treeView.getRoot().children[treeView.getRoot().children.Count-1].id},TreeViewSelectionOptions.RevealAndFrame);
103122
Text = "";
104123
}
105124

@@ -108,5 +127,42 @@ public void Test(string command)
108127
Text = command;
109128
ParseResult();
110129
}
130+
131+
public void ParseResultLuaTable(LuaTable luaTable,int depth)
132+
{
133+
if (depth >= GSUnityLuaShellConst.LuaTableDepth)
134+
{
135+
return;
136+
}
137+
138+
foreach (var key in luaTable.GetKeys())
139+
{
140+
var value = luaTable[key];
141+
if (value == null)
142+
{
143+
treeView.AddChild($"{key} nil",false,depth);
144+
continue;
145+
}
146+
treeView.AddChild($"{key} {value.ToString()}",false,depth);
147+
if (value is LuaTable)
148+
{
149+
ParseResultLuaTable(value as LuaTable, depth+1);
150+
}
151+
}
152+
153+
for (int i = 1; i <= luaTable.Length; i++)
154+
{
155+
object value = luaTable[i];
156+
if (value == null)
157+
{
158+
break;
159+
}
160+
treeView.AddChild($"{value.ToString()}",false,depth);
161+
if (value is LuaTable)
162+
{
163+
ParseResultLuaTable(value as LuaTable, depth+1);
164+
}
165+
}
166+
}
111167
}
112168
}

Assets/Scipts/GSUnityLuaShellWindowExec.cs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using XLua;
35

46
namespace GSUnityLuaShell
@@ -7,6 +9,8 @@ namespace GSUnityLuaShell
79
public partial class GSUnityLuaShellWindow
810
{
911
public delegate LuaEnv GSLuaShellDelegate();
12+
13+
private LuaTable mEnvTable = null;
1014

1115
// private GSLuaShellDelegate _luaEnvDelegate;
1216

@@ -17,6 +21,7 @@ public partial class GSUnityLuaShellWindow
1721

1822
public object[] Exec(string command)
1923
{
24+
2025
if (command == null)
2126
{
2227
return new object[]{"command is null"};
@@ -46,12 +51,28 @@ public object[] Exec(string command)
4651
{
4752
return new object[]{"luaEnv is null,please check your luaEnv avaliable"};
4853
}
54+
55+
if (mEnvTable == null)
56+
{
57+
try
58+
{
59+
mEnvTable = luaEnv.DoString(@"
60+
local env = {}
61+
setmetatable(env, {__index = _G})
62+
return env
63+
") [0] as LuaTable;
64+
}
65+
catch (Exception e)
66+
{
67+
return new object[]{"init lua env failed",e};
68+
}
69+
}
4970

5071
object[] objects = null;
5172
bool success = false;
5273
try
5374
{
54-
objects = luaEnv.DoString($"return {command}");
75+
objects = luaEnv.DoString($"return {command}",env:mEnvTable);
5576
success = true;
5677
}
5778
catch (Exception e)
@@ -62,13 +83,14 @@ public object[] Exec(string command)
6283
{
6384
try
6485
{
65-
objects = luaEnv.DoString(command);
86+
objects = luaEnv.DoString(command,env:mEnvTable);
6687
}
6788
catch (Exception e)
6889
{
6990
return new object[]{$"command:{command}",e};
7091
}
7192
}
93+
7294
return objects;
7395
}
7496
}

Assets/Scipts/GSUnityLuaShellWindowGM.cs

+3-42
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public object[] ExecGM(string command)
6464
[GSUnityLuaShellGMItem(("pl"))]
6565
public object[] Loaded()
6666
{
67-
return getValue("package.loaded");
67+
return Exec("package.loaded");
6868
}
6969

7070
[GSUnityLuaShellGMItem(("r"))]
@@ -90,52 +90,13 @@ public object[] Reload()
9090
end
9191
return all_loaded_packages
9292
";
93-
return getValue(command);
93+
return Exec(command);
9494
}
9595

9696
[GSUnityLuaShellGMItem(("g"))]
9797
public object[] G()
9898
{
99-
return getValue("_G");
100-
}
101-
102-
private object[] getValue(string value)
103-
{
104-
object[] objects = Exec(value);
105-
if (objects.Length <= 0)
106-
{
107-
return new object[]{$"value:{value} not exit"};
108-
}
109-
110-
System.Collections.Generic.List<object> results = new System.Collections.Generic.List<object>();
111-
printLuaValue(results,objects[0]);
112-
return results.ToArray();
113-
}
114-
115-
private void printLuaValue(System.Collections.Generic.List<object> result,object obj)
116-
{
117-
if (obj is LuaTable)
118-
{
119-
LuaTable table = obj as LuaTable;
120-
foreach (var key in table.GetKeys())
121-
{
122-
result.Add($"{key} {table[key].ToString()}");
123-
}
124-
125-
for (int i = 1; i <= int.MaxValue; i++)
126-
{
127-
object value = table[i];
128-
if (value == null)
129-
{
130-
break;
131-
}
132-
result.Add($"{table[i].ToString()}");
133-
}
134-
}
135-
else
136-
{
137-
result.Add(obj.ToString());
138-
}
99+
return Exec("_G");
139100
}
140101
}
141102

readme.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
A Lua Shell for Unity.
1+
# UnityLuaShell
22

3+
A Lua Shell for Unity.
34

45

5-
Reference: https://github.com/kavenGw/unity-shell
66

7+
# Reference
78

9+
https://github.com/kavenGw/unity-shell
810

9-
Depends on: https://github.com/protocolbuffers/protobuf
1011

1112

13+
## Dependences
1214

13-
15+
https://github.com/protocolbuffers/protobuf
16+
17+
18+
19+
# Contact
20+
21+

0 commit comments

Comments
 (0)