The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
editor_main.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
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"
24#include "filesystem.hpp"
27
29
30namespace editor {
31
32std::string initialize_addon()
33{
34 std::string addon_id = "";
35 while(true)
36 {
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
119EXIT_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 (take_screenshot) {
129 ERR_ED << "Map file '" << filename << "' does not exist. Unable to take screenshot!";
130 return EXIT_ERROR;
131 }
132 editor.context_manager_->load_map(filename, false);
133 editor.do_screenshot(screenshot_filename);
134 return EXIT_NORMAL;
135 }
136
137 if (!filename.empty()) {
139 editor.context_manager_->set_default_dir(filename);
140 editor.context_manager_->load_map_dialog(true);
141 } else {
142 editor.context_manager_->load_map(filename, false);
143
144 // HACK: this fixes an issue where the button overlays would be missing when
145 // the loaded map appears.
146 //
147 // Do note adding a redraw_everything call to context_manager::refresh_all also
148 // fixes the issue, but I'm pretty sure thats just because editor_controller::
149 // display_redraw_callback gets called, which then calls set_button_state.
150 //
151 // -- vultraz, 2018-02-24
152 editor.set_button_state();
153 }
154 }
155
156 e = editor.main_loop();
157 } catch(const editor_exception& e) {
158 ERR_ED << "Editor exception in editor::start: " << e.what();
159 throw;
160 }
162 ERR_ED << "Possibly leaked " << editor_action::get_instance_count() << " action objects";
163 }
164
165 return e;
166}
167
168} //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:89
auto format_local_timestamp(const std::chrono::system_clock::time_point &time, std::string_view format="%F %T")
Definition chrono.hpp:72
Manage the empty-palette in the editor.
Definition action.cpp:31
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()
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
bool create_directory_if_missing(const std::string &dirname)
Creates a directory if it does not exist already.
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=std::ios_base::binary, bool create_directory=true)
std::unique_ptr< std::ostream > scoped_ostream
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.
#define e
#define h