about summary refs log tree commit diff stats
path: root/src/game_data.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Added path tracking for debuggingStar Rauchenberger2024-07-161-1/+2
|
* Merge branch 'future'Star Rauchenberger2024-07-011-0/+4
|\
| * Merge branch 'tinysphere' into futureStar Rauchenberger2024-05-171-0/+4
| |\
| | * Expand sphere 1 in door shuffle + no ECHStar Rauchenberger2024-04-211-0/+4
| | |
| | * Merge branch 'future' into tinysphereStar Rauchenberger2024-04-211-61/+71
| | |\
* | | | Go back to old logging systemStar Rauchenberger2024-06-101-32/+31
| | | | | | | | | | | | | | | | Brought in libfmt to handle string formatting and replace a bunch of ostringstream uses.
* | | | Fixed subway map when eye_painting is mappedStar Rauchenberger2024-06-101-6/+4
| | | |
* | | | Fixed broken paintings and window repaintingStar Rauchenberger2024-06-081-6/+10
| | | |
* | | | Merge branch 'subway'Star Rauchenberger2024-06-061-48/+173
|\ \ \ \ | |/ / / |/| | |
| * | | Proper painting reachability detectionStar Rauchenberger2024-06-061-9/+25
| | | |
| * | | Show unchecked paintingsStar Rauchenberger2024-05-291-7/+26
| | | |
| * | | Fixed art gallery paintingStar Rauchenberger2024-05-151-0/+5
| | | |
| * | | Logging changesStar Rauchenberger2024-05-151-1/+6
| | | |
| * | | Warn on singleton subway tag dataStar Rauchenberger2024-05-141-0/+14
| | | |
| * | | Switch to wx loggingStar Rauchenberger2024-05-141-38/+21
| | | |
| * | | Hovered connections on subway map!Star Rauchenberger2024-05-141-1/+24
| | | |
| * | | Started making subway mapStar Rauchenberger2024-05-121-1/+61
| | | |
* | | | Handle panel location name config fieldStar Rauchenberger2024-05-171-6/+24
|/ / /
* | | Merge branch 'future'Star Rauchenberger2024-04-221-61/+71
|\ \ \ | |/ / |/| / | |/
| * Fix crossroads roof accessStar Rauchenberger2024-03-171-1/+1
| | | | | | | | honestly embarrassing
| * Added sunwarp shuffleStar Rauchenberger2024-03-021-1/+19
| |
| * Fix pilgrimage detection for vanilla doorsStar Rauchenberger2024-02-281-0/+19
| | | | | | | | Also add new pilgrimage options.
| * Added real pilgrimage detectionStar Rauchenberger2024-02-261-69/+34
| |
| * Added sunwarp access supportStar Rauchenberger2024-01-311-18/+26
| |
* | Fixed handling of door groupsStar Rauchenberger2024-04-011-2/+2
|/
* Fix some bad memory access stuffStar Rauchenberger2024-01-191-82/+99
|
* Stop relying on correctly set working directoryStar Rauchenberger2023-11-261-4/+8
|
* Fixed hunt-only areas not showing up for huntsStar Rauchenberger2023-11-171-0/+1
|
* Show hunt panels optionStar Rauchenberger2023-11-171-1/+6
|
* Use hardcoded AP ids nowStar Rauchenberger2023-09-171-5/+108
|
* Panelsanity supportStar Rauchenberger2023-08-251-48/+84
|
* Rewrote how panel solvability is determinedStar Rauchenberger2023-08-251-0/+5
| | | | This fixes an edge case with LEVEL 2, and likely fixes an undiscovered issue with no-doors Pilgrimage.
* LEVEL 2 and required_panel supportStar Rauchenberger2023-08-031-0/+28
|
* Doors show up in popups in definition orderStar Rauchenberger2023-07-031-1/+6
|
pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses
//   without any warranty.
//   by Gregory Pakosz (@gpakosz)
// https://github.com/gpakosz/whereami

// in case you want to #include "whereami.c" in a larger compilation unit
#if !defined(WHEREAMI_H)
#include <whereami.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__linux__) || defined(__CYGWIN__)
#undef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#elif defined(__APPLE__)
#undef _DARWIN_C_SOURCE
#define _DARWIN_C_SOURCE
#define _DARWIN_BETTER_REALPATH
#endif

#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
#include <stdlib.h>
#endif

#if !defined(WAI_MALLOC)
#define WAI_MALLOC(size) malloc(size)
#endif

#if !defined(WAI_FREE)
#define WAI_FREE(p) free(p)
#endif

#if !defined(WAI_REALLOC)
#define WAI_REALLOC(p, size) realloc(p, size)
#endif

