Ejemplo n.º 1
0
        void ExportPrefabs()
        {
            var export = new ExportLogger("PrefabListDocs");

            // Prefab List: http://docs.oxidemod.org/rust/#prefab-list
            export.Log("# Prefab List");

            foreach (var str in GameManifest.Current.pooledStrings.OrderBy(x => x.str))
            {
                if (!str.str.StartsWith("assets/"))
                {
                    continue;
                }

                // Autospawn: assets/bundled/prefabs/autospawn/
                // FX: assets/bundled/prefabs/fx/
                // Content: assets/content/
                // Prefabs: assets/prefabs/
                // Third Party: assets/standard assets/third party/

                //var prefab = str.str.Substring(str.str.LastIndexOf("/", StringComparison.Ordinal) + 1).Replace(".prefab", "");

                export.Log($"| {str.str} |");
            }
        }
Ejemplo n.º 2
0
        void ExportSkins()
        {
            var export   = new ExportLogger("ItemSkinsDocs");
            var items    = ItemManager.itemList;
            var itemList = items.OrderBy(x => x.displayName.english).ToList();

            // Item Skins: http://docs.oxidemod.org/rust/#item-skins
            export.Log("# Item Skins");

            foreach (var item in itemList)
            {
                var displayname = item.displayName.english.Replace("\t", "").Replace("\r", "").Replace("\n", "");
                var skins       = GetSkins(item);
                if (skins.Count == 0)
                {
                    continue;
                }
                export.Log("");
                export.Log($"## {displayname} ({item.shortname})");
                export.Log("| Skin Id      | Skin name                         |");
                export.Log("|--------------|-----------------------------------|");

                foreach (var skin in skins.OrderBy(x => x.Name))
                {
                    var idSpacer   = Fillup(skin.Id.ToString(), 13);
                    var nameSpacer = Fillup(skin.Name, 34);
                    export.Log($"| {skin.Id}{idSpacer}| {skin.Name}{nameSpacer}|");
                }
            }
        }
Ejemplo n.º 3
0
        public static void HandleProperty(FieldInfo field, object obj, JSONObject data, WXHierarchyContext context)
        {
            try {
                innerHandleProperty(field.FieldType, field, obj, data, context);
            } catch (Exception e) {
                if (ExportLogger.LOGGING)
                {
                    ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Exception, "Type: " + field.FieldType + "\nObject: " + obj + "\nJson: " + data, e));
                }

                Debug.LogException(e);
            }
        }
Ejemplo n.º 4
0
        protected override JSONObject ExportResource(ExportPreset preset)
        {
            var realPath = Path.Combine(BridgeExport.TMP_BINDING_CACHE, GetExportPath());

            if (ExportLogger.LOGGING)
            {
                ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.System, "File: " + realPath + " Exists: " + File.Exists(realPath)));
            }

            AddFile(new WXScriptFile(realPath, GetExportPath()));

            dontExportDescriptionJSON = true;
            return(null);
        }
Ejemplo n.º 5
0
        void ExportItems()
        {
            var export   = new ExportLogger("ItemListDocs");
            var items    = ItemManager.itemList;
            var itemList = items.OrderBy(x => x.shortname).ToList();

            // Item List: http://docs.oxidemod.org/rust/#item-list
            export.Log("# Item List");
            export.Log("");
            export.Log("| Item Id       | Item Name                    | Item Shortname           |");
            export.Log("|---------------|------------------------------|--------------------------|");

            foreach (var item in itemList)
            {
                var displayname = item.displayName.english.Replace("\t", "").Replace("\r", "").Replace("\n", "");
                var idSpacer    = Fillup(item.itemid.ToString(), 14);
                var nameSpacer  = Fillup(displayname, 29);
                var snameSpacer = Fillup(item.shortname, 25);

                export.Log($"| {item.itemid}{idSpacer}| {displayname}{nameSpacer}| {item.shortname}{snameSpacer}|");
            }
        }
