mirror of
				https://bitbucket.org/Ioncannon/project-meteor-server.git
				synced 2025-05-20 08:26:59 -04:00 
			
		
		
		
	Removed NLua and replaced it with MoonSharp. Scripting for NPCs has been implemented, but still have to test a lot.
This commit is contained in:
		| @@ -4,7 +4,8 @@ using FFXIVClassic_Map_Server.dataobjects; | ||||
| using FFXIVClassic_Map_Server.packets.receive.events; | ||||
| using FFXIVClassic_Map_Server.packets.send; | ||||
| using FFXIVClassic_Map_Server.packets.send.events; | ||||
| using NLua; | ||||
| using MoonSharp.Interpreter; | ||||
| using MoonSharp.Interpreter.Interop; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.IO; | ||||
| @@ -17,59 +18,129 @@ namespace FFXIVClassic_Map_Server.lua | ||||
|     class LuaEngine | ||||
|     { | ||||
|         const string FILEPATH_COMMANDS = "./scripts/command/{0}.lua"; | ||||
|         const string FILEPATH_EVENTS = "./scripts/talk/{0}.lua"; | ||||
|  | ||||
|         Lua lstate = new Lua(); | ||||
|         const string FILEPATH_NPCS = "./scripts/zones/{0}/npcs/{1}.lua"; | ||||
|  | ||||
|         public LuaEngine() | ||||
|         {           | ||||
|         { | ||||
|             UserData.RegistrationPolicy = InteropRegistrationPolicy.Automatic; | ||||
|         } | ||||
|  | ||||
|         public List<LuaParam> doActorOnInstantiate(Player player, Actor target) | ||||
|         { | ||||
|             string luaPath; | ||||
|  | ||||
|             if (target is Npc) | ||||
|             { | ||||
|                 luaPath = String.Format(FILEPATH_NPCS, target.zoneId, target.getName()); | ||||
|                 if (File.Exists(luaPath)) | ||||
|                 { | ||||
|                     Script script = new Script(); | ||||
|                     script.DoFile(luaPath); | ||||
|                     DynValue result = script.Call(script.Globals["onInstantiate"], player, target); | ||||
|                     List<LuaParam> lparams = LuaUtils.createLuaParamList(result); | ||||
|                     return lparams; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     List<SubPacket> sendError = new List<SubPacket>(); | ||||
|                     sendError.Add(EndEventPacket.buildPacket(player.actorId, player.playerSession.eventCurrentOwner, player.playerSession.eventCurrentStarter)); | ||||
|                     player.sendMessage(SendMessagePacket.MESSAGE_TYPE_SYSTEM_ERROR, "", String.Format("ERROR: Could not find script for actor {0}.", target.getName())); | ||||
|                     player.playerSession.queuePacket(BasePacket.createPacket(sendError, true, false)); | ||||
|                     return null; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             return null; | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public void doEventStart(ConnectedPlayer player, Actor target, EventStartPacket packet) | ||||
|         public void doActorOnEventStarted(Player player, Actor target) | ||||
|         { | ||||
|             string luaPath; | ||||
|  | ||||
|             if (target is Command) | ||||
|             { | ||||
|                 luaPath = String.Format(FILEPATH_COMMANDS, target.getName()); | ||||
|                 if (File.Exists(luaPath)) | ||||
|                 { | ||||
|                     Script script = new Script(); | ||||
|                     script.DoFile(luaPath); | ||||
|                     DynValue result = script.Call(script.Globals["onEventStarted"], player, target); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     List<SubPacket> sendError = new List<SubPacket>(); | ||||
|                     sendError.Add(EndEventPacket.buildPacket(player.actorId, player.playerSession.eventCurrentOwner, player.playerSession.eventCurrentStarter)); | ||||
|                     player.sendMessage(SendMessagePacket.MESSAGE_TYPE_SYSTEM_ERROR, "", String.Format("ERROR: Could not find script for actor {0}.", target.getName())); | ||||
|                     player.playerSession.queuePacket(BasePacket.createPacket(sendError, true, false)); | ||||
|                 } | ||||
|             } | ||||
|             else if (target is Npc) | ||||
|                 luaPath = String.Format(FILEPATH_EVENTS, target.getName()); | ||||
|             else | ||||
|                 luaPath = ""; | ||||
|  | ||||
|             if (File.Exists(luaPath)) | ||||
|             { | ||||
|                 lstate.DoFile(luaPath); | ||||
|                 var eventStarted = lstate["eventStarted"] as LuaFunction; | ||||
|                 eventStarted.Call(new LuaPlayer(player), player.eventCurrentOwner, LuaUtils.createLuaParamObjectList(packet.luaParams)); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 List<SubPacket> sendError = new List<SubPacket>(); | ||||
|                 sendError.Add(EndEventPacket.buildPacket(player.actorID, player.eventCurrentOwner, player.eventCurrentStarter)); | ||||
|                 sendError.Add(SendMessagePacket.buildPacket(player.actorID, player.actorID, SendMessagePacket.MESSAGE_TYPE_GENERAL_INFO, "", "ERROR: Could not find script for event")); | ||||
|                 player.queuePacket(BasePacket.createPacket(sendError, true, false)); | ||||
|                 luaPath = String.Format(FILEPATH_NPCS, target.zoneId, target.getName()); | ||||
|                 if (File.Exists(luaPath)) | ||||
|                 { | ||||
|                     Script script = new Script(); | ||||
|                     script.DoFile(luaPath); | ||||
|                     DynValue result = script.Call(script.Globals["onEventStarted"], player, target); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     List<SubPacket> sendError = new List<SubPacket>(); | ||||
|                     sendError.Add(EndEventPacket.buildPacket(player.actorId, player.playerSession.eventCurrentOwner, player.playerSession.eventCurrentStarter)); | ||||
|                     player.sendMessage(SendMessagePacket.MESSAGE_TYPE_SYSTEM_ERROR, "", String.Format("ERROR: Could not find script for actor {0}.", target.getName())); | ||||
|                     player.playerSession.queuePacket(BasePacket.createPacket(sendError, true, false)); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public void doEventUpdated(ConnectedPlayer player, Actor target, EventUpdatePacket packet) | ||||
|         public void doActorOnEventUpdated(Player player, Actor target, EventUpdatePacket eventUpdate) | ||||
|         { | ||||
|             string luaPath = String.Format(FILEPATH_EVENTS, ((Command)target).getName()); | ||||
|             string luaPath; | ||||
|  | ||||
|             if (File.Exists(luaPath)) | ||||
|             if (target is Command) | ||||
|             { | ||||
|                 lstate.DoFile(luaPath); | ||||
|                 var eventStarted = lstate["eventUpdated"] as LuaFunction; | ||||
|                 eventStarted.Call(new LuaPlayer(player), player.eventCurrentOwner, packet.step, LuaUtils.createLuaParamObjectList(packet.luaParams)); | ||||
|                 luaPath = String.Format(FILEPATH_COMMANDS, target.getName()); | ||||
|                 if (File.Exists(luaPath)) | ||||
|                 { | ||||
|                     Script script = new Script(); | ||||
|                     script.DoFile(luaPath); | ||||
|                     DynValue result = script.Call(script.Globals["onEventUpdate"], player, target, eventUpdate.step, eventUpdate.luaParams); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     List<SubPacket> sendError = new List<SubPacket>(); | ||||
|                     sendError.Add(EndEventPacket.buildPacket(player.actorId, player.playerSession.eventCurrentOwner, player.playerSession.eventCurrentStarter)); | ||||
|                     player.sendMessage(SendMessagePacket.MESSAGE_TYPE_SYSTEM_ERROR, "", String.Format("ERROR: Could not find script for actor {0}.", target.getName())); | ||||
|                     player.playerSession.queuePacket(BasePacket.createPacket(sendError, true, false)); | ||||
|                 } | ||||
|             } | ||||
|             else | ||||
|             else if (target is Npc) | ||||
|             { | ||||
|                 List<SubPacket> sendError = new List<SubPacket>(); | ||||
|                 sendError.Add(EndEventPacket.buildPacket(player.actorID, player.eventCurrentOwner, player.eventCurrentStarter)); | ||||
|                 sendError.Add(SendMessagePacket.buildPacket(player.actorID, player.actorID, SendMessagePacket.MESSAGE_TYPE_GENERAL_INFO, "", "ERROR: Could not find script for event")); | ||||
|                 player.queuePacket(BasePacket.createPacket(sendError, true, false)); | ||||
|                 luaPath = String.Format(FILEPATH_NPCS, target.zoneId, target.getName()); | ||||
|                 if (File.Exists(luaPath)) | ||||
|                 { | ||||
|                     Script script = new Script(); | ||||
|                     script.DoFile(luaPath); | ||||
|  | ||||
|                     //Have to do this to combine LuaParams | ||||
|                     List<Object> objects = new List<Object>(); | ||||
|                     objects.Add(player); | ||||
|                     objects.Add(target); | ||||
|                     objects.Add(eventUpdate.step); | ||||
|                     objects.AddRange(LuaUtils.createLuaParamObjectList(eventUpdate.luaParams)); | ||||
|  | ||||
|                     //Run Script | ||||
|                     DynValue result = script.Call(script.Globals["onEventUpdate"], objects.ToArray()); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     List<SubPacket> sendError = new List<SubPacket>(); | ||||
|                     sendError.Add(EndEventPacket.buildPacket(player.actorId, player.playerSession.eventCurrentOwner, player.playerSession.eventCurrentStarter)); | ||||
|                     player.sendMessage(SendMessagePacket.MESSAGE_TYPE_SYSTEM_ERROR, "", String.Format("ERROR: Could not find script for actor {0}.", target.getName())); | ||||
|                     player.playerSession.queuePacket(BasePacket.createPacket(sendError, true, false)); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|          | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										27
									
								
								FFXIVClassic Map Server/lua/LuaNpc.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								FFXIVClassic Map Server/lua/LuaNpc.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| using FFXIVClassic_Map_Server.Actors; | ||||
| using FFXIVClassic_Map_Server.dataobjects; | ||||
| using FFXIVClassic_Map_Server.packets.send; | ||||
| using FFXIVClassic_Map_Server.packets.send.events; | ||||
| using MoonSharp.Interpreter; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| namespace FFXIVClassic_Map_Server.lua | ||||
| { | ||||
|  | ||||
|     [MoonSharpUserData] | ||||
|     class LuaNpc | ||||
|     { | ||||
|         private Npc npc; | ||||
|  | ||||
|         public LuaNpc(Npc npc) | ||||
|         { | ||||
|             this.npc = npc; | ||||
|         } | ||||
|  | ||||
|        | ||||
|     } | ||||
| } | ||||
| @@ -1,7 +1,8 @@ | ||||
| using FFXIVClassic_Map_Server.dataobjects; | ||||
| using FFXIVClassic_Map_Server.Actors; | ||||
| using FFXIVClassic_Map_Server.dataobjects; | ||||
| using FFXIVClassic_Map_Server.packets.send; | ||||
| using FFXIVClassic_Map_Server.packets.send.events; | ||||
| using NLua; | ||||
| using MoonSharp.Interpreter; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| @@ -10,23 +11,24 @@ using System.Threading.Tasks; | ||||
|  | ||||
| namespace FFXIVClassic_Map_Server.lua | ||||
| { | ||||
|     [MoonSharpUserData] | ||||
|     class LuaPlayer | ||||
|     { | ||||
|         private ConnectedPlayer player; | ||||
|         private Player player; | ||||
|  | ||||
|         public LuaPlayer(ConnectedPlayer player) | ||||
|         public LuaPlayer(Player player) | ||||
|         { | ||||
|             this.player = player; | ||||
|         } | ||||
|  | ||||
|         public void setMusic(ushort musicID, ushort playMode) | ||||
|         { | ||||
|             player.queuePacket(SetMusicPacket.buildPacket(player.actorID, musicID, playMode), true, false); | ||||
|             player.playerSession.queuePacket(SetMusicPacket.buildPacket(player.actorId, musicID, playMode), true, false); | ||||
|         } | ||||
|  | ||||
|         public void setWeather(uint weatherID) | ||||
|         { | ||||
|             player.queuePacket(SetWeatherPacket.buildPacket(player.actorID, weatherID), true, false); | ||||
|             player.playerSession.queuePacket(SetWeatherPacket.buildPacket(player.actorId, weatherID), true, false); | ||||
|         } | ||||
|  | ||||
|         public void getParameter(string paramName) | ||||
| @@ -51,23 +53,23 @@ namespace FFXIVClassic_Map_Server.lua | ||||
|  | ||||
|         public void logout() | ||||
|         { | ||||
|             player.queuePacket(LogoutPacket.buildPacket(player.actorID), true, false); | ||||
|             player.playerSession.queuePacket(LogoutPacket.buildPacket(player.actorId), true, false); | ||||
|         } | ||||
|  | ||||
|         public void quitGame() | ||||
|         { | ||||
|             player.queuePacket(QuitPacket.buildPacket(player.actorID), true, false); | ||||
|             player.playerSession.queuePacket(QuitPacket.buildPacket(player.actorId), true, false); | ||||
|         } | ||||
|  | ||||
|         public void runEvent(string functionName, params object[] parameters) | ||||
|         { | ||||
|             List<LuaParam> lParams = LuaUtils.createLuaParamList(parameters); | ||||
|             player.queuePacket(RunEventFunctionPacket.buildPacket(player.actorID, player.eventCurrentOwner, player.eventCurrentStarter, functionName, lParams), true, false); | ||||
|         //    player.playerSession.queuePacket(RunEventFunctionPacket.buildPacket(player.actorId, player.eventCurrentOwner, player.eventCurrentStarter, functionName, lParams), true, false); | ||||
|         } | ||||
|  | ||||
|         public void endEvent() | ||||
|         { | ||||
|             player.queuePacket(EndEventPacket.buildPacket(player.actorID, player.eventCurrentOwner, player.eventCurrentStarter), true, false); | ||||
|           //  player.playerSession.queuePacket(EndEventPacket.buildPacket(player.actorId, player.eventCurrentOwner, player.eventCurrentStarter), true, false); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -2,6 +2,7 @@ | ||||
| using FFXIVClassic_Map_Server.Actors; | ||||
| using FFXIVClassic_Map_Server.dataobjects; | ||||
| using FFXIVClassic_Map_Server.lua; | ||||
| using MoonSharp.Interpreter; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.IO; | ||||
| @@ -190,6 +191,23 @@ namespace FFXIVClassic_Map_Server | ||||
|             return luaParams; | ||||
|         } | ||||
|  | ||||
|         public static List<LuaParam> createLuaParamList(DynValue fromScript) | ||||
|         { | ||||
|             List<LuaParam> luaParams = new List<LuaParam>(); | ||||
|  | ||||
|             if (fromScript.Type == DataType.Tuple) | ||||
|             { | ||||
|                 foreach (DynValue d in fromScript.Tuple) | ||||
|                 { | ||||
|                     addToList(d, luaParams); | ||||
|                 } | ||||
|             } | ||||
|             else | ||||
|                 addToList(fromScript, luaParams); | ||||
|  | ||||
|             return luaParams; | ||||
|         } | ||||
|  | ||||
|         public static List<LuaParam> createLuaParamList(params object[] list) | ||||
|         { | ||||
|             List<LuaParam> luaParams = new List<LuaParam>(); | ||||
| @@ -209,6 +227,37 @@ namespace FFXIVClassic_Map_Server | ||||
|             return luaParams; | ||||
|         } | ||||
|  | ||||
|         private static void addToList(DynValue d, List<LuaParam> luaParams) | ||||
|         { | ||||
|             if (d.Type == DataType.Number) | ||||
|             { | ||||
|                 luaParams.Add(new LuaParam(0x0, (uint)d.Number)); | ||||
|             } | ||||
|             else if (d.Type == DataType.Number) | ||||
|             { | ||||
|                 luaParams.Add(new LuaParam(0x0, (int)d.Number)); | ||||
|             } | ||||
|             else if (d.Type == DataType.String) | ||||
|             { | ||||
|                 luaParams.Add(new LuaParam(0x2, (string)d.String)); | ||||
|             } | ||||
|             else if (d.Type == DataType.Boolean) | ||||
|             { | ||||
|                 if (d.Boolean) | ||||
|                     luaParams.Add(new LuaParam(0x3, null)); | ||||
|                 else | ||||
|                     luaParams.Add(new LuaParam(0x4, null)); | ||||
|             } | ||||
|             else if (d.Type == DataType.Nil) | ||||
|             { | ||||
|                 luaParams.Add(new LuaParam(0x5, null)); | ||||
|             } | ||||
|             else if (d.Type == DataType.Table) | ||||
|             { | ||||
|                 //luaParams.Add(new LuaParam(0x6, ((Actor)o).actorId)); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private static void addToList(object o, List<LuaParam> luaParams) | ||||
|         { | ||||
|             if (o is uint) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user