Repository navigation
Expand file tree
/
Copy pathMod.cs
More file actions
134 lines (116 loc) · 4.75 KB
/
Copy pathMod.cs
File metadata and controls
134 lines (116 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Colossal.IO.AssetDatabase;
using Colossal.Logging;
using Colossal.Serialization.Entities;
using DisableAccidents.ModSettings;
using Game;
using Game.Modding;
using Game.Prefabs;
using Game.SceneFlow;
using Unity.Collections;
using Unity.Entities;
namespace DisableAccidents
{
public class Mod : IMod
{
public static ILog log = LogManager.GetLogger($"{nameof(DisableAccidents)}").SetShowsErrorsInUI(false);
internal static Setting m_Setting;
public void OnLoad(UpdateSystem updateSystem)
{
log.Info(nameof(OnLoad));
m_Setting = new Setting(this);
m_Setting.RegisterInOptionsUI();
GameManager.instance.localizationManager.AddSource("en-US", new LocaleEN(m_Setting));
AssetDatabase.global.LoadSettings(nameof(DisableAccidents), m_Setting);
m_Setting.Apply();
updateSystem.UpdateAt<NoTrafficAccidentsSystem>(SystemUpdatePhase.ModificationEnd);
updateSystem.UpdateAt<InitialSettingsApplySystem>(SystemUpdatePhase.ModificationEnd);
}
public void OnDispose()
{
log.Info(nameof(OnDispose));
if (m_Setting != null)
{
m_Setting.UnregisterInOptionsUI();
m_Setting = null;
Setting.Instance = null;
}
}
}
public partial class InitialSettingsApplySystem : GameSystemBase
{
protected override void OnCreate()
{
base.OnCreate();
this.Enabled = true;
}
protected override void OnUpdate()
{
if (Setting.Instance != null)
{
var accidentSystem = EntityManager.World.GetOrCreateSystemManaged<NoTrafficAccidentsSystem>();
accidentSystem.ForceUpdate(
Setting.Instance.ModEnabled,
Setting.Instance.ModEnabled ? 0f : (Setting.Instance.AccidentProbability / 100f)
);
this.Enabled = false;
}
}
}
public partial class NoTrafficAccidentsSystem : GameSystemBase
{
private PrefabSystem m_PrefabSystem;
private EntityQuery m_AccidentPrefabQuery;
private bool m_LastEnabledState;
private float m_LastMultiplier;
protected override void OnCreate()
{
base.OnCreate();
m_PrefabSystem = World.GetOrCreateSystemManaged<PrefabSystem>();
m_AccidentPrefabQuery = GetEntityQuery(
ComponentType.ReadOnly<EventData>(),
ComponentType.ReadOnly<TrafficAccidentData>(),
ComponentType.Exclude<Locked>()
);
RequireForUpdate(m_AccidentPrefabQuery);
}
public void ForceUpdate(bool currentEnabled, float multiplier)
{
m_LastEnabledState = currentEnabled;
m_LastMultiplier = multiplier;
UpdateAccidentProbabilities(currentEnabled, multiplier);
}
protected override void OnUpdate()
{
bool currentEnabled = Setting.Instance?.ModEnabled ?? false;
float currentMultiplier = currentEnabled ? 0f : (Setting.Instance?.AccidentProbability ?? 100) / 100f;
if (currentEnabled != m_LastEnabledState || currentMultiplier != m_LastMultiplier)
{
m_LastEnabledState = currentEnabled;
m_LastMultiplier = currentMultiplier;
UpdateAccidentProbabilities(currentEnabled, currentMultiplier);
}
}
private void UpdateAccidentProbabilities(bool disableAccidents, float multiplier)
{
Mod.log.Info($"Setting accident probabilities to {(disableAccidents ? 0f : multiplier * 100f):0}% of vanilla");
using var accidentPrefabs = m_AccidentPrefabQuery.ToEntityArray(Allocator.Temp);
foreach (var prefab in accidentPrefabs)
{
if (!m_PrefabSystem.GetPrefab<PrefabBase>(prefab).TryGet(out TrafficAccident accident))
{
continue;
}
var data = EntityManager.GetComponentData<TrafficAccidentData>(prefab);
data.m_OccurenceProbability = disableAccidents ? 0f : accident.m_OccurrenceProbability * multiplier;
EntityManager.SetComponentData(prefab, data);
}
}
protected override void OnGameLoadingComplete(Purpose purpose, GameMode mode)
{
base.OnGameLoadingComplete(purpose, mode);
bool currentEnabled = Setting.Instance?.ModEnabled ?? false;
float currentMultiplier = currentEnabled ? 0f : (Setting.Instance?.AccidentProbability ?? 100) / 100f;
ForceUpdate(currentEnabled, currentMultiplier);
}
}
}