2016-06-12 20:12:59 +01:00
|
|
|
|
using FFXIVClassic.Common;
|
2016-06-08 22:29:04 +01:00
|
|
|
|
using FFXIVClassic_Map_Server.packets;
|
2016-03-28 11:31:21 -04:00
|
|
|
|
using FFXIVClassic_Map_Server.actors.director;
|
2016-01-20 23:18:10 -05:00
|
|
|
|
using FFXIVClassic_Map_Server.Actors;
|
2016-01-01 14:03:55 -05:00
|
|
|
|
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;
|
2016-01-24 17:11:35 -05:00
|
|
|
|
using MoonSharp.Interpreter;
|
|
|
|
|
using MoonSharp.Interpreter.Interop;
|
2016-02-03 00:45:11 -05:00
|
|
|
|
using MoonSharp.Interpreter.Loaders;
|
2016-01-01 14:03:55 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace FFXIVClassic_Map_Server.lua
|
|
|
|
|
{
|
|
|
|
|
class LuaEngine
|
|
|
|
|
{
|
2016-03-06 17:55:42 -05:00
|
|
|
|
const string FILEPATH_PLAYER = "./scripts/player.lua";
|
|
|
|
|
const string FILEPATH_ZONE = "./scripts/zones/{0}/zone.lua";
|
2016-01-28 23:24:20 -05:00
|
|
|
|
const string FILEPATH_COMMANDS = "./scripts/commands/{0}.lua";
|
2016-03-28 11:31:21 -04:00
|
|
|
|
const string FILEPATH_DIRECTORS = "./scripts/directors/{0}.lua";
|
2016-01-24 17:11:35 -05:00
|
|
|
|
const string FILEPATH_NPCS = "./scripts/zones/{0}/npcs/{1}.lua";
|
2016-01-01 14:03:55 -05:00
|
|
|
|
|
|
|
|
|
public LuaEngine()
|
2016-01-24 17:11:35 -05:00
|
|
|
|
{
|
|
|
|
|
UserData.RegistrationPolicy = InteropRegistrationPolicy.Automatic;
|
2016-01-01 14:03:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
public static List<LuaParam> DoActorInstantiate(Player player, Actor tarGet)
|
2016-01-24 17:11:35 -05:00
|
|
|
|
{
|
|
|
|
|
string luaPath;
|
2016-01-01 14:03:55 -05:00
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
if (tarGet is Npc)
|
2016-01-24 17:11:35 -05:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
luaPath = String.Format(FILEPATH_NPCS, tarGet.zoneId, tarGet.GetName());
|
2016-01-24 17:11:35 -05:00
|
|
|
|
if (File.Exists(luaPath))
|
2016-04-02 17:56:01 -04:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(luaPath);
|
2016-04-02 17:56:01 -04:00
|
|
|
|
|
2016-04-07 22:34:10 -04:00
|
|
|
|
if (script == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
DynValue result = script.Call(script.Globals["init"], tarGet);
|
2016-06-14 21:29:10 +01:00
|
|
|
|
List<LuaParam> lparams = LuaUtils.CreateLuaParamList(result);
|
2016-01-24 17:11:35 -05:00
|
|
|
|
return lparams;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
SendError(player, String.Format("ERROR: Could not find script for actor {0}.", tarGet.GetName()));
|
2016-01-24 17:11:35 -05:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2016-01-25 01:10:43 -05:00
|
|
|
|
}
|
2016-01-24 17:11:35 -05:00
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
public static void DoActorOnEventStarted(Player player, Actor tarGet, EventStartPacket eventStart)
|
2016-01-01 14:03:55 -05:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
if (tarGet is Npc)
|
2016-05-29 16:03:24 -04:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
((Npc)tarGet).DoEventStart(player, eventStart);
|
2016-05-29 16:03:24 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-01 14:03:55 -05:00
|
|
|
|
string luaPath;
|
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
if (tarGet is Command)
|
2016-01-01 14:03:55 -05:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
luaPath = String.Format(FILEPATH_COMMANDS, tarGet.GetName());
|
2016-01-01 14:03:55 -05:00
|
|
|
|
}
|
2016-06-14 22:54:02 +01:00
|
|
|
|
else if (tarGet is Director)
|
2016-03-28 11:31:21 -04:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
luaPath = String.Format(FILEPATH_DIRECTORS, tarGet.GetName());
|
2016-03-28 11:31:21 -04:00
|
|
|
|
}
|
2016-02-03 00:45:11 -05:00
|
|
|
|
else
|
2016-06-14 22:54:02 +01:00
|
|
|
|
luaPath = String.Format(FILEPATH_NPCS, tarGet.zoneId, tarGet.GetName());
|
2016-02-03 00:45:11 -05:00
|
|
|
|
|
|
|
|
|
if (File.Exists(luaPath))
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(luaPath);
|
2016-02-03 00:45:11 -05:00
|
|
|
|
|
2016-04-07 22:34:10 -04:00
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-06-14 21:29:10 +01:00
|
|
|
|
//Have to Do this to combine LuaParams
|
2016-02-03 00:45:11 -05:00
|
|
|
|
List<Object> objects = new List<Object>();
|
|
|
|
|
objects.Add(player);
|
2016-06-14 22:54:02 +01:00
|
|
|
|
objects.Add(tarGet);
|
2016-03-20 21:18:46 -04:00
|
|
|
|
objects.Add(eventStart.triggerName);
|
2016-04-01 23:24:14 -04:00
|
|
|
|
|
|
|
|
|
if (eventStart.luaParams != null)
|
2016-06-14 21:29:10 +01:00
|
|
|
|
objects.AddRange(LuaUtils.CreateLuaParamObjectList(eventStart.luaParams));
|
2016-02-03 00:45:11 -05:00
|
|
|
|
|
|
|
|
|
//Run Script
|
2016-04-02 17:56:01 -04:00
|
|
|
|
if (!script.Globals.Get("onEventStarted").IsNil())
|
|
|
|
|
script.Call(script.Globals["onEventStarted"], objects.ToArray());
|
2016-02-03 00:45:11 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
SendError(player, String.Format("ERROR: Could not find script for actor {0}.", tarGet.GetName()));
|
2016-01-01 14:03:55 -05:00
|
|
|
|
}
|
2016-02-03 00:45:11 -05:00
|
|
|
|
|
2016-01-01 14:03:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
public static void DoActorOnSpawn(Player player, Npc tarGet)
|
2016-04-02 17:56:01 -04:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
string luaPath = String.Format(FILEPATH_NPCS, tarGet.zoneId, tarGet.GetName());
|
2016-04-02 17:56:01 -04:00
|
|
|
|
|
|
|
|
|
if (File.Exists(luaPath))
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(luaPath);
|
2016-04-07 22:34:10 -04:00
|
|
|
|
|
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-04-02 17:56:01 -04:00
|
|
|
|
//Run Script
|
|
|
|
|
if (!script.Globals.Get("onSpawn").IsNil())
|
2016-06-14 22:54:02 +01:00
|
|
|
|
script.Call(script.Globals["onSpawn"], player, tarGet);
|
2016-04-02 17:56:01 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
SendError(player, String.Format("ERROR: Could not find script for actor {0}.", tarGet.GetName()));
|
2016-04-02 17:56:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
public static void DoActorOnEventUpdated(Player player, Actor tarGet, EventUpdatePacket eventUpdate)
|
2016-01-01 14:03:55 -05:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
if (tarGet is Npc)
|
2016-05-29 16:03:24 -04:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
((Npc)tarGet).DoEventUpdate(player, eventUpdate);
|
2016-05-29 16:03:24 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 23:24:14 -04:00
|
|
|
|
string luaPath;
|
2016-01-01 14:03:55 -05:00
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
if (tarGet is Command)
|
|
|
|
|
luaPath = String.Format(FILEPATH_COMMANDS, tarGet.GetName());
|
|
|
|
|
else if (tarGet is Director)
|
|
|
|
|
luaPath = String.Format(FILEPATH_DIRECTORS, tarGet.GetName());
|
2016-02-03 00:45:11 -05:00
|
|
|
|
else
|
2016-06-14 22:54:02 +01:00
|
|
|
|
luaPath = String.Format(FILEPATH_NPCS, tarGet.zoneId, tarGet.GetName());
|
2016-01-28 23:24:20 -05:00
|
|
|
|
|
2016-02-03 00:45:11 -05:00
|
|
|
|
if (File.Exists(luaPath))
|
2016-01-01 14:03:55 -05:00
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(luaPath);
|
2016-01-24 17:11:35 -05:00
|
|
|
|
|
2016-04-07 22:34:10 -04:00
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-06-14 21:29:10 +01:00
|
|
|
|
//Have to Do this to combine LuaParams
|
2016-02-03 00:45:11 -05:00
|
|
|
|
List<Object> objects = new List<Object>();
|
|
|
|
|
objects.Add(player);
|
2016-06-14 22:54:02 +01:00
|
|
|
|
objects.Add(tarGet);
|
2016-04-07 22:34:10 -04:00
|
|
|
|
objects.Add(eventUpdate.val2);
|
2016-06-14 21:29:10 +01:00
|
|
|
|
objects.AddRange(LuaUtils.CreateLuaParamObjectList(eventUpdate.luaParams));
|
2016-01-24 17:11:35 -05:00
|
|
|
|
|
2016-02-03 00:45:11 -05:00
|
|
|
|
//Run Script
|
2016-04-02 17:56:01 -04:00
|
|
|
|
if (!script.Globals.Get("onEventUpdate").IsNil())
|
|
|
|
|
script.Call(script.Globals["onEventUpdate"], objects.ToArray());
|
2016-01-01 14:03:55 -05:00
|
|
|
|
}
|
2016-02-03 00:45:11 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
SendError(player, String.Format("ERROR: Could not find script for actor {0}.", tarGet.GetName()));
|
2016-02-03 00:45:11 -05:00
|
|
|
|
}
|
2016-01-01 14:03:55 -05:00
|
|
|
|
}
|
2016-03-06 17:55:42 -05:00
|
|
|
|
|
2016-06-14 21:29:10 +01:00
|
|
|
|
public static void OnZoneIn(Player player)
|
2016-03-06 17:55:42 -05:00
|
|
|
|
{
|
2016-06-14 21:29:10 +01:00
|
|
|
|
string luaPath = String.Format(FILEPATH_ZONE, player.GetZone().actorId);
|
2016-03-06 17:55:42 -05:00
|
|
|
|
|
|
|
|
|
if (File.Exists(luaPath))
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(luaPath);
|
2016-04-07 22:34:10 -04:00
|
|
|
|
|
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-03-06 17:55:42 -05:00
|
|
|
|
//Run Script
|
2016-04-02 17:56:01 -04:00
|
|
|
|
if (!script.Globals.Get("onZoneIn").IsNil())
|
|
|
|
|
script.Call(script.Globals["onZoneIn"], player);
|
2016-03-06 17:55:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 21:29:10 +01:00
|
|
|
|
public static void OnBeginLogin(Player player)
|
2016-04-14 08:30:21 -04:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(FILEPATH_PLAYER))
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(FILEPATH_PLAYER);
|
2016-04-14 08:30:21 -04:00
|
|
|
|
|
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//Run Script
|
|
|
|
|
if (!script.Globals.Get("onBeginLogin").IsNil())
|
|
|
|
|
script.Call(script.Globals["onBeginLogin"], player);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 21:29:10 +01:00
|
|
|
|
public static void OnLogin(Player player)
|
2016-03-06 17:55:42 -05:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(FILEPATH_PLAYER))
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(FILEPATH_PLAYER);
|
2016-03-06 17:55:42 -05:00
|
|
|
|
|
2016-04-07 22:34:10 -04:00
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-03-06 17:55:42 -05:00
|
|
|
|
//Run Script
|
2016-04-14 08:30:21 -04:00
|
|
|
|
if (!script.Globals.Get("onLogin").IsNil())
|
|
|
|
|
script.Call(script.Globals["onLogin"], player);
|
2016-03-06 17:55:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 22:54:02 +01:00
|
|
|
|
public static Script LoadScript(string filename)
|
2016-04-02 17:56:01 -04:00
|
|
|
|
{
|
|
|
|
|
Script script = new Script();
|
|
|
|
|
((FileSystemScriptLoader)script.Options.ScriptLoader).ModulePaths = FileSystemScriptLoader.UnpackStringPaths("./scripts/?;./scripts/?.lua");
|
2016-06-14 22:54:02 +01:00
|
|
|
|
script.Globals["GetWorldManager"] = (Func<WorldManager>)Server.GetWorldManager;
|
|
|
|
|
script.Globals["GetStaticActor"] = (Func<string, Actor>)Server.GetStaticActors;
|
|
|
|
|
script.Globals["GetWorldMaster"] = (Func<Actor>)Server.GetWorldManager().GetActor;
|
|
|
|
|
script.Globals["GetItemGamedata"] = (Func<uint, Item>)Server.GetItemGamedata;
|
2016-04-07 22:34:10 -04:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
script.DoFile(filename);
|
|
|
|
|
}
|
|
|
|
|
catch(SyntaxErrorException e)
|
|
|
|
|
{
|
2016-06-14 05:09:30 +01:00
|
|
|
|
Program.Log.Error("LUAERROR: {0}.", e.DecoratedMessage);
|
2016-04-07 22:34:10 -04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-04-02 17:56:01 -04:00
|
|
|
|
return script;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-29 16:03:24 -04:00
|
|
|
|
public static void SendError(Player player, string message)
|
2016-04-07 22:34:10 -04:00
|
|
|
|
{
|
2016-06-14 21:29:10 +01:00
|
|
|
|
List<SubPacket> SendError = new List<SubPacket>();
|
|
|
|
|
SendError.Add(EndEventPacket.BuildPacket(player.actorId, player.currentEventOwner, player.currentEventName));
|
|
|
|
|
player.SendMessage(SendMessagePacket.MESSAGE_TYPE_SYSTEM_ERROR, "", message);
|
|
|
|
|
player.playerSession.QueuePacket(BasePacket.CreatePacket(SendError, true, false));
|
2016-04-07 22:34:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 08:30:21 -04:00
|
|
|
|
|
2016-06-14 21:29:10 +01:00
|
|
|
|
internal static void DoDirectorOnTalked(Director director, Player player, Npc npc)
|
2016-04-14 08:30:21 -04:00
|
|
|
|
{
|
2016-06-14 21:29:10 +01:00
|
|
|
|
string luaPath = String.Format(FILEPATH_DIRECTORS, director.GetName());
|
2016-04-14 08:30:21 -04:00
|
|
|
|
|
|
|
|
|
if (File.Exists(luaPath))
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(luaPath);
|
2016-04-14 08:30:21 -04:00
|
|
|
|
|
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//Run Script
|
|
|
|
|
if (!script.Globals.Get("onTalked").IsNil())
|
|
|
|
|
script.Call(script.Globals["onTalked"], player, npc);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-14 21:29:10 +01:00
|
|
|
|
SendError(player, String.Format("ERROR: Could not find script for director {0}.", director.GetName()));
|
2016-04-14 08:30:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 21:29:10 +01:00
|
|
|
|
internal static void DoDirectorOnCommand(Director director, Player player, Command command)
|
2016-04-14 08:30:21 -04:00
|
|
|
|
{
|
2016-06-14 21:29:10 +01:00
|
|
|
|
string luaPath = String.Format(FILEPATH_DIRECTORS, director.GetName());
|
2016-04-14 08:30:21 -04:00
|
|
|
|
|
|
|
|
|
if (File.Exists(luaPath))
|
|
|
|
|
{
|
2016-06-14 22:54:02 +01:00
|
|
|
|
Script script = LoadScript(luaPath);
|
2016-04-14 08:30:21 -04:00
|
|
|
|
|
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//Run Script
|
|
|
|
|
if (!script.Globals.Get("onCommand").IsNil())
|
|
|
|
|
script.Call(script.Globals["onCommand"], player, command);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-14 21:29:10 +01:00
|
|
|
|
SendError(player, String.Format("ERROR: Could not find script for director {0}.", director.GetName()));
|
2016-04-14 08:30:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-01 14:03:55 -05:00
|
|
|
|
}
|
|
|
|
|
}
|