This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WTSController.cs
210 lines (177 loc) · 10.9 KB
/
WTSController.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
extern alias TLM;
using Klyte.Commons;
using Klyte.Commons.Interfaces;
using Klyte.Commons.Utils;
using Klyte.WriteTheSigns.ModShared;
using Klyte.WriteTheSigns.Overrides;
using Klyte.WriteTheSigns.Singleton;
using Klyte.WriteTheSigns.Sprites;
using Klyte.WriteTheSigns.Tools;
using Klyte.WriteTheSigns.Utils;
using SpriteFontPlus;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Klyte.WriteTheSigns
{
public class WTSController : BaseController<WriteTheSignsMod, WTSController>
{
public RoadSegmentTool RoadSegmentToolInstance => ToolsModifierControl.toolController.GetComponent<RoadSegmentTool>();
public BuildingEditorTool BuildingEditorToolInstance => ToolsModifierControl.toolController.GetComponent<BuildingEditorTool>();
public VehicleEditorTool VehicleEditorToolInstance => ToolsModifierControl.toolController.GetComponent<VehicleEditorTool>();
internal WTSAtlasesLibrary AtlasesLibrary { get; private set; }
internal WTSBuildingPropsSingleton BuildingPropsSingleton { get; private set; }
internal WTSVehicleTextsSingleton VehicleTextsSingleton { get; private set; }
internal WTSOnNetPropsSingleton OnNetPropsSingleton { get; private set; }
internal WTSHighwayShieldsSingleton HighwayShieldsSingleton { get; private set; }
internal WTSHighwayShieldsAtlasLibrary HighwayShieldsAtlasLibrary { get; private set; }
internal IBridgeTLM ConnectorTLM { get; private set; }
internal IBridgeADR ConnectorADR { get; private set; }
protected override string ClassBridgeUUI { get; } = "Klyte.WriteTheSigns.ModShared.BridgeUUI";
public WTSRoadPropsSingleton RoadPropsSingleton { get; private set; }
public Dictionary<string, Dictionary<string, string>> AbbreviationFiles { get; private set; }
public FontServer FontServer => FontServer.instance;
public static int DefaultTextureSizeFont => 512 << WriteTheSignsMod.StartTextureSizeFont;
public event Action EventFontsReloadedFromFolder;
public event Action EventOnDistrictChanged;
public event Action EventOnParkChanged;
public event Action<ushort?> EventOnBuildingNameChanged;
public event Action EventOnZeroMarkerChanged;
public event Action EventOnPostalCodeChanged;
public static void OnDistrictChanged()
{
if (LoadingManager.instance.m_LoadingWrapper.loadingComplete)
{
WriteTheSignsMod.Controller?.EventOnDistrictChanged?.Invoke();
}
}
public static void OnParkChanged()
{
if (LoadingManager.instance.m_LoadingWrapper.loadingComplete)
{
WriteTheSignsMod.Controller?.EventOnParkChanged?.Invoke();
}
}
public static void OnBuildingNameChanged(ushort? buildingId) => WriteTheSignsMod.Controller?.EventOnBuildingNameChanged?.Invoke(buildingId);
public static void OnCityNameChanged()
{
if (LoadingManager.instance.m_LoadingWrapper.loadingComplete)
{
WTSCacheSingleton.ClearCacheCityName();
}
}
public static void OnZeroMarkChanged() => WriteTheSignsMod.Controller?.EventOnZeroMarkerChanged?.Invoke();
public static void OnPostalCodeChanged() => WriteTheSignsMod.Controller?.EventOnPostalCodeChanged?.Invoke();
public void Awake()
{
ToolsModifierControl.toolController.AddExtraToolToController<RoadSegmentTool>();
ToolsModifierControl.toolController.AddExtraToolToController<BuildingEditorTool>();
ToolsModifierControl.toolController.AddExtraToolToController<VehicleEditorTool>();
ToolsModifierControl.toolController.AddExtraToolToController<SegmentEditorPickerTool>();
ReloadAbbreviationFiles();
FontServer.Ensure();
AtlasesLibrary = gameObject.AddComponent<WTSAtlasesLibrary>();
BuildingPropsSingleton = gameObject.AddComponent<WTSBuildingPropsSingleton>();
RoadPropsSingleton = gameObject.AddComponent<WTSRoadPropsSingleton>();
VehicleTextsSingleton = gameObject.AddComponent<WTSVehicleTextsSingleton>();
OnNetPropsSingleton = gameObject.AddComponent<WTSOnNetPropsSingleton>();
HighwayShieldsSingleton = gameObject.AddComponent<WTSHighwayShieldsSingleton>();
HighwayShieldsAtlasLibrary = gameObject.AddComponent<WTSHighwayShieldsAtlasLibrary>();
ConnectorTLM = PluginUtils.GetImplementationTypeForMod<BridgeTLMFallback, IBridgeTLM>(gameObject, "TransportLinesManager", "14.0.0.0", "Klyte.WriteTheSigns.ModShared.BridgeTLM");
ConnectorADR = PluginUtils.GetImplementationTypeForMod<BridgeADRFallback, IBridgeADR>(gameObject, "KlyteAddresses", "3.1.1.0", "Klyte.WriteTheSigns.ModShared.BridgeADR");
}
protected override void StartActions()
{
ReloadFontsFromPath();
BuildingManager.instance.EventBuildingReleased += WTSBuildingDataCaches.PurgeBuildingCache;
BuildingManager.instance.EventBuildingRelocated += WTSBuildingDataCaches.PurgeBuildingCache;
NetManagerOverrides.EventSegmentNameChanged += OnNameSeedChanged;
BuildingManager.instance.EventBuildingRelocated += WTSCacheSingleton.ClearCacheBuildingName;
BuildingManager.instance.EventBuildingReleased += WTSCacheSingleton.ClearCacheBuildingName;
BuildingManager.instance.EventBuildingCreated += WTSCacheSingleton.ClearCacheBuildingName;
EventOnDistrictChanged += WTSCacheSingleton.ClearCacheDistrictName;
EventOnParkChanged += WTSCacheSingleton.ClearCacheParkName;
EventOnBuildingNameChanged += WTSCacheSingleton.ClearCacheBuildingName;
EventOnPostalCodeChanged += WTSCacheSingleton.ClearCachePostalCode;
EventOnZeroMarkerChanged += OnNameSeedChanged;
FontServer.instance.m_defaultShader = DEFAULT_SHADER_TEXT;
}
private void OnNameSeedChanged(ushort segmentId) => OnNameSeedChanged();
private void OnNameSeedChanged()
{
WTSCacheSingleton.ClearCacheSegmentNameParam();
WTSCacheSingleton.ClearCachePostalCode();
WTSCacheSingleton.ClearCacheBuildingName(null);
}
public static void ReloadFontsFromPath()
{
FontServer.instance.ResetCollection();
FontServer.instance.RegisterFont(DEFAULT_FONT_KEY, KlyteResourceLoader.LoadResourceData("UI.DefaultFont.SourceSansPro-Regular.ttf"), WTSController.DefaultTextureSizeFont);
foreach (string fontFile in Directory.GetFiles(FontFilesPath, "*.ttf"))
{
FontServer.instance.RegisterFont(Path.GetFileNameWithoutExtension(fontFile), File.ReadAllBytes(fontFile), WTSController.DefaultTextureSizeFont);
}
WriteTheSignsMod.Controller?.EventFontsReloadedFromFolder?.Invoke();
}
public void ReloadAbbreviationFiles()
{
AbbreviationFiles = LoadAbbreviationFiles(AbbreviationFilesPath);
WTSCacheSingleton.ClearCacheSegmentNameParam();
}
private static Dictionary<string, Dictionary<string, string>> LoadAbbreviationFiles(string path)
{
var result = new Dictionary<string, Dictionary<string, string>>();
foreach (string filename in Directory.GetFiles(path, "*.txt").Select(x => x.Split(Path.DirectorySeparatorChar).Last()))
{
string fileContents = File.ReadAllText(path + Path.DirectorySeparatorChar + filename, Encoding.UTF8);
result[filename] = new Dictionary<string, string>();
foreach (string[] entry in fileContents.Split(Environment.NewLine.ToCharArray()).Select(x => x?.Trim()?.Split('=')).Where(x => x != null && x.Length == 2))
{
result[filename][entry[0]] = entry[1];
}
LogUtils.DoLog($"LOADED Files at {path} ({filename}) QTT: {result[filename].Count}");
}
return result;
}
public static readonly string FOLDER_NAME = FileUtils.BASE_FOLDER_PATH + "WriteTheSigns";
public const string m_defaultFileNameBuildingsXml = "WTS_DefaultBuildingsConfig";
public const string m_defaultFileNameVehiclesXml = "WTS_DefaultVehiclesConfig";
public const string m_defaultFileNamePropsXml = "WTS_DefaultPropsConfig";
public const string m_defaultFileNameShieldXml = "WTS_ShieldConfig";
public const string DEFAULT_GAME_PROP_LAYOUT_FOLDER = "PropsDefaultLayouts";
public const string DEFAULT_GAME_BUILDINGS_CONFIG_FOLDER = "BuildingsDefaultPlacing";
public const string DEFAULT_GAME_VEHICLES_CONFIG_FOLDER = "VehiclesDefaultPlacing";
public const string DEFAULT_HW_SHIELDS_CONFIG_FOLDER = "HighwayShieldsLayouts";
public const string ABBREVIATION_FILES_FOLDER = "AbbreviationFiles";
public const string FONTS_FILES_FOLDER = "Fonts";
public const string EXTRA_SPRITES_FILES_FOLDER = "Sprites";
public const string DEFAULT_FONT_KEY = "/DEFAULT/";
public const string EXTRA_SPRITES_FILES_FOLDER_ASSETS = "K45WTS_Sprites";
public static string DefaultPropsLayoutConfigurationFolder { get; } = FOLDER_NAME + Path.DirectorySeparatorChar + DEFAULT_GAME_PROP_LAYOUT_FOLDER;
public static string DefaultBuildingsConfigurationFolder { get; } = FOLDER_NAME + Path.DirectorySeparatorChar + DEFAULT_GAME_BUILDINGS_CONFIG_FOLDER;
public static string DefaultVehiclesConfigurationFolder { get; } = FOLDER_NAME + Path.DirectorySeparatorChar + DEFAULT_GAME_VEHICLES_CONFIG_FOLDER;
public static string DefaultHwShieldsConfigurationFolder { get; } = FOLDER_NAME + Path.DirectorySeparatorChar + DEFAULT_HW_SHIELDS_CONFIG_FOLDER;
public static string ExtraSpritesFolder { get; } = FOLDER_NAME + Path.DirectorySeparatorChar + EXTRA_SPRITES_FILES_FOLDER;
public static string AbbreviationFilesPath { get; } = FOLDER_NAME + Path.DirectorySeparatorChar + ABBREVIATION_FILES_FOLDER;
public static string FontFilesPath { get; } = FOLDER_NAME + Path.DirectorySeparatorChar + FONTS_FILES_FOLDER;
public static Shader DEFAULT_SHADER_TEXT = WTSShaderLibrary.instance.GetShaders()["Klyte/WTS/klytetextboards"];
internal bool? m_tlmExistsAndActive = null;
internal bool? m_addressesExistsAndActive = null;
public static bool ___RELOADSH
{
get => false; set
{
if (value)
{
WTSShaderLibrary.instance.ReloadFromDisk();
DEFAULT_SHADER_TEXT = WTSShaderLibrary.instance.GetShaders()["Klyte/WTS/klytetextboards"];
FontServer.instance.m_defaultShader = WTSShaderLibrary.instance.GetShaders()["Klyte/WTS/klytetextboards"];
ReloadFontsFromPath();
}
}
}
}
}