The Battle for Wesnoth  1.19.7+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 #include "serialization/chrono.hpp"
27 
29 
30 namespace editor {
31 
32 std::string initialize_addon()
33 {
34  std::string addon_id = "";
35  while(true)
36  {
37  gui2::dialogs::editor_choose_addon choose(addon_id);
38  if(choose.show()) {
39  break;
40  } else {
41  return "";
42  }
43  }
44 
45  if(addon_id == "///newaddon///") {
46  static constexpr std::string_view ts_format = "%Y-%m-%d_%H-%M-%S";
47  std::string timestamp = chrono::format_local_timestamp(std::chrono::system_clock::now(), ts_format);
48 
49  addon_id = "MyAwesomeAddon-" + timestamp;
50  std::string addon_id_new = addon_id;
51 
52  if(gui2::dialogs::prompt::execute(addon_id_new)) {
53  if(addon_filename_legal(addon_id_new)) {
54  addon_id = addon_id_new;
55  }
56  }
57  }
58 
59  if(addon_id == "mainline") {
60  return addon_id;
61  }
62 
63  std::string addon_dir = filesystem::get_addons_dir() + "/" + addon_id;
64 
65  if(filesystem::file_exists(addon_dir)) {
66  return addon_id;
67  }
68 
69  // create folders
71  filesystem::create_directory_if_missing(addon_dir + "/maps");
72  filesystem::create_directory_if_missing(addon_dir + "/scenarios");
73  filesystem::create_directory_if_missing(addon_dir + "/images");
74  filesystem::create_directory_if_missing(addon_dir + "/images/units");
75  filesystem::create_directory_if_missing(addon_dir + "/masks");
76  filesystem::create_directory_if_missing(addon_dir + "/music");
77  filesystem::create_directory_if_missing(addon_dir + "/sounds");
78  filesystem::create_directory_if_missing(addon_dir + "/translations");
79  filesystem::create_directory_if_missing(addon_dir + "/units");
80  filesystem::create_directory_if_missing(addon_dir + "/utils");
81 
82  // create files
83  // achievements
84  {
85  filesystem::scoped_ostream stream = filesystem::ostream_file(addon_dir + "/achievements.cfg");
86  *stream << "";
87  }
88 
89  // _server.pbl
90  {
91  filesystem::scoped_ostream stream = filesystem::ostream_file(addon_dir + "/_server.pbl");
92  *stream << "";
93  }
94 
95  // a basic _main.cfg
96  {
97  filesystem::scoped_ostream stream = filesystem::ostream_file(addon_dir + "/_main.cfg");
98  *stream << "#textdomain wesnoth-" << addon_id << "\n"
99  << "[textdomain]" << "\n"
100  << " name=\"wesnoth-" << addon_id << "\"\n"
101  << " path=\"data/add-ons/" << addon_id << "/translations\"\n"
102  << "[/textdomain]\n"
103  << "\n"
104  << "[binary_path]\n"
105  << " path=data/add-ons/" << addon_id << "\n"
106  << "[/binary_path]\n"
107  << "\n"
108  << "{~add-ons/" << addon_id << "/scenarios}\n"
109  << "{~add-ons/" << addon_id << "/utils}\n"
110  << "\n"
111  << "[units]\n"
112  << " {~add-ons/" << addon_id << "/units}\n"
113  << "[/units]\n";
114  }
115 
116  return addon_id;
117 }
118 
119 EXIT_STATUS start(bool clear_id, const std::string& filename, bool take_screenshot, const std::string& screenshot_filename)
120 {
122  try {
124 
125  editor_controller editor(clear_id);
126 
127  if (!filename.empty() && filesystem::file_exists(filename)) {
129  editor.context_manager_->set_default_dir(filename);
130  editor.context_manager_->load_map_dialog(true);
131  } else {
132  editor.context_manager_->load_map(filename, false);
133 
134  // HACK: this fixes an issue where the button overlays would be missing when
135  // the loaded map appears.
136  //
137  // Do note adding a redraw_everything call to context_manager::refresh_all also
138  // fixes the issue, but I'm pretty sure thats just because editor_controller::
139  // display_redraw_callback gets called, which then calls set_button_state.
140  //
141  // -- vultraz, 2018-02-24
142  editor.set_button_state();
143  }
144 
145  if (take_screenshot) {
146  editor.do_screenshot(screenshot_filename);
147  e = EXIT_NORMAL;
148  }
149  }
150 
151  if (!take_screenshot) {
152  e = editor.main_loop();
153  }
154  } catch(const editor_exception& e) {
155  ERR_ED << "Editor exception in editor::start: " << e.what();
156  throw;
157  }
159  ERR_ED << "Possibly leaked " << editor_action::get_instance_count() << " action objects";
160  }
161 
162  return e;
163 }
164 
165 } //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.
static bool timestamp
Definition: log.cpp:61
auto format_local_timestamp(const std::chrono::system_clock::time_point &time, std::string_view format="%F %T")
Definition: chrono.hpp:61
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:32
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:326
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:54
static bool create_directory_if_missing(const bfs::path &dirpath)
Definition: filesystem.cpp:360
std::string get_addons_dir()
constexpr uint32_t scope_editor
std::string filename
Filename.
bool addon_filename_legal(const std::string &name)
Checks whether an add-on file name is legal or not.
Definition: validation.cpp:66
#define e
#define h