diff --git a/client/src/game_state.cpp b/client/src/game_state.cpp index 4d7b79f..52e8b73 100644 --- a/client/src/game_state.cpp +++ b/client/src/game_state.cpp @@ -13,7 +13,6 @@ #include "game_state.h" #include "terminal.h" #include "ui/desktop.h" -#include "ui/i_window_content.h" #include "ui/ui_window.h" #include "ui/editor.h" #include "ui/login_screen.h" @@ -23,12 +22,13 @@ #include "debug/debug_overlay.h" void GameState::_init_desktop(void) { - _desktop = std::make_unique(_screen_width, _screen_height, this); + _desktop = std::make_unique(_screen_width, _screen_height, this, _initial_session_id); auto term = std::make_unique(this); term->set_session_id(_initial_session_id); auto term_window = std::make_unique("Terminal", 100, 100, 800, 500); + term_window->set_session_id(_initial_session_id); UIWindow* window_ptr = term_window.get(); term_window->set_content(std::move(term)); _desktop->add_window(std::move(term_window)); @@ -229,6 +229,7 @@ void GameState::update(float dt, int draw_calls, int shape_verts, int text_verts if(new_term) { Terminal* term = dynamic_cast(new_term->get_content()); if(term) { + new_term->set_session_id(session_id); term->set_session_id(session_id); _desktop->register_session(session_id, new_term); } @@ -242,6 +243,7 @@ void GameState::update(float dt, int draw_calls, int shape_verts, int text_verts editor->set_buffer_content(args[2]); auto editor_window = std::make_unique(args[1].c_str(), 200, 200, 600, 400); + editor_window->set_session_id(session_id); editor_window->set_content(std::move(editor)); _desktop->add_window(std::move(editor_window)); } diff --git a/client/src/ui/desktop.cpp b/client/src/ui/desktop.cpp index 2e66ff0..19cd4a2 100644 --- a/client/src/ui/desktop.cpp +++ b/client/src/ui/desktop.cpp @@ -26,8 +26,10 @@ static const std::string& get_random_snippet(const std::vector& sni return snippets[rand() % snippets.size()]; } -Desktop::Desktop(int screen_width, int screen_height, GameState* game_state) { +Desktop::Desktop(int screen_width, int screen_height, GameState* game_state, + uint32_t initial_session_id) { _taskbar = std::make_unique(screen_width, screen_height); + _initial_session_id = initial_session_id; _game_state= game_state; _focused_window = nullptr; _window_awaiting_session_id = nullptr; @@ -144,6 +146,7 @@ void Desktop::handle_event(SDL_Event* event, int screen_width, int screen_height } else if(app_to_launch == "Editor") { auto editor = std::make_unique(); auto editor_window = std::make_unique("Editor", 200, 200, 600, 400); + editor_window->set_session_id(_initial_session_id); editor_window->set_content(std::move(editor)); add_window(std::move(editor_window)); _launcher_is_open = false; @@ -183,10 +186,7 @@ void Desktop::update(float dt, int screen_width, int screen_height) { IWindowContent* content = _focused_window->get_content(); if(content) { WindowAction action = content->get_pending_action(); - uint32_t session_id = 0; - if(auto term = dynamic_cast(content)) { - session_id = term->get_session_id(); - } + uint32_t session_id = _focused_window->get_session_id(); switch(action.type) { case ActionType::WRITE_FILE: { if(session_id != 0) { diff --git a/client/src/ui/desktop.h b/client/src/ui/desktop.h index fced735..c22370e 100644 --- a/client/src/ui/desktop.h +++ b/client/src/ui/desktop.h @@ -24,7 +24,8 @@ struct ScrollingText { class Desktop { public: - Desktop(int screen_width, int screen_height, GameState* game_state); + Desktop(int screen_width, int screen_height, GameState* game_state, + uint32_t initial_session_id); ~Desktop(void); void add_window(std::unique_ptr window); @@ -52,4 +53,5 @@ private: std::vector _background_text; std::vector _snippets; bool _launcher_is_open; + uint32_t _initial_session_id; }; diff --git a/client/src/ui/ui_window.h b/client/src/ui/ui_window.h index d2e3de7..ca06328 100644 --- a/client/src/ui/ui_window.h +++ b/client/src/ui/ui_window.h @@ -45,6 +45,8 @@ public: bool is_mouse_over_resize_handle(int mouse_x, int mouse_y) const; const std::string& get_title(void) const; Rect get_rect(void) const; + void set_session_id(uint32_t id) { _session_id = id; } + uint32_t get_session_id(void) const { return _session_id; } private: friend class Taskbar; /* Allow taskbar to access private members. */ @@ -64,4 +66,6 @@ private: bool _is_resizing; int _resize_margin; + + uint32_t _session_id; }; diff --git a/common/src/lua_api.cpp b/common/src/lua_api.cpp index 8fcaa48..b37e880 100644 --- a/common/src/lua_api.cpp +++ b/common/src/lua_api.cpp @@ -30,22 +30,50 @@ std::string rm(Session& context, const std::string& filename) { return ""; } -std::string write_file(Session& context, const std::string& filename, +std::string write_file(Session& context, const std::string& path, const std::string& content) { - vfs_node* current_dir = context.get_current_dir(); - auto it = current_dir->children.find(filename); + vfs_node* parent_dir = nullptr; + std::string filename; - if(it != current_dir->children.end()) { - /* File exists, update content. */ + if(path[0] == '/') { + Machine* session_machine = context.get_session_machine(); + vfs_node* root = session_machine->vfs_root; + + size_t last_slash = path.find_last_of('/'); + if(last_slash == 0) { + /* File in root. */ + parent_dir = root; + filename = path.substr(1); + } else { + std::string parent_path = path.substr(0, last_slash); + parent_dir = find_node_by_path(root, parent_path); + filename = path.substr(last_slash+1); + } + } else { + /* Relative path. */ + parent_dir = context.get_current_dir(); + filename = path; + } + + if(!parent_dir) { + return "write: cannot create file '" + path + "': No such file or directory"; + } + + if(parent_dir->type != DIR_NODE) { + return "write: cannot create file in '" + get_full_path(parent_dir) + "': Not a directory"; + } + + auto it = parent_dir->children.find(filename); + if(it != parent_dir->children.end()) { if(it->second->type == DIR_NODE) { - return "write: " + filename + ": Is a directory."; + return "write: " + path + ": Is a directory"; } it->second->content = content; } else { /* File does not exist, create it. */ - vfs_node* new_file = new_node(filename, FILE_NODE, current_dir); + vfs_node* new_file = new_node(filename, FILE_NODE, parent_dir); new_file->content = content; - current_dir->children[filename] = new_file; + parent_dir->children[filename] = new_file; } return ""; } diff --git a/common/src/lua_api.h b/common/src/lua_api.h index 57e7a9c..6d685df 100644 --- a/common/src/lua_api.h +++ b/common/src/lua_api.h @@ -11,7 +11,7 @@ namespace api { /* FILESYSTEM ACTIONS. */ vfs_node* get_current_dir(Session& context); std::string rm(Session& context, const std::string& filename); -std::string write_file(Session& context, const std::string& filename, +std::string write_file(Session& context, const std::string& path, const std::string& content); std::string ls(Session& context); std::string cd(Session& context, const std::string& path);