diff --git a/Utility Mods/FixBlockCategories/Data/Scripts/FixBlockCategories/MainSession.cs b/Utility Mods/FixBlockCategories/Data/Scripts/FixBlockCategories/MainSession.cs new file mode 100644 index 000000000..779ef2413 --- /dev/null +++ b/Utility Mods/FixBlockCategories/Data/Scripts/FixBlockCategories/MainSession.cs @@ -0,0 +1,76 @@ +using System; +using Sandbox.Definitions; +using System.Collections.Generic; +using Sandbox.ModAPI; +using VRage.Game.Components; +using VRage.Network; +using VRage.Utils; + +namespace SC.FixBlockCategories +{ + [MySessionComponentDescriptor(MyUpdateOrder.NoUpdate, int.MaxValue)] // load last + internal class MainSession : MySessionComponentBase + { + private HashSet _nullSubtypes = new HashSet(); + private Dictionary _subtypeToTypePairing = new Dictionary(); + + public override void LoadData() + { + MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Start check..."); + + _subtypeToTypePairing = new Dictionary(); + foreach (var def in MyDefinitionManager.Static.GetAllDefinitions()) + { + var blockdef = def as MyCubeBlockDefinition; + if (blockdef == null) + continue; + + if (string.IsNullOrEmpty(blockdef.Id.SubtypeName)) + { + _nullSubtypes.Add(blockdef.Id.TypeId.ToString().Replace("MyObjectBuilder_", "")); + continue; + } + + _subtypeToTypePairing[blockdef.Id.SubtypeName] = blockdef.Id.TypeId.ToString().Replace("MyObjectBuilder_", ""); + } + + foreach (var catdef in MyDefinitionManager.Static.GetCategories().Values) + { + MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] CHK {catdef.Name}"); + var replaced = new HashSet(); + foreach (var item in catdef.ItemIds) + { + var name = FixItemName(catdef.Name, item); + if (name != null) + replaced.Add(name); + } + catdef.ItemIds = replaced; + } + + MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Finished."); + } + + private string FixItemName(string categoryName, string subtypeId) + { + // already valid + if (subtypeId.Contains("/")) + return subtypeId; + + string typeId; + if (_subtypeToTypePairing.TryGetValue(subtypeId, out typeId)) // keen broke block category items with just subtypeid + { + MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Replaced {categoryName}::{typeId + "/" + subtypeId}"); + return typeId + "/" + subtypeId; + } + else if (_nullSubtypes.Contains(subtypeId)) + { + // subtype-less blocks (i.e. gravity generator) + MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Replaced {categoryName}::{subtypeId + "/(null)"}"); + return subtypeId + "/(null)"; + } + + MyLog.Default.WriteLineAndConsole($"[FixBlockCategories] Removed {categoryName}::{subtypeId} (does the block exist?)"); + return null; + } + } +} diff --git a/Utility Mods/FixBlockCategories/metadata.mod b/Utility Mods/FixBlockCategories/metadata.mod new file mode 100644 index 000000000..83173346a --- /dev/null +++ b/Utility Mods/FixBlockCategories/metadata.mod @@ -0,0 +1,4 @@ + + + 1.0 + \ No newline at end of file diff --git a/Utility Mods/FixBlockCategories/modinfo_main.sbmi b/Utility Mods/FixBlockCategories/modinfo_main.sbmi new file mode 100644 index 000000000..0fa87a5a0 --- /dev/null +++ b/Utility Mods/FixBlockCategories/modinfo_main.sbmi @@ -0,0 +1,11 @@ + + + 76561198274566684 + 0 + + + 3473465973 + Steam + + + \ No newline at end of file diff --git a/Utility Mods/FixBlockCategories/thumb.jpg b/Utility Mods/FixBlockCategories/thumb.jpg new file mode 100644 index 000000000..15e2d6a07 Binary files /dev/null and b/Utility Mods/FixBlockCategories/thumb.jpg differ