//no param private void RegisterNMethod(int indent, StringBuilder sb) { string indentStr = CSToLuaRegisterHelper.GetIndent(indent); if (methodInfo.ReturnType == typeof(void)) { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}{1}.{2}();", indentStr, classRegister.RegisterType, methodInfo.Name)); } else { sb.AppendLine(string.Format("{0}obj.{1}();", indentStr, methodInfo.Name)); } } else { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}{1} rv = {2}.{3}();", indentStr, CSToLuaRegisterHelper.GetTypeFullName(methodInfo.ReturnType), classRegister.RegisterType, methodInfo.Name)); } else { sb.AppendLine(string.Format("{0}{1} rv = obj.{2}();", indentStr, CSToLuaRegisterHelper.GetTypeFullName(methodInfo.ReturnType), methodInfo.Name)); } } }
private void RegisterReturnMethod(int indent, StringBuilder sb) { int returnCount = 0; if (methodInfo.ReturnType == typeof(void)) { returnCount = 0; } else { sb.Append(CSToLuaRegisterHelper.GetPushAction(indent, methodInfo.ReturnType, "rv")); returnCount += 1; } returnCount += parRefIndex.Count; returnCount += parOutIndex.Count; for (int i = 0; i < parRefIndex.Count; i++) { sb.Append(CSToLuaRegisterHelper.GetPushAction(indent, parTypes[parRefIndex[i]], "v" + parRefIndex[i])); } for (int i = 0; i < parOutIndex.Count; i++) { sb.Append(CSToLuaRegisterHelper.GetPushAction(indent, parTypes[parOutIndex[i]], "v" + parOutIndex[i])); } sb.AppendLine(string.Format("{0}return {1};", CSToLuaRegisterHelper.GetIndent(indent), returnCount)); }
private void RegisterCheckParamCount(int indent, StringBuilder sb) { int minParamCount = parTypes.Count; if (parDefaultStartIndex != -1) { minParamCount = parDefaultStartIndex; } else { if (isParams) { minParamCount -= 1; } } if (!methodInfo.IsStatic) { minParamCount += 1; } string indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}int top = luaState.GetTop();", indentStr)); sb.AppendLine(string.Format("{0}if(top< {1}){{", indentStr, minParamCount)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}UnityEngine.Debug.LogError(\"Count not enough!!\");", indentStr)); sb.AppendLine(string.Format("{0}return 0;", indentStr)); indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}", indentStr)); }
private void RegisterParamsDefaultMethod(int indent, StringBuilder sb, int startIndex) { string indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}if(top > " + (startIndex + parDefaultStartIndex - 2) + " && top < " + (startIndex + parTypes.Count - 1) + "){{", indentStr)); RegisterNParamsDefaultMethod(indent + 1, sb, startIndex, true); RegisterReturnMethod(indent + 1, sb); sb.AppendLine(string.Format("{0}}}else{{", indentStr)); RegisterParamsNDefaultMethod(indent + 1, sb, startIndex); RegisterReturnMethod(indent + 1, sb); sb.AppendLine(string.Format("{0}}}", indentStr)); }
public string RegisterFunctionToLua(int indent) { if (constructorInfo == null || constructorInfo.Length == 0) { return(""); } StringBuilder sb = new StringBuilder(); string indentStr = ""; sb.Append(CSToLuaRegisterHelper.GetRegisterFunStart(indent, "CreateInstance")); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}int top = luaState.GetTop();", indentStr)); sb.AppendLine(string.Format("{0}System.Object[] pInfos = new System.Object[top-1];", indentStr)); sb.AppendLine(string.Format("{0}for(int i =0;i<top -1;i++){{", indentStr)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}pInfos[i] = luaState.ToSystemObject(i+2,typeof(System.Object));", indentStr)); indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}", indentStr)); sb.AppendLine(string.Format("{0}try{{", indentStr)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}{1} obj = ({1})System.Activator.CreateInstance(typeof({1}), pInfos);", indentStr, CSToLuaRegisterHelper.GetTypeFullName(classRegister.RegisterType))); sb.Append(CSToLuaRegisterHelper.GetPushAction(indent, classRegister.RegisterType, "obj")); indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}catch{{", indentStr)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}luaState.PushNil();", indentStr)); indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}", indentStr)); sb.AppendLine(string.Format("{0}return 1;", indentStr)); indent--; sb.Append(CSToLuaRegisterHelper.GetRegisterFunEnd(indent)); return(sb.ToString()); }
// no params ,no default value private void RegisterNPamasNDefaultMethod(int indent, StringBuilder sb, int startIndex) { string indentStr = CSToLuaRegisterHelper.GetIndent(indent); List <string> values = new List <string>(); for (int i = 0; i < parTypes.Count; i++) { sb.Append(CSToLuaRegisterHelper.GetToAction(indent, parTypes[i], startIndex + i, "v" + i)); if (parRefIndex.IndexOf(i) >= 0) { values.Add("ref v" + i); } else if (parOutIndex.IndexOf(i) >= 0) { values.Add("out v" + i); } else { values.Add("v" + i); } } if (methodInfo.ReturnType == typeof(void)) { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}{1}.{2}({3});", indentStr, classRegister.RegisterType, methodInfo.Name, string.Join(",", values.ToArray()))); } else { sb.AppendLine(string.Format("{0}obj.{1}({2});", indentStr, methodInfo.Name, string.Join(",", values.ToArray()))); } } else { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}{1} rv = {2}.{3}({4});", indentStr, CSToLuaRegisterHelper.GetTypeFullName(methodInfo.ReturnType), classRegister.RegisterType, methodInfo.Name, string.Join(",", values.ToArray()))); } else { sb.AppendLine(string.Format("{0}{1} rv = obj.{2}({3});", indentStr, CSToLuaRegisterHelper.GetTypeFullName(methodInfo.ReturnType), methodInfo.Name, string.Join(",", values.ToArray()))); } } }
public string RegisterToLua() { int indent = 0; string indentStr = ""; StringBuilder sb = new StringBuilder(); sb.AppendLine("public static class " + GetRegisterFileName() + "{"); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}private static Game.Core.DotLua.LuaState luaState;", indentStr)); sb.AppendLine(string.Format("{0}public static int RegisterToLua(Game.Core.DotLua.LuaState lua){{", indentStr)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}luaState = lua;", indentStr)); sb.AppendLine(string.Format("{0}luaState.NewTable();", indentStr)); sb.AppendLine(string.Format("{0}luaState.PushValue(-1);", indentStr)); sb.AppendLine(string.Format("{0}luaState.SetField(-2,\"__index\");", indentStr)); sb.AppendLine(string.Format("{0}luaState.PushValue(-1);", indentStr)); sb.AppendLine(string.Format("{0}luaState.SetGlobal(\"{1}\");", indentStr, registerType.Name)); sb.AppendLine(string.Format("{0}luaState.PushValue(-1);", indentStr)); sb.AppendLine(string.Format("{0}luaState.SetMetaTable(-2);", indentStr)); foreach (ICSToLuaRegister register in registerList) { sb.Append(register.RegisterActionToLua(indent)); } sb.AppendLine(string.Format("{0}return luaState.L_Ref(Game.Core.DotLua.LuaAPI.LUA_REGISTRYINDEX);", indentStr)); indent--; sb.AppendLine(string.Format("{0}}}", CSToLuaRegisterHelper.GetIndent(indent))); foreach (ICSToLuaRegister register in registerList) { sb.Append(register.RegisterFunctionToLua(indent)); } sb.AppendLine("}"); return(sb.ToString()); }
private string RegisterGetFunction(int indent) { StringBuilder sb = new StringBuilder(); sb.Append(CSToLuaRegisterHelper.GetRegisterFunStart(indent, GetFunName(CSToLuaFieldType.Get))); indent++; if (fieldInfo.IsStatic) { sb.Append(CSToLuaRegisterHelper.GetPushAction(indent, fieldInfo.FieldType, CSToLuaRegisterHelper.GetTypeFullName(classRegister.RegisterType) + "." + fieldInfo.Name)); } else { sb.Append(CSToLuaRegisterHelper.GetToUserDataAction(indent, classRegister.RegisterType, 1, "obj")); sb.Append(CSToLuaRegisterHelper.GetPushAction(indent, fieldInfo.FieldType, "obj." + fieldInfo.Name)); } sb.AppendLine(string.Format("{0}return 1;", CSToLuaRegisterHelper.GetIndent(indent))); indent--; sb.Append(CSToLuaRegisterHelper.GetRegisterFunEnd(indent)); return(sb.ToString()); }
private string RegisterGetSetFunction(int indent) { StringBuilder sb = new StringBuilder(); sb.Append(CSToLuaRegisterHelper.GetRegisterFunStart(indent, GetFunName(CSToLuaFieldType.GetSet))); indent++; sb.AppendLine(string.Format("{0}int top = luaState.GetTop();", CSToLuaRegisterHelper.GetIndent(indent))); sb.AppendLine(string.Format("{0}if(top == 0){{", CSToLuaRegisterHelper.GetIndent(indent))); indent++; sb.AppendLine(string.Format("{0}return {1}(ptr);", CSToLuaRegisterHelper.GetIndent(indent), GetFunName(CSToLuaFieldType.Get))); indent--; sb.AppendLine(string.Format("{0}}}else{{", CSToLuaRegisterHelper.GetIndent(indent))); indent++; sb.AppendLine(string.Format("{0}return {1}(ptr);", CSToLuaRegisterHelper.GetIndent(indent), GetFunName(CSToLuaFieldType.Set))); indent--; sb.AppendLine(string.Format("{0}}}", CSToLuaRegisterHelper.GetIndent(indent))); indent--; sb.Append(CSToLuaRegisterHelper.GetRegisterFunEnd(indent)); return(sb.ToString()); }
private string RegisterSetFunction(int indent) { StringBuilder sb = new StringBuilder(); sb.Append(CSToLuaRegisterHelper.GetRegisterFunStart(indent, GetFunName(CSToLuaPropertyType.Set))); indent++; if (propertyInfo.GetSetMethod().IsStatic) { sb.Append(CSToLuaRegisterHelper.GetToAction(indent, propertyInfo.PropertyType, 1, "v")); sb.AppendLine(string.Format("{0}{1}.{2} = v;", CSToLuaRegisterHelper.GetIndent(indent), CSToLuaRegisterHelper.GetTypeFullName(classRegister.RegisterType), propertyInfo.Name)); } else { sb.Append(CSToLuaRegisterHelper.GetToUserDataAction(indent, classRegister.RegisterType, 1, "obj")); sb.Append(CSToLuaRegisterHelper.GetToAction(indent, propertyInfo.PropertyType, 2, "v")); sb.AppendLine(string.Format("{0}obj.{1} = v;", CSToLuaRegisterHelper.GetIndent(indent), propertyInfo.Name)); } sb.AppendLine(string.Format("{0}return 0;", CSToLuaRegisterHelper.GetIndent(indent))); indent--; sb.Append(CSToLuaRegisterHelper.GetRegisterFunEnd(indent)); return(sb.ToString()); }
//have params but no default private void RegisterParamsNDefaultMethod(int indent, StringBuilder sb, int startIndex) { string indentStr = CSToLuaRegisterHelper.GetIndent(indent); List <string> values = new List <string>(); for (int i = 0; i < parTypes.Count - 1; i++) { sb.Append(CSToLuaRegisterHelper.GetToAction(indent, parTypes[i], startIndex + i, "v" + i)); if (parRefIndex.IndexOf(i) >= 0) { values.Add("ref v" + i); } else if (parOutIndex.IndexOf(i) >= 0) { values.Add("out v" + i); } else { values.Add("v" + i); } } Type paramsType = parTypes[parTypes.Count - 1]; string paramsTypeName = CSToLuaRegisterHelper.GetTypeFullName(paramsType); paramsTypeName = paramsTypeName.Replace("[]", ""); Type sPType = CSToLuaRegisterHelper.GetTypeByName(paramsTypeName); sb.AppendLine(string.Format("{0}{1}[] ps;", indentStr, paramsTypeName)); sb.AppendLine(string.Format("{0}if(top > " + (startIndex + parTypes.Count - 2) + "){{", indentStr)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}ps = new {1}[{2}];", indentStr, paramsTypeName, (startIndex + parTypes.Count - 3))); sb.AppendLine(string.Format("{0}for(int i =0;i<top - " + (startIndex + parTypes.Count - 2) + ";i++){{", indentStr)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); if (CSToLuaRegisterHelper.IsBaseType(sPType)) { sb.AppendLine(string.Format("{0}ps[{1}]={2}luaState.{3}({4});", indentStr, "i", CSToLuaRegisterHelper.GetToTypeCast(sPType), CSToLuaRegisterHelper.GetToActionStr(sPType), (startIndex + parTypes.Count - 1) + "+i")); } else { sb.AppendLine(string.Format("{0}ps[{1}]=({2})luaState.ToSystemObject({3},typeof({2}));", indentStr, "i", paramsTypeName, (startIndex + parTypes.Count - 1) + "+i")); } indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}", indentStr)); indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}else{{", indentStr)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}ps = new {1}[0];", indentStr, paramsTypeName)); indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}", indentStr)); values.Add("ps"); if (methodInfo.ReturnType == typeof(void)) { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}{1}.{2}({3});", indentStr, classRegister.RegisterType, methodInfo.Name, string.Join(",", values.ToArray()))); } else { sb.AppendLine(string.Format("{0}obj.{1}({2});", indentStr, methodInfo.Name, string.Join(",", values.ToArray()))); } } else { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}{1} rv = {2}.{3}({4});", indentStr, CSToLuaRegisterHelper.GetTypeFullName(methodInfo.ReturnType), classRegister.RegisterType, methodInfo.Name, string.Join(",", values.ToArray()))); } else { sb.AppendLine(string.Format("{0}{1} rv = obj.{2}({3});", indentStr, CSToLuaRegisterHelper.GetTypeFullName(methodInfo.ReturnType), methodInfo.Name, string.Join(",", values.ToArray()))); } } }
//No params but have default private void RegisterNParamsDefaultMethod(int indent, StringBuilder sb, int startIndex, bool ingoreLast = false) { string indentStr = CSToLuaRegisterHelper.GetIndent(indent); List <string> values = new List <string>(); for (int i = 0; i < parDefaultStartIndex; i++) { sb.Append(CSToLuaRegisterHelper.GetToAction(indent, parTypes[i], startIndex + i, "v" + i)); if (parRefIndex.IndexOf(i) >= 0) { values.Add("ref v" + i); } else if (parOutIndex.IndexOf(i) >= 0) { values.Add("out v" + i); } else { values.Add("v" + i); } } for (int i = parDefaultStartIndex; i < parTypes.Count; i++) { string typeName = CSToLuaRegisterHelper.GetTypeFullName(parTypes[i]); object defValue = methodInfo.GetParameters()[i].DefaultValue; string defStr = CSToLuaRegisterHelper.GetDefaultValue(defValue, parTypes[i]); sb.AppendLine(string.Format("{0}{1} v" + i + " = {2};", indentStr, typeName, defStr)); } if (methodInfo.ReturnType != typeof(void)) { sb.AppendLine(string.Format("{0}{1} rv = {2};", indentStr, CSToLuaRegisterHelper.GetTypeFullName(methodInfo.ReturnType), CSToLuaRegisterHelper.GetDefaultValue(methodInfo.ReturnType))); } for (int i = 0; i < (ingoreLast ? parTypes.Count - parDefaultStartIndex : parTypes.Count - parDefaultStartIndex + 1); i++) { sb.AppendLine(string.Format("{0}if(top == {1}){{", indentStr, startIndex + parDefaultStartIndex + i - 1)); indent++; indentStr = CSToLuaRegisterHelper.GetIndent(indent); List <string> newValues = new List <string>(); newValues.AddRange(values); for (int j = 0; j < i; j++) { sb.Append(CSToLuaRegisterHelper.GetToActionWithNoType(indent, parTypes[parDefaultStartIndex + j], startIndex + parDefaultStartIndex + j, "v" + (parDefaultStartIndex + j))); newValues.Add("v" + (parDefaultStartIndex + j)); } if (methodInfo.ReturnType == typeof(void)) { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}{1}.{2}({3});", indentStr, classRegister.RegisterType, methodInfo.Name, string.Join(",", newValues.ToArray()))); } else { sb.AppendLine(string.Format("{0}obj.{1}({2});", indentStr, methodInfo.Name, string.Join(",", newValues.ToArray()))); } } else { if (methodInfo.IsStatic) { sb.AppendLine(string.Format("{0}rv = {1}.{2}({3});", indentStr, classRegister.RegisterType, methodInfo.Name, string.Join(",", newValues.ToArray()))); } else { sb.AppendLine(string.Format("{0}rv = obj.{1}({2});", indentStr, methodInfo.Name, string.Join(",", newValues.ToArray()))); } } indent--; indentStr = CSToLuaRegisterHelper.GetIndent(indent); sb.AppendLine(string.Format("{0}}}", indentStr)); } }