Skip to content

Commit

Permalink
Update for libnds 2.0.0 -- DSi Wi-Fi!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Epicpkmn11 committed Nov 12, 2024
1 parent cc5fdd6 commit d37aef1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 64 deletions.
62 changes: 32 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,23 @@ NITRO :=
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -marm -mthumb-interwork -march=armv5te -mtune=arm946e-s
ARCH := -march=armv5te -mtune=arm946e-s

CFLAGS := -g -Wall -Wno-psabi -O3\
$(ARCH) $(INCLUDE) -DARM9
CFLAGS := -g -Wall -O2 -ffunction-sections -fdata-sections\
$(ARCH) $(INCLUDE) -DARM9
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project (order is important)
#---------------------------------------------------------------------------------
LIBS := -lqrencode -lfat -ldswifi9 -lmm9 -lnds9
LIBS := -lqrencode -lfat -ldswifi9 -lnds9

# automagically add maxmod library
ifneq ($(strip $(AUDIO)),)
LIBS := -lmm9 $(LIBS)
endif

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
Expand All @@ -77,9 +82,9 @@ ifneq ($(BUILD),$(notdir $(CURDIR)))
export OUTPUT := $(CURDIR)/$(TARGET)

export VPATH := $(CURDIR)/$(subst /,,$(dir $(ICON)))\
$(foreach dir,$(SOURCES),$(CURDIR)/$(dir))\
$(foreach dir,$(DATA),$(CURDIR)/$(dir))\
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir))
$(foreach dir,$(SOURCES),$(CURDIR)/$(dir))\
$(foreach dir,$(DATA),$(CURDIR)/$(dir))\
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir))

export DEPSDIR := $(CURDIR)/$(BUILD)

