yum-slop/YOTS

an optimized toggle system for vrchat

git clone https://git.yummers.dev/yum-slop/YOTS

yumImprove error reportingce52a61

master
3.6 KiB142 linesraw
1#if UNITY_EDITOR
2
3using UnityEngine;
4using VRC.SDK3.Avatars.ScriptableObjects;
5using UnityEditor;
6using System.IO;
7
8namespace YOTS
9{
10  [DisallowMultipleComponent]
11  [AddComponentMenu("YOTS Config")]
12  public class YOTSConfig : MonoBehaviour {
13    [Tooltip("The JSON configuration file.")]
14    public TextAsset jsonConfig;
15
16    [TextArea(5, 30)]
17    public string jsonContent;
18
19    [SerializeField, HideInInspector]
20    private TextAsset lastJsonConfig;
21
22    void OnValidate() {
23      gameObject.tag = "EditorOnly";
24
25      // Only update jsonContent when jsonConfig actually changes
26      if (jsonConfig != lastJsonConfig) {
27        if (jsonConfig != null) {
28          jsonContent = jsonConfig.text;
29        }
30        lastJsonConfig = jsonConfig;
31      }
32    }
33  }
34
35  [CustomEditor(typeof(YOTSConfig))]
36  public class YOTSConfigEditor : Editor
37  {
38    private YOTSConfig config;
39
40    private void OnEnable()
41    {
42      config = (YOTSConfig)target;
43    }
44
45    public override void OnInspectorGUI()
46    {
47      EditorGUI.BeginChangeCheck();
48
49      // Draw the default inspector
50      DrawDefaultInspector();
51
52      EditorGUILayout.HelpBox(
53        "You can inspect and edit the JSON config using the textbox above. " +
54        "Changes are saved automatically. Changes from external editors will " +
55        "appear upon tabbing back into Unity.",
56        MessageType.Info);
57
58
59      // If changes were made in the inspector
60      if (EditorGUI.EndChangeCheck())
61      {
62        // Save changes immediately
63        SaveJsonToFile();
64      }
65      // Only check for file changes if we're not currently editing
66      else if (config.jsonConfig != null)
67      {
68        string currentContent = config.jsonConfig.text;
69        if (currentContent != config.jsonContent)
70        {
71          config.jsonContent = currentContent;
72          GUI.changed = true;
73        }
74      }
75
76      // Check for Ctrl+S
77      Event e = Event.current;
78      if (e.type == EventType.KeyDown && e.keyCode == KeyCode.S && e.control)
79      {
80        e.Use();
81        SaveJsonToFile();
82      }
83    }
84
85    private void SaveJsonToFile()
86    {
87      if (config.jsonConfig == null)
88      {
89        Debug.LogWarning("No JSON config file assigned!");
90        return;
91      }
92
93      string assetPath = AssetDatabase.GetAssetPath(config.jsonConfig);
94      if (string.IsNullOrEmpty(assetPath))
95      {
96        Debug.LogError("Could not find asset path!");
97        return;
98      }
99
100      try
101      {
102        // Write the modified content from our component
103        File.WriteAllText(assetPath, config.jsonContent);
104
105        // Force Unity to reload the file from disk
106        AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
107
108        // Update the TextAsset to match our changes
109        var serializedObject = new SerializedObject(config.jsonConfig);
110        serializedObject.FindProperty("m_Script").stringValue = config.jsonContent;
111        serializedObject.ApplyModifiedProperties();
112
113        EditorUtility.SetDirty(config.jsonConfig);
114        AssetDatabase.SaveAssets();
115        Debug.Log($"Successfully saved JSON to {assetPath}");
116      }
117      catch (System.Exception ex)
118      {
119        Debug.LogError($"Error saving JSON: {ex.Message}");
120      }
121    }
122  }
123
124  // Add this new class to handle file modifications
125  public class JsonFileProcessor : UnityEditor.AssetModificationProcessor
126  {
127    private static void OnWillSaveAssets(string[] paths)
128    {
129      foreach (string path in paths)
130      {
131        if (path.EndsWith(".json"))
132        {
133            // Force Unity to reimport the asset
134            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
135        }
136      }
137    }
138  }
139}
140
141#endif  // UNITY_EDITOR
142