<commit_before>#include "oh/Platform.hpp"
#include "oh/SpaceConnection.hpp"
#include "oh/HostedObject.hpp"
#include "oh/SpaceTimeOffsetManager.hpp"
#include "MonoDefs.hpp"
#include "MonoDomain.hpp"
#include "MonoContext.hpp"
#include "MonoObject.hpp"
#include "MonoDelegate.hpp"
#include "MonoArray.hpp"
#include "MonoException.hpp"
#include "util/SentMessage.hpp"
using namespace Sirikata;
using namespace Mono;
static MonoObject* Mono_Context_CurrentUUID() {
    UUID uuid = MonoContext::getSingleton().getUUID();
    Mono::Object obj = MonoContext::getSingleton().getDomain().UUID(uuid);
    return obj.object();
}



/*
static MonoObject* Mono_Context_CurrentObject() {
    Mono::Object obj =  Mono::Domain::root().UUID(MonoContext::getSingleton().getVWObject());
    return obj.object();
}

static MonoObject* Mono_Context_CallFunction(MonoObject*callback) {
    Mono::Object obj =  Mono::Domain::root().UUID(MonoContext::getSingleton().getVWObject());
    return obj.object();
}
*/
static void Mono_Context_CallFunctionCallback(const std::tr1::weak_ptr<HostedObject>&weak_ho,
                                              const Mono::Domain &domain,
                                              const Mono::Delegate &callback,
                                              SentMessage*sentMessage,
                                              const RoutableMessageHeader&responseHeader,
                                              MemoryReference responseBody) {
    std::tr1::shared_ptr<HostedObject>ho(weak_ho);
    bool keepquery = false;
    if (ho) {
        MonoContext::getSingleton().push(MonoContextData());
        MonoContext::getSingleton().setVWObject(&*ho,domain);
        String header;
        responseHeader.SerializeToString(&header);
        try {
            Object ret = callback.invoke(MonoContext::getSingleton().getDomain().ByteArray(header.data(),
                                                                              header.size()),
                            MonoContext::getSingleton().getDomain().ByteArray(responseBody.data(),
                                                                              responseBody.size()));
            if (!ret.null()) {
                keepquery = ret.unboxBoolean();
            }
        }catch (Mono::Exception&e) {
            SILOG(mono,debug,"Callback raised exception: "<<e);
            keepquery = false;
        }
        MonoContext::getSingleton().pop();
    }
    if (!keepquery) {
        delete sentMessage;
    }
}
static MonoObject* InternalMono_Context_CallFunction(MonoObject *message, MonoObject*callback, const Duration&duration){
    std::tr1::shared_ptr<HostedObject> ho=MonoContext::getSingleton().getVWObject();
    MemoryBuffer buf;
    
    Mono::Array(message).unboxInPlaceByteArray(buf);
    if (ho&&!buf.empty()) {
        RoutableMessageHeader hdr;
        MemoryReference body=hdr.ParseFromArray(&buf[0],buf.size());
        SentMessage*sm=hdr.has_id()?new SentMessage(hdr.id(),ho->getTracker()):new SentMessage(ho->getTracker());
        sm->setCallback(std::tr1::bind(&Mono_Context_CallFunctionCallback,
                                       ho->getWeakPtr(),
                                       MonoContext::getSingleton().getDomain(),
                                       Mono::Delegate(Mono::Object(callback)),
                                       _1,_2,_3));
        sm->setTimeout(duration);
        sm->header()=hdr;
        sm->send(body);
    }else {
        return MonoContext::getSingleton().getDomain().Boolean(false).object();
    }
    return MonoContext::getSingleton().getDomain().Boolean(true).object();
}
static MonoObject* Mono_Context_CallFunctionWithTimeout(MonoObject *message, MonoObject*callback, MonoObject*duration){
    return InternalMono_Context_CallFunction(message,callback,Mono::Object(duration).unboxTime()-Time::epoch());
}
static MonoObject* Mono_Context_CallFunction(MonoObject *message, MonoObject*callback){
    return InternalMono_Context_CallFunction(message,callback,Duration::seconds(4.0));
}
static MonoObject* Mono_Context_SendMessage(MonoObject *message){
    std::tr1::shared_ptr<HostedObject> ho=MonoContext::getSingleton().getVWObject();
    MemoryBuffer buf;
    
    Mono::Array(message).unboxInPlaceByteArray(buf);
    if (ho&&!buf.empty()) {
        RoutableMessageHeader hdr;
        MemoryReference body=hdr.ParseFromArray(&buf[0],buf.size());
        
        ho->send(hdr,body);
    }else {
        return MonoContext::getSingleton().getDomain().Boolean(false).object();
    }
    return MonoContext::getSingleton().getDomain().Boolean(true).object();
}

