EntryPoint.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.IO;
  3. using System.Linq;
  4. using Content.Shared.Humanoid.Markings;
  5. using Content.Shared.IoC;
  6. using Content.Shared.Maps;
  7. using Robust.Shared;
  8. using Robust.Shared.Configuration;
  9. using Robust.Shared.ContentPack;
  10. using Robust.Shared.Map;
  11. using Robust.Shared.Prototypes;
  12. using Robust.Shared.Serialization.Markdown;
  13. using Robust.Shared.Serialization.Markdown.Sequence;
  14. using Robust.Shared.Serialization.Markdown.Value;
  15. using Robust.Shared.Utility;
  16. namespace Content.Shared.Entry
  17. {
  18. public sealed class EntryPoint : GameShared
  19. {
  20. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  21. [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
  22. [Dependency] private readonly IResourceManager _resMan = default!;
  23. private readonly ResPath _ignoreFileDirectory = new("/IgnoredPrototypes/");
  24. public override void PreInit()
  25. {
  26. IoCManager.InjectDependencies(this);
  27. SharedContentIoC.Register();
  28. }
  29. public override void Shutdown()
  30. {
  31. _prototypeManager.PrototypesReloaded -= PrototypeReload;
  32. }
  33. public override void Init()
  34. {
  35. IgnorePrototypes();
  36. }
  37. public override void PostInit()
  38. {
  39. base.PostInit();
  40. InitTileDefinitions();
  41. IoCManager.Resolve<MarkingManager>().Initialize();
  42. #if DEBUG
  43. var configMan = IoCManager.Resolve<IConfigurationManager>();
  44. configMan.OverrideDefault(CVars.NetFakeLagMin, 0.075f);
  45. configMan.OverrideDefault(CVars.NetFakeLoss, 0.005f);
  46. configMan.OverrideDefault(CVars.NetFakeDuplicates, 0.005f);
  47. #endif
  48. }
  49. private void InitTileDefinitions()
  50. {
  51. _prototypeManager.PrototypesReloaded += PrototypeReload;
  52. // Register space first because I'm a hard coding hack.
  53. var spaceDef = _prototypeManager.Index<ContentTileDefinition>(ContentTileDefinition.SpaceID);
  54. _tileDefinitionManager.Register(spaceDef);
  55. var prototypeList = new List<ContentTileDefinition>();
  56. foreach (var tileDef in _prototypeManager.EnumeratePrototypes<ContentTileDefinition>())
  57. {
  58. if (tileDef.ID == ContentTileDefinition.SpaceID)
  59. {
  60. continue;
  61. }
  62. prototypeList.Add(tileDef);
  63. }
  64. // Sort ordinal to ensure it's consistent client and server.
  65. // So that tile IDs match up.
  66. prototypeList.Sort((a, b) => string.Compare(a.ID, b.ID, StringComparison.Ordinal));
  67. foreach (var tileDef in prototypeList)
  68. {
  69. _tileDefinitionManager.Register(tileDef);
  70. }
  71. _tileDefinitionManager.Initialize();
  72. }
  73. private void PrototypeReload(PrototypesReloadedEventArgs obj)
  74. {
  75. /* I am leaving this here commented out to re-iterate
  76. - our game is shitcode
  77. - tiledefmanager no likey proto reloads and you must re-assign the tile ids.
  78. if (!obj.WasModified<ContentTileDefinition>())
  79. return;
  80. */
  81. // Need to re-allocate tiledefs due to how prototype reloads work
  82. foreach (var def in _prototypeManager.EnumeratePrototypes<ContentTileDefinition>())
  83. {
  84. def.AssignTileId(_tileDefinitionManager[def.ID].TileId);
  85. }
  86. }
  87. private void IgnorePrototypes()
  88. {
  89. if (!TryReadFile(out var sequences))
  90. return;
  91. foreach (var sequence in sequences)
  92. {
  93. foreach (var node in sequence.Sequence)
  94. {
  95. var path = new ResPath(((ValueDataNode) node).Value);
  96. if (string.IsNullOrEmpty(path.Extension))
  97. {
  98. _prototypeManager.AbstractDirectory(path);
  99. }
  100. else
  101. {
  102. _prototypeManager.AbstractFile(path);
  103. }
  104. }
  105. }
  106. }
  107. private bool TryReadFile([NotNullWhen(true)] out List<SequenceDataNode>? sequence)
  108. {
  109. sequence = new();
  110. foreach (var path in _resMan.ContentFindFiles(_ignoreFileDirectory))
  111. {
  112. if (!_resMan.TryContentFileRead(path, out var stream))
  113. continue;
  114. using var reader = new StreamReader(stream, EncodingHelpers.UTF8);
  115. var documents = DataNodeParser.ParseYamlStream(reader).FirstOrDefault();
  116. if (documents == null)
  117. continue;
  118. sequence.Add((SequenceDataNode) documents.Root);
  119. }
  120. return true;
  121. }
  122. }
  123. }