| // | Aidan Lister | // +----------------------------------------------------------------------+ // // $Id: version_compare.php,v 1.1 2005/07/11 16:34:36 ggiunta Exp $ /** * Replace version_compare() * * @category PHP * @package PHP_Compat * @link http://php.net/function.version_compare * @author Philippe Jausions * @author Aidan Lister * @version $Revision: 1.1 $ * @since PHP 4.1.0 * @require PHP 4.0.0 (user_error) */ if (!function_exists('version_compare')) { function version_compare($version1, $version2, $operator = '<') { // Check input if (!is_scalar($version1)) { user_error('version_compare() expects parameter 1 to be string, ' . gettype($version1) . ' given', E_USER_WARNING); return; } if (!is_scalar($version2)) { user_error('version_compare() expects parameter 2 to be string, ' . gettype($version2) . ' given', E_USER_WARNING); return; } if (!is_scalar($operator)) { user_error('version_compare() expects parameter 3 to be string, ' . gettype($operator) . ' given', E_USER_WARNING); return; } // Standardise versions $v1 = explode('.', str_replace('..', '.', preg_replace('/([^0-9\.]+)/', '.$1.', str_replace(array('-', '_', '+'), '.', trim($version1))))); $v2 = explode('.', str_replace('..', '.', preg_replace('/([^0-9\.]+)/', '.$1.', str_replace(array('-', '_', '+'), '.', trim($version2))))); // Replace empty entries at the start of the array while (empty($v1[0]) && array_shift($v1)) {} while (empty($v2[0]) && array_shift($v2)) {} // Release state order // '#' stands for any number $versions = array( 'dev' => 0, 'alpha' => 1, 'a' => 1, 'beta' => 2, 'b' => 2, 'RC' => 3, '#' => 4, 'p' => 5, 'pl' => 5); // Loop through each segment in the version string $compare = 0; for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++) { if ($v1[$i] == $v2[$i]) { continue; } $i1 = $v1[$i]; $i2 = $v2[$i]; if (is_numeric($i1) && is_numeric($i2)) { $compare = ($i1 < $i2) ? -1 : 1; break; } // We use the position of '#' in the versions list // for numbers... (so take care of # in original string) if ($i1 == '#') { $i1 = ''; } elseif (is_numeric($i1)) { $i1 = '#'; } if ($i2 == '#') { $i2 = ''; } elseif (is_numeric($i2)) { $i2 = '#'; } if (isset($versions[$i1]) && isset($versions[$i2])) { $compare = ($versions[$i1] < $versions[$i2]) ? -1 : 1; } elseif (isset($versions[$i1])) { $compare = 1; } elseif (isset($versions[$i2])) { $compare = -1; } else { $compare = 0; } break; } // If previous loop didn't find anything, compare the "extra" segments if ($compare == 0) { if (count($v2) > count($v1)) { if (isset($versions[$v2[$i]])) { $compare = ($versions[$v2[$i]] < 4) ? 1 : -1; } else { $compare = -1; } } elseif (count($v2) < count($v1)) { if (isset($versions[$v1[$i]])) { $compare = ($versions[$v1[$i]] < 4) ? -1 : 1; } else { $compare = 1; } } } // Compare the versions if (func_num_args() > 2) { switch ($operator) { case '>': case 'gt': return (bool) ($compare > 0); break; case '>=': case 'ge': return (bool) ($compare >= 0); break; case '<=': case 'le': return (bool) ($compare <= 0); break; case '==': case '=': case 'eq': return (bool) ($compare == 0); break; case '<>': case '!=': case 'ne': return (bool) ($compare != 0); break; case '': case '<': case 'lt': return (bool) ($compare < 0); break; default: return; } } return $compare; } } ?>id='n89' href='#n89'>89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
#include "validator.h"

#include <iostream>

#include "proto/human.pb.h"
#include "structs.h"
#include "util/identifiers.h"

namespace com::fourisland::lingo2_archipelago {
namespace {

class Validator {
 public:
  explicit Validator(const CollectedInfo& info) : info_(info) {}

