Esempio n. 1
0
		private void RunScript()
		{
#if !DEBUG
			try
			{
#endif
            	ScriptLine line;
            	//int while_count = 0;

                if (System.String.Equals(Globals.Script_MainFile, ""))
            	{
                    Globals.gamedata.CurrentScriptState = ScriptState.Stopped;
                    ScriptEngine.Script_Error("Script Thread entry point not set... did you forget to load first?");
                    Globals.l2net_home.SetStartScript();
	                return;
            	}
                
                //clean up pathfinding stuff
                ScriptEngine.is_Moving = false;
                ScriptEngine.Moving_List.Clear();
                
	            System.IO.StreamReader filein = new System.IO.StreamReader(Globals.Script_MainFile);
				ScriptFile sf = new ScriptFile();
				sf.Name = Globals.Script_MainFile;
				sf.ReadScript(filein);
				filein.Close();

                Files.Add(sf.Name, sf);

	            ScriptThread scr_thread = new ScriptThread();
	            scr_thread.Current_File = Globals.Script_MainFile;
	            scr_thread.Line_Pos = 0;

	            VariableList stack_bottom = new VariableList();
	            scr_thread._stack.Add(stack_bottom);

                scr_thread.ID = GetUniqueThreadID();
                Threads.Add(scr_thread.ID, scr_thread);

	            ScriptEvent pop_event;
	            ScriptEventCaller evn_call;

                bool all_sleeping;
                bool time_passed;

                while (true)//Globals.gamedata.running)
                {
                    all_sleeping = true;

                    //need to check the state of the script thread
                    if (IsRunning())//if running
                    {
#if !TESTING
                        try
                        {
#endif
                            foreach (ScriptThread cthread in Threads.Values)
                            {
                                CurrentThread = cthread.ID;

                                System.DateTime finish = System.DateTime.Now.AddTicks(Globals.Script_Ticks_Per_Switch);
                                time_passed = false;
                                BumpThread = false;

                                while (IsRunning() && (cthread.Sleep_Until < System.DateTime.Now) && !time_passed)
                                {
                                    line = Get_Line(Line_Pos);

                                    if (Proccess_Line(line, true))
                                    {
                                        all_sleeping = false;
                                    }

                                    time_passed = (System.DateTime.Now > finish || BumpThread);
                                }
                            }
#if !TESTING
                        }
                        catch
                        {
                            //set was modified... no biggie
                        }
#endif

                        try
                        {
                            //lets pop off all our events
                            lock (eventqueueLock)
                            {
                                while (EventQueueCount() > 0)
                                {
                                    all_sleeping = false;
                                    pop_event = EventQueueDequeue();

                                    //time to handle the event... fun fun fun

                                    switch (pop_event.Type)
                                    {
                                        case EventType.ServerPacket:
                                            if (ServerPacketsContainsKey((int)pop_event.Type2))
                                            {
                                                //the event exists...
                                                evn_call = ServerPacketsGetCaller((int)pop_event.Type2);

                                                Call_Event(evn_call, pop_event);
                                            }
                                            break;
                                        case EventType.ServerPacketEX:
                                            if (ServerPacketsEXContainsKey((int)pop_event.Type2))
                                            {
                                                //the event exists...
                                                evn_call = ServerPacketsEXGetCaller((int)pop_event.Type2);

                                                Call_Event(evn_call, pop_event);
                                            }
                                            break;
                                        case EventType.ClientPacket:
                                            if (ClientPacketsContainsKey((int)pop_event.Type2))
                                            {
                                                //the event exists...
                                                evn_call = ClientPacketsGetCaller((int)pop_event.Type2);

                                                Call_Event(evn_call, pop_event);
                                            }
                                            break;
                                        case EventType.ClientPacketEX:
                                            if (ClientPacketsEXContainsKey((int)pop_event.Type2))
                                            {
                                                //the event exists...
                                                evn_call = ClientPacketsEXGetCaller((int)pop_event.Type2);

                                                Call_Event(evn_call, pop_event);
                                            }
                                            break;
                                        case EventType.SelfPacket:
                                            if (SelfPacketsContainsKey((int)pop_event.Type2))
                                            {
                                                //the event exists...
                                                evn_call = SelfPacketsGetCaller((int)pop_event.Type2);

                                                Call_Event(evn_call, pop_event);
                                            }
                                            break;
                                        case EventType.SelfPacketEX:
                                            if (SelfPacketsEXContainsKey((int)pop_event.Type2))
                                            {
                                                //the event exists...
                                                evn_call = SelfPacketsEXGetCaller((int)pop_event.Type2);

                                                Call_Event(evn_call, pop_event);
                                            }
                                            break;
                                        default:
                                            //need to check if the event exists in our list
                                            if (EventsContainsKey((int)pop_event.Type))
                                            {
                                                //the event exists...
                                                evn_call = EventsGetCaller((int)pop_event.Type);
                                                Call_Event(evn_call, pop_event);
                                            }
                                            break;
                                    }
                                }//end of while dequeque
                            }
                        }
                        catch//(Exception e)
                        {
                            ScriptEngine.Script_Error("Event Error... possible wrong file or function name?");
                        }
                    }//end of if

                    switch (Globals.gamedata.CurrentScriptState)
                    {
                        case ScriptState.Stopped:
                        case ScriptState.Running:
                            break;
                        case ScriptState.Paused:
                            System.Threading.Thread.Sleep(Globals.SLEEP_WhileScript);
                            break;
                        case ScriptState.DelayStart:
                            System.Threading.Thread.Sleep(Globals.SLEEP_Script_Reset);
                            Globals.gamedata.CurrentScriptState = ScriptState.Running;
                            break;
                        default:
                            return;
                    }

                    if (all_sleeping)
                    {
                        System.Threading.Thread.Sleep(Globals.SLEEP_WhileScript);
                    }
                }//end of while true
#if !DEBUG
			}
			catch (Exception e)
			{
                string broke_file = "";
                try
                {
                    broke_file = ((ScriptThread)Threads[CurrentThread]).Current_File;
                }
                catch
                {
                    broke_file = "no file loaded";
                }
                ScriptEngine.Script_Error("Script Thread Crashed File: " + broke_file);
                ScriptEngine.Script_Error(e.Message + " : " + e.InnerException);
			}
#endif
        }
