|
| 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 | +} |
0 commit comments