/
unitsync.h
244 lines (185 loc) · 7.84 KB
/
unitsync.h
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* This file is part of the Springlobby (GPL v2 or later), see COPYING */
#ifndef LIBSPRINGLOBBY_HEADERGUARD_SPRINGUNITSYNC_H
#define LIBSPRINGLOBBY_HEADERGUARD_SPRINGUNITSYNC_H
#include "mmoptionmodel.h"
#include "data.h"
#include <lslutils/type_forwards.h>
#include "image.h"
#include <boost/signals2/signal.hpp>
#include <mutex>
#include <map>
#include <set>
#ifdef HAVE_WX
#include <wx/event.h>
#endif
namespace LSL
{
class UnitsyncImage;
struct GameOptions;
struct CachedMapInfo;
struct SpringMapInfo;
class UnitsyncLib;
class WorkerThread;
#ifdef HAVE_WX
extern const wxEventType UnitSyncAsyncOperationCompletedEvt;
#endif
enum ImageType {
IMAGE_MAP = 0,
IMAGE_MAP_THUMB,
IMAGE_METALMAP,
IMAGE_HEIGHTMAP,
};
class Unitsync
{
private:
typedef boost::signals2::signal<void(std::string)>
StringSignalType;
public:
typedef StringSignalType::slot_type
StringSignalSlotType;
Unitsync();
Unitsync(const Unitsync&) = delete;
Unitsync& operator=(const Unitsync&) = delete;
~Unitsync();
StringVector GetGameList() const;
bool GameExists(const std::string& gamename, const std::string& hash = "");
UnitsyncGame GetGame(const std::string& gamename);
UnitsyncGame GetGame(int index);
GameOptions GetGameOptions(const std::string& name);
StringVector GetGameDeps(const std::string& name) const;
StringVector GetMapList() const;
StringVector GetGameValidMapList(const std::string& gamename) const;
bool MapExists(const std::string& mapname, const std::string& hash = "");
UnitsyncMap GetMap(const std::string& mapname);
UnitsyncMap GetMap(int index);
GameOptions GetMapOptions(const std::string& name);
StringVector GetSides(const std::string& gamename);
UnitsyncImage GetSidePicture(const std::string& gamename, const std::string& SideName);
bool LoadUnitSyncLib(const std::string& unitsyncloc);
void FreeUnitSyncLib();
bool IsLoaded() const;
std::string GetSpringVersion() const;
void UnSetCurrentArchive();
StringVector GetAIList(const std::string& gamename) const;
StringVector GetAIInfos(int index) const;
GameOptions GetAIOptions(const std::string& gamename, int index);
StringVector GetUnitsList(const std::string& gamename);
bool ReloadUnitSyncLib();
void SetSpringDataPath(const std::string& path);
bool GetSpringDataPath(std::string& path);
std::string GetMac();
std::string GetSys();
bool GetPlaybackList(std::set<std::string>& files, bool ReplayType = true) const; //savegames otehrwise
std::string GetArchivePath(const std::string& name) const;
/// schedule a map for prefetching
void PrefetchMap(const std::string& mapname);
void PrefetchGame(const std::string& gamename);
boost::signals2::connection RegisterEvtHandler(const StringSignalSlotType& handler);
void UnregisterEvtHandler(boost::signals2::connection& conn);
void PostEvent(const std::string& evt); // helper for WorkItems
void LoadUnitSyncLibAsync(const std::string& filename);
int GetSpringConfigInt(const std::string& name, int defvalue);
float GetSpringConfigFloat(const std::string& name, float defvalue);
std::string GetSpringConfigString(const std::string& name, const std::string& defvalue);
void SetSpringConfigInt(const std::string& name, int value);
void SetSpringConfigString(const std::string& name, const std::string& value);
void SetSpringConfigFloat(const std::string& name, float value);
std::string GetConfigFilePath();
void GetMapExAsync(const std::string& mapname);
void GetMapImageAsync(const std::string& mapname, ImageType imgtype, int width, int height);
//! get a map image, if width/height is set, scale it to the given dimensions
UnitsyncImage GetScaledMapImage(const std::string& mapname, ImageType imgtype, int width = -1, int height = -1);
/// fetch all errors from unitsync and push to our error handling
void FetchUnitsyncErrors(const std::string& prefix);
//! returns the absolute path of the requested item, creates the file when not exists
std::string GetMapImagePath(const std::string& mapname, ImageType imgtype) const;
std::string GetMapOptionsPath(const std::string& mapname) const;
std::string GetMapInfoPath(const std::string& mapname) const;
std::string GetGameOptionsPath(const std::string& name) const;
std::string GetSidesCachePath(const std::string& gamename) const;
std::string GetSideImageCachePath(const std::string& gamename, const std::string sidename) const;
std::string GetUnitsCacheFilePath(const std::string& gamename) const;
private:
std::string GetMapHash(const std::string& name);
std::string GetGameHash(const std::string& name);
bool GetImageFromCache(const std::string& cachefile, UnitsyncImage& img, ImageType imgtype);
UnitsyncImage GetImageFromUS(const std::string& mapname, const MapInfo& info, ImageType imgtype);
void ClearCache();
void GetSpringDataPaths();
/// get minimap with native width x height
UnitsyncImage GetMinimap(const std::string& mapname);
/// get metalmap with native width x height
UnitsyncImage GetMetalmap(const std::string& mapname);
/// get heightmap with native width x height
UnitsyncImage GetHeightmap(const std::string& mapname);
bool FileExists(const std::string& name) const;
std::string GetTextfileAsString(const std::string& gamename, const std::string& file_path);
StringVector GetMapDeps(const std::string& name);
UnitsyncImage GetImage(const std::string& image_path, bool useWhiteAsTransparent = true) const;
LocalArchivesVector m_maps_list; /// mapname -> hash
LocalArchivesVector m_mods_list; /// gamename -> hash
LocalArchivesVector m_mods_archive_name; /// gamename -> archive name
LocalArchivesVector m_maps_archive_name; /// mapname -> archive name
StringVector m_map_array; // this vector is CUSTOM SORTED ALPHABETICALLY, DON'T USE TO ACCESS UNITSYNC DIRECTLY
StringVector m_mod_array; // this vector is CUSTOM SORTED ALPHABETICALLY, DON'T USE TO ACCESS UNITSYNC DIRECTLY
StringVector m_unsorted_map_array; // this is because unitsync doesn't have a search map index by name ..
StringVector m_unsorted_mod_array; // this isn't necessary but makes things more symmetrical :P
StringVector m_datapaths;
/// caches sett().GetCachePath(), because that method calls back into
/// susynclib(), there's a good chance main thread blocks on some
/// WorkerThread operation... cache is invalidated on reload.
std::string m_cache_path;
std::map<std::string, GameOptions> m_map_gameoptions;
std::map<std::string, GameOptions> m_game_gameoptions;
mutable std::mutex m_lock;
WorkerThread* m_cache_thread;
StringSignalType m_async_ops_complete_sig;
//! this function returns only the cache path without the file extension,
//! the extension itself would be added in the function as needed
std::string GetFileCachePath(const std::string& archivename, bool IsGame, bool usehash = true) const;
bool _LoadUnitSyncLib(const std::string& unitsyncloc);
void _FreeUnitSyncLib();
MapInfo _GetMapInfoEx(const std::string& mapname);
void PopulateArchiveList();
friend Unitsync& usync();
bool supportsManualUnLoad;
};
Unitsync& usync();
struct GameOptions
{
OptionMapBool bool_map;
OptionMapFloat float_map;
OptionMapString string_map;
OptionMapList list_map;
OptionMapSection section_map;
};
/// Helper class for managing async operations safely
class UnitSyncAsyncOps : public boost::noncopyable
{
public:
UnitSyncAsyncOps(const Unitsync::StringSignalSlotType& evtHandler)
: m_evtHandler_connection()
{
m_evtHandler_connection = usync().RegisterEvtHandler(evtHandler);
}
~UnitSyncAsyncOps()
{
Disconnect();
}
void Disconnect()
{
usync().UnregisterEvtHandler(m_evtHandler_connection);
}
bool Connected()
{
return m_evtHandler_connection.connected();
}
void GetMapImageAsync(const std::string& mapname, ImageType imgtype, int width, int height)
{
usync().GetMapImageAsync(mapname, imgtype, width, height);
}
private:
boost::signals2::connection m_evtHandler_connection;
};
} // namespace LSL
#endif // LIBSPRINGLOBBY_HEADERGUARD_SPRINGUNITSYNC_H