Hacking Gaming Misc Help for modding developement Nintendo Switch

Octokling

New Member
OP
Newbie
Joined
Jul 1, 2023
Messages
2
Trophies
0
XP
24
Country
France
I'm currently developing a mod for Super Mario Odyssey, and I've been stuck on something for quite a while.

I want to implement a private beta system in my mod by creating a web server that serves an API returning the UIDs of consoles authorized to use the mod. I also want to include an automatic update system using GitHub.

To achieve this, I'm downloading my data using the cURL library and storing the information from a data stream obtained from Amethyst-szs in the smo-lunakit repository.

Everything seems to be working fine until this point, but I'm having trouble parsing the response. The cJSON library (which I'm using for parsing) is returning NULL. The size of the API response is 4831 bytes.

The puzzling part is that parsing works with smaller API responses, such as 600 bytes.

I've debugged the issue and noticed that it's related to a memory allocation problem, but I can't figure out why.

Could you please help me identify what I've done wrong?

Thank you in advance for your assistance.


C++:
HOOK_DEFINE_TRAMPOLINE(GameSystemInit) {
    static void Callback(GameSystem *thisPtr) {

        sead::Heap* testHeap = sead::ExpHeap::create(50000000, "TestHeap",sead::HeapMgr::instance()->getCurrentHeap(), 8,
            sead::Heap::HeapDirection::cHeapDirection_Forward, false);
      
        Test::createInstance(testHeap);
      
        Orig(thisPtr);
        Test::instance()->test(testHeap);
    }
};


extern "C" void exl_main(void* x0, void* x1) {
    exl::hook::Initialize();
    runCodePatches();
    GameSystemInit::InstallAtOffset(0x535850);
}
extern "C" NORETURN void exl_exception_entry() {
    EXL_ABORT(0x420);
}
C++:
class Test{
    SEAD_SINGLETON_DISPOSER(Test);
    Test();
    ~Test();
public:
    void test(sead::Heap* heap);
    void init(sead::Heap* heap) { //Not used for the moment
        mHeap = heap;
        Logger::log("mHeap remplacé !");
    }
private:
    sead::Heap* mHeap;
};
C++:
#include "test.h"
#include "dataStream.h"

SEAD_SINGLETON_DISPOSER_IMPL(Test)
Test::Test() = default;
Test::~Test() = default;
void Test::test(sead::Heap* heap)
{
    sead::ScopedCurrentHeapSetter heapSetter(heap);
  
    DataStream dataStream = DataStream(15000);
    DataDownloader* download = new (heap) DataDownloader();
    download->DownloadString(dataStream, "link:\\willbosstwitchbot.glitch.me/devSMO"); //temporary link replace link:\\ than URL i can't send links
    download->~DataDownloader();
  
    cJSON* data = cJSON_Parse((const char*)dataStream.getData());
    if (data == nullptr) {
        Logger::log("Error : %s", cJSON_GetErrorPtr());
    }
    return;
}
C++:
#pragma once

#include "types.h"
#include "heap/seadHeapMgr.h"
#include "logger/Logger.hpp"
class DataStream {
    u8* mBuffer = nullptr;
    u32 mBufferSize = 0; //I just add this
    u32 mBufferMaxSize = 0;
    u32 mBufferPos = 0;
    sead::Heap* mHeap = nullptr;

    /// resizes the buffer by the amount specified.
    /// \param amt size to add to the buffer (not the new size of the buffer).
    /// \return True if reallocation was successful.
    /// \return False if heap cannot support new size, or reallocation fails.
    bool resize(u32 amt) {
        u32 newSize = mBufferMaxSize + (amt - (mBufferMaxSize - mBufferPos));
        if(mHeap->getFreeSize() > newSize) {
            // note: if reallocation fails, the game crashes
            u8* newBuffer = (u8*)mHeap->tryRealloc(mBuffer, newSize, sizeof(u8*));
            if(!newBuffer)
                return false;
            mBuffer = newBuffer;
            mBufferMaxSize = newSize; //I just add this
            return true;
        }
        return false;
    }
public:
    explicit DataStream(u32 startSize) {
        if(startSize > 0) {
            mBuffer = new u8[startSize]();
            mBufferMaxSize = startSize;
        }
        mBufferPos = 0;
        mHeap = sead::HeapMgr::instance()->getCurrentHeap();
    }

    ~DataStream() {
        Logger::log("Cleaning up Stream.\n");
        delete mBuffer;
    }

    template <typename T>
    u32 write(T* data, u32 len = sizeof(T)) {
        if (mBufferPos + len > mBufferMaxSize) {
            if (!resize(len))
                return 0;
        }
        memcpy(mBuffer + mBufferPos, data, len);
        mBufferPos += len;
        if (mBufferPos > mBufferSize)//I just add this
            mBufferSize = mBufferPos;//I just add this
        return len;
    }