Ejemplo n.º 6
0
        private static void RegisterUnityProperties()
        {
            AddPropertyHandler(typeof(Vector2), (obj, context) => {
                Vector2 v = (Vector2)obj;
                if (v == null)
                {
                    return(null);
                }

                JSONObject vec2 = new JSONObject(JSONObject.Type.ARRAY);
                vec2.Add(v.x);
                vec2.Add(v.y);

                return(vec2);
            });

            AddPropertyHandler(typeof(Vector3), (obj, context) => {
                Vector3 v = (Vector3)obj;
                if (v == null)
                {
                    return(null);
                }

                JSONObject vec3 = new JSONObject(JSONObject.Type.ARRAY);
                vec3.Add(v.x);
                vec3.Add(v.y);
                vec3.Add(v.z);

                return(vec3);
            });

            AddPropertyHandler(typeof(Vector4), (obj, context) => {
                Vector4 v = (Vector4)obj;
                if (v == null)
                {
                    return(null);
                }

                JSONObject vec4 = new JSONObject(JSONObject.Type.ARRAY);
                vec4.Add(v.x);
                vec4.Add(v.y);
                vec4.Add(v.z);
                vec4.Add(v.w);

                return(vec4);
            });

            AddPropertyHandler(typeof(Quaternion), (obj, context) => {
                Quaternion v = (Quaternion)obj;
                if (v == null)
                {
                    return(null);
                }

                JSONObject array4 = new JSONObject(JSONObject.Type.ARRAY);
                array4.Add(v.x);
                array4.Add(v.y);
                array4.Add(v.z);
                array4.Add(v.w);

                return(array4);
            });


            AddPropertyHandler(typeof(Matrix4x4), (obj, context) => {
                Matrix4x4 v = (Matrix4x4)obj;
                if (v == null)
                {
                    return(null);
                }

                JSONObject array16 = new JSONObject(JSONObject.Type.ARRAY);
                array16.Add(v.m00);
                array16.Add(v.m01);
                array16.Add(v.m02);
                array16.Add(v.m03);
                array16.Add(v.m10);
                array16.Add(v.m11);
                array16.Add(v.m12);
                array16.Add(v.m13);
                array16.Add(v.m20);
                array16.Add(v.m21);
                array16.Add(v.m22);
                array16.Add(v.m23);
                array16.Add(v.m30);
                array16.Add(v.m31);
                array16.Add(v.m32);
                array16.Add(v.m33);

                return(array16);
            });

            AddPropertyHandler(typeof(Color), (obj, context) => {
                Color c = (Color)obj;
                if (c == null)
                {
                    return(null);
                }

                JSONObject vec4 = new JSONObject(JSONObject.Type.ARRAY);
                vec4.Add((int)(c.r * 255));
                vec4.Add((int)(c.g * 255));
                vec4.Add((int)(c.b * 255));
                vec4.Add((int)(c.a * 255));

                return(vec4);
            });

            AddPropertyHandler(typeof(TextAsset), (obj, context) => {
                TextAsset t = (TextAsset)obj;
                if (!t)
                {
                    return(null);
                }

                string path = AssetDatabase.GetAssetPath(t);
                // string copyToPath = Path.Combine(WXResourceStore.storagePath, path);
                // Debug.Log("WXResourceStore.storagePath:" + WXResourceStore.storagePath);
                // Debug.Log("path:" + path);
                // Debug.Log("copyToPath:" + copyToPath);

                // Regex regex = new Regex(".txt$");
                // copyToPath = regex.Replace(copyToPath, ".json");

                // if (!Directory.Exists(WXResourceStore.storagePath + "Assets/")) {
                //     Directory.CreateDirectory(WXResourceStore.storagePath + "Assets/");
                // }

                // FileStream fs = new FileStream(copyToPath, FileMode.Create, FileAccess.Write);

                // wxFileUtil.WriteData(fs, JsonConvert.SerializeObject(new { text = t.text }));
                // fs.Close();

                JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
                JSONObject data = new JSONObject(JSONObject.Type.OBJECT);
                // Debug.Log("JsonConvert.SerializeObject(t.text): " + JsonConvert.SerializeObject(t.text));
                string text = JsonConvert.SerializeObject(t.text);
                // 去掉首尾双引号
                text = text.Remove(0, 1);
                text = text.Substring(0, text.Length - 1);
                data.AddField("text", text);
                data.AddField("path", path);
                json.AddField("type", "UnityEngine.TextAsset".EscapeNamespaceSimple());
                json.AddField("value", data);
                return(json);
            });

            AddPropertyHandler(typeof(Material), (obj, context) => {
                Material material = (Material)obj;
                if (material == null)
                {
                    return(null);
                }

                WXMaterial materialConverter = new WXMaterial(material, null);
                string materialPath          = materialConverter.Export(context.preset);
                context.AddResource(materialPath);

                JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
                JSONObject data = new JSONObject(JSONObject.Type.OBJECT);
                json.AddField("type", "UnityEngine.Material".EscapeNamespaceSimple());
                json.AddField("value", data);
                data.AddField("path", materialPath);

                return(json);
            });

            AddPropertyHandler(typeof(List <>), (obj, context) => {
                IEnumerable list = (IEnumerable)obj;
                if (list == null)
                {
                    return(null);
                }

                JSONObject result = new JSONObject(JSONObject.Type.ARRAY);

                var enumerator = ((IEnumerable)list).GetEnumerator();
                while (enumerator.MoveNext())
                {
                    object itemObj = enumerator.Current;
                    if (itemObj == null)
                    {
                        continue;
                    }
                    if (itemObj.GetType() == typeof(List <>))
                    {
                        throw new Exception("List不支持嵌套");
                    }
                    else
                    {
                        Type type = itemObj.GetType();

                        if (type.IsSubclassOf(typeof(UnityEngine.Component)) || type == typeof(UnityEngine.GameObject))
                        {
                            var o         = (UnityEngine.Object)itemObj;
                            GameObject go = null;
                            if (o == null)
                            {
                                continue;
                            }
                            if (type.IsSubclassOf(typeof(UnityEngine.Component)))
                            {
                                go = ((Component)o).gameObject;
                            }
                            else if (type == typeof(UnityEngine.GameObject))
                            {
                                go = (GameObject)o;
                            }

                            var path = AssetDatabase.GetAssetPath(o);

                            // Prefab?
                            if (go.transform.IsChildOf(context.Root.transform) || path == "")
                            {
                                if (!propertiesHandlerDictionary.ContainsKey(type) && !type.IsArray && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(List <>)) && type.IsSerializable)
                                {
                                    var sobj = new JSONObject(JSONObject.Type.OBJECT);
                                    result.Add(sobj);
                                    SerializableHandler(type, sobj, itemObj, context);
                                }
                                else if (type != typeof(System.Object) && propertiesHandlerDictionary.ContainsKey(type))
                                {
                                    var res = propertiesHandlerDictionary[type](itemObj, context);
                                    result.Add(res);
                                }
                                continue;
                            }

                            if (!WXMonoBehaviourExportHelper.exportedResourcesSet.Contains(go))
                            {
                                WXMonoBehaviourExportHelper.exportedResourcesSet.Add(go);
                                WXPrefab converter = new WXPrefab(go, path);
                                string prefabPath  = converter.Export(ExportPreset.GetExportPreset("prefab"));
                                context.AddResource(prefabPath);
                            }

                            var prefabInfo = new JSONObject(JSONObject.Type.OBJECT);
                            prefabInfo.AddField("type", type.FullName.EscapeNamespaceSimple());
                            prefabInfo.AddField("path", path);

                            var innerData = new JSONObject(JSONObject.Type.OBJECT);
                            innerData.AddField("type", "UnityPrefabWrapper");
                            innerData.AddField("value", prefabInfo);
                            // data.AddField(field.Name, innerData);
                            result.Add(innerData);
                        }
                        else if (!propertiesHandlerDictionary.ContainsKey(type) && !type.IsArray && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(List <>)) && type.IsSerializable)
                        {
                            var sobj = new JSONObject(JSONObject.Type.OBJECT);
                            result.Add(sobj);
                            SerializableHandler(type, sobj, itemObj, context);
                        }
                        else if (type != typeof(System.Object) && propertiesHandlerDictionary.ContainsKey(type))
                        {
                            var res = propertiesHandlerDictionary[type](itemObj, context);
                            result.Add(res);
                        }
                    }
                }
                return(result);
            });

            // disgusting code logic :(
            // refactor should be needed
            AddPropertyHandler(typeof(MonoBehaviour), (obj, context) => {
                var m = (MonoBehaviour)obj;
                if (!m)
                {
                    return(null);
                }

                if (ExportLogger.LOGGING)
                {
                    ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Property, "Object: " + obj + "\nType: " + obj.GetType()));
                }

                var go = m.gameObject;
                JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT);

                var path = AssetDatabase.GetAssetPath(go);

                // Prefab?
                if (go.transform.IsChildOf(context.Root.transform) || path == "")
                {
                    var typeName        = m.GetType().FullName;
                    var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(typeName);
                    innerData.AddField("type", escapedTypeName);
                    innerData.AddField("value", context.AddComponentInProperty(new WXEngineMonoBehaviour(m), (Component)obj));
                    return(innerData);
                }

                if (!WXMonoBehaviourExportHelper.exportedResourcesSet.Contains(go))
                {
                    WXMonoBehaviourExportHelper.exportedResourcesSet.Add(go);
                    WXPrefab converter = new WXPrefab(go, path);
                    string prefabPath  = converter.Export(ExportPreset.GetExportPreset("prefab"));
                    context.AddResource(prefabPath);
                }
                var prefabInfo = new JSONObject(JSONObject.Type.OBJECT);

                {
                    var typeName        = m ? m.GetType().FullName : "UnityEngine.GameObject";
                    var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(typeName);
                    prefabInfo.AddField("type", escapedTypeName);
                }
                prefabInfo.AddField("path", path);

                innerData.AddField("type", "UnityPrefabWrapper");
                innerData.AddField("value", prefabInfo);
                return(innerData);
            });

            AddPropertyHandler(typeof(Component), (obj, context) => {
                Component c = (Component)obj;
                if (!c)
                {
                    return(null);
                }

                if (ExportLogger.LOGGING)
                {
                    ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Property, "Object: " + obj + "\nType: " + obj.GetType()));
                }

                JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT);


                var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(c.GetType().FullName);
                innerData.AddField("type", escapedTypeName);

                if (c is Transform)
                {
                    innerData = GetPrefabOnSerializedField(context, c.gameObject, innerData, false);
                }

                // 下面是adaptor独有的类才需要单独写
                // Physics
                //else if (c is BoxCollider) {
                //    innerData.AddField("value", context.AddComponent(new WXBoxCollider((BoxCollider)c), (BoxCollider)c));
                //} else if (c is SphereCollider) {
                //    innerData.AddField("value", context.AddComponent(new WXSphereCollider((SphereCollider)c), (SphereCollider)c));
                //} else if (c is CapsuleCollider) {
                //    innerData.AddField("value", context.AddComponent(new WXCapsuleCollider((CapsuleCollider)c), (CapsuleCollider)c));
                //} else if (c is MeshCollider) {
                //    innerData.AddField("value", context.AddComponent(new WXMeshCollider((MeshCollider)c), (MeshCollider)c));
                //} else if (c is Rigidbody) {
                //    innerData.AddField("value", context.AddComponent(new WXRigidbody((Rigidbody)c), (Rigidbody)c));
                //}

                else if (c is AudioSource)
                {
                    innerData.AddField("value", context.AddComponentInProperty(new WXEngineAudioSource((AudioSource)c), (AudioSource)c));
                }

                // 有ref的类
                else
                {
                    // if (context.componentDictionary.ContainsKey(c)) {
                    innerData.AddField("value", context.AddComponentInProperty(new WXUnityComponent(c), c));
                    // }
                }
                return(innerData);
            });

            AddPropertyHandler(typeof(GameObject), (obj, context) => {
                var go = (GameObject)obj;
                if (!go)
                {
                    return(null);
                }

                if (ExportLogger.LOGGING)
                {
                    ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Property, "Object: " + obj + "\nType: " + obj.GetType()));
                }

                JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT);

                return(GetPrefabOnSerializedField(context, go, innerData));
            });
        }
Ejemplo n.º 7
0
        // inner recursion method, thus each if-statement must have @return in the end of the following block.
        private static void innerHandleProperty(Type type, FieldInfo field, object obj, JSONObject data, WXHierarchyContext context)
        {
            if (ExportLogger.LOGGING)
            {
                ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Inner, "Type: " + type + "\nObject: " + obj + "\nJson: " + data));
            }

            if (obj == null ||
                type == null ||
                type == typeof(System.Object) ||
                field.IsDefined(typeof(NonSerializedAttribute)) ||
                field.IsDefined(typeof(HideInInspector)))
            {
                return;
            }

            if (WXBridge.isNGUIPreset)
            {
                Type wxnguiType = WXMonoBehaviourExportHelper.GetWXNGUIComponentType(type);
                if (wxnguiType != null)
                {
                    var nativeComp = (Component)field.GetValue(obj);

                    if (nativeComp != null)
                    {
                        var go = nativeComp.gameObject;
                        Debug.Log(nativeComp);
                        var entity = context.MakeEntity(go);

                        // 特殊处理
                        if (wxnguiType.FullName == "WXEngine.WXTransform2DComponent")
                        {
                            data.AddField(field.Name, context.AddComponentInProperty(
                                              (WXComponent)Activator.CreateInstance(wxnguiType, (object)nativeComp.transform),
                                              nativeComp.transform
                                              ));
                        }
                        else
                        {
                            data.AddField(field.Name, context.AddComponentInProperty(
                                              (WXComponent)Activator.CreateInstance(wxnguiType, nativeComp, go, entity),
                                              nativeComp
                                              ));
                        }
                    }
                }
            }

            if (WXBridge.isUGUIPreset)
            {
                Type wxuguiType = WXMonoBehaviourExportHelper.GetWXUGUIComponentType(type);
                if (wxuguiType != null)
                {
                    var nativeComp = (Component)field.GetValue(obj);

                    if (nativeComp != null)
                    {
                        var go = nativeComp.gameObject;
                        Debug.Log(nativeComp);
                        var entity = context.MakeEntity(go);

                        // 特殊处理
                        if (wxuguiType.FullName == "WeChat.WXUGUITransform2DComponent")
                        {
                            data.AddField(field.Name, context.AddComponent(
                                              (WXComponent)Activator.CreateInstance(wxuguiType, (object)nativeComp.transform),
                                              nativeComp.transform
                                              ));
                        }
                        else
                        {
                            data.AddField(field.Name, context.AddComponent(
                                              (WXComponent)Activator.CreateInstance(wxuguiType, nativeComp, go, entity),
                                              nativeComp
                                              ));
                        }
                    }
                }
            }

            if (WXBridge.isUGUIPreset)
            {
                Type wxuguiType = WXMonoBehaviourExportHelper.GetWXUGUIComponentType(type);
                if (wxuguiType != null)
                {
                    var nativeComp = (Component)field.GetValue(obj);

                    if (nativeComp != null)
                    {
                        var go = nativeComp.gameObject;
                        Debug.Log(nativeComp);
                        var entity = context.MakeEntity(go);

                        // 特殊处理
                        if (wxuguiType.FullName == "WeChat.WXUGUITransform2DComponent")
                        {
                            data.AddField(field.Name, context.AddComponent(
                                              (WXComponent)Activator.CreateInstance(wxuguiType, (object)nativeComp.transform),
                                              nativeComp.transform
                                              ));
                        }
                        else
                        {
                            data.AddField(field.Name, context.AddComponent(
                                              (WXComponent)Activator.CreateInstance(wxuguiType, nativeComp, go, entity),
                                              nativeComp
                                              ));
                        }
                    }
                }
            }

            if (WXMonoBehaviourExportHelper.IsInBlackList(type))
            {
                return;
            }
            if (propertiesHandlerDictionary.ContainsKey(type))
            {
                var res = InvokePropertyHandler(type, field.GetValue(obj), context);
                if (res)
                {
                    data.AddField(field.Name, res);
                }
                return;
            }
            else if (type.IsArray || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>)))
            {
                var res = InvokePropertyHandler(typeof(List <>), field.GetValue(obj), context);
                if (res)
                {
                    data.AddField(field.Name, res);
                }
                return;
            }
            else if (type.IsEnum)
            {
                var res = JSONObject.Create((int)field.GetValue(obj));
                if (res)
                {
                    data.AddField(field.Name, res);
                }
                return;
            }
            else if (type.IsSubclassOf(typeof(UnityEngine.Component)) || type == typeof(UnityEngine.GameObject))
            {
                // Prefab or not
                var        o  = (UnityEngine.Object)field.GetValue(obj);
                GameObject go = null;

                if (o == null)
                {
                    return;
                }
                if (type.IsSubclassOf(typeof(UnityEngine.Component)))
                {
                    go = ((Component)o).gameObject;
                }
                else if (type == typeof(UnityEngine.GameObject))
                {
                    go = (GameObject)o;
                }
                var path = AssetDatabase.GetAssetPath(o);

                if (go.transform.IsChildOf(context.Root.transform) || path == "")
                {
                    if (type.IsSerializable)
                    {
                        var _innerData = new JSONObject(JSONObject.Type.OBJECT);
                        data.AddField(field.Name, _innerData);
                        SerializableHandler(type, _innerData, field.GetValue(obj), context);
                        return;
                    }
                    innerHandleProperty(type.BaseType, field, obj, data, context);
                    return;
                }

                if (!WXMonoBehaviourExportHelper.exportedResourcesSet.Contains(go))
                {
                    WXMonoBehaviourExportHelper.exportedResourcesSet.Add(go);
                    WXPrefab converter  = new WXPrefab(go, path);
                    string   prefabPath = converter.Export(ExportPreset.GetExportPreset("prefab"));
                    context.AddResource(prefabPath);
                }

                var prefabInfo = new JSONObject(JSONObject.Type.OBJECT);
                prefabInfo.AddField("type", type.FullName.EscapeNamespaceSimple());
                prefabInfo.AddField("path", path);

                var innerData = new JSONObject(JSONObject.Type.OBJECT);
                innerData.AddField("type", "UnityPrefabWrapper");
                innerData.AddField("value", prefabInfo);
                data.AddField(field.Name, innerData);
                return;
            }
            else if (type.IsSerializable)
            {
                // data of serializable object as a new JSONObject to be added
                var innerData = new JSONObject(JSONObject.Type.OBJECT);
                data.AddField(field.Name, innerData);
                SerializableHandler(type, innerData, field.GetValue(obj), context);
                return;
            }
            innerHandleProperty(type.BaseType, field, obj, data, context);
        }