forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cpp
More file actions
247 lines (225 loc) · 6.21 KB
/
Copy pathCore.cpp
File metadata and controls
247 lines (225 loc) · 6.21 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
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
245
246
247
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <sstream>
using namespace std;
#include "dfhack/Error.h"
#include "dfhack/Process.h"
#include "dfhack/Core.h"
#include "dfhack/Console.h"
#include "dfhack/VersionInfoFactory.h"
#include "dfhack/PluginManager.h"
#include "ModuleFactory.h"
#include "dfhack/modules/Gui.h"
#include "dfhack/modules/Vegetation.h"
#include "dfhack/modules/Maps.h"
#include "dfhack/modules/World.h"
#include <stdio.h>
using namespace DFHack;
void cheap_tokenise(string const& input, vector<string> &output)
{
istringstream str(input);
istream_iterator<string> cur(str), end;
output.assign(cur, end);
}
struct IODATA
{
Core * core;
PluginManager * plug_mgr;
};
int fIOthread(void * iodata)
{
Core * core = ((IODATA*) iodata)->core;
PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr;
if(plug_mgr == 0)
{
dfout << "Something horrible happened to the plugin manager in Core's constructor..." << std::endl;
return 0;
}
fprintf(dfout_C,"DFHack is ready. Have a nice day! Type in '?' or 'help' for help.\n");
//dfterm << << endl;
int clueless_counter = 0;
while (true)
{
string command = "";
dfout <<"[DFHack]# ";
getline(cin, command);
if (cin.eof())
{
command = "q";
dfout << std::endl; // No newline from the user here!
}
if(command=="help" || command == "?")
{
dfout << "Available commands:" << endl;
}
else if( command == "" )
{
clueless_counter++;
}
else
{
vector <string> parts;
cheap_tokenise(command,parts);
if(parts.size() == 0)
{
clueless_counter++;
}
else
{
string first = parts[0];
parts.erase(parts.begin());
command_result res = plug_mgr->InvokeCommand(first, parts);
if(res == CR_NOT_IMPLEMENTED)
{
dfout << "Invalid command." << endl;
clueless_counter ++;
}
else if(res == CR_FAILURE)
{
dfout << "ERROR!" << endl;
}
}
}
if(clueless_counter == 3)
{
dfout << "Do 'help' or '?' for the list of available commands." << endl;
clueless_counter = 0;
}
}
}
Core::Core()
{
// init the console. This must be always the first step!
con = new Console();
plug_mgr = 0;
// find out what we are...
vif = new DFHack::VersionInfoFactory("Memory.xml");
p = new DFHack::Process(vif);
if (!p->isIdentified())
{
dfout << "Couldn't identify this version of DF." << std::endl;
errorstate = true;
delete p;
p = NULL;
return;
}
vinfo = p->getDescriptor();
// init module storage
allModules.clear();
memset(&(s_mods), 0, sizeof(s_mods));
// create mutex for syncing with interactive tasks
AccessMutex = SDL_CreateMutex();
if(!AccessMutex)
{
dfout << "Mutex creation failed." << std::endl;
errorstate = true;
return;
}
// all OK
errorstate = false;
// lock mutex
SDL_mutexP(AccessMutex);
plug_mgr = new PluginManager(this);
// look for all plugins,
// create IO thread
IODATA *temp = new IODATA;
temp->core = this;
temp->plug_mgr = plug_mgr;
DFThread * IO = SDL_CreateThread(fIOthread, (void *) temp);
delete temp;
// and let DF do its thing.
};
void Core::Suspend()
{
SDL_mutexP(AccessMutex);
}
void Core::Resume()
{
for(unsigned int i = 0 ; i < allModules.size(); i++)
{
allModules[i]->OnResume();
}
SDL_mutexV(AccessMutex);
}
int Core::Update()
{
if(errorstate)
return -1;
// do persistent stuff here
SDL_mutexV(AccessMutex);
// other threads can claim the mutex here and use DFHack.
// NO CODE SHOULD EVER BE PLACED HERE
SDL_mutexP(AccessMutex);
return 0;
};
int Core::Shutdown ( void )
{
errorstate = 1;
if(plug_mgr)
{
delete plug_mgr;
plug_mgr = 0;
}
// invalidate all modules
for(unsigned int i = 0 ; i < allModules.size(); i++)
{
delete allModules[i];
}
allModules.clear();
memset(&(s_mods), 0, sizeof(s_mods));
dfout << std::endl;
// kill the console object
delete con;
con = 0;
return -1;
}
/*******************************************************************************
M O D U L E S
*******************************************************************************/
#define MODULE_GETTER(TYPE) \
TYPE * Core::get##TYPE() \
{ \
if(errorstate) return NULL;\
if(!s_mods.p##TYPE)\
{\
Module * mod = create##TYPE();\
s_mods.p##TYPE = (TYPE *) mod;\
allModules.push_back(mod);\
}\
return s_mods.p##TYPE;\
}
MODULE_GETTER(Creatures);
MODULE_GETTER(Engravings);
MODULE_GETTER(Maps);
MODULE_GETTER(Gui);
MODULE_GETTER(World);
MODULE_GETTER(Materials);
MODULE_GETTER(Items);
MODULE_GETTER(Translation);
MODULE_GETTER(Vegetation);
MODULE_GETTER(Buildings);
MODULE_GETTER(Constructions);