123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- /*
- © Siemens AG, 2019
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- <http://www.apache.org/licenses/LICENSE-2.0>.
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- namespace RosSharp.RosBridgeClient.MessageGeneration
- {
- public abstract class PackageAutoGenEditorWindow : EditorWindow
- {
- [SerializeField]
- private static string lastPackageDirectory = string.Empty;
- [SerializeField]
- private static string lastOutputDirectory = string.Empty;
- private string inPkgPath = "";
- private string rosPackageName = "";
- private string outPkgPath = Path.Combine(System.Environment.CurrentDirectory, "Assets", "RosSharpMessages");
- protected abstract string GenerationType { get; }
- protected abstract string FileExtension { get; }
- protected virtual void OnGUI()
- {
- GUILayout.Label("Package " + GenerationType + " auto generation", EditorStyles.boldLabel);
- EditorGUILayout.BeginHorizontal();
- inPkgPath = EditorGUILayout.TextField("Input Package Path", inPkgPath);
- if (GUILayout.Button("Browse Package...", GUILayout.Width(150)))
- {
- inPkgPath = EditorUtility.OpenFolderPanel("Select Package...", lastPackageDirectory, "");
- if (!inPkgPath.Equals(""))
- {
- rosPackageName = inPkgPath.Split('/').Last();
- }
- }
- EditorGUILayout.EndHorizontal();
- rosPackageName = EditorGUILayout.TextField("ROS Package Name:", rosPackageName);
- EditorGUILayout.BeginHorizontal();
- outPkgPath = EditorGUILayout.TextField("Output Location", outPkgPath);
- if (GUILayout.Button("Select Folder...", GUILayout.Width(150)))
- {
- outPkgPath = EditorUtility.OpenFolderPanel("Select Folder...", lastOutputDirectory, "");
- }
- EditorGUILayout.EndHorizontal();
- if (GUILayout.Button("GENERATE!"))
- {
- if (inPkgPath.Equals(""))
- {
- EditorUtility.DisplayDialog(
- title: "Error",
- message: "Empty input package path!\nPlease specify input package",
- ok: "Bricks without straw");
- }
- else
- {
- lastPackageDirectory = inPkgPath;
- lastOutputDirectory = outPkgPath;
- try
- {
- string[] files = Directory.GetFiles(Path.Combine(inPkgPath, FileExtension), "*." + FileExtension);
- if (files.Length == 0)
- {
- EditorUtility.DisplayDialog(
- title: "No action files found!",
- message: "No action files found!",
- ok: "Bricks without straw");
- Reset();
- }
- else
- {
- // Keep a list of warnings
- List<string> warnings = new List<string>();
- for (int i = 0; i < files.Length; i++)
- {
- string file = files[i];
- EditorUtility.DisplayProgressBar(
- "Working...(" + (i + 1) + "/" + files.Length + ")",
- "Parsing " + file,
- (i + 1) / (float)files.Length);
- try
- {
- warnings.AddRange(Generate(file, outPkgPath, rosPackageName));
- }
- catch (MessageTokenizerException e)
- {
- Debug.LogError(e.ToString() + e.Message);
- EditorUtility.DisplayDialog(
- title: "Message Tokenizer Exception",
- message: e.Message,
- ok: "Wait. That's illegal");
- }
- catch (MessageParserException e)
- {
- Debug.LogError(e.ToString() + e.Message);
- EditorUtility.DisplayDialog(
- title: "Message Parser Exception",
- message: e.Message,
- ok: "Sorry but you can't ignore errors.");
- }
- }
- // Done
- EditorUtility.ClearProgressBar();
- AssetDatabase.Refresh();
- if (warnings.Count > 0)
- {
- EditorUtility.DisplayDialog(
- title: "Code Generation Complete",
- message: "Output at: " + outPkgPath + "\nYou have " + warnings.Count + " warning(s)",
- ok: "I like to live dangerously");
- foreach (string w in warnings)
- {
- Debug.LogWarning(w);
- }
- }
- else
- {
- EditorUtility.DisplayDialog(
- title: "Code Generation Complete",
- message: "Output at: " + outPkgPath,
- ok: "Thank you!");
- }
- Reset();
- }
- }
- catch (DirectoryNotFoundException e)
- {
- EditorUtility.DisplayDialog(
- title: "Message Folder not found",
- message: e.Message,
- ok: "Bricks without straw");
- Reset();
- }
- }
- }
- }
- private void OnInspectorUpdate()
- {
- Repaint();
- }
- private void Reset()
- {
- inPkgPath = "";
- rosPackageName = "";
- outPkgPath = Path.Combine(System.Environment.CurrentDirectory, "Assets", "RosSharpMessages");
- }
- protected abstract List<string> Generate(string inPath, string outPath, string rosPackageName = "");
- }
- }
|