OSDN Git Service

VS 1.17 Hot-Fix, minor tweaks
[automap/automap.git] / Automap / AutomapMod.cs
1 using System.Collections.ObjectModel;
2 using System.Linq;
3
4 using Vintagestory.API.Client;
5 using Vintagestory.API.Common;
6
7
8 namespace Automap
9 {
10     public class AutomapMod : ModSystem
11     {
12                 public const string _configFilename = @"automap.json";
13
14         private ICoreAPI API { get; set; }
15         private ICoreClientAPI ClientAPI { get; set; }
16         private ILogger Logger { get; set; }
17         private AutomapSystem _localAutomap;
18         private AutomapGUIDialog _automapDialog;
19
20
21         public override bool ShouldLoad(EnumAppSide forSide)
22         {
23             return forSide.IsClient();
24         }
25
26                 public override void StartClientSide(ICoreClientAPI api)
27                 {
28                         this.API = api;
29
30                         if (api.Side == EnumAppSide.Client)
31                         {
32                                 this.ClientAPI = api as ICoreClientAPI;
33                                 this.Logger = Mod.Logger;
34
35                                 ClientAPI.Logger.VerboseDebug("Automap Present!");
36                                 PrepareClientsideConfig( );
37                                 _localAutomap = new AutomapSystem(this.ClientAPI, this.Logger,  this.CachedConfiguration);
38
39                                 ClientAPI.Input.RegisterHotKey(AutomapGUIDialog._automapControlPanelKey, "Automap control panel", GlKeys.M, HotkeyType.GUIOrOtherControls, shiftPressed: true);
40                                 ClientAPI.Input.SetHotKeyHandler(AutomapGUIDialog._automapControlPanelKey, ToggleAM_Dialog);
41
42                                 ClientAPI.Event.LeaveWorld += PersistParameterChange;
43                         }
44
45             base.StartClientSide(api);
46         }
47
48                 public override double ExecuteOrder()
49                 {
50                         return 1.2;
51                 }
52
53                 private bool ToggleAM_Dialog(KeyCombination comb)
54                 {
55                         _automapDialog = _automapDialog  ?? new AutomapGUIDialog(ClientAPI, _localAutomap, this.CachedConfiguration);
56                         if (_automapDialog.IsOpened()) _automapDialog.TryClose();
57                         else _automapDialog.TryOpen();
58
59                         return true;
60                 }
61
62                 internal PersistedConfiguration CachedConfiguration {
63                         get
64                         {
65                         return ( PersistedConfiguration )ClientAPI.ObjectCache[_configFilename];
66                         }
67                         set
68                         {
69                         ClientAPI.ObjectCache.Add(_configFilename, value);
70                         }
71                 }
72
73                 private void PrepareClientsideConfig( )
74                 {
75                 PersistedConfiguration config = ClientAPI.LoadModConfig<PersistedConfiguration>(_configFilename);
76
77                 if (config == null) {
78                 //Regen default
79                 Mod.Logger.Warning("Regenerating default config as it was missing / unparsable...");
80                 ClientAPI.StoreModConfig<PersistedConfiguration>(new PersistedConfiguration(defaults: true ), _configFilename);
81                 config = ClientAPI.LoadModConfig<PersistedConfiguration>(_configFilename);
82                 }
83
84                 this.CachedConfiguration = config;
85                 }
86
87                 public override void Dispose( )
88                 {
89                 //TAKE OUT THE TRASH!
90                 //_localAutomap = null;
91                 _automapDialog.Dispose( );
92                 _automapDialog = null;
93
94                 base.Dispose( );
95                 }
96
97                 internal void PersistParameterChange( )
98                 {
99                 //Store altered parameters
100
101                 ClientAPI.StoreModConfig<PersistedConfiguration>(this.CachedConfiguration, _configFilename);
102                 }
103
104                 #region External Interfaces
105                 //Perhaps Other mods can make use of POI / EOI?
106
107                 public ReadOnlyCollection<PointOfInterest> PointsOfInterest {
108                         get 
109                         {
110                                 return _localAutomap.POIs.ToList().AsReadOnly();
111                         }
112                 }
113
114                 public ReadOnlyCollection<EntityOfInterest> EntitiesOfInterest {
115                         get 
116                         {
117                                 return _localAutomap.EOIs.ToList().AsReadOnly();
118                         }
119                 }
120
121                 public CommandType Status
122                 {
123                         get { return _localAutomap.CurrentState; }
124                 }
125
126                 #endregion
127
128
129         }
130 }
131