Skip to content

Commit 3f0c2f8

Browse files
committed
functional proof of concept for deleting objects
1 parent 1503629 commit 3f0c2f8

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SCENE_PATCH
2+
{
3+
name = test
4+
type = DeleteObject
5+
scene = VABmodern
6+
7+
ObjectNames
8+
{
9+
=VABmodern
10+
}
11+
}

Source/DeleteObjectScenePatch.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
using Log = KSPBuildTools.Log;
8+
9+
namespace GameObjectPatcher
10+
{
11+
internal class DeleteObject : ScenePatch
12+
{
13+
[Persistent] public string[] ObjectNames;
14+
15+
public override void Execute()
16+
{
17+
foreach (var objectName in ObjectNames)
18+
{
19+
var gameObject = GameObject.Find(objectName);
20+
if (gameObject == null)
21+
{
22+
Log.Error($"GameObject {objectName} not found");
23+
}
24+
else
25+
{
26+
GameObject.Destroy(gameObject);
27+
}
28+
}
29+
}
30+
}
31+
}

Source/GameObjectPatcherAddon.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using UnityEngine;
34
using UnityEngine.SceneManagement;
45
using Log = KSPBuildTools.Log;
@@ -10,6 +11,8 @@ public class GameObjectPatcherAddon : MonoBehaviour
1011
{
1112
void Awake()
1213
{
14+
GameObject.DontDestroyOnLoad(this);
15+
1316
SceneManager.sceneLoaded += OnSceneLoaded;
1417
}
1518

@@ -21,6 +24,47 @@ void OnDestroy()
2124
private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
2225
{
2326
Log.Message("OnSceneLoaded: " + scene.name);
27+
28+
if (m_scenePatches.TryGetValue(scene.name, out var scenePatches))
29+
{
30+
foreach (var scenePatch in scenePatches)
31+
{
32+
Log.Debug($"executing patch {scenePatch.name}");
33+
scenePatch.Execute();
34+
}
35+
}
36+
}
37+
38+
Dictionary<string, List<ScenePatch>> m_scenePatches = new Dictionary<string, List<ScenePatch>>();
39+
40+
public void ModuleManagerPostLoad()
41+
{
42+
foreach (var urlConfig in GameDatabase.Instance.root.AllConfigs)
43+
{
44+
if (urlConfig.type == "SCENE_PATCH")
45+
{
46+
var scenes = urlConfig.config.GetValuesList("scene");
47+
48+
if (scenes.Count == 0)
49+
{
50+
Log.Error($"SCENE_PATCH config {urlConfig.url} must have at least one 'scene' value");
51+
continue;
52+
}
53+
54+
var scenePatch = ScenePatch.Create(urlConfig);
55+
56+
if (scenePatch == null) continue;
57+
58+
foreach (var scene in scenes)
59+
{
60+
if (!m_scenePatches.ContainsKey(scene))
61+
{
62+
m_scenePatches[scene] = new List<ScenePatch>();
63+
}
64+
m_scenePatches[scene].Add(scenePatch);
65+
}
66+
}
67+
}
2468
}
2569
}
2670
}

Source/ScenePatch.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using KSPBuildTools;
2+
using Steamworks;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace GameObjectPatcher
10+
{
11+
public class ScenePatch
12+
{
13+
// might want to abstract this out at some point
14+
public static ScenePatch Create(UrlDir.UrlConfig urlConfig)
15+
{
16+
List<Type> scenePatchTypes = AssemblyLoader.GetSubclassesOfParentClass(typeof(ScenePatch));
17+
18+
ConfigNode configNode = urlConfig.config;
19+
string typeName = configNode.GetValue("type") ?? "";
20+
Type type = scenePatchTypes.FirstOrDefault(t => t.Name == typeName);
21+
if (type == null)
22+
{
23+
Log.Error($"{typeName} is not a valid ScenePatch type: {urlConfig.url}");
24+
return null;
25+
}
26+
27+
try
28+
{
29+
var result = (ScenePatch)Activator.CreateInstance(type);
30+
result.Load(configNode);
31+
return result;
32+
}
33+
catch (Exception ex)
34+
{
35+
Log.Error($"failed to create scenepatch {typeName} : {urlConfig.url}");
36+
Log.Exception(ex);
37+
}
38+
39+
return null;
40+
}
41+
42+
[Persistent]
43+
public string name = "";
44+
45+
public virtual void Load(ConfigNode configNode)
46+
{
47+
ConfigNode.LoadObjectFromConfig(this, configNode);
48+
}
49+
50+
public virtual void Execute() { }
51+
}
52+
}

0 commit comments

Comments
 (0)