static TextFile GenEnum() { TextFile tf = new TextFile(); string typeName = type.ToString(); // tf.AddLine().Add("// {0}", typeName); // remove name space int lastDot = typeName.LastIndexOf('.'); if (lastDot >= 0) { typeName = typeName.Substring(lastDot + 1); } if (typeName.IndexOf('+') >= 0) { return(null); } TextFile tfDef = tf.Add("Bridge.define(\"{0}\", {{", JSNameMgr.JsFullName(type)).In(); tfDef.Add("$kind: \"enum\","); TextFile tfSta = tfDef.Add("statics: {").In(); Type uType = Enum.GetUnderlyingType(type); FieldInfo[] fields = type.GetFields(BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static); for (int i = 0; i < fields.Length; i++) { string v = ""; if (uType == typeof(ulong)) { v = System.Convert.ToUInt64(fields[i].GetValue(null)).ToString(); } else { v = System.Convert.ToInt64(fields[i].GetValue(null)).ToString(); } tfSta.Add("{0}: {1}{2}", fields[i].Name, v, i == fields.Length - 1 ? "" : ","); } tfSta.BraceOut(); tfDef.BraceOutSC(); return(tf); }
static void GenJsMessageIDs() { TextFile tf = new TextFile(null, "// auto gen"); tf.Add("// 记录每个脚本事件对应的ID号"); TextFile tfK = tf.Add("Bridge.MessageIDs = ").BraceIn(); for (int i = 0; i < infos.Length; i++) { Info info = infos[i]; tfK.Add("\"{0}\": {1},", info.methodName, i); } tfK.BraceOutSC(); string s = tf.Format(-1); File.WriteAllText(JsFile, s); }
static void GenMMgr(string className) { TextFile tf = new TextFile(null, "// auto gen"); tf.Add("using UnityEngine;"); tf.Add("using UnityEngine.UI;"); tf.Add("using System;"); tf.Add("using System.Collections;"); tf.Add("using System.Collections.Generic;"); tf.Add("using jsb;"); tf.AddLine(); TextFile tfNs = tf.Add("namespace jsb").BraceIn(); { TextFile tfC = tfNs.Add("public class {0}", className).BraceIn(); { tfC.Add("static Dictionary<string, int[]> jID = new Dictionary<string, int[]>();"); { tfC.AddMultiline(@" static int[] GetJsClassMessages(string jsFullName) { if (jID.ContainsKey(jsFullName)) return jID[jsFullName]; if (!JSMgr.vCall.CallJSFunctionName(JSCache.GetBridgeJsID(), ""getLMsgs"", jsFullName)) throw new Exception(""call Bridge.getLMsgs failed!""); string str = JSApi.getStringS((int)JSApi.GetType.JSFunRet); if (string.IsNullOrEmpty(str)) { jID[jsFullName] = null; return null; } string[] arr = str.Split(','); int[] r = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) r[i] = int.Parse(arr[i]); jID[jsFullName] = r; return r; }"); } tfC.AddLine(); { tfC.AddMultiline(@" public static void CreateMessages(string jsFullName, GameObject go) { int[] ids = GetJsClassMessages(jsFullName); if (ids == null) return; // ID号 JS和CS保持一致才不会错 for (int i = 0; i < ids.Length; i++) { Type type = MessageTypes[ids[i]]; if (go.GetComponent(type) == null) go.AddComponent(type); } }"); } tfC.AddLine(); { TextFile tfM = tfC.Add("static Type[] MessageTypes = new Type[]").BraceIn(); { for (int i = 0; i < infos.Length; i++) { Info info = infos[i]; tfM.Add("typeof(jsb.{0}),", info.className); } } tfM.BraceOutSC(); } } tfC.BraceOut(); } tfNs.BraceOut(); string s = tf.Format(-1); File.WriteAllText(CsDir + "/" + className + ".cs", s); }
public static TextFile Build_DelegateFunction(Type classType, MemberInfo memberInfo, Type delType, int methodTag, int argIndex) { // building a closure // a function having a up-value: jsFunction string getDelFunctionName = GetMethodArg_DelegateFuncionName(classType, memberInfo.Name, methodTag, argIndex); TextFile tf = new TextFile(); MethodInfo delInvoke = delType.GetMethod("Invoke"); ParameterInfo[] ps = delInvoke.GetParameters(); Type returnType = delType.GetMethod("Invoke").ReturnType; var argsParam = new args(); for (int i = 0; i < ps.Length; i++) { argsParam.Add(ps[i].Name); } // format as <t,u,v> string stringTOfMethod = string.Empty; if (delType.ContainsGenericParameters) { var arg = new args(); foreach (var t in delType.GetGenericArguments()) { arg.Add(t.Name); } stringTOfMethod = arg.Format(args.ArgsFormat.GenericT); } // this function name is used in BuildFields, don't change TextFile tfFun = tf.Add("public static {0} {1}{2}(CSRepresentedObject objFunction)", JSNameMgr.CsFullName(delType, CsNameOption.CompilableWithT), // [0] getDelFunctionName, // [2] stringTOfMethod // [1] ) .BraceIn(); { tfFun.Add("if (objFunction == null || objFunction.jsObjID == 0)") .In() .Add("return null;") .Out() .AddLine(); tfFun.Add("{0} action = JSMgr.getJSFunCSDelegateRel<{0}>(objFunction.jsObjID);", JSNameMgr.CsFullName(delType, CsNameOption.CompilableWithT)); tfFun.Add("if (action != null)") .In() .Add("return action;") .Out() .AddLine(); TextFile tfAction = tfFun.Add("action = ({0}) => ", argsParam.Format(args.ArgsFormat.OnlyList)) .BraceIn(); { tfAction.Add("JSMgr.vCall.CallJSFunctionValue(0, objFunction.jsObjID{0}{1});", (argsParam.Count > 0) ? ", " : "", argsParam); if (returnType != typeof(void)) { tfAction.Add("return (" + JSNameMgr.CsFullName(returnType) + ")" + JSDataExchangeEditor.Get_GetJSReturn(returnType) + ";"); } tfAction.BraceOutSC(); } tfFun.Add("JSMgr.addJSFunCSDelegateRel(objFunction.jsObjID, action);") .Add("return action;\n"); tfFun.BraceOut(); } return(tf); }