欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

applechronos.lua

时间太紧了
LUA
第 1 页 / 共 2 页
字号:
	--	args:
	--		id - ID for the timer. If not specified, then ID will
	--		be this:GetName()
	--
	--	returns:
	--		true - exists
	--		false - does not
	--]]
	isTimerActive = function ( id ) 
		if ( not Chronos.online ) then 
			return;
		end

		if ( not id ) then 
			id = this:GetName();
		end

		-- Create a table for this id's timers
		if ( not ChronosData.timers[id] ) then
			return false;
		end

		return true;
	end;

	--[[
	--	getTime()
	--
	--		returns the Chronos internal elapsed time.
	--
	--	returns:
	--		(elaspedTime)
	--		
	--		elapsedTime - time in seconds since Chronos initialized
	--]]
	
	getTime = function() 
		return ChronosData.elapsedTime;
	end;

	--[[
	--	performTask( TaskObject );
	--		
	--		Queues up a task to be completed over time. 
	--		Contains a before and after function 
	--		to be called when the task is started and
	--		completed.
	--		
	--	Args:
	--		TaskObject - a table containing:
	--		{
	--		 (Required:)
	--		  step - function to be performed, must be fast
	--
	--		  isDone - function which determines if the 
	--		  	task is completed. 
	--		  	Returns true when done
	--		  	Returns false if the task should continue
	--		  	 to call step() each frame. 
	--		  
	--
	--		 (Optional:)
	--		  stepArgs - arguments to be passed to step
	--		  doneArgs - arguments to be passed to isDone
	--
	--		  before - function called before the first step
	--		  beforeArgs - arguments passed to Before
	--
	--		  after - function called when isDone returns true
	--		  afterArgs - arguments passed
	--
	--		  limit - a number defining the maximum number
	--		  	of steps that will be peformed before
	--		  	the task is removed to prevent lag.
	--		  	(Defaults to 100)
	--		}
	--]]

	performTask = function (taskTable, name) 
		if ( not Chronos.online ) then 
			return;
		end

		-- Valid table?
		if ( not taskTable ) then
			--Sea.io.error ("Chronos Error Detection: Invalid table to Chronos.peformTask", this:GetName());
			return nil;
		end

		-- Must contain a step function
		if ( not taskTable.step or type(taskTable.step) ~= "function" ) then 
			--Sea.io.derror(CH_DEBUG_T,"Chronos Error Detection: You must specify a step function to be called to perform the task. (",this:GetName(),")");
			return nil;
		end
		
		-- Must contain a completion function
		if ( not taskTable.isDone or type(taskTable.isDone) ~= "function" ) then 
			--Sea.io.derror(CH_DEBUG_T,"Chronos Error Detection: You must specify an isDone function to be called to indicate if the task is complete. (",this:GetName(),")");
			return nil;
		end

		-- Get an ID
		if ( not name ) then 
			name = this:GetName();
		end

		-- Set the limit
		if ( not taskTable.limit ) then 
			taskTable.limit = Chronos.MAX_STEPS_PER_TASK;
		end
		
		local foundId = nil;

		for i=1,table.getn(ChronosData.tasks) do 
			if ( ChronosData.tasks[i].name == id ) then 
				foundId = i;
			end
		end
		
		-- Add it to the task list
		if ( not foundId ) then 
			taskTable.name = name;
			table.insert(ChronosData.tasks, taskTable);
			return true;
		elseif ( not ChronosData.tasks[foundId].errorSent ) then
			ChronosData.tasks[foundId].errorSent = true;
			--Sea.io.error ("Chronos Error Detection: There's already a task with the ID: ", name );
			return nil;
		end
	end;

	--[[
	--	Chronos.afterInit(func, ...)
	--		Performs func after the game has truely started.
	--]]
	afterInit = function (func, ...)
		if(UnitName("player") and UnitName("player")~=UKNOWNBEING and UnitName("player")~=UNKNOWNBEING and UnitName("player")~=UNKNOWNOBJECT and ChronosData.variablesLoaded) then
			func(unpack(arg));
		else
			Chronos.schedule(0.2,Chronos.afterInit,func,unpack(arg));
		end
	end;
};

--[[ Event Handlers ]]--
function Chronos_OnLoad()
	ChronosData.elapsedTime = 0;
	ChronosData.variablesLoaded = false;
	Chronos.afterInit(Chronos_SkyRegister);
	this:RegisterEvent("VARIABLES_LOADED");	
end

function Chronos_OnEvent(event)
	if(event == "VARIABLES_LOADED") then
		ChronosData.variablesLoaded = true;
		ChronosFrame:Show();
	end	
end

