Actually call on_reset callback
[lsnes.git] / src / core / ui-services.cpp
blobd849e255ceca12a9cc7010709a0a086260666773
1 #include "cmdhelp/lua.hpp"
2 #include "core/advdumper.hpp"
3 #include "core/command.hpp"
4 #include "core/dispatch.hpp"
5 #include "core/instance.hpp"
6 #include "core/moviedata.hpp"
7 #include "core/project.hpp"
8 #include "core/queue.hpp"
9 #include "core/rom.hpp"
10 #include "core/ui-services.hpp"
11 #include "library/keyboard.hpp"
12 #include <functional>
14 namespace
16 void fill_namemap(project_info& p, uint64_t id, std::map<uint64_t, std::string>& namemap,
17 std::map<uint64_t, std::set<uint64_t>>& childmap)
19 namemap[id] = p.get_branch_name(id);
20 auto s = p.branch_children(id);
21 for(auto i : s)
22 fill_namemap(p, i, namemap, childmap);
23 childmap[id] = s;
26 void update_dumperinfo(emulator_instance& inst, std::map<std::string, dumper_information_1>& new_dumpers,
27 dumper_factory_base* d)
29 struct dumper_information_1 inf;
30 inf.factory = d;
31 inf.name = d->name();
32 std::set<std::string> mset = d->list_submodes();
33 for(auto i : mset)
34 inf.modes[i] = d->modename(i);
35 inf.active = inst.mdumper->busy(d);
36 inf.hidden = d->hidden();
37 new_dumpers[d->id()] = inf;
41 void do_flush_slotinfo();
43 void UI_get_branch_map(emulator_instance& inst, uint64_t& cur, std::map<uint64_t, std::string>& namemap,
44 std::map<uint64_t, std::set<uint64_t>>& childmap)
46 auto project = inst.project;
47 inst.iqueue->run([project, &cur, &namemap, &childmap]() {
48 auto p = project->get();
49 if(!p) return;
50 fill_namemap(*p, 0, namemap, childmap);
51 cur = p->get_current_branch();
52 });
55 void UI_call_flush(emulator_instance& inst, std::function<void(std::exception&)> onerror)
57 auto project = inst.project;
58 inst.iqueue->run_async([project]() {
59 auto p = project->get();
60 if(p) p->flush();
61 }, onerror);
64 void UI_create_branch(emulator_instance& inst, uint64_t id, const std::string& name,
65 std::function<void(std::exception&)> onerror)
67 auto project = inst.project;
68 inst.iqueue->run_async([project, id, name]() {
69 auto p = project->get();
70 if(!p) return;
71 p->create_branch(id, name);
72 p->flush();
73 }, onerror);
76 void UI_rename_branch(emulator_instance& inst, uint64_t id, const std::string& name,
77 std::function<void(std::exception&)> onerror)
79 auto project = inst.project;
80 auto supdater = inst.supdater;
81 inst.iqueue->run_async([project, supdater, id, name]() {
82 auto p = project->get();
83 if(!p) return;
84 p->set_branch_name(id, name);
85 p->flush();
86 supdater->update();
87 }, onerror);
90 void UI_reparent_branch(emulator_instance& inst, uint64_t id, uint64_t pid,
91 std::function<void(std::exception&)> onerror)
93 auto project = inst.project;
94 auto supdater = inst.supdater;
95 inst.iqueue->run_async([project, supdater, id, pid]() {
96 auto p = project->get();
97 if(!p) return;
98 p->set_parent_branch(id, pid);
99 p->flush();
100 supdater->update();
101 }, onerror);
104 void UI_delete_branch(emulator_instance& inst, uint64_t id, std::function<void(std::exception&)> onerror)
106 auto project = inst.project;
107 inst.iqueue->run_async([project, id]() {
108 auto p = project->get();
109 if(!p) return;
110 p->delete_branch(id);
111 p->flush();
112 }, onerror);
115 void UI_switch_branch(emulator_instance& inst, uint64_t id, std::function<void(std::exception&)> onerror)
117 auto project = inst.project;
118 auto supdater = inst.supdater;
119 inst.iqueue->run_async([project, supdater, id]() {
120 auto p = project->get();
121 if(!p) return;
122 p->set_current_branch(id);
123 p->flush();
124 supdater->update();
125 }, onerror);
128 project_author_info UI_load_author_info(emulator_instance& inst)
130 project_author_info x;
131 inst.iqueue->run([&inst, &x]() {
132 project_info* proj = inst.project->get();
133 x.is_project = (proj != NULL);
134 x.autorunlua = false;
135 if(proj) {
136 x.projectname = proj->name;
137 x.directory = proj->directory;
138 x.prefix = proj->prefix;
139 x.luascripts = proj->luascripts;
140 x.gamename = proj->gamename;
141 for(auto i : proj->authors)
142 x.authors.push_back(i);
143 } else {
144 x.prefix = get_mprefix_for_project();
145 x.gamename = inst.mlogic->get_mfile().gamename;
146 for(auto i : inst.mlogic->get_mfile().authors)
147 x.authors.push_back(i);
150 return x;
153 void UI_save_author_info(emulator_instance& inst, project_author_info& info)
155 inst.iqueue->run([&inst, info]() {
156 project_info* proj = inst.project->get();
157 std::set<std::string> oldscripts;
158 std::vector<std::pair<std::string, std::string>> _authors(info.authors.begin(), info.authors.end());
159 if(proj) {
160 for(auto i : proj->luascripts)
161 oldscripts.insert(i);
162 proj->gamename = info.gamename;
163 proj->authors = _authors;
164 proj->prefix = info.prefix;
165 proj->directory = info.directory;
166 proj->name = info.projectname;
167 proj->luascripts = info.luascripts;
168 proj->flush();
169 //For save status to immediately update.
170 do_flush_slotinfo();
171 inst.supdater->update();
172 inst.dispatch->title_change();
173 } else {
174 inst.mlogic->get_mfile().gamename = info.gamename;
175 inst.mlogic->get_mfile().authors = _authors;
176 set_mprefix_for_project(info.prefix);
178 if(proj && info.autorunlua)
179 for(auto i : info.luascripts)
180 if(!oldscripts.count(i))
181 inst.command->invoke(CLUA::run.name, i);
185 dumper_information UI_get_dumpers(emulator_instance& inst)
187 dumper_information x;
188 inst.iqueue->run([&inst, &x]() {
189 std::set<dumper_factory_base*> dset = dumper_factory_base::get_dumper_set();
190 for(auto i : dset)
191 update_dumperinfo(inst, x.dumpers, i);
193 return x;
196 void UI_start_dump(emulator_instance& inst, dumper_factory_base& factory, const std::string& mode,
197 const std::string& prefix)
199 inst.iqueue->run([&inst, &factory, mode, prefix]() {
200 inst.mdumper->start(factory, mode, prefix);
204 void UI_end_dump(emulator_instance& inst, dumper_factory_base& factory)
206 inst.iqueue->run([&inst, &factory]() {
207 auto in = inst.mdumper->get_instance(&factory);
208 delete in;
212 void UI_do_keypress(emulator_instance& inst, const keyboard::modifier_set& mods, keyboard::key_key& key,
213 bool polarity)
215 auto _key = &key;
216 inst.iqueue->run_async([mods, _key, polarity]() {
217 _key->set_state(mods, polarity ? 1 : 0);
218 }, [](std::exception& e) {});
221 bool UI_has_movie(emulator_instance& inst)
223 bool ret = false;
224 inst.iqueue->run([&inst, &ret]() {
225 ret = !!*inst.mlogic && !inst.rom->isnull();
227 return ret;
230 void UI_save_movie(emulator_instance& inst, std::ostringstream& stream)
232 inst.iqueue->run([&inst, &stream]() {
233 auto prj = inst.project->get();
234 if(prj) {
235 inst.mlogic->get_mfile().gamename = prj->gamename;
236 inst.mlogic->get_mfile().authors = prj->authors;
238 inst.mlogic->get_mfile().dyn.active_macros.clear();
239 inst.mlogic->get_mfile().save(stream, inst.mlogic->get_rrdata(), false);
243 std::pair<std::string, std::string> UI_lookup_platform_and_game(emulator_instance& inst)
245 std::string plat;
246 std::string game;
247 inst.iqueue->run([&inst, &plat, &game]() {
248 auto prj = inst.project->get();
249 if(prj)
250 game = prj->gamename;
251 else
252 game = inst.mlogic->get_mfile().gamename;
253 plat = lookup_sysregion_mapping(inst.mlogic->get_mfile().gametype->get_name());
255 return std::make_pair(plat, game);
258 std::string UI_get_project_otherpath(emulator_instance& inst)
260 std::string path;
261 inst.iqueue->run([&inst, &path]() {
262 path = inst.project->otherpath();
264 return path;
267 std::string UI_get_project_moviepath(emulator_instance& inst)
269 std::string path;
270 inst.iqueue->run([&inst, &path]() {
271 path = inst.project->moviepath();
273 return path;
276 bool UI_in_project_context(emulator_instance& inst)
278 bool pc;
279 inst.iqueue->run([&inst, &pc]() {
280 pc = (inst.project->get() != NULL);
282 return pc;