    template <typename T>
    u32 read(T* data, u32 len = sizeof(T)) {
        u32 readSize = mBufferPos + len > mBufferMaxSize ? len : mBufferMaxSize - mBufferPos;
        memcpy(data, mBuffer + mBufferPos, readSize);
        mBufferPos += readSize;
        return readSize;
    }

    void rewind(u32 len = 0) {
        if(len == 0)
            mBufferPos = 0;
        else {
            mBufferPos -= len;
            if(mBufferPos < 0)
                mBufferPos = 0;
        }
    }

    void skip(u32 len) {
        if(mBufferPos + len > mBufferMaxSize) {
            if(!resize(len))
                return;
        }
        memset(mBuffer + mBufferPos, 0, len);
        mBufferPos += len;
    }

    u32 getSize() const {
        return mBufferSize + 1;
    }

    u8* getData() const {
        return mBuffer + 0;
    }

    bool isAtEnd() const {
        return mBufferPos == mBufferMaxSize;
    }

};

Furthermore, when I allocate too much memory to dataStream, specifically more than approximately 18,000 bytes my console freezes and with CPU core #1 reaching 100% usage.
 
Last edited by Octokling,

Octokling

New Member
OP
Newbie
Joined
Jul 1, 2023
Messages
2
Trophies
0
XP
24
Country
France
setting.hpp sets size of fake heap. Did you look at that?

Fake heap size by default is 20 kB.

It seems you are trying to get heap from game's memory, but I guess it's not properly used.
Thank you sooooooo much !
I wasn't aware of this file.
I had been struggling for over 2 months xDDD

I changed the value of HeapSize, and now it works.

Thank you very much once again.
:D:D:D
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • SylverReZ @ SylverReZ:
    They probably said "Hey, why not we combine the two together and make a 'new' DS to sell".
  • Veho @ Veho:
    It's a DS Lite in a slightly bigger DS Lite shell.
    +1
  • Veho @ Veho:
    It's not a Nintendo / iQue official product, it's a 3rd party custom.
    +1
  • Veho @ Veho:
    Nothing special about it other than it's more comfortable than the Lite
    for people with beefy hands.
    +1
  • Jayro @ Jayro:
    I have yaoi anime hands, very lorge but slender.
  • Jayro @ Jayro:
    I'm Slenderman.
  • Veho @ Veho:
    I have hands.
  • BakerMan @ BakerMan:
    imagine not having hands, cringe
    +1
  • AncientBoi @ AncientBoi:
    ESPECIALLY for things I do to myself :sad:.. :tpi::rofl2: Or others :shy::blush::evil:
    +1
  • The Real Jdbye @ The Real Jdbye:
    @SylverReZ if you could find a v5 DS ML you would have the best of both worlds since the v5 units had the same backlight brightness levels as the DS Lite unlockable with flashme
  • The Real Jdbye @ The Real Jdbye:
    but that's a long shot
  • The Real Jdbye @ The Real Jdbye:
    i think only the red mario kart edition phat was v5
  • BigOnYa @ BigOnYa:
    A woman with no arms and no legs was sitting on a beach. A man comes along and the woman says, "I've never been hugged before." So the man feels bad and hugs her. She says "Well i've also never been kissed before." So he gives her a kiss on the cheek. She says "Well I've also never been fucked before." So the man picks her up, and throws her in the ocean and says "Now you're fucked."
    +2
  • BakerMan @ BakerMan:
    lmao
  • BakerMan @ BakerMan:
    anyways, we need to re-normalize physical media

    if i didn't want my games to be permanent, then i'd rent them
    +1
  • BigOnYa @ BigOnYa:
    Agreed, that why I try to buy all my games on disc, Xbox anyways. Switch games (which I pirate tbh) don't matter much, I stay offline 24/7 anyways.
  • AncientBoi @ AncientBoi:
    I don't pirate them, I Use Them :mellow:. Like I do @BigOnYa 's couch :tpi::evil::rofl2:
    +1
  • cearp @ cearp:
    @BakerMan - you can still "own" digital media, arguably easier and better than physical since you can make copies and backups, as much as you like.

    The issue is DRM
  • cearp @ cearp:
    You can buy drm free games / music / ebooks, and if you keep backups of your data (like documents and family photos etc), then you shouldn't lose the game. but with a disk, your toddler could put it in the toaster and there goes your $60

    :rofl2:
  • cearp @ cearp:
    still, I agree physical media is nice to have. just pointing out the issue is drm
  • rqkaiju2 @ rqkaiju2:
    i like physical media because it actually feels like you own it. thats why i plan on burning music to cds
  • cearp @ cearp:
    It's nice to not have to have a lot of physical things though, saves space
    +1
  • AncientBoi @ AncientBoi:
    Nor clothes 🤮 . Saves on time, soap, water and money having to wash them. :D
    AncientBoi @ AncientBoi: Nor clothes 🤮 . Saves on time, soap, water and money having to wash them. :D