Exemplo n.º 1
0
 private static void ConvertTileMaps()
 {
     foreach (string extension in new[] { TileMapUtil.MAP_EXTENSION, TileMapUtil.WORLD_EXTENSION })
     {
         string[] files = Directory.GetFiles(Application.dataPath, "*." + extension, SearchOption.AllDirectories);
         TileMapUtil.ConvertTileMaps(files.ToList(), TravrsalSettingsManager.Get("tiledPath", SDKUtil.TILED_PATH_DEFAULT));
     }
     AssetDatabase.Refresh();
 }
Exemplo n.º 2
0
        private IEnumerator CreateDocumentation()
        {
            documentationInProgress = true;

            string converterPath = TravrsalSettingsManager.Get("tiledPath", SDKUtil.TILED_PATH_DEFAULT);

            if (!string.IsNullOrEmpty(converterPath))
            {
                converterPath = Path.GetDirectoryName(converterPath) + "/tmxrasterizer.exe";
            }

            foreach (string dir in GetWorldPaths())
            {
                string worldName = Path.GetFileName(dir);
                string docuPath  = GetDocuPath(worldName);
                try
                {
                    if (Directory.Exists(docuPath))
                    {
                        Directory.Delete(docuPath, true);
                    }
                    Directory.CreateDirectory(docuPath);
                }
                catch
                {
                    EditorUtility.DisplayDialog("Error", $"Could not access documentation directory ({docuPath}). Most likely it is open somewhere in an explorer.", "OK");
                    continue;
                }

                string root = GetWorldsRoot() + $"/{worldName}/";

                // fill HTML template
                string id   = AssetDatabase.FindAssets("WorldDocu")[0];
                string path = AssetDatabase.GUIDToAssetPath(id);
                DirectoryUtil.Copy(Application.dataPath + "/../" + path, docuPath);
                AssetDatabase.Refresh();

                string html = File.ReadAllText(docuPath + "index.html");
                html = html.Replace("{WorldName}", worldName);
                html = html.Replace("{WorldKey}", worldName);
                html = html.Replace("{AppVersion}", Application.version); // FIXME: points to wrong version
                html = html.Replace("{Date}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));

                foreach (string folder in new[] { "Data", "Images", "Logic", "Materials", "Pieces", "Sceneries", "Audio/Effects", "Audio/Music" })
                {
                    HashSet <string> doneAlready = new HashSet <string>();
                    string[]         assets      = new string[0];
                    string           objects     = "";
                    int    objCount     = 0;
                    string variableName = Path.GetFileName(folder);
                    switch (folder)
                    {
                    case "Data":
                        variableName = "Zones";
                        break;
                    }
                    if (Directory.Exists($"{root}{folder}"))
                    {
                        Directory.CreateDirectory(docuPath + folder);

                        assets = AssetDatabase.FindAssets("*", new[] { $"{root}{folder}" });
                        foreach (string asset in assets)
                        {
                            string assetPath = AssetDatabase.GUIDToAssetPath(asset);
                            if (doneAlready.Contains(assetPath))
                            {
                                continue;
                            }
                            doneAlready.Add(assetPath);
                            if (assetPath.ToLower().EndsWith(".md"))
                            {
                                continue;
                            }

                            string imageLink       = "NoPreview.png";
                            bool   withExtension   = true;
                            bool   generatePreview = false;
                            Type   type            = null;

                            switch (folder)
                            {
                            case "Data":
                                if (!assetPath.ToLower().EndsWith(".tmx"))
                                {
                                    continue;
                                }
                                imageLink = folder + "/" + Path.GetFileName(assetPath) + ".png";
                                TileMapUtil.TileMapToImage(assetPath, docuPath + imageLink, converterPath);
                                withExtension = false;
                                break;

                            case "Images":
                                generatePreview = true;
                                type            = typeof(Texture2D);
                                break;

                            case "Materials":
                                if (!assetPath.ToLower().EndsWith(".mat"))
                                {
                                    continue;
                                }
                                withExtension   = false;
                                generatePreview = true;
                                type            = typeof(Material);
                                break;

                            case "Pieces":
                                if (!assetPath.ToLower().EndsWith(".prefab"))
                                {
                                    continue;
                                }
                                withExtension   = false;
                                generatePreview = true;
                                break;

                            case "Sceneries":
                                if (!assetPath.ToLower().EndsWith(".prefab"))
                                {
                                    continue;
                                }
                                withExtension   = false;
                                generatePreview = true;
                                break;

                            case "Audio/Music":
                            case "Audio/Effects":
                                generatePreview = true;
                                type            = typeof(AudioClip);
                                break;
                            }
                            objCount++;

                            if (generatePreview)
                            {
                                GameObject         prefab = null;
                                UnityEngine.Object obj;
                                if (type != null)
                                {
                                    obj = AssetDatabase.LoadAssetAtPath(assetPath, type);
                                }
                                else
                                {
                                    prefab = PrefabUtility.LoadPrefabContents(assetPath);
                                    obj    = prefab;
                                }
                                if (obj != null)
                                {
                                    Texture2D icon = AssetPreview.GetAssetPreview(obj);

                                    while (icon == null && AssetPreview.IsLoadingAssetPreview(obj.GetInstanceID()))
                                    {
                                        Repaint();
                                        yield return(new EditorWaitForSeconds(0.05f));

                                        icon = AssetPreview.GetAssetPreview(obj);
                                    }
                                    if (prefab != null)
                                    {
                                        PrefabUtility.UnloadPrefabContents(prefab);
                                    }

                                    // still will not return something for all assets
                                    if (icon == null || !icon.isReadable)
                                    {
                                        continue;
                                    }

                                    byte[] bytes = icon.EncodeToPNG();
                                    if (bytes == null)
                                    {
                                        continue;
                                    }

                                    File.WriteAllBytes(docuPath + folder + "/" + Path.GetFileName(assetPath) + ".png", bytes);
                                }
                                else
                                {
                                    // most likely a directory
                                    continue;
                                }
                            }

                            objects += "<div class=\"media mb-1\">";
                            string imageName = folder + "/" + Path.GetFileName(assetPath) + ".png";
                            string accessKey = assetPath.Substring((root + folder).Length + 1);
                            if (!withExtension)
                            {
                                accessKey = accessKey.Substring(0, accessKey.LastIndexOf('.'));
                            }

                            if (generatePreview)
                            {
                                imageLink = File.Exists(docuPath + imageName) ? imageName : "MissingPreview.png";
                            }
                            objects += "<img src=\"" + imageLink + "\" class=\"mr-3\" width=\"128\">";
                            objects += "<div class=\"media-body\">/" + worldName + "/" + accessKey;
                            objects += "</div></div>";
                        }
                        objects += "</table>";
                    }
                    html = html.Replace($"{{{variableName}List}}", objects);
                    html = html.Replace($"{{{variableName}Count}}", objCount.ToString());
                }

                // copy data contents and world descriptor
                DirectoryUtil.Copy(root + "/Data", docuPath + "/Data");
                FileUtil.CopyFileOrDirectory(root + "/World.json", docuPath + "/World.json");

                // remove all meta files
                foreach (string fileName in Directory.EnumerateFiles(docuPath, "*.meta", SearchOption.AllDirectories))
                {
                    FileUtil.DeleteFileOrDirectory(fileName);
                }

                File.WriteAllText(docuPath + "index.html", html);

                // create zip
                if (File.Exists(GetDocuArchivePath(worldName)))
                {
                    File.Delete(GetDocuArchivePath(worldName));
                }
                ZipFile.CreateFromDirectory(docuPath, GetDocuArchivePath(worldName), CompressionLevel.Fastest, false);
            }

            documentationInProgress = false;
        }