Expand All @@ -91,34 +96,34 @@ BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) $(PNGFILES:.

# prepare NitroFS directory
ifneq ($(strip $(NITRO)),)
export NITRO_FILES := $(CURDIR)/$(NITRO)
export NITRO_FILES := $(CURDIR)/$(NITRO)
endif

# get audio list for maxmod
ifneq ($(strip $(AUDIO)),)
export MODFILES := $(foreach dir,$(notdir $(wildcard $(AUDIO)/*.*)),$(CURDIR)/$(AUDIO)/$(dir))
export MODFILES := $(foreach dir,$(notdir $(wildcard $(AUDIO)/*.*)),$(CURDIR)/$(AUDIO)/$(dir))

# place the soundbank file in NitroFS if using it
ifneq ($(strip $(NITRO)),)
export SOUNDBANK := $(NITRO_FILES)/soundbank.bin
# place the soundbank file in NitroFS if using it
ifneq ($(strip $(NITRO)),)
export SOUNDBANK := $(NITRO_FILES)/soundbank.bin

# otherwise, needs to be loaded from memory
else
export SOUNDBANK := soundbank.bin
BINFILES += $(SOUNDBANK)
endif
# otherwise, needs to be loaded from memory
else
export SOUNDBANK := soundbank.bin
BINFILES += $(SOUNDBANK)
endif
endif

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
Expand All @@ -131,10 +136,10 @@ export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)

export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))

export INCLUDE := $(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir))\
$(foreach dir,$(LIBDIRS),-I$(dir)/include)\
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export INCLUDE := $(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir))\
$(foreach dir,$(LIBDIRS),-I$(dir)/include)\
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)

.PHONY: $(BUILD) all clean

Expand Down Expand Up @@ -171,15 +176,15 @@ endif
all: $(OUTPUT).nds $(OUTPUT).dsi

$(OUTPUT).nds: $(OUTPUT).elf $(NITRO_FILES) $(GAME_ICON)
$(SILENTCMD)ndstool -c $@ -9 $(OUTPUT).elf $(_ADDFILES) \
$(SILENTCMD)ndstool -c $@ -9 $(OUTPUT).elf $(_ARM7_ELF) $(_ADDFILES) \
-z 80040000
$(SILENTCMD)$(PYTHON) ../animatedbannerpatch.py $@ ../banner.bin
$(SILENTCMD)$(PYTHON) ../animatedbannerpatch.py $@ $(BANNER)
@echo built ... $(notdir $@)

$(OUTPUT).dsi: $(OUTPUT).elf $(NITRO_FILES) $(GAME_ICON)
$(SILENTCMD)ndstool -c $@ -9 $(OUTPUT).elf $(_ADDFILES) \
$(SILENTCMD)ndstool -c $@ -9 $(OUTPUT).elf $(_ARM7_ELF) $(_ADDFILES) \
-g $(GAME_CODE) 00 "WORDLE DS" -z 80040000 -u 00030004
$(SILENTCMD)$(PYTHON) ../animatedbannerpatch.py $@ ../banner.bin
$(SILENTCMD)$(PYTHON) ../animatedbannerpatch.py $@ $(BANNER)
@echo built ... $(notdir $@)

$(OUTPUT).elf: $(OFILES)
Expand All @@ -190,9 +195,6 @@ $(OFILES_SOURCES) : $(HFILES)
# need to build soundbank first
$(OFILES): $(SOUNDBANK)

banner.bin:
@python3

#---------------------------------------------------------------------------------
# rule to build solution from music files
#---------------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions source/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ Game::Game() :

// Check if bootstub exists
extern char *fake_heap_end;
__bootstub *bootstub = (struct __bootstub *)fake_heap_end;
_bootstubExists = bootstub->bootsig == BOOTSIG;
EnvNdsBootstubHeader *bootstub = (struct EnvNdsBootstubHeader *)fake_heap_end;
_bootstubExists = bootstub->magic == ENV_NDS_BOOTSTUB_MAGIC;
}

Game::~Game() {
Expand Down Expand Up @@ -141,7 +141,7 @@ bool Game::run() {
u16 pressed, held;
char16_t key = NOKEY;
touchPosition touch;
while(1) {
while(pmMainLoop()) {
do {
swiWaitForVBlank();
scanKeys();
Expand All @@ -160,7 +160,7 @@ bool Game::run() {

if(!Gfx::popupVisible() && _showRefresh && !_data.refreshSprite().visible())
_data.refreshSprite().visible(true).update();
} while(!pressed && key == Kbd::NOKEY);
} while(pmMainLoop() && (!pressed && key == Kbd::NOKEY));

// Process keyboard
switch(key) {
Expand Down
2 changes: 1 addition & 1 deletion source/howto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void howtoMenu(bool slowFade) {
scanKeys();
pressed = keysDown();
touchRead(&touch);
} while(!((pressed & (KEY_A | KEY_B)) || ((pressed & KEY_TOUCH) && (touch.px > 232 && touch.py < 24))));
} while(pmMainLoop() && (!((pressed & (KEY_A | KEY_B)) || ((pressed & KEY_TOUCH) && (touch.px > 232 && touch.py < 24)))));

Gfx::fadeOut(FADE_FAST, FADE_TOP | FADE_BOTTOM);

Expand Down
16 changes: 8 additions & 8 deletions source/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void Settings::showMenu() {

Gfx::fadeIn(FADE_FAST, FADE_BOTTOM);

while(1) {
while(pmMainLoop()) {
if(game->data().oldSettingsMenu()) {
hardToggle
.gfx(_hardMode ? game->data().toggleOnGfx() : game->data().toggleOffGfx())
Expand All @@ -207,7 +207,7 @@ void Settings::showMenu() {
swiWaitForVBlank();
scanKeys();
pressed = keysDown();
} while(!(pressed & (KEY_B | KEY_TOUCH)));
} while(pmMainLoop() && !(pressed & (KEY_B | KEY_TOUCH)));

if(pressed & KEY_B) {
break;
Expand Down Expand Up @@ -346,7 +346,7 @@ void Settings::gameSettings() {
Sprite musicToggle(false, SpriteSize_32x16, SpriteColorFormat_16Color);
musicToggle.move(game->data().musicToggle());

while(1) {
while(pmMainLoop()) {
hardModeToggle
.gfx(_hardMode ? game->data().toggleOnGfx() : game->data().toggleOffGfx())
.palette(_hardMode ? TilePalette::green : TilePalette::gray);
Expand All @@ -367,7 +367,7 @@ void Settings::gameSettings() {
swiWaitForVBlank();
scanKeys();
pressed = keysDown();
} while(!(pressed & (KEY_B | KEY_TOUCH)));
} while(pmMainLoop() && !(pressed & (KEY_B | KEY_TOUCH)));

if(pressed & KEY_B) {
break;
Expand Down Expand Up @@ -417,7 +417,7 @@ void Settings::shareMsgSettings() {
Sprite urlToggle(false, SpriteSize_32x16, SpriteColorFormat_16Color);
urlToggle.move(game->data().shareUrlToggle());

while(1) {
while(pmMainLoop()) {
timerToggle
.gfx(_shareTimer ? game->data().toggleOnGfx() : game->data().toggleOffGfx())
.palette(_shareTimer ? TilePalette::green : TilePalette::gray);
Expand All @@ -435,7 +435,7 @@ void Settings::shareMsgSettings() {
swiWaitForVBlank();
scanKeys();
pressed = keysDown();
} while(!(pressed & (KEY_B | KEY_TOUCH)));
} while(pmMainLoop() && !(pressed & (KEY_B | KEY_TOUCH)));

if(pressed & KEY_B) {
break;
Expand Down Expand Up @@ -501,7 +501,7 @@ void Settings::selectMod() {
break;
}
}
while(1) {
while(pmMainLoop()) {
// Scroll if needed
if(cursorPos < scrollPos)
scrollPos = cursorPos;
Expand All @@ -521,7 +521,7 @@ void Settings::selectMod() {
scanKeys();
pressed = keysDown();
held = keysDownRepeat();
} while(!(held & (KEY_UP | KEY_DOWN | KEY_LEFT | KEY_RIGHT) || pressed & (KEY_A | KEY_B | KEY_TOUCH)));
} while(pmMainLoop() && !(held & (KEY_UP | KEY_DOWN | KEY_LEFT | KEY_RIGHT) || pressed & (KEY_A | KEY_B | KEY_TOUCH)));

if(held & KEY_UP) {
if(cursorPos > 0)
Expand Down
6 changes: 3 additions & 3 deletions source/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool Stats::showQr() {
do {
swiWaitForVBlank();
scanKeys();
} while(!(keysDown() & (KEY_A | KEY_B | KEY_TOUCH)));
} while(pmMainLoop() && !(keysDown() & (KEY_A | KEY_B | KEY_TOUCH)));

return true;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ void Stats::showMenu() {
// Change to stats menu background
game->data().statsBottom().decompressAll(BG_SUB(0));

while(1) {
while(pmMainLoop()) {
// Print stats
if(game->data().oldStatsMenu()) {
// Old version without the average time
Expand Down Expand Up @@ -261,7 +261,7 @@ void Stats::showMenu() {
scanKeys();
pressed = keysDown();
touchRead(&touch);
} while(!(pressed & (KEY_A | KEY_B | KEY_TOUCH)));
} while(pmMainLoop() && !(pressed & (KEY_A | KEY_B | KEY_TOUCH)));

if(pressed & (KEY_A | KEY_B)) {
break;
Expand Down
52 changes: 35 additions & 17 deletions source/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "json.hpp"

#include <dswifi9.h>
#include <nds/interrupts.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string>
Expand All @@ -18,7 +19,7 @@ struct HttpResponse {

HttpResponse httpGet(const char *host, const char *path) {
char request[256];
snprintf(request, sizeof(request), "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Wordle DS\r\n\r\n", path, host);
snprintf(request, sizeof(request), "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: Wordle DS\r\n\r\n", path, host);

hostent *hostEnt = gethostbyname(host);
int sock = socket(AF_INET, SOCK_STREAM, 0);
Expand All @@ -35,13 +36,19 @@ HttpResponse httpGet(const char *host, const char *path) {
while(true) {
char buffer[256];
int len = recv(sock, buffer, 255, 0);
if(len > 0) {
buffer[len] = '\0';
res.content += buffer;

if(len < 255)
break;
}
buffer[len] = '\0';
res.content += buffer;

// Seems dumb, but waiting for the socket to timeout takes
// much longer than waiting a few vblanks
// (and if you don't add a bit of delay, it doesn't always
// have 255 ready)
// TODO: something better
swiWaitForVBlank();

if(len < 255)
break;
}

shutdown(sock, 0);
Expand All @@ -50,24 +57,35 @@ HttpResponse httpGet(const char *host, const char *path) {
if(res.content.compare(0, 9, "HTTP/1.1 ") == 0) {
res.status = atoi(res.content.c_str() + 9); // Get status code number
res.content = res.content.substr(res.content.find("\r\n\r\n") + 4); // Get content (after the headers)

// Parse chunked transfer
std::string parsed;
while(true) {
uint len = strtol(res.content.c_str(), NULL, 16);
if(len == 0)
break;

if(len > res.content.size())
return {0, "Request failed"};

parsed += res.content.substr(res.content.find("\r\n") + 2, len);
res.content = res.content.substr(res.content.find("\r\n") + 2 + len + 2);
}
res.content = parsed;

return res;
} else {
return {0, "Request failed"};
}

}

void WiFi::getWords(const char *url) {
Gfx::showPopup("Connecting to Wi-Fi");
if(!Wifi_CheckInit()) {
if(Wifi_InitDefault(WFC_CONNECT)) {
Gfx::showPopup("Connected to Wi-Fi!");
} else {
Gfx::showPopup("Failed to connect to Wi-Fi", 120);
return;
}
if(Wifi_InitDefault(WFC_CONNECT)) {
Gfx::showPopup("Connected to Wi-Fi!");
} else {
Wifi_EnableWifi();
Gfx::showPopup("Failed to connect to Wi-Fi", 120);
return;
}

// Parse out the host and request path
Expand All @@ -90,7 +108,7 @@ void WiFi::getWords(const char *url) {
// Try get the page
Gfx::showPopup("Updating words...");
HttpResponse res = httpGet(host, path);
Wifi_DisableWifi();
Wifi_DisconnectAP();

if(res.status == 200) {
// We should now have a JSON array of new IDs
Expand Down
Loading

0 comments on commit d37aef1

Please sign in to comment.