private void DrawHeader() { headerArea.Begin(); GUILayout.BeginHorizontal(); GUILayout.Label("Project workspace:", GUILayout.ExpandWidth(false)); var newProjectWorkspacePath = GUILayout.TextField(projectWorkspacePath, GUILayout.ExpandWidth(true)); if (!newProjectWorkspacePath.Equals(projectWorkspacePath)) { projectWorkspacePath = newProjectWorkspacePath.Trim(); UnityPythonObject.Instance.Config.ScriptWorkspacePath = projectWorkspacePath; UnityPythonObject.Instance.SaveConfig(); } if (GUILayout.Button("Reload", GUILayout.Width(100))) { ReloadProjectWorkspace(); } GUILayout.EndHorizontal(); projectFilesScrollPosition = GUILayout.BeginScrollView(projectFilesScrollPosition); GUILayout.BeginHorizontal(); foreach (var file in projectFiles) { string caption = Path.GetFileName(file.Path); if (GUILayout.Button(currentFile == file ? "= " + caption + " =" : caption, GUILayout.ExpandWidth(false))) { AbortFileActions(); currentFile = file; } } if (GUILayout.Button("+ New File", GUILayout.ExpandWidth(false))) { for (int i = 0; ; i++) { string path = Path.Combine(projectWorkspacePath, "NewFile" + (i == 0 ? "" : i.ToString()) + ".py"); if (!File.Exists(path)) { var newFile = new ScriptEditorFile("print(\"Hello world!\")", path); projectFiles.Add(newFile); SaveProjectFile(newFile); currentFile = newFile; break; } } } GUILayout.EndHorizontal(); GUILayout.EndScrollView(); headerArea.End(); }
public void ReloadProjectWorkspace() { AbortFileActions(); bool isDefaultPath = false; string configPath = UnityPythonObject.Instance.Config.ScriptWorkspacePath; projectWorkspacePath = configPath; string defaultPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SkylinesPython"); if (configPath == null || configPath == "") { projectWorkspacePath = defaultPath; } isDefaultPath = Util.NormalizePath(projectWorkspacePath) == Util.NormalizePath(defaultPath); if (projectWorkspacePath.Length == 0) { contextMsg = "Invalid project workspace path"; return; } var exampleFileExists = false; projectFiles.Clear(); System.IO.Directory.CreateDirectory(projectWorkspacePath); try { foreach (var file in FileUtil.ListFilesInDirectory(projectWorkspacePath)) { if (Path.GetExtension(file) == ".py") { var fileContent = new ScriptEditorFile(File.ReadAllText(file), file); projectFiles.Add(fileContent); if (Path.GetFileName(file) == ExampleScriptFileName) { exampleFileExists = true; currentFile = fileContent; } } } if (!exampleFileExists) { var exampleFile = new ScriptEditorFile(ScriptEditorFile.DefaultSource, Path.Combine(projectWorkspacePath, ExampleScriptFileName)); projectFiles.Add(exampleFile); SaveProjectFile(exampleFile); if (currentFile == null) { currentFile = exampleFile; } } if (isDefaultPath) { try { string archivePath = Path.Combine(ModPath.Instsance.AssemblyPath, "ExamplePythonScripts.zip"); string destPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.Combine("SkylinesPython", "Examples")); using (var unzip = new Unzip(archivePath)) { unzip.ExtractToDirectory(destPath); } } catch { } } } catch (Exception ex) { contextMsg = ex.Message; return; } contextMsg = string.Empty; }
private static void SaveProjectFile(ScriptEditorFile file) => File.WriteAllText(file.Path, file.Source);