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
  • S @ salazarcosplay:
    Im gonna see if I can find a ps4 to buy
  • S @ salazarcosplay:
    now that firm ware 11 supposedly is exploitable
  • S @ salazarcosplay:
    did you see the fallout series
  • BigOnYa @ BigOnYa:
    Yea is pretty good
  • BakerMan @ BakerMan:
    an elder scrolls movie or show would be cool, but which elder scrolls game would it be based on?
  • BakerMan @ BakerMan:
    oh who am i kidding it'd be skyrim
    +1
  • BakerMan @ BakerMan:
    but,since they're only a few years apart, a morrowind + oblivion series would also be cool
  • K3Nv2 @ K3Nv2:
    Taco Saturday
  • AncientBoi @ AncientBoi:
    Uhh, It's 🌯 Saturday dude. :) js
  • BigOnYa @ BigOnYa:
    Nope that for tomorrow, cinco de mayo, today is bbq chicken on the grill.
  • K3Nv2 @ K3Nv2:
    Juan's new years I forgot
    +2
  • AncientBoi @ AncientBoi:
    :hrth::toot::grog::grog::grog::bow: HAPPY BIRTHDAY to me :bow::grog::grog::toot::hrth:
  • K3Nv2 @ K3Nv2:
    One day away from Juan's birthday
  • K3Nv2 @ K3Nv2:
    Only if you send him feet
    +1
  • BigOnYa @ BigOnYa:
    Happy birthday!
    +1
  • AncientBoi @ AncientBoi:
    Thank You :D
  • realtimesave @ realtimesave:
    heh I got a guy who created an account just yesterday asking me where to find mig switch roms
  • realtimesave @ realtimesave:
    too much FBI watching this website to answer that kind of question lol
  • K3Nv2 @ K3Nv2:
    Has the mig switch found loopholes without requiring game keys?
  • Xdqwerty @ Xdqwerty:
    @AncientBoi, happy birthday
    Xdqwerty @ Xdqwerty: