Revision: 1611
          http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=1611&view=rev
Author:   rorthomas
Date:     2011-01-30 17:33:58 +0000 (Sun, 30 Jan 2011)

Log Message:
-----------
added script interface to online API
improved some script things

Modified Paths:
--------------
    trunk/source/main/CMakeLists.txt
    trunk/source/main/ScriptEngine.cpp
    trunk/source/main/ScriptEngine.h

Modified: trunk/source/main/CMakeLists.txt
===================================================================
--- trunk/source/main/CMakeLists.txt    2011-01-30 15:38:18 UTC (rev 1610)
+++ trunk/source/main/CMakeLists.txt    2011-01-30 17:33:58 UTC (rev 1611)
@@ -56,6 +56,7 @@
 setup_lib(MYGUI)
 setup_lib(OPENAL)
 setup_lib(LUA)
+setup_lib(CURL)
 setup_lib(SOCKETW)
 setup_lib(MOFILEREADER)
 setup_lib(PAGED)

Modified: trunk/source/main/ScriptEngine.cpp
===================================================================
--- trunk/source/main/ScriptEngine.cpp  2011-01-30 15:38:18 UTC (rev 1610)
+++ trunk/source/main/ScriptEngine.cpp  2011-01-30 17:33:58 UTC (rev 1611)
@@ -37,6 +37,15 @@
 #include "scriptstring/scriptstring.h"
 // AS addons end
 
+#ifdef USE_CURL
+#define CURL_STATICLIB
+#include <stdio.h>
+#include <curl/curl.h>
+#include <curl/types.h>
+#include <curl/easy.h>
+#endif //USE_CURL
+
+#include "rornet.h"
 #include "water.h"
 #include "Beam.h"
 #include "Settings.h"
@@ -486,8 +495,8 @@
        result = engine->RegisterObjectMethod("GameScriptClass", "void 
setDirectionArrow(const string &in, vector3)", 
AngelScript::asMETHOD(GameScript,setDirectionArrow), 
AngelScript::asCALL_THISCALL); assert(result>=0);
        result = engine->RegisterObjectMethod("GameScriptClass", "void 
hideDirectionArrow()", AngelScript::asMETHOD(GameScript,hideDirectionArrow), 
AngelScript::asCALL_THISCALL); assert(result>=0);
        result = engine->RegisterObjectMethod("GameScriptClass", "void 
registerForEvent(int)", AngelScript::asMETHOD(GameScript,registerForEvent), 
AngelScript::asCALL_THISCALL); assert(result>=0);
-       //result = engine->RegisterObjectMethod("GameScriptClass", "BeamClass 
@getCurrentTruck()", AngelScript::asMETHOD(GameScript,getCurrentTruck), 
AngelScript::asCALL_THISCALL); assert(result>=0);
-       //result = engine->RegisterObjectMethod("GameScriptClass", "BeamClass 
@getTruckByNum(int)", AngelScript::asMETHOD(GameScript,getTruckByNum), 
AngelScript::asCALL_THISCALL); assert(result>=0);
+       result = engine->RegisterObjectMethod("GameScriptClass", "BeamClass 
@getCurrentTruck()", AngelScript::asMETHOD(GameScript,getCurrentTruck), 
AngelScript::asCALL_THISCALL); assert(result>=0);
+       result = engine->RegisterObjectMethod("GameScriptClass", "BeamClass 
@getTruckByNum(int)", AngelScript::asMETHOD(GameScript,getTruckByNum), 
AngelScript::asCALL_THISCALL); assert(result>=0);
        result = engine->RegisterObjectMethod("GameScriptClass", "int 
getChatFontSize()", AngelScript::asMETHOD(GameScript,getChatFontSize), 
AngelScript::asCALL_THISCALL); assert(result>=0);
        result = engine->RegisterObjectMethod("GameScriptClass", "void 
setChatFontSize(int)", AngelScript::asMETHOD(GameScript,setChatFontSize), 
AngelScript::asCALL_THISCALL); assert(result>=0);
        result = engine->RegisterObjectMethod("GameScriptClass", "void 
showChooser(const string &in, const string &in, const string &in)", 
AngelScript::asMETHOD(GameScript,showChooser), AngelScript::asCALL_THISCALL); 
assert(result>=0);
@@ -503,6 +512,8 @@
        result = engine->RegisterObjectMethod("GameScriptClass", "void 
startTimer()", AngelScript::asMETHOD(GameScript,startTimer), 
AngelScript::asCALL_THISCALL); assert(result>=0);
        result = engine->RegisterObjectMethod("GameScriptClass", "void 
stopTimer()", AngelScript::asMETHOD(GameScript,stopTimer), 
AngelScript::asCALL_THISCALL); assert(result>=0);
        result = engine->RegisterObjectMethod("GameScriptClass", "float 
rangeRandom(float, float)", AngelScript::asMETHOD(GameScript,rangeRandom), 
AngelScript::asCALL_THISCALL); assert(result>=0);
+       result = engine->RegisterObjectMethod("GameScriptClass", "int 
useOnlineAPI(const string &in, string &out)", 
AngelScript::asMETHOD(GameScript,useOnlineAPI), AngelScript::asCALL_THISCALL); 
assert(result>=0);
+       
 
        // class CacheSystem
        result = engine->RegisterObjectType("CacheSystemClass", 
sizeof(CacheSystem), AngelScript::asOBJ_REF | AngelScript::asOBJ_NOHANDLE);
@@ -1134,6 +1145,102 @@
        return Ogre::Math::RangeRandom(from, to);
 }
 