#ifndef WAI_NOINLINE
#if defined(_MSC_VER)
#define WAI_NOINLINE __declspec(noinline)
#elif defined(__GNUC__)
#define WAI_NOINLINE __attribute__((noinline))
#else
#error unsupported compiler
#endif
#endif

#if defined(_MSC_VER)
#define WAI_RETURN_ADDRESS() _ReturnAddress()
#elif defined(__GNUC__)
#define WAI_RETURN_ADDRESS() \
  __builtin_extract_return_addr(__builtin_return_address(0))
#else
#error unsupported compiler
#endif

#if defined(_WIN32)

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#if defined(_MSC_VER)
#pragma warning(push, 3)
#endif
#include <intrin.h>
#include <windows.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#include <stdbool.h>

static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity,
                                      int* dirname_length) {
  wchar_t buffer1[MAX_PATH];
  wchar_t buffer2[MAX_PATH];
  wchar_t* path = NULL;
  int length = -1;
  bool ok;

  for (ok = false; !ok; ok = true) {
    DWORD size;
    int length_, length__;

    size = GetModuleFileNameW(module, buffer1,
                              sizeof(buffer1) / sizeof(buffer1[0]));

    if (size == 0)
      break;
    else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0]))) {
      DWORD size_ = size;
      do {
        wchar_t* path_;

        path_ = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_ * 2);
        if (!path_) break;
        size_ *= 2;
        path = path_;
        size = GetModuleFileNameW(module, path, size_);
      } while (size == size_);

      if (size == size_) break;
    } else
      path = buffer1;

    if (!_wfullpath(buffer2, path, MAX_PATH)) break;
    length_ = (int)wcslen(buffer2);
    length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, out, capacity,
                                   NULL, NULL);

    if (length__ == 0)
      length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0,
                                     NULL, NULL);
    if (length__ == 0) break;

    if (length__ <= capacity && dirname_length) {
      int i;

      for (i = length__ - 1; i >= 0; --i) {
        if (out[i] == '\\') {
          *dirname_length = i;
          break;
        }
      }
    }

    length = length__;
  }

  if (path != buffer1) WAI_FREE(path);

  return ok ? length : -1;
}

WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getExecutablePath)(
    char* out, int capacity, int* dirname_length) {
  return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length);
}

WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity,
                                                        int* dirname_length) {
  HMODULE module;
  int length = -1;

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4054)
#endif
  if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
                            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                        (LPCTSTR)WAI_RETURN_ADDRESS(), &module))
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
  {
    length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length);
  }

  return length;
}

#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || \
    defined(WAI_USE_PROC_SELF_EXE)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__linux__)
#include <linux/limits.h>
#else
#include <limits.h>
#endif
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#include <stdbool.h>

#if !defined(WAI_PROC_SELF_EXE)
#if defined(__sun)
#define WAI_PROC_SELF_EXE "/proc/self/path/a.out"
#else
#define WAI_PROC_SELF_EXE "/proc/self/exe"
#endif
#endif

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity,
                                  int* dirname_length) {
  char buffer[PATH_MAX];
  char* resolved = NULL;
  int length = -1;
  bool ok;

  for (ok = false; !ok; ok = true) {
    resolved = realpath(WAI_PROC_SELF_EXE, buffer);
    if (!resolved) break;

    length = (int)strlen(resolved);
    if (length <= capacity) {
      memcpy(out, resolved, length);

      if (dirname_length) {
        int i;

        for (i = length - 1; i >= 0; --i) {
          if (out[i] == '/') {
            *dirname_length = i;
            break;
          }
        }
      }
    }
  }

  return ok ? length : -1;
}

#if !defined(WAI_PROC_SELF_MAPS_RETRY)
#define WAI_PROC_SELF_MAPS_RETRY 5
#endif

#if !defined(WAI_PROC_SELF_MAPS)
#if defined(__sun)
#define WAI_PROC_SELF_MAPS "/proc/self/map"
#else
#define WAI_PROC_SELF_MAPS "/proc/self/maps"
#endif
#endif

#if defined(__ANDROID__) || defined(ANDROID)
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
#include <stdbool.h>

WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity,
                                                        int* dirname_length) {
  int length = -1;
  FILE* maps = NULL;

  for (int r = 0; r < WAI_PROC_SELF_MAPS_RETRY; ++r) {
    maps = fopen(WAI_PROC_SELF_MAPS, "r");
    if (!maps) break;

    for (;;) {
      char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX];
      uint64_t low, high;
      char perms[5];
      uint64_t offset;
      uint32_t major, minor;
      char path[PATH_MAX];
      uint32_t inode;

      if (!fgets(buffer, sizeof(buffer), maps)) break;

      if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n",
                 &low, &high, perms, &offset, &major, &minor, &inode,
                 path) == 8) {
        uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS();
        if (low <= addr && addr <= high) {
          char* resolved;

          resolved = realpath(path, buffer);
          if (!resolved) break;

          length = (int)strlen(resolved);
#if defined(__ANDROID__) || defined(ANDROID)
          if (length > 4 && buffer[length - 1] == 'k' &&
              buffer[length - 2] == 'p' && buffer[length - 3] == 'a' &&
              buffer[length - 4] == '.') {
            int fd = open(path, O_RDONLY);
            if (fd == -1) {
              length = -1;  // retry
              break;
            }

            char* begin = (char*)mmap(0, offset, PROT_READ, MAP_SHARED, fd, 0);
            if (begin == MAP_FAILED) {
              close(fd);
              length = -1;  // retry
              break;
            }

            char* p = begin + offset - 30;  // minimum size of local file header
            while (p >= begin)              // scan backwards
            {
              if (*((uint32_t*)p) ==
                  0x04034b50UL)  // local file header signature found
              {
                uint16_t length_ = *((uint16_t*)(p + 26));

                if (length + 2 + length_ < (int)sizeof(buffer)) {
                  memcpy(&buffer[length], "!/", 2);
                  memcpy(&buffer[length + 2], p + 30, length_);
                  length += 2 + length_;
                }

                break;
              }

              --p;
            }

            munmap(begin, offset);
            close(fd);
          }
#endif
          if (length <= capacity) {
            memcpy(out, resolved, length);

            if (dirname_length) {
              int i;

              for (i = length - 1; i >= 0; --i) {
                if (out[i] == '/') {
                  *dirname_length = i;
                  break;
                }
              }
            }
          }

          break;
        }
      }
    }

    fclose(maps);
    maps = NULL;

    if (length != -1) break;
  }

  return length;
}

#elif defined(__APPLE__)

#include <dlfcn.h>
#include <limits.h>
#include <mach-o/dyld.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity,
                                  int* dirname_length) {
  char buffer1[PATH_MAX];
  char buffer2[PATH_MAX];
  char* path = buffer1;
  char* resolved = NULL;
  int length = -1;
  bool ok;

  for (ok = false; !ok; ok = true) {
    uint32_t size = (uint32_t)sizeof(buffer1);
    if (_NSGetExecutablePath(path, &size) == -1) {
      path = (char*)WAI_MALLOC(size);
      if (!_NSGetExecutablePath(path, &size)) break;
    }

    resolved = realpath(path, buffer2);
    if (!resolved) break;

    length = (int)strlen(resolved);
    if (length <= capacity) {
      memcpy(out, resolved, length);

      if (dirname_length) {
        int i;

        for (i = length - 1; i >= 0; --i) {
          if (out[i] == '/') {
            *dirname_length = i;
            break;
          }
        }
      }
    }
  }

  if (path != buffer1) WAI_FREE(path);

  return ok ? length : -1;
}

WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity,
                                                        int* dirname_length) {
  char buffer[PATH_MAX];
  char* resolved = NULL;
  int length = -1;

  for (;;) {
    Dl_info info;

    if (dladdr(WAI_RETURN_ADDRESS(), &info)) {
      resolved = realpath(info.dli_fname, buffer);
      if (!resolved) break;

      length = (int)strlen(resolved);
      if (length <= capacity) {
        memcpy(out, resolved, length);

        if (dirname_length) {
          int i;

          for (i = length - 1; i >= 0; --i) {
            if (out[i] == '/') {
              *dirname_length = i;
              break;
            }
          }
        }
      }
    }

    break;
  }

  return length;
}

#elif defined(__QNXNTO__)

#include <dlfcn.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if !defined(WAI_PROC_SELF_EXE)
#define WAI_PROC_SELF_EXE "/proc/self/exefile"
#endif

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity,
                                  int* dirname_length) {
  char buffer1[PATH_MAX];
  char buffer2[PATH_MAX];
  char* resolved = NULL;
  FILE* self_exe = NULL;
  int length = -1;
  bool ok;

  for (ok = false; !ok; ok = true) {
    self_exe = fopen(WAI_PROC_SELF_EXE, "r");
    if (!self_exe) break;

    if (!fgets(buffer1, sizeof(buffer1), self_exe)) break;

    resolved = realpath(buffer1, buffer2);
    if (!resolved) break;

    length = (int)strlen(resolved);
    if (length <= capacity) {
      memcpy(out, resolved, length);

      if (dirname_length) {
        int i;

        for (i = length - 1; i >= 0; --i) {
          if (out[i] == '/') {
            *dirname_length = i;
            break;
          }
        }
      }
    }
  }

  fclose(self_exe);

  return ok ? length : -1;
}

WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) {
  char buffer[PATH_MAX];
  char* resolved = NULL;
  int length = -1;

  for (;;) {
    Dl_info info;

    if (dladdr(WAI_RETURN_ADDRESS(), &info)) {
      resolved = realpath(info.dli_fname, buffer);
      if (!resolved) break;

      length = (int)strlen(resolved);
      if (length <= capacity) {
        memcpy(out, resolved, length);

        if (dirname_length) {
          int i;

          for (i = length - 1; i >= 0; --i) {
            if (out[i] == '/') {
              *dirname_length = i;
              break;
            }
          }
        }
      }
    }

    break;
  }

  return length;
}

#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
    defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)

#include <dlfcn.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysctl.h>
#include <sys/types.h>

#if defined(__OpenBSD__)

#include <unistd.h>

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity,
                                  int* dirname_length) {
  char buffer1[4096];
  char buffer2[PATH_MAX];
  char buffer3[PATH_MAX];
  char** argv = (char**)buffer1;
  char* resolved = NULL;
  int length = -1;
  bool ok;

  for (ok = false; !ok; ok = true) {
    int mib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV};
    size_t size;

    if (sysctl(mib, 4, NULL, &size, NULL, 0) != 0) break;

    if (size > sizeof(buffer1)) {
      argv = (char**)WAI_MALLOC(size);
      if (!argv) break;
    }

    if (sysctl(mib, 4, argv, &size, NULL, 0) != 0) break;

    if (strchr(argv[0], '/')) {
      resolved = realpath(argv[0], buffer2);
      if (!resolved) break;
    } else {
      const char* PATH = getenv("PATH");
      if (!PATH) break;

      size_t argv0_length = strlen(argv[0]);

      const char* begin = PATH;
      while (1) {
        const char* separator = strchr(begin, ':');
        const char* end = separator ? separator : begin + strlen(begin);

        if (end - begin > 0) {
          if (*(end - 1) == '/') --end;

          if (((end - begin) + 1 + argv0_length + 1) <= sizeof(buffer2)) {
            memcpy(buffer2, begin, end - begin);
            buffer2[end - begin] = '/';
            memcpy(buffer2 + (end - begin) + 1, argv[0], argv0_length + 1);

            resolved = realpath(buffer2, buffer3);
            if (resolved) break;
          }
        }

        if (!separator) break;

        begin = ++separator;
      }

      if (!resolved) break;
    }

    length = (int)strlen(resolved);
    if (length <= capacity) {
      memcpy(out, resolved, length);

      if (dirname_length) {
        int i;

        for (i = length - 1; i >= 0; --i) {
          if (out[i] == '/') {
            *dirname_length = i;
            break;
          }
        }
      }
    }
  }

  if (argv != (char**)buffer1) WAI_FREE(argv);

  return ok ? length : -1;
}

#else

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity,
                                  int* dirname_length) {
  char buffer1[PATH_MAX];
  char buffer2[PATH_MAX];
  char* path = buffer1;
  char* resolved = NULL;
  int length = -1;
  bool ok;

  for (ok = false; !ok; ok = true) {
#if defined(__NetBSD__)
    int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
#else
    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
#endif
    size_t size = sizeof(buffer1);

    if (sysctl(mib, 4, path, &size, NULL, 0) != 0) break;

    resolved = realpath(path, buffer2);
    if (!resolved) break;

    length = (int)strlen(resolved);
    if (length <= capacity) {
      memcpy(out, resolved, length);

      if (dirname_length) {
        int i;

        for (i = length - 1; i >= 0; --i) {
          if (out[i] == '/') {
            *dirname_length = i;
            break;
          }
        }
      }
    }
  }

  return ok ? length : -1;
}

#endif

WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity,
                                                        int* dirname_length) {
  char buffer[PATH_MAX];
  char* resolved = NULL;
  int length = -1;

  for (;;) {
    Dl_info info;

    if (dladdr(WAI_RETURN_ADDRESS(), &info)) {
      resolved = realpath(info.dli_fname, buffer);
      if (!resolved) break;

      length = (int)strlen(resolved);
      if (length <= capacity) {
        memcpy(out, resolved, length);

        if (dirname_length) {
          int i;

          for (i = length - 1; i >= 0; --i) {
            if (out[i] == '/') {
              *dirname_length = i;
              break;
            }
          }
        }
      }
    }

    break;
  }

  return length;
}

#else

#error unsupported platform

#endif

#ifdef __cplusplus
}
#endif