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.
  • Xdqwerty @ Xdqwerty:
    good night
  • BakerMan @ BakerMan:
    as to you
  • K3Nv2 @ K3Nv2:
    How do you know if the night will be good when you're asleep
  • BakerMan @ BakerMan:
    because i didn't say i was asleep
  • BakerMan @ BakerMan:
    i said i was sleeping...
  • BakerMan @ BakerMan:
    sleeping with uremum
  • K3Nv2 @ K3Nv2:
    Even my mum slept on that uremum
  • TwoSpikedHands @ TwoSpikedHands:
    yall im torn... ive been hacking away at tales of phantasia GBA (the USA version) and have so many documents of reverse engineering i've done
  • TwoSpikedHands @ TwoSpikedHands:
    I just found out that the EU version is better in literally every way, better sound quality, better lighting, and there's even a patch someone made to make the text look nicer
  • TwoSpikedHands @ TwoSpikedHands:
    Do I restart now using what i've learned on the EU version since it's a better overall experience? or do I continue with the US version since that is what ive been using, and if someone decides to play my hack, it would most likely be that version?
  • Sicklyboy @ Sicklyboy:
    @TwoSpikedHands, I'll preface this with the fact that I know nothing about the game, but, I think it depends on what your goals are. Are you trying to make a definitive version of the game? You may want to refocus your efforts on the EU version then. Or, are you trying to make a better US version? In which case, the only way to make a better US version is to keep on plugging away at that one ;)
  • Sicklyboy @ Sicklyboy:
    I'm not familiar with the technicalities of the differences between the two versions, but I'm wondering if at least some of those differences are things that you could port over to the US version in your patch without having to include copyrighted assets from the EU version
  • TwoSpikedHands @ TwoSpikedHands:
    @Sicklyboy I am wanting to fully change the game and bend it to my will lol. I would like to eventually have the ability to add more characters, enemies, even have a completely different story if i wanted. I already have the ability to change the tilemaps in the US version, so I can basically make my own map and warp to it in game - so I'm pretty far into it!
  • TwoSpikedHands @ TwoSpikedHands:
    I really would like to make a hack that I would enjoy playing, and maybe other people would too. swapping to the EU version would also mean my US friends could not legally play it
  • TwoSpikedHands @ TwoSpikedHands:
    I am definitely considering porting over some of the EU features without using the actual ROM itself, tbh that would probably be the best way to go about it... but i'm sad that the voice acting is so.... not good on the US version. May not be a way around that though
  • TwoSpikedHands @ TwoSpikedHands:
    I appreciate the insight!
  • The Real Jdbye @ The Real Jdbye:
    @TwoSpikedHands just switch, all the knowledge you learned still applies and most of the code and assets should be the same anyway
  • The Real Jdbye @ The Real Jdbye:
    and realistically they wouldn't

    be able to play it legally anyway since they need a ROM and they probably don't have the means to dump it themselves
  • The Real Jdbye @ The Real Jdbye:
    why the shit does the shitbox randomly insert newlines in my messages
  • Veho @ Veho:
    It does that when I edit a post.
  • Veho @ Veho:
    It inserts a newline in a random spot.
  • The Real Jdbye @ The Real Jdbye:
    never had that i don't think
    The Real Jdbye @ The Real Jdbye: never had that i don't think