  void Validate() const {
    for (const auto& [map_name, map_info] : info_.maps) {
      ValidateMap(map_name, map_info);
    }
    for (const auto& [room_identifier, room_info] : info_.rooms) {
      ValidateRoom(room_identifier, room_info);
    }
    for (const auto& [door_identifier, door_info] : info_.doors) {
      ValidateDoor(door_identifier, door_info);
    }
    for (const auto& [port_identifier, port_info] : info_.ports) {
      ValidatePort(port_identifier, port_info);
    }
    for (const auto& [painting_identifier, painting_info] : info_.paintings) {
      ValidatePainting(painting_identifier, painting_info);
    }
    for (const auto& [panel_identifier, panel_info] : info_.panels) {
      ValidatePanel(panel_identifier, panel_info);
    }
    for (const auto& [keyholder_identifier, keyholder_info] :
         info_.keyholders) {
      ValidateKeyholder(keyholder_identifier, keyholder_info);
    }
    for (const auto& [letter_identifier, letter_info] : info_.letters) {
      ValidateLetter(letter_identifier, letter_info);
    }
    for (const auto& [ending_name, ending_info] : info_.endings) {
      ValidateEnding(ending_name, ending_info);
    }
    for (const auto& [panel_name, panel_info] : info_.panel_names) {
      ValidatePanelName(panel_name, panel_info);
    }
    for (const auto& [prog_name, prog_info] : info_.progressives) {
      ValidateProgressive(prog_name, prog_info);
    }
    for (const auto& [group_name, group_info] : info_.door_groups) {
      ValidateDoorGroup(group_name, group_info);
    }
  }

 private:
  void ValidateMap(const std::string& map_name, const MapInfo& map_info) const {
    for (const auto& [node_path, node_info] : map_info.game_nodes) {
      if (node_info.uses > 1) {
        std::cout << "Map " << map_name << " node " << node_path
                  << " is used in multiple places." << std::endl;
      } else if (node_info.uses == 0) {
        std::cout << "Map " << map_name << " node " << node_path
                  << " is not used." << std::endl;
      }

      if (!node_info.defined) {
        std::cout << "Map " << map_name << " node " << node_path
                  << " is not defined in the game file." << std::endl;
      }
    }
  }

  void ValidateRoom(const RoomIdentifier& room_identifier,
                    const RoomInfo& room_info) const {
    if (room_info.definitions.empty()) {
      std::cout << "Room " << room_identifier.ShortDebugString()
                << " has no definition, but was referenced:" << std::endl;

      for (const DoorIdentifier& door_identifier :
           room_info.doors_referenced_by) {
        std::cout << "    DOOR " << door_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const PanelIdentifier& panel_identifier :
           room_info.panels_referenced_by) {
        std::cout << "    PANEL " << panel_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const HumanConnection& connection :
           room_info.connections_referenced_by) {
        std::cout << "    CONNECTION " << connection.ShortDebugString()
                  << std::endl;
      }
    } else if (room_info.definitions.size() > 1) {
      std::cout << "Room " << room_identifier.ShortDebugString()
                << " was defined multiple times." << std::endl;
    }
  }

  bool DoesDoorNeedLocationName(const HumanDoor& h_door,
                                const std::string& map_name) const {
    if (h_door.type() != DoorType::STANDARD) {
      return false;
    }

    if (h_door.keyholders_size() > 0 || h_door.endings_size() > 0 ||
        h_door.complete_at() > 0) {
      return true;
    }

    if (h_door.panels_size() > 4) {
      return true;
    }

    std::set<std::string> map_areas;
    for (const PanelIdentifier& pi : h_door.panels()) {
      auto full_pi =
          GetCompletePanelIdentifierWithoutAnswer(pi, map_name, std::nullopt);
      if (full_pi) {
        auto panel_info_it = info_.panels.find(*full_pi);
        if (panel_info_it != info_.panels.end()) {
          const PanelInfo& panel_info = panel_info_it->second;

          map_areas.insert(panel_info.map_area_name);
        }
      }
    }

    if (map_areas.size() > 1) {
      return true;
    }

    return false;
  }

  void ValidateDoor(const DoorIdentifier& door_identifier,
                    const DoorInfo& door_info) const {
    if (door_info.definitions.empty()) {
      std::cout << "Door " << door_identifier.ShortDebugString()
                << " has no definition, but was referenced:" << std::endl;

      for (const DoorIdentifier& other_door_identifier :
           door_info.doors_referenced_by) {
        std::cout << "    DOOR " << other_door_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const PanelIdentifier& panel_identifier :
           door_info.panels_referenced_by) {
        std::cout << "    PANEL " << panel_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const PaintingIdentifier& painting_identifier :
           door_info.paintings_referenced_by) {
        std::cout << "    PAINTING " << painting_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const PortIdentifier& port_identifier :
           door_info.ports_referenced_by) {
        std::cout << "    PORT " << port_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const HumanConnection& connection :
           door_info.connections_referenced_by) {
        std::cout << "    CONNECTION " << connection.ShortDebugString()
                  << std::endl;
      }

      for (const std::string& prog_name :
           door_info.progressives_referenced_by) {
        std::cout << "    PROGRESSIVE " << prog_name << std::endl;
      }

      for (const std::string& group_name :
           door_info.door_groups_referenced_by) {
        std::cout << "    DOOR GROUP " << group_name << std::endl;
      }

      if (door_info.has_id) {
        std::cout << "    An AP ID is present." << std::endl;
      }
    } else if (door_info.definitions.size() > 1) {
      std::cout << "Door " << door_identifier.ShortDebugString()
                << " was defined multiple times." << std::endl;
    }

    if (door_info.malformed_identifiers.HasAny()) {
      std::cout << "Door " << door_identifier.ShortDebugString()
                << " has malformed identifiers:" << std::endl;

      for (const PaintingIdentifier& painting_identifier :
           door_info.malformed_identifiers.paintings) {
        std::cout << "    PAINTING " << painting_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const PanelIdentifier& panel_identifier :
           door_info.malformed_identifiers.panels) {
        std::cout << "    PANEL " << panel_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const KeyholderIdentifier& keyholder_identifier :
           door_info.malformed_identifiers.keyholders) {
        std::cout << "    KEYHOLDER " << keyholder_identifier.ShortDebugString()
                  << std::endl;
      }
    }

    for (const HumanDoor& h_door : door_info.definitions) {
      if (DoesDoorNeedLocationName(h_door, door_identifier.map()) &&
          !h_door.has_location_name()) {
        std::cout << "Door " << door_identifier.ShortDebugString()
                  << " needs an explicit location name." << std::endl;
      }

      if (h_door.double_letters() &&
          (h_door.type() == DoorType::STANDARD ||
           h_door.type() == DoorType::LOCATION_ONLY ||
           h_door.type() == DoorType::GRAVESTONE)) {
        std::cout << "Door " << door_identifier.ShortDebugString()
                  << " is a location that depends on double_letters."
                  << std::endl;
      }

      bool needs_id = (h_door.type() != DoorType::EVENT);
      if (door_info.has_id != needs_id) {
        if (needs_id) {
          std::cout << "Door " << door_identifier.ShortDebugString()
                    << " is missing an AP ID." << std::endl;
        } else {
          std::cout << "Door " << door_identifier.ShortDebugString()
                    << " should not have an AP ID." << std::endl;
        }
      }
    }
  }

  void ValidatePort(const PortIdentifier& port_identifier,
                    const PortInfo& port_info) const {
    if (port_info.definitions.empty()) {
      std::cout << "Port " << port_identifier.ShortDebugString()
                << " has no definition, but was referenced:" << std::endl;

      for (const HumanConnection& connection :
           port_info.connections_referenced_by) {
        std::cout << "    CONNECTION " << connection.ShortDebugString()
                  << std::endl;
      }
    } else if (port_info.definitions.size() > 1) {
      std::cout << "Port " << port_identifier.ShortDebugString()
                << " was defined multiple times." << std::endl;
    }

    if (!port_info.target_connections_referenced_by.empty()) {
      for (const HumanPort& port : port_info.definitions) {
        if (port.has_required_door()) {
          std::cout << "Port " << port_identifier.ShortDebugString()
                    << " has a required door but is the target of a connection:"
                    << std::endl;

          for (const HumanConnection& connection :
               port_info.target_connections_referenced_by) {
            std::cout << "    CONNECTION " << connection.ShortDebugString()
                      << std::endl;
          }
        }
      }
    }
  }