static void Mono_Context_TickDelay(MonoObject*duration) {
    std::tr1::shared_ptr<HostedObject> ho=MonoContext::getSingleton().getVWObject();
    if (ho) {
        Duration period(Mono::Object(duration).unboxTime()-Time::epoch());
        NOT_IMPLEMENTED(mono);
        //ho->tickDelay(period)
    }
}

void Mono_Context_setTime(MonoObject *timeRetval, const Time& cur) {
    uint64 curRaw=cur.raw();
    Mono::Object retval(timeRetval);
    uint32 curLowerLower=curRaw%65536;
    uint32 curLowerUpper=(curRaw/65536)%65536;
    uint32 curLower=curLowerLower+curLowerUpper*65536;
    curRaw/=65536;
    curRaw/=65536;
    uint32 curUpper=curRaw;
    retval.send("setLowerUpper",
                MonoContext::getSingleton().getDomain().UInt32(curLower),
                MonoContext::getSingleton().getDomain().UInt32(curUpper)); 
}
static void Mono_Context_GetTime(MonoObject*space_id,MonoObject *timeRetval) {
    SpaceID sid=SpaceID(Mono::Object(space_id).unboxUUID());
    Duration offset = SpaceTimeOffsetManager::getSingleton().getSpaceTimeOffset(sid);
    Time cur=Time::now()+offset;
    Mono_Context_setTime(timeRetval,cur);
}


static void Mono_Context_GetLocalTime(MonoObject *timeRetval) {
    Time cur=Time::now();
    //SILOG(monoscript,warning,"Time should be "<<cur.raw());
    Mono_Context_setTime(timeRetval,cur);
}
static void Mono_Context_GetTimeByteArray(MonoObject*space_id,MonoObject *timeRetval) {
    MemoryBuffer buf;
    Mono::Array(space_id).unboxInPlaceByteArray(buf);
    if (buf.size()==16) {
        SpaceID sid=SpaceID(*Sirikata::Array<unsigned char, 16,true>().memcpy(buf.data(),buf.size()));
        Duration offset = SpaceTimeOffsetManager::getSingleton().getSpaceTimeOffset(sid);
        Time cur=Time::now()+offset;
        //SILOG(monoscript,warning,"Time should be "<<cur.raw());
        Mono_Context_setTime(timeRetval,cur);
    }else {
         Mono_Context_GetLocalTime(timeRetval);
    }
}

