Example #1
0
int LuaEvents::UnregisterEventHook(lua_State *l, ByteString eventName)
{
	if (lua_isfunction(l, 2))
	{
		lua_pushstring(l, eventName.c_str());
		lua_rawget(l, LUA_REGISTRYINDEX);
		if (!lua_istable(l, -1))
		{
			lua_pop(l, -1);
			lua_newtable(l);
			lua_pushstring(l, eventName.c_str());
			lua_pushvalue(l, -2);
			lua_rawset(l, LUA_REGISTRYINDEX);
		}
		int len = lua_objlen(l, -1);
		int adjust = 0;
		for (int i = 1; i <= len; i++)
		{
			lua_rawgeti(l, -1, i + adjust);
			// unregister the function
			if (lua_equal(l, 2, -1))
			{
				lua_pop(l, 1);
				adjust++;
				i--;
			}
			// Update the function index in the table if we've removed a previous function
			else if (adjust)
				lua_rawseti(l, -2, i);
			else
				lua_pop(l, 1);
		}
	}
	return 0;
}
int luatpt_getscript(lua_State* l)
{
	int scriptID = luaL_checkinteger(l, 1);
	const char *filename = luaL_checkstring(l, 2);
	int runScript = luaL_optint(l, 3, 0);
	int confirmPrompt = luaL_optint(l, 4, 1);

	ByteString url = ByteString::Build(SCHEME "starcatcher.us/scripts/main.lua?get=", scriptID);
	if (confirmPrompt && !ConfirmPrompt::Blocking("Do you want to install script?", url.FromUtf8(), "Install"))
		return 0;

	int ret;
	ByteString scriptData = http::Request::Simple(url, &ret);
	if (!scriptData.size() || !filename)
	{
		return luaL_error(l, "Server did not return data");
	}
	if (ret != 200)
	{
		return luaL_error(l, http::StatusText(ret).ToUtf8().c_str());
	}

	if (!strcmp(scriptData.c_str(), "Invalid script ID\r\n"))
	{
		return luaL_error(l, "Invalid Script ID");
	}

	FILE *outputfile = fopen(filename, "r");
	if (outputfile)
	{
		fclose(outputfile);
		outputfile = NULL;
		if (!confirmPrompt || ConfirmPrompt::Blocking("File already exists, overwrite?", ByteString(filename).FromUtf8(), "Overwrite"))
		{
			outputfile = fopen(filename, "wb");
		}
		else
		{
			return 0;
		}
	}
	else
	{
		outputfile = fopen(filename, "wb");
	}
	if (!outputfile)
	{
		return luaL_error(l, "Unable to write to file");
	}

	fputs(scriptData.c_str(), outputfile);
	fclose(outputfile);
	outputfile = NULL;
	if (runScript)
	{
		luaL_dostring(l, ByteString::Build("dofile('", filename, "')").c_str());
	}

	return 0;
}
Example #3
0
bool LuaEvents::HandleEvent(LuaScriptInterface *luacon_ci, Event *event, ByteString eventName)
{
	ui::Engine::Ref().LastTick(Platform::GetTime());
	bool cont = true;
	lua_State* l = luacon_ci->l;
	lua_pushstring(l, eventName.c_str());
	lua_rawget(l, LUA_REGISTRYINDEX);
	if (!lua_istable(l, -1))
	{
		lua_pop(l, 1);
		lua_newtable(l);
		lua_pushstring(l, eventName.c_str());
		lua_pushvalue(l, -2);
		lua_rawset(l, LUA_REGISTRYINDEX);
	}
	int len = lua_objlen(l, -1);
	for (int i = 1; i <= len && cont; i++)
	{
		lua_rawgeti(l, -1, i);
		int numArgs = event->PushToStack(l);
		int callret = lua_pcall(l, numArgs, 1, 0);
		if (callret)
		{
			if (luacon_geterror(luacon_ci) == "Error: Script not responding")
			{
				ui::Engine::Ref().LastTick(Platform::GetTime());
				for (int j = i; j <= len - 1; j++)
				{
					lua_rawgeti(l, -2, j + 1);
					lua_rawseti(l, -3, j);
				}
				lua_pushnil(l);
				lua_rawseti(l, -3, len);
				i--;
			}
			luacon_ci->Log(CommandInterface::LogError, luacon_geterror(luacon_ci));
			lua_pop(l, 1);
		}
		else
		{
			if (!lua_isnoneornil(l, -1))
				cont = lua_toboolean(l, -1);
			lua_pop(l, 1);
		}
		len = lua_objlen(l, -1);
	}
	lua_pop(l, 1);
	return cont;
}
Example #4
0
void OpenURI(ByteString uri)
{
#if defined(WIN)
	ShellExecute(0, "OPEN", uri.c_str(), NULL, NULL, 0);
#elif defined(MACOSX)
	char *cmd = (char*)malloc(7+uri.length());
	strcpy(cmd, "open ");
	strappend(cmd, (char*)uri.c_str());
	system(cmd);
#elif defined(LIN)
	char *cmd = (char*)malloc(11+uri.length());
	strcpy(cmd, "xdg-open ");
	strappend(cmd, (char*)uri.c_str());
	system(cmd);
#else
	printf("Cannot open browser\n");
#endif
}
Example #5
0
int LuaEvents::RegisterEventHook(lua_State *l, ByteString eventName)
{
	if (lua_isfunction(l, 2))
	{
		lua_pushstring(l, eventName.c_str());
		lua_rawget(l, LUA_REGISTRYINDEX);
		if (!lua_istable(l, -1))
		{
			lua_pop(l, 1);
			lua_newtable(l);
			lua_pushstring(l, eventName.c_str());
			lua_pushvalue(l, -2);
			lua_rawset(l, LUA_REGISTRYINDEX);
		}
		int c = lua_objlen(l, -1);
		lua_pushvalue(l, 2);
		lua_rawseti(l, -2, c + 1);
	}
	lua_pushvalue(l, 2);
	return 1;
}
Example #6
0
//=============================================================================
double NumUtils::ToDouble(const String& str)
{
	try
	{
		const ByteString ascii = StringUtils::ToAscii(str);
		return ::strtod(ascii.c_str(), 0);
	}
	catch(Exception& /*e*/)
	{
	}
	return 0.0;
}
Example #7
0
//=============================================================================
long NumUtils::ToLong(const String& str, int base)
{
	try
	{
		const ByteString ascii = StringUtils::ToAscii(str);
		return strtol(ascii.c_str(), 0, base);
	}
	catch(Exception& /*e*/)
	{
	}
	return 0L;
}
Example #8
0
FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDFAction_GetFilePath(FPDF_ACTION pDict, void* buffer, unsigned long buflen) {
  unsigned long type = FPDFAction_GetType(pDict);
  if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH)
    return 0;

  CPDF_Action action(CPDFDictionaryFromFPDFAction(pDict));
  ByteString path = action.GetFilePath().ToUTF8();
  unsigned long len = path.GetLength() + 1;
  if (buffer && len <= buflen)
    memcpy(buffer, path.c_str(), len);
  return len;
}
void FileBrowserActivity::RenameSave(SaveFile * file)
{
	ByteString newName = TextPrompt::Blocking("Rename", "Change save name", file->GetDisplayName(), "", 0).ToUtf8();
	if (newName.length())
	{
		newName = directory + PATH_SEP + newName + ".cps";
		int ret = rename(file->GetName().c_str(), newName.c_str());
		if (ret)
			ErrorMessage::Blocking("Error", "Could not rename file");
		else
			loadDirectory(directory, "");
	}
	else
		ErrorMessage::Blocking("Error", "No save name given");
}
Example #10
0
FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDFAction_GetURIPath(FPDF_DOCUMENT document,
                      FPDF_ACTION pDict,
                      void* buffer,
                      unsigned long buflen) {
  CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
  if (!pDoc)
    return 0;

  unsigned long type = FPDFAction_GetType(pDict);
  if (type != PDFACTION_URI)
    return 0;

  CPDF_Action action(CPDFDictionaryFromFPDFAction(pDict));
  ByteString path = action.GetURI(pDoc);
  unsigned long len = path.GetLength() + 1;
  if (buffer && len <= buflen)
    memcpy(buffer, path.c_str(), len);
  return len;
}
int luatpt_screenshot(lua_State* l)
{
	int captureUI = luaL_optint(l, 1, 0);
	int fileType = luaL_optint(l, 2, 0);
	std::vector<char> data;
	if(captureUI)
	{
		VideoBuffer screenshot(ui::Engine::Ref().g->DumpFrame());
		if(fileType == 1) {
			data = format::VideoBufferToBMP(screenshot);
		} else if(fileType == 2) {
			data = format::VideoBufferToPPM(screenshot);
		} else {
			data = format::VideoBufferToPNG(screenshot);
		}
	}
	else
	{
		VideoBuffer screenshot(luacon_ren->DumpFrame());
		if(fileType == 1) {
			data = format::VideoBufferToBMP(screenshot);
		} else if(fileType == 2) {
			data = format::VideoBufferToPPM(screenshot);
		} else {
			data = format::VideoBufferToPNG(screenshot);
		}
	}
	ByteString filename = ByteString::Build("screenshot_", Format::Width(screenshotIndex++, 6));
	if(fileType == 1) {
		filename += ".bmp";
	} else if(fileType == 2) {
		filename += ".ppm";
	} else {
		filename += ".png";
	}
	Client::Ref().WriteFile(data, filename);
	lua_pushstring(l, filename.c_str());
	return 1;
}
Example #12
0
 void DebugLog::write_entry(UnicodeStringRef entry)
 {
     ByteString bytes;
     ascii_encode(entry.get(), &bytes);
     OutputDebugStringA((char*)bytes.c_str());
 }
Example #13
0
void Event::PushString(lua_State * l, ByteString str)
{
#ifdef LUACONSOLE
	lua_pushstring(l, str.c_str());
#endif
}