Skip to content

Commit f594d8f

Browse files
committed
Starting work on part editing, have a converter from part to json!
1 parent 810ca9f commit f594d8f

File tree

7 files changed

+776
-18
lines changed

7 files changed

+776
-18
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ crashlytics-build.properties
6262
.idea/
6363

6464
# Copyrighted Packages
65-
Packages/KSP2_x64
65+
Packages/KSP2_x64
66+
Assets/engine_0v_xenon_dawn*

Assets/Editor/PartEditor.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

Assets/Editor/PartEditor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)