Esempio n. 2
0
        private void Call_Event(ScriptEventCaller evn_call, ScriptEvent pop_event)
        {
            if (evn_call == null)
            {
                return;
            }
            //is the file loaded?
            if (!Files.ContainsKey(evn_call.File))
            {
                System.IO.StreamReader filein = new System.IO.StreamReader(evn_call.File);
                ScriptFile sf = new ScriptFile();
                sf.Name = evn_call.File;
                sf.ReadScript(filein);
                filein.Close();

                Files.Add(sf.Name, sf);
            }
            //find the function
            int dest_line = Get_Function_Line(evn_call.Function, evn_call.File);
            if (dest_line == -1)
            {
                ScriptEngine.Script_Error("EVENT CALLER: FUNCTION DOES NOT EXIST : " + evn_call.File + " : " + evn_call.Function);
                Globals.gamedata.CurrentScriptState = ScriptState.Error;
                return;
            }

            //create a new thread at the function...
            ScriptThread scr_thread = new ScriptThread();
            scr_thread.Current_File = evn_call.File;
            scr_thread.Line_Pos = dest_line + 1;

            VariableList stack_bottom = new VariableList();
            //copy the variables over
            foreach (ScriptVariable svar in pop_event.Variables)
            {
                stack_bottom.Add_Variable(svar);
            }
            scr_thread._stack.Add(stack_bottom);
            scr_thread.ID = GetUniqueThreadID();
            Threads.Add(scr_thread.ID, scr_thread);
        }
Esempio n. 3
0
        private static ScriptThread CreateThread(string fname)
        {
            int lin = Get_Function_Line(fname, ((ScriptThread)Threads[CurrentThread]).Current_File);

            ScriptThread scr_thread = new ScriptThread();
            scr_thread.Current_File = ((ScriptThread)Threads[CurrentThread]).Current_File;
            scr_thread.Line_Pos = lin + 1;

            VariableList stack_bottom = new VariableList();
            scr_thread._stack.Add(stack_bottom);

            scr_thread.ID = GetUniqueThreadID();

            return scr_thread;
        }