PingusDemo::PingusDemo(const Pathname& pathname) : m_levelname(), m_checksum(), m_events() { std::vector<ReaderObject> lines = FileReader::parse_many(pathname); if (lines.empty()) { raise_exception(std::runtime_error, "'" << pathname.str() << "', demo file is empty"); } else { if (lines.front().get_name() == "level") { ReaderMapping reader = lines.front().get_mapping(); if (!reader.read_string("name", m_levelname)) { raise_exception(std::runtime_error, "(level (name ...)) entry missing in demo file '" << pathname.str() << "'"); } reader.read_string("checksum", m_checksum); } for(auto i = lines.begin() + 1; i != lines.end(); ++i) { if (i->get_name() != "checksum") // workaround for old incorrectly recorded demo files { m_events.push_back(ServerEvent(*i)); } } } }
// Save the current level void EditorScreen::save(const Pathname& file) { std::string filename = file.get_sys_path(); if (System::get_file_extension(filename) == "prefab") { level_pathname = file; log_info("Save to: %1%", file.str()); plf->save_prefab(filename); } else { level_pathname = file; log_info("Save to: %1%", file.str()); plf->save_level(filename); } }
Levelset::Levelset(const Pathname& pathname) : completion(0) { FileReader reader = FileReader::parse(pathname); if (reader.get_name() != "pingus-levelset") { PingusError::raise("Error: " + pathname.str() + ": not a 'pingus-levelset' file"); } else { reader.read_string("title", title); reader.read_string("description", description); std::string image; if (reader.read_string("image", image)) this->image = Resource::load_sprite(image); FileReader level_reader = reader.read_section("levels"); std::vector<FileReader> sections = level_reader.get_sections(); for(std::vector<FileReader>::iterator i = sections.begin(); i != sections.end(); ++i) { if (i->get_name() == "level") { Level* level = new Level(); if (i->read_string("filename", level->resname)) { level->plf = PLFResMgr::load_plf(level->resname); level->accessible = false; level->finished = false; levels.push_back(level); } else { std::cout << "Levelset: " << pathname.str() << " is missing filename tag" << std::endl; delete level; } } } } refresh(); }
std::unique_ptr<EditorLevel> EditorLevel::from_level_file(const Pathname& pathname) { log_info("%1%", pathname.str()); // Load the level from the file - we don't care what it's res_name is. PingusLevel plf(pathname); std::unique_ptr<EditorLevel> level(new EditorLevel); // Assign all of the level information to our LevelImpl level->impl->levelname = plf.get_levelname(); level->impl->description = plf.get_description(); level->impl->ambient_light = plf.get_ambient_light(); level->impl->size = plf.get_size(); level->impl->number_of_pingus = plf.get_number_of_pingus(); level->impl->number_to_save = plf.get_number_to_save(); level->impl->actions = plf.get_actions(); level->impl->time = plf.get_time(); level->impl->author = plf.get_author(); level->impl->music = plf.get_music(); // remove obsolete "none" tag if (level->impl->music == "none") { level->impl->music = ""; } // Get the objects auto objs = plf.get_objects(); for (auto i = objs.begin(); i != objs.end(); i++) { LevelObjPtr obj = LevelObjFactory::create(*i); if (obj) { level->add_object(obj); } } level->sort(); return level; }
std::unique_ptr<EditorLevel> EditorLevel::from_prefab_file(const Pathname& pathname) { log_info("%1%", pathname.str()); // Load the level from the file - we don't care what it's res_name is. PrefabFile prefab = PrefabFile::from_path(pathname); std::unique_ptr<EditorLevel> level(new EditorLevel); // FIMXE: there would be better way to handle prefab size, but it's // probably not worth the effort level->impl->size.width = 1920; level->impl->size.height = 1200; // FIXME: overrides are getting ignored // Get the objects auto objs = prefab.get_objects(); for (auto i = objs.begin(); i != objs.end(); i++) { LevelObjPtr obj = LevelObjFactory::create(*i); if (obj) { // move origin of the level to the center of it obj->set_pos(obj->get_pos() + Vector3f(static_cast<float>(level->impl->size.width)/2.0f, static_cast<float>(level->impl->size.height)/2.0f)); level->add_object(obj); } } level->sort(); return level; }
ControllerPtr Manager::create_controller(const Pathname& filename) { ControllerPtr controller(new Controller(desc)); ReaderObject reader_object = Reader::parse(filename); if (reader_object.get_name() != "pingus-controller") { raise_exception(std::runtime_error, "Controller: invalid config file '" << filename.str() << "'"); } else { ReaderMapping reader = reader_object.get_mapping(); ReaderMapping controls_mapping; if (!reader.read_mapping("controls", controls_mapping)) { log_warn("%1%: 'controls' section missing", filename); } else { for (const auto& key : controls_mapping.get_keys()) { ReaderCollection collection; if (!controls_mapping.read_collection(key.c_str(), collection)) { log_error("%1%: mapping must contain object at %2%", filename, key); } else { if (StringUtil::has_suffix(key, "pointer")) { int id = desc.get_definition(key).id; ControllerPointer* ctrl_pointer = controller->get_pointer(id); for(const auto& object : collection.get_objects()) { auto pointer = create_pointer(object, ctrl_pointer); if (pointer) { ctrl_pointer->add_pointer(std::move(pointer)); } else { log_error("Manager: pointer: Couldn't create pointer %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "scroller")) { int id = desc.get_definition(key).id; ControllerScroller* ctrl_scroller = controller->get_scroller(id); for(const auto& object : collection.get_objects()) { auto scroller = create_scroller(object, ctrl_scroller); if (scroller) { ctrl_scroller->add_scroller(std::move(scroller)); } else { log_error("Manager: scroller: Couldn't create scroller %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "button")) { int id = desc.get_definition(key).id; ControllerButton* ctrl_button = controller->get_button(id); for(const auto& object : collection.get_objects()) { auto button = create_button(object, ctrl_button); if (button) { ctrl_button->add_button(std::move(button)); } else { log_error("Manager: button: Couldn't create button %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "axis")) { int id = desc.get_definition(key).id; ControllerAxis* ctrl_axis = controller->get_axis(id); for(const auto& object : collection.get_objects()) { auto axis = create_axis(object, ctrl_axis); if (axis) { ctrl_axis->add_axis(std::move(axis)); } else { log_error("Manager: axis: Couldn't create axis %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "keyboard")) { int id = desc.get_definition(key).id; ControllerKeyboard* ctrl_keyboard = controller->get_keyboard(id); for(const auto& object : collection.get_objects()) { std::unique_ptr<Keyboard> keyboard = create_keyboard(object, ctrl_keyboard); if (keyboard) { ctrl_keyboard->add_keyboard(std::move(keyboard)); } else { log_error("Manager: keyboard: Couldn't create keyboard %1%", object.get_name()); } } } else { raise_exception(std::runtime_error, "Manager: Unkown Element in Controller Config: " << key); } } } } } controllers.push_back(controller); return controller; }
void PingusLevel::load(const std::string& resname, const Pathname& pathname) { impl->checksum = System::checksum(pathname); impl->resname = resname; ReaderObject reader_object = FileReader::parse(pathname); if (reader_object.get_name() != "pingus-level") { raise_exception(std::runtime_error, "Error: " << pathname.str() << ": not a 'pingus-level' file"); } else { ReaderMapping reader = reader_object.get_mapping(); int version; if (reader.read_int("version", version)) log_info("Levelfile Version: %1%", version); else log_info("Unknown Levelfile Version: %1%", version); ReaderMapping head; if (!reader.read_mapping("head", head)) { raise_exception(std::runtime_error, "Error: (head) section not found in '" << pathname.str() << "'"); } else { log_info("Reading head"); head.read_string("levelname", impl->levelname); head.read_string("description", impl->description); head.read_size ("levelsize", impl->size); head.read_string("music", impl->music); head.read_int ("time", impl->time); head.read_int ("number-of-pingus", impl->number_of_pingus); head.read_int ("number-to-save", impl->number_to_save); head.read_colorf("ambient-light", impl->ambient_light); head.read_string("author", impl->author); log_info("Size: %1%x%2%", impl->size.width, impl->size.height); ReaderMapping actions; if (head.read_mapping("actions", actions)) { std::vector<std::string> lst = actions.get_keys(); for(std::vector<std::string>::iterator i = lst.begin(); i != lst.end(); ++i) { int count = 0; log_info("Actions: %1%", i->c_str()); if (actions.read_int(i->c_str(), count)) impl->actions[*i] = count; } } else { raise_exception(std::runtime_error, "Error: (pingus-level head actions) not found in '" << pathname.str() << "'"); } } ReaderCollection collection; if (reader.read_collection("objects", collection)) { std::vector<ReaderObject> object_lst = collection.get_objects(); for(auto i = object_lst.begin(); i != object_lst.end(); ++i) { impl->objects.push_back(*i); } } } }
void PingusLevel::load(const std::string& resname, const Pathname& pathname) { impl->resname = resname; FileReader reader = FileReader::parse(pathname); if (reader.get_name() != "pingus-level") { PingusError::raise("Error: " + pathname.str() + ": not a 'pingus-level' file"); } else { int version; if (reader.read_int("version", version)) pout(PINGUS_DEBUG_LOADING) << "Levelfile Version: " << version << std::endl; else pout(PINGUS_DEBUG_LOADING) << "Unknown Levelfile Version: " << version << std::endl; FileReader head; if (!reader.read_section("head", head)) { PingusError::raise("Error: (head) section not found in '" + pathname.str() + "'"); } else { pout(PINGUS_DEBUG_LOADING) << "Reading head" << std::endl; head.read_string("levelname", impl->levelname); head.read_string("description", impl->description); head.read_size ("levelsize", impl->size); head.read_string("music", impl->music); head.read_int ("time", impl->time); head.read_int ("difficulty", impl->difficulty); head.read_int ("number-of-pingus", impl->number_of_pingus); head.read_int ("number-to-save", impl->number_to_save); head.read_color ("ambient-light", impl->ambient_light); head.read_string("author", impl->author); pout(PINGUS_DEBUG_LOADING) << "Size: " << impl->size.width << " " << impl->size.height << std::endl; FileReader actions; if (head.read_section("actions", actions)) { std::vector<std::string> lst = actions.get_section_names(); for(std::vector<std::string>::iterator i = lst.begin(); i != lst.end(); ++i) { int count = 0; pout(PINGUS_DEBUG_LOADING) << "Actions: " << i->c_str() << std::endl; if (actions.read_int(i->c_str(), count)) impl->actions[*i] = count; } } else { PingusError::raise("Error: (pingus-level head actions) not found in '" + pathname.str() + "'"); } } FileReader objects; if (reader.read_section("objects", objects)) { std::vector<FileReader> object_lst = objects.get_sections(); for(std::vector<FileReader>::iterator i = object_lst.begin(); i != object_lst.end(); ++i) { impl->objects.push_back(*i); } } } }
ControllerPtr Manager::create_controller(const Pathname& filename) { ControllerPtr controller(new Controller(desc)); FileReader reader = FileReader::parse(filename); if (reader.get_name() != "pingus-controller") { raise_exception(std::runtime_error, "Controller: invalid config file '" << filename.str() << "'"); } else { const std::vector<FileReader>& sections = reader.get_sections(); for (std::vector<FileReader>::const_iterator i = sections.begin(); i != sections.end(); ++i) { if (StringUtil::has_suffix(i->get_name(), "pointer")) { const std::vector<FileReader>& pointers = i->get_sections(); for(std::vector<FileReader>::const_iterator j = pointers.begin(); j != pointers.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerPointer* ctrl_pointer = controller->get_pointer(id); Pointer* pointer = create_pointer(*j, ctrl_pointer); if (pointer) ctrl_pointer->add_pointer(pointer); else log_error("Manager: pointer: Couldn't create pointer %1%", j->get_name()); } } else if (StringUtil::has_suffix(i->get_name(), "scroller")) { const std::vector<FileReader>& scrollers = i->get_sections(); for(std::vector<FileReader>::const_iterator j = scrollers.begin(); j != scrollers.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerScroller* ctrl_scroller = controller->get_scroller(id); Scroller* scroller = create_scroller(*j, ctrl_scroller); if (scroller) ctrl_scroller->add_scroller(scroller); else log_error("Manager: scroller: Couldn't create scroller %1%", j->get_name()); } } else if (StringUtil::has_suffix(i->get_name(), "button")) { const std::vector<FileReader>& buttons = i->get_sections(); for(std::vector<FileReader>::const_iterator j = buttons.begin(); j != buttons.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerButton* ctrl_button = controller->get_button(id); Button* button = create_button(*j, ctrl_button); if (button) ctrl_button->add_button(button); else log_error("Manager: button: Couldn't create button %1%", j->get_name()); } } else if (StringUtil::has_suffix(i->get_name(), "axis")) { const std::vector<FileReader>& axes = i->get_sections(); for(std::vector<FileReader>::const_iterator j = axes.begin(); j != axes.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerAxis* ctrl_axis = controller->get_axis(id); Axis* axis = create_axis(*j, ctrl_axis); if (axis) ctrl_axis->add_axis(axis); else log_error("Manager: axis: Couldn't create axis %1%", j->get_name()); } } else if (StringUtil::has_suffix(i->get_name(), "keyboard")) { const std::vector<FileReader>& keyboards = i->get_sections(); for(std::vector<FileReader>::const_iterator j = keyboards.begin(); j != keyboards.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerKeyboard* ctrl_keyboard = controller->get_keyboard(id); Keyboard* keyboard = create_keyboard(*j, ctrl_keyboard); if (keyboard) ctrl_keyboard->add_keyboard(keyboard); else log_error("Manager: keyboard: Couldn't create keyboard %1%", j->get_name()); } } else { raise_exception(std::runtime_error, "Manager: Unkown Element in Controller Config: " << i->get_name()); } } } controllers.push_back(controller); return controller; }