Beispiel #1
0
	static void writeMethod(StreamWriter n, MethodInfo info, Type t, string alias, List<string>[]  errors)
	{
		//参数有效性检查,遇到不支持的参数类型,就报错返回
		ParameterInfo[] pinfos = info.GetParameters();
		List<ParamTypeData> paramTypsList = new List<ParamTypeData>();
		try
		{
			for (int i = 0; i < pinfos.Length; ++i)
			{
				ParamTypeData paramType = new ParamTypeData(pinfos[i].ParameterType);
				paramTypsList.Add(paramType);
			}
		}
		catch (Exception e)
		{
			string err = string.Format("NOT SUPPORT Param type in: {0}.{1}(...) \n{2}", t.Name, info.Name, e.ToString());
			errors[0].Add(err);
			return;
		}

		writeLine(n,"[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]");

		string functionName = alias;
		if (string.IsNullOrEmpty(alias))
		{
			functionName = info.Name;
			string luafuncName;
			if (op_funcLuaNames.TryGetValue(functionName, out luafuncName))
			{
				functionName = luafuncName;
			}
		}

		funcNames.Add(functionName);

		ParamTypeData T = new ParamTypeData(t);
		ParamTypeData returnType = new ParamTypeData(info.ReturnType);
		writeLine(n,"public static int {0}(IntPtr L)", functionName);
		writeLine(n,"{");

		WriteTryBegin(n);

		int paramIndexBegin = 1;
		if (info.IsStatic == false)
		{
			paramIndexBegin = 2;
            writeSelf(n,T);
		}

		bool isStatic = info.IsStatic;

		for (int i = 0; i < pinfos.Length; ++i)
		{
			ParamTypeData paramType = paramTypsList[i];
			paraseParam(n, paramType, i, i + paramIndexBegin);
		}

		ParamTypeData[] paramTypes = paramTypsList.ToArray();

		//写入方法调用
		string opname; 
		if ( op_funcNames.TryGetValue(info.Name,out opname) )
		{
			if( info.Name != "op_UnaryNegation") //不是单目操作符
				writeLine(n, "{0} ret = ({1})param0 {2} ({3})param1;", returnType.csFullTypeName, paramTypes[0].csFullTypeName, opname, paramTypes[1].csFullTypeName);
			else
				writeLine(n, "{0} ret = {2}({1})param0;",returnType.csFullTypeName, paramTypes[0].csFullTypeName, opname);

		}
		else
		{
			writeLine(n, "{1} {2}.{0}(", info.Name, returnType.luaTypeName == "void" ? "" : returnType.csFullTypeName + " ret = ", isStatic ? T.csFullTypeName : "self");
			for (int i = 0; i < pinfos.Length; ++i)
			{
				writeLine(n, "({2})param{0}{1}", i, i == pinfos.Length - 1 ? "" : ",", paramTypes[i].csFullTypeName);
			}
			writeLine(n, ");");
		}

		if (returnType.luaTypeName != "void")
		{
			if (returnType.isNeedMt)
			{
				if (info.Name == "AddComponent" || info.Name == "GetComponent")
				{
					writeLine(n, "string mt = {0};", "param0");
				}
				else
				{
					if (returnType.IsNeedMetatable())
					{
						if (returnType.IsClass)
							writeLine(n, "string mt = ({0});", "ret != null ? ret.GetType().Name : \"\" ");
						else
							writeLine(n, "string mt = {0};", "ret.GetType().Name");

						int paramCount = pinfos.Length;
						if (isStatic == false)
							paramCount += 1;

						writeLine(n, "if(LuaDLL.lua_isstring(L,{0}))", paramCount + 1);
						writeLine(n, "\tmt = LuaDLL.lua_tostring(L,{0});", paramCount + 1);
					}
				}
				returnType.WriteToLuaFromCS(n, "ret", 0, "mt");
			}
			else
			{
				returnType.WriteToLuaFromCS(n, "ret", 0);
			}
		}

		if (info.Name == "Destroy")
		{
			writeLine(n, "IntPtr idptr = LuaDLL.lua_touserdata(L, 1);");
            writeLine(n, "LuaStatic.RemoveGameObject(idptr);");
		}
		else if (isStatic == false && t.IsValueType)
		{
			writeLine(n, "IntPtr idptr = LuaDLL.lua_touserdata(L, 1);");
			n.WriteLine("objs[idptr.ToInt64()] = self;");
		}

		writeLine(n,"return {0};", returnType.luaTypeName == "void" ? "0" : "1");

		WriteTryEnd(n);

		writeLine(n, "}");
		writeLine(n, "");
		writeLine(n, "");
	}
Beispiel #2
0
    //从cs对象读属性写回Lua
	static void writeReadProp(StreamWriter n, 
		Type objt,
		Type propt,
		string propName,
		bool isStatic,
		bool isEnum
		)
	{
		ParamTypeData objT = new ParamTypeData(objt);
		string objectTypeFullName = objT.csFullTypeName;

		//需要如下信息
		//属性名	info.Name							--> propName
		//属性类型	info.PropertyType.FullName			--> propTypeFullName
		//属性类型	info.PropertyType.Name				--> propTypeName
		//isStatic
		//对象类型	t.FullName							--> objectTypeFullName
		//info.PropertyType.IsEnum						--> isEnum
		//namespace	info.PropertyType.Namesapce			--> propNamespace

		writeLine(n, "//---------------------------------");

		funcNames.Add("get_"+propName);
		n.WriteLine("[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]");

		ParamTypeData paramType = new ParamTypeData(propt);
		writeLine(n,"public static int get_{0}(IntPtr L)", propName);
		writeLine(n,"{");

		WriteTryBegin(n);

        if (isStatic == false)
        {
            writeSelf(n,objT);
        }

		string owner = isStatic ? objectTypeFullName : "self";
		string variableExp = string.Format("{0}.{1}", owner, propName);

		paramType.WriteToLuaFromCS(n, variableExp, 0);

		n.WriteLine("return 1;");

		WriteTryEnd(n);

		n.WriteLine("}");
		n.WriteLine("");
		n.WriteLine("");
		n.WriteLine("");

	}