// NeL - MMORPG Framework // Copyright (C) 2010 Winch Gate Property Limited // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . #include "stdafx.h" #include #include #include "export_nel.h" #include "export_appdata.h" // *************************************************************************** bool CExportNel::scriptEvaluate (const char *script, void *out, TNelScriptValueType type) { BOOL result=TRUE; init_thread_locals(); four_typed_value_locals(Parser* parser,Value* code,Value* result,StringStream* source); vl.parser = new Parser; vl.source = new StringStream (const_cast(script)); vl.source->log_to(NULL); save_current_frames(); try { vl.source->flush_whitespace(); vl.code = vl.parser->compile_all(vl.source); vl.result = vl.code->eval(); vl.source->flush_whitespace(); vl.source->close(); // No prb ? if (vl.result!=FALSE) { switch (type) { case scriptFloat: *(float*)out=vl.result->to_float(); break; case scriptBool: *(bool*)out=vl.result->to_bool()!=FALSE; break; case scriptNode: *(INode**)out= vl.result->to_node(); } } } catch (...) { restore_current_frames(); result=FALSE; vl.source->close(); } pop_value_locals(); return (result!=FALSE); } // *************************************************************************** int CExportNel::getScriptAppData (Animatable *node, uint32 id, int def) { // Get the chunk AppDataChunk *ap=node->GetAppDataChunk (MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // Not found ? return default if (ap==NULL) return def; // String to int int value; if (NLMISC::fromString(std::string((const char*)ap->data), value)) return value; else return def; } // *************************************************************************** void CExportNel::setScriptAppData (Animatable *node, uint32 id, int value) { // Int to string std::string block = NLMISC::toString(value); // Remove data node->RemoveAppDataChunk (MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // Copy data char *copy = (char *)MAX_malloc(block.size() + 1); strcpy(copy, block.c_str()); // Add data node->AddAppDataChunk(MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id, block.size() + 1, copy); } // *************************************************************************** float CExportNel::getScriptAppData (Animatable *node, uint32 id, float def) { // Get the chunk AppDataChunk *ap=node->GetAppDataChunk (MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // Not found ? return default if (ap==NULL) return def; // String to int float value = 0.f; if (toFloatMax((const char*)ap->data, value)) return value; else return def; } // *************************************************************************** void CExportNel::setScriptAppData (Animatable *node, uint32 id, float value) { std::string str = toStringMax(value); // Remove data node->RemoveAppDataChunk (MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // Copy data char *copy = (char *)MAX_malloc(str.length() + 1); strcpy (copy, str.c_str()); // Add data node->AddAppDataChunk(MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id, str.length() + 1, copy); } // *************************************************************************** std::string CExportNel::getScriptAppData (Animatable *node, uint32 id, const std::string& def) { // Get the chunk AppDataChunk *ap=node->GetAppDataChunk (MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // Not found ? return default if (ap==NULL) return def; // String to int if (((const char*)ap->data)[ap->length - 1] == 0) return (const char*)ap->data; else return def; } // *************************************************************************** void CExportNel::setScriptAppData (Animatable *node, uint32 id, const std::string& value) { // Remove data node->RemoveAppDataChunk (MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // Copy data char *copy = (char *)MAX_malloc(value.length() + 1); strcpy(copy, value.c_str()); // Add data node->AddAppDataChunk(MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id, value.size() + 1, copy); } // *************************************************************************** NLMISC::CRGBA CExportNel::getScriptAppData (Animatable *node, uint32 id, NLMISC::CRGBA def) { // Get the chunk AppDataChunk *ap=node->GetAppDataChunk (MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // Not found ? return default if (ap == NULL) return def; // String to RGBA if (((const char*)ap->data)[ap->length - 1] == 0) { const char *ptr= (const char*)ap->data; int r = 255, g = 255, b = 255, a = 255; sscanf (ptr, "%d %d %d %d", &r, &g, &b, &a); NLMISC::clamp (r, 0, 255); NLMISC::clamp (g, 0, 255); NLMISC::clamp (b, 0, 255); NLMISC::clamp (a, 0, 255); return NLMISC::CRGBA(r,g,b,a); } else return def; } // *************************************************************************** void CExportNel::setScriptAppData (Animatable *node, uint32 id, NLMISC::CRGBA val) { // Remove data node->RemoveAppDataChunk(MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id); // RGBA to string std::string value = NLMISC::toString("%d %d %d %d", val.R, val.G, val.B, val.A); // Copy data char *copy = (char *)MAX_malloc(value.length() + 1); strcpy(copy, value.c_str()); // Add data node->AddAppDataChunk(MAXSCRIPT_UTILITY_CLASS_ID, UTILITY_CLASS_ID, id, value.size() + 1, copy); } // *************************************************************************** void CExportNel::getScriptAppDataVPWT (Animatable *node, CVPWindTreeAppData &apd) { nlassert(NEL3D_APPDATA_VPWT_LEVELMAX == CVPWindTreeAppData::HrcDepth); apd.FreqScale= getScriptAppData(node, NEL3D_APPDATA_VPWT_FREQ_SCALE, 2.f); apd.DistScale= getScriptAppData(node, NEL3D_APPDATA_VPWT_DIST_SCALE, 2.f); apd.SpecularLighting= getScriptAppData(node, NEL3D_APPDATA_VPWT_USE_SPEC, BST_UNCHECKED); for(uint i=0; i