  void ValidatePainting(const PaintingIdentifier& painting_identifier,
                        const PaintingInfo& painting_info) const {
    if (painting_info.definitions.empty()) {
      std::cout << "Painting " << painting_identifier.ShortDebugString()
                << " has no definition, but was referenced:" << std::endl;

      for (const DoorIdentifier& door_identifier :
           painting_info.doors_referenced_by) {
        std::cout << "    DOOR " << door_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const HumanConnection& connection :
           painting_info.connections_referenced_by) {
        std::cout << "    CONNECTION " << connection.ShortDebugString()
                  << std::endl;
      }
    } else if (painting_info.definitions.size() > 1) {
      std::cout << "Painting " << painting_identifier.ShortDebugString()
                << " was defined multiple times." << std::endl;
    }

    if (!painting_info.target_connections_referenced_by.empty()) {
      for (const HumanPainting& painting : painting_info.definitions) {
        if (painting.has_required_door()) {
          std::cout << "Painting " << painting_identifier.ShortDebugString()
                    << " has a required door but is the target of a connection:"
                    << std::endl;

          for (const HumanConnection& connection :
               painting_info.target_connections_referenced_by) {
            std::cout << "    CONNECTION " << connection.ShortDebugString()
                      << std::endl;
          }
        }
      }
    }
  }

  void ValidatePanel(const PanelIdentifier& panel_identifier,
                     const PanelInfo& panel_info) const {
    if (panel_identifier.name().empty()) {
      std::cout << "Panel " << panel_identifier.ShortDebugString()
                << " has no name." << std::endl;
    }

    if (panel_info.definitions.empty()) {
      std::cout << "Panel " << panel_identifier.ShortDebugString()
                << " has no definition, but was referenced:" << std::endl;

      for (const DoorIdentifier& door_identifier :
           panel_info.doors_referenced_by) {
        std::cout << "    DOOR " << door_identifier.ShortDebugString()
                  << std::endl;
      }

      for (const HumanConnection& connection :
           panel_info.connections_referenced_by) {
        std::cout << "    CONNECTION " << connection.ShortDebugString()
                  << std::endl;
      }

      if (panel_info.has_id) {
        std::cout << "    An AP ID is present." << std::endl;
      }
    } else if (panel_info.definitions.size() > 1) {
      std::cout << "Panel " << panel_identifier.ShortDebugString()
                << " was defined multiple times." << std::endl;
    }

    for (const auto& [answer, proxy_info] : panel_info.proxies) {
      if (proxy_info.definitions.empty()) {
        std::cout << "Panel " << panel_identifier.ShortDebugString()
                  << " with answer \"" << answer
                  << "\" has no definition, but was referenced:" << std::endl;

        for (const DoorIdentifier& door_identifier :
             proxy_info.doors_referenced_by) {
          std::cout << "    DOOR " << door_identifier.ShortDebugString()
                    << std::endl;
        }

        for (const HumanConnection& connection :
             proxy_info.connections_referenced_by) {
          std::cout << "    CONNECTION " << connection.ShortDebugString()
                    << std::endl;
        }
      } else if (proxy_info.definitions.size() > 1) {
        std::cout << "Panel " << panel_identifier.ShortDebugString()
                  << " with answer \"" << answer
                  << "\" was defined multiple times." << std::endl;
      }
    }

    if (!panel_info.has_id) {
      std::cout << "Panel " << panel_identifier.ShortDebugString()
                << " is missing an AP ID." << std::endl;
    }

    if (!panel_info.target_connections_referenced_by.empty()) {
      for (const HumanPanel& panel : panel_info.definitions) {
        if (panel.has_required_door()) {
          std::cout << "Panel " << panel_identifier.ShortDebugString()
                    << " has a required door but is the target of a connection:"
                    << std::endl;

          for (const HumanConnection& connection :
               panel_info.target_connections_referenced_by) {
            std::cout << "    CONNECTION " << connection.ShortDebugString()
                      << std::endl;
          }
        }
      }
    }
  }

  void ValidateKeyholder(const KeyholderIdentifier& keyholder_identifier,
                         const KeyholderInfo& keyholder_info) const {
    if (keyholder_info.definitions.empty()) {
      std::cout << "Keyholder " << keyholder_identifier.ShortDebugString()
                << " has no definition, but was referenced:" << std::endl;

      for (const DoorIdentifier& door_identifier :
           keyholder_info.doors_referenced_by) {
        std::cout << "    DOOR " << door_identifier.ShortDebugString()
                  << std::endl;
      }

      if (keyholder_info.has_id) {
        std::cout << "    An AP ID is present." << std::endl;
      }
    } else if (keyholder_info.definitions.size() > 1) {
      std::cout << "Keyholder " << keyholder_identifier.ShortDebugString()
                << " was defined multiple times." << std::endl;
    }

    for (const HumanKeyholder& h_keyholder : keyholder_info.definitions) {
      bool needs_id = (h_keyholder.has_key());

      if (needs_id != keyholder_info.has_id) {
        if (needs_id) {
          std::cout << "Keyholder " << keyholder_identifier.ShortDebugString()
                    << " is missing an AP ID." << std::endl;
        } else {
          std::cout << "Keyholder " << keyholder_identifier.ShortDebugString()
                    << " should not have an AP ID." << std::endl;
        }
      }
    }
  }

