[Add] Editor build menu and patch Lua sandbox vuln.
This commit is contained in:
		
							parent
							
								
									233dd384f6
								
							
						
					
					
						commit
						b4855fa47e
					
				@ -358,6 +358,12 @@ void GameState::send_file_read_request(uint32_t session_id, const std::string& p
 | 
				
			|||||||
                                             {std::to_string(session_id), path}));
 | 
					                                             {std::to_string(session_id), path}));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void GameState::send_build_file_request(uint32_t session_id, const std::string& path,
 | 
				
			||||||
 | 
					                                        const std::string& content) {
 | 
				
			||||||
 | 
					  _network->send(net_protocol::build_message(net_protocol::Opcode::C2S_BUILD_FILE,
 | 
				
			||||||
 | 
					                                             {std::to_string(session_id), path, content}));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void GameState::send_create_session_request(void) {
 | 
					void GameState::send_create_session_request(void) {
 | 
				
			||||||
  if(_network && _network->is_connected()) {
 | 
					  if(_network && _network->is_connected()) {
 | 
				
			||||||
    _network->send(net_protocol::build_message(net_protocol::Opcode::C2S_CREATE_SESSION));
 | 
					    _network->send(net_protocol::build_message(net_protocol::Opcode::C2S_CREATE_SESSION));
 | 
				
			||||||
 | 
				
			|||||||
@ -35,6 +35,7 @@ public:
 | 
				
			|||||||
  /* Public network interface for UI components. */
 | 
					  /* Public network interface for UI components. */
 | 
				
			||||||
  void send_network_command(uint32_t session_id, const std::string& command);
 | 
					  void send_network_command(uint32_t session_id, const std::string& command);
 | 
				
			||||||
  void send_file_write_request(uint32_t session_id, const std::string& path, const std::string& content);
 | 
					  void send_file_write_request(uint32_t session_id, const std::string& path, const std::string& content);
 | 
				
			||||||
 | 
					  void send_build_file_request(uint32_t session_id, const std::string& path, const std::string& content);
 | 
				
			||||||
  void send_file_read_request(uint32_t session_id, const std::string& path);
 | 
					  void send_file_read_request(uint32_t session_id, const std::string& path);
 | 
				
			||||||
  void send_create_session_request(void);
 | 
					  void send_create_session_request(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -200,6 +200,12 @@ void Desktop::update(float dt, int screen_width, int screen_height) {
 | 
				
			|||||||
          }
 | 
					          }
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        case ActionType::BUILD_FILE: {
 | 
				
			||||||
 | 
					          if(session_id != 0) {
 | 
				
			||||||
 | 
					            _game_state->send_build_file_request(session_id, action.payload1, action.payload2);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        default:
 | 
					        default:
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
@ -14,6 +14,10 @@ Editor::Editor(void)
 | 
				
			|||||||
  _menu_bar->add_menu_item("File", "Save", [this]() {
 | 
					  _menu_bar->add_menu_item("File", "Save", [this]() {
 | 
				
			||||||
    _pending_action = {ActionType::WRITE_FILE, _filename, _buffer.get_text()};
 | 
					    _pending_action = {ActionType::WRITE_FILE, _filename, _buffer.get_text()};
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					  _menu_bar->add_menu("Build");
 | 
				
			||||||
 | 
					  _menu_bar->add_menu_item("Build", "Run Build", [this]() {
 | 
				
			||||||
 | 
					    _pending_action = {ActionType::BUILD_FILE, _filename, _buffer.get_text()};
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Editor::Editor(const std::string& filename)
 | 
					Editor::Editor(const std::string& filename)
 | 
				
			||||||
@ -25,6 +29,10 @@ Editor::Editor(const std::string& filename)
 | 
				
			|||||||
  _menu_bar->add_menu_item("File", "Save", [this]() {
 | 
					  _menu_bar->add_menu_item("File", "Save", [this]() {
 | 
				
			||||||
    _pending_action = {ActionType::WRITE_FILE, _filename, _buffer.get_text()};
 | 
					    _pending_action = {ActionType::WRITE_FILE, _filename, _buffer.get_text()};
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					  _menu_bar->add_menu("Build");
 | 
				
			||||||
 | 
					  _menu_bar->add_menu_item("Build", "Run Build", [this]() {
 | 
				
			||||||
 | 
					    _pending_action = {ActionType::BUILD_FILE, _filename, _buffer.get_text()};
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Editor::~Editor(void) {}
 | 
					Editor::~Editor(void) {}
 | 
				
			||||||
 | 
				
			|||||||
@ -6,6 +6,7 @@ enum class ActionType {
 | 
				
			|||||||
  NONE,
 | 
					  NONE,
 | 
				
			||||||
  WRITE_FILE,
 | 
					  WRITE_FILE,
 | 
				
			||||||
  READ_FILE,
 | 
					  READ_FILE,
 | 
				
			||||||
 | 
					  BUILD_FILE,
 | 
				
			||||||
  CLOSE_WINDOW
 | 
					  CLOSE_WINDOW
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -9,7 +9,7 @@
 | 
				
			|||||||
#include "vfs.h"
 | 
					#include "vfs.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
LuaProcessor::LuaProcessor(Session& context) {
 | 
					LuaProcessor::LuaProcessor(Session& context) {
 | 
				
			||||||
  _lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::io, sol::lib::table);
 | 
					  _lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::table);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* Expose vfs_node struct members to Lua. */
 | 
					  /* Expose vfs_node struct members to Lua. */
 | 
				
			||||||
  _lua.new_usertype<vfs_node>("vfs_node",
 | 
					  _lua.new_usertype<vfs_node>("vfs_node",
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,7 @@ enum class Opcode : uint8_t {
 | 
				
			|||||||
  C2S_COMMAND,    /* args: [session_id, command] */
 | 
					  C2S_COMMAND,    /* args: [session_id, command] */
 | 
				
			||||||
  C2S_WRITE_FILE, /* args: [session_id, path, content] */
 | 
					  C2S_WRITE_FILE, /* args: [session_id, path, content] */
 | 
				
			||||||
  C2S_READ_FILE,  /* args: [session_id, path] */
 | 
					  C2S_READ_FILE,  /* args: [session_id, path] */
 | 
				
			||||||
 | 
					  C2S_BUILD_FILE, /* args: [session_id, path, content] */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* Server -> Client messages. */
 | 
					  /* Server -> Client messages. */
 | 
				
			||||||
  S2C_CREATE_ACCOUNT_SUCCESS,
 | 
					  S2C_CREATE_ACCOUNT_SUCCESS,
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,7 @@
 | 
				
			|||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "network_manager.h"
 | 
					#include "network_manager.h"
 | 
				
			||||||
 | 
					#include "lua_api.h"
 | 
				
			||||||
#include "net/message_protocol.h"
 | 
					#include "net/message_protocol.h"
 | 
				
			||||||
#include "db/database_manager.h"
 | 
					#include "db/database_manager.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -210,6 +211,28 @@ void NetworkManager::on_message(std::shared_ptr<net::TcpConnection> connection,
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
 | 
					      case net_protocol::Opcode::C2S_BUILD_FILE:
 | 
				
			||||||
 | 
					        if(args.size() == 3) {
 | 
				
			||||||
 | 
					          uint32_t session_id = std::stoul(args[0]);
 | 
				
			||||||
 | 
					          if(player->sessions.count(session_id)) {
 | 
				
			||||||
 | 
					            Session* session = player->sessions.at(session_id).get();
 | 
				
			||||||
 | 
					            const std::string& path = args[1];
 | 
				
			||||||
 | 
					            const std::string& content = args[2];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Save the file to ensure the VFS is up to date. */
 | 
				
			||||||
 | 
					            session->write_file(path, content);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* The 'build.lua' script is just a wrapper around 'api::create_executable'. */
 | 
				
			||||||
 | 
					            std::string exec_path = path;
 | 
				
			||||||
 | 
					            size_t dot_pos = exec_path.rfind(".lua");
 | 
				
			||||||
 | 
					            if(dot_pos != std::string::npos) {
 | 
				
			||||||
 | 
					              exec_path.erase(dot_pos);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            /* The api function returns an error string, but we'll ignore it here for now. */
 | 
				
			||||||
 | 
					            api::create_executable(*session, exec_path, content);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
    default:
 | 
					    default:
 | 
				
			||||||
      fprintf(stderr, "Received invalid opcode %d from active player.\n",
 | 
					      fprintf(stderr, "Received invalid opcode %d from active player.\n",
 | 
				
			||||||
              static_cast<int>(opcode));
 | 
					              static_cast<int>(opcode));
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user