function Chronos_OnUpdate(dt)
	if ( not Chronos.online ) then 
		return;
	end
	if ( ChronosData.variablesLoaded == false ) then 
		return;
	end
	
	if ( ChronosData.elapsedTime ) then
		ChronosData.elapsedTime = ChronosData.elapsedTime + dt;
	else
		ChronosData.elapsedTime = dt;
	end

	local timeThisUpdate = 0;
	local largest = 0;
	local largestName = nil;

	if ( not ChronosData.anonTodo ) then 
		ChronosData.anonTodo = {};
	end

	-- Handle Anonymous Scheduled Tasks
	for k,v in ChronosData.anonTodo do 
		ChronosData.lastID = k;
		-- Call all handlers whose time has been exceeded
		while(v[1] and v[1].time <= GetTime()) do
			-- Lets start the timer
			Chronos.startTimer();

			
			local todo = table.remove(v,1);
			if(todo.args) then
				if ( todo.handler ) then 
					todo.handler(unpack(todo.args));
				end
			else
				if ( todo.handler ) then 
					todo.handler();
				end
			end
			-- End the timer
			local runTime = Chronos.endTimer();
		
			-- Update the elapsed time
			timeThisUpdate = timeThisUpdate + runTime;

			-- Check if this was the biggest hog yet
			if ( runTime > largest ) then 
				largest = runTime;
				largestName = k;
			end

			-- Check if we've overrun our limit
			if ( timeThisUpdate > Chronos.MAX_TIME_PER_STEP ) then
				--Sea.io.derror(CH_DEBUG_T,"Chronos Warns: Maximum cpu time usage exceeded by an Addon. Largest was: ", largestName );
				break;
			end
		end	

		-- Clean out the table
		if ( table.getn(v) == 0 ) then 
			ChronosData.anonTodo[k] = nil;
		end
	end

	-- ### Remove later for efficiency
	if ( largestName ) then
		--MFC.IO.print(CH_DEBUG, " Largest anonymous task: ", largestName, ", ", largest);
	end

	
	-- Handle Named
	-- Call all handlers whose time has been exceeded
	while( ChronosData.namedTodo[1] and 
		ChronosData.namedTodo[1].time <= GetTime()) do
		
		-- Lets start the timer
		Chronos.startTimer();

		-- Lets perform the action
		local todo = table.remove(ChronosData.namedTodo,1);
		if(todo.args) then
			todo.handler(unpack(todo.args));
		else
			todo.handler();
		end
		
		-- End the timer
		local runTime = Chronos.endTimer();
		
		-- Update the elapsed time
		timeThisUpdate = timeThisUpdate + runTime;

		-- Check if this was the biggest hog yet
		if ( runTime > largest ) then 
			largest = runTime;
			largestName = todo.name;
		end

		-- Check if we've overrun our limit
		if ( timeThisUpdate > Chronos.MAX_TIME_PER_STEP ) then
			--Sea.io.derror(CH_DEBUG_T,"Chronos Warning: Maximum cpu time-limit exceeded. Largest was: ", largestName );
			break;
		end

		-- ### Remove later for efficiency
		--MFC.IO.print(CH_DEBUG, " Largest named task: ", todo.name );
	end
		
	local largest = 0;
	local largestName = nil;

	-- Perform tasks if the time limit is not exceeded
	-- Only perform each task once at most per update
	-- 
	local ctn = table.getn(ChronosData.tasks);
	for i=1, ctn do
		-- Perform a task
		runTime = Chronos_OnUpdate_Tasks(timeThisUpdate);
		timeThisUpdate = timeThisUpdate + runTime;

		-- Check if this was the biggest hog yet
		if ( runTime > largest ) then 
			largest = runTime;
			largestName = i;
		end

		-- Check if we've overrun our limit
		if ( timeThisUpdate > Chronos.MAX_TIME_PER_STEP ) then
			--Sea.io.derror(CH_DEBUG_T,"Chronos Warning: Maximum cpu usage time exceeded on task. Largest task was: ", largestName );
			break
		end

		if ( largestName ) then
			-- ### Remove later for efficiency
			--MFC.IO.print(CH_DEBUG, " Largest named task: ", largestName );
		end
	end		
end

-- Updates a single task
function Chronos_OnUpdate_Tasks(timeThisUpdate)
	if ( not Chronos.online ) then 
		return;
	end

	-- Lets start the timer
	Chronos.startTimer();

	-- Execute the first task
	if ( ChronosData.tasks[1] ) then
		-- Obtains the first task
		local task = table.remove(ChronosData.tasks, 1);

		-- Start the task if not yet started
		if ( not task.started ) then 
			if ( task.before ) then
				if ( task.beforeArgs ) then
					task.before(unpack(task.beforeArgs));
				else
					task.before();
				end
			end

			-- Mark the task as started
			task.started = true;
		end

		-- Perform a step in the task
		if ( task.stepArgs ) then
			task.step(unpack(task.stepArgs));
		else
			task.step();
		end
			
		-- Check if the task is completed.
		if ( task.isDone() ) then
			-- Call the after-task
			if ( task.after ) then
				if ( task.afterArgs ) then 
					task.after(unpack(task.afterArgs) );
				else
					task.after();
				end
			end
		else
			if ( not task.count ) then 
				task.count = 1; 
			else
				task.count = task.count + 1; 				
			end

			if ( task.count < task.limit ) then 
				-- Move them to the back of the list
				table.insert(ChronosData.tasks, task);
			else
				--Sea.io.derror(CH_DEBUG_T, "Task killed due to limit-break: ", task.name ); 
			end
		end
	end

	-- End the timer
	return Chronos.endTimer();		
end

-- Cosmos Registrations
-- Notes to self: 
-- 	* Change to Sky Registration
-- 	* Relocate to its own module?
-- 	

function Chronos_SkyRegister()
	if(Sky) then
		local comlist = SCHEDULE_COMM;
		local desc = SCHEDULE_DESC;
		local id = "SCHEDULE";
		local func = function(msg)
			local i,j,seconds,command = string.find(msg,"([%d\.]+) (.*)");
			if(seconds and command) then
				Chronos.schedule(seconds,Chronos_SendChatCommand,command);
			else
			--	Sea.io.print(SCHEDULE_USAGE1);
			--	Sea.io.print(SCHEDULE_USAGE2);
			end
		end
		Sky.registerSlashCommand(
			{
			id=id;
			commands=comlist;
			onExecute=func;
			description=desc;
			}
		);
	end
end

--[[
--	Sends a chat command through the standard 
--]]
function Chronos_SendChatCommand(command)
	local text = ChatFrameEditBox:GetText();
	ChatFrameEditBox:SetText(command);
	ChatEdit_SendText(ChatFrameEditBox);
	ChatFrameEditBox:SetText(text);
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -