Esempio n. 1
0
    static void ImportJson()
    {
        string jsonPath = EditorUtility.OpenFilePanel("Select JSON file", "", "json");

        if (jsonPath.Length == 0)
        {
            return;
        }
        var bytes = JsonToFlexBufferConverter.ConvertFile(jsonPath);

        if (bytes == null)
        {
            return;
        }

        var fileName = Path.GetFileNameWithoutExtension(jsonPath);

        var flxPath = EditorUtility.SaveFilePanel(
            "Save as FlexBuffer",
            "",
            fileName + ".bytes",
            "bytes");

        if (flxPath.Length != 0)
        {
            File.WriteAllBytes(flxPath, bytes);
        }
    }
Esempio n. 2
0
    void OnGUI()
    {
        if (GUILayout.Button("Open FlexBuffer file..."))
        {
            var jsonPath = EditorUtility.OpenFilePanel("Select FlexBuffer file", "", "bytes");
            if (jsonPath.Length == 0)
            {
                return;
            }

            _path = jsonPath;

            _query = "";

            var bytes = File.ReadAllBytes(_path);

            _fileSize = bytes.Length;

            var root = FlxValue.FromBytes(bytes);
            _treeView      = new FlexBufferTreeView(root, _treeViewState);
            _treeViewState = new TreeViewState();
        }

        if (_path.Length == 0)
        {
            return;
        }

        var jsonFileString = _jsonPath.Length > 0 ? $"| {_jsonPath} [{_jsonFileSize}]" : "";

        GUILayout.Label($"{_path} [{_fileSize}] {jsonFileString}");

        if (_jsonPath.Length > 0)
        {
            GUILayout.BeginHorizontal();
        }

        if (GUILayout.Button("Export as JSON..."))
        {
            var jsonPath = EditorUtility.SaveFilePanel(
                "Save as JSON",
                "",
                $"{Path.GetFileNameWithoutExtension(_path)}.json",
                "json");

            if (jsonPath.Length != 0)
            {
                var prettyJson = FlexBuffersPreferences.PrettyPrintedJson ? _treeView._rootValue.ToPrettyJson() : _treeView._rootValue.ToJson;
                _jsonFileSize = prettyJson.Length;
                File.WriteAllText(jsonPath, prettyJson);
            }

            _jsonPath = jsonPath;
        }

        if (_jsonPath.Length > 0)
        {
            if (GUILayout.Button("Open JSON"))
            {
                Application.OpenURL($"file://{_jsonPath}");
            }

            if (GUILayout.Button("Import from JSON"))
            {
                var bytes = JsonToFlexBufferConverter.ConvertFile(_jsonPath);

                if (bytes != null)
                {
                    File.WriteAllBytes(_path, bytes);
                }

                var root = FlxValue.FromBytes(bytes);
                _treeView      = new FlexBufferTreeView(root, _treeViewState);
                _treeViewState = new TreeViewState();
            }
            GUILayout.EndHorizontal();
        }

        var newQuery = GUILayout.TextField(_query);

        if (newQuery != _query)
        {
            var query = FlxQueryParser.Convert(newQuery);
            _query = newQuery;
            _treeView?.SetQuery(query);
        }

        _treeView?.OnGUI(new Rect(0, 80, position.width, position.height - 80));


        if (Event.current.type == EventType.KeyUp && (Event.current.modifiers == EventModifiers.Control ||
                                                      Event.current.modifiers == EventModifiers.Command))
        {
            if (Event.current.keyCode == KeyCode.C)
            {
                Event.current.Use();
                var selection = _treeView?.GetSelection();
                if (selection != null && selection.Count > 0)
                {
                    if (_treeView.GetRows().First(item => item.id == selection[0]) is FlxValueTreeViewItem row)
                    {
                        GUIUtility.systemCopyBuffer = row.FlxValue.ToJson;
                    }
                }
            }
        }
    }