Skip to content

Commit 0859729

Browse files
committed
Add FixBlockCategories mod
1 parent 3722e5b commit 0859729

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using Sandbox.Definitions;
3+
using System.Collections.Generic;
4+
using Sandbox.ModAPI;
5+
using VRage.Game.Components;
6+
using VRage.Network;
7+
using VRage.Utils;
8+
9+
namespace SC.FixBlockCategories
10+
{
11+
[MySessionComponentDescriptor(MyUpdateOrder.NoUpdate, int.MaxValue)] // load last
12+
internal class MainSession : MySessionComponentBase
13+
{
14+
private HashSet<string> _nullSubtypes = new HashSet<string>();
15+
private Dictionary<string, string> _subtypeToTypePairing = new Dictionary<string, string>();
16+
17+
public override void LoadData()
18+
{
19+
MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Start check...");
20+
21+
_subtypeToTypePairing = new Dictionary<string, string>();
22+
foreach (var def in MyDefinitionManager.Static.GetAllDefinitions())
23+
{
24+
var blockdef = def as MyCubeBlockDefinition;
25+
if (blockdef == null)
26+
continue;
27+
28+
if (string.IsNullOrEmpty(blockdef.Id.SubtypeName))
29+
{
30+
_nullSubtypes.Add(blockdef.Id.TypeId.ToString().Replace("MyObjectBuilder_", ""));
31+
continue;
32+
}
33+
34+
_subtypeToTypePairing[blockdef.Id.SubtypeName] = blockdef.Id.TypeId.ToString().Replace("MyObjectBuilder_", "");
35+
}
36+
37+
foreach (var catdef in MyDefinitionManager.Static.GetCategories().Values)
38+
{
39+
MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] CHK {catdef.Name}");
40+
var replaced = new HashSet<string>();
41+
foreach (var item in catdef.ItemIds)
42+
{
43+
var name = FixItemName(catdef.Name, item);
44+
if (name != null)
45+
replaced.Add(name);
46+
}
47+
catdef.ItemIds = replaced;
48+
}
49+
50+
MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Finished.");
51+
}
52+
53+
private string FixItemName(string categoryName, string subtypeId)
54+
{
55+
// already valid
56+
if (subtypeId.Contains("/"))
57+
return subtypeId;
58+
59+
string typeId;
60+
if (_subtypeToTypePairing.TryGetValue(subtypeId, out typeId)) // keen broke block category items with just subtypeid
61+
{
62+
MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Replaced {categoryName}::{typeId + "/" + subtypeId}");
63+
return typeId + "/" + subtypeId;
64+
}
65+
else if (_nullSubtypes.Contains(subtypeId))
66+
{
67+
// subtype-less blocks (i.e. gravity generator)
68+
MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Replaced {categoryName}::{subtypeId + "/(null)"}");
69+
return subtypeId + "/(null)";
70+
}
71+
72+
MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Removed {categoryName}::{subtypeId} (does the block exist?)");
73+
return null;
74+
}
75+
}
76+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<ModVersion>1.0</ModVersion>
4+
</ModMetadata>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<MyObjectBuilder_ModInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<SteamIDOwner>76561198274566684</SteamIDOwner>
4+
<WorkshopId>0</WorkshopId>
5+
<WorkshopIds>
6+
<WorkshopId>
7+
<Id>3473465973</Id>
8+
<ServiceName>Steam</ServiceName>
9+
</WorkshopId>
10+
</WorkshopIds>
11+
</MyObjectBuilder_ModInfo>
859 KB
Loading

0 commit comments

Comments
 (0)