larsiusprime / SteamWrap

Haxe native extension for the Steam API
MIT License
106 stars 44 forks source link

SteamWrap.cpp #10

Closed hughsando closed 7 years ago

hughsando commented 7 years ago

Just looking over https://github.com/larsiusprime/SteamWrap/blob/cloud/native/SteamWrap.cpp#L965

You should probably return "alloc_null()" rather than "allloc_int(0)" - make the check on the haxe side nicer (!=null). You will need to free the malloc buffer. The "alloc_string" will take its own copy, so value result = alloc_string(buffer); free(buffer); return result; You should also use the alloc_string_len instead since it is slightly faster and prevents binary data getting terminated early if it happens to come across the zero-byte. (not such a problem if you are only expecting ascii)

larsiusprime commented 7 years ago

Whew! I'm super late here @hughsando!

Does this look better?

value SteamWrap_FileRead(value fileName)
{
    if (!val_is_string(fileName) || !CheckInit())
        return alloc_null();

    const char * fName = val_string(fileName);

    bool exists = SteamRemoteStorage()->FileExists(fName);
    if(!exists) return alloc_int(0);

    int length = SteamRemoteStorage()->GetFileSize(fName);

    char *bytesData = (char *)malloc(length);
    int32 result = SteamRemoteStorage()->FileRead(fName, bytesData, length);

    value returnValue = alloc_string_len(bytesData, length);
    free(bytesData);
    return returnValue;
}
DEFINE_PRIM(SteamWrap_FileRead, 1);
hughsando commented 7 years ago

Yep - looks good!