int LuaStack::executeScriptFile(const char* filename) { CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename"); static const std::string BYTECODE_FILE_EXT = ".luac"; static const std::string NOT_BYTECODE_FILE_EXT = ".lua"; std::string buf(filename); // // remove .lua or .luac // size_t pos = buf.rfind(BYTECODE_FILE_EXT); if (pos != std::string::npos) { buf = buf.substr(0, pos); } else { pos = buf.rfind(NOT_BYTECODE_FILE_EXT); if (pos == buf.length() - NOT_BYTECODE_FILE_EXT.length()) { buf = buf.substr(0, pos); } } FileUtils *utils = FileUtils::getInstance(); // // 1. check .lua suffix // 2. check .luac suffix // std::string tmpfilename = buf + NOT_BYTECODE_FILE_EXT; if (utils->isFileExist(tmpfilename)) { buf = tmpfilename; } else { tmpfilename = buf + BYTECODE_FILE_EXT; if (utils->isFileExist(tmpfilename)) { buf = tmpfilename; } } std::string fullPath = utils->fullPathForFilename(buf); Data data = utils->getDataFromFile(fullPath); int rn = 0; if (!data.isNull()) { if (luaLoadBuffer(_state, (const char*)data.getBytes(), (int)data.getSize(), fullPath.c_str()) == 0) { rn = executeFunction(0); } } return rn; }
void GameNode3DReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table* node3DOptions) { auto options = (GameNode3DOption*)node3DOptions; std::string name = options->name()->c_str(); node->setName(name); _sceneBrushInstance = nullptr; bool skyBoxEnabled = options->skyBoxEnabled() != 0; if (skyBoxEnabled) { std::string leftFileData = options->leftFileData()->path()->c_str(); std::string rightFileData = options->rightFileData()->path()->c_str(); std::string upFileData = options->upFileData()->path()->c_str(); std::string downFileData = options->downFileData()->path()->c_str(); std::string forwardFileData = options->forwardFileData()->path()->c_str(); std::string backFileData = options->backFileData()->path()->c_str(); FileUtils *fileUtils = FileUtils::getInstance(); if (fileUtils->isFileExist(leftFileData) && fileUtils->isFileExist(rightFileData) && fileUtils->isFileExist(upFileData) && fileUtils->isFileExist(downFileData) && fileUtils->isFileExist(forwardFileData) && fileUtils->isFileExist(backFileData)) { _sceneBrushInstance = CameraBackgroundSkyBoxBrush::create(leftFileData, rightFileData, upFileData, downFileData, forwardFileData, backFileData); } } std::string customProperty = options->customProperty()->c_str(); ComExtensionData* extensionData = ComExtensionData::create(); extensionData->setCustomProperty(customProperty); if (node->getComponent(ComExtensionData::COMPONENT_NAME)) { node->removeComponent(ComExtensionData::COMPONENT_NAME); } node->addComponent(extensionData); bool useDefaultLight = options->useDefaultLight(); if (useDefaultLight) { AmbientLight* defaultLight = AmbientLight::create(Color3B::WHITE); defaultLight->setIntensity(0.5f); node->addChild(defaultLight); } }
void UserCameraReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table* userCameraDOptions) { auto options = (UserCameraOptions*)userCameraDOptions; Camera* camera = static_cast<Camera*>(node); int cameraFlag = options->cameraFlag(); auto node3DReader = Node3DReader::getInstance(); node3DReader->setPropsWithFlatBuffers(node, (Table*)(options->node3DOption())); bool skyBoxEnabled = options->skyBoxEnabled() != 0; if (skyBoxEnabled) { std::string leftFileData = options->leftFileData()->path()->c_str(); std::string rightFileData = options->rightFileData()->path()->c_str(); std::string upFileData = options->upFileData()->path()->c_str(); std::string downFileData = options->downFileData()->path()->c_str(); std::string forwardFileData = options->forwardFileData()->path()->c_str(); std::string backFileData = options->backFileData()->path()->c_str(); FileUtils *fileUtils = FileUtils::getInstance(); if (fileUtils->isFileExist(leftFileData) && fileUtils->isFileExist(rightFileData) && fileUtils->isFileExist(upFileData) && fileUtils->isFileExist(downFileData) && fileUtils->isFileExist(forwardFileData) && fileUtils->isFileExist(backFileData)) { CameraBackgroundSkyBoxBrush* brush = CameraBackgroundSkyBoxBrush::create(leftFileData, rightFileData, upFileData, downFileData, forwardFileData, backFileData); camera->setBackgroundBrush(brush); } else { if (GameNode3DReader::getSceneBrushInstance() != nullptr) camera->setBackgroundBrush(GameNode3DReader::getSceneBrushInstance()); } } else { if (GameNode3DReader::getSceneBrushInstance() != nullptr) camera->setBackgroundBrush(GameNode3DReader::getSceneBrushInstance()); } }
int cocos2dx_lua_loader(lua_State *L) { static const std::string BYTECODE_FILE_EXT = ".luac"; static const std::string NOT_BYTECODE_FILE_EXT = ".lua"; std::string filename(luaL_checkstring(L, 1)); size_t pos = filename.rfind(BYTECODE_FILE_EXT); if (pos != std::string::npos) { filename = filename.substr(0, pos); } else { pos = filename.rfind(NOT_BYTECODE_FILE_EXT); if (pos == filename.length() - NOT_BYTECODE_FILE_EXT.length()) { filename = filename.substr(0, pos); } } pos = filename.find_first_of("."); while (pos != std::string::npos) { filename.replace(pos, 1, "/"); pos = filename.find_first_of("."); } // search file in package.path unsigned char* chunk = nullptr; ssize_t chunkSize = 0; std::string chunkName; FileUtils* utils = FileUtils::getInstance(); lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); std::string searchpath(lua_tostring(L, -1)); lua_pop(L, 1); size_t begin = 0; size_t next = searchpath.find_first_of(";", 0); do { if (next == std::string::npos) next = searchpath.length(); std::string prefix = searchpath.substr(begin, next); if (prefix[0] == '.' && prefix[1] == '/') { prefix = prefix.substr(2); } /// LZU /// Change loading to support path like './?/init.lua' /* pos = prefix.find("?.lua"); chunkName = prefix.substr(0, pos) + filename + BYTECODE_FILE_EXT; */ pos = prefix.find("?"); prefix = prefix.substr(0,pos) + filename + prefix.substr(pos+1); pos = prefix.find(".lua"); chunkName = prefix.substr(0,pos) + BYTECODE_FILE_EXT; /// END LZU if (utils->isFileExist(chunkName)) { chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize); break; } else { /// LZU chunkName = prefix.substr(0,pos) + NOT_BYTECODE_FILE_EXT; // chunkName = prefix.substr(0, pos) + filename + NOT_BYTECODE_FILE_EXT; /// END LZU if (utils->isFileExist(chunkName)) { chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize); break; } } begin = next + 1; next = searchpath.find_first_of(";", begin); } while (begin < (int)searchpath.length()); if (chunk) { LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->luaLoadBuffer(L, (char*)chunk, (int)chunkSize, chunkName.c_str()); free(chunk); } else { CCLOG("can not get file data of %s", chunkName.c_str()); return 0; } return 1; }