static void Mono_Context_GetTimeString(MonoObject*space_id, MonoObject* timeRetval) {
    std::string ss=Mono::Object(space_id).unboxString();
    try {
        SpaceID sid=SpaceID(UUID(ss,UUID::HumanReadable()));
        Duration offset = SpaceTimeOffsetManager::getSingleton().getSpaceTimeOffset(sid);
        Time cur=Time::now()+offset;
        Mono_Context_setTime(timeRetval,cur);<commit_msg>windows vc9 build fix<commit_after>#include "oh/Platform.hpp"
#include "oh/SpaceConnection.hpp"
#include "oh/HostedObject.hpp"
#include "oh/SpaceTimeOffsetManager.hpp"
#include "MonoDefs.hpp"
#include "MonoDomain.hpp"
#include "MonoContext.hpp"
#include "MonoObject.hpp"
#include "MonoDelegate.hpp"
#include "MonoArray.hpp"
#include "MonoException.hpp"
#include "util/SentMessage.hpp"
using namespace Sirikata;
using namespace Mono;
static MonoObject* Mono_Context_CurrentUUID() {
    UUID uuid = MonoContext::getSingleton().getUUID();
    Mono::Object obj = MonoContext::getSingleton().getDomain().UUID(uuid);
    return obj.object();
}



/*
static MonoObject* Mono_Context_CurrentObject() {
    Mono::Object obj =  Mono::Domain::root().UUID(MonoContext::getSingleton().getVWObject());
    return obj.object();
}

static MonoObject* Mono_Context_CallFunction(MonoObject*callback) {
    Mono::Object obj =  Mono::Domain::root().UUID(MonoContext::getSingleton().getVWObject());
    return obj.object();
}
*/
static void Mono_Context_CallFunctionCallback(const std::tr1::weak_ptr<HostedObject>&weak_ho,
                                              const Mono::Domain &domain,
                                              const Mono::Delegate &callback,
                                              SentMessage*sentMessage,
                                              const RoutableMessageHeader&responseHeader,
                                              MemoryReference responseBody) {
    std::tr1::shared_ptr<HostedObject>ho(weak_ho);
    bool keepquery = false;
    if (ho) {
        MonoContext::getSingleton().push(MonoContextData());
        MonoContext::getSingleton().setVWObject(&*ho,domain);
        String header;
        responseHeader.SerializeToString(&header);
        try {
            Object ret = callback.invoke(MonoContext::getSingleton().getDomain().ByteArray(header.data(),
                                                                              header.size()),
                            MonoContext::getSingleton().getDomain().ByteArray(responseBody.data(),
                                                                              responseBody.size()));
            if (!ret.null()) {
                keepquery = ret.unboxBoolean();
            }
        }catch (Mono::Exception&e) {
            SILOG(mono,debug,"Callback raised exception: "<<e);
            keepquery = false;
        }
        MonoContext::getSingleton().pop();
    }
    if (!keepquery) {
        delete sentMessage;
    }
}
static MonoObject* InternalMono_Context_CallFunction(MonoObject *message, MonoObject*callback, const Duration&duration){
    std::tr1::shared_ptr<HostedObject> ho=MonoContext::getSingleton().getVWObject();
    MemoryBuffer buf;
    
    Mono::Array(message).unboxInPlaceByteArray(buf);
    if (ho&&!buf.empty()) {
        RoutableMessageHeader hdr;
        MemoryReference body=hdr.ParseFromArray(&buf[0],buf.size());
        SentMessage*sm=hdr.has_id()?new SentMessage(hdr.id(),ho->getTracker()):new SentMessage(ho->getTracker());
        sm->setCallback(std::tr1::bind(&Mono_Context_CallFunctionCallback,
                                       ho->getWeakPtr(),
                                       MonoContext::getSingleton().getDomain(),
                                       Mono::Delegate(Mono::Object(callback)),
                                       _1,_2,_3));
        sm->setTimeout(duration);
        sm->header()=hdr;
        sm->send(body);
    }else {
        return MonoContext::getSingleton().getDomain().Boolean(false).object();
    }
    return MonoContext::getSingleton().getDomain().Boolean(true).object();
}
static MonoObject* Mono_Context_CallFunctionWithTimeout(MonoObject *message, MonoObject*callback, MonoObject*duration){
    return InternalMono_Context_CallFunction(message,callback,Mono::Object(duration).unboxTime()-Time::epoch());
}
static MonoObject* Mono_Context_CallFunction(MonoObject *message, MonoObject*callback){
    return InternalMono_Context_CallFunction(message,callback,Duration::seconds(4.0));
}
static MonoObject* Mono_Context_SendMessage(MonoObject *message){
    std::tr1::shared_ptr<HostedObject> ho=MonoContext::getSingleton().getVWObject();
    MemoryBuffer buf;
    
    Mono::Array(message).unboxInPlaceByteArray(buf);
    if (ho&&!buf.empty()) {
        RoutableMessageHeader hdr;
        MemoryReference body=hdr.ParseFromArray(&buf[0],buf.size());
        
        ho->send(hdr,body);
    }else {
        return MonoContext::getSingleton().getDomain().Boolean(false).object();
    }
    return MonoContext::getSingleton().getDomain().Boolean(true).object();
}

static void Mono_Context_TickDelay(MonoObject*duration) {
    std::tr1::shared_ptr<HostedObject> ho=MonoContext::getSingleton().getVWObject();
    if (ho) {
        Duration period(Mono::Object(duration).unboxTime()-Time::epoch());
        NOT_IMPLEMENTED(mono);
        //ho->tickDelay(period)
    }
}

void Mono_Context_setTime(MonoObject *timeRetval, const Time& cur) {
    uint64 curRaw=cur.raw();
    Mono::Object retval(timeRetval);
    uint32 curLowerLower=curRaw%65536;
    uint32 curLowerUpper=(curRaw/65536)%65536;
    uint32 curLower=curLowerLower+curLowerUpper*65536;
    curRaw/=65536;
    curRaw/=65536;
    uint32 curUpper=curRaw;
    retval.send("setLowerUpper",
                MonoContext::getSingleton().getDomain().UInt32(curLower),
                MonoContext::getSingleton().getDomain().UInt32(curUpper)); 
}
static void Mono_Context_GetTime(MonoObject*space_id,MonoObject *timeRetval) {
    SpaceID sid=SpaceID(Mono::Object(space_id).unboxUUID());
    Duration offset = SpaceTimeOffsetManager::getSingleton().getSpaceTimeOffset(sid);
    Time cur=Time::now()+offset;
    Mono_Context_setTime(timeRetval,cur);
}


static void Mono_Context_GetLocalTime(MonoObject *timeRetval) {
    Time cur=Time::now();
    //SILOG(monoscript,warning,"Time should be "<<cur.raw());
    Mono_Context_setTime(timeRetval,cur);
}
static void Mono_Context_GetTimeByteArray(MonoObject*space_id,MonoObject *timeRetval) {
    MemoryBuffer buf;
    Mono::Array(space_id).unboxInPlaceByteArray(buf);
    if (buf.size()==16) {
        SpaceID sid=SpaceID(*Sirikata::Array<unsigned char, 16,true>().memcpy(&buf[0],buf.size()));
        Duration offset = SpaceTimeOffsetManager::getSingleton().getSpaceTimeOffset(sid);
        Time cur=Time::now()+offset;
        //SILOG(monoscript,warning,"Time should be "<<cur.raw());
        Mono_Context_setTime(timeRetval,cur);
    }else {
         Mono_Context_GetLocalTime(timeRetval);
    }
}

static void Mono_Context_GetTimeString(MonoObject*space_id, MonoObject* timeRetval) {
    std::string ss=Mono::Object(space_id).unboxString();
    try {
        SpaceID sid=SpaceID(UUID(ss,UUID::HumanReadable()));
        Duration offset = SpaceTimeOffsetManager::getSingleton().getSpaceTimeOffset(sid);
        Time cur=Time::now()+offset;
        Mono_Context_setTime(timeRetval,cur);
{'fraction_non_alphanumeric': 0.13221481322148132, 'fraction_numerical': 0.008890800889080088, 'mean_word_length': 3.2382254633849894, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5826181', 'n_tokens_mistral': 4072, 'n_tokens_neox': 4045, 'n_words': 552}