+//#ifdef USE_CURL
+//hacky hack to fill memory with data for curl
+// from: http://curl.haxx.se/libcurl/c/getinmemory.html
+static size_t curlWriteMemoryCallback(void *ptr, size_t size, size_t nmemb, 
void *data)
+{
+  size_t realsize = size * nmemb;
+  struct curlMemoryStruct *mem = (struct curlMemoryStruct *)data;
+ 
+  mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
+  if (mem->memory == NULL) {
+    /* out of memory! */ 
+    printf("not enough memory (realloc returned NULL)\n");
+    exit(EXIT_FAILURE);
+  }
+ 
+  memcpy(&(mem->memory[mem->size]), ptr, realsize);
+  mem->size += realsize;
+  mem->memory[mem->size] = 0;
+ 
+  return realsize;
+}
+//#endif //USE_CURL
+
+int GameScript::useOnlineAPI(const std::string &apiquery, std::string &result)
+{
+//#ifdef USE_CURL
+       struct curlMemoryStruct chunk;
+ 
+       chunk.memory = (char *)malloc(1);  /* will be grown as needed by the 
realloc above */ 
+       chunk.size = 0;    /* no data at this point */ 
+
+       CURLcode res;
+       CURL *curl = curl_easy_init();
+       if(!curl)
+       {
+               result = "ERROR: failed to init curl";
+               return 1;
+       }
+
+       char *curl_err_str[CURL_ERROR_SIZE];
+       memset(curl_err_str, 0, CURL_ERROR_SIZE);
+
+       string url = "http://"; + string(REPO_SERVER) + apiquery;
+       curl_easy_setopt(curl, CURLOPT_URL,              url.c_str());
+
+       /* send all data to this function  */ 
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteMemoryCallback);
+ 
+       /* we pass our 'chunk' struct to the callback function */ 
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
+ 
+
+       // logging stuff
+       //curl_easy_setopt(curl, CURLOPT_STDERR,           LogManager::getsin 
InstallerLog::getSingleton()->getLogFilePtr());
+       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,      curl_err_str[0]);
+
+       // http related settings
+       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,   1); // follow redirects
+       curl_easy_setopt(curl, CURLOPT_AUTOREFERER,      1); // set the 
Referer: field in requests where it follows a Location: redirect. 
+       curl_easy_setopt(curl, CURLOPT_MAXREDIRS,        20);
+       curl_easy_setopt(curl, CURLOPT_USERAGENT,        "RoR");
+       curl_easy_setopt(curl, CURLOPT_FILETIME,         1);
+       
+       // TO BE DONE: ADD SSL
+       // see: http://curl.haxx.se/libcurl/c/simplessl.html
+       // curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1L); 
+
+       res = curl_easy_perform(curl);
+       curl_easy_cleanup(curl);
+
+       //printf("%lu bytes retrieved\n", (long)chunk.size);
+
+
+       if(chunk.memory)
+       {
+               // convert memory into std::string now
+               result = string(chunk.memory);
+
+               // then free
+               free(chunk.memory);
+       }
+ 
+       /* we're done with libcurl, so clean it up */ 
+       curl_global_cleanup();
+
+       if(res != CURLE_OK)
+       {
+               const char *errstr = curl_easy_strerror(res);
+               result = "ERROR: " + string(errstr);
+               return 1;
+       }
+
+       return 0;
+//#endif //USE_CURL
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // CBytecodeStream
 CBytecodeStream::CBytecodeStream(std::string filename) : f(0)

Modified: trunk/source/main/ScriptEngine.h
===================================================================
--- trunk/source/main/ScriptEngine.h    2011-01-30 15:38:18 UTC (rev 1610)
+++ trunk/source/main/ScriptEngine.h    2011-01-30 17:33:58 UTC (rev 1611)
@@ -41,6 +41,15 @@
 
 #define AS_INTERFACE_VERSION "0.1.0" //!< versioning for the scripting 
interface
 
+#ifdef USE_CURL
+
+struct curlMemoryStruct {
+  char *memory;
+  size_t size;
+};
+
+#endif // USE_CURL
+
 /**
  * @file ScriptEngine.h
  * @version 0.1.0
@@ -326,6 +335,7 @@
        int setMaterialEmissive(const std::string &materialName, float red, 
float green, float blue);
        
        float rangeRandom(float from, float to);
+       int useOnlineAPI(const std::string &apiquery, std::string &result);
        Ogre::Vector3 getPersonPosition();
 };
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Rigsofrods-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel

Reply via email to