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); }
// [MenuItem("JS for Unity/Generate JS Enum Bindings")] // public static void GenerateEnumBindings() // { // JSGenerator2.OnBegin(); // // for (int i = 0; i < JSBindingSettings.enums.Length; i++) // { // JSGenerator2.Clear(); // JSGenerator2.type = JSBindingSettings.enums[i]; // JSGenerator2.GenerateEnum(); // } // // JSGenerator2.OnEnd(); // // Debug.Log("Generate JS Enum Bindings finish. total = " + JSBindingSettings.enums.Length.ToString()); // } //public static Dictionary<Type, string> typeClassName = new Dictionary<Type, string>(); //static string className = string.Empty; public static void GenBindings(Type[] arrEnums, Type[] arrClasses) { JSGenerator.OnBegin(); TextFile tfAll = new TextFile(); TextFile tfFun = tfAll.Add("(function ($hc) {").In().Add("\"use strict\";"); int hc = 1; // enums for (int i = 0; i < arrEnums.Length; i++) { JSGenerator.Clear(); JSGenerator.type = arrEnums[i]; TextFile tf = JSGenerator.GenEnum(); if (tf != null) { tfFun.Add("if ($hc < {0}) {{ return; }}", hc++); tfFun.AddLine().Add(tf.Ch); } } // classes for (int i = 0; i < arrClasses.Length; i++) { JSGenerator.Clear(); JSGenerator.type = arrClasses[i]; //if (!typeClassName.TryGetValue(type, out className)) // className = type.Name; TextFile tf = JSGenerator.GenerateClass(); tfFun.Add("if ($hc < {0}) {{ return; }}", hc++); tfFun.AddLine() //.Add("if (Bridge.findObj(\"{0}\") == null) {{", type.JsFullName()) //.In() .Add(tf.Ch) //.BraceOut() ; } tfFun.Out().Add("})(1000000);"); File.WriteAllText(JSMgr.jsGenFile, tfAll.Format(-1)); JSGenerator.OnEnd(); Debug.Log("Generate JS Bindings OK. enum " + arrEnums.Length.ToString() + ", class " + arrClasses.Length.ToString()); }
static void GenAMessage(Info info) { TextFile tf = new TextFile(null, "// auto gen"); tf.Add("using UnityEngine;"); tf.Add("using UnityEngine.UI;"); tf.AddLine(); TextFile tfNs = tf.Add("namespace jsb").BraceIn(); { TextFile tfC = tfNs.Add("public class {0} : MonoBehaviour", info.className).BraceIn(); { TextFile tfM = tfC.Add("public void {0}", info.signature).BraceIn(); { tfM.Add("JSComponent[] coms = GetComponents<JSComponent>();") .Add("if (coms == null || coms.Length == 0)") .BraceIn() .Add("Destroy(this);") .Add("return;") .BraceOut() .AddLine() .Add("foreach (var com in coms)"); TextFile tfF = tfM.BraceIn(); { tfF.Add("com.RecvMsg({0});", info.argList); } tfF.BraceOut(); } tfM.BraceOut(); } tfC.BraceOut(); } tfNs.BraceOut(); string s = tf.Format(-1); File.WriteAllText(CsDir + "/" + info.className + ".cs", 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 void GenWraps(Type[] arrEnums, Type[] arrClasses, HashSet <string> bridgeTypes) { GeneratorHelp.ClearTypeInfo(); Dictionary <Type, TypeStatus> dict = new Dictionary <Type, TypeStatus>(); Action <Type> onNewType = null; onNewType = (nt) => { while (true) { if (nt.IsByRef || nt.IsArray) { nt = nt.GetElementType(); continue; } if (nt.IsGenericType && !nt.IsGenericTypeDefinition) { foreach (var ga in nt.GetGenericArguments()) { onNewType(ga); } nt = nt.GetGenericTypeDefinition(); continue; } if (nt.IsGenericParameter) { return; } break; } if (!bridgeTypes.Contains(nt.FullName) && !dict.ContainsKey(nt)) { dict.Add(nt, new TypeStatus()); } }; Func <Type, TypeStatus> getParent = (type) => { if (dict.ContainsKey(type)) { return(dict[type]); } return(null); }; foreach (var type in arrEnums) { onNewType(type); } foreach (var type in arrClasses) { onNewType(type); } while (true) { Type[] keys = new Type[dict.Count]; dict.Keys.CopyTo(keys, 0); foreach (Type type in keys) { TypeStatus ts = dict[type]; if (ts.status != TypeStatus.Status.Wait) { continue; } if (ShouldIgnoreType(type)) { ts.status = TypeStatus.Status.Ignored; continue; } if (type.IsEnum) { GenEnum(type, ts, getParent, onNewType); } else if (typeof(Delegate).IsAssignableFrom(type)) { GenDelegate(type, ts, getParent, onNewType); } else { GenInterfaceOrStructOrClass(type, ts, getParent, onNewType); } } bool bContinue = false; foreach (var kv in dict) { if (kv.Value.status == TypeStatus.Status.Wait) { bContinue = true; break; } } if (!bContinue) { break; } } TextFile tfAll = new TextFile(); tfAll.Add("#pragma warning disable 626, 824"); foreach (var kv in dict) { if (kv.Value.status == TypeStatus.Status.Exported && !kv.Value.IsInnerType) { tfAll.Add(kv.Value.tf.Ch); tfAll.AddLine(); } } tfAll.Add("#pragma warning restore 626, 824"); File.WriteAllText(JSBindingSettings.CswFilePath, tfAll.Format(-1)); }