/* * Precompute element names. */ private void PrecomputeElementNames(Board board) { potentialRoots = BoardUtils.ComputeRoots(board); elementNameList = new string[potentialRoots.Count]; for (int i = 0; i < potentialRoots.Count; i++) { elementNameList[i] = potentialRoots[i].GetActionLabel(); } }
/* * Read project */ public static bool ReadProject(Project project, string projectFolder) { // Asset paths are relative to project folder string unityPrjFolder = Application.dataPath.Replace("Assets", ""); projectFolder = projectFolder.Replace(unityPrjFolder, ""); // Ensure the target folder is empty, and exists. if (!IsProjectFolderEmpty()) { ClearProjectFolder(); CreateProjectFolders(); } try { string fullPrjPath = projectFolder + "/" + projectFileName; TextAsset projectAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(fullPrjPath); if (projectAsset == null) { throw new Exception("No project asset found at " + fullPrjPath + "."); } string projectContents = Encoding.UTF8.GetString(projectAsset.bytes); JSONNode root = JSONNode.Parse(projectContents); project.project = root["name"]; // Read attributes EditorUtility.DisplayProgressBar("Arcweave", "Creating attributes...", 0.0f); ReadAttributes(project, root["attributes"].AsObject); // Read components EditorUtility.DisplayProgressBar("Arcweave", "Creating components...", 15.0f); ReadComponents(project, root["components"].AsObject, projectFolder); // Read elements EditorUtility.DisplayProgressBar("Arcweave", "Creating elements...", 30.0f); ReadElements(project, root["elements"].AsObject); // Read connections EditorUtility.DisplayProgressBar("Arcweave", "Creating connections...", 45.0f); ReadConnections(project, root["connections"].AsObject); // Read notes EditorUtility.DisplayProgressBar("Arcweave", "Creating notes...", 60.0f); ReadNotes(project, root["notes"].AsObject); // Read boards EditorUtility.DisplayProgressBar("Arcweave", "Creating boards...", 75.0f); BoardUtils.ReadBoards(project, root["boards"].AsObject); // Re-do the entity linking project.Relink(); // Resolve references inside elements and connections // Must happen after reading everything, so Arcweave has the referenced elements instantiated. EditorUtility.DisplayProgressBar("Arcweave", "Preprocessing HTML...", 90.0f); for (int i = 0; i < project.elements.Length; i++) { project.elements[i].ParseHTML(project); } for (int i = 0; i < project.connections.Length; i++) { project.connections[i].ParseHTML(project); } // Set default roots for boards for (int i = 0; i < project.boards.Length; i++) { BoardUtils.SetDefaultRoot(project.boards[i]); EditorUtility.SetDirty(project.boards[i]); } } catch (Exception e) { Debug.LogError("[Arcweave] Cannot load project: " + e.Message + "\n" + e.StackTrace); EditorUtility.ClearProgressBar(); return(false); } EditorUtility.ClearProgressBar(); return(true); }
/* * Read project */ public static bool ReadProject(Project project, string projectFolder) { // Force an Asset Refresh before starting this, in case Unity // doesn't have auto-refresh on. AssetDatabase.Refresh(); // Asset paths are relative to project folder string unityPrjFolder = Application.dataPath.Replace("Assets", ""); projectFolder = projectFolder.Replace(unityPrjFolder, ""); // Ensure the target folder is empty, and exists. if (!IsProjectFolderEmpty()) { ClearProjectFolder(); CreateProjectFolders(); } try { string fullPrjPath = projectFolder + "/" + projectFileName; TextAsset projectAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(fullPrjPath); if (projectAsset == null) { throw new Exception("No project asset found at " + fullPrjPath + "."); } else { AssetImportHelper.SetupSprites(projectFolder); } string projectContents = Encoding.UTF8.GetString(projectAsset.bytes); JSONNode root = JSONNode.Parse(projectContents); project.project = root["name"]; // Read assets EditorUtility.DisplayProgressBar("Arcweave", "Loading assets...", 0.0f); ReadAssets(project, root["assets"].AsObject, projectFolder); // Read attributes EditorUtility.DisplayProgressBar("Arcweave", "Creating attributes...", 10.0f); ReadAttributes(project, root["attributes"].AsObject); // Read components EditorUtility.DisplayProgressBar("Arcweave", "Creating components...", 20.0f); ReadComponents(project, root["components"].AsObject, projectFolder); // Read elements EditorUtility.DisplayProgressBar("Arcweave", "Creating elements...", 30.0f); ReadElements(project, root["elements"].AsObject, projectFolder); // Read jumpers EditorUtility.DisplayProgressBar("Arcweave", "Creating jumpers...", 37.5f); ReadJumpers(project, root["jumpers"].AsObject); // Read connections EditorUtility.DisplayProgressBar("Arcweave", "Creating connections...", 45.0f); ReadConnections(project, root["connections"].AsObject); // Read notes EditorUtility.DisplayProgressBar("Arcweave", "Creating notes...", 60.0f); ReadNotes(project, root["notes"].AsObject); // Read boards EditorUtility.DisplayProgressBar("Arcweave", "Creating boards...", 75.0f); BoardUtils.ReadBoards(project, root["boards"].AsObject); // Re-do the entity linking project.Relink(); // Resolve references inside elements and connections // Must happen after reading everything, so Arcweave has the referenced elements instantiated. EditorUtility.DisplayProgressBar("Arcweave", "Preprocessing HTML...", 90.0f); for (int i = 0; i < project.elements.Length; i++) { project.elements[i].ParseHTML(project); } for (int i = 0; i < project.connections.Length; i++) { project.connections[i].ParseHTML(project); } // Set default roots for boards for (int i = 0; i < project.boards.Length; i++) { BoardUtils.SetDefaultRoot(project.boards[i]); EditorUtility.SetDirty(project.boards[i]); } // Handle root set in the JSON string startingElementID = root["startingElement"]; if (!string.IsNullOrEmpty(startingElementID)) { Element startingElement = project.GetElement(startingElementID); if (startingElement != null) { Board rootBoard = project.GetBoardForElement(startingElementID); if (rootBoard != null) { project.startingBoardIdx = project.GetBoardIndex(rootBoard); rootBoard.rootElementId = startingElementID; } } else { Debug.LogWarning("[Arcweave] Cannot find starting element of id: " + startingElementID + ". Fallback to computing roots."); } } } catch (Exception e) { Debug.LogError("[Arcweave] Cannot load project: " + e.Message + "\n" + e.StackTrace); EditorUtility.ClearProgressBar(); return(false); } Debug.Log("[Arcweave] Import completed successfully."); EditorUtility.ClearProgressBar(); return(true); }