aboutsummaryrefslogtreecommitdiff
path: root/advtrains_train_subway/textures/advtrains_subway_wagon_line3.png
blob: 19e3af1c9c44d279359c235caed2ac28e9025d6b (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 00 00 00 02 00 02 03 00 00 00 89 16 3c .PNG........IHDR...............<
0020 69 00 00 00 09 50 4c 54 45 00 00 00 7f 00 7f ff ff ff 4a f5 c0 0c 00 00 00 01 74 52 4e 53 00 40 i....PLTE.........J.......tRNS.@
0040 e6 d8 66 00 00 00 80 49 44 41 54 78 da ed d3 a1 11 c0 20 10 04 40 52 44 a8 2b e6 d3 01 86 be 30 ..f....IDATx.........@RD.+.....0
0060 a9 12 49 70 51 88 b0 eb de dd dc cd a7 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ..IpQ...........................
0080 00 00 00 00 f0 c9 19 d7 38 8e 88 e5 01 f2 f3 0e 50 db fa 06 ee a9 81 a2 01 13 ec f7 86 00 00 00 ........8.......P...............
00a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
00c0 00 00 00 00 c0 8f 75 5a c5 0d 8d 45 a8 1e 0f 00 00 00 00 49 45 4e 44 ae 42 60 82 ......uZ...E.......IEND.B`.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
-------------
-- lua sandboxed environment

-- function to cross out functions and userdata.
-- modified from dump()
function atlatc.remove_invalid_data(o, nested)
	if o==nil then return nil end
	local valid_dt={["nil"]=true, boolean=true, number=true, string=true}
	if type(o) ~= "table" then
		--check valid data type
		if not valid_dt[type(o)] then
			return nil
		end
		return o
	end
	-- Contains table -> true/nil of currently nested tables
	nested = nested or {}
	if nested[o] then
		return nil
	end
	nested[o] = true
	for k, v in pairs(o) do
		v = atlatc.remove_invalid_data(v, nested)
	end
	nested[o] = nil
	return o
end


local env_proto={
	load = function(self, envname, data)
		self.name=envname
		self.sdata=data.sdata and atlatc.remove_invalid_data(data.sdata) or {}
		self.fdata={}
		self.init_code=data.init_code or ""
		self.step_code=data.step_code or ""
	end,
	save = function(self)
		-- throw any function values out of the sdata table
		self.sdata = atlatc.remove_invalid_data(self.sdata)
		return {sdata = self.sdata, init_code=self.init_code, step_code=self.step_code}
	end,
}

--Environment
--Code modified from mesecons_luacontroller (credit goes to Jeija and mesecons contributors)

local safe_globals = {
	"assert", "error", "ipairs", "next", "pairs", "select",
	"tonumber", "tostring", "type", "unpack", "_VERSION"
}

--print is actually minetest.chat_send_all()
--using advtrains.print_concat_table because it's cool
local function safe_print(t, ...)
	local str=advtrains.print_concat_table({t, ...})
	minetest.log("action", "[atlatc] "..str)
	minetest.chat_send_all(str)
end

local function safe_date()
	return(os.date("*t",os.time()))
end

-- string.rep(str, n) with a high value for n can be used to DoS
-- the server. Therefore, limit max. length of generated string.
local function safe_string_rep(str, n)
	if #str * n > 2000 then
		debug.sethook() -- Clear hook
		error("string.rep: string length overflow", 2)
	end

	return string.rep(str, n)
end

-- string.find with a pattern can be used to DoS the server.
-- Therefore, limit string.find to patternless matching.
-- Note: Disabled security since there are enough security leaks and this would be unneccessary anyway to DoS the server
local function safe_string_find(...)
	--if (select(4, ...)) ~= true then
	--	debug.sethook() -- Clear hook
	--	error("string.find: 'plain' (fourth parameter) must always be true for security reasons.")
	--end

	return string.find(...)
end

local mp=minetest.get_modpath("advtrains_luaautomation")

local static_env = {
	--core LUA functions
	print = safe_print,
	string = {
		byte = string.byte,
		char = string.char,
		format = string.format,
		len = string.len,
		lower = string.lower,
		upper = string.upper,
		rep = safe_string_rep,
		reverse = string.reverse,
		sub = string.sub,
		find = safe_string_find,
	},
	math = {
		abs = math.abs,
		acos = math.acos,
		asin = math.asin,
		atan = math.atan,
		atan2 = math.atan2,
		ceil = math.ceil,
		cos = math.cos,
		cosh = math.cosh,
		deg = math.deg,
		exp = math.exp,
		floor = math.floor,
		fmod = math.fmod,
		frexp = math.frexp,
		huge = math.huge,
		ldexp = math.ldexp,
		log = math.log,
		log10 = math.log10,
		max = math.max,
		min = math.min,
		modf = math.modf,
		pi = math.pi,
		pow = math.pow,
		rad = math.rad,
		random = math.random,
		sin = math.sin,
		sinh = math.sinh,
		sqrt = math.sqrt,
		tan = math.tan,
		tanh = math.tanh,
	},
	table = {
		concat = table.concat,
		insert = table.insert,
		maxn = table.maxn,
		remove = table.remove,
		sort = table.sort,
	},
	os = {
		clock = os.clock,
		difftime = os.difftime,
		time = os.time,
		date = safe_date,
	},
	POS = function(x,y,z) return {x=x, y=y, z=z} end,
	getstate = advtrains.getstate,
	setstate = advtrains.setstate,
	is_passive = advtrains.is_passive,
	--interrupts are handled per node, position unknown. (same goes for digilines)
	--however external interrupts can be set here.
	interrupt_pos = function(parpos, imesg)
		local pos=atlatc.pcnaming.resolve_pos(parpos)
		atlatc.interrupt.add(0, pos, {type="ext_int", ext_int=true, message=imesg})
	end,
}

-- If interlocking is present, enable route setting functions
if advtrains.interlocking then
	local function gen_checks(signal, route_name, noroutesearch)
		assertt(route_name, "string")
		local pos = atlatc.pcnaming.resolve_pos(signal)
		local sigd = advtrains.interlocking.db.get_sigd_for_signal(pos)
		if not sigd then
			error("There's no signal at "..minetest.pos_to_string(pos))
		end
		local tcbs = advtrains.interlocking.db.get_tcbs(sigd)
		if not tcbs then
			error("Inconsistent configuration, no tcbs for signal at "..minetest.pos_to_string(pos))
		end
		
		local routeid, route
		if not noroutesearch then
			for routeidt, routet in ipairs(tcbs.routes) do
				if routet.name == route_name then
					routeid = routeidt
					route = routet
					break
				end
			end
			if not route then
				error("No route called "..route_name.." at "..minetest.pos_to_string(pos))
			end
		end
		return pos, sigd, tcbs, routeid, route
	end


	static_env.can_set_route = function(signal, route_name)
		local pos, sigd, tcbs, routeid, route = gen_checks(signal, route_name)
		-- if route is already set on signal, return whether it's committed
		if tcbs.routeset == routeid then
			return tcbs.route_committed
		end
		-- actually try setting route (parameter 'true' designates try-run
		local ok = advtrains.interlocking.route.set_route(sigd, route, true)
		return ok
	end
	static_env.set_route = function(signal, route_name)
		local pos, sigd, tcbs, routeid, route = gen_checks(signal, route_name)
		return advtrains.interlocking.route.update_route(sigd, tcbs, routeid)
	end
	static_env.cancel_route = function(signal)
		local pos, sigd, tcbs, routeid, route = gen_checks(signal, "", true)
		return advtrains.interlocking.route.update_route(sigd, tcbs, nil, true)
	end
	static_env.get_aspect = function(signal)
		local pos = atlatc.pcnaming.resolve_pos(signal)
		return advtrains.interlocking.signal_get_aspect(pos)
	end
	static_env.set_aspect = function(signal, asp)
		local pos = atlatc.pcnaming.resolve_pos(signal)
		return advtrains.interlocking.signal_set_aspect(pos)
	end
end

for _, name in pairs(safe_globals) do
	static_env[name] = _G[name]
end


--The environment all code calls get is a table that has set static_env as metatable.
--In general, every variable is local to a single code chunk, but kept persistent over code re-runs. Data is also saved, but functions and userdata and circular references are removed
--Init code and step code's environments are not saved
-- S - Table that can contain any save data global to the environment. Will be saved statically. Can't contain functions or userdata or circular references.
-- F - Table global to the environment, can contain volatile data that is deleted when server quits.
--     The init code should populate this table with functions and other definitions.

local proxy_env={}
--proxy_env gets a new metatable in every run, but is the shared environment of all functions ever defined.

-- returns: true, fenv if successful; nil, error if error 
function env_proto:execute_code(localenv, code, evtdata, customfct)
	local metatbl ={
		__index = function(t, i)
			if i=="S" then
				return self.sdata
			elseif i=="F" then
				return self.fdata
			elseif i=="event" then
				return evtdata
			elseif customfct and customfct[i] then
				return customfct[i]
			elseif localenv and localenv[i] then
				return localenv[i]
			end
			return static_env[i]
		end,
		__newindex = function(t, i, v)
			if i=="S" or i=="F" or i=="event" or (customfct and customfct[i]) or static_env[i] then
				debug.sethook()
				error("Trying to overwrite environment contents")
			end
			localenv[i]=v
		end,
	}
	setmetatable(proxy_env, metatbl)
	local fun, err=loadstring(code)
	if not fun then
		return false, err
	end
	
	setfenv(fun, proxy_env)
	local succ, data = pcall(fun)
	if succ then
		data=localenv
	end
	return succ, data
end

function env_proto:run_initcode()
	if self.init_code and self.init_code~="" then
		local old_fdata=self.fdata
		self.fdata = {}