redundancy reduced

This commit is contained in:
MihailRis 2024-04-30 14:49:55 +03:00
parent 8c86bcae54
commit 9b843817f8
10 changed files with 136 additions and 146 deletions

View File

@ -1,8 +1,5 @@
function create_setting(id, name, step, postfix)
local info = core.get_setting_info(id)
if postfix == nil then
postfix = ""
end
document.settings_panel:add(gui.template("track_setting", {
id=id,
name=gui.str(name, "settings"),
@ -10,7 +7,7 @@ function create_setting(id, name, step, postfix)
min=info.min,
max=info.max,
step=step,
postfix=postfix
postfix=postfix or ""
}))
end
@ -41,6 +38,6 @@ function on_open()
create_checkbox("camera.shaking", "Camera Shaking")
document.langs_btn.text = string.format(
"%s: %s", gui.str("Language", "settings"),
gui.get_locales_info()[gui.get_locale()].name
gui.get_locales_info()[core.get_setting("ui.language")].name
)
end

View File

@ -352,52 +352,52 @@ namespace audio {
/// @brief Initialize audio system or use no audio mode
/// @param enabled try to initialize actual audio
extern void initialize(bool enabled);
void initialize(bool enabled);
/// @brief Load audio file info and PCM data
/// @param file audio file
/// @param headerOnly read header only
/// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return PCM audio data
extern PCM* load_PCM(const fs::path& file, bool headerOnly);
PCM* load_PCM(const fs::path& file, bool headerOnly);
/// @brief Load sound from file
/// @param file audio file path
/// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM
/// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return new Sound instance
extern Sound* load_sound(const fs::path& file, bool keepPCM);
Sound* load_sound(const fs::path& file, bool keepPCM);
/// @brief Create new sound from PCM data
/// @param pcm PCM data
/// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM
/// @return new Sound instance
extern Sound* create_sound(std::shared_ptr<PCM> pcm, bool keepPCM);
Sound* create_sound(std::shared_ptr<PCM> pcm, bool keepPCM);
/// @brief Open new PCM stream from file
/// @param file audio file path
/// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return new PCMStream instance
extern PCMStream* open_PCM_stream(const fs::path& file);
PCMStream* open_PCM_stream(const fs::path& file);
/// @brief Open new audio stream from file
/// @param file audio file path
/// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource
/// @return new Stream instance
extern Stream* open_stream(const fs::path& file, bool keepSource);
Stream* open_stream(const fs::path& file, bool keepSource);
/// @brief Open new audio stream from source
/// @param stream PCM data source
/// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource
/// @return new Stream instance
extern Stream* open_stream(std::shared_ptr<PCMStream> stream, bool keepSource);
Stream* open_stream(std::shared_ptr<PCMStream> stream, bool keepSource);
/// @brief Configure 3D listener
/// @param position listener position
/// @param velocity listener velocity (used for Doppler effect)
/// @param lookAt point the listener look at
/// @param up camera up vector
extern void set_listener(
void set_listener(
glm::vec3 position,
glm::vec3 velocity,
glm::vec3 lookAt,
@ -415,7 +415,7 @@ namespace audio {
/// (PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH)
/// @param channel channel index
/// @return speaker id or 0
extern speakerid_t play(
speakerid_t play(
Sound* sound,
glm::vec3 position,
bool relative,
@ -435,7 +435,7 @@ namespace audio {
/// @param loop loop stream
/// @param channel channel index
/// @return speaker id or 0
extern speakerid_t play(
speakerid_t play(
std::shared_ptr<Stream> stream,
glm::vec3 position,
bool relative,
@ -454,7 +454,7 @@ namespace audio {
/// @param loop loop stream
/// @param channel channel index
/// @return speaker id or 0
extern speakerid_t play_stream(
speakerid_t play_stream(
const fs::path& file,
glm::vec3 position,
bool relative,
@ -467,49 +467,49 @@ namespace audio {
/// @brief Get speaker by id
/// @param id speaker id
/// @return speaker or nullptr
extern Speaker* get_speaker(speakerid_t id);
Speaker* get_speaker(speakerid_t id);
/// @brief Create new channel.
/// All non-builtin channels will be destroyed on audio::reset() call
/// @param name channel name
/// @return new channel index
extern int create_channel(const std::string& name);
int create_channel(const std::string& name);
/// @brief Get channel index by name
/// @param name channel name
/// @return channel index or -1
extern int get_channel_index(const std::string& name);
int get_channel_index(const std::string& name);
/// @brief Get channel by index. 0 - is master channel
/// @param index channel index
/// @return channel or nullptr
extern Channel* get_channel(int index);
Channel* get_channel(int index);
/// @brief Get channel by name.
/// @param name channel name
/// @return channel or nullptr
extern Channel* get_channel(const std::string& name);
Channel* get_channel(const std::string& name);
/// @brief Get stream associated with speaker
/// @param id speaker id
/// @return stream or nullptr
extern std::shared_ptr<Stream> get_associated_stream(speakerid_t id);
std::shared_ptr<Stream> get_associated_stream(speakerid_t id);
/// @brief Get alive speakers number (including paused)
extern size_t count_speakers();
size_t count_speakers();
/// @brief Get playing streams number (including paused)
extern size_t count_streams();
size_t count_streams();
/// @brief Update audio streams and sound instanced
/// @param delta time elapsed since the last update (seconds)
extern void update(double delta);
void update(double delta);
/// @brief Stop all playing audio in channel, reset channel state
extern void reset_channel(int channel);
void reset_channel(int channel);
/// @brief Finalize audio system
extern void close();
void close();
};
#endif // AUDIO_AUDIO_H_

View File

@ -29,8 +29,8 @@ inline const std::string BIND_HUD_INVENTORY = "hud.inventory";
class ContentBuilder;
namespace corecontent {
extern void setup_bindings();
extern void setup(ContentBuilder* builder);
void setup_bindings();
void setup(ContentBuilder* builder);
}
#endif // CORE_DEFS_H_

View File

@ -34,64 +34,64 @@ void files::rafile::read(char* buffer, std::streamsize size) {
}
bool files::write_bytes(fs::path filename, const ubyte* data, size_t size) {
std::ofstream output(filename, std::ios::binary);
if (!output.is_open())
return false;
output.write((const char*)data, size);
output.close();
return true;
std::ofstream output(filename, std::ios::binary);
if (!output.is_open())
return false;
output.write((const char*)data, size);
output.close();
return true;
}
uint files::append_bytes(fs::path filename, const ubyte* data, size_t size) {
std::ofstream output(filename, std::ios::binary | std::ios::app);
if (!output.is_open())
return 0;
uint position = output.tellp();
output.write((const char*)data, size);
output.close();
return position;
std::ofstream output(filename, std::ios::binary | std::ios::app);
if (!output.is_open())
return 0;
uint position = output.tellp();
output.write((const char*)data, size);
output.close();
return position;
}
bool files::read(fs::path filename, char* data, size_t size) {
std::ifstream output(filename, std::ios::binary);
if (!output.is_open())
return false;
output.read(data, size);
output.close();
return true;
std::ifstream output(filename, std::ios::binary);
if (!output.is_open())
return false;
output.read(data, size);
output.close();
return true;
}
ubyte* files::read_bytes(fs::path filename, size_t& length) {
std::ifstream input(filename, std::ios::binary);
if (!input.is_open())
return nullptr;
input.seekg(0, std::ios_base::end);
length = input.tellg();
input.seekg(0, std::ios_base::beg);
std::unique_ptr<ubyte[]> files::read_bytes(fs::path filename, size_t& length) {
std::ifstream input(filename, std::ios::binary);
if (!input.is_open())
return nullptr;
input.seekg(0, std::ios_base::end);
length = input.tellg();
input.seekg(0, std::ios_base::beg);
std::unique_ptr<char> data(new char[length]);
input.read(data.get(), length);
input.close();
return (ubyte*)data.release();
auto data = std::make_unique<ubyte[]>(length);
input.read((char*)data.get(), length);
input.close();
return data;
}
std::string files::read_string(fs::path filename) {
size_t size;
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
if (bytes == nullptr) {
throw std::runtime_error("could not to load file '"+
filename.string()+"'");
}
return std::string((const char*)bytes.get(), size);
size_t size;
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
if (bytes == nullptr) {
throw std::runtime_error("could not to load file '"+
filename.string()+"'");
}
return std::string((const char*)bytes.get(), size);
}
bool files::write_string(fs::path filename, const std::string content) {
std::ofstream file(filename);
if (!file) {
return false;
}
file << content;
return true;
std::ofstream file(filename);
if (!file) {
return false;
}
file << content;
return true;
}
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
@ -104,11 +104,11 @@ bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool c
}
std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
std::string text = files::read_string(filename);
try {
auto obj = json::parse(filename.string(), text);
std::string text = files::read_string(filename);
try {
auto obj = json::parse(filename.string(), text);
return obj;
} catch (const parsing_error& error) {
} catch (const parsing_error& error) {
std::cerr << error.errorLog() << std::endl;
throw std::runtime_error("could not to parse "+filename.string());
}
@ -123,19 +123,19 @@ std::unique_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
}
std::vector<std::string> files::read_list(fs::path filename) {
std::ifstream file(filename);
if (!file) {
throw std::runtime_error("could not to open file "+filename.u8string());
}
std::vector<std::string> lines;
std::string line;
while (std::getline(file, line)) {
std::ifstream file(filename);
if (!file) {
throw std::runtime_error("could not to open file "+filename.u8string());
}
std::vector<std::string> lines;
std::string line;
while (std::getline(file, line)) {
util::trim(line);
if (line.length() == 0)
continue;
if (line[0] == '#')
continue;
lines.push_back(line);
}
return lines;
if (line.length() == 0)
continue;
if (line[0] == '#')
continue;
lines.push_back(line);
}
return lines;
}

View File

@ -31,20 +31,20 @@ namespace files {
/// @param file target file
/// @param data data bytes array
/// @param size size of data bytes array
extern bool write_bytes(fs::path file, const ubyte* data, size_t size);
bool write_bytes(fs::path file, const ubyte* data, size_t size);
/// @brief Append bytes array to the file without any extra data
/// @param file target file
/// @param data data bytes array
/// @param size size of data bytes array
extern uint append_bytes(fs::path file, const ubyte* data, size_t size);
uint append_bytes(fs::path file, const ubyte* data, size_t size);
/// @brief Write string to the file
extern bool write_string(fs::path filename, const std::string content);
bool write_string(fs::path filename, const std::string content);
/// @brief Write dynamic data to the JSON file
/// @param nice if true, human readable format will be used, otherwise minimal
extern bool write_json(
bool write_json(
fs::path filename,
const dynamic::Map* obj,
bool nice=true);
@ -52,21 +52,21 @@ namespace files {
/// @brief Write dynamic data to the binary JSON file
/// (see src/coders/binary_json_spec.md)
/// @param compressed use gzip compression
extern bool write_binary_json(
bool write_binary_json(
fs::path filename,
const dynamic::Map* obj,
bool compressed=false
);
extern bool read(fs::path, char* data, size_t size);
extern ubyte* read_bytes(fs::path, size_t& length);
extern std::string read_string(fs::path filename);
bool read(fs::path, char* data, size_t size);
std::unique_ptr<ubyte[]> read_bytes(fs::path, size_t& length);
std::string read_string(fs::path filename);
/// @brief Read JSON or BJSON file
/// @param file *.json or *.bjson file
extern std::unique_ptr<dynamic::Map> read_json(fs::path file);
extern std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
extern std::vector<std::string> read_list(fs::path file);
std::unique_ptr<dynamic::Map> read_json(fs::path file);
std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
std::vector<std::string> read_list(fs::path file);
}
#endif /* FILES_FILES_H_ */
#endif /* FILES_FILES_H_ */

View File

@ -25,7 +25,7 @@ void menus::create_version_label(Engine* engine) {
auto gui = engine->getGUI();
auto text = ENGINE_VERSION_STRING+" development build";
gui->add(guiutil::create(
"<label z-index='1000' color='#FFFFFF80' gravity='top-right' margin='4'>"
"<label z-index='1000' color='#FFFFFF80' gravity='bottom-left' margin='4'>"
+text+
"</label>"
));

View File

@ -90,9 +90,9 @@ static int l_file_mkdirs(lua_State* L) {
static int l_file_read_bytes(lua_State* L) {
fs::path path = resolve_path(L, lua_tostring(L, 1));
if (fs::is_regular_file(path)) {
size_t length = (size_t) fs::file_size(path);
size_t length = static_cast<size_t>(fs::file_size(path));
ubyte* bytes = files::read_bytes(path, length);
auto bytes = files::read_bytes(path, length);
lua_createtable(L, length, 0);
int newTable = lua_gettop(L);

View File

@ -413,12 +413,6 @@ static int l_gui_get_locales_info(lua_State* L) {
return 1;
}
/// @brief gui.get_locale() -> string
static int l_gui_get_locale(lua_State* L) {
lua_pushstring(L, langs::current->getId().c_str());
return 1;
}
const luaL_Reg guilib [] = {
{"get_viewport", lua_wrap_errors<l_gui_getviewport>},
{"getattr", lua_wrap_errors<l_gui_getattr>},
@ -426,7 +420,6 @@ const luaL_Reg guilib [] = {
{"get_env", lua_wrap_errors<l_gui_get_env>},
{"str", lua_wrap_errors<l_gui_str>},
{"reindex", lua_wrap_errors<l_gui_reindex>},
{"get_locale", lua_wrap_errors<l_gui_get_locale>},
{"get_locales_info", lua_wrap_errors<l_gui_get_locales_info>},
{NULL, NULL}
};

View File

@ -4,9 +4,9 @@
#include <string>
namespace platform {
extern void configure_encoding();
void configure_encoding();
// @return environment locale in ISO format ll_CC
extern std::string detect_locale();
std::string detect_locale();
}
#endif // UTIL_PLATFORM_H_
#endif // UTIL_PLATFORM_H_

View File

@ -7,55 +7,55 @@
namespace util {
/// @brief Function used for string serialization in text formats
extern std::string escape(const std::string& s);
std::string escape(const std::string& s);
/// @brief Function used for error messages
extern std::string quote(const std::string& s);
std::string quote(const std::string& s);
extern std::wstring lfill(std::wstring s, uint length, wchar_t c);
extern std::wstring rfill(std::wstring s, uint length, wchar_t c);
std::wstring lfill(std::wstring s, uint length, wchar_t c);
std::wstring rfill(std::wstring s, uint length, wchar_t c);
extern uint encode_utf8(uint32_t c, ubyte* bytes);
extern uint32_t decode_utf8(uint& size, const char* bytes);
extern std::string wstr2str_utf8(const std::wstring ws);
extern std::wstring str2wstr_utf8(const std::string s);
extern bool is_integer(std::string text);
extern bool is_integer(std::wstring text);
extern bool is_valid_filename(std::wstring name);
uint encode_utf8(uint32_t c, ubyte* bytes);
uint32_t decode_utf8(uint& size, const char* bytes);
std::string wstr2str_utf8(const std::wstring ws);
std::wstring str2wstr_utf8(const std::string s);
bool is_integer(std::string text);
bool is_integer(std::wstring text);
bool is_valid_filename(std::wstring name);
extern void ltrim(std::string &s);
extern void rtrim(std::string &s);
extern void trim(std::string &s);
void ltrim(std::string &s);
void rtrim(std::string &s);
void trim(std::string &s);
extern std::string to_string(double x);
extern std::wstring to_wstring(double x, int precision);
std::string to_string(double x);
std::wstring to_wstring(double x, int precision);
extern std::string base64_encode(const ubyte* data, size_t size);
extern std::vector<ubyte> base64_decode(const char* str, size_t size);
extern std::vector<ubyte> base64_decode(const std::string& str);
std::string base64_encode(const ubyte* data, size_t size);
std::vector<ubyte> base64_decode(const char* str, size_t size);
std::vector<ubyte> base64_decode(const std::string& str);
extern std::string mangleid(uint64_t value);
std::string mangleid(uint64_t value);
extern int replaceAll(std::string& str, const std::string& from, const std::string& to);
int replaceAll(std::string& str, const std::string& from, const std::string& to);
extern double parse_double(const std::string& str);
extern double parse_double(const std::string& str, size_t offset, size_t len);
double parse_double(const std::string& str);
double parse_double(const std::string& str, size_t offset, size_t len);
extern std::wstring lower_case(const std::wstring& str);
extern std::wstring upper_case(const std::wstring& str);
extern std::wstring capitalized(const std::wstring& str);
extern std::wstring pascal_case(const std::wstring& str);
std::wstring lower_case(const std::wstring& str);
std::wstring upper_case(const std::wstring& str);
std::wstring capitalized(const std::wstring& str);
std::wstring pascal_case(const std::wstring& str);
/// @brief Convert `any_prefix:some_data_id` to `some data id`. Leaves
/// '_' characters at end of the id.
/// @param id source id
/// @return resulting caption or empty string if there's nothing but prefix
extern std::string id_to_caption(const std::string& id);
std::string id_to_caption(const std::string& id);
extern std::vector<std::string> split(const std::string& str, char delimiter);
extern std::vector<std::wstring> split(const std::wstring& str, char delimiter);
std::vector<std::string> split(const std::string& str, char delimiter);
std::vector<std::wstring> split(const std::wstring& str, char delimiter);
extern std::string format_data_size(size_t size);
std::string format_data_size(size_t size);
}
#endif // UTIL_STRINGUTIL_H_