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"
15 void fill_namemap(project_info
& p
, uint64_t id
, std::map
<uint64_t, std::string
>& namemap
,
16 std::map
<uint64_t, std::set
<uint64_t>>& childmap
)
18 namemap
[id
] = p
.get_branch_name(id
);
19 auto s
= p
.branch_children(id
);
21 fill_namemap(p
, i
, namemap
, childmap
);
25 void update_dumperinfo(emulator_instance
& inst
, std::map
<std::string
, dumper_information_1
>& new_dumpers
,
26 dumper_factory_base
* d
)
28 struct dumper_information_1 inf
;
31 std::set
<std::string
> mset
= d
->list_submodes();
33 inf
.modes
[i
] = d
->modename(i
);
34 inf
.active
= inst
.mdumper
->busy(d
);
35 new_dumpers
[d
->id()] = inf
;
39 void do_flush_slotinfo();
41 void UI_get_branch_map(emulator_instance
& inst
, uint64_t& cur
, std::map
<uint64_t, std::string
>& namemap
,
42 std::map
<uint64_t, std::set
<uint64_t>>& childmap
)
44 auto project
= inst
.project
;
45 inst
.iqueue
->run([project
, &cur
, &namemap
, &childmap
]() {
46 auto p
= project
->get();
48 fill_namemap(*p
, 0, namemap
, childmap
);
49 cur
= p
->get_current_branch();
53 void UI_call_flush(emulator_instance
& inst
, std::function
<void(std::exception
&)> onerror
)
55 auto project
= inst
.project
;
56 inst
.iqueue
->run_async([project
]() {
57 auto p
= project
->get();
62 void UI_create_branch(emulator_instance
& inst
, uint64_t id
, const std::string
& name
,
63 std::function
<void(std::exception
&)> onerror
)
65 auto project
= inst
.project
;
66 inst
.iqueue
->run_async([project
, id
, name
]() {
67 auto p
= project
->get();
69 p
->create_branch(id
, name
);
74 void UI_rename_branch(emulator_instance
& inst
, uint64_t id
, const std::string
& name
,
75 std::function
<void(std::exception
&)> onerror
)
77 auto project
= inst
.project
;
78 auto supdater
= inst
.supdater
;
79 inst
.iqueue
->run_async([project
, supdater
, id
, name
]() {
80 auto p
= project
->get();
82 p
->set_branch_name(id
, name
);
88 void UI_reparent_branch(emulator_instance
& inst
, uint64_t id
, uint64_t pid
,
89 std::function
<void(std::exception
&)> onerror
)
91 auto project
= inst
.project
;
92 auto supdater
= inst
.supdater
;
93 inst
.iqueue
->run_async([project
, supdater
, id
, pid
]() {
94 auto p
= project
->get();
96 p
->set_parent_branch(id
, pid
);
102 void UI_delete_branch(emulator_instance
& inst
, uint64_t id
, std::function
<void(std::exception
&)> onerror
)
104 auto project
= inst
.project
;
105 inst
.iqueue
->run_async([project
, id
]() {
106 auto p
= project
->get();
108 p
->delete_branch(id
);
113 void UI_switch_branch(emulator_instance
& inst
, uint64_t id
, std::function
<void(std::exception
&)> onerror
)
115 auto project
= inst
.project
;
116 auto supdater
= inst
.supdater
;
117 inst
.iqueue
->run_async([project
, supdater
, id
]() {
118 auto p
= project
->get();
120 p
->set_current_branch(id
);
126 project_author_info
UI_load_author_info(emulator_instance
& inst
)
128 project_author_info x
;
129 inst
.iqueue
->run([&inst
, &x
]() {
130 project_info
* proj
= inst
.project
->get();
131 x
.is_project
= (proj
!= NULL
);
132 x
.autorunlua
= false;
134 x
.projectname
= proj
->name
;
135 x
.directory
= proj
->directory
;
136 x
.prefix
= proj
->prefix
;
137 x
.luascripts
= proj
->luascripts
;
138 x
.gamename
= proj
->gamename
;
139 for(auto i
: proj
->authors
)
140 x
.authors
.push_back(i
);
142 x
.prefix
= get_mprefix_for_project();
143 x
.gamename
= inst
.mlogic
->get_mfile().gamename
;
144 for(auto i
: inst
.mlogic
->get_mfile().authors
)
145 x
.authors
.push_back(i
);
151 void UI_save_author_info(emulator_instance
& inst
, project_author_info
& info
)
153 inst
.iqueue
->run([&inst
, info
]() {
154 project_info
* proj
= inst
.project
->get();
155 std::set
<std::string
> oldscripts
;
156 std::vector
<std::pair
<std::string
, std::string
>> _authors(info
.authors
.begin(), info
.authors
.end());
158 for(auto i
: proj
->luascripts
)
159 oldscripts
.insert(i
);
160 proj
->gamename
= info
.gamename
;
161 proj
->authors
= _authors
;
162 proj
->prefix
= info
.prefix
;
163 proj
->directory
= info
.directory
;
164 proj
->name
= info
.projectname
;
165 proj
->luascripts
= info
.luascripts
;
167 //For save status to immediately update.
169 inst
.supdater
->update();
170 inst
.dispatch
->title_change();
172 inst
.mlogic
->get_mfile().gamename
= info
.gamename
;
173 inst
.mlogic
->get_mfile().authors
= _authors
;
174 set_mprefix_for_project(info
.prefix
);
176 if(proj
&& info
.autorunlua
)
177 for(auto i
: info
.luascripts
)
178 if(!oldscripts
.count(i
))
179 inst
.command
->invoke(CLUA::run
.name
, i
);
183 dumper_information
UI_get_dumpers(emulator_instance
& inst
)
185 dumper_information x
;
186 inst
.iqueue
->run([&inst
, &x
]() {
187 std::set
<dumper_factory_base
*> dset
= dumper_factory_base::get_dumper_set();
189 update_dumperinfo(inst
, x
.dumpers
, i
);
194 void UI_start_dump(emulator_instance
& inst
, dumper_factory_base
& factory
, const std::string
& mode
,
195 const std::string
& prefix
)
197 lsnes_instance
.iqueue
->run([&inst
, &factory
, mode
, prefix
]() {
198 inst
.mdumper
->start(factory
, mode
, prefix
);
202 void UI_end_dump(emulator_instance
& inst
, dumper_factory_base
& factory
)
204 lsnes_instance
.iqueue
->run([&inst
, &factory
]() {
205 auto in
= inst
.mdumper
->get_instance(&factory
);
210 void UI_do_keypress(emulator_instance
& inst
, const keyboard::modifier_set
& mods
, keyboard::key_key
& key
,
214 inst
.iqueue
->run_async([mods
, _key
, polarity
]() {
215 _key
->set_state(mods
, polarity
? 1 : 0);
216 }, [](std::exception
& e
) {});
219 bool UI_has_movie(emulator_instance
& inst
)
222 lsnes_instance
.iqueue
->run([&inst
, &ret
]() {
223 ret
= !!*inst
.mlogic
&& !inst
.rom
->isnull();
228 void UI_save_movie(emulator_instance
& inst
, std::ostringstream
& stream
)
230 lsnes_instance
.iqueue
->run([&inst
, &stream
]() {
231 inst
.mlogic
->get_mfile().is_savestate
= false;
232 auto prj
= inst
.project
->get();
234 inst
.mlogic
->get_mfile().gamename
= prj
->gamename
;
235 inst
.mlogic
->get_mfile().authors
= prj
->authors
;
237 inst
.mlogic
->get_mfile().active_macros
.clear();
238 inst
.mlogic
->get_mfile().save(stream
, inst
.mlogic
->get_rrdata());
242 std::pair
<std::string
, std::string
> UI_lookup_platform_and_game(emulator_instance
& inst
)
246 lsnes_instance
.iqueue
->run([&inst
, &plat
, &game
]() {
247 auto prj
= inst
.project
->get();
249 game
= prj
->gamename
;
251 game
= inst
.mlogic
->get_mfile().gamename
;
252 plat
= lookup_sysregion_mapping(inst
.mlogic
->get_mfile().gametype
->get_name());
254 return std::make_pair(plat
, game
);
257 std::string
UI_get_project_otherpath(emulator_instance
& inst
)
260 lsnes_instance
.iqueue
->run([&inst
, &path
]() {
261 path
= inst
.project
->otherpath();
266 std::string
UI_get_project_moviepath(emulator_instance
& inst
)
269 lsnes_instance
.iqueue
->run([&inst
, &path
]() {
270 path
= inst
.project
->moviepath();
275 bool UI_in_project_context(emulator_instance
& inst
)
278 lsnes_instance
.iqueue
->run([&inst
, &pc
]() {
279 pc
= (inst
.project
->get() != NULL
);