Skip to content

Commit 9ade75f

Browse files
committed
初次提交
1 parent c3bc41d commit 9ade75f

File tree

228 files changed

+136
-8501
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

228 files changed

+136
-8501
lines changed

Diff for: Assembly-CSharp.zip

-359 KB
Binary file not shown.

Diff for: Assets/YKFramwork/Editor/DllToLuaLib.cs

+93-61
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
public class DllToLuaLib : Editor {
1212

1313
private static string[] LUA_KEYWORDS = { "local", "function", "end", "then" };
14-
private static string[] DLL_NAMES = { "mscorlib", "UnityEngine", "Assembly-CSharp" };
14+
private static string[] DLL_NAMES = {
15+
"mscorlib",
16+
#if UNITY_2017_1_OR_NEWER
17+
"UnityEngine.CoreModule",
18+
#else
19+
"UnityEngine",
20+
#endif
21+
"Assembly-CSharp"
22+
};
1523

1624
[MenuItem("Tools/DllToLuaLib", false, -100)]
1725
private static void GenDlls()
@@ -20,7 +28,13 @@ private static void GenDlls()
2028
List<MethodInfo> allExtensionMethodList = new List<MethodInfo>();
2129
foreach (string dllName in DLL_NAMES)
2230
{
23-
Assembly assembly = Assembly.Load(dllName);
31+
Assembly assembly = null;
32+
try
33+
{
34+
assembly = Assembly.Load(dllName);
35+
}
36+
catch (FileNotFoundException) { }
37+
2438
if (assembly != null)
2539
{
2640
Type[] types = assembly.GetTypes();
@@ -79,11 +93,17 @@ private static void GenDlls()
7993
foreach (string nameSpace in nameSpaceSet)
8094
{
8195
string fileName = nameSpace + ".ns.lua";
82-
string content = nameSpace + " = {}";
96+
StringBuilder contentSb = new StringBuilder();
97+
contentSb.Append("---@class ");
98+
contentSb.Append(nameSpace);
99+
contentSb.AppendLine();
100+
contentSb.Append(nameSpace);
101+
contentSb.Append(" = {}");
102+
string content = contentSb.ToString();
83103
fileDict[fileName] = Encoding.UTF8.GetBytes(content);
84104
}
85-
string zipFileName = Application.dataPath + "/../" + dllName + ".zip";
86-
ZipDerctory(zipFileName, fileDict);
105+
string zipFileName = Application.dataPath + "/../LuaLib/" + dllName + ".zip";
106+
WriteZip(zipFileName, fileDict);
87107
Debug.Log(dllName + ".zip generating is complete!");
88108
}
89109
}
@@ -104,15 +124,15 @@ private static List<MethodInfo> GetExtensionMethods(Type extendedType, List<Meth
104124

105125
private static void GenType(Type type, List<MethodInfo> extensionMethodList, out string fileName, out string content)
106126
{
107-
string typeName = TypeToString(type, true);
127+
string typeName = TypeToString(type, false, true);
108128
string typeFileName = typeName + ".lua";
109129
StringBuilder typeScriptSb = new StringBuilder();
110130
typeScriptSb.Append("---@class ");
111131
typeScriptSb.Append(typeName);
112132
typeScriptSb.Append(" : ");
113133
if (type.BaseType != null)
114134
{
115-
typeScriptSb.Append(TypeToString(type.BaseType, true));
135+
typeScriptSb.Append(TypeToString(type.BaseType, false, true));
116136
}
117137
else
118138
{
@@ -295,14 +315,18 @@ private static void GenType(Type type, List<MethodInfo> extensionMethodList, out
295315
{
296316
ParameterInfo param = paramList[paramIndex];
297317
typeScriptSb.Append("---@param ");
298-
typeScriptSb.Append(GetParamName(param));
299-
typeScriptSb.Append(" ");
300-
typeScriptSb.Append(TypeToString(param.ParameterType));
301318
if (param.IsDefined(typeof(ParamArrayAttribute), false))
302319
{
303-
typeScriptSb.Append("|");
320+
typeScriptSb.Append("... ");
304321
typeScriptSb.Append(TypeToString(param.ParameterType.GetElementType()));
322+
typeScriptSb.Append("|");
323+
}
324+
else
325+
{
326+
typeScriptSb.Append(GetParamName(param));
327+
typeScriptSb.Append(" ");
305328
}
329+
typeScriptSb.Append(TypeToString(param.ParameterType));
306330
typeScriptSb.AppendLine();
307331
}
308332
if (returnList.Count > 0)
@@ -328,14 +352,14 @@ private static void GenType(Type type, List<MethodInfo> extensionMethodList, out
328352
}
329353
typeScriptSb.Append(methodName);
330354
typeScriptSb.Append("(");
331-
if (paramList.Count > 0)
332-
{
333-
typeScriptSb.Append(GetParamName(paramList[0]));
334-
}
335-
for (int paramIndex = 1; paramIndex < paramList.Count; paramIndex++)
355+
for (int paramIndex = 0; paramIndex < paramList.Count; paramIndex++)
336356
{
337-
typeScriptSb.Append(", ");
338-
typeScriptSb.Append(GetParamName(paramList[paramIndex]));
357+
if (paramIndex > 0)
358+
{
359+
typeScriptSb.Append(", ");
360+
}
361+
ParameterInfo param = paramList[paramIndex];
362+
typeScriptSb.Append(param.IsDefined(typeof(ParamArrayAttribute), false) ? "..." : GetParamName(param));
339363
}
340364
typeScriptSb.Append(") end");
341365
typeScriptSb.AppendLine();
@@ -351,7 +375,7 @@ private static void GenType(Type type, List<MethodInfo> extensionMethodList, out
351375
content = typeScriptSb.ToString();
352376
}
353377

354-
private static string TypeToString(Type type, bool classDefine = false)
378+
private static string TypeToString(Type type, bool inFun = false, bool classDefine = false)
355379
{
356380
if (!classDefine)
357381
{
@@ -382,15 +406,15 @@ private static string TypeToString(Type type, bool classDefine = false)
382406

383407
if (type.IsArray)
384408
{
385-
return TypeToString(type.GetElementType()) + "[]";
409+
return TypeToString(type.GetElementType(), inFun) + "[]";
386410
}
387411

388412
if (type.IsGenericType)
389413
{
390414
Type[] genericArgTypes = type.GetGenericArguments();
391415
if (genericArgTypes.Length == 1 && typeof(IList<>).MakeGenericType(genericArgTypes).IsAssignableFrom(type))
392416
{
393-
return TypeToString(genericArgTypes[0]) + "[]";
417+
return TypeToString(genericArgTypes[0], inFun) + "[]";
394418
}
395419

396420
if (genericArgTypes.Length == 2 && typeof(IDictionary<,>).MakeGenericType(genericArgTypes).IsAssignableFrom(type))
@@ -406,14 +430,14 @@ private static string TypeToString(Type type, bool classDefine = false)
406430
{
407431
MethodInfo method = type == typeof(Delegate) || type == typeof(MulticastDelegate) ?
408432
type.GetMethod("DynamicInvoke") : type.GetMethod("Invoke");
409-
return MethodToString(method);
433+
return MethodToString(method, inFun);
410434
}
411435
}
412436

413437
if (type.FullName == null)
414438
{
415439
//GenericTypeDefinition like T
416-
return TypeToString(type.BaseType ?? typeof(object));
440+
return TypeToString(type.BaseType ?? typeof(object), inFun, classDefine);
417441
}
418442

419443
char[] typeNameChars = type.ToString().ToCharArray();
@@ -457,7 +481,7 @@ private static string TypeToString(Type type, bool classDefine = false)
457481
return sb.ToString();
458482
}
459483

460-
private static string MethodToString(MethodInfo method)
484+
private static string MethodToString(MethodInfo method, bool inFun = false)
461485
{
462486
if (method == null)
463487
{
@@ -486,58 +510,61 @@ private static string MethodToString(MethodInfo method)
486510
}
487511
}
488512

489-
return MethodToString(paramList, returnList);
513+
return MethodToString(paramList, returnList, inFun);
490514
}
491515

492-
private static string MethodToString(List<ParameterInfo> paramList, List<ParameterInfo> returnList)
516+
private static string MethodToString(List<ParameterInfo> paramList, List<ParameterInfo> returnList, bool inFun = false)
493517
{
494518
StringBuilder sb = new StringBuilder();
495-
sb.Append("fun(");
496-
if (paramList.Count > 0)
519+
if (inFun && returnList.Count > 0)
497520
{
498-
sb.Append(GetParamName(paramList[0]));
499-
sb.Append(":");
500-
sb.Append(ParamTypeToString(paramList[0].ParameterType));
521+
sb.Append("(");
501522
}
502-
for (int paramIndex = 1; paramIndex < paramList.Count; paramIndex++)
503-
{
504-
ParameterInfo param = paramList[paramIndex];
505-
sb.Append(", ");
506-
sb.Append(GetParamName(param));
507-
sb.Append(":");
508-
sb.Append(ParamTypeToString(param.ParameterType));
523+
sb.Append("fun(");
524+
for (int paramIndex = 0; paramIndex < paramList.Count; paramIndex++)
525+
{
526+
if (paramIndex > 0)
527+
{
528+
sb.Append(", ");
529+
}
530+
ParameterInfo param = paramList[paramIndex];
509531
if (param.IsDefined(typeof(ParamArrayAttribute), false))
510532
{
511-
sb.Append("|");
533+
sb.Append("...:");
512534
sb.Append(TypeToString(param.ParameterType.GetElementType()));
535+
sb.Append("|");
513536
}
537+
else
538+
{
539+
sb.Append(GetParamName(param));
540+
sb.Append(":");
541+
}
542+
sb.Append(TypeToString(param.ParameterType));
514543
}
515544
sb.Append(")");
516545
if (returnList.Count > 0)
517546
{
518547
sb.Append(":");
519-
sb.Append(ParamTypeToString(returnList[0].ParameterType));
548+
if (returnList.Count > 1)
549+
{
550+
sb.Append("(");
551+
}
520552
for (int returnIndex = 1; returnIndex < returnList.Count; returnIndex++)
521553
{
522-
sb.Append(", ");
523-
sb.Append(ParamTypeToString(returnList[returnIndex].ParameterType));
554+
if (returnIndex > 0)
555+
{
556+
sb.Append(", ");
557+
}
558+
sb.Append(TypeToString(returnList[returnIndex].ParameterType, returnList.Count == 1));
559+
}
560+
if (returnList.Count > 1)
561+
{
562+
sb.Append(")");
563+
}
564+
if (inFun)
565+
{
566+
sb.Append(")");
524567
}
525-
}
526-
return sb.ToString();
527-
}
528-
529-
private static string ParamTypeToString(Type paramType)
530-
{
531-
StringBuilder sb = new StringBuilder();
532-
bool isDelegate = typeof(Delegate).IsAssignableFrom(paramType);
533-
if (isDelegate)
534-
{
535-
sb.Append("(");
536-
}
537-
sb.Append(TypeToString(paramType));
538-
if (isDelegate)
539-
{
540-
sb.Append(")");
541568
}
542569
return sb.ToString();
543570
}
@@ -556,9 +583,15 @@ private static string GetParamName(ParameterInfo param)
556583
return paramName;
557584
}
558585

559-
private static void ZipDerctory(string zipedDirectory, Dictionary<string, byte[]> fileDict, int compressionLevel = 9)
586+
private static void WriteZip(string zipFileName, Dictionary<string, byte[]> fileDict, int compressionLevel = 9)
560587
{
561-
FileStream fileStream = File.Create(zipedDirectory);
588+
FileInfo zipFile = new FileInfo(zipFileName);
589+
DirectoryInfo dir = zipFile.Directory;
590+
if (!dir.Exists)
591+
{
592+
dir.Create();
593+
}
594+
FileStream fileStream = zipFile.Create();
562595
ZipOutputStream zipStream = new ZipOutputStream(fileStream);
563596
zipStream.SetLevel(compressionLevel);
564597
foreach (string fileName in fileDict.Keys)
@@ -567,7 +600,6 @@ private static void ZipDerctory(string zipedDirectory, Dictionary<string, byte[]
567600
byte[] buffer = fileDict[fileName];
568601
zipStream.Write(buffer, 0, buffer.Length);
569602
}
570-
zipStream.Flush();
571603
zipStream.Close();
572604
fileStream.Close();
573605
}

Diff for: Lua/.idea/Lua.iml

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

Diff for: Lua/.idea/libraries/Assembly_CSharp.xml

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

Diff for: Lua/.idea/libraries/LuaApi.xml

-9
This file was deleted.

Diff for: Lua/.idea/libraries/UnityEngine.xml

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

Diff for: Lua/.idea/libraries/mscorlib.xml

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

0 commit comments

Comments
 (0)