|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Reflection; |
| 6 | +using KSP; |
| 7 | +using UnityEditor; |
| 8 | +using Cheese.Extensions; |
| 9 | +using KSP.IO; |
| 10 | +using KSP.Modules; |
| 11 | +using KSP.Sim.Definitions; |
| 12 | +using Newtonsoft.Json; |
| 13 | +using Newtonsoft.Json.UnityConverters; |
| 14 | +using Newtonsoft.Json.UnityConverters.Configuration; |
| 15 | +using UnityEditor.VersionControl; |
| 16 | +using UnityEngine; |
| 17 | + |
| 18 | +[CustomEditor(typeof(CorePartData))] |
| 19 | +public class PartEditor : Editor |
| 20 | +{ |
| 21 | + |
| 22 | + private static bool _initialized = false; |
| 23 | + |
| 24 | + // Just initialize all the conversion stuff |
| 25 | + private static void Initialize() |
| 26 | + { |
| 27 | + typeof(IOProvider).GetMethod("Init", BindingFlags.Static | BindingFlags.NonPublic) |
| 28 | + ?.Invoke(null, new object[] { }); |
| 29 | + _initialized = true; |
| 30 | + Module_Engine mod; |
| 31 | + } |
| 32 | + |
| 33 | + public override void OnInspectorGUI() |
| 34 | + { |
| 35 | + base.OnInspectorGUI(); |
| 36 | + if (!GUILayout.Button("Save Part JSON")) return; |
| 37 | + if (!_initialized) Initialize(); |
| 38 | + var core = (serializedObject.targetObject as CorePartData)?.Core!; |
| 39 | + var targetGO = (serializedObject.targetObject as CorePartData).gameObject; |
| 40 | + if (core == null) return; |
| 41 | + // Clear out the serialized part modules and reserialize them |
| 42 | + core.data.serializedPartModules.Clear(); |
| 43 | + foreach (var child in targetGO.GetComponents<PartBehaviourModule>()) |
| 44 | + { |
| 45 | + child.GetType().GetMethod("AddDataModules", BindingFlags.Instance | BindingFlags.NonPublic)?.Invoke(child, new object[] {}); |
| 46 | + foreach (var data in child.DataModules.Values) |
| 47 | + { |
| 48 | + data.GetType().GetMethod("RebuildDataContext", BindingFlags.Instance | BindingFlags.NonPublic) |
| 49 | + ?.Invoke(data, new object[] { }); |
| 50 | + } |
| 51 | + core.data.serializedPartModules.Add(new SerializedPartModule(child,false)); |
| 52 | + } |
| 53 | + var json = IOProvider.ToJson(core); |
| 54 | + File.WriteAllText($"{Application.dataPath}/{core.data.partName}.json", json); |
| 55 | + AssetDatabase.Refresh(); |
| 56 | + EditorUtility.DisplayDialog("Part Exported", $"Json is at: {Application.dataPath}/{core.data.partName}.json", "ok"); |
| 57 | + } |
| 58 | +} |
0 commit comments