about summary refs log tree commit diff stats
path: root/data/maps/the_liberated
Commit message (Expand)AuthorAgeFilesLines
* Annotate "worldport entrances"Star Rauchenberger2025-10-191-0/+4
* Added display names to portsStar Rauchenberger2025-09-281-0/+1
* [Data] Annotate shuffleable portsStar Rauchenberger2025-09-211-1/+2
* Changed how door location names are formattedStar Rauchenberger2025-08-303-2/+1
* Converted puzzle symbols to an enumStar Rauchenberger2025-08-201-8/+8
* Maps have display names nowStar Rauchenberger2025-08-201-0/+1
* Added the_liberatedStar Rauchenberger2025-08-154-0/+89
' href='#n98'>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
/*
 * Copyright (C) 2008 Tantric
 * Copyright (C) 2012 Pedro Aguiar
 * Copyright (C) 2017 hatkirby
 *
 * Originally part of the WiiTweet project, which was distributed under the
 * GPL.
 */
#include "netinf.h"
#include <gccore.h>
#include <network.h>
#include <ogc/lwp_watchdog.h>
#include <sys/errno.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

static bool networkInit = false;
static lwp_t networkThread = LWP_THREAD_NULL;
static u8 netStack[32768] ATTRIBUTE_ALIGN(32);
static char wiiIP[16] = {0};
static bool netHalt = false;

static void* interface(void* arg)
{
  static bool prevInit = false;

  while (!netHalt)
  {
    int retry = 5;
    int result = -1;

    while ((retry > 0) && (!netHalt))
    {
      if (prevInit)
      {
        net_deinit();

        for (int i=0; (i<400) && !netHalt; i++)
        {
          if (net_get_status() != -EBUSY)
          {
            usleep(2000);
            net_wc24cleanup();
            prevInit = false;
            usleep(20000);

            break;
          }

          usleep(20000);
        }
      }

      usleep(2000);

      if (net_init_async(NULL, NULL))
      {
        sleep(1);

        retry--;

        continue;
      }

      result = net_get_status();
      for (int wait = 400; (wait > 0) && (result == -EBUSY) && !netHalt; wait++)
      {
        usleep(20000);

        result = net_get_status();
      }

      if (result == 0)
      {
        break;
      }

      retry--;

      usleep(2000);
    }

    if (result == 0)
    {
      struct in_addr hostip;
      hostip.s_addr = net_gethostip();

      if (hostip.s_addr)
      {
        strcpy(wiiIP, inet_ntoa(hostip));
        networkInit = true;
        prevInit = true;
      }
    }

    if (!netHalt)
    {
      LWP_SuspendThread(networkThread);
    }
  }

  return NULL;
}

static void startNetworkThread()
{
  netHalt = false;

  if (networkThread == LWP_THREAD_NULL)
  {
    LWP_CreateThread(&networkThread, interface, NULL, netStack, 8192, 40);
  } else {
    LWP_ResumeThread(networkThread);
  }
}

static void stopNetworkThread()
{
  if ((networkThread == LWP_THREAD_NULL)
    || (!LWP_ThreadIsSuspended(networkThread)))
  {
    return;
  }

  netHalt = true;

  LWP_ResumeThread(networkThread);
  LWP_JoinThread(networkThread, NULL);

  networkThread = LWP_THREAD_NULL;
}

bool initializeNetwork()
{
  stopNetworkThread();

  if (networkInit && (net_gethostip() > 0))
  {
    return true;
  }

  networkInit = false;

  printf("Initializing network...\n");

  u64 start = gettime();
  startNetworkThread();

  while (!LWP_ThreadIsSuspended(networkThread))
  {
    usleep(50 * 1000);

    if (diff_sec(start, gettime()) > 10)
    {
      break;
    }
  }

  return networkInit;
}