
/*H********************************************************************************/
/*!
    \File legacy_dirtysdk_test_bench.cpp

    \Description
        Main file for LegacyDirtySDK Test Application.

    \Copyright
        Copyright (c) 2017 Electronic Arts Inc.

    \Version 01/19/2017 (amakoukji) First Version
*/
/********************************************************************************H*/

#include <stdlib.h>

#include "DirtySDK/platform.h"
#include "DirtySDK/dirtysock/dirtylib.h"
#include "DirtySDK/dirtysock/dirtymem.h"
#include "DirtySDK/dirtysock/netconn.h"

#if defined(DIRTYCODE_PS4)
#include "coreallocator/icoreallocator_interface.h"
#endif

#include "LegacyDirtySDK/misc/lobbylan.h"
#include "LegacyDirtySDK/misc/qosapi.h"
#include "LegacyDirtySDK/proto/protoping.h"
#include "LegacyDirtySDK/proto/protopingmanager.h"
#include "LegacyDirtySDK/util/sort.h"
#include "LegacyDirtySDK/util/tagfield.h"
#include "LegacyDirtySDK/web/netresource.h"
#include "LegacyDirtySDK/web/weboffer.h"

#if !EA_DLL
// Mem Stuff
void *DirtyMemAlloc(int32_t iSize, int32_t iMemModule, int32_t iMemGroup, void *pMemGroupUserData)
{
    return malloc(static_cast<size_t>(iSize));
}

void DirtyMemFree(void *pMem, int32_t iMemModule, int32_t iMemGroup, void *pMemGroupUserData)
{
    free(pMem);
}

void* operator new[](size_t size, const char* pName, int flags, unsigned debugFlags, const char* file, int line)
{
    int32_t iMemGroup;
    void *pMemGroupUserData;
    DirtyMemGroupQuery(&iMemGroup, &pMemGroupUserData);
    return DirtyMemAlloc((int32_t)size, 123456, iMemGroup, pMemGroupUserData);
}

void* operator new[](size_t size, size_t alignment, size_t alignmentOffset, const char* pName, int flags, unsigned debugFlags, const char* file, int line)
{
    int32_t iMemGroup;
    void *pMemGroupUserData;
    DirtyMemGroupQuery(&iMemGroup, &pMemGroupUserData);
    return DirtyMemAlloc((int32_t)size, 123456, iMemGroup, pMemGroupUserData);
}
#endif

#if defined(DIRTYCODE_PS4)
// DirtyCoreAllocator is a bare bones memory allocator that piggy-backs on top of DirtyMem functionality
class DirtyCoreAllocator : public EA::Allocator::ICoreAllocator
{
private:
     int32_t m_iMemModule;
     int32_t m_iMemGroup;
     void    *m_pMemGroupUserData;

public:
    DirtyCoreAllocator():
    m_iMemModule(DIRTYEVENT_DISP_MEMID),
    m_iMemGroup(-1),
    m_pMemGroupUserData(NULL)
    {

    }

    ~DirtyCoreAllocator() {}

    void *Alloc(size_t size, const char *name, unsigned int flags)
    {
        if (m_iMemGroup == -1)
        {
            DirtyMemGroupQuery(&m_iMemGroup, &m_pMemGroupUserData);  
        }
        return(DirtyMemAlloc((int32_t)size, m_iMemModule, m_iMemGroup, m_pMemGroupUserData));
    }

    void *Alloc(size_t size, const char *name, unsigned int flags, unsigned int align, unsigned int alignOffset = 0)
    {
        // Since we are using DirtyMemAlloc, which does not support alignment, just call the simplified Alloc
        return(Alloc(size, name, flags));
    }

    void Free(void *block, size_t size=0)
    {
        DirtyMemFree(block, m_iMemModule, m_iMemGroup, m_pMemGroupUserData);
    }
};

ICOREALLOCATOR_INTERFACE_API EA::Allocator::ICoreAllocator *EA::Allocator::ICoreAllocator::GetDefaultAllocator()
{
    // NOTE: we can't just use another global allocator object, since we'd have an initialization race condition between the two globals.
    //   to avoid that, we use a static function var, which is init'd the first time the function is called.
    static DirtyCoreAllocator sTestAllocator; // static function members are only constructed on first function call
    return &sTestAllocator;
}
#endif

// callbacks
CommRef* CommAllConstruct(int32_t maxwid, int32_t maxinp, int32_t maxout);
CommRef* CommAllConstruct(int32_t maxwid, int32_t maxinp, int32_t maxout)
{
    NetPrintf(("CommAllConstruct cb %d, %d, %d\n", maxwid, maxinp, maxout));
    return(NULL);
}

/*  
Note: This implementation is bare bones and only setup to make sure the modules moved from 
      DirtySDK link and load properly. Since these are legacy modules we do net expect 
      any testing will need to be done on them.
      If this changes in the future we can copy the old tests from Tester2 to this package.
*/


#if defined(DIRTYCODE_XBOXONE)
int main(Platform::Array<Platform::String^>^ args)
#else
int main()
#endif
{
    NetPrintf(("Legacy DirtySDK Test Bench running...\n"));

    DirtyAddrT myAddr;
    uint32_t addr = 0x0100007f;
    DirtyAddrFromHostAddr(&myAddr, &addr);
    LobbyLanRefT *pLobbyLanRef = LobbyLanCreate2("TestBenchLan", &myAddr, "legDsKind", 1, &CommAllConstruct);
    LobbyLanDestroy(pLobbyLanRef);

    NetConnStartup("-noupnp -singlethreaded -servicename=legacytest");

    QosApiRefT *pQosApi = QosApiCreate(NULL, NULL, 0);
    QosApiDestroy(pQosApi);

    ProtoPingRefT *pProtoPing = ProtoPingCreate(1);
    ProtoPingDestroy(pProtoPing);

    PingManagerRefT *pPingManager = PingManagerCreate(1);
    PingManagerDestroy(pPingManager);

    NetConnShutdown(0);

    return 0;
}


