The Battle for Wesnoth  1.19.0-dev
editor_main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Tomasz Sniatowski <kailoran@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-editor"
17 
19 
20 #include "addon/validation.hpp"
21 #include "gettext.hpp"
23 #include "gui/dialogs/prompt.hpp"
24 #include "filesystem.hpp"
26 
28 
29 namespace editor {
30 
31 std::string initialize_addon()
32 {
33  std::string addon_id = "";
34  while(true)
35  {
36  gui2::dialogs::editor_choose_addon choose(addon_id);
37  if(choose.show()) {
38  break;
39  } else {
40  return "";
41  }
42  }
43 
44  if(addon_id == "///newaddon///") {
45  std::string& addon_id_new = addon_id;
46  std::int64_t current_millis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
47  addon_id = "MyAwesomeAddon-"+std::to_string(current_millis);
48  if (gui2::dialogs::prompt::execute(addon_id_new)) {
49  addon_id = !addon_filename_legal(addon_id_new) ? "MyAwesomeAddon-"+std::to_string(current_millis) : addon_id_new;
50  }
51  }
52 
53  if(addon_id == "mainline") {
54  return addon_id;
55  }
56 
57  std::string addon_dir = filesystem::get_addons_dir() + "/" + addon_id;
58 
59  if(filesystem::file_exists(addon_dir)) {
60  return addon_id;
61  }
62 
63  // create folders
65  filesystem::create_directory_if_missing(addon_dir + "/maps");
66  filesystem::create_directory_if_missing(addon_dir + "/scenarios");
67  filesystem::create_directory_if_missing(addon_dir + "/images");
68  filesystem::create_directory_if_missing(addon_dir + "/images/units");
69  filesystem::create_directory_if_missing(addon_dir + "/masks");
70  filesystem::create_directory_if_missing(addon_dir + "/music");
71  filesystem::create_directory_if_missing(addon_dir + "/sounds");
72  filesystem::create_directory_if_missing(addon_dir + "/translations");
73  filesystem::create_directory_if_missing(addon_dir + "/units");
74  filesystem::create_directory_if_missing(addon_dir + "/utils");
75 
76  // create files
77  // achievements
78  {
79  filesystem::scoped_ostream stream = filesystem::ostream_file(addon_dir + "/achievements.cfg");
80  *stream << "";
81  }
82 
83  // _server.pbl
84  {
85  filesystem::scoped_ostream stream = filesystem::ostream_file(addon_dir + "/_server.pbl");
86  *stream << "";
87  }
88 
89  // a basic _main.cfg
90  {
91  filesystem::scoped_ostream stream = filesystem::ostream_file(addon_dir + "/_main.cfg");
92  *stream << "#textdomain wesnoth-" << addon_id << "\n"
93  << "[textdomain]" << "\n"
94  << " name=\"wesnoth-" << addon_id << "\"\n"
95  << " path=\"data/add-ons/" << addon_id << "/translations\"\n"
96  << "[/textdomain]\n"
97  << "\n"
98  << "[binary_path]\n"
99  << " path=data/add-ons/" << addon_id << "\n"
100  << "[/binary_path]\n"
101  << "\n"
102  << "{~add-ons/" << addon_id << "/scenarios}\n"
103  << "{~add-ons/" << addon_id << "/utils}\n"
104  << "\n"
105  << "[units]\n"
106  << " {~add-ons/" << addon_id << "/units}\n"
107  << "[/units]\n";
108  }
109 
110  return addon_id;
111 }
112 
113 EXIT_STATUS start(bool clear_id, const std::string& filename, bool take_screenshot, const std::string& screenshot_filename)
114 {
116  try {
118 
119  editor_controller editor(clear_id);
120 
121  if (!filename.empty() && filesystem::file_exists (filename)) {
122  if (filesystem::is_directory(filename)) {
123  editor.context_manager_->set_default_dir(filename);
124  editor.context_manager_->load_map_dialog(true);
125  } else {
126  editor.context_manager_->load_map(filename, false);
127 
128  // HACK: this fixes an issue where the button overlays would be missing when
129  // the loaded map appears. Since we're gonna drop this ridiculous GUI1 drawing
130  // stuff in 1.15 I'm not going to waste time coming up with a better fix.
131  //
132  // Do note adding a redraw_everything call to context_manager::refresh_all also
133  // fixes the issue, but I'm pretty sure thats just because editor_controller::
134  // display_redraw_callback gets called, which then calls set_button_state.
135  //
136  // -- vultraz, 2018-02-24
137  editor.set_button_state();
138  }
139 
140  if (take_screenshot) {
141  editor.do_screenshot(screenshot_filename);
142  e = EXIT_NORMAL;
143  }
144  }
145 
146  if (!take_screenshot) {
147  e = editor.main_loop();
148  }
149  } catch(const editor_exception& e) {
150  ERR_ED << "Editor exception in editor::start: " << e.what();
151  throw;
152  }
154  ERR_ED << "Possibly leaked " << editor_action::get_instance_count() << " action objects";
155  }
156 
157  return e;
158 }
159 
160 } //end namespace editor
Base class for editor actions.
static int get_instance_count()
Debugging aid.
The editor_controller class contains the mouse and keyboard event handling routines for the editor.
bool show(const unsigned auto_close_time=0)
Shows the window.
#define ERR_ED
lg::log_domain log_editor("editor")
Declarations for File-IO.
Manage the empty-palette in the editor.
Definition: action.cpp:31
@ EXIT_ERROR
Definition: editor_main.hpp:27
@ EXIT_NORMAL
Definition: editor_main.hpp:24
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
std::string initialize_addon()
Definition: editor_main.cpp:31
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:319
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
filesystem::scoped_ostream ostream_file(const std::string &fname, std::ios_base::openmode mode, bool create_directory)
std::unique_ptr< std::ostream > scoped_ostream
Definition: filesystem.hpp:51
static bool create_directory_if_missing(const bfs::path &dirpath)
Definition: filesystem.cpp:353
std::string get_addons_dir()
constexpr uint32_t scope_editor
bool addon_filename_legal(const std::string &name)
Checks whether an add-on file name is legal or not.
Definition: validation.cpp:67
#define e
#define h