/* * Get an actionscript class with the given namespace and class name * in the format: package::ClassName */ AS3_Val get_class(const char * as_namespaceclass_path) { /* TODO: Merge with get_class2()? */ AS3_Val as_namespace; AS3_Val as_class; AS3_Val ret; char * class_ptr = NULL; /* TODO might want to store classes in a table to save loading again */ class_ptr = strstr(as_namespaceclass_path, "::"); if (class_ptr > as_namespaceclass_path) { as_namespace = AS3_StringN( as_namespaceclass_path, (class_ptr - as_namespaceclass_path) / sizeof(char) ); as_class = AS3_String(class_ptr + 2); } else { as_namespace = AS3_Undefined(); as_class = AS3_String(as_namespaceclass_path); } ret = AS3_NSGet(as_namespace, as_class); /* TODO check for failure getting class */ AS3_Release(as_namespace); AS3_Release(as_class); return ret; }
/* * Get an actionscript class with the given namespace and class name * in the format: package (may be NULL), ClassName */ AS3_Val get_class2(const char * as_namespace_path, const char * as_class_path) { AS3_Val as_namespace; AS3_Val as_class; AS3_Val ret; /* TODO might want to store classes in a table to save loading again */ if (as_namespace_path) { as_namespace = AS3_String(as_namespace_path); } else { as_namespace = AS3_Undefined(); } as_class = AS3_String(as_class_path); ret = AS3_NSGet(as_namespace, as_class); /* TODO check for failure getting class */ AS3_Release(as_namespace); AS3_Release(as_class); return ret; }
void traceBuffer(char *label, unsigned char *buf, int bufLen) { AS3_Trace(AS3_String(label)); int len= 50; // chars per line int o, offset=0; for (o= 0; o<bufLen/len; o++, offset+=len) { debugBuf= malloc(sizeof(char)*(len*3)+1); int t; for (t= 0; t<len; t++) { debugBuf[t*3]= hex[(buf[t+offset]>>4)]; debugBuf[(t*3)+1]= hex[(buf[t+offset]&0xf)]; debugBuf[(t*3)+2]= ' '; } debugBuf[t*3]= 0; AS3_Trace(AS3_String(debugBuf)); free(debugBuf); } if (bufLen%len) { debugBuf= malloc(sizeof(char)*(len*3)+1); int t; for (t= 0; t<bufLen%len; t++) { debugBuf[t*3]= hex[(buf[t+offset]>>4)]; debugBuf[(t*3)+1]= hex[(buf[t+offset]&0xf)]; debugBuf[(t*3)+2]= ' '; } debugBuf[t*3]= 0; AS3_Trace(AS3_String(debugBuf)); free(debugBuf); }
static AS3_Val echo(void* self, AS3_Val args) { char* val = NULL; AS3_ArrayValue(args, "StrType", &val); if(val == NULL) { char* nullString = "null"; return AS3_String(nullString); } return AS3_String(val); }
/* Текст дополнительного окна описания локации */ AS3_Val QSPGetVarsDesc(void *param, AS3_Val args) { AS3_Val res; char *descUTF8; if (qspCurVars) { descUTF8 = qspW2C(qspCurVars); res = AS3_String(descUTF8); free(descUTF8); } else res = AS3_String(0); return res; }
/* Название текущей локации */ AS3_Val QSPGetCurLoc(void *param, AS3_Val args) { AS3_Val res; char *locUTF8; if (qspCurLoc >= 0) { locUTF8 = qspW2C(qspLocs[qspCurLoc].Name); res = AS3_String(locUTF8); free(locUTF8); } else res = AS3_String(0); return res; }
/* Полный путь к загруженному файлу игры */ AS3_Val QSPGetQstFullPath(void *param, AS3_Val args) { AS3_Val res; char *pathUTF8; if (qspQstFullPath) { pathUTF8 = qspW2C(qspQstFullPath); res = AS3_String(pathUTF8); free(pathUTF8); } else res = AS3_String(0); return res; }
AS3_Val doUnAes(void *data, AS3_Val args) { AS3_Val input = NULL; unsigned int in_len; char * ar; AS3_ArrayValue(args, "AS3ValType, IntType", &input, &in_len); ar = (char *)malloc(in_len); AS3_ByteArray_readBytes((void*)ar, input, in_len); char * aesData; int out_len = 0; char * keyStr = "kael"; //use AES to Decrypt the bytes aesData = DES_Decrypt(ar, keyStr, in_len, &out_len ); //make a new as3 byteArray var AS3_Val baNS = AS3_String("flash.utils"); AS3_Val baClass = AS3_NSGetS(baNS, "ByteArray"); AS3_Val emptyParams = AS3_Array(""); AS3_Val byteArray2 = AS3_New(baClass, emptyParams); AS3_ByteArray_writeBytes(byteArray2, aesData, out_len); return byteArray2; }
/* * Helpers */ int trace( const char *message ) { AS3_Val as3_message = AS3_String(message); AS3_Trace(as3_message); AS3_Release(as3_message); return 0; }
//Method exposed to ActionScript //Takes a String and echos it static AS3_Val echo(void* self, AS3_Val args) { AS3_Val byteArray; AS3_ArrayValue( args, "AS3ValType", &byteArray); AS3_Val length = AS3_GetS(byteArray, "length"); sztrace("length getted!"); int len = AS3_IntValue(length); //if(len>0) return length; char *data=NULL; data=malloc(len); memset(data,0,len); int fileLen = AS3_ByteArray_readBytes(data,byteArray, len); char *out= NULL; out = malloc(fileLen); memset(out,0,fileLen); ByteArray * bytearray = ByteArray_new(fileLen); bytearray->data = data; PdfFile_free(PdfFile_parse(bytearray,out)); ByteArray_free(bytearray); return AS3_String(out); }
/* Дата и время компиляции */ AS3_Val QSPGetCompiledDateTime(void *param, AS3_Val args) { AS3_Val res; char *infoUTF8 = qspW2C(QSP_FMT(__DATE__) QSP_FMT(", ") QSP_FMT(__TIME__)); res = AS3_String(infoUTF8); free(infoUTF8); return res; }
/* Версия */ AS3_Val QSPGetVersion(void *param, AS3_Val args) { AS3_Val res; char *verUTF8 = qspW2C(QSP_VER); res = AS3_String(verUTF8); free(verUTF8); return res; }
/* setup some useful constants */ void initialize_as3_constants() { no_params = AS3_Array(""); zero_param = AS3_Int(0); Number_class = get_class2(NULL, "Number"); int_class = get_class2(NULL, "int"); String_class = get_class2(NULL, "String"); Boolean_class = get_class2(NULL, "Boolean"); flash_utils_namespace = AS3_String("flash.utils"); getQualifiedClassName_method = AS3_NSGetS(flash_utils_namespace, "getQualifiedClassName"); Array_class = get_class2(NULL, "Array"); }
/* Получить имя переменной с указанным индексом */ AS3_Val QSPGetVarNameByIndex(void *param, AS3_Val args) { int index; QSP_CHAR *name; char *nameUTF8; AS3_Val res; AS3_ArrayValue(args, "IntType", &index); if (index < 0 || index >= QSP_VARSCOUNT || !qspVars[index].Name) return AS3_Null(); nameUTF8 = qspW2C(qspVars[index].Name); res = AS3_String(nameUTF8); free(nameUTF8); return res; }
/* Получить описание ошибки по ее номеру */ AS3_Val QSPGetErrorDesc(void *param, AS3_Val args) { int errorNum; QSP_CHAR *str; char *strUTF8; AS3_Val res; AS3_ArrayValue(args, "IntType", &errorNum); str = qspGetErrorDesc(errorNum); strUTF8 = qspW2C(str); res = AS3_String(strUTF8); free(strUTF8); return res; }
AS3_Val doDecode(void* data,AS3_Val args) { AS3_Val input = NULL; int in_len; char * ar; AS3_ArrayValue(args,"AS3ValType,IntType",&input,&in_len); ar = (char*)malloc(in_len); AS3_ByteArray_readBytes((void*)ar,input,in_len); char * decryptData; char * key="kael"; decryptData = dofileEx(ar, key, in_len); //make a new as3 byteArray var AS3_Val baNS = AS3_String("flash.utils"); AS3_Val baClass = AS3_NSGetS(baNS, "ByteArray"); AS3_Val emptyParams = AS3_Array(""); AS3_Val byteArray2 = AS3_New(baClass, emptyParams); AS3_ByteArray_writeBytes(byteArray2, decryptData, in_len); return byteArray2; }
static AS3_Val thunk_CreatePacketFromByteArray(void *gg_clientData, AS3_Val gg_args) { AS3_Val bytes; AS3_ArrayValue(gg_args, "AS3ValType", &bytes); AS3_Val actionVal = AS3_CallTS("readUnsignedByte", bytes, ""); AS3_Val transactionIdVal = AS3_CallTS("readUnsignedInt", bytes, ""); AS3_Val protocolVersionVal = AS3_CallTS("readUnsignedShort", bytes, ""); AS3_Val pplive_protocol_namespace = AS3_String("com.pplive.p2p.network.protocol"); AS3_Val PacketClass = AS3_NSGetS(pplive_protocol_namespace, "Packet"); AS3_Val paramArray = AS3_Array("IntType, IntType, IntType", AS3_IntValue(actionVal), AS3_IntValue(transactionIdVal), AS3_IntValue(protocolVersionVal)); AS3_Val packet = AS3_New(PacketClass, paramArray); AS3_Release(paramArray); AS3_Release(PacketClass); AS3_Release(pplive_protocol_namespace); AS3_Release(protocolVersionVal); AS3_Release(transactionIdVal); AS3_Release(actionVal); AS3_Release(bytes); return packet; }
static AS3_Val thunk_InitAnnounceResponsePacket(void *gg_clientData, AS3_Val gg_args) { AS3_Val packet; AS3_Val bytes; AS3_ArrayValue(gg_args, "AS3ValType, AS3ValType", &packet, &bytes); AS3_Val paramArray = AS3_Array("AS3ValType", bytes); AS3_Val p2p_struct_namespace = AS3_String("com.pplive.p2p.struct"); AS3_Val PeerDownloadInfoClass = AS3_NSGetS(p2p_struct_namespace, "PeerDownloadInfo"); AS3_Val BlockMapClass = AS3_NSGetS(p2p_struct_namespace, "BlockMap"); AS3_Val peerDownloadInfo = AS3_New(PeerDownloadInfoClass, paramArray); AS3_Val blockMapInfo = AS3_New(BlockMapClass, paramArray); AS3_SetS(packet, "peerDownloadInfo", peerDownloadInfo); AS3_SetS(packet, "blockMap", blockMapInfo); AS3_Release(blockMapInfo); AS3_Release(peerDownloadInfo); AS3_Release(BlockMapClass); AS3_Release(PeerDownloadInfoClass); AS3_Release(p2p_struct_namespace); AS3_Release(paramArray); AS3_Release(bytes); AS3_Release(packet); return NULL; }
/** * Scan in a wavetable. Wavetable should be at least one longer than the table size. */ static AS3_Val wavetableIn(void *self, AS3_Val args) { AS3_Val settings; int bufferPosition; int channels; int frames; float *buffer; int sourceBufferPosition; float *sourceBuffer; double phaseArg; float phase; double phaseAddArg; float phaseAdd; double phaseResetArg; float phaseReset; int tableSize; int count; int intPhase; float *wavetablePosition; float *scratch; double y1Arg, y2Arg; float y1, y2; float fractional, fractionalIncrement, instantBend; AS3_ArrayValue(args, "IntType, IntType, IntType, IntType, AS3ValType", &bufferPosition, &sourceBufferPosition, &channels, &frames, &settings); AS3_ObjectValue(settings, "tableSize:IntType, phase:DoubleType, phaseAdd:DoubleType, phaseReset:DoubleType, y1:DoubleType, y2:DoubleType", &tableSize, &phaseArg, &phaseAddArg, &phaseResetArg, &y1Arg, &y2Arg); buffer = (float *) bufferPosition; sourceBuffer = (float *) sourceBufferPosition; phaseAdd = (float) phaseAddArg * tableSize; // num source frames to add per output frames phase = (float) phaseArg * tableSize; // translate into a frame count into the table phaseReset = (float) phaseResetArg * tableSize; y1 = (float) y1Arg; y2 = (float) y2Arg; // Expand the pitch modulation into scratch // expandLine(scratch1, y1, y2, frames); // draws spline segment into scratch1 // scratch = (float *) scratch1; // Make sure we got everything right //sprintf(trace, "Wavetable size=%d phase=%f phaseAdd=%f y1=%f y2=%f", tableSize, phase, phaseAdd, y1, y2); //sztrace(trace); count=frames; fractional = 0.0; fractionalIncrement = 1 / (float) frames; if (channels == 1) { while (count--) { while (phase >= tableSize) { if (phaseReset == -1) { // no looping! return 0; } else { // wrap phase to the loop point phase -= tableSize; phase += phaseReset; } } intPhase = (int) phase; // int phase wavetablePosition = sourceBuffer + intPhase; *buffer++ = interpolate(*wavetablePosition, *(wavetablePosition+1), phase - intPhase); // Increment phase by adjusting phaseAdd for instantaneous pitch bend instantBend = interpolate(y1, y2, fractional); fractional += fractionalIncrement; phase += phaseAdd * shiftToFreq(instantBend); } } else if (channels == 2 ) { while (count--) { while (phase >= tableSize) { if (phaseReset == -1) { // no looping! return 0; } else { // wrap phase to the loop point phase -= tableSize; phase += phaseReset; } } intPhase = ((int)(phase*0.5))*2; // int phase, round to even frames, for each stereo frame pair wavetablePosition = sourceBuffer + intPhase; *buffer++ = interpolate(*wavetablePosition, *(wavetablePosition+2), phase - intPhase); *buffer++ = interpolate(*(wavetablePosition+1), *(wavetablePosition+3), phase - intPhase); // Increment phase by adjusting phaseAdd for instantaneous pitch bend instantBend = interpolate(y1, y2, fractional); fractional += fractionalIncrement; phase += phaseAdd * shiftToFreq(instantBend); } } // Scale back down to a factor, and write the final phase value back to AS3 phase /= tableSize; AS3_Set(settings, AS3_String("phase"), AS3_Number(phase)); return 0; }
/* * Take the Lua stack item at index i and convert it into an * ActionScript value. */ AS3_Val get_as3_value_from_lua_stack_type(lua_State * L, int i, int type) { /* WARNING: Panic alert! Use L*_FN checkers here! */ LCALL(L, stack); AS3_Val value; switch (type) { case LUA_TSTRING: /* strings */ { size_t length = 0; const char * str = lua_tolstring(L, i, &length); if (str == NULL) /* NOTE: This is unreachable. Assert instead */ { length = 6; str = "(null)"; } /* NOTE: Alchemy .5a truncates embedded zeroes in string * regardless to the passed length */ value = AS3_StringN(str, length); } break; case LUA_TBOOLEAN: /* booleans */ value = lua_toboolean(L, i) ? AS3_True() : AS3_False(); break; case LUA_TNUMBER: /* numbers */ value = AS3_Number(lua_tonumber(L, i)); break; case LUA_TNONE: /* fall through */ case LUA_TNIL: /* nil */ value = AS3_Null(); break; case LUA_TUSERDATA: /* userdata */ { void * userdata = lua_touserdata(L, i); lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_METATABLE); if (userdata == NULL || !lua_getmetatable(L, i)) { lua_pop(L, 1); /* Pop AS3LUA_METATABLE */ value = as3_value_from_foreign_userdata(L, i); } else if (!lua_rawequal(L, -2, -1)) { lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */ value = as3_value_from_foreign_userdata(L, i); } else { lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */ AS3LuaUserData * userdata = (AS3LuaUserData *)lua_touserdata(L, i); value = userdata->value; /* * We just created one more reference to the AS3 value, * as it still lives inside Lua. * (And will probably be collected by GC.) */ AS3_Acquire(value); } } break; case LUA_TFUNCTION: /* function */ value = setup_callback(L, i); break; case LUA_TLIGHTUSERDATA: /* TODO: blackbox this type */ case LUA_TTABLE: /* TODO: deal with this type */ case LUA_TTHREAD: /* TODO: blackbox this type */ value = AS3_String(lua_typename(L, type)); break; default: /* unreachable */ fatal_error("unknown Lua type"); break; } #ifdef DO_SPAM SPAM(("get_as3_value_from_lua_stack(): end")); AS3_Trace( AS3_Call( getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", value) ) ); #endif /* DO_SPAM */ LCHECK_FN(L, stack, 0, fatal_error); return value; }
/* * Take the foreign userdata at given Lua stack index * and convert it into an ActionScriptValue. * TODO: Wrap it into a black-box value. */ AS3_Val as3_value_from_foreign_userdata(lua_State * L, int index) { return AS3_String("userdata"); }
T(b2EdgeShape); C(b2EdgeShape, m_vertex0, b2Vec2); C(b2EdgeShape, m_vertex1, b2Vec2); C(b2EdgeShape, m_vertex2, b2Vec2); C(b2EdgeShape, m_vertex3, b2Vec2); B(b2EdgeShape, m_hasVertex0); B(b2EdgeShape, m_hasVertex3); T(----------); T(b2LoopShape); V(b2LoopShape, m_vertices, m_count); I(b2LoopShape, m_count); AS3_Trace(AS3_String("Creating and stepping a b2World in C++")); b2Vec2 gravity; gravity.Set(0.0f, -10.0f); bool doSleep = true; b2World* m_world = new b2World(gravity, doSleep); m_world->Step(0.05f, 15.0f, 15.0f); delete m_world; AS3_Trace(AS3_String("C++ b2World test done!")); AS3_Val core = b2Core(); AS3_SetS(core, "emptyFunction", AS3F(emptyFunction)); AS3_SetS(core, "testFunction", AS3F(testFunction)); AS3_Trace(AS3_Ptr(&b2_aabbExtension)); AS3_Trace(AS3_Ptr(&b2_aabbMultiplier)); AS3_Trace(AS3_Ptr(&b2_linearSlop));