1 #include "core/command.hpp"
2 #include "core/dispatch.hpp"
3 #include "core/framebuffer.hpp"
4 #include "core/instance.hpp"
5 #include "core/memorymanip.hpp"
6 #include "core/memorywatch.hpp"
7 #include "core/project.hpp"
8 #include "core/window.hpp"
9 #include "fonts/wrapper.hpp"
10 #include "library/directory.hpp"
11 #include "library/framebuffer-font2.hpp"
12 #include "library/string.hpp"
13 #include "library/int24.hpp"
14 #include "library/mathexpr-ntype.hpp"
15 #include "library/memorywatch.hpp"
16 #include "library/memorywatch-list.hpp"
17 #include "library/memorywatch-fb.hpp"
18 #include "library/memorywatch-null.hpp"
31 std::map
<std::string
, std::pair
<framebuffer::font2
*, size_t>> fonts_in_use
;
33 framebuffer::font2
& get_builtin_font2()
35 static framebuffer::font2
f(main_font
);
39 framebuffer::font2
* get_font(const std::string filename
)
43 return &get_builtin_font2();
44 std::string abs_filename
= directory::absolute_path(filename
);
45 if(fonts_in_use
.count(abs_filename
)) {
46 fonts_in_use
[abs_filename
].second
++;
47 return fonts_in_use
[abs_filename
].first
;
49 framebuffer::font2
* f
= new framebuffer::font2(abs_filename
);
51 fonts_in_use
[abs_filename
] = std::make_pair(f
, 1);
58 void put_font(framebuffer::font2
* font
)
60 //Handle NULL font (always there).
63 //Find font using this.
65 for(auto& i
: fonts_in_use
)
66 if(i
.second
.first
== font
)
70 fonts_in_use
[filename
].second
--;
71 if(!fonts_in_use
[filename
].second
) {
72 delete fonts_in_use
[filename
].first
;
73 fonts_in_use
.erase(filename
);
77 std::string
json_string_default(const JSON::node
& node
, const std::string
& pointer
, const std::string
& dflt
)
79 return (node
.type_of(pointer
) == JSON::string
) ? node
[pointer
].as_string8() : dflt
;
82 uint64_t json_unsigned_default(const JSON::node
& node
, const std::string
& pointer
, uint64_t dflt
)
84 return (node
.type_of(pointer
) == JSON::number
) ? node
[pointer
].as_uint() : dflt
;
87 int64_t json_signed_default(const JSON::node
& node
, const std::string
& pointer
, int64_t dflt
)
89 return (node
.type_of(pointer
) == JSON::number
) ? node
[pointer
].as_int() : dflt
;
92 bool json_boolean_default(const JSON::node
& node
, const std::string
& pointer
, bool dflt
)
94 return (node
.type_of(pointer
) == JSON::boolean
) ? node
[pointer
].as_bool() : dflt
;
97 void dummy_target_fn(const std::string
& n
, const std::string
& v
) {}
100 memwatch_printer::memwatch_printer()
102 position
= PC_MEMORYWATCH
;
104 onscreen_alt_origin_x
= false;
105 onscreen_alt_origin_y
= false;
106 onscreen_cliprange_x
= false;
107 onscreen_cliprange_y
= false;
108 onscreen_fg_color
= 0xFFFFFF;
109 onscreen_bg_color
= -1;
110 onscreen_halo_color
= 0;
113 JSON::node
memwatch_printer::serialize()
115 JSON::node
ndata(JSON::object
);
117 case PC_DISABLED
: ndata
["position"] = JSON::s("disabled"); break;
118 case PC_MEMORYWATCH
: ndata
["position"] = JSON::s("memorywatch"); break;
119 case PC_ONSCREEN
: ndata
["position"] = JSON::s("onscreen"); break;
121 ndata
["cond_enable"] = JSON::b(cond_enable
);
122 ndata
["enabled"] = JSON::s(enabled
);
123 ndata
["onscreen_xpos"] = JSON::s(onscreen_xpos
);
124 ndata
["onscreen_ypos"] = JSON::s(onscreen_ypos
);
125 ndata
["onscreen_alt_origin_x"] = JSON::b(onscreen_alt_origin_x
);
126 ndata
["onscreen_alt_origin_y"] = JSON::b(onscreen_alt_origin_y
);
127 ndata
["onscreen_cliprange_x"] = JSON::b(onscreen_cliprange_x
);
128 ndata
["onscreen_cliprange_y"] = JSON::b(onscreen_cliprange_y
);
129 ndata
["onscreen_font"] = JSON::s(onscreen_font
);
130 ndata
["onscreen_fg_color"] = JSON::i(onscreen_fg_color
);
131 ndata
["onscreen_bg_color"] = JSON::i(onscreen_bg_color
);
132 ndata
["onscreen_halo_color"] = JSON::i(onscreen_halo_color
);
136 void memwatch_printer::unserialize(const JSON::node
& node
)
138 std::string _position
= json_string_default(node
, "position", "");
139 if(_position
== "disabled") position
= PC_DISABLED
;
140 else if(_position
== "memorywatch") position
= PC_MEMORYWATCH
;
141 else if(_position
== "onscreen") position
= PC_ONSCREEN
;
142 else position
= PC_MEMORYWATCH
;
143 cond_enable
= json_boolean_default(node
, "cond_enable", false);
144 enabled
= json_string_default(node
, "enabled", "");
145 onscreen_xpos
= json_string_default(node
, "onscreen_xpos", "");
146 onscreen_ypos
= json_string_default(node
, "onscreen_ypos", "");
147 onscreen_alt_origin_x
= json_boolean_default(node
, "onscreen_alt_origin_x", false);
148 onscreen_alt_origin_y
= json_boolean_default(node
, "onscreen_alt_origin_y", false);
149 onscreen_cliprange_x
= json_boolean_default(node
, "onscreen_cliprange_x", false);
150 onscreen_cliprange_y
= json_boolean_default(node
, "onscreen_cliprange_y", false);
151 onscreen_font
= json_string_default(node
, "onscreen_font", "");
152 onscreen_fg_color
= json_signed_default(node
, "onscreen_fg_color", false);
153 onscreen_bg_color
= json_signed_default(node
, "onscreen_bg_color", false);
154 onscreen_halo_color
= json_signed_default(node
, "onscreen_halo_color", false);
157 gcroot_pointer
<memorywatch::item_printer
> memwatch_printer::get_printer_obj(
158 std::function
<gcroot_pointer
<mathexpr::mathexpr
>(const std::string
& n
)> vars
)
160 gcroot_pointer
<memorywatch::item_printer
> ptr
;
161 memorywatch::output_list
* l
;
162 memorywatch::output_fb
* f
;
164 std::string _enabled
= (enabled
!= "") ? enabled
: "true";
168 ptr
= gcroot_pointer
<memorywatch::item_printer
>(new memorywatch::output_null
);
171 ptr
= gcroot_pointer
<memorywatch::item_printer
>(new memorywatch::output_list
);
172 l
= dynamic_cast<memorywatch::output_list
*>(ptr
.as_pointer());
173 l
->cond_enable
= cond_enable
;
176 l
->enabled
= mathexpr::mathexpr::parse(*mathexpr::expression_value(), _enabled
, vars
);
178 l
->enabled
= mathexpr::mathexpr::parse(*mathexpr::expression_value(), "true", vars
);
179 } catch(std::exception
& e
) {
180 (stringfmt() << "Error while parsing conditional: " << e
.what()).throwex();
182 l
->set_output(dummy_target_fn
);
185 ptr
= gcroot_pointer
<memorywatch::item_printer
>(new memorywatch::output_fb
);
186 f
= dynamic_cast<memorywatch::output_fb
*>(ptr
.as_pointer());
188 f
->set_dtor_cb([](memorywatch::output_fb
& obj
) { put_font(obj
.font
); });
189 f
->cond_enable
= cond_enable
;
190 std::string while_parsing
= "(unknown)";
192 while_parsing
= "conditional";
194 f
->enabled
= mathexpr::mathexpr::parse(*mathexpr::expression_value(), _enabled
, vars
);
196 f
->enabled
= mathexpr::mathexpr::parse(*mathexpr::expression_value(), "true", vars
);
197 while_parsing
= "X position";
198 f
->pos_x
= mathexpr::mathexpr::parse(*mathexpr::expression_value(), onscreen_xpos
, vars
);
199 while_parsing
= "Y position";
200 f
->pos_y
= mathexpr::mathexpr::parse(*mathexpr::expression_value(), onscreen_ypos
, vars
);
201 } catch(std::exception
& e
) {
202 (stringfmt() << "Error while parsing " << while_parsing
<< ": " << e
.what()).throwex();
204 f
->alt_origin_x
= onscreen_alt_origin_x
;
205 f
->alt_origin_y
= onscreen_alt_origin_y
;
206 f
->cliprange_x
= onscreen_cliprange_x
;
207 f
->cliprange_y
= onscreen_cliprange_y
;
208 f
->fg
= onscreen_fg_color
;
209 f
->bg
= onscreen_bg_color
;
210 f
->halo
= onscreen_halo_color
;
212 f
->font
= get_font(onscreen_font
);
213 } catch(std::exception
& e
) {
214 messages
<< "Bad font '" << onscreen_font
<< "': " << e
.what() << std::endl
;
215 f
->font
= &get_builtin_font2();
222 memwatch_item::memwatch_item()
231 mspace
= &CORE().memory
;
234 JSON::node
memwatch_item::serialize()
236 JSON::node
ndata(JSON::object
);
237 ndata
["printer"] = printer
.serialize();
238 ndata
["expr"] = JSON::s(expr
);
239 ndata
["format"] = JSON::s(format
);
240 ndata
["bytes"] = JSON::u(bytes
);
241 ndata
["signed"] = JSON::b(signed_flag
);
242 ndata
["float"] = JSON::b(float_flag
);
243 ndata
["endianess"] = JSON::i(endianess
);
244 ndata
["scale_div"] = JSON::u(scale_div
);
245 ndata
["addr_base"] = JSON::u(addr_base
);
246 ndata
["addr_size"] = JSON::u(addr_size
);
250 void memwatch_item::unserialize(const JSON::node
& node
)
252 if(node
.type_of("printer") == JSON::object
)
253 printer
.unserialize(node
["printer"]);
255 printer
= memwatch_printer();
256 expr
= json_string_default(node
, "expr", "0");
257 format
= json_string_default(node
, "format", "");
258 bytes
= json_unsigned_default(node
, "bytes", 0);
259 signed_flag
= json_boolean_default(node
, "signed", false);
260 float_flag
= json_boolean_default(node
, "float", false);
261 endianess
= json_signed_default(node
, "endianess", false);
262 scale_div
= json_unsigned_default(node
, "scale_div", 1);
263 addr_base
= json_unsigned_default(node
, "addr_base", 0);
264 addr_size
= json_unsigned_default(node
, "addr_size", 0);
267 memorywatch::memread_oper
* memwatch_item::get_memread_oper()
271 memorywatch::memread_oper
* o
= new memorywatch::memread_oper
;
273 o
->signed_flag
= signed_flag
;
274 o
->float_flag
= float_flag
;
275 o
->endianess
= endianess
;
276 o
->scale_div
= scale_div
;
277 o
->addr_base
= addr_base
;
278 o
->addr_size
= addr_size
;
283 void memwatch_item::compatiblity_unserialize(const std::string
& item
)
286 if(!(r
= regex("C0x([0-9A-Fa-f]{1,16})z([bBwWoOdDqQfF])(H([0-9A-Ga-g]))?", item
)))
287 throw std::runtime_error("Unknown compatiblity memory watch");
288 std::string _addr
= r
[1];
289 std::string _type
= r
[2];
290 std::string _hext
= r
[4];
291 uint64_t addr
= strtoull(_addr
.c_str(), NULL
, 16);
292 char type
= _type
[0];
293 char hext
= (_hext
!= "") ? _hext
[0] : 0;
295 case 'b': bytes
= 1; signed_flag
= true; float_flag
= false; break;
296 case 'B': bytes
= 1; signed_flag
= false; float_flag
= false; break;
297 case 'w': bytes
= 2; signed_flag
= true; float_flag
= false; break;
298 case 'W': bytes
= 2; signed_flag
= false; float_flag
= false; break;
299 case 'o': bytes
= 3; signed_flag
= true; float_flag
= false; break;
300 case 'O': bytes
= 3; signed_flag
= false; float_flag
= false; break;
301 case 'd': bytes
= 4; signed_flag
= true; float_flag
= false; break;
302 case 'D': bytes
= 4; signed_flag
= false; float_flag
= false; break;
303 case 'q': bytes
= 8; signed_flag
= true; float_flag
= false; break;
304 case 'Q': bytes
= 8; signed_flag
= false; float_flag
= false; break;
305 case 'f': bytes
= 4; signed_flag
= true; float_flag
= true; break;
306 case 'F': bytes
= 8; signed_flag
= true; float_flag
= true; break;
307 default: bytes
= 0; break;
309 auto mdata
= CORE().memory
.lookup(addr
);
312 addr_base
= mdata
.first
->base
;
313 addr_size
= mdata
.first
->size
;
314 endianess
= mdata
.first
->endian
;
322 if(hext
>= '0' && hext
<= '9')
325 width
= (hext
& 0x1F) + 9;
326 format
= (stringfmt() << "%0" << width
<< "x").str();
329 expr
= (stringfmt() << "0x" << std::hex
<< addr
).str();
331 mspace
= &CORE().memory
;
332 printer
.position
= memwatch_printer::PC_MEMORYWATCH
;
333 printer
.cond_enable
= false;
334 printer
.enabled
= "true";
335 printer
.onscreen_xpos
= "0";
336 printer
.onscreen_ypos
= "0";
337 printer
.onscreen_alt_origin_x
= false;
338 printer
.onscreen_alt_origin_y
= false;
339 printer
.onscreen_cliprange_x
= false;
340 printer
.onscreen_cliprange_y
= false;
341 printer
.onscreen_font
= "";
342 printer
.onscreen_fg_color
= 0xFFFFFF;
343 printer
.onscreen_bg_color
= -1;
344 printer
.onscreen_halo_color
= 0;
347 std::set
<std::string
> memwatch_set::enumerate()
349 std::set
<std::string
> r
;
355 void memwatch_set::clear(const std::string
& name
)
357 std::map
<std::string
, memwatch_item
> nitems
= items
;
360 std::swap(items
, nitems
);
361 auto pr
= CORE().project
.get();
363 pr
->watches
.erase(name
);
366 CORE().fbuf
.redraw_framebuffer();
369 void memwatch_set::set(const std::string
& name
, const std::string
& item
)
372 if(item
!= "" && item
[0] != '{') {
375 _item
.compatiblity_unserialize(item
);
376 } catch(std::exception
& e
) {
377 messages
<< "Can't handle old memory watch '" << name
<< "'" << std::endl
;
381 _item
.unserialize(JSON::node(item
));
385 memwatch_item
& memwatch_set::get(const std::string
& name
)
387 if(!items
.count(name
))
388 throw std::runtime_error("No such memory watch named '" + name
+ "'");
392 std::string
memwatch_set::get_string(const std::string
& name
, JSON::printer
* printer
)
395 auto y
= x
.serialize();
396 auto z
= y
.serialize(printer
);
400 void memwatch_set::watch(struct framebuffer::queue
& rq
)
402 //Set framebuffer for all FB watches.
403 watch_set
.foreach([&rq
](memorywatch::item
& i
) {
404 memorywatch::output_fb
* fb
= dynamic_cast<memorywatch::output_fb
*>(i
.printer
.as_pointer());
409 erase_unused_watches();
412 bool memwatch_set::rename(const std::string
& oldname
, const std::string
& newname
)
414 std::map
<std::string
, memwatch_item
> nitems
= items
;
415 if(nitems
.count(newname
))
417 if(!nitems
.count(oldname
))
419 nitems
[newname
] = nitems
[oldname
];
420 nitems
.erase(oldname
);
422 std::swap(items
, nitems
);
423 CORE().fbuf
.redraw_framebuffer();
427 void memwatch_set::set(const std::string
& name
, memwatch_item
& item
)
429 std::map
<std::string
, memwatch_item
> nitems
= items
;
432 std::swap(items
, nitems
);
433 auto pr
= CORE().project
.get();
435 pr
->watches
[name
] = get_string(name
);
438 CORE().fbuf
.redraw_framebuffer();
441 std::string
memwatch_set::get_value(const std::string
& name
)
443 return watch_set
.get(name
).get_value();
446 void memwatch_set::set_multi(std::list
<std::pair
<std::string
, memwatch_item
>>& list
)
448 std::map
<std::string
, memwatch_item
> nitems
= items
;
450 nitems
[i
.first
] = i
.second
;
452 std::swap(items
, nitems
);
453 auto pr
= CORE().project
.get();
456 pr
->watches
[i
.first
] = get_string(i
.first
);
459 CORE().fbuf
.redraw_framebuffer();
462 void memwatch_set::set_multi(std::list
<std::pair
<std::string
, std::string
>>& list
)
464 std::list
<std::pair
<std::string
, memwatch_item
>> _list
;
467 it
.unserialize(JSON::node(i
.second
));
468 _list
.push_back(std::make_pair(i
.first
, it
));
473 void memwatch_set::clear_multi(const std::set
<std::string
>& names
)
475 std::map
<std::string
, memwatch_item
> nitems
= items
;
479 std::swap(items
, nitems
);
480 auto pr
= CORE().project
.get();
483 pr
->watches
.erase(i
);
486 CORE().fbuf
.redraw_framebuffer();
489 void memwatch_set::rebuild(std::map
<std::string
, memwatch_item
>& nitems
)
492 memorywatch::set new_set
;
493 std::map
<std::string
, gcroot_pointer
<mathexpr::mathexpr
>> vars
;
494 auto vars_fn
= [&vars
](const std::string
& n
) -> gcroot_pointer
<mathexpr::mathexpr
> {
496 vars
[n
] = gcroot_pointer
<mathexpr::mathexpr
>(gcroot_pointer_object_tag(),
497 mathexpr::expression_value());
500 for(auto& i
: nitems
) {
501 mathexpr::operinfo
* memread_oper
= i
.second
.get_memread_oper();
503 gcroot_pointer
<mathexpr::mathexpr
> rt_expr
;
504 gcroot_pointer
<memorywatch::item_printer
> rt_printer
;
505 std::vector
<gcroot_pointer
<mathexpr::mathexpr
>> v
;
507 rt_expr
= mathexpr::mathexpr::parse(*mathexpr::expression_value(),
508 i
.second
.expr
, vars_fn
);
509 } catch(std::exception
& e
) {
510 (stringfmt() << "Error while parsing address/expression: "
511 << e
.what()).throwex();
513 v
.push_back(rt_expr
);
515 rt_expr
= gcroot_pointer
<mathexpr::mathexpr
>(gcroot_pointer_object_tag(),
516 mathexpr::expression_value(), memread_oper
, v
, true);
519 rt_printer
= i
.second
.printer
.get_printer_obj(vars_fn
);
521 //Set final callback for list objects (since it wasn't known on creation).
522 auto list_obj
= dynamic_cast<memorywatch::output_list
*>(rt_printer
.as_pointer());
524 list_obj
->set_output([this](const std::string
& n
, const std::string
& v
) {
525 this->watch_output(n
, v
);
528 memorywatch::item
it(*mathexpr::expression_value());
529 *vars_fn(i
.first
) = *rt_expr
;
530 it
.expr
= vars_fn(i
.first
);
531 it
.printer
= rt_printer
;
532 it
.format
= i
.second
.format
;
533 new_set
.create(i
.first
, it
);
539 watch_set
.swap(new_set
);
541 garbage_collectable::do_gc();
544 void memwatch_set::watch_output(const std::string
& name
, const std::string
& value
)
546 used_memorywatches
[name
] = true;
547 window_vars
[name
] = utf8::to32(value
);
550 void memwatch_set::erase_unused_watches()
552 for(auto& i
: used_memorywatches
) {
554 window_vars
.erase(i
.first
);