  void ValidateLetter(const LetterIdentifier& letter_identifier,
                      const LetterInfo& letter_info) const {
    std::string letter_name = std::string(1, std::get<0>(letter_identifier)) +
                              (std::get<1>(letter_identifier) ? "2" : "1");

    if (letter_info.defined_in.empty()) {
      std::cout << "Letter " << letter_name
                << " has no definition, but was referenced:" << std::endl;

      if (letter_info.has_id) {
        std::cout << "    An AP ID is present." << std::endl;
      }
    } else if (letter_info.defined_in.size() > 1) {
      std::cout << "Letter " << letter_name
                << " was defined in multiple places:" << std::endl;

      for (const RoomIdentifier& room_identifier : letter_info.defined_in) {
        std::cout << "    " << room_identifier.ShortDebugString() << std::endl;
      }
    }

    if (!letter_info.has_id) {
      std::cout << "Letter " << letter_name << " is missing an AP ID."
                << std::endl;
    }
  }

  void ValidateEnding(const std::string& ending_name,
                      const EndingInfo& ending_info) const {
    if (ending_info.defined_in.empty()) {
      std::cout << "Ending " << ending_name
                << " has no definition, but was referenced:" << std::endl;

      for (const DoorIdentifier& door_identifier :
           ending_info.doors_referenced_by) {
        std::cout << "    DOOR " << door_identifier.ShortDebugString()
                  << std::endl;
      }

      if (ending_info.has_id) {
        std::cout << "    An AP ID is present." << std::endl;
      }
    } else if (ending_info.defined_in.size() > 1) {
      std::cout << "Ending " << ending_name
                << " was defined in multiple places:" << std::endl;

      for (const RoomIdentifier& room_identifier : ending_info.defined_in) {
        std::cout << "    " << room_identifier.ShortDebugString() << std::endl;
      }
    }

    if (!ending_info.has_id) {
      std::cout << "Ending " << ending_name << " is missing an AP ID."
                << std::endl;
    }
  }

  void ValidatePanelName(const std::string& panel_name,
                         const PanelNameInfo& panel_info) const {
    if (panel_info.panels_used_by.size() > 1) {
      std::cout << "The location name \"" << panel_name
                << "\" is used by multiple panels:" << std::endl;

      for (const PanelIdentifier& panel_identifier :
           panel_info.panels_used_by) {
        std::cout << "    PANEL " << panel_identifier.ShortDebugString()
                  << std::endl;
      }
    }
  }

  void ValidateProgressive(const std::string& prog_name,
                           const ProgressiveInfo& prog_info) const {
    if (prog_info.definitions.empty()) {
      std::cout << "Progressive \"" << prog_name
                << "\" has no definition, but was referenced:" << std::endl;

      if (prog_info.has_id) {
        std::cout << "    An AP ID is present." << std::endl;
      }
    } else if (prog_info.definitions.size() > 1) {
      std::cout << "Progressive \"" << prog_name
                << "\" has multiple definitions." << std::endl;
    }

    if (!prog_info.has_id) {
      std::cout << "Progressive \"" << prog_name << "\" is missing an AP ID."
                << std::endl;
    }
  }

  void ValidateDoorGroup(const std::string& group_name,
                         const DoorGroupInfo& group_info) const {
    if (group_info.definitions.empty()) {
      std::cout << "Door group \"" << group_name
                << "\" has no definition, but was referenced:" << std::endl;

      if (group_info.has_id) {
        std::cout << "    An AP ID is present." << std::endl;
      }
    } else if (group_info.definitions.size() > 1) {
      std::cout << "Door group \"" << group_name
                << "\" has multiple definitions." << std::endl;
    }

    if (!group_info.has_id) {
      std::cout << "Door group \"" << group_name << "\" is missing an AP ID."
                << std::endl;
    }
  }

  const CollectedInfo& info_;
};

}  // namespace

void ValidateCollectedInfo(const CollectedInfo& info) {
  Validator validator(info);
  validator.Validate();
}

}  // namespace com::fourisland::lingo2_archipelago