From 4f2f13061f6e643617ccafbb6b43f76d1cdda088 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Wed, 18 Dec 2024 01:19:29 -0300 Subject: [PATCH 01/16] feature/luajit --- .../Player/PlayerSayCommand.cs | 20 +- .../Contracts/Scripts/Entities.cs | 263 +++ .../Contracts/Scripts/ILuaManager.cs | 7 + .../Contracts/Scripts/ITalkAction.cs | 10 + .../NeoServer.Scripts.LuaJIT/ArgManager.cs | 21 + .../ConfigDefinitions.cs | 318 ++++ .../ConfigFunctions.cs | 64 + .../NeoServer.Scripts.LuaJIT/ConfigManager.cs | 545 ++++++ .../CreatureFunctions.cs | 99 ++ .../DataLuaJit/core.lua | 14 + .../DataLuaJit/global.lua | 13 + .../DataLuaJit/libs/functions/game.lua | 9 + .../DataLuaJit/libs/functions/load.lua | 3 + .../libs/functions/revscriptsys.lua | 344 ++++ .../DataLuaJit/libs/libs.lua | 2 + .../scripts/actions/Tools/shovel.lua | 9 + .../globalevents/Customs/save_interval.lua | 37 + .../scripts/globalevents/example_one.lua | 7 + .../scripts/globalevents/example_two.lua | 8 + .../scripts/reward_chest/boss_think.lua | 12 + .../scripts/talkactions/god/benchmark.lua | 42 + .../scripts/talkactions/god/reload.lua | 86 + .../scripts/talkactions/player/help.lua | 48 + .../scripts/talkactions/player/test.lua | 34 + .../GlobalFunctions.cs | 22 + .../Interfaces/IConfigManager.cs | 34 + .../Interfaces/ICreatureFunctions.cs | 8 + .../Interfaces/ILuaEnvironment.cs | 40 + .../Interfaces/ILuaManager.cs | 6 + .../Interfaces/ILuaScriptInterface.cs | 38 + .../Interfaces/IScripts.cs | 14 + .../Interfaces/ITalkActions.cs | 17 + .../NeoServer.Scripts.LuaJIT/Logger.cs | 36 + .../LoggerFunctions.cs | 75 + .../LuaDefinitions.cs | 251 +++ .../LuaEnvironment.cs | 319 ++++ .../LuaFunctionsLoader.cs | 1554 +++++++++++++++++ .../NeoServer.Scripts.LuaJIT/LuaManager.cs | 123 ++ .../LuaScriptInterface.cs | 325 ++++ .../NeoServer.Scripts.LuaJIT.csproj | 70 + .../PlayerFunctions.cs | 157 ++ .../NeoServer.Scripts.LuaJIT/Script.cs | 97 + .../ScriptEnvironment.cs | 255 +++ .../NeoServer.Scripts.LuaJIT/Scripts.cs | 202 +++ .../Structs/UserDataStruct.cs | 8 + .../NeoServer.Scripts.LuaJIT/TalkAction.cs | 83 + .../TalkActionFunctions.cs | 220 +++ .../NeoServer.Scripts.LuaJIT/TalkActions.cs | 130 ++ .../UtilsDefinitions.cs | 778 +++++++++ .../NeoServer.Scripts.LuaJIT/config.lua | 37 + src/NeoServer.sln | 38 +- src/Standalone/IoC/Modules/LuaInjection.cs | 12 + .../NeoServer.Server.Standalone.csproj | 63 +- src/Standalone/Program.cs | 3 + 54 files changed, 6984 insertions(+), 46 deletions(-) create mode 100644 src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/Entities.cs create mode 100644 src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs create mode 100644 src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ITalkAction.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/ArgManager.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/ConfigDefinitions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/ConfigFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/CreatureFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/core.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/global.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/load.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/revscriptsys.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/libs.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/actions/Tools/shovel.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/reload.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/test.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/GlobalFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ICreatureFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaScriptInterface.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IScripts.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LoggerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaManager.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/PlayerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Structs/UserDataStruct.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/TalkActionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/config.lua diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs index 68d995634..a0c786540 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs @@ -1,4 +1,6 @@ using System.Linq; +using Microsoft.VisualBasic; +using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.DataStores; @@ -14,11 +16,13 @@ public class PlayerSayCommand : ICommand { private readonly IChatChannelStore _chatChannelStore; private readonly IGameServer _game; + private readonly ILuaManager _luaManager; - public PlayerSayCommand(IGameServer game, IChatChannelStore chatChannelStore) + public PlayerSayCommand(IGameServer game, IChatChannelStore chatChannelStore, ILuaManager luaManager) { _game = game; _chatChannelStore = chatChannelStore; + _luaManager = luaManager; } public void Execute(IPlayer player, IConnection connection, PlayerSayPacket playerSayPacket) @@ -29,6 +33,20 @@ public void Execute(IPlayer player, IConnection connection, PlayerSayPacket play var message = playerSayPacket.Message?.Trim(); + //estava chamando aqui lua manager + var messageData = message.Split(" "); + + var talkAction = _luaManager.GetTalkAction(messageData[0]); + var parameter = ""; + + if (messageData.Count() > 1) + parameter = messageData[1]; + + if (talkAction != null) + { + talkAction.ExecuteSay(player, messageData[0], parameter, playerSayPacket.TalkType); + } + if (player.CastSpell(message)) return; switch (playerSayPacket.TalkType) diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/Entities.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/Entities.cs new file mode 100644 index 000000000..30dfde637 --- /dev/null +++ b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/Entities.cs @@ -0,0 +1,263 @@ +using System; +using System.Collections.Generic; + +namespace NeoServer.Application.Common.Contracts.Scripts +{ + public enum DirectionType : byte + { + DIRECTION_NORTH = 0, + DIRECTION_EAST = 1, + DIRECTION_SOUTH = 2, + DIRECTION_WEST = 3, + + DIRECTION_DIAGONAL_MASK = 4, + DIRECTION_SOUTHWEST = DIRECTION_DIAGONAL_MASK | 0, + DIRECTION_SOUTHEAST = DIRECTION_DIAGONAL_MASK | 1, + DIRECTION_NORTHWEST = DIRECTION_DIAGONAL_MASK | 2, + DIRECTION_NORTHEAST = DIRECTION_DIAGONAL_MASK | 3, + + DIRECTION_LAST = DIRECTION_NORTHEAST, + DIRECTION_NONE = 8 + } + + public struct PositionLua + { + public ushort x; + public ushort y; + public byte z; + + public PositionLua(ushort initX, ushort initY, byte initZ) + { + x = initX; + y = initY; + z = initZ; + } + + public static bool AreInRange(PositionLua p1, PositionLua p2, int deltax, int deltay) + { + return GetDistanceX(p1, p2) <= deltax && GetDistanceY(p1, p2) <= deltay; + } + + public static bool AreInRange(PositionLua p1, PositionLua p2, int deltax, int deltay, int deltaz) + { + return GetDistanceX(p1, p2) <= deltax && GetDistanceY(p1, p2) <= deltay && GetDistanceZ(p1, p2) <= deltaz; + } + + public static int GetOffsetX(PositionLua p1, PositionLua p2) + { + return p1.x - p2.x; + } + + public static int GetOffsetY(PositionLua p1, PositionLua p2) + { + return p1.y - p2.y; + } + + public static int GetOffsetZ(PositionLua p1, PositionLua p2) + { + return p1.z - p2.z; + } + + public static int GetDistanceX(PositionLua p1, PositionLua p2) + { + return Math.Abs(GetOffsetX(p1, p2)); + } + + public static int GetDistanceY(PositionLua p1, PositionLua p2) + { + return Math.Abs(GetOffsetY(p1, p2)); + } + + public static int GetDistanceZ(PositionLua p1, PositionLua p2) + { + return Math.Abs(GetOffsetZ(p1, p2)); + } + + public static int GetDiagonalDistance(PositionLua p1, PositionLua p2) + { + return Math.Max(GetDistanceX(p1, p2), GetDistanceY(p1, p2)); + } + + public static double GetEuclideanDistance(PositionLua p1, PositionLua p2) + { + int dx = GetDistanceX(p1, p2); + int dy = GetDistanceY(p1, p2); + return Math.Sqrt(dx * dx + dy * dy); + } + + public override string ToString() + { + return $"( {GetX()}, {GetY()}, {GetZ()} )"; + } + + public int GetX() + { + return x; + } + + public int GetY() + { + return y; + } + + public int GetZ() + { + return z; + } + } + + public static class PositionHasher + { + public static int GetHashCode(PositionLua p) + { + return (int)p.x | (p.y << 16) | (p.z << 32); + } + } + + public static class PositionExtensions + { + public static bool Equals(PositionLua p1, PositionLua p2) + { + return p1.x == p2.x && p1.y == p2.y && p1.z == p2.z; + } + + public static PositionLua Add(PositionLua p1, PositionLua p2) + { + return new PositionLua((ushort)(p1.x + p2.x), (ushort)(p1.y + p2.y), (byte)(p1.z + p2.z)); + } + + public static PositionLua Subtract(PositionLua p1, PositionLua p2) + { + return new PositionLua((ushort)(p1.x - p2.x), (ushort)(p1.y - p2.y), (byte)(p1.z - p2.z)); + } + } + + public static class DirectionExtensions + { + private static readonly Dictionary DirectionStrings = new Dictionary + { + { DirectionType.DIRECTION_NORTH, "North" }, + { DirectionType.DIRECTION_EAST, "East" }, + { DirectionType.DIRECTION_WEST, "West" }, + { DirectionType.DIRECTION_SOUTH, "South" }, + { DirectionType.DIRECTION_SOUTHWEST, "South-West" }, + { DirectionType.DIRECTION_SOUTHEAST, "South-East" }, + { DirectionType.DIRECTION_NORTHWEST, "North-West" }, + { DirectionType.DIRECTION_NORTHEAST, "North-East" } + }; + + public static string ToDirectionString(this DirectionType dir) + { + if (DirectionStrings.TryGetValue(dir, out var result)) + { + return result; + } + + return dir.ToString(); + } + } + + public class ThingLua() + { + #region Members + + public PositionLua Position { get; set; } + + #endregion + + #region Properties + + #endregion + + #region Public Methods + + #endregion + } + + public class CreatureLua : ThingLua + { + #region Constructors + + public CreatureLua(string name, PositionLua position) + { + SetId(); + Name = name; Position = position; + } + + #endregion + + #region Properties + + public int Id { get; set; } + public string Name { get; set; } + + #endregion + + #region Public Methods + + public virtual void SetId() { } + + public virtual CreatureLua GetPlayer() => null; + public virtual CreatureLua GetNpc() => null; + public virtual CreatureLua GetMonster() => null; + + #endregion + } + + public class PlayerLua(int guid, string name, int level, PositionLua position) : CreatureLua(name, position) + { + #region Members + + #endregion + + #region Properties + + public int PlayerFirstID => 0x10000000; + public int PlayerLastID => 0x50000000; + + public int Guid { get; set; } = guid; + + public int Level { get; set; } = level; + + #endregion + + #region Public Methods + + public override void SetId() + { + base.SetId(); + + // guid = player id from database + if (Id == 0 && Guid != 0) + { + Id = PlayerFirstID + guid; + if (Id == int.MaxValue) + { + //Logger.GetInstance().Error($"[SetId] Player {Name} has max 'id' value of uint32_t"); + } + } + } + + public override PlayerLua GetPlayer() => this; + + #endregion + } + + public class ItemLua() : ThingLua + { + #region Members + + #endregion + + #region Properties + + public ushort Id { get; set; } + public string Name { get; set; } + + #endregion + + #region Public Methods + + #endregion + } +} diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs new file mode 100644 index 000000000..6fee8ae97 --- /dev/null +++ b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs @@ -0,0 +1,7 @@ +namespace NeoServer.Application.Common.Contracts.Scripts; + +public interface ILuaManager +{ + void Start(); + ITalkAction GetTalkAction(string name); +} diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ITalkAction.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ITalkAction.cs new file mode 100644 index 000000000..d88778ed4 --- /dev/null +++ b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ITalkAction.cs @@ -0,0 +1,10 @@ +using NeoServer.Game.Common.Chats; +using NeoServer.Game.Common.Contracts.Creatures; + +namespace NeoServer.Application.Common.Contracts.Scripts +{ + public interface ITalkAction + { + bool ExecuteSay(IPlayer player, string words, string param, SpeechType type); + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ArgManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ArgManager.cs new file mode 100644 index 000000000..a5da05fc6 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ArgManager.cs @@ -0,0 +1,21 @@ +namespace NeoServer.Scripts.LuaJIT; + +public class ArgManager +{ + #region Members + + #endregion + + #region Instance + + private static ArgManager _instance = null; + public static ArgManager GetInstance() => _instance == null ? _instance = new ArgManager() : _instance; + + #endregion + + #region Properties + + public string ExePath { get; set; } + + #endregion +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigDefinitions.cs new file mode 100644 index 000000000..d8a2b22d7 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigDefinitions.cs @@ -0,0 +1,318 @@ +namespace NeoServer.Scripts.LuaJIT; + +// Enum +public enum BooleanConfigType : int +{ + ALLOW_CHANGEOUTFIT, + ONE_PLAYER_ON_ACCOUNT, + AIMBOT_HOTKEY_ENABLED, + REMOVE_RUNE_CHARGES, + EXPERIENCE_FROM_PLAYERS, + FREE_PREMIUM, + REPLACE_KICK_ON_LOGIN, + BIND_ONLY_GLOBAL_ADDRESS, + OPTIMIZE_DATABASE, + MARKET_PREMIUM, + EMOTE_SPELLS, + STAMINA_SYSTEM, + WARN_UNSAFE_SCRIPTS, + CONVERT_UNSAFE_SCRIPTS, + CLASSIC_ATTACK_SPEED, + SCRIPTS_CONSOLE_LOGS, + REMOVE_WEAPON_AMMO, + REMOVE_WEAPON_CHARGES, + REMOVE_POTION_CHARGES, + GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE, + GLOBAL_SERVER_SAVE_CLEAN_MAP, + GLOBAL_SERVER_SAVE_CLOSE, + GLOBAL_SERVER_SAVE_SHUTDOWN, + FORCE_MONSTERTYPE_LOAD, + HOUSE_OWNED_BY_ACCOUNT, + CLEAN_PROTECTION_ZONES, + ALLOW_BLOCK_SPAWN, + HOUSE_PURSHASED_SHOW_PRICE, + ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS, + WEATHER_RAIN, + WEATHER_THUNDER, + TOGGLE_FREE_QUEST, + ONLY_PREMIUM_ACCOUNT, + TOGGLE_MAP_CUSTOM, + ALL_CONSOLE_LOG, + STAMINA_TRAINER, + STAMINA_PZ, + PUSH_WHEN_ATTACKING, + SORT_LOOT_BY_CHANCE, + TOGGLE_SAVE_INTERVAL, + TOGGLE_SAVE_INTERVAL_CLEAN_MAP, + PREY_ENABLED, + PREY_FREE_THIRD_SLOT, + TASK_HUNTING_ENABLED, + TASK_HUNTING_FREE_THIRD_SLOT, + STASH_MOVING, + TOGGLE_IMBUEMENT_SHRINE_STORAGE, + TOGGLE_IMBUEMENT_NON_AGGRESSIVE_FIGHT_ONLY, + AUTOLOOT, + AUTOBANK, + RATE_USE_STAGES, + INVENTORY_GLOW, + TELEPORT_SUMMONS, + TOGGLE_DOWNLOAD_MAP, + USE_ANY_DATAPACK_FOLDER, + ALLOW_RELOAD, + BOOSTED_BOSS_SLOT, + XP_DISPLAY_MODE, + TOGGLE_GOLD_POUCH_ALLOW_ANYTHING, + TOGGLE_GOLD_POUCH_QUICKLOOT_ONLY, + TOGGLE_SERVER_IS_RETRO, + TOGGLE_TRAVELS_FREE, + TELEPORT_PLAYER_TO_VOCATION_ROOM, + OLD_PROTOCOL, + TOGGLE_HAZARDSYSTEM, + LOYALTY_ENABLED, + PARTY_AUTO_SHARE_EXPERIENCE, + PARTY_SHARE_LOOT_BOOSTS, + RESET_SESSIONS_ON_STARTUP, + TOGGLE_WHEELSYSTEM, + TOGGLE_ATTACK_SPEED_ONFIST, + VIP_SYSTEM_ENABLED, + VIP_AUTOLOOT_VIP_ONLY, + VIP_STAY_ONLINE, + REWARD_CHEST_COLLECT_ENABLED, + TOGGLE_MOUNT_IN_PZ, + TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART, + TOGGLE_RECEIVE_REWARD, + TOGGLE_MAINTAIN_MODE, + + LAST_BOOLEAN_CONFIG +}; + +public enum StringConfigType +{ + MAP_NAME, + MAP_DOWNLOAD_URL, + MAP_AUTHOR, + HOUSE_RENT_PERIOD, + SERVER_NAME, + SERVER_MOTD, + OWNER_NAME, + OWNER_EMAIL, + URL, + LOCATION, + IP, + WORLD_TYPE, + MYSQL_HOST, + MYSQL_USER, + MYSQL_PASS, + MYSQL_DB, + MYSQL_SOCK, + AUTH_TYPE, + DEFAULT_PRIORITY, + STORE_IMAGES_URL, + MAP_CUSTOM_NAME, + MAP_CUSTOM_AUTHOR, + DISCORD_WEBHOOK_URL, + SAVE_INTERVAL_TYPE, + GLOBAL_SERVER_SAVE_TIME, + DATA_DIRECTORY, + CORE_DIRECTORY, + FORGE_FIENDISH_INTERVAL_TYPE, + FORGE_FIENDISH_INTERVAL_TIME, + TIBIADROME_CONCOCTION_TICK_TYPE, + M_CONST, + MAINTAIN_MODE_MESSAGE, + + LAST_STRING_CONFIG +}; + +public enum IntegerConfigType +{ + SQL_PORT, + MAX_PLAYERS, + PZ_LOCKED, + DEFAULT_DESPAWNRANGE, + DEFAULT_DESPAWNRADIUS, + RATE_EXPERIENCE, + RATE_SKILL, + RATE_LOOT, + RATE_MAGIC, + RATE_SPAWN, + RATE_KILLING_IN_THE_NAME_OF_POINTS, + HOUSE_PRICE_PER_SQM, + HOUSE_BUY_LEVEL, + MAX_MESSAGEBUFFER, + ACTIONS_DELAY_INTERVAL, + EX_ACTIONS_DELAY_INTERVAL, + KICK_AFTER_MINUTES, + PROTECTION_LEVEL, + DEATH_LOSE_PERCENT, + STATUSQUERY_TIMEOUT, + FRAG_TIME, + WHITE_SKULL_TIME, + GAME_PORT, + LOGIN_PORT, + STATUS_PORT, + STAIRHOP_DELAY, + MAX_CONTAINER, + MAX_CONTAINER_ITEM, + MARKET_OFFER_DURATION, + DEPOT_BOXES, + FREE_DEPOT_LIMIT, + PREMIUM_DEPOT_LIMIT, + CHECK_EXPIRED_MARKET_OFFERS_EACH_MINUTES, + MAX_MARKET_OFFERS_AT_A_TIME_PER_PLAYER, + EXP_FROM_PLAYERS_LEVEL_RANGE, + MAX_PACKETS_PER_SECOND, + COMPRESSION_LEVEL, + STORE_COIN_PACKET, + DAY_KILLS_TO_RED, + WEEK_KILLS_TO_RED, + MONTH_KILLS_TO_RED, + RED_SKULL_DURATION, + BLACK_SKULL_DURATION, + ORANGE_SKULL_DURATION, + GLOBAL_SERVER_SAVE_NOTIFY_DURATION, + PUSH_DELAY, + PUSH_DISTANCE_DELAY, + STASH_ITEMS, + PARTY_LIST_MAX_DISTANCE, + STAMINA_ORANGE_DELAY, + STAMINA_GREEN_DELAY, + STAMINA_TRAINER_DELAY, + STAMINA_PZ_GAIN, + STAMINA_TRAINER_GAIN, + SAVE_INTERVAL_TIME, + PREY_REROLL_PRICE_LEVEL, + PREY_SELECTION_LIST_PRICE, + PREY_BONUS_TIME, + PREY_BONUS_REROLL_PRICE, + PREY_FREE_REROLL_TIME, + TASK_HUNTING_LIMIT_EXHAUST, + TASK_HUNTING_REROLL_PRICE_LEVEL, + TASK_HUNTING_SELECTION_LIST_PRICE, + TASK_HUNTING_BONUS_REROLL_PRICE, + TASK_HUNTING_FREE_REROLL_TIME, + MAX_ALLOWED_ON_A_DUMMY, + FREE_QUEST_STAGE, + DEPOTCHEST, + CRITICALCHANCE, + ADVENTURERSBLESSING_LEVEL, + FORGE_MAX_ITEM_TIER, + FORGE_COST_ONE_SLIVER, + FORGE_SLIVER_AMOUNT, + FORGE_CORE_COST, + FORGE_MAX_DUST, + FORGE_FUSION_DUST_COST, + FORGE_TRANSFER_DUST_COST, + FORGE_BASE_SUCCESS_RATE, + FORGE_BONUS_SUCCESS_RATE, + FORGE_TIER_LOSS_REDUCTION, + FORGE_AMOUNT_MULTIPLIER, + FORGE_MIN_SLIVERS, + FORGE_MAX_SLIVERS, + FORGE_INFLUENCED_CREATURES_LIMIT, + FORGE_FIENDISH_CREATURES_LIMIT, + BESTIARY_KILL_MULTIPLIER, + BOSSTIARY_KILL_MULTIPLIER, + BOOSTED_BOSS_LOOT_BONUS, + BOOSTED_BOSS_KILL_BONUS, + FAMILIAR_TIME, + BUY_AOL_COMMAND_FEE, + BUY_BLESS_COMMAND_FEE, + HAZARD_CRITICAL_INTERVAL, + HAZARD_CRITICAL_CHANCE, + HAZARD_CRITICAL_MULTIPLIER, + HAZARD_DAMAGE_MULTIPLIER, + HAZARD_DODGE_MULTIPLIER, + HAZARD_PODS_DROP_MULTIPLIER, + HAZARD_PODS_TIME_TO_DAMAGE, + HAZARD_EXP_BONUS_MULTIPLIER, + HAZARD_LOOT_BONUS_MULTIPLIER, + HAZARD_PODS_DAMAGE, + HAZARD_PODS_TIME_TO_SPAWN, + HAZARD_SPAWN_PLUNDER_MULTIPLIER, + LOW_LEVEL_BONUS_EXP, + LOYALTY_POINTS_PER_CREATION_DAY, + LOYALTY_POINTS_PER_PREMIUM_DAY_SPENT, + LOYALTY_POINTS_PER_PREMIUM_DAY_PURCHASED, + WHEEL_POINTS_PER_LEVEL, + MULTIPLIER_ATTACKONFIST, + MAX_SPEED_ATTACKONFIST, + TIBIADROME_CONCOCTION_COOLDOWN, + TIBIADROME_CONCOCTION_DURATION, + T_CONST, + PARALLELISM, + BOSS_DEFAULT_TIME_TO_FIGHT_AGAIN, + BOSS_DEFAULT_TIME_TO_DEFEAT, + + VIP_BONUS_EXP, + VIP_BONUS_LOOT, + VIP_BONUS_SKILL, + VIP_FAMILIAR_TIME_COOLDOWN_REDUCTION, + + REWARD_CHEST_MAX_COLLECT_ITEMS, + DISCORD_WEBHOOK_DELAY_MS, + + PVP_MAX_LEVEL_DIFFERENCE, + + LAST_INTEGER_CONFIG +}; + +public enum FloatingConfigType +{ + BESTIARY_RATE_CHARM_SHOP_PRICE, + + RATE_HEALTH_REGEN, + RATE_HEALTH_REGEN_SPEED, + RATE_MANA_REGEN, + RATE_MANA_REGEN_SPEED, + RATE_SOUL_REGEN, + RATE_SOUL_REGEN_SPEED, + + RATE_SPELL_COOLDOWN, + RATE_ATTACK_SPEED, + RATE_OFFLINE_TRAINING_SPEED, + RATE_EXERCISE_TRAINING_SPEED, + + RATE_MONSTER_HEALTH, + RATE_MONSTER_ATTACK, + RATE_MONSTER_DEFENSE, + RATE_BOSS_HEALTH, + RATE_BOSS_ATTACK, + RATE_BOSS_DEFENSE, + + RATE_NPC_HEALTH, + RATE_NPC_ATTACK, + RATE_NPC_DEFENSE, + LOYALTY_BONUS_PERCENTAGE_MULTIPLIER, + PARTY_SHARE_LOOT_BOOSTS_DIMINISHING_FACTOR, + + PVP_RATE_DAMAGE_TAKEN_PER_LEVEL, + PVP_RATE_DAMAGE_REDUCTION_PER_LEVEL, + + HOUSE_PRICE_RENT_MULTIPLIER, + HOUSE_RENT_RATE, + + LAST_FLOATING_CONFIG +}; + +public enum ReloadType : byte +{ + RELOAD_TYPE_NONE, + RELOAD_TYPE_ALL, + RELOAD_TYPE_CHAT, + RELOAD_TYPE_CONFIG, + RELOAD_TYPE_EVENTS, + RELOAD_TYPE_CORE, + RELOAD_TYPE_IMBUEMENTS, + RELOAD_TYPE_ITEMS, + RELOAD_TYPE_MODULES, + RELOAD_TYPE_MONSTERS, + RELOAD_TYPE_MOUNTS, + RELOAD_TYPE_NPCS, + RELOAD_TYPE_RAIDS, + RELOAD_TYPE_SCRIPTS, + RELOAD_TYPE_GROUPS, + + // Every is last + RELOAD_TYPE_LAST +}; \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigFunctions.cs new file mode 100644 index 000000000..ccdd55ccc --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigFunctions.cs @@ -0,0 +1,64 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; +public class ConfigFunctions : LuaScriptInterface +{ + public ConfigFunctions() : base(nameof(ConfigFunctions)) + { + } + + public static void Init(LuaState L) + { + RegisterTable(L, "configManager"); + RegisterMethod(L, "configManager", "getString", LuaConfigManagerGetString); + RegisterMethod(L, "configManager", "getNumber", LuaConfigManagerGetNumber); + RegisterMethod(L, "configManager", "getBoolean", LuaConfigManagerGetBoolean); + RegisterMethod(L, "configManager", "getFloat", LuaConfigManagerGetFloat); + + RegisterTable(L, "configKeys"); + + //RegisterVariable(L, "configKeys", "ALLOW_CHANGEOUTFIT", BooleanConfig.ALLOW_CHANGEOUTFIT); + + foreach (var item in Enum.GetValues()) + RegisterVariable(L, "configKeys", item.ToString(), item); + + foreach (var item in Enum.GetValues()) + RegisterVariable(L, "configKeys", item.ToString(), item); + + foreach (var item in Enum.GetValues()) + RegisterVariable(L, "configKeys", item.ToString(), item); + + foreach (var item in Enum.GetValues()) + RegisterVariable(L, "configKeys", item.ToString(), item); + + RegisterVariable(L, "configKeys", "BASE_DIRECTORY", AppContext.BaseDirectory); + } + + private static int LuaConfigManagerGetString(LuaState L) + { + // configManager:getString() + PushString(L, ConfigManager.GetInstance().GetString(GetNumber(L, -1))); + return 1; + } + + private static int LuaConfigManagerGetNumber(LuaState L) + { + // configManager:getNumber() + Lua.PushNumber(L, ConfigManager.GetInstance().GetNumber(GetNumber(L, -1))); + return 1; + } + + private static int LuaConfigManagerGetBoolean(LuaState L) + { + // configManager:getBoolean() + PushBoolean(L, ConfigManager.GetInstance().GetBoolean(GetNumber(L, -1))); + return 1; + } + + private static int LuaConfigManagerGetFloat(LuaState L) + { + // configManager:getFloat() + Lua.PushNumber(L, ConfigManager.GetInstance().GetFloat(GetNumber(L, -1))); + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs new file mode 100644 index 000000000..a8b2ba46a --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs @@ -0,0 +1,545 @@ +using LuaNET; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT; + +public class ConfigManager : IConfigManager +{ + #region Instance + + private static ConfigManager _instance; + public static ConfigManager GetInstance() => _instance == null ? _instance = new ConfigManager() : _instance; + + #endregion + + #region Members + + private readonly string[] stringConfig = new string[(int)StringConfigType.LAST_STRING_CONFIG]; + private readonly int[] integerConfig = new int[(int)IntegerConfigType.LAST_INTEGER_CONFIG]; + private readonly bool[] booleanConfig = new bool[(int)BooleanConfigType.LAST_BOOLEAN_CONFIG]; + private readonly float[] floatingConfig = new float[(int)FloatingConfigType.LAST_FLOATING_CONFIG]; + + private bool loaded = false; + + #endregion + + #region Injection + + /// + /// A reference to the logger in use. + /// + private readonly ILogger _logger; + + #endregion + + public ConfigManager(ILogger logger) + { + _instance = this; + + _logger = logger.ForContext(); + } + + public bool Load(string file) + { + configFileLua = file.Split("\\").LastOrDefault(); + var L = Lua.NewState(); + if (L.pointer == 0) + { + throw new System.IO.IOException("Failed to allocate memory"); + } + + Lua.OpenLibs(L); + + if (Lua.DoFile(L, file) > 0) + { + _logger.Error("[ConfigManager::load] - {0}", Lua.ToString(L, -1)); + Lua.Close(L); + return false; + } + +//#if !DEBUG_LOG +// g_logger().setLevel(getGlobalString(L, "logLevel", "info")); +//#endif + + // Parse config + // Info that must be loaded one time (unless we reset the modules involved) + if (!loaded) + { + //booleanConfig[(int)BooleanConfig.BIND_ONLY_GLOBAL_ADDRESS] = GetGlobalBoolean(L, "bindOnlyGlobalAddress", false); + //bool[OPTIMIZE_DATABASE] = getGlobalBoolean(L, "startupDatabaseOptimization", true); + //bool[TOGGLE_MAP_CUSTOM] = getGlobalBoolean(L, "toggleMapCustom", true); + //bool[TOGGLE_MAINTAIN_MODE] = getGlobalBoolean(L, "toggleMaintainMode", false); + //string[MAINTAIN_MODE_MESSAGE] = getGlobalString(L, "maintainModeMessage", ""); + + //string[IP] = getGlobalString(L, "ip", "127.0.0.1"); + //string[MAP_NAME] = getGlobalString(L, "mapName", "canary"); + //string[MAP_DOWNLOAD_URL] = getGlobalString(L, "mapDownloadUrl", ""); + //string[MAP_AUTHOR] = getGlobalString(L, "mapAuthor", "Eduardo Dantas"); + + //string[MAP_CUSTOM_NAME] = getGlobalString(L, "mapCustomName", ""); + //string[MAP_CUSTOM_AUTHOR] = getGlobalString(L, "mapCustomAuthor", "OTServBR"); + + //string[HOUSE_RENT_PERIOD] = getGlobalString(L, "houseRentPeriod", "never"); + //float[HOUSE_PRICE_RENT_MULTIPLIER] = getGlobalFloat(L, "housePriceRentMultiplier", 1.0); + //float[HOUSE_RENT_RATE] = getGlobalFloat(L, "houseRentRate", 1.0); + //string[MYSQL_HOST] = getGlobalString(L, "mysqlHost", "127.0.0.1"); + //string[MYSQL_USER] = getGlobalString(L, "mysqlUser", "root"); + //string[MYSQL_PASS] = getGlobalString(L, "mysqlPass", ""); + //string[MYSQL_DB] = getGlobalString(L, "mysqlDatabase", "canary"); + //string[MYSQL_SOCK] = getGlobalString(L, "mysqlSock", ""); + + //string[AUTH_TYPE] = getGlobalString(L, "authType", "password"); + //bool[RESET_SESSIONS_ON_STARTUP] = getGlobalBoolean(L, "resetSessionsOnStartup", false); + + //integer[SQL_PORT] = getGlobalNumber(L, "mysqlPort", 3306); + + integerConfig[(int)IntegerConfigType.GAME_PORT] = GetGlobalNumber(L, "gameProtocolPort", 7172); + integerConfig[(int)IntegerConfigType.LOGIN_PORT] = GetGlobalNumber(L, "loginProtocolPort", 7171); + integerConfig[(int)IntegerConfigType.STATUS_PORT] = GetGlobalNumber(L, "statusProtocolPort", 7171); + + //integer[MARKET_OFFER_DURATION] = getGlobalNumber(L, "marketOfferDuration", 30 * 24 * 60 * 60); + + //integer[FREE_DEPOT_LIMIT] = getGlobalNumber(L, "freeDepotLimit", 2000); + //integer[PREMIUM_DEPOT_LIMIT] = getGlobalNumber(L, "premiumDepotLimit", 8000); + //integer[DEPOT_BOXES] = getGlobalNumber(L, "depotBoxes", 20); + //integer[STASH_ITEMS] = getGlobalNumber(L, "stashItemCount", 5000); + + //bool[OLD_PROTOCOL] = getGlobalBoolean(L, "allowOldProtocol", true); + } + + //bool[ALLOW_CHANGEOUTFIT] = getGlobalBoolean(L, "allowChangeOutfit", true); + //bool[ONE_PLAYER_ON_ACCOUNT] = getGlobalBoolean(L, "onePlayerOnlinePerAccount", true); + //bool[AIMBOT_HOTKEY_ENABLED] = getGlobalBoolean(L, "hotkeyAimbotEnabled", true); + //bool[REMOVE_RUNE_CHARGES] = getGlobalBoolean(L, "removeChargesFromRunes", true); + //bool[EXPERIENCE_FROM_PLAYERS] = getGlobalBoolean(L, "experienceByKillingPlayers", false); + //bool[FREE_PREMIUM] = getGlobalBoolean(L, "freePremium", false); + //bool[REPLACE_KICK_ON_LOGIN] = getGlobalBoolean(L, "replaceKickOnLogin", true); + //bool[MARKET_PREMIUM] = getGlobalBoolean(L, "premiumToCreateMarketOffer", true); + //bool[EMOTE_SPELLS] = getGlobalBoolean(L, "emoteSpells", false); + //bool[STAMINA_SYSTEM] = getGlobalBoolean(L, "staminaSystem", true); + //bool[WARN_UNSAFE_SCRIPTS] = getGlobalBoolean(L, "warnUnsafeScripts", true); + //bool[CONVERT_UNSAFE_SCRIPTS] = getGlobalBoolean(L, "convertUnsafeScripts", true); + //bool[CLASSIC_ATTACK_SPEED] = getGlobalBoolean(L, "classicAttackSpeed", false); + //bool[TOGGLE_ATTACK_SPEED_ONFIST] = getGlobalBoolean(L, "toggleAttackSpeedOnFist", false); + //integer[MULTIPLIER_ATTACKONFIST] = getGlobalNumber(L, "multiplierSpeedOnFist", 5); + //integer[MAX_SPEED_ATTACKONFIST] = getGlobalNumber(L, "maxSpeedOnFist", 500); + booleanConfig[(int)BooleanConfigType.SCRIPTS_CONSOLE_LOGS] = GetGlobalBoolean(L, "showScriptsLogInConsole", true); + //bool[STASH_MOVING] = getGlobalBoolean(L, "stashMoving", false); + //bool[ALLOW_BLOCK_SPAWN] = getGlobalBoolean(L, "allowBlockSpawn", true); + //bool[REMOVE_WEAPON_AMMO] = getGlobalBoolean(L, "removeWeaponAmmunition", true); + //bool[REMOVE_WEAPON_CHARGES] = getGlobalBoolean(L, "removeWeaponCharges", true); + //bool[REMOVE_POTION_CHARGES] = getGlobalBoolean(L, "removeChargesFromPotions", true); + //bool[GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE] = getGlobalBoolean(L, "globalServerSaveNotifyMessage", true); + //bool[GLOBAL_SERVER_SAVE_CLEAN_MAP] = getGlobalBoolean(L, "globalServerSaveCleanMap", false); + //bool[GLOBAL_SERVER_SAVE_CLOSE] = getGlobalBoolean(L, "globalServerSaveClose", false); + //bool[FORCE_MONSTERTYPE_LOAD] = getGlobalBoolean(L, "forceMonsterTypesOnLoad", true); + //bool[HOUSE_OWNED_BY_ACCOUNT] = getGlobalBoolean(L, "houseOwnedByAccount", false); + //bool[CLEAN_PROTECTION_ZONES] = getGlobalBoolean(L, "cleanProtectionZones", false); + //bool[GLOBAL_SERVER_SAVE_SHUTDOWN] = getGlobalBoolean(L, "globalServerSaveShutdown", true); + //bool[PUSH_WHEN_ATTACKING] = getGlobalBoolean(L, "pushWhenAttacking", false); + + //bool[WEATHER_RAIN] = getGlobalBoolean(L, "weatherRain", false); + //bool[WEATHER_THUNDER] = getGlobalBoolean(L, "thunderEffect", false); + //bool[ALL_CONSOLE_LOG] = getGlobalBoolean(L, "allConsoleLog", false); + //bool[TOGGLE_FREE_QUEST] = getGlobalBoolean(L, "toggleFreeQuest", true); + //bool[AUTOLOOT] = getGlobalBoolean(L, "autoLoot", false); + //bool[AUTOBANK] = getGlobalBoolean(L, "autoBank", false); + //bool[STAMINA_TRAINER] = getGlobalBoolean(L, "staminaTrainer", false); + //bool[STAMINA_PZ] = getGlobalBoolean(L, "staminaPz", false); + //bool[SORT_LOOT_BY_CHANCE] = getGlobalBoolean(L, "sortLootByChance", false); + booleanConfig[(int)BooleanConfigType.TOGGLE_SAVE_INTERVAL] = GetGlobalBoolean(L, "toggleSaveInterval", false); + booleanConfig[(int)BooleanConfigType.TOGGLE_SAVE_INTERVAL_CLEAN_MAP] = GetGlobalBoolean(L, "toggleSaveIntervalCleanMap", false); + //bool[TELEPORT_SUMMONS] = getGlobalBoolean(L, "teleportSummons", false); + booleanConfig[(int)BooleanConfigType.ALLOW_RELOAD] = GetGlobalBoolean(L, "allowReload", false); + + //bool[ONLY_PREMIUM_ACCOUNT] = getGlobalBoolean(L, "onlyPremiumAccount", false); + //bool[RATE_USE_STAGES] = getGlobalBoolean(L, "rateUseStages", false); + //bool[TOGGLE_IMBUEMENT_SHRINE_STORAGE] = getGlobalBoolean(L, "toggleImbuementShrineStorage", true); + //bool[TOGGLE_IMBUEMENT_NON_AGGRESSIVE_FIGHT_ONLY] = getGlobalBoolean(L, "toggleImbuementNonAggressiveFightOnly", false); + + //bool[TOGGLE_DOWNLOAD_MAP] = getGlobalBoolean(L, "toggleDownloadMap", false); + //bool[USE_ANY_DATAPACK_FOLDER] = getGlobalBoolean(L, "useAnyDatapackFolder", false); + //bool[INVENTORY_GLOW] = getGlobalBoolean(L, "inventoryGlowOnFiveBless", false); + //bool[XP_DISPLAY_MODE] = getGlobalBoolean(L, "experienceDisplayRates", true); + + //string[DEFAULT_PRIORITY] = getGlobalString(L, "defaultPriority", "high"); + //string[SERVER_NAME] = getGlobalString(L, "serverName", ""); + //string[SERVER_MOTD] = getGlobalString(L, "serverMotd", ""); + //string[OWNER_NAME] = getGlobalString(L, "ownerName", ""); + //string[OWNER_EMAIL] = getGlobalString(L, "ownerEmail", ""); + //string[URL] = getGlobalString(L, "url", ""); + //string[LOCATION] = getGlobalString(L, "location", ""); + //string[WORLD_TYPE] = getGlobalString(L, "worldType", "pvp"); + //string[STORE_IMAGES_URL] = getGlobalString(L, "coinImagesURL", ""); + //string[DISCORD_WEBHOOK_URL] = getGlobalString(L, "discordWebhookURL", ""); + stringConfig[(int)StringConfigType.SAVE_INTERVAL_TYPE] = GetGlobalString(L, "saveIntervalType", ""); + //string[GLOBAL_SERVER_SAVE_TIME] = getGlobalString(L, "globalServerSaveTime", "06:00"); + //string[DATA_DIRECTORY] = getGlobalString(L, "dataPackDirectory", "data-otservbr-global"); + stringConfig[(int)StringConfigType.CORE_DIRECTORY] = GetGlobalString(L, "coreDirectory", "data"); + + //string[FORGE_FIENDISH_INTERVAL_TYPE] = getGlobalString(L, "forgeFiendishIntervalType", "hour"); + //string[FORGE_FIENDISH_INTERVAL_TIME] = getGlobalString(L, "forgeFiendishIntervalTime", "1"); + + //integer[MAX_PLAYERS] = getGlobalNumber(L, "maxPlayers"); + //integer[PZ_LOCKED] = getGlobalNumber(L, "pzLocked", 60000); + //integer[DEFAULT_DESPAWNRANGE] = getGlobalNumber(L, "deSpawnRange", 2); + //integer[DEFAULT_DESPAWNRADIUS] = getGlobalNumber(L, "deSpawnRadius", 50); + //integer[RATE_EXPERIENCE] = getGlobalNumber(L, "rateExp", 1); + //integer[RATE_SKILL] = getGlobalNumber(L, "rateSkill", 1); + //integer[RATE_LOOT] = getGlobalNumber(L, "rateLoot", 1); + //integer[RATE_MAGIC] = getGlobalNumber(L, "rateMagic", 1); + //integer[RATE_SPAWN] = getGlobalNumber(L, "rateSpawn", 1); + //integer[RATE_KILLING_IN_THE_NAME_OF_POINTS] = getGlobalNumber(L, "rateKillingInTheNameOfPoints", 1); + + //integer[HOUSE_PRICE_PER_SQM] = getGlobalNumber(L, "housePriceEachSQM", 1000); + //integer[HOUSE_BUY_LEVEL] = getGlobalNumber(L, "houseBuyLevel", 0); + //bool[HOUSE_PURSHASED_SHOW_PRICE] = getGlobalBoolean(L, "housePurchasedShowPrice", false); + //bool[ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS] = getGlobalBoolean(L, "onlyInvitedCanMoveHouseItems", true); + + //integer[ACTIONS_DELAY_INTERVAL] = getGlobalNumber(L, "timeBetweenActions", 200); + //integer[EX_ACTIONS_DELAY_INTERVAL] = getGlobalNumber(L, "timeBetweenExActions", 1000); + //integer[MAX_MESSAGEBUFFER] = getGlobalNumber(L, "maxMessageBuffer", 4); + //integer[KICK_AFTER_MINUTES] = getGlobalNumber(L, "kickIdlePlayerAfterMinutes", 15); + //integer[PROTECTION_LEVEL] = getGlobalNumber(L, "protectionLevel", 1); + //integer[DEATH_LOSE_PERCENT] = getGlobalNumber(L, "deathLosePercent", -1); + //integer[STATUSQUERY_TIMEOUT] = getGlobalNumber(L, "statusTimeout", 5000); + //integer[FRAG_TIME] = getGlobalNumber(L, "timeToDecreaseFrags", 24 * 60 * 60 * 1000); + //integer[WHITE_SKULL_TIME] = getGlobalNumber(L, "whiteSkullTime", 15 * 60 * 1000); + //integer[STAIRHOP_DELAY] = getGlobalNumber(L, "stairJumpExhaustion", 2000); + //integer[MAX_CONTAINER] = getGlobalNumber(L, "maxContainer", 500); + //integer[MAX_CONTAINER_ITEM] = getGlobalNumber(L, "maxItem", 5000); + //integer[EXP_FROM_PLAYERS_LEVEL_RANGE] = getGlobalNumber(L, "expFromPlayersLevelRange", 75); + //integer[CHECK_EXPIRED_MARKET_OFFERS_EACH_MINUTES] = getGlobalNumber(L, "checkExpiredMarketOffersEachMinutes", 60); + //integer[MAX_MARKET_OFFERS_AT_A_TIME_PER_PLAYER] = getGlobalNumber(L, "maxMarketOffersAtATimePerPlayer", 100); + //integer[MAX_PACKETS_PER_SECOND] = getGlobalNumber(L, "maxPacketsPerSecond", 25); + //integer[COMPRESSION_LEVEL] = getGlobalNumber(L, "packetCompressionLevel", 6); + //integer[STORE_COIN_PACKET] = getGlobalNumber(L, "coinPacketSize", 25); + //integer[DAY_KILLS_TO_RED] = getGlobalNumber(L, "dayKillsToRedSkull", 3); + //integer[WEEK_KILLS_TO_RED] = getGlobalNumber(L, "weekKillsToRedSkull", 5); + //integer[MONTH_KILLS_TO_RED] = getGlobalNumber(L, "monthKillsToRedSkull", 10); + //integer[RED_SKULL_DURATION] = getGlobalNumber(L, "redSkullDuration", 30); + //integer[BLACK_SKULL_DURATION] = getGlobalNumber(L, "blackSkullDuration", 45); + //integer[ORANGE_SKULL_DURATION] = getGlobalNumber(L, "orangeSkullDuration", 7); + //integer[GLOBAL_SERVER_SAVE_NOTIFY_DURATION] = getGlobalNumber(L, "globalServerSaveNotifyDuration", 5); + + //integer[PARTY_LIST_MAX_DISTANCE] = getGlobalNumber(L, "partyListMaxDistance", 0); + + //integer[PUSH_DELAY] = getGlobalNumber(L, "pushDelay", 1000); + //integer[PUSH_DISTANCE_DELAY] = getGlobalNumber(L, "pushDistanceDelay", 1500); + + //integer[STAMINA_ORANGE_DELAY] = getGlobalNumber(L, "staminaOrangeDelay", 1); + //integer[STAMINA_GREEN_DELAY] = getGlobalNumber(L, "staminaGreenDelay", 5); + //integer[STAMINA_PZ_GAIN] = getGlobalNumber(L, "staminaPzGain", 1); + //integer[STAMINA_TRAINER_DELAY] = getGlobalNumber(L, "staminaTrainerDelay", 5); + //integer[STAMINA_TRAINER_GAIN] = getGlobalNumber(L, "staminaTrainerGain", 1); + integerConfig[(int)IntegerConfigType.SAVE_INTERVAL_TIME] = GetGlobalNumber(L, "saveIntervalTime", 1); + //integer[MAX_ALLOWED_ON_A_DUMMY] = getGlobalNumber(L, "maxAllowedOnADummy", 1); + //integer[FREE_QUEST_STAGE] = getGlobalNumber(L, "freeQuestStage", 1); + //integer[DEPOTCHEST] = getGlobalNumber(L, "depotChest", 4); + //integer[CRITICALCHANCE] = getGlobalNumber(L, "criticalChance", 10); + + //integer[ADVENTURERSBLESSING_LEVEL] = getGlobalNumber(L, "adventurersBlessingLevel", 21); + //integer[FORGE_MAX_ITEM_TIER] = getGlobalNumber(L, "forgeMaxItemTier", 10); + //integer[FORGE_COST_ONE_SLIVER] = getGlobalNumber(L, "forgeCostOneSliver", 20); + //integer[FORGE_SLIVER_AMOUNT] = getGlobalNumber(L, "forgeSliverAmount", 3); + //integer[FORGE_CORE_COST] = getGlobalNumber(L, "forgeCoreCost", 50); + //integer[FORGE_MAX_DUST] = getGlobalNumber(L, "forgeMaxDust", 225); + //integer[FORGE_FUSION_DUST_COST] = getGlobalNumber(L, "forgeFusionCost", 100); + //integer[FORGE_TRANSFER_DUST_COST] = getGlobalNumber(L, "forgeTransferCost", 100); + //integer[FORGE_BASE_SUCCESS_RATE] = getGlobalNumber(L, "forgeBaseSuccessRate", 50); + //integer[FORGE_BONUS_SUCCESS_RATE] = getGlobalNumber(L, "forgeBonusSuccessRate", 15); + //integer[FORGE_TIER_LOSS_REDUCTION] = getGlobalNumber(L, "forgeTierLossReduction", 50); + //integer[FORGE_AMOUNT_MULTIPLIER] = getGlobalNumber(L, "forgeAmountMultiplier", 3); + //integer[FORGE_MIN_SLIVERS] = getGlobalNumber(L, "forgeMinSlivers", 3); + //integer[FORGE_MAX_SLIVERS] = getGlobalNumber(L, "forgeMaxSlivers", 7); + //integer[FORGE_INFLUENCED_CREATURES_LIMIT] = getGlobalNumber(L, "forgeInfluencedLimit", 300); + //integer[FORGE_FIENDISH_CREATURES_LIMIT] = getGlobalNumber(L, "forgeFiendishLimit", 3); + //integer[DISCORD_WEBHOOK_DELAY_MS] = getGlobalNumber(L, "discordWebhookDelayMs", Webhook::DEFAULT_DELAY_MS); + + //float[BESTIARY_RATE_CHARM_SHOP_PRICE] = getGlobalFloat(L, "bestiaryRateCharmShopPrice", 1.0); + //float[RATE_HEALTH_REGEN] = getGlobalFloat(L, "rateHealthRegen", 1.0); + //float[RATE_HEALTH_REGEN_SPEED] = getGlobalFloat(L, "rateHealthRegenSpeed", 1.0); + //float[RATE_MANA_REGEN] = getGlobalFloat(L, "rateManaRegen", 1.0); + //float[RATE_MANA_REGEN_SPEED] = getGlobalFloat(L, "rateManaRegenSpeed", 1.0); + //float[RATE_SOUL_REGEN] = getGlobalFloat(L, "rateSoulRegen", 1.0); + //float[RATE_SOUL_REGEN_SPEED] = getGlobalFloat(L, "rateSoulRegenSpeed", 1.0); + //float[RATE_SPELL_COOLDOWN] = getGlobalFloat(L, "rateSpellCooldown", 1.0); + //float[RATE_ATTACK_SPEED] = getGlobalFloat(L, "rateAttackSpeed", 1.0); + //float[RATE_OFFLINE_TRAINING_SPEED] = getGlobalFloat(L, "rateOfflineTrainingSpeed", 1.0); + //float[RATE_EXERCISE_TRAINING_SPEED] = getGlobalFloat(L, "rateExerciseTrainingSpeed", 1.0); + + //float[RATE_MONSTER_HEALTH] = getGlobalFloat(L, "rateMonsterHealth", 1.0); + //float[RATE_MONSTER_ATTACK] = getGlobalFloat(L, "rateMonsterAttack", 1.0); + //float[RATE_MONSTER_DEFENSE] = getGlobalFloat(L, "rateMonsterDefense", 1.0); + //float[RATE_BOSS_HEALTH] = getGlobalFloat(L, "rateBossHealth", 1.0); + //float[RATE_BOSS_ATTACK] = getGlobalFloat(L, "rateBossAttack", 1.0); + //float[RATE_BOSS_DEFENSE] = getGlobalFloat(L, "rateBossDefense", 1.0); + //integer[BOSS_DEFAULT_TIME_TO_FIGHT_AGAIN] = getGlobalNumber(L, "bossDefaultTimeToFightAgain", 20 * 60 * 60); + //integer[BOSS_DEFAULT_TIME_TO_DEFEAT] = getGlobalNumber(L, "bossDefaultTimeToDefeat", 20 * 60); + + //float[RATE_NPC_HEALTH] = getGlobalFloat(L, "rateNpcHealth", 1.0); + //float[RATE_NPC_ATTACK] = getGlobalFloat(L, "rateNpcAttack", 1.0); + //float[RATE_NPC_DEFENSE] = getGlobalFloat(L, "rateNpcDefense", 1.0); + + //bool[PREY_ENABLED] = getGlobalBoolean(L, "preySystemEnabled", true); + //bool[PREY_FREE_THIRD_SLOT] = getGlobalBoolean(L, "preyFreeThirdSlot", false); + //integer[PREY_REROLL_PRICE_LEVEL] = getGlobalNumber(L, "preyRerollPricePerLevel", 200); + //integer[PREY_SELECTION_LIST_PRICE] = getGlobalNumber(L, "preySelectListPrice", 5); + //integer[PREY_BONUS_TIME] = getGlobalNumber(L, "preyBonusTime", 7200); + //integer[PREY_BONUS_REROLL_PRICE] = getGlobalNumber(L, "preyBonusRerollPrice", 1); + //integer[PREY_FREE_REROLL_TIME] = getGlobalNumber(L, "preyFreeRerollTime", 72000); + + //bool[TASK_HUNTING_ENABLED] = getGlobalBoolean(L, "taskHuntingSystemEnabled", true); + //bool[TASK_HUNTING_FREE_THIRD_SLOT] = getGlobalBoolean(L, "taskHuntingFreeThirdSlot", false); + //integer[TASK_HUNTING_LIMIT_EXHAUST] = getGlobalNumber(L, "taskHuntingLimitedTasksExhaust", 72000); + //integer[TASK_HUNTING_REROLL_PRICE_LEVEL] = getGlobalNumber(L, "taskHuntingRerollPricePerLevel", 200); + //integer[TASK_HUNTING_SELECTION_LIST_PRICE] = getGlobalNumber(L, "taskHuntingSelectListPrice", 5); + //integer[TASK_HUNTING_BONUS_REROLL_PRICE] = getGlobalNumber(L, "taskHuntingBonusRerollPrice", 1); + //integer[TASK_HUNTING_FREE_REROLL_TIME] = getGlobalNumber(L, "taskHuntingFreeRerollTime", 72000); + + //integer[BESTIARY_KILL_MULTIPLIER] = getGlobalNumber(L, "bestiaryKillMultiplier", 1); + //integer[BOSSTIARY_KILL_MULTIPLIER] = getGlobalNumber(L, "bosstiaryKillMultiplier", 1); + //bool[BOOSTED_BOSS_SLOT] = getGlobalBoolean(L, "boostedBossSlot", true); + //integer[BOOSTED_BOSS_LOOT_BONUS] = getGlobalNumber(L, "boostedBossLootBonus", 250); + //integer[BOOSTED_BOSS_KILL_BONUS] = getGlobalNumber(L, "boostedBossKillBonus", 3); + + //integer[FAMILIAR_TIME] = getGlobalNumber(L, "familiarTime", 30); + + //bool[TOGGLE_GOLD_POUCH_ALLOW_ANYTHING] = getGlobalBoolean(L, "toggleGoldPouchAllowAnything", false); + //bool[TOGGLE_GOLD_POUCH_QUICKLOOT_ONLY] = getGlobalBoolean(L, "toggleGoldPouchQuickLootOnly", false); + //bool[TOGGLE_SERVER_IS_RETRO] = getGlobalBoolean(L, "toggleServerIsRetroPVP", false); + //bool[TOGGLE_TRAVELS_FREE] = getGlobalBoolean(L, "toggleTravelsFree", false); + //integer[BUY_AOL_COMMAND_FEE] = getGlobalNumber(L, "buyAolCommandFee", 0); + //integer[BUY_BLESS_COMMAND_FEE] = getGlobalNumber(L, "buyBlessCommandFee", 0); + //bool[TELEPORT_PLAYER_TO_VOCATION_ROOM] = getGlobalBoolean(L, "teleportPlayerToVocationRoom", true); + + //bool[TOGGLE_HAZARDSYSTEM] = getGlobalBoolean(L, "toogleHazardSystem", true); + //integer[HAZARD_CRITICAL_INTERVAL] = getGlobalNumber(L, "hazardCriticalInterval", 2000); + //integer[HAZARD_CRITICAL_CHANCE] = getGlobalNumber(L, "hazardCriticalChance", 750); + //integer[HAZARD_CRITICAL_MULTIPLIER] = getGlobalNumber(L, "hazardCriticalMultiplier", 25); + //integer[HAZARD_DAMAGE_MULTIPLIER] = getGlobalNumber(L, "hazardDamageMultiplier", 200); + //integer[HAZARD_DODGE_MULTIPLIER] = getGlobalNumber(L, "hazardDodgeMultiplier", 85); + //integer[HAZARD_PODS_DROP_MULTIPLIER] = getGlobalNumber(L, "hazardPodsDropMultiplier", 87); + //integer[HAZARD_PODS_TIME_TO_DAMAGE] = getGlobalNumber(L, "hazardPodsTimeToDamage", 2000); + //integer[HAZARD_PODS_TIME_TO_SPAWN] = getGlobalNumber(L, "hazardPodsTimeToSpawn", 4000); + //integer[HAZARD_EXP_BONUS_MULTIPLIER] = getGlobalNumber(L, "hazardExpBonusMultiplier", 2); + //integer[HAZARD_LOOT_BONUS_MULTIPLIER] = getGlobalNumber(L, "hazardLootBonusMultiplier", 2); + //integer[HAZARD_PODS_DAMAGE] = getGlobalNumber(L, "hazardPodsDamage", 5); + //integer[HAZARD_SPAWN_PLUNDER_MULTIPLIER] = getGlobalNumber(L, "hazardSpawnPlunderMultiplier", 25); + //integer[LOW_LEVEL_BONUS_EXP] = getGlobalNumber(L, "lowLevelBonusExp", 50); + + //bool[LOYALTY_ENABLED] = getGlobalBoolean(L, "loyaltyEnabled", true); + //integer[LOYALTY_POINTS_PER_CREATION_DAY] = getGlobalNumber(L, "loyaltyPointsPerCreationDay", 1); + //integer[LOYALTY_POINTS_PER_PREMIUM_DAY_SPENT] = getGlobalNumber(L, "loyaltyPointsPerPremiumDaySpent", 0); + //integer[LOYALTY_POINTS_PER_PREMIUM_DAY_PURCHASED] = getGlobalNumber(L, "loyaltyPointsPerPremiumDayPurchased", 0); + //float[LOYALTY_BONUS_PERCENTAGE_MULTIPLIER] = getGlobalFloat(L, "loyaltyBonusPercentageMultiplier", 1.0); + + //bool[TOGGLE_WHEELSYSTEM] = getGlobalBoolean(L, "wheelSystemEnabled", true); + //integer[WHEEL_POINTS_PER_LEVEL] = getGlobalNumber(L, "wheelPointsPerLevel", 1); + + //bool[PARTY_AUTO_SHARE_EXPERIENCE] = getGlobalBoolean(L, "partyAutoShareExperience", true); + //bool[PARTY_SHARE_LOOT_BOOSTS] = getGlobalBoolean(L, "partyShareLootBoosts", true); + //float[PARTY_SHARE_LOOT_BOOSTS_DIMINISHING_FACTOR] = getGlobalFloat(L, "partyShareLootBoostsDimishingFactor", 0.7f); + //integer[TIBIADROME_CONCOCTION_COOLDOWN] = getGlobalNumber(L, "tibiadromeConcoctionCooldown", 24 * 60 * 60); + //integer[TIBIADROME_CONCOCTION_DURATION] = getGlobalNumber(L, "tibiadromeConcoctionDuration", 1 * 60 * 60); + //string[TIBIADROME_CONCOCTION_TICK_TYPE] = getGlobalString(L, "tibiadromeConcoctionTickType", "online"); + + //string[M_CONST] = getGlobalString(L, "memoryConst", "1<<16"); + //integer[T_CONST] = getGlobalNumber(L, "temporaryConst", 2); + //integer[PARALLELISM] = getGlobalNumber(L, "parallelism", 2); + + //// Vip System + //bool[VIP_SYSTEM_ENABLED] = getGlobalBoolean(L, "vipSystemEnabled", false); + //integer[VIP_BONUS_EXP] = getGlobalNumber(L, "vipBonusExp", 0); + //integer[VIP_BONUS_LOOT] = getGlobalNumber(L, "vipBonusLoot", 0); + //integer[VIP_BONUS_SKILL] = getGlobalNumber(L, "vipBonusSkill", 0); + //bool[VIP_AUTOLOOT_VIP_ONLY] = getGlobalBoolean(L, "vipAutoLootVipOnly", false); + //bool[VIP_STAY_ONLINE] = getGlobalBoolean(L, "vipStayOnline", false); + //integer[VIP_FAMILIAR_TIME_COOLDOWN_REDUCTION] = getGlobalNumber(L, "vipFamiliarTimeCooldownReduction", 0); + + //bool[REWARD_CHEST_COLLECT_ENABLED] = getGlobalBoolean(L, "rewardChestCollectEnabled", true); + //integer[REWARD_CHEST_MAX_COLLECT_ITEMS] = getGlobalNumber(L, "rewardChestMaxCollectItems", 200); + + //// PVP System + //float[PVP_RATE_DAMAGE_TAKEN_PER_LEVEL] = getGlobalFloat(L, "pvpRateDamageTakenPerLevel", 0.0); + //float[PVP_RATE_DAMAGE_REDUCTION_PER_LEVEL] = getGlobalFloat(L, "pvpRateDamageReductionPerLevel", 0.0); + //integer[PVP_MAX_LEVEL_DIFFERENCE] = getGlobalNumber(L, "pvpMaxLevelDifference", 0); + + //bool[TOGGLE_MOUNT_IN_PZ] = getGlobalBoolean(L, "toggleMountInProtectionZone", false); + + //bool[TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART] = getGlobalBoolean(L, "togglehouseTransferOnRestart", false); + + //bool[TOGGLE_RECEIVE_REWARD] = getGlobalBoolean(L, "toggleReceiveReward", false); + + loaded = true; + Lua.Close(L); + return true; + } + + //public bool Reload() + //{ + // //bool result = Load(); + // //if (TransformToSHA1(GetString(StringConfig.ServerMotd)) != Game.Instance.GetMotdHash()) + // //{ + // // Game.Instance.IncrementMotdNum(); + // //} + // //return result; + //} + + public string GetString(StringConfigType what) + { + if (what >= StringConfigType.LAST_STRING_CONFIG) + { + _logger.Warning($"[ConfigManager.GetString] - Accessing invalid index: {what}"); + return string.Empty; + } + return stringConfig[(int)what]; + } + + public int GetNumber(IntegerConfigType what) + { + if (what >= IntegerConfigType.LAST_INTEGER_CONFIG) + { + _logger.Warning($"[ConfigManager.GetNumber] - Accessing invalid index: {what}"); + return 0; + } + return integerConfig[(int)what]; + } + + public short GetShortNumber(IntegerConfigType what) + { + if (what >= IntegerConfigType.LAST_INTEGER_CONFIG) + { + _logger.Warning($"[ConfigManager.GetShortNumber] - Accessing invalid index: {what}"); + return 0; + } + return (short)integerConfig[(int)what]; + } + + public ushort GetUShortNumber(IntegerConfigType what) => (ushort)integerConfig[(int)what]; + + public bool GetBoolean(BooleanConfigType what) + { + if (what >= BooleanConfigType.LAST_BOOLEAN_CONFIG) + { + _logger.Warning($"[ConfigManager.GetBoolean] - Accessing invalid index: {what}"); + return false; + } + return booleanConfig[(int)what]; + } + + public float GetFloat(FloatingConfigType what) + { + if (what >= FloatingConfigType.LAST_FLOATING_CONFIG) + { + _logger.Warning($"[ConfigManager.GetFloat] - Accessing invalid index: {what}"); + return 0; + } + return floatingConfig[(int)what]; + } + + public string SetConfigFileLua(string what) + { + return configFileLua = what; + } + + public string GetConfigFileLua() + { + return configFileLua; + } + + private string configFileLua = ""; + + private ConfigManager() + { + // Implementar lógica do construtor aqui + } + + private static readonly string DummyStr = string.Empty; + + //private string TransformToSHA1(string input) + //{ + // using (SHA1 sha1 = SHA1.Create()) + // { + // byte[] hashBytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); + // StringBuilder sb = new StringBuilder(); + // foreach (byte b in hashBytes) + // { + // sb.Append(b.ToString("x2")); + // } + // return sb.ToString(); + // } + //} + + public string GetGlobalString(LuaState L, string identifier, string defaultValue) + { + Lua.GetGlobal(L, identifier); + if (Lua.IsString(L, -1)) + { + return defaultValue; + } + + ulong len = 0; + var str = Lua.ToLString(L, -1, ref len); + Lua.Pop(L, 1); + return str; + } + + public int GetGlobalNumber(LuaState L, string identifier, int defaultValue = 0) + { + Lua.GetGlobal(L, identifier); + if (Lua.IsNumber(L, -1)) + { + return defaultValue; + } + + int val = (int)Lua.ToNumber(L, -1); + Lua.Pop(L, 1); + return val; + } + + public bool GetGlobalBoolean(LuaState L, string identifier, bool defaultValue) + { + Lua.GetGlobal(L, identifier); + if (Lua.IsBoolean(L, -1)) + { + if (Lua.IsString(L, -1)) + { + return defaultValue; + } + + ulong len = 0; + var str = Lua.ToLString(L, -1, ref len); + Lua.Pop(L, 1); + return BooleanString(str); + } + + var val = Lua.ToBoolean(L, -1); + Lua.Pop(L, 1); + return val; + } + + public float GetGlobalFloat(LuaState L, string identifier, float defaultValue = 0.0f) + { + Lua.GetGlobal(L, identifier); + if (Lua.IsNumber(L, -1)) + { + return defaultValue; + } + + float val = (float)Lua.ToNumber(L, -1); + Lua.Pop(L, 1); + return val; + } + + private bool BooleanString(string str) + { + if (string.IsNullOrEmpty(str)) + { + return false; + } + + var ch = str.ToLower(); + return ch != "f" && ch != "n" && ch != "0"; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/CreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/CreatureFunctions.cs new file mode 100644 index 000000000..1ff41dfeb --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/CreatureFunctions.cs @@ -0,0 +1,99 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Creatures.Models.Bases; +using NeoServer.Server.Common.Contracts; + +namespace NeoServer.Scripts.LuaJIT; + +public class CreatureFunctions : LuaScriptInterface, ICreatureFunctions +{ + private static IGameCreatureManager _gameCreatureManager; + + public CreatureFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(TalkActionFunctions)) + { + _gameCreatureManager = gameCreatureManager; + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Creature", "", LuaCreatureCreate); + RegisterMetaMethod(L, "Creature", "__eq", LuaUserdataCompare); + RegisterMethod(L, "Creature", "getId", LuaGetId); + RegisterMethod(L, "Creature", "getName", LuaGetName); + } + + private static int LuaCreatureCreate(LuaState L) + { + // Creature(id or name or userdata) + + ICreature creature = null; + if (IsNumber(L, 2)) + { + //creature = Game.GetInstance().GetCreatureByID(GetNumber(L, 2)); + var id = GetNumber(L, 2); + creature = _gameCreatureManager.GetCreatures().FirstOrDefault(c => c.CreatureId == id); + } + else if (IsString(L, 2)) + { + var name = GetString(L, 2); + creature = (Creature)_gameCreatureManager.GetCreatures().FirstOrDefault(c => c.Name.Equals(name)); + } + else if (IsUserdata(L, 2)) + { + //LuaData_t type = getUserdataType(L, 2); + //if (type != LuaData_t::Player && type != LuaData_t::Monster && type != LuaData_t::Npc) + //{ + // lua_pushnil(L); + // return 1; + //} + //creature = getUserdataShared(L, 2); + } + else + { + creature = null; + } + + if (creature != null) + { + PushUserdata(L, creature); + SetCreatureMetatable(L, -1, creature); + } + else + { + Lua.PushNil(L); + } + return 1; + } + + private static int LuaGetId(LuaState L) + { + // creature:getId() + var creature = GetUserdata(L, 1); + + if (creature != null) + { + Lua.PushNumber(L, creature.CreatureId); + } + else + { + Lua.PushNil(L); + } + return 1; + } + + private static int LuaGetName(LuaState L) + { + // player:getName() + var creature = GetUserdata(L, 1); + + if (creature == null) + { + ReportError(nameof(LuaGetName), GetErrorDesc(ErrorCodeType.LUA_ERROR_PLAYER_NOT_FOUND)); + PushBoolean(L, false); + return 0; + } + + PushString(L, creature.Name); + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/core.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/core.lua new file mode 100644 index 000000000..44c5194b9 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/core.lua @@ -0,0 +1,14 @@ +print('print from core.lua') +print(configKeys.BASE_DIRECTORY) + +logger.debug('Starting lua.') +print(os.getenv('LOCAL_LUA_DEBUGGER_VSCODE')) +if os.getenv('LOCAL_LUA_DEBUGGER_VSCODE') == '1' then + require('lldebugger').start() + logger.debug('Started LUA debugger.') +end + +CORE_DIRECTORY = configKeys.BASE_DIRECTORY .. 'DataLuaJit' + +dofile(CORE_DIRECTORY .. '/global.lua') +dofile(CORE_DIRECTORY .. '/libs/libs.lua') \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/global.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/global.lua new file mode 100644 index 000000000..35421a81e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/global.lua @@ -0,0 +1,13 @@ +-- for use of: data\scripts\globalevents\customs\save_interval.lua +print('print from global.lua') + +SAVE_INTERVAL_TYPE = configManager.getString(configKeys.SAVE_INTERVAL_TYPE) +SAVE_INTERVAL_CONFIG_TIME = configManager.getNumber(configKeys.SAVE_INTERVAL_TIME) +SAVE_INTERVAL_TIME = 0 +if SAVE_INTERVAL_TYPE == "second" then + SAVE_INTERVAL_TIME = 1000 +elseif SAVE_INTERVAL_TYPE == "minute" then + SAVE_INTERVAL_TIME = 60 * 1000 +elseif SAVE_INTERVAL_TYPE == "hour" then + SAVE_INTERVAL_TIME = 60 * 60 * 1000 +end \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua new file mode 100644 index 000000000..489740320 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua @@ -0,0 +1,9 @@ +-- function Game.broadcastMessage(message, messageType) +-- if not messageType then +-- messageType = MESSAGE_GAME_HIGHLIGHT +-- end + +-- for _, player in ipairs(Game.getPlayers()) do +-- player:sendTextMessage(messageType, message) +-- end +-- end \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/load.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/load.lua new file mode 100644 index 000000000..331210007 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/load.lua @@ -0,0 +1,3 @@ +-- Load core functions +--dofile(CORE_DIRECTORY .. "/libs/functions/game.lua") +dofile(CORE_DIRECTORY .. "/libs/functions/revscriptsys.lua") \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/revscriptsys.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/revscriptsys.lua new file mode 100644 index 000000000..67d0c374e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/revscriptsys.lua @@ -0,0 +1,344 @@ +-- Create functions revscriptsys +function createFunctions(class) + local exclude = { [2] = { "is" }, [3] = { "get", "set", "add", "can" }, [4] = { "need" } } + local temp = {} + for name, func in pairs(class) do + local add = true + for strLen, strTable in pairs(exclude) do + if table.contains(strTable, name:sub(1, strLen)) then + add = false + end + end + if add then + local str = name:sub(1, 1):upper() .. name:sub(2) + local getFunc = function(self) + return func(self) + end + local setFunc = function(self, ...) + return func(self, ...) + end + local get = "get" .. str + local set = "set" .. str + if not (rawget(class, get) and rawget(class, set)) then + table.insert(temp, { set, setFunc, get, getFunc }) + end + end + end + for _, func in ipairs(temp) do + rawset(class, func[1], func[2]) + rawset(class, func[3], func[4]) + end +end + +-- Creature index +do + local function CreatureIndex(self, key) + local methods = getmetatable(self) + if key == "uid" then + return methods.getId(self) + elseif key == "type" then + local creatureType = 0 + if methods.isPlayer(self) then + creatureType = THING_TYPE_PLAYER + elseif methods.isMonster(self) then + creatureType = THING_TYPE_MONSTER + elseif methods.isNpc(self) then + creatureType = THING_TYPE_NPC + end + return creatureType + elseif key == "itemid" then + return 1 + elseif key == "actionid" then + return 0 + end + return methods[key] + end + rawgetmetatable("Player").__index = CreatureIndex + -- rawgetmetatable("Monster").__index = CreatureIndex + -- rawgetmetatable("Npc").__index = CreatureIndex +end + +-- Item index +-- do +-- local function ItemIndex(self, key) +-- local methods = getmetatable(self) +-- if key == "itemid" then +-- return methods.getId(self) +-- elseif key == "actionid" then +-- return methods.getActionId(self) +-- elseif key == "uid" then +-- return methods.getUniqueId(self) +-- elseif key == "type" then +-- return methods.getSubType(self) +-- end +-- return methods[key] +-- end +-- rawgetmetatable("Item").__index = ItemIndex +-- rawgetmetatable("Container").__index = ItemIndex +-- rawgetmetatable("Teleport").__index = ItemIndex +-- end + +-- Action revscriptsys +-- do +-- local function ActionNewIndex(self, key, value) +-- if key == "onUse" then +-- self:onUse(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("Action").__newindex = ActionNewIndex +-- end + +-- TalkAction revscriptsys +do + local function TalkActionNewIndex(self, key, value) + if key == "onSay" then + self:onSay(value) + return + end + rawset(self, key, value) + end + print('TalkAction revscriptsys') + print(configKeys.BASE_DIRECTORY) + + local meta = rawgetmetatable("TalkAction") + print('meta') + print(meta) + meta.__newindex = TalkActionNewIndex +end + +-- -- Sets a custom __newindex behavior for the EventCallback class's metatable. It dynamically maps certain keys to predefined callback methods within the EventCallback class. When a key matching a method name is added, it triggers the associated function, sets the event type, and logs the registration. This allows for flexible, runtime assignment of various event handlers through Lua scripts. +-- local eventCallbacks = Game.getEventCallbacks() +-- do +-- local function EventCallbackNewIndex(self, key, value) +-- local func = eventCallbacks[key] +-- if func and type(func) == "function" then +-- logger.debug("[Registering EventCallback: {}", key) +-- func(self, value) +-- self:type(key) +-- else +-- reportError("Invalid EventCallback with name: {}", tostring(key)) +-- end +-- end +-- rawgetmetatable("EventCallback").__newindex = EventCallbackNewIndex +-- end + + -- CreatureEvent revscriptsys +-- do +-- local function CreatureEventNewIndex(self, key, value) +-- if key == "onLogin" then +-- self:type("login") +-- self:onLogin(value) +-- return +-- elseif key == "onLogout" then +-- self:type("logout") +-- self:onLogout(value) +-- return +-- elseif key == "onThink" then +-- self:type("think") +-- self:onThink(value) +-- return +-- elseif key == "onPrepareDeath" then +-- self:type("preparedeath") +-- self:onPrepareDeath(value) +-- return +-- elseif key == "onDeath" then +-- self:type("death") +-- self:onDeath(value) +-- return +-- elseif key == "onKill" then +-- self:type("kill") +-- self:onKill(value) +-- return +-- elseif key == "onAdvance" then +-- self:type("advance") +-- self:onAdvance(value) +-- return +-- elseif key == "onModalWindow" then +-- self:type("modalwindow") +-- self:onModalWindow(value) +-- return +-- elseif key == "onTextEdit" then +-- self:type("textedit") +-- self:onTextEdit(value) +-- return +-- elseif key == "onHealthChange" then +-- self:type("healthchange") +-- self:onHealthChange(value) +-- return +-- elseif key == "onManaChange" then +-- self:type("manachange") +-- self:onManaChange(value) +-- return +-- elseif key == "onExtendedOpcode" then +-- self:type("extendedopcode") +-- self:onExtendedOpcode(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("CreatureEvent").__newindex = CreatureEventNewIndex +-- end + +-- -- MoveEvent revscriptsys +-- do +-- local function MoveEventNewIndex(self, key, value) +-- if key == "onEquip" then +-- self:type("equip") +-- self:onEquip(value) +-- return +-- elseif key == "onDeEquip" then +-- self:type("deequip") +-- self:onDeEquip(value) +-- return +-- elseif key == "onAddItem" then +-- self:type("additem") +-- self:onAddItem(value) +-- return +-- elseif key == "onRemoveItem" then +-- self:type("removeitem") +-- self:onRemoveItem(value) +-- return +-- elseif key == "onStepIn" then +-- self:type("stepin") +-- self:onStepIn(value) +-- return +-- elseif key == "onStepOut" then +-- self:type("stepout") +-- self:onStepOut(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("MoveEvent").__newindex = MoveEventNewIndex +-- end + +-- -- GlobalEvent revscriptsys +-- do +-- local function GlobalEventNewIndex(self, key, value) +-- if key == "onThink" then +-- self:onThink(value) +-- return +-- elseif key == "onTime" then +-- self:onTime(value) +-- return +-- elseif key == "onStartup" then +-- self:type("startup") +-- self:onStartup(value) +-- return +-- elseif key == "onShutdown" then +-- self:type("shutdown") +-- self:onShutdown(value) +-- return +-- elseif key == "onRecord" then +-- self:type("record") +-- self:onRecord(value) +-- return +-- elseif key == "onPeriodChange" then +-- self:type("periodchange") +-- self:onPeriodChange(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("GlobalEvent").__newindex = GlobalEventNewIndex +-- end + +-- -- Weapons revscriptsys +-- do +-- local function WeaponNewIndex(self, key, value) +-- if key == "onUseWeapon" then +-- self:onUseWeapon(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("Weapon").__newindex = WeaponNewIndex +-- end + +-- -- Spells revscriptsys +-- do +-- local function SpellNewIndex(self, key, value) +-- if key == "onCastSpell" then +-- self:onCastSpell(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("Spell").__newindex = SpellNewIndex +-- end + +-- -- Monsters revscriptsys +-- do +-- local function MonsterTypeNewIndex(self, key, value) +-- if key == "onThink" then +-- self:eventType(MONSTERS_EVENT_THINK) +-- self:onThink(value) +-- return +-- elseif key == "onAppear" then +-- self:eventType(MONSTERS_EVENT_APPEAR) +-- self:onAppear(value) +-- return +-- elseif key == "onDisappear" then +-- self:eventType(MONSTERS_EVENT_DISAPPEAR) +-- self:onDisappear(value) +-- return +-- elseif key == "onMove" then +-- self:eventType(MONSTERS_EVENT_MOVE) +-- self:onMove(value) +-- return +-- elseif key == "onSay" then +-- self:eventType(MONSTERS_EVENT_SAY) +-- self:onSay(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("MonsterType").__newindex = MonsterTypeNewIndex +-- end + +-- -- Npcs revscriptsys +-- do +-- local function NpcTypeNewIndex(self, key, value) +-- if key == "onThink" then +-- self:eventType(NPCS_EVENT_THINK) +-- self:onThink(value) +-- return +-- elseif key == "onAppear" then +-- self:eventType(NPCS_EVENT_APPEAR) +-- self:onAppear(value) +-- return +-- elseif key == "onDisappear" then +-- self:eventType(NPCS_EVENT_DISAPPEAR) +-- self:onDisappear(value) +-- return +-- elseif key == "onMove" then +-- self:eventType(NPCS_EVENT_MOVE) +-- self:onMove(value) +-- return +-- elseif key == "onSay" then +-- self:eventType(NPCS_EVENT_SAY) +-- self:onSay(value) +-- return +-- elseif key == "onBuyItem" then +-- self:eventType(NPCS_EVENT_PLAYER_BUY) +-- self:onBuyItem(value) +-- return +-- elseif key == "onSellItem" then +-- self:eventType(NPCS_EVENT_PLAYER_SELL) +-- self:onSellItem(value) +-- return +-- elseif key == "onCheckItem" then +-- self:eventType(NPCS_EVENT_PLAYER_CHECK_ITEM) +-- self:onCheckItem(value) +-- return +-- elseif key == "onCloseChannel" then +-- self:eventType(NPCS_EVENT_PLAYER_CLOSE_CHANNEL) +-- self:onBuyItem(value) +-- return +-- end +-- rawset(self, key, value) +-- end +-- rawgetmetatable("NpcType").__newindex = NpcTypeNewIndex +-- end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/libs.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/libs.lua new file mode 100644 index 000000000..76af80bff --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/libs.lua @@ -0,0 +1,2 @@ +-- Load core functions +dofile(CORE_DIRECTORY .. "/libs/functions/load.lua") diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/actions/Tools/shovel.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/actions/Tools/shovel.lua new file mode 100644 index 000000000..ac69947d4 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/actions/Tools/shovel.lua @@ -0,0 +1,9 @@ +-- local shovel = Action() + +-- function shovel.onUse(player, item, itemEx, fromPosition, target, toPosition, isHotkey) +-- print('shovel.onUse from lua') +-- --return onUseShovel(player, item, itemEx, fromPosition, target, toPosition, isHotkey) +-- end + +-- shovel:id(3457, 5710) +-- shovel:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua new file mode 100644 index 000000000..c363f1ba3 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua @@ -0,0 +1,37 @@ +-- local function serverSave(interval) +-- --if configManager.getBoolean(configKeys.TOGGLE_SAVE_INTERVAL_CLEAN_MAP) then +-- -- cleanMap() +-- --end + +-- saveServer() +-- local message = string.format(SAVE_INTERVAL_CONFIG_TIME > 1 and "Server save complete. Next save in %d %ss!" or "Server save complete. Next save in %d %s!", SAVE_INTERVAL_CONFIG_TIME, SAVE_INTERVAL_TYPE) +-- Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) +-- logger.info(message) +-- --Webhook.sendMessage("Server save", message, WEBHOOK_COLOR_WARNING) +-- end + +-- local save = GlobalEvent("save") + +-- function save.onTime(interval) +-- local remainingTime = 60 * 1000 +-- if configManager.getBoolean(configKeys.TOGGLE_SAVE_INTERVAL) then +-- local message = "The server will save all accounts within " .. (remainingTime / 1000) .. " seconds. \z +-- You might lag or freeze for 5 seconds, please find a safe place." +-- Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) +-- logger.info(string.format(message, SAVE_INTERVAL_CONFIG_TIME, SAVE_INTERVAL_TYPE)) +-- addEvent(serverSave, remainingTime, interval) +-- return true +-- end +-- return not configManager.getBoolean(configKeys.TOGGLE_SAVE_INTERVAL) +-- end + +-- if SAVE_INTERVAL_TIME ~= 0 then +-- --print(SAVE_INTERVAL_CONFIG_TIME * SAVE_INTERVAL_TIME) +-- save:interval(SAVE_INTERVAL_CONFIG_TIME * SAVE_INTERVAL_TIME) +-- else +-- return logger.error(string.format("[save.onTime] - Save interval type '%s' is not valid, use 'second', 'minute' or 'hour'", SAVE_INTERVAL_TYPE)) +-- --print(string.format("[save.onTime] - Save interval type '%s' is not valid, use 'second', 'minute' or 'hour'", SAVE_INTERVAL_TYPE)) +-- --return false +-- end + +-- save:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua new file mode 100644 index 000000000..afa1a04f1 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua @@ -0,0 +1,7 @@ +-- local Example_One = GlobalEvent("Example one") +-- function Example_One.onStartup() +-- print('GlobalEvent Example_One: onStartup') +-- return true +-- end + +-- Example_One:register() \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua new file mode 100644 index 000000000..f5072721e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua @@ -0,0 +1,8 @@ +-- local Example_Two = GlobalEvent("Example two") +-- function Example_Two.onThink(interval) +-- print('GlobalEvent Example_Two: onThink') +-- return true +-- end + +-- Example_Two:interval(10000) -- 10 seconds interval +-- Example_Two:register() \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua new file mode 100644 index 000000000..f9116b258 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua @@ -0,0 +1,12 @@ +-- local bossThink = CreatureEvent("BossThink") + +-- function bossThink.onThink(creature, interval) +-- print('bossThink.onThink from lua') +-- if not creature then +-- return true +-- end + +-- --ResetAndSetTargetList(creature) +-- end + +-- bossThink:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua new file mode 100644 index 000000000..39d30e1a4 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua @@ -0,0 +1,42 @@ +-- local talkAction = TalkAction("/benchmark") + +-- function talkAction.onSay(player, words, param) +-- do +-- local units = { +-- ['seconds'] = 1, +-- ['milliseconds'] = 1000, +-- ['microseconds'] = 1000000, +-- ['nanoseconds'] = 1000000000 +-- } + +-- function benchmark(unit, decPlaces, n, f, ...) +-- local elapsed = 0 +-- local multiplier = units[unit] +-- for i = 1, n do +-- local now = os.clock() +-- f(...) +-- elapsed = elapsed + (os.clock() - now) +-- end +-- print( +-- string.format('Benchmark results: %d calls | %.'.. decPlaces ..'f %s elapsed | %.'.. decPlaces ..'f %s avg execution time.', n, elapsed * multiplier, unit, (elapsed / n) * multiplier, unit)) +-- end +-- end + +-- function test(n) +-- local x = "x"; +-- local str = "Minha string" .. x +-- --local result = 10 + 20; +-- -- print(n) +-- --local t = {} +-- --for i = 1, n do +-- --[i] = i +-- --local str = "Minha string" .. x +-- -- print(i) +-- --end +-- end + +-- benchmark('milliseconds', 2, 500000, test, 1) -- Benchmark results: 500 function calls | 254.96 milliseconds elapsed | 0.51 milliseconds avg execution time. +-- end + +-- talkAction:separator(" ") +-- talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/reload.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/reload.lua new file mode 100644 index 000000000..838f6dcc0 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/reload.lua @@ -0,0 +1,86 @@ +-- local talkAction = TalkAction("/reload") + +-- function talkAction.onSay(player, words, param) +-- local reloadTypes = { +-- ["all"] = RELOAD_TYPE_ALL, + +-- ["chat"] = RELOAD_TYPE_CHAT, +-- ["channel"] = RELOAD_TYPE_CHAT, +-- ["chatchannels"] = RELOAD_TYPE_CHAT, + +-- ["config"] = RELOAD_TYPE_CONFIG, +-- ["configuration"] = RELOAD_TYPE_CONFIG, + +-- ["events"] = RELOAD_TYPE_EVENTS, + +-- ["items"] = RELOAD_TYPE_ITEMS, + +-- ["module"] = RELOAD_TYPE_MODULES, +-- ["modules"] = RELOAD_TYPE_MODULES, + +-- ["monster"] = RELOAD_TYPE_MONSTERS, +-- ["monsters"] = RELOAD_TYPE_MONSTERS, + +-- ["mount"] = RELOAD_TYPE_MOUNTS, +-- ["mounts"] = RELOAD_TYPE_MOUNTS, + +-- ["npc"] = RELOAD_TYPE_NPCS, +-- ["npcs"] = RELOAD_TYPE_NPCS, + +-- ["raid"] = RELOAD_TYPE_RAIDS, +-- ["raids"] = RELOAD_TYPE_RAIDS, + +-- ["scripts"] = RELOAD_TYPE_SCRIPTS, +-- ["script"] = RELOAD_TYPE_SCRIPTS, + +-- ["rate"] = RELOAD_TYPE_CORE, +-- ["rates"] = RELOAD_TYPE_CORE, +-- ["stage"] = RELOAD_TYPE_CORE, +-- ["stages"] = RELOAD_TYPE_CORE, +-- ["global"] = RELOAD_TYPE_CORE, +-- ["core"] = RELOAD_TYPE_CORE, +-- ["lib"] = RELOAD_TYPE_CORE, +-- ["libs"] = RELOAD_TYPE_CORE, + +-- ["imbuements"] = RELOAD_TYPE_IMBUEMENTS, + +-- ["group"] = RELOAD_TYPE_GROUPS, +-- ["groups"] = RELOAD_TYPE_GROUPS, +-- } + +-- if not configManager.getBoolean(configKeys.ALLOW_RELOAD) then +-- print("Reload command is disabled.") +-- --self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload command is disabled.") +-- return true +-- end + +-- if param == "" then +-- print("Command param required.") +-- --self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Command param required.") +-- return true +-- end + +-- -- create log +-- --logCommand(self, "/reload", param) + +-- local reloadType = reloadTypes[param:lower()] +-- if reloadType then +-- -- Force save server before reload +-- --saveServer() +-- --SaveHirelings() +-- --logger.info("Saved Hirelings") +-- --self:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is saved.. Now will reload configs!") + +-- Game.reload(reloadType) +-- --self:sendTextMessage(MESSAGE_LOOK, string.format("Reloaded %s.", param:lower())) +-- --logger.info("Reloaded {}", param:lower()) +-- elseif not reloadType then +-- --self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload type not found.") +-- --logger.warn("[reload.onSay] - Reload type '{}' not found", param) +-- end + +-- return true +-- end + +-- talkAction:separator(" ") +-- talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua new file mode 100644 index 000000000..d152c8fb6 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua @@ -0,0 +1,48 @@ +-- local talkAction = TalkAction("!help") + +-- function Player.teste(self, message) +-- print('message from player:teste() ' .. self:getName() .. ' - ' .. message) +-- end + +-- local function testEvent1() +-- print('testEvent1') +-- end + +-- local function testEvent2(param) +-- print('testEvent2 param: ' .. param) +-- end + +-- function talkAction.onSay(player, words, param) +-- print('executing talkAction from lua: '.. words .. ' ' .. param) + +-- player:teste('aaaaaaaaaaaaaa') +-- print(player:getName()) +-- print(player:getId()) + +-- local creature = Creature("Muniz") +-- print(creature:getName()) +-- print(creature:getId()) + +-- if player == creature then +-- print('player == creature') +-- end + +-- local showScriptsLogInConsole = configManager.getBoolean(configKeys.SCRIPTS_CONSOLE_LOGS); + +-- print(showScriptsLogInConsole) + +-- local evt1Id = addEvent(testEvent1, 1000) +-- print('evt1Id: ' .. evt1Id) + +-- local evt2Id = addEvent(testEvent1, 8000) +-- print('evt2Id: ' .. evt2Id) +-- stopEvent(evt2Id) + +-- local evt3Id = addEvent(testEvent2, 10000, player:getName()) +-- print('evt3Id: ' .. evt3Id) + +-- return true +-- end + +-- talkAction:separator(" ") +-- talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/test.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/test.lua new file mode 100644 index 000000000..a35b7203e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/test.lua @@ -0,0 +1,34 @@ +local talkAction = TalkAction("!test") + +function talkAction.onSay(player, words, param) + print('executing talkAction from lua: '.. words .. ' ' .. param) + + print(player) + print(player:getName()) + print(player:getId()) + + local creature = Creature("GOD") + + if creature then + print(creature) + print(creature:getName()) + print(creature:getId()) + + if player == creature then + print('player == creature') + end + end + + local showInConsole = configManager.getBoolean(configKeys.SCRIPTS_CONSOLE_LOGS); + + print(showInConsole) + + player:sendTextMessage(0, param, 0); + + print('end') + + return true +end + +talkAction:separator(" ") +talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/GlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/GlobalFunctions.cs new file mode 100644 index 000000000..1a808105d --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/GlobalFunctions.cs @@ -0,0 +1,22 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; + +public class GlobalFunctions : LuaScriptInterface +{ + public GlobalFunctions() : base(nameof(GlobalFunctions)) + { + } + + public static void Init(LuaState L) + { + RegisterGlobalMethod(L, "rawgetmetatable", LuaRawGetMetatable); + } + + private static int LuaRawGetMetatable(LuaState L) + { + // rawgetmetatable(metatableName) + Lua.GetMetaTable(L, GetString(L, 1)); + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs new file mode 100644 index 000000000..db810eef2 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs @@ -0,0 +1,34 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; + +public interface IConfigManager +{ + public bool Load(string file); + + //public bool Reload(); + + public string GetString(StringConfigType what); + + public int GetNumber(IntegerConfigType what); + + public short GetShortNumber(IntegerConfigType what); + + public ushort GetUShortNumber(IntegerConfigType what); + + public bool GetBoolean(BooleanConfigType what); + + public float GetFloat(FloatingConfigType what); + + public string SetConfigFileLua(string what); + + public string GetConfigFileLua(); + + public string GetGlobalString(LuaState L, string identifier, string defaultValue); + + public int GetGlobalNumber(LuaState L, string identifier, int defaultValue = 0); + + public bool GetGlobalBoolean(LuaState L, string identifier, bool defaultValue); + + public float GetGlobalFloat(LuaState L, string identifier, float defaultValue = 0.0f); +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ICreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ICreatureFunctions.cs new file mode 100644 index 000000000..25d01f903 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ICreatureFunctions.cs @@ -0,0 +1,8 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; + +public interface ICreatureFunctions +{ + void Init(LuaState L); +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs new file mode 100644 index 000000000..6a45e39c7 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs @@ -0,0 +1,40 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; + +public interface ILuaEnvironment : ILuaScriptInterface +{ + public LuaState GetLuaState(); + + public bool InitState(); + + public bool ReInitState(); + + public bool CloseState(); + + public LuaScriptInterface GetTestInterface(); + + //public Combat GetCombatObject(uint id); + + //public Combat CreateCombatObject(LuaScriptInterface @interface); + + //public void ClearCombatObjects(LuaScriptInterface @interface); + + //public AreaCombat GetAreaObject(uint id); + + //public uint CreateAreaObject(LuaScriptInterface @interface); + + //public void ClearAreaObjects(LuaScriptInterface @interface); + + //public static T CreateWeaponObject(LuaScriptInterface interfaceInstance) where T : YourWeaponType, new(); + + //public static T GetWeaponObject(uint id) where T : YourWeaponType; + + //public static void ClearWeaponObjects(LuaScriptInterface interfaceInstance); + + //public bool IsShuttingDown(); + + public void ExecuteTimerEvent(uint eventIndex); + + public void CollectGarbage(); +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs new file mode 100644 index 000000000..f6c5e576d --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs @@ -0,0 +1,6 @@ +//namespace NeoServer.Scripts.LuaJIT; + +//public interface ILuaManager +//{ +// void Start(); +//} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaScriptInterface.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaScriptInterface.cs new file mode 100644 index 000000000..9eaca8476 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaScriptInterface.cs @@ -0,0 +1,38 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface ILuaScriptInterface +{ + public bool ReInitState(); + + public bool LoadFile(string file, string scriptName); + + public int GetEvent(string eventName); + + public int GetEvent(); + + public int GetMetaEvent(string globalName, string eventName); + + public string GetFileById(int scriptId); + + public string GetStackTrace(string errorDesc); + + public bool PushFunction(int functionId); + + public bool InitState(); + + public bool CloseState(); + + public bool CallFunction(int parameters); + + public void CallVoidFunction(int parameters); + + public string GetInterfaceName(); + + public string GetLastLuaError(); + + public string GetLoadingFile(); + + public string GetLoadingScriptName(); + + public void SetLoadingScriptName(string scriptName); +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IScripts.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IScripts.cs new file mode 100644 index 000000000..f0c0a3ad4 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IScripts.cs @@ -0,0 +1,14 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IScripts +{ + public void ClearAllScripts(); + + public bool LoadEventSchedulerScripts(string fileName); + + public bool LoadScripts(string loadPath, bool isLib, bool reload); + + public LuaScriptInterface GetScriptInterface(); + + public int GetScriptId(); +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs new file mode 100644 index 000000000..b3d75f152 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs @@ -0,0 +1,17 @@ +using NeoServer.Game.Common.Chats; +using NeoServer.Game.Common.Contracts.Creatures; + +namespace NeoServer.Scripts.LuaJIT; + +public interface ITalkActions +{ + public bool CheckWord(IPlayer player, SpeechType type, string words, string word, TalkAction talkActionPtr); + + public TalkActionResultType CheckPlayerCanSayTalkAction(IPlayer player, SpeechType type, string words); + + public bool RegisterLuaEvent(TalkAction talkAction); + + public void Clear(); + + public TalkAction GetTalkAction(string word); +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs new file mode 100644 index 000000000..814c5ecb0 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs @@ -0,0 +1,36 @@ +using Serilog; + +namespace NeoServer.Scripts.LuaJIT; + +public class Logger +{ + private static Logger _instance; + public static Logger GetInstance() => _instance == null ? _instance = new Logger() : _instance; + + private ILogger _logger; + + public Logger() + { + _logger = new LoggerConfiguration().CreateLogger(); + } + + public void Debug(string message, string args = "") + { + _logger.Debug(message, args); + } + + public void Info(string message, string args = "") + { + _logger.Information(message, args); + } + + public void Warn(string message, string args = "") + { + _logger.Warning(message, args); + } + + public void Error(string message, string args = "") + { + _logger.Error(message, args); + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LoggerFunctions.cs new file mode 100644 index 000000000..6fccca3f9 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LoggerFunctions.cs @@ -0,0 +1,75 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; + +public class LoggerFunctions : LuaScriptInterface +{ + public LoggerFunctions() : base(nameof(LoggerFunctions)) + { + } + + public static void Init(LuaState L) + { + RegisterTable(L, "logger"); + RegisterMethod(L, "logger", "info", LuaLoggerInfo); + RegisterMethod(L, "logger", "warn", LuaLoggerWarn); + RegisterMethod(L, "logger", "error", LuaLoggerError); + RegisterMethod(L, "logger", "debug", LuaLoggerDebug); + } + + private static int LuaLoggerInfo(LuaState L) + { + // logger.info(text) + if (IsString(L, 1)) + { + Logger.GetInstance().Info(GetFormatedLoggerMessage(L)); + } + else + { + //reportErrorFunc("First parameter needs to be a string"); + } + return 1; + } + + private static int LuaLoggerWarn(LuaState L) + { + // logger.info(text) + if (IsString(L, 1)) + { + Logger.GetInstance().Warn(GetFormatedLoggerMessage(L)); + } + else + { + //reportErrorFunc("First parameter needs to be a string"); + } + return 1; + } + + private static int LuaLoggerError(LuaState L) + { + // logger.info(text) + if (IsString(L, 1)) + { + Logger.GetInstance().Error(GetFormatedLoggerMessage(L)); + } + else + { + //reportErrorFunc("First parameter needs to be a string"); + } + return 1; + } + + private static int LuaLoggerDebug(LuaState L) + { + // logger.info(text) + if (IsString(L, 1)) + { + Logger.GetInstance().Debug(GetFormatedLoggerMessage(L)); + } + else + { + //reportErrorFunc("First parameter needs to be a string"); + } + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs new file mode 100644 index 000000000..26a8afc19 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs @@ -0,0 +1,251 @@ +namespace NeoServer.Scripts.LuaJIT; + +// Enums +public enum LuaDataType : byte +{ + Unknown, + Item, + Container, + Teleport, + Player, + Monster, + Npc, + MonsterType, + NpcType, + Tile, + Variant, + Position, + NetworkMessage, + ModalWindow, + Guild, + Group, + Vocation, + Town, + House, + ItemType, + Combat, + Condition, + Charm, + Loot, + MonsterSpell, + Spell, + Party, + Action, + TalkAction, + CreatureEvent, + MoveEvent, + GlobalEvent, + Weapon, + Imbuement, + Mount, + ItemClassification, +}; + +public enum CreatureEventType +{ + CREATURE_EVENT_NONE, + CREATURE_EVENT_LOGIN, + CREATURE_EVENT_LOGOUT, + CREATURE_EVENT_THINK, + CREATURE_EVENT_PREPAREDEATH, + CREATURE_EVENT_DEATH, + CREATURE_EVENT_KILL, + CREATURE_EVENT_ADVANCE, + CREATURE_EVENT_MODALWINDOW, + CREATURE_EVENT_TEXTEDIT, + CREATURE_EVENT_HEALTHCHANGE, + CREATURE_EVENT_MANACHANGE, + // Otclient additional network opcodes. + CREATURE_EVENT_EXTENDED_OPCODE, +}; + +public enum MoveEventType +{ + MOVE_EVENT_STEP_IN, + MOVE_EVENT_STEP_OUT, + MOVE_EVENT_EQUIP, + MOVE_EVENT_DEEQUIP, + MOVE_EVENT_ADD_ITEM, + MOVE_EVENT_REMOVE_ITEM, + MOVE_EVENT_ADD_ITEM_ITEMTILE, + MOVE_EVENT_REMOVE_ITEM_ITEMTILE, + + MOVE_EVENT_LAST, + MOVE_EVENT_NONE +}; + +public enum RaidStateType +{ + RAIDSTATE_IDLE, + RAIDSTATE_EXECUTING, +}; + +public enum TalkActionResultType +{ + TALKACTION_CONTINUE, + TALKACTION_BREAK, + TALKACTION_FAILED, +}; + +public enum GlobalEventType +{ + GLOBALEVENT_NONE, + GLOBALEVENT_TIMER, + + GLOBALEVENT_STARTUP, + GLOBALEVENT_SHUTDOWN, + GLOBALEVENT_RECORD, + GLOBALEVENT_PERIODCHANGE, + GLOBALEVENT_ON_THINK, +}; + +public enum ModuleTypeType +{ + MODULE_TYPE_RECVBYTE, + MODULE_TYPE_NONE, +}; + +public enum LuaVariantType +{ + VARIANT_NONE, + + VARIANT_NUMBER, + VARIANT_POSITION, + VARIANT_TARGETPOSITION, + VARIANT_STRING, +}; + +public enum ErrorCodeType +{ + LUA_ERROR_PLAYER_NOT_FOUND, + LUA_ERROR_CREATURE_NOT_FOUND, + LUA_ERROR_NPC_NOT_FOUND, + LUA_ERROR_NPC_TYPE_NOT_FOUND, + LUA_ERROR_MONSTER_NOT_FOUND, + LUA_ERROR_MONSTER_TYPE_NOT_FOUND, + LUA_ERROR_ITEM_NOT_FOUND, + LUA_ERROR_THING_NOT_FOUND, + LUA_ERROR_TILE_NOT_FOUND, + LUA_ERROR_HOUSE_NOT_FOUND, + LUA_ERROR_COMBAT_NOT_FOUND, + LUA_ERROR_CONDITION_NOT_FOUND, + LUA_ERROR_AREA_NOT_FOUND, + LUA_ERROR_CONTAINER_NOT_FOUND, + LUA_ERROR_VARIANT_NOT_FOUND, + LUA_ERROR_VARIANT_UNKNOWN, + LUA_ERROR_SPELL_NOT_FOUND, + LUA_ERROR_ACTION_NOT_FOUND, + LUA_ERROR_TALK_ACTION_NOT_FOUND, + LUA_ERROR_ZONE_NOT_FOUND, +}; + +public enum TargetSearchTypeType +{ + TARGETSEARCH_DEFAULT, + TARGETSEARCH_NEAREST, + TARGETSEARCH_HP, + TARGETSEARCH_DAMAGE, + TARGETSEARCH_RANDOM +}; + +public enum MapMarkType +{ + MAPMARK_TICK = 0, + MAPMARK_QUESTION = 1, + MAPMARK_EXCLAMATION = 2, + MAPMARK_STAR = 3, + MAPMARK_CROSS = 4, + MAPMARK_TEMPLE = 5, + MAPMARK_KISS = 6, + MAPMARK_SHOVEL = 7, + MAPMARK_SWORD = 8, + MAPMARK_FLAG = 9, + MAPMARK_LOCK = 10, + MAPMARK_BAG = 11, + MAPMARK_SKULL = 12, + MAPMARK_DOLLAR = 13, + MAPMARK_REDNORTH = 14, + MAPMARK_REDSOUTH = 15, + MAPMARK_REDEAST = 16, + MAPMARK_REDWEST = 17, + MAPMARK_GREENNORTH = 18, + MAPMARK_GREENSOUTH = 19, +}; + +public enum RuleViolationTypeType : byte +{ + REPORT_TYPE_NAME = 0, + REPORT_TYPE_STATEMENT = 1, + REPORT_TYPE_BOT = 2 +}; + +public enum RuleViolationReasonsType : byte +{ + REPORT_REASON_NAMEINAPPROPRIATE = 0, + REPORT_REASON_NAMEPOORFORMATTED = 1, + REPORT_REASON_NAMEADVERTISING = 2, + REPORT_REASON_NAMEUNFITTING = 3, + REPORT_REASON_NAMERULEVIOLATION = 4, + REPORT_REASON_INSULTINGSTATEMENT = 5, + REPORT_REASON_SPAMMING = 6, + REPORT_REASON_ADVERTISINGSTATEMENT = 7, + REPORT_REASON_UNFITTINGSTATEMENT = 8, + REPORT_REASON_LANGUAGESTATEMENT = 9, + REPORT_REASON_DISCLOSURE = 10, + REPORT_REASON_RULEVIOLATION = 11, + REPORT_REASON_STATEMENT_BUGABUSE = 12, + REPORT_REASON_UNOFFICIALSOFTWARE = 13, + REPORT_REASON_PRETENDING = 14, + REPORT_REASON_HARASSINGOWNERS = 15, + REPORT_REASON_FALSEINFO = 16, + REPORT_REASON_ACCOUNTSHARING = 17, + REPORT_REASON_STEALINGDATA = 18, + REPORT_REASON_SERVICEATTACKING = 19, + REPORT_REASON_SERVICEAGREEMENT = 20 +}; + +public enum BugReportTypeType : byte +{ + BUG_CATEGORY_MAP = 0, + BUG_CATEGORY_TYPO = 1, + BUG_CATEGORY_TECHNICAL = 2, + BUG_CATEGORY_OTHER = 3 +}; + +// Struct +public struct LuaVariant +{ + public LuaVariantType Type = LuaVariantType.VARIANT_NONE; + public string Text; + public string InstantName; + public string RuneName; + //public Position Pos; + public uint Number = 0; + + public LuaVariant() + { + } +} + +public struct LuaTimerEventDesc +{ + public int ScriptId = -1; + public string ScriptName; + public int Function = -1; + public List Parameters; + public string EventId = string.Empty; + + public LuaTimerEventDesc() + { + Parameters = new List(); + } + + public LuaTimerEventDesc(int scriptId, string scriptName, int function, List parameters, string eventId) + { + this.ScriptId = scriptId; + this.ScriptName = scriptName; + this.Function = function; + this.Parameters = parameters; + this.EventId = eventId; + } +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs new file mode 100644 index 000000000..f4796fdea --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs @@ -0,0 +1,319 @@ +using LuaNET; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT; + +public class LuaEnvironment : LuaScriptInterface, ILuaEnvironment +{ + #region Members + + private static LuaEnvironment _instance = null; + + private static bool shuttingDown = false; + + //private static readonly Dictionary> combatIdMap = new Dictionary>(); + //private static readonly Dictionary combatMap = new Dictionary(); + //private static uint lastCombatId = 0; + + //private static readonly Dictionary> areaIdMap = new Dictionary>(); + //private static readonly Dictionary areaMap = new Dictionary(); + //private static uint lastAreaId = 0; + + //private static uint lastWeaponId; + //private static Dictionary weaponMap = new Dictionary(); + //private static Dictionary> weaponIdMap = new Dictionary>(); + + public readonly Dictionary timerEvents = new Dictionary(); + public uint LastEventTimerId = 1; + + private static readonly List cacheFiles = new List(); + + private static LuaScriptInterface testInterface; + + private static int runningEventId = EVENT_ID_USER; + + #endregion + + #region Injection + + /// + /// A reference to the logger in use. + /// + private readonly ILogger _logger; + + #endregion + + #region Instance + + public static LuaEnvironment GetInstance() => _instance == null ? _instance = new LuaEnvironment() : _instance; + + public LuaEnvironment() : base("Main Interface") + { + + } + + #endregion + + #region Constructors + + public LuaEnvironment(ILogger logger) : base("Main Interface") + { + _instance = this; + + _logger = logger.ForContext(); + } + + ~LuaEnvironment() + { + if (testInterface == null) + { + } + + shuttingDown = true; + CloseState(); + } + + #endregion + + #region Public Methods + + public LuaState GetLuaState() + { + if (shuttingDown) + { + return luaState; + } + + if (luaState.IsNull) + { + InitState(); + } + + return luaState; + } + + public bool InitState() + { + luaState = Lua.NewState(); + //LuaFunctionsLoader.Load(luaState); + runningEventId = EVENT_ID_USER; + + return true; + } + + public bool ReInitState() + { + // TODO: get children, reload children + CloseState(); + return InitState(); + } + + public bool CloseState() + { + if (luaState.IsNull) + { + return false; + } + + //foreach (var combatEntry in combatIdMap) + //{ + // ClearCombatObjects(combatEntry.Key); + //} + + //foreach (var areaEntry in areaIdMap) + //{ + // ClearAreaObjects(areaEntry.Key); + //} + + foreach (var timerEntry in timerEvents) + { + var timerEventDesc = timerEntry.Value; + foreach (var parameter in timerEventDesc.Parameters) + { + Lua.UnRef(luaState, LUA_REGISTRYINDEX, parameter); + } + Lua.UnRef(luaState, LUA_REGISTRYINDEX, timerEventDesc.Function); + } + + //combatIdMap.Clear(); + //areaIdMap.Clear(); + timerEvents.Clear(); + cacheFiles.Clear(); + + Lua.Close(luaState); + luaState.pointer = 0; + return true; + } + + public LuaScriptInterface GetTestInterface() + { + if (testInterface == null) + { + testInterface = new LuaScriptInterface("Test Interface"); + testInterface.InitState(); + } + return testInterface; + } + + //public Combat GetCombatObject(uint id) + //{ + // if (combatMap.TryGetValue(id, out var combat)) + // { + // return combat; + // } + // return null; + //} + + //public Combat CreateCombatObject(LuaScriptInterface @interface) + //{ + // var combat = new Combat(); + // combatMap[++lastCombatId] = combat; + // if (!combatIdMap.ContainsKey(@interface)) + // { + // combatIdMap[@interface] = new List(); + // } + // combatIdMap[@interface].Add(lastCombatId); + // return combat; + //} + + //public void ClearCombatObjects(LuaScriptInterface @interface) + //{ + // if (combatIdMap.TryGetValue(@interface, out var combatIds)) + // { + // foreach (var id in combatIds) + // { + // if (combatMap.TryGetValue(id, out var combat)) + // { + // combatMap.Remove(id); + // } + // } + // combatIds.Clear(); + // } + //} + + //public AreaCombat GetAreaObject(uint id) + //{ + // if (areaMap.TryGetValue(id, out var areaCombat)) + // { + // return areaCombat; + // } + // return null; + //} + + //public uint CreateAreaObject(LuaScriptInterface @interface) + //{ + // areaMap[++lastAreaId] = new AreaCombat(); + // if (!areaIdMap.ContainsKey(@interface)) + // { + // areaIdMap[@interface] = new List(); + // } + // areaIdMap[@interface].Add(lastAreaId); + // return lastAreaId; + //} + + //public void ClearAreaObjects(LuaScriptInterface @interface) + //{ + // if (areaIdMap.TryGetValue(@interface, out var areaIds)) + // { + // foreach (var id in areaIds) + // { + // if (areaMap.TryGetValue(id, out var areaCombat)) + // { + // areaMap.Remove(id); + // } + // } + // areaIds.Clear(); + // } + //} + + //public static T CreateWeaponObject(LuaScriptInterface interfaceInstance) where T : YourWeaponType, new() + //{ + // var weapon = new T(interfaceInstance); + // var weaponId = ++lastWeaponId; + // weaponMap[weaponId] = weapon; + // if (!weaponIdMap.ContainsKey(interfaceInstance)) + // { + // weaponIdMap[interfaceInstance] = new List(); + // } + // weaponIdMap[interfaceInstance].Add(weaponId); + // return weapon; + //} + + //public static T GetWeaponObject(uint id) where T : YourWeaponType + //{ + // if (weaponMap.TryGetValue(id, out var weapon)) + // { + // return weapon; + // } + // return null; + //} + + //public static void ClearWeaponObjects(LuaScriptInterface interfaceInstance) + //{ + // if (weaponIdMap.TryGetValue(interfaceInstance, out var weaponIds)) + // { + // weaponIds.Clear(); + // } + // weaponMap.Clear(); + //} + + public static bool IsShuttingDown() + { + return shuttingDown; + } + + public void ExecuteTimerEvent(uint eventIndex) + { + if (timerEvents.TryGetValue(eventIndex, out var timerEventDesc)) + { + timerEvents.Remove(eventIndex); + + Lua.RawGetI(luaState, LUA_REGISTRYINDEX, timerEventDesc.Function); + + var reverseList = timerEventDesc.Parameters.ToList(); + reverseList.Reverse(); + + foreach (var parameter in reverseList) + { + Lua.RawGetI(luaState, LUA_REGISTRYINDEX, parameter); + } + + if (ReserveScriptEnv()) + { + var env = GetScriptEnv(); + env.SetTimerEvent(); + env.SetScriptId(timerEventDesc.ScriptId, this); + CallFunction(timerEventDesc.Parameters.Count); + } + else + { + _logger.Error($"[LuaEnvironment::executeTimerEvent - Lua file {GetLoadingFile()}] Call stack overflow. Too many lua script calls being nested"); + } + + Lua.UnRef(luaState, LUA_REGISTRYINDEX, timerEventDesc.Function); + foreach (var parameter in timerEventDesc.Parameters) + { + Lua.UnRef(luaState, LUA_REGISTRYINDEX, parameter); + } + } + } + + public void CollectGarbage() + { + bool collecting = false; + + if (!collecting) + { + collecting = true; + + for (int i = -1; ++i < 2;) + { + Lua.GC(luaState, LuaGCParam.Collect, 0); + } + + collecting = false; + } + } + + #endregion +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs new file mode 100644 index 000000000..ff0103b9a --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs @@ -0,0 +1,1554 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Runtime.InteropServices; +using LuaNET; +using NeoServer.Application.Common.Contracts.Scripts; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Items; + +namespace NeoServer.Scripts.LuaJIT; + +public class LuaFunctionsLoader +{ + public const int LUA_REGISTRYINDEX = (-10000); + public const int LUA_ENVIRONINDEX = (-10001); + public const int LUA_GLOBALSINDEX = (-10002); + + public static string GetErrorDesc(ErrorCodeType code) + { + switch (code) + { + case ErrorCodeType.LUA_ERROR_PLAYER_NOT_FOUND: + return "Player not found"; + case ErrorCodeType.LUA_ERROR_CREATURE_NOT_FOUND: + return "Creature not found"; + case ErrorCodeType.LUA_ERROR_NPC_NOT_FOUND: + return "Npc not found"; + case ErrorCodeType.LUA_ERROR_NPC_TYPE_NOT_FOUND: + return "Npc type not found"; + case ErrorCodeType.LUA_ERROR_MONSTER_NOT_FOUND: + return "Monster not found"; + case ErrorCodeType.LUA_ERROR_MONSTER_TYPE_NOT_FOUND: + return "Monster type not found"; + case ErrorCodeType.LUA_ERROR_ITEM_NOT_FOUND: + return "Item not found"; + case ErrorCodeType.LUA_ERROR_THING_NOT_FOUND: + return "Thing not found"; + case ErrorCodeType.LUA_ERROR_TILE_NOT_FOUND: + return "Tile not found"; + case ErrorCodeType.LUA_ERROR_HOUSE_NOT_FOUND: + return "House not found"; + case ErrorCodeType.LUA_ERROR_COMBAT_NOT_FOUND: + return "Combat not found"; + case ErrorCodeType.LUA_ERROR_CONDITION_NOT_FOUND: + return "Condition not found"; + case ErrorCodeType.LUA_ERROR_AREA_NOT_FOUND: + return "Area not found"; + case ErrorCodeType.LUA_ERROR_CONTAINER_NOT_FOUND: + return "Container not found"; + case ErrorCodeType.LUA_ERROR_VARIANT_NOT_FOUND: + return "Variant not found"; + case ErrorCodeType.LUA_ERROR_VARIANT_UNKNOWN: + return "Unknown variant type"; + case ErrorCodeType.LUA_ERROR_SPELL_NOT_FOUND: + return "Spell not found"; + case ErrorCodeType.LUA_ERROR_ACTION_NOT_FOUND: + return "Action not found"; + case ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND: + return "TalkAction not found"; + case ErrorCodeType.LUA_ERROR_ZONE_NOT_FOUND: + return "Zone not found"; + default: + return "Bad error code"; + } + } + + public static int ProtectedCall(LuaState L, int nargs, int nresults) + { + var ret = 0; + if (ValidateDispatcherContext(nameof(ProtectedCall))) + { + return ret; + } + + int errorIndex = Lua.GetTop(L) - nargs; + //int errorIndex = -1 - nargs - 1; + Lua.PushCFunction(L, LuaErrorHandler); + Lua.Insert(L, errorIndex); + + //int before = Lua.GetTop(L); + + ret = Lua.PCall(L, nargs, nresults, errorIndex); + Lua.Remove(L, errorIndex); + return ret; + } + + public void ReportError(string errorDesc) + { + ReportError("__FUNCTION__", errorDesc); + } + + public static void ReportError(string function, string errorDesc, bool stackTrace = false) + { + int scriptId, callbackId; + bool timerEvent; + LuaScriptInterface scriptInterface; + + GetScriptEnv().GetEventInfo(out scriptId, out scriptInterface, out callbackId, out timerEvent); + + //Logger.Error("Lua script error: \nscriptInterface: [{0}]\nscriptId: [{1}]\ntimerEvent: [{2}]\n callbackId:[{3}]\nfunction: [{4}]\nerror [{5}]", + // scriptInterface != null ? scriptInterface.GetInterfaceName() : "", + // scriptId != 0 ? scriptInterface.GetFileById(scriptId) : "", + // timerEvent ? "in a timer event called from:" : "", + // callbackId != 0 ? scriptInterface.GetFileById(callbackId) : "", + // function ?? "", + // (stackTrace && scriptInterface != null) ? scriptInterface.GetStackTrace(errorDesc) : errorDesc); + + Console.WriteLine(string.Format("Lua script error: \nscriptInterface: [{0}]\nscriptId: [{1}]\ntimerEvent: [{2}]\n callbackId:[{3}]\nfunction: [{4}]\nerror [{5}]", + scriptInterface != null ? scriptInterface.GetInterfaceName() : "", + scriptId != 0 ? scriptInterface.GetFileById(scriptId) : "", + timerEvent ? "in a timer event called from:" : "", + callbackId != 0 ? scriptInterface.GetFileById(callbackId) : "", + function ?? "", + (stackTrace && scriptInterface != null) ? scriptInterface.GetStackTrace(errorDesc) : errorDesc)); + } + + public static int LuaErrorHandler(LuaState L) + { + string errorMessage = PopString(L); + LuaScriptInterface scriptInterface = GetScriptEnv().GetScriptInterface(); + Debug.Assert(scriptInterface != null); // This fires if the ScriptEnvironment hasn't been set up + PushString(L, scriptInterface.GetStackTrace(errorMessage)); + return 1; + } + + public static void PushVariant(LuaState L, LuaVariant var) + { + if (ValidateDispatcherContext(nameof(PushVariant))) + { + return; + } + + Lua.CreateTable(L, 0, 4); + SetField(L, "type", (double)var.Type); + + switch (var.Type) + { + case LuaVariantType.VARIANT_NUMBER: + SetField(L, "number", var.Number); + break; + case LuaVariantType.VARIANT_STRING: + SetField(L, "string", var.Text); + break; + case LuaVariantType.VARIANT_TARGETPOSITION: + case LuaVariantType.VARIANT_POSITION: + { + //TODO: Muniz + //PushPosition(L, var.Po); + //Lua.SetField(L, -2, "pos"); + break; + } + default: + break; + } + + SetField(L, "instantName", var.InstantName); + SetField(L, "runeName", var.RuneName); + SetMetatable(L, -1, "Variant"); + } + + public static void PushThing(LuaState L, IThing thing) + { + if (ValidateDispatcherContext(nameof(PushThing))) + { + return; + } + + if (thing == null) + { + Lua.CreateTable(L, 0, 4); + SetField(L, "uid", 0); + SetField(L, "itemid", 0); + SetField(L, "actionid", 0); + SetField(L, "type", 0); + return; + } + + if (thing is ItemLua item) + { + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + } + else if (thing is ICreature creature) + { + PushUserdata(L, creature); + SetCreatureMetatable(L, -1, creature); + } + else + { + Lua.PushNil(L); + } + } + + //public static void PushCylinder(LuaState L, Cylinder cylinder) + //{ + // if (ValidateDispatcherContext(nameof(PushCylinder))) + // { + // return; + // } + + // if (cylinder.GetCreature() is Creature creature) + // { + // PushUserdata(L, creature); + // SetCreatureMetatable(L, -1, creature); + // } + // else if (cylinder.GetItem() is Item parentItem) + // { + // PushUserdata(L, parentItem); + // SetItemMetatable(L, -1, parentItem); + // } + // else if (cylinder.GetTile() is Tile tile) + // { + // PushUserdata(L, tile); + // SetMetatable(L, -1, "Tile"); + // } + // else if (cylinder == VirtualCylinder.VirtualCylinder) + // { + // PushBoolean(L, true); + // } + // else + // { + // Lua.PushNil(L); + // } + //} + + public static void PushString(LuaState L, string value) + { + if (ValidateDispatcherContext(nameof(PushString))) + { + return; + } + + Lua.PushLString(L, value, (ulong)value.Length); + } + + public static void PushCallback(LuaState L, int callback) + { + if (ValidateDispatcherContext(nameof(PushCallback))) + { + return; + } + + Lua.RawGetI(L, LUA_REGISTRYINDEX, callback); + } + + public static string PopString(LuaState L) + { + if (Lua.GetTop(L) == 0) + { + return string.Empty; + } + + string str = GetString(L, -1); + Lua.Pop(L, 1); + return str; + } + + public static int PopCallback(LuaState L) + { + return Lua.Ref(L, LUA_REGISTRYINDEX); + } + + // Metatables + public static void SetMetatable(LuaState L, int index, string name) + { + if (ValidateDispatcherContext(nameof(SetMetatable))) + { + return; + } + + Lua.GetMetaTable(L, name); + Lua.SetMetaTable(L, index - 1); + } + + public static void SetWeakMetatable(LuaState L, int index, string name) + { + HashSet weakObjectTypes = new HashSet(); + + if (ValidateDispatcherContext(nameof(SetWeakMetatable))) + { + return; + } + + string weakName = name + "_weak"; + + if (weakObjectTypes.Add(name)) + { + Lua.GetMetaTable(L, name); + int childMetatable = Lua.GetTop(L); + + Lua.NewMetaTable(L, weakName); + int metatable = Lua.GetTop(L); + + List methodKeys = new List { "__index", "__metatable", "__eq" }; + foreach (string metaKey in methodKeys) + { + Lua.GetField(L, childMetatable, metaKey); + Lua.SetField(L, metatable, metaKey); + } + + List methodIndexes = new List { 'h', 'p', 't' }; + foreach (int metaIndex in methodIndexes) + { + Lua.RawGetI(L, childMetatable, metaIndex); + Lua.RawSetI(L, metatable, metaIndex); + } + + Lua.PushNil(L); + Lua.SetField(L, metatable, "__gc"); + + Lua.Remove(L, childMetatable); + } + else + { + Lua.GetMetaTable(L, weakName); + } + + Lua.SetMetaTable(L, index - 1); + } + + public static void SetItemMetatable(LuaState L, int index, ItemLua item) + { + if (ValidateDispatcherContext(nameof(SetItemMetatable))) + { + return; + } + + //if (item != null && item.GetContainer() != null) + //{ + // Lua.GetMetaTable(L, "Container"); + //} + //else if (item != null && item.GetTeleport() != null) + //{ + // Lua.GetMetaTable(L, "Teleport"); + //} + //else + //{ + Lua.GetMetaTable(L, "Item"); + //} + + Lua.SetMetaTable(L, index - 1); + } + + public static void SetCreatureMetatable(LuaState L, int index, ICreature creature) + { + if (ValidateDispatcherContext(nameof(SetCreatureMetatable))) + { + return; + } + + if (creature != null && creature is IPlayer) + { + Lua.GetMetaTable(L, "Player"); + } + else if (creature != null && creature is IMonster) + { + Lua.GetMetaTable(L, "Monster"); + } + else + { + Lua.GetMetaTable(L, "Npc"); + } + + Lua.SetMetaTable(L, index - 1); + } + + //public static CombatDamage GetCombatDamage(LuaState L) + //{ + // CombatDamage damage = new CombatDamage + // { + // Primary = { Value = GetNumber(L, -4), Type = GetNumber(L, -3) }, + // Secondary = { Value = GetNumber(L, -2), Type = GetNumber(L, -1) } + // }; + + // Lua.Pop(L, 4); + // return damage; + //} + + public static string GetFormatedLoggerMessage(LuaState L) + { + string format = GetString(L, 1); + int n = Lua.GetTop(L); + var args = new List(); + + for (int i = 2; i <= n; i++) + { + if (IsString(L, i)) + { + args.Add(Lua.ToString(L, i)); + } + else if (IsNumber(L, i)) + { + args.Add(Lua.ToNumber(L, i)); + } + else if (IsBoolean(L, i)) + { + args.Add(Lua.ToBoolean(L, i) ? "true" : "false"); + } + else if (IsUserdata(L, i)) + { + LuaDataType userType = GetUserdataType(L, i); + args.Add(GetUserdataTypeName(userType)); + } + else if (IsTable(L, i)) + { + args.Add("table"); + } + else if (IsNil(L, i)) + { + args.Add("nil"); + } + else if (IsFunction(L, i)) + { + args.Add("function"); + } + else + { + //g_logger().warn("[{}] invalid param type", nameof(GetFormatedLoggerMessage)); + Console.WriteLine("[{0}] invalid param type", nameof(GetFormatedLoggerMessage)); + } + } + + try + { + List indexedArguments = args.Select((arg, index) => $"{{{index}}}").ToList(); + string formattedMessage = string.Format(format, indexedArguments.ToArray()); + return formattedMessage; + //return fmt.vformat(format, args); + } + catch (Exception e) + { + //g_logger().error("[{}] format error: {}", nameof(GetFormatedLoggerMessage), e.what()); + } + + return string.Empty; + } + + public static string GetString(LuaState L, int arg) + { + ulong len = 0; + var c_str = Lua.ToLString(L, arg, ref len); + if (c_str == null || len == 0) + { + return ""; + } + return c_str; + } + + public static PositionLua GetPosition(LuaState L, int arg, out int stackpos) + { + PositionLua position = new PositionLua + { + x = GetField(L, arg, "x"), + y = GetField(L, arg, "y"), + z = GetField(L, arg, "z") + }; + + Lua.GetField(L, arg, "stackpos"); + if (Lua.IsNil(L, -1)) + { + stackpos = 0; + } + else + { + stackpos = GetNumber(L, -1); + } + + Lua.Pop(L, 4); + return position; + } + + public static PositionLua GetPosition(LuaState L, int arg) + { + PositionLua position = new PositionLua + { + x = GetField(L, arg, "x"), + y = GetField(L, arg, "y"), + z = GetField(L, arg, "z") + }; + + Lua.Pop(L, 3); + return position; + } + + //public static Outfit_t GetOutfit(LuaState L, int arg) + //{ + // Outfit_t outfit = new Outfit_t + // { + // lookMountFeet = GetField(L, arg, "lookMountFeet"), + // lookMountLegs = GetField(L, arg, "lookMountLegs"), + // lookMountBody = GetField(L, arg, "lookMountBody"), + // lookMountHead = GetField(L, arg, "lookMountHead"), + // lookFamiliarsType = GetField(L, arg, "lookFamiliarsType"), + // lookMount = GetField(L, arg, "lookMount"), + // lookAddons = GetField(L, arg, "lookAddons"), + + // lookFeet = GetField(L, arg, "lookFeet"), + // lookLegs = GetField(L, arg, "lookLegs"), + // lookBody = GetField(L, arg, "lookBody"), + // lookHead = GetField(L, arg, "lookHead"), + + // lookTypeEx = GetField(L, arg, "lookTypeEx"), + // lookType = GetField(L, arg, "lookType") + // }; + + // Lua.Pop(L, 13); + // return outfit; + //} + + public static LuaVariant GetVariant(LuaState L, int arg) + { + LuaVariant var = new LuaVariant + { + InstantName = GetFieldString(L, arg, "instantName"), + RuneName = GetFieldString(L, arg, "runeName"), + Type = GetField(L, arg, "type") + }; + + switch (var.Type) + { + case LuaVariantType.VARIANT_NUMBER: + var.Number = GetField(L, arg, "number"); + Lua.Pop(L, 4); + break; + + case LuaVariantType.VARIANT_STRING: + var.Text = GetFieldString(L, arg, "string"); + Lua.Pop(L, 4); + break; + + case LuaVariantType.VARIANT_POSITION: + case LuaVariantType.VARIANT_TARGETPOSITION: + Lua.GetField(L, arg, "pos"); + //TODO: MUNIZ + //var.Pos = GetPosition(L, Lua.GetTop(L)); + Lua.Pop(L, 4); + break; + + default: + var.Type = LuaVariantType.VARIANT_NONE; + Lua.Pop(L, 3); + break; + } + + return var; + } + + //public static Thing GetThing(LuaState L, int arg) + //{ + // Thing thing; + // if (Lua.GetMetaTable(L, arg) != 0) + // { + // Lua.RawGetI(L, -1, 't'); + // switch (GetNumber(L, -1)) + // { + // case LuaDataType.Item: + // thing = GetUserdataShared(L, arg); + // break; + // case LuaDataType.Container: + // thing = GetUserdataShared(L, arg); + // break; + // case LuaDataType.Teleport: + // thing = GetUserdataShared(L, arg); + // break; + // case LuaDataType.Player: + // thing = GetUserdataShared(L, arg); + // break; + // case LuaDataType.Monster: + // thing = GetUserdataShared(L, arg); + // break; + // case LuaDataType.Npc: + // thing = GetUserdataShared(L, arg); + // break; + // default: + // thing = null; + // break; + // } + // Lua.Pop(L, 2); + // } + // else + // { + // thing = GetScriptEnv().GetThingByUID(GetNumber(L, arg)); + // } + // return thing; + //} + + //public static shared_ptr GetCreature(LuaState L, int arg) + //{ + // if (IsUserdata(L, arg)) + // { + // return GetUserdataShared(L, arg); + // } + // return g_game().GetCreatureByID(GetNumber(L, arg)); + //} + + //public static shared_ptr GetPlayer(LuaState L, int arg, bool allowOffline = false) + //{ + // if (IsUserdata(L, arg)) + // { + // return GetUserdataShared(L, arg); + // } + // else if (IsNumber(L, arg)) + // { + // return g_game().GetPlayerByID(GetNumber(L, arg), allowOffline); + // } + // else if (IsString(L, arg)) + // { + // return g_game().GetPlayerByName(GetString(L, arg), allowOffline); + // } + // g_logger().Warn("LuaFunctionsLoader::GetPlayer: Invalid argument."); + // return null; + //} + + //public static shared_ptr GetGuild(LuaState L, int arg, bool allowOffline = false) + //{ + // if (IsUserdata(L, arg)) + // { + // return GetUserdataShared(L, arg); + // } + // else if (IsNumber(L, arg)) + // { + // return g_game().GetGuild(GetNumber(L, arg), allowOffline); + // } + // else if (IsString(L, arg)) + // { + // return g_game().GetGuildByName(GetString(L, arg), allowOffline); + // } + // g_logger().Warn("LuaFunctionsLoader::GetGuild: Invalid argument."); + // return null; + //} + + public static string GetFieldString(LuaState L, int arg, string key) + { + Lua.GetField(L, arg, key); + return GetString(L, -1); + } + + public static LuaDataType GetUserdataType(LuaState L, int arg) + { + if (Lua.GetMetaTable(L, arg) == 0) + { + return LuaDataType.Unknown; + } + Lua.RawGetI(L, -1, 't'); + + LuaDataType type = GetNumber(L, -1); + Lua.Pop(L, 2); + + return type; + } + + public static string GetUserdataTypeName(LuaDataType userType) + { + //return magic_enum::enum_name(userType).data(); + return userType.ToString(); + } + + // Push + public static void PushBoolean(LuaState L, bool value) + { + if (ValidateDispatcherContext(nameof(PushBoolean))) + { + return; + } + + Lua.PushBoolean(L, value); + } + + //public static void PushCombatDamage(LuaState L, CombatDamage damage) + //{ + // if (ValidateDispatcherContext(nameof(PushCombatDamage))) + // { + // return; + // } + + // Lua.PushNumber(L, damage.primary.value); + // Lua.PushNumber(L, damage.primary.type); + // Lua.PushNumber(L, damage.secondary.value); + // Lua.PushNumber(L, damage.secondary.type); + // Lua.PushNumber(L, damage.origin); + //} + + //public static void PushInstantSpell(LuaState L, InstantSpell spell) + //{ + // if (ValidateDispatcherContext(__FUNCTION__)) + // { + // return; + // } + + // Lua.CreateTable(L, 0, 6); + + // SetField(L, "name", spell.GetName()); + // SetField(L, "words", spell.GetWords()); + // SetField(L, "level", spell.GetLevel()); + // SetField(L, "mlevel", spell.GetMagicLevel()); + // SetField(L, "mana", spell.GetMana()); + // SetField(L, "manapercent", spell.GetManaPercent()); + + // SetMetatable(L, -1, "Spell"); + //} + + public static void PushPosition(LuaState L, PositionLua position, int stackpos = 0) + { + if (ValidateDispatcherContext(nameof(PushPosition))) + { + return; + } + + Lua.CreateTable(L, 0, 4); + + SetField(L, "x", position.x); + SetField(L, "y", position.y); + SetField(L, "z", position.z); + SetField(L, "stackpos", stackpos); + + SetMetatable(L, -1, "Position"); + } + + //public static void PushOutfit(LuaState L, Outfit_t outfit) + //{ + // if (ValidateDispatcherContext(__FUNCTION__)) + // { + // return; + // } + + // Lua.CreateTable(L, 0, 13); + // SetField(L, "lookType", outfit.lookType); + // SetField(L, "lookTypeEx", outfit.lookTypeEx); + // SetField(L, "lookHead", outfit.lookHead); + // SetField(L, "lookBody", outfit.lookBody); + // SetField(L, "lookLegs", outfit.lookLegs); + // SetField(L, "lookFeet", outfit.lookFeet); + // SetField(L, "lookAddons", outfit.lookAddons); + // SetField(L, "lookMount", outfit.lookMount); + // SetField(L, "lookMountHead", outfit.lookMountHead); + // SetField(L, "lookMountBody", outfit.lookMountBody); + // SetField(L, "lookMountLegs", outfit.lookMountLegs); + // SetField(L, "lookMountFeet", outfit.lookMountFeet); + // SetField(L, "lookFamiliarsType", outfit.lookFamiliarsType); + //} + + public static void RegisterClass(LuaState L, string className, string baseClass, LuaFunction newFunction = null) + { + // className = {} + Lua.NewTable(L); + Lua.PushValue(L, -1); + Lua.SetGlobal(L, className); + int methods = Lua.GetTop(L); + + // methodsTable = {} + Lua.NewTable(L); + int methodsTable = Lua.GetTop(L); + + if (newFunction != null) + { + // className.__call = newFunction + Lua.PushCFunction(L, newFunction); + Lua.SetField(L, methodsTable, "__call"); + } + + uint parents = 0; + if (!string.IsNullOrEmpty(baseClass)) + { + Lua.GetGlobal(L, baseClass); + Lua.RawGetI(L, -1, 'p'); + parents = GetNumber(L, -1) + 1; + Lua.Pop(L, 1); + Lua.SetField(L, methodsTable, "__index"); + } + + // setmetatable(className, methodsTable) + Lua.SetMetaTable(L, methods); + + // className.metatable = {} + Lua.NewMetaTable(L, className); + int metatable = Lua.GetTop(L); + + // className.metatable.__metatable = className + Lua.PushValue(L, methods); + Lua.SetField(L, metatable, "__metatable"); + + // className.metatable.__index = className + Lua.PushValue(L, methods); + Lua.SetField(L, metatable, "__index"); + + // className.metatable['h'] = hash + Lua.PushNumber(L, (double)className.GetHashCode()); + Lua.RawSetI(L, metatable, 'h'); + + // className.metatable['p'] = parents + Lua.PushNumber(L, parents); + Lua.RawSetI(L, metatable, 'p'); + + // className.metatable['t'] = type + Enum.TryParse(className, true, out var userTypeEnum); + if (userTypeEnum != null) + { + Lua.PushNumber(L, (double)userTypeEnum); + } + else + { + Lua.PushNumber(L, (double)LuaDataType.Unknown); + } + Lua.RawSetI(L, metatable, 't'); + + // pop className, className.metatable + Lua.Pop(L, 2); + } + + public static void RegisterMethod(LuaState L, string globalName, string methodName, LuaFunction func) + { + // globalName.methodName = func + Lua.GetGlobal(L, globalName); + Lua.PushCFunction(L, func); + Lua.SetField(L, -2, methodName); + + // pop globalName + Lua.Pop(L, 1); + } + + public static void RegisterTable(LuaState L, string tableName) + { + // _G[tableName] = {} + Lua.NewTable(L); + Lua.SetGlobal(L, tableName); + } + + public static void RegisterMetaMethod(LuaState L, string className, string methodName, LuaFunction func) + { + // className.metatable.methodName = func + Lua.GetMetaTable(L, className); + Lua.PushCFunction(L, func); + Lua.SetField(L, -2, methodName); + + // pop className.metatable + Lua.Pop(L, 1); + } + + public static void RegisterVariable(LuaState L, string tableName, string name, double value) + { + // tableName.name = value + Lua.GetGlobal(L, tableName); + SetField(L, name, value); + + // pop tableName + Lua.Pop(L, 1); + } + + public static void RegisterVariable(LuaState L, string tableName, string name, string value) + { + // tableName.name = value + Lua.GetGlobal(L, tableName); + SetField(L, name, value); + + // pop tableName + Lua.Pop(L, 1); + } + public static void RegisterVariable(LuaState L, string tableName, string name, BooleanConfigType value) + { + RegisterVariable(L, tableName, name, (double)value); + } + + public static void RegisterVariable(LuaState L, string tableName, string name, StringConfigType value) + { + RegisterVariable(L, tableName, name, (double)value); + } + + public static void RegisterVariable(LuaState L, string tableName, string name, IntegerConfigType value) + { + RegisterVariable(L, tableName, name, (double)value); + } + + public static void RegisterVariable(LuaState L, string tableName, string name, FloatingConfigType value) + { + RegisterVariable(L, tableName, name, (double)value); + } + + public static void RegisterGlobalBoolean(LuaState L, string name, bool value) + { + // _G[name] = value + PushBoolean(L, value); + Lua.SetGlobal(L, name); + } + + public static void RegisterGlobalMethod(LuaState L, string functionName, LuaFunction func) + { + // _G[functionName] = func + Lua.PushCFunction(L, func); + Lua.SetGlobal(L, functionName); + } + + public static void RegisterGlobalVariable(LuaState L, string name, double value) + { + // _G[name] = value + Lua.PushNumber(L, value); + Lua.SetGlobal(L, name); + } + + public static void RegisterGlobalVariable(LuaState L, string name, ReloadType value) + { + // _G[name] = value + RegisterGlobalVariable(L, name, (double)value); + } + + public static void RegisterGlobalVariable(LuaState L, string name, MessageClassesType value) + { + // _G[name] = value + RegisterGlobalVariable(L, name, (double)value); + } + + public static void RegisterGlobalString(LuaState L, string variable, string name) + { + // Example: RegisterGlobalString(L, "VARIABLE_NAME", "variable string"); + PushString(L, name); + Lua.SetGlobal(L, variable); + } + + public static string EscapeString(string str) + { + string s = str; + s.Replace("\\", "\\\\"); + s.Replace("\"", "\\\""); + s.Replace("'", "\\'"); + s.Replace("[[", "\\[["); + return s; + } + + //public static int LuaUserdataCompare(LuaState L) + //{ + // PushBoolean(L, GetUserdata(L, 1) == GetUserdata(L, 2)); + // return 1; + //} + + + public static int LuaUserdataCompare(LuaState L) where T : class + { + PushBoolean(L, GetUserdata(L, 1) == GetUserdata(L, 2)); + return 1; + } + + public static void RegisterSharedClass(LuaState L, string className, string baseClass, LuaFunction newFunction) + { + RegisterClass(L, className, baseClass, newFunction); + RegisterMetaMethod(L, className, "__gc", LuaGarbageCollection); + } + + public static int LuaGarbageCollection(LuaState L) + { + try + { + IntPtr userdata = (IntPtr)Lua.ToUserData(L, 1); + var stru = (UserDataStruct)Marshal.PtrToStructure(Marshal.ReadIntPtr(userdata), typeof(UserDataStruct)); + + if (_objects.ContainsKey(stru.Index)) + { + var obj = _objects[stru.Index]; + _objects.Remove(stru.Index); + _objectsBackMap.Remove(obj); + } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + } + catch (Exception e) + { + Logger.GetInstance().Error($"Exception in GarbageCollection: {e.InnerException}"); + } + + return 0; + } + + public static bool ValidateDispatcherContext(string fncName) + { + //if (g_dispatcher().context().isOn() && g_dispatcher().context().isAsync()) + //{ + // g_logger().warn("[{}] The call to lua was ignored because the '{}' task is trying to communicate while in async mode.", fncName, g_dispatcher().context().getName()); + // return LUA_ERRRUN > 0; + //} + + //return 0; + + return false; + } + + private static int scriptEnvIndex = 0; + private static ScriptEnvironment[] scriptEnv = new ScriptEnvironment[16]; + + //public static void pushUserdata(LuaState L, T value) where T : class + //{ + // IntPtr userdata = (nint)Lua.NewUserData(L, (uint)IntPtr.Size); + // System.Runtime.InteropServices.Marshal.WriteIntPtr(userdata, System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0)); + //} + + public static T GetNumber(LuaState L, int arg) where T : struct + { + if (typeof(T).IsEnum) + { + return (T)Enum.ToObject(typeof(T), (long)Lua.ToNumber(L, arg)); + } + else if (typeof(T).IsPrimitive) + { + return (T)Convert.ChangeType(Lua.ToNumber(L, arg), typeof(T)); + } + else + { + throw new NotSupportedException($"Type {typeof(T)} is not supported."); + } + } + + public static T GetNumber(LuaState L, int arg, T defaultValue) where T : struct + { + int parameters = Lua.GetTop(L); + if (parameters == 0 || arg > parameters) + { + return defaultValue; + } + return GetNumber(L, arg); + } + + public static T GetUserdataShared(LuaState L, int arg) where T : struct + { + IntPtr userdata = (IntPtr)Lua.ToUserData(L, arg); + + if (userdata == IntPtr.Zero) + { + return default(T); + } + + IntPtr ptr = Marshal.ReadIntPtr(userdata); + return (T)Marshal.PtrToStructure(ptr, typeof(T)); + } + + public enum LuaType + { + /// + /// + /// + None = -1, + /// + /// LUA_TNIL + /// + Nil = 0, + /// + /// LUA_TBOOLEAN + /// + Boolean = 1, + /// + /// LUA_TLIGHTUSERDATA + /// + LightUserData = 2, + /// + /// LUA_TNUMBER + /// + Number = 3, + /// + /// LUA_TSTRING + /// + String = 4, + /// + /// LUA_TTABLE + /// + Table = 5, + /// + /// LUA_TFUNCTION + /// + Function = 6, + /// + /// LUA_TUSERDATA + /// + UserData = 7, + /// + /// LUA_TTHREAD + /// + /// // + Thread = 8, + } + + public static int ToNetObject(LuaState state, int index, IntPtr tag) + { + //if (state.Type(index) != LuaType.UserData) + // return -1; + + IntPtr userData; + + if (Lua.GetMetaTable(state, index) != 0) + { + userData = (IntPtr)Lua.ToUserData(state, index); + if (userData != IntPtr.Zero) + return Marshal.ReadInt32(userData); + } + + //userData = state.CheckUData(index, "luaNet_class"); + //if (userData != IntPtr.Zero) + // return Marshal.ReadInt32(userData); + + //userData = state.CheckUData(index, "luaNet_searchbase"); + //if (userData != IntPtr.Zero) + // return Marshal.ReadInt32(userData); + + //userData = state.CheckUData(index, "luaNet_function"); + //if (userData != IntPtr.Zero) + // return Marshal.ReadInt32(userData); + + return -1; + } + + public static T GetUserdata(LuaState L, int arg) where T : class + { + var userdata = GetRawUserdata(L, arg); + if (userdata == IntPtr.Zero) + { + return null; + } + + var stru = (UserDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(UserDataStruct)); + + return (T)_objects[stru.Index]; + + //var type = (LuaType)Lua.Type(L, arg); + + //switch (type) + //{ + // case LuaType.UserData: + // { + // int udata = ToNetObject(L, arg, 0); + // return (T)_objects[udata]; + // //return udata != -1 ? _objects[udata] : GetUserData(luaState, index); + // } + // default: + // return null; + //} + + //if (_userData.TryGetValue(typeof(T), out var @object)) + // return (T)@object; + + //return null; + + //IntPtr userdata = GetRawUserdata(L, arg); + //if (userdata == IntPtr.Zero) + //{ + // return null; + //} + //return (T)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(T)); + } + + //public static void RemoveUserdata() where T : class + //{ + // var key = typeof(T); + // if (_userData.ContainsKey(key)) + // _userData.Remove(key); + + // //IntPtr userdata = GetRawUserdata(L, arg); + // //if (userdata == IntPtr.Zero) + // //{ + // // return null; + // //} + // //return (T)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(T)); + //} + + public static IntPtr GetRawUserdata(LuaState L, int arg) where T : class + { + return (nint)Lua.ToUserData(L, arg); + } + + public static bool GetBoolean(LuaState L, int arg) + { + return Lua.ToBoolean(L, arg); + } + + public static bool GetBoolean(LuaState L, int arg, bool defaultValue) + { + int parameters = Lua.GetTop(L); + if (parameters == 0 || arg > parameters) + { + return defaultValue; + } + return Lua.ToBoolean(L, arg); + } + + public static string GetString(LuaState L, int arg, string defaultValue) + { + int parameters = Lua.GetTop(L); + if (parameters == 0 || arg > parameters) + { + return defaultValue; + } + return GetString(L, arg); + } + + public static T GetField(LuaState L, int arg, string key) where T : struct + { + Lua.GetField(L, arg, key); + return GetNumber(L, -1); + } + + public static bool IsNumber(LuaState L, int arg) + { + return Lua.Type(L, arg) == LuaNET.LuaType.Number; + } + + public static bool IsString(LuaState L, int arg) + { + return Lua.IsString(L, arg); + } + + public static bool IsBoolean(LuaState L, int arg) + { + return Lua.IsBoolean(L, arg); + } + + public static bool IsTable(LuaState L, int arg) + { + return Lua.IsTable(L, arg); + } + + public static bool IsFunction(LuaState L, int arg) + { + return Lua.IsFunction(L, arg); + } + + public static bool IsNil(LuaState L, int arg) + { + return Lua.IsNil(L, arg); + } + + public static bool IsUserdata(LuaState L, int arg) + { + return Lua.IsUserData(L, arg); + } + + public static void SetField(LuaState L, string index, double value) + { + Lua.PushNumber(L, value); + Lua.SetField(L, -2, index); + } + + public static void SetField(LuaState L, string index, string value) + { + PushString(L, value); + Lua.SetField(L, -2, index); + } + + public LuaFunctionsLoader() + { + for (int i = 0; i < scriptEnv.Length; i++) + { + scriptEnv[i] = new ScriptEnvironment(); + } + } + + public ScriptEnvironment InternalGetScriptEnv() + { + if (scriptEnvIndex < 0 || scriptEnvIndex >= 16) + { + throw new IndexOutOfRangeException(); + } + return scriptEnv[scriptEnvIndex]; + } + + public bool InternalReserveScriptEnv() + { + return ++scriptEnvIndex < 16; + } + + public static ScriptEnvironment GetScriptEnv() + { + if (scriptEnvIndex < 0 || scriptEnvIndex >= 16) + { + throw new IndexOutOfRangeException(); + } + return scriptEnv[scriptEnvIndex]; + } + + public static bool ReserveScriptEnv() + { + return ++scriptEnvIndex < 16; + } + + public static void ResetScriptEnv() + { + if (scriptEnvIndex < 0) + { + throw new IndexOutOfRangeException(); + } + scriptEnv[scriptEnvIndex--].ResetEnv(); + } + + /// + /// Compatibility NewIndexedUserData with constant parameter + /// + /// + /// + public static IntPtr NewUserData(LuaState L, int size) + { + return (IntPtr)Lua.NewUserData(L, (UIntPtr)size); + } + + public static void NewUData(LuaState L, int val) + { + IntPtr pointer = NewUserData(L, Marshal.SizeOf(typeof(int))); + Marshal.WriteInt32(pointer, val); + } + + public static T ToObject(LuaState L, int index, bool freeGCHandle = true) + { + //if (IsNil(index) || !IsLightUserData(index)) + // return default(T); + + IntPtr data = (IntPtr)Lua.ToUserData(L, index); + if (data == IntPtr.Zero) + return default(T); + + var handle = GCHandle.FromIntPtr(data); + if (!handle.IsAllocated) + return default(T); + + var reference = (T)handle.Target; + + if (freeGCHandle) + handle.Free(); + + return reference; + } + + public static void PushUserdata2(LuaState L, T value, int a) where T : class + { + IntPtr ptr = IntPtr.Zero; + try + { + IntPtr userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); + + ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); + Marshal.WriteIntPtr(userdata, ptr); + } + catch (Exception ex) + { + + } + finally + { + if (ptr != IntPtr.Zero) + { + try + { + Marshal.FreeHGlobal(ptr); + } + catch (Exception ex) + { + } + } + } + } + + public static void PushUserdata(LuaState L, T value, int a) where T : struct + { + IntPtr ptr = IntPtr.Zero; + try + { + //IntPtr userdata2 = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); + + //ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); + //Marshal.WriteIntPtr(userdata2, ptr); + + //TextWriter tw = Console.Out; + //GCHandle gch = GCHandle.Alloc(tw); + GCHandle gch2 = GCHandle.Alloc(value, GCHandleType.Pinned); + //gch.Free(); + + IntPtr userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); + + //var a = GCHandle.Alloc(value, GCHandleType.Pinned); + + //fixed (T* aptr = &value) + //{ + // Console.WriteLine(*aptr); + //} + + //ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); + //ptr = GCHandle.Alloc(value).AddrOfPinnedObject(); + Marshal.WriteIntPtr(userdata, gch2.AddrOfPinnedObject()); + //Marshal.WriteIntPtr(userdata, ptr); + gch2.Free(); + } + catch (Exception ex) + { + + } + finally + { + if (ptr != IntPtr.Zero) + { + try + { + //Marshal.FreeHGlobal(ptr); + } + catch (Exception ex) + { + } + } + } + } + + // Compare cache entries by exact reference to avoid unwanted aliases + private class ReferenceComparer : IEqualityComparer + { + public new bool Equals(object x, object y) + { + if (x != null && y != null && x.GetType() == y.GetType() && x.GetType().IsValueType && y.GetType().IsValueType) + return x.Equals(y); // Special case for boxed value types + return ReferenceEquals(x, y); + } + + public int GetHashCode(object obj) + { + return obj.GetHashCode(); + } + } + + // object to object # + static readonly Dictionary _objectsBackMap = new Dictionary(new ReferenceComparer()); + // object # to object (FIXME - it should be possible to get object address as an object #) + static readonly Dictionary _objects = new Dictionary(); + static readonly List _structuresToReuse = new List(); + + static readonly ConcurrentQueue finalizedReferences = new ConcurrentQueue(); + + //internal EventHandlerContainer PendingEvents = new EventHandlerContainer(); + + /// + /// We want to ensure that objects always have a unique ID + /// + static int _nextObj; + + private static int AddObject(object obj) + { + // New object: inserts it in the list + int index = _nextObj++; + _objects[index] = obj; + + if (!obj.GetType().IsValueType || obj.GetType().IsEnum) + _objectsBackMap[obj] = index; + + return index; + } + + public static void PushUserdata(LuaState L, object o) + { + int index = -1; + + // Pushes nil + if (o == null) + { + Lua.PushNil(L); + return; + } + + // Object already in the list of Lua objects? Push the stored reference. + bool found = (!o.GetType().IsValueType || o.GetType().IsEnum) && _objectsBackMap.TryGetValue(o, out index); + + index = AddObject(o); + + IntPtr userdata; + + UserDataStruct stru; + + if (_structuresToReuse.Any()) + { + stru = _structuresToReuse.FirstOrDefault(); + _structuresToReuse.Remove(stru); + + stru.Index = index; + userdata = stru.Ptr; + } + else + { + userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); + + // Registre o userdata no registry Lua + //Lua.PushValue(L, -1); // Duplicar o userdata + //int referenceIndex = Lua.Ref(L, LUA_REGISTRYINDEX); + + stru = new UserDataStruct(index, userdata, 0); + } + + GCHandle gch2 = GCHandle.Alloc(stru, GCHandleType.Pinned); + + Marshal.WriteIntPtr(userdata, gch2.AddrOfPinnedObject()); + gch2.Free(); + + //lua_setupvalue + + //var obj1 = _objects[index]; + //var obj2 = _objects[userdata]; + + //IntPtr pointer = (IntPtr)Lua.NewUserData(L, (ulong)Marshal.SizeOf(typeof(int))); + //Marshal.WriteInt32(pointer, index); + + //_userData.Add(typeof(T), value); + + //IntPtr ptr = IntPtr.Zero; + //try + //{ + // IntPtr userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); + + // ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); + // Marshal.WriteIntPtr(userdata, ptr); + //} + //catch(Exception ex) + //{ + + //} + //finally + //{ + // if (ptr != IntPtr.Zero) + // { + // try + // { + // Marshal.FreeHGlobal(ptr); + // } + // catch (Exception ex) + // { + // } + // } + //} + } + + //public static void PushUserdata(LuaState L, TalkAction value) + //{ + // var userdata = Lua.NewUserData(L, (ulong)IntPtr.Size); + // Marshal.StructureToPtr(value, (nint)userdata, false); + + // //System.Runtime.InteropServices.Marshal.StructureToPtr(value, userData, false); + // //var userData = (IntPtr)Lua.NewUserData(L, sizeof(T)); + //} + + //public static void PushUserdata(LuaState L, T value) where T : class + //{ + // var userdata = Lua.NewUserData(L, (ulong)IntPtr.Size); + // //Marshal.StructureToPtr(value, userdata, false); + + // //System.Runtime.InteropServices.Marshal.StructureToPtr(value, userData, false); + // //var userData = (IntPtr)Lua.NewUserData(L, sizeof(T)); + //} + + //public static void PushUserdata(LuaState L, IntPtr value) + //{ + // var userData = (IntPtr)Lua.NewUserData(L, sizeof(value)); + //} + + //public static void PushString(LuaState L, string value) + //{ + // lua_pushstring(L, value); + //} +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaManager.cs new file mode 100644 index 000000000..1c3aacdc7 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaManager.cs @@ -0,0 +1,123 @@ +using LuaNET; +using NeoServer.Application.Common.Contracts.Scripts; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT; + +public class LuaManager : ILuaManager +{ + #region Members + + #endregion + + #region Injection + + /// + /// A reference to the logger instance in use. + /// + private readonly ILogger _logger; + + /// + /// A reference to the lua enviroment instance in use. + /// + private readonly ILuaEnvironment _luaEnviroment; + + /// + /// A reference to the config manager instance in use. + /// + private readonly IConfigManager _configManager; + + /// + /// A reference to the scripts instance in use. + /// + private readonly IScripts _scripts; + + private readonly ICreatureFunctions _creatureFunctions; + + private readonly ITalkActions _talkActions; + + #endregion + + #region Constructors + + public LuaManager( + ILogger logger, + ILuaEnvironment luaEnviroment, + IConfigManager configManager, + IScripts scripts, + ICreatureFunctions creatureFunctions, + ITalkActions talkActions) + { + _logger = logger; + _luaEnviroment = luaEnviroment; + _configManager = configManager; + _scripts = scripts; + _creatureFunctions = creatureFunctions; + _talkActions = talkActions; + } + + #endregion + + public ITalkAction GetTalkAction(string name) => _talkActions.GetTalkAction(name); + + public void Start() + { + var dir = AppContext.BaseDirectory; + + if (!string.IsNullOrEmpty(ArgManager.GetInstance().ExePath)) + dir = ArgManager.GetInstance().ExePath; + + ModulesLoadHelper(_luaEnviroment.InitState(), "luaEnviroment"); + + var luaState = _luaEnviroment.GetLuaState(); + + if (luaState.IsNull) + { + //Game.DieSafely("Invalid lua state, cannot load lua functions."); + Console.WriteLine("Invalid lua state, cannot load lua functions."); + } + + Lua.OpenLibs(luaState); + + //CoreFunctions.Init(L); + //CreatureFunctions.Init(L); + //EventFunctions.Init(L); + //ItemFunctions.Init(L); + //MapFunctions.Init(L); + //ZoneFunctions.Init(L); + + LoggerFunctions.Init(luaState); + ConfigFunctions.Init(luaState); + GlobalFunctions.Init(luaState); + TalkActionFunctions.Init(luaState); + + //CreatureFunctions.Init(luaState); + _creatureFunctions.Init(luaState); + + PlayerFunctions.Init(luaState); + + //GameFunctions.Init(L); + //CreatureFunctions.Init(L); + //PlayerFunctions.Init(L); + //ActionFunctions.Init(L); + //GlobalEventFunctions.Init(L); + //CreatureEventsFunctions.Init(L); + + ModulesLoadHelper(_configManager.Load($"{dir}\\config.lua"), $"config.lua"); + + ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}\\DataLuaJit/core.lua", "core.lua"), "core.lua"); + + ModulesLoadHelper(_scripts.LoadScripts($"{dir}\\DataLuaJit/scripts", false, false), "/DataLuaJit/scripts"); + } + + #region Private Methods + + private void ModulesLoadHelper(bool loaded, string moduleName) + { + _logger.Information($"Loaded {moduleName}"); + if (!loaded) + _logger.Error(string.Format("Cannot load: {0}", moduleName)); + } + + #endregion +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs new file mode 100644 index 000000000..600f390d9 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs @@ -0,0 +1,325 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; + +public class LuaScriptInterface : LuaFunctionsLoader, ILuaScriptInterface +{ + public static int EVENT_ID_LOADING = 1; + public static int EVENT_ID_USER = 1000; + + protected int eventTableRef; + protected LuaState luaState; + protected int runningEventId = EVENT_ID_USER; + + private string lastLuaError; + private string interfaceName; + private string loadingFile; + private string loadedScriptName; + + protected Dictionary cacheFiles; + + private LuaEnvironment g_luaEnvironment() => LuaEnvironment.GetInstance(); + + public LuaScriptInterface(string initInterfaceName) + { + interfaceName = initInterfaceName; + } + + ~LuaScriptInterface() + { + CloseState(); + } + + public bool ReInitState() + { + //g_luaEnvironment().ClearCombatObjects(this); + //g_luaEnvironment().ClearAreaObjects(this); + + CloseState(); + return InitState(); + } + + public bool LoadFile(string file, string scriptName) + { + int ret = Lua.LoadFile(luaState, file); + if (ret != 0) + { + lastLuaError = PopString(luaState); + return false; + } + + if (!IsFunction(luaState, -1)) + { + return false; + } + + loadingFile = file; + + SetLoadingScriptName(scriptName); + + if (!ReserveScriptEnv()) + { + return false; + } + + ScriptEnvironment env = GetScriptEnv(); + env.SetScriptId(EVENT_ID_LOADING, this); + + ret = ProtectedCall(luaState, 0, 0); + if (ret != 0) + { + ReportError(null, PopString(luaState)); + ResetScriptEnv(); + return false; + } + + ResetScriptEnv(); + return true; + } + + public int GetEvent(string eventName) + { + Lua.RawGetI(luaState, LUA_REGISTRYINDEX, eventTableRef); + if (!IsTable(luaState, -1)) + { + Lua.Pop(luaState, 1); + return -1; + } + + Lua.GetGlobal(luaState, eventName); + if (!IsFunction(luaState, -1)) + { + Lua.Pop(luaState, 2); + return -1; + } + + Lua.PushValue(luaState, -1); + Lua.RawSetI(luaState, -3, runningEventId); + Lua.Pop(luaState, 2); + + Lua.PushNil(luaState); + Lua.SetGlobal(luaState, eventName); + + cacheFiles[runningEventId] = loadingFile + ":" + eventName; + return runningEventId++; + } + + public int GetEvent() + { + if (!IsFunction(luaState, -1)) + { + return -1; + } + + Lua.RawGetI(luaState, LUA_REGISTRYINDEX, eventTableRef); + if (!IsTable(luaState, -1)) + { + Lua.Pop(luaState, 1); + return -1; + } + + Lua.PushValue(luaState, -2); + Lua.RawSetI(luaState, -2, runningEventId); + Lua.Pop(luaState, 2); + + cacheFiles[runningEventId] = loadingFile + ":callback"; + return runningEventId++; + } + + public int GetMetaEvent(string globalName, string eventName) + { + Lua.RawGetI(luaState, LUA_REGISTRYINDEX, eventTableRef); + if (!IsTable(luaState, -1)) + { + Lua.Pop(luaState, 1); + return -1; + } + + Lua.GetGlobal(luaState, globalName); + Lua.GetField(luaState, -1, eventName); + if (!IsFunction(luaState, -1)) + { + Lua.Pop(luaState, 3); + return -1; + } + + Lua.PushValue(luaState, -1); + Lua.RawSetI(luaState, -4, runningEventId); + Lua.Pop(luaState, 1); + + Lua.PushNil(luaState); + Lua.SetField(luaState, -2, eventName); + Lua.Pop(luaState, 2); + + cacheFiles[runningEventId] = loadingFile + ":" + globalName + "@" + eventName; + return runningEventId++; + } + + public string GetFileById(int scriptId) + { + if (scriptId == EVENT_ID_LOADING) + { + return loadingFile; + } + + if (cacheFiles.TryGetValue(scriptId, out var file)) + { + return file; + } + + return "(Unknown scriptfile)"; + } + + public string GetStackTrace(string errorDesc) + { + Lua.GetGlobal(luaState, "debug"); + if (!IsTable(luaState, -1)) + { + Lua.Pop(luaState, 1); + return errorDesc; + } + + Lua.GetField(luaState, -1, "traceback"); + if (!IsFunction(luaState, -1)) + { + Lua.Pop(luaState, 2); + return errorDesc; + } + + Lua.Replace(luaState, -2); + PushString(luaState, errorDesc); + Lua.Call(luaState, 1, 1); + return PopString(luaState); + } + + public bool PushFunction(int functionId) + { + Lua.RawGetI(luaState, LUA_REGISTRYINDEX, eventTableRef); + if (!IsTable(luaState, -1)) + { + return false; + } + + Lua.RawGetI(luaState, -1, functionId); + Lua.Replace(luaState, -2); + return IsFunction(luaState, -1); + } + + public bool InitState() + { + luaState = g_luaEnvironment().GetLuaState(); + if (luaState.IsNull) + { + return false; + } + + Lua.OpenLibs(luaState); + + Lua.NewTable(luaState); + eventTableRef = Lua.Ref(luaState, LUA_REGISTRYINDEX); + runningEventId = EVENT_ID_USER; + + cacheFiles = new Dictionary(); + + return true; + } + + public bool CloseState() + { + if (LuaEnvironment.IsShuttingDown()) + { + luaState.pointer = 0; + } + + if (luaState.IsNull || g_luaEnvironment().GetLuaState().pointer == 0) + { + return false; + } + + cacheFiles.Clear(); + if (eventTableRef != -1) + { + Lua.UnRef(luaState, LUA_REGISTRYINDEX, eventTableRef); + eventTableRef = -1; + } + + luaState.pointer = 0; + return true; + } + + public bool CallFunction(int parameters) + { + bool result = false; + int size = Lua.GetTop(luaState); + if (ProtectedCall(luaState, parameters, 1) != 0) + { + LuaScriptInterface.ReportError(null, LuaScriptInterface.GetString(luaState, -1)); + } + else + { + result = LuaScriptInterface.GetBoolean(luaState, -1); + } + + Lua.Pop(luaState, 1); + if ((Lua.GetTop(luaState) + parameters + 1) != size) + { + LuaScriptInterface.ReportError(null, "Stack size changed!"); + } + + ResetScriptEnv(); + return result; + } + + public void CallVoidFunction(int parameters) + { + int size = Lua.GetTop(luaState); + if (ProtectedCall(luaState, parameters, 0) != 0) + { + LuaScriptInterface.ReportError(null, LuaScriptInterface.PopString(luaState)); + } + + if ((Lua.GetTop(luaState) + parameters + 1) != size) + { + LuaScriptInterface.ReportError(null, "Stack size changed!"); + } + + ResetScriptEnv(); + } + + public string GetInterfaceName() + { + return interfaceName; + } + + public string GetLastLuaError() + { + return lastLuaError; + } + + public string GetLoadingFile() + { + return loadingFile; + } + + public string GetLoadingScriptName() + { + // If scripty name is empty, return warning informing + if (string.IsNullOrEmpty(loadedScriptName)) + { + //g_logger().warn("[LuaScriptInterface::getLoadingScriptName] - Script name is empty"); + Console.WriteLine("[LuaScriptInterface::getLoadingScriptName] - Script name is empty"); + } + + return loadedScriptName; + } + + public void SetLoadingScriptName(string scriptName) + { + loadedScriptName = scriptName; + } + + public virtual LuaState GetLuaState() + { + return luaState; + } +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj new file mode 100644 index 000000000..4303335c2 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj @@ -0,0 +1,70 @@ + + + + net9.0 + enable + enable + + + + + + + + + + + + + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + + diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/PlayerFunctions.cs new file mode 100644 index 000000000..fbcd3795f --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/PlayerFunctions.cs @@ -0,0 +1,157 @@ +using LuaNET; +using NeoServer.Game.Common.Chats; +using NeoServer.Game.Common.Contracts.Creatures; + +namespace NeoServer.Scripts.LuaJIT; + +public class PlayerFunctions : LuaScriptInterface +{ + public PlayerFunctions() : base(nameof(PlayerFunctions)) + { + } + + public static void Init(LuaState L) + { + RegisterSharedClass(L, "Player", "Creature", LuaPlayerCreate); + RegisterMetaMethod(L, "Player", "__eq", LuaUserdataCompare); + //RegisterMethod(L, "Player", "teleportTo", LuaTeleportTo); + + RegisterMethod(L, "Player", "sendTextMessage", LuaPlayerSendTextMessage); + } + + private static int LuaPlayerCreate(LuaState L) + { + // Player(id or guid or name or userdata) + IPlayer player = null; + if (IsNumber(L, 2)) + { + var id = GetNumber(L, 2); + //if (id >= Player::getFirstID() && id <= Player::getLastID()) + //{ + // player = g_game().getPlayerByID(id); + //} + //else + //{ + // player = g_game().getPlayerByGUID(id); + //} + } + else if (IsString(L, 2)) + { + //ReturnValue ret = g_game().getPlayerByNameWildcard(getString(L, 2), player); + //if (ret != RETURNVALUE_NOERROR) + //{ + // lua_pushnil(L); + // lua_pushnumber(L, ret); + // return 2; + //} + } + else if (IsUserdata(L, 2)) + { + //if (GetUserdataType(L, 2) != LuaData_t::Player) + //{ + // lua_pushnil(L); + // return 1; + //} + //player = GetUserdata(L, 2); + } + else + { + player = null; + } + + if (player != null) + { + PushUserdata(L, player); + SetMetatable(L, -1, "Player"); + } + else + { + Lua.PushNil(L); + } + return 1; + } + + //private static int LuaTeleportTo(LuaState L) + //{ + // // creature:teleportTo(position[, pushMovement = false]) + // bool pushMovement = GetBoolean(L, 3, false); + + // var position = GetPosition(L, 2); + + // var creature = GetUserdata(L, 1); + + // if (creature == null) + // { + // ReportError(nameof(LuaTeleportTo), GetErrorDesc(ErrorCodeType.LUA_ERROR_CREATURE_NOT_FOUND)); + // PushBoolean(L, false); + // return 1; + // } + + // var oldPosition = creature.Position; + // if (Game.GetInstance().InternalTeleport(creature, position, pushMovement)) + // { + // Logger.GetInstance().Error($"[{nameof(LuaTeleportTo)}] Failed to teleport creature {creature.Name}, on position {oldPosition}, error code: {0}");// GetReturnMessage(ret)); + // PushBoolean(L, false); + // return 1; + // } + + // PushBoolean(L, true); + // return 1; + //} + + private static int LuaPlayerSendTextMessage(LuaState L) + { + // player:sendTextMessage(type, text[, position, primaryValue = 0, primaryColor = TEXTCOLOR_NONE[, secondaryValue = 0, secondaryColor = TEXTCOLOR_NONE]]) + // player:sendTextMessage(type, text, channelId) + + var player = GetUserdata(L, 1); + if (player == null) + { + Lua.PushNil(L); + return 1; + } + + int parameters = Lua.GetTop(L); + + //TextMessage message(GetNumber(L, 2), GetString(L, 3)); + var messageType = GetNumber(L, 2); + var messageText = GetString(L, 3); + + if (parameters == 4) + { + //uint16_t channelId = getNumber(L, 4); + //const auto &channel = g_chat().getChannel(player, channelId); + //if (!channel || !channel->hasUser(player)) + //{ + // pushBoolean(L, false); + // return 1; + //} + //message.channelId = channelId; + } + else + { + //if (parameters >= 6) + //{ + // message.position = getPosition(L, 4); + // message.primary.value = getNumber(L, 5); + // message.primary.color = getNumber(L, 6); + //} + + //if (parameters >= 8) + //{ + // message.secondary.value = getNumber(L, 7); + // message.secondary.color = getNumber(L, 8); + //} + } + + //player->sendTextMessage(message); + + //Game.GetInstance().Logger.Information($"LuaPlayerSendTextMessage: player {player.Name}, message: {messageText}"); + + player.Say(messageText, SpeechType.Say); + + PushBoolean(L, true); + + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs new file mode 100644 index 000000000..50cfbc228 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs @@ -0,0 +1,97 @@ +namespace NeoServer.Scripts.LuaJIT; + +public class Script +{ + /// + /// Explicit constructor. + /// + /// Lua Script Interface + public Script(LuaScriptInterface scriptInterface) + { + _scriptInterface = scriptInterface; + } + + /// + /// Check if script is loaded. + /// + /// true if loaded, false otherwise + public bool IsLoadedCallback() + { + return LoadedCallback; + } + + /// + /// Set the loaded callback status. + /// + /// Loaded status to set + public void SetLoadedCallback(bool loaded) + { + LoadedCallback = loaded; + } + + // Load revscriptsys callback + public bool LoadCallback() + { + if (_scriptInterface == null) + { + Logger.GetInstance().Error($"[Script.LoadCallback] ScriptInterface is null, scriptId = {ScriptId}"); + return false; + } + + if (ScriptId != 0) + { + Logger.GetInstance().Error($"[Script.LoadCallback] ScriptId is not zero, scriptId = {ScriptId}, scriptName {_scriptInterface.GetLoadingScriptName()}"); + return false; + } + + int id = _scriptInterface.GetEvent(); + if (id == -1) + { + Logger.GetInstance().Error($"[Script.LoadCallback] Event {GetScriptTypeName()} not found for script with name {_scriptInterface.GetLoadingScriptName()}"); + return false; + } + + SetLoadedCallback(true); + ScriptId = id; + return true; + } + + // NOTE: Pure virtual method ( = 0) that must be implemented in derived classes + // Script type (Action, CreatureEvent, GlobalEvent, MoveEvent, Spell, Weapon) + public virtual string GetScriptTypeName() + { + return string.Empty; + } + + // Method to access the ScriptInterface in derived classes + public LuaScriptInterface GetScriptInterface() + { + return _scriptInterface; + } + + public virtual void SetScriptInterface(LuaScriptInterface newInterface) + { + _scriptInterface = newInterface; + } + + // Method to access the ScriptId in derived classes + public virtual int GetScriptId() + { + return ScriptId; + } + + public virtual void SetScriptId(int newScriptId) + { + ScriptId = newScriptId; + } + + // If script is loaded callback + private bool LoadedCallback = false; + + private int ScriptId = 0; + protected LuaScriptInterface _scriptInterface = null; + + // You may need to replace Logger.Error with the appropriate logging mechanism in your C# code. + // Additionally, consider using properties instead of public fields for encapsulation. + // The 'virtual' keyword is used to allow overriding these methods in derived classes. +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs new file mode 100644 index 000000000..5d734a470 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs @@ -0,0 +1,255 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace NeoServer.Scripts.LuaJIT; + +public class ScriptEnvironment +{ + // script file id + private int scriptId; + private int callbackId; + private bool timerEvent; + + private LuaScriptInterface luaScriptInterface; + + // local item map + //private Dictionary> localMap; + //private uint lastUID; + + // temporary item list + //private Dictionary> tempItems; + + // for npc scripts + //private Npc curNpc = nullptr; + + // result map + //private uint lastResultId; + //private Dictionary tempResults; + + public ScriptEnvironment() + { + ResetEnv(); + } + + ~ScriptEnvironment() + { + ResetEnv(); + } + + public void ResetEnv() + { + scriptId = 0; + callbackId = 0; + timerEvent = false; + luaScriptInterface = null; + //localMap.Clear(); + //tempResults.Clear(); + + //var pair = tempItems.equal_range(this); + //var it = pair.first; + //while (it != pair.second) + //{ + // std.shared_ptr item = it->second; + // it = tempItems.erase(it); + //} + } + + public void SetScriptId(int newScriptId, LuaScriptInterface newScriptInterface) + { + scriptId = newScriptId; + luaScriptInterface = newScriptInterface; + } + + public bool SetCallbackId(int newCallbackId, LuaScriptInterface scriptInterface) + { + if (callbackId != 0) + { + // nested callbacks are not allowed + if (luaScriptInterface != null) + { + luaScriptInterface.ReportError("Nested callbacks!"); + } + return false; + } + + callbackId = newCallbackId; + luaScriptInterface = scriptInterface; + return true; + } + + public int GetScriptId() { + return scriptId; + } + + public LuaScriptInterface GetScriptInterface() + { + return luaScriptInterface; + } + + public void SetTimerEvent() + { + timerEvent = true; + } + + public void GetEventInfo(out int retScriptId, out LuaScriptInterface retScriptInterface, out int retCallbackId, out bool retTimerEvent) + { + retScriptId = scriptId; + retScriptInterface = luaScriptInterface; + retCallbackId = callbackId; + retTimerEvent = timerEvent; + } + + //public uint AddThing(std.shared_ptr thing) + //{ + // if (thing == null || thing.isRemoved()) + // { + // return 0; + // } + + // std.shared_ptr creature = thing.getCreature(); + // if (creature != null) + // { + // return creature.getID(); + // } + + // std.shared_ptr item = thing.getItem(); + // if (item != null && item.hasAttribute(ItemAttribute_t.UNIQUEID)) + // { + // return item.getAttribute(ItemAttribute_t.UNIQUEID); + // } + + // foreach (var it in localMap) + // { + // if (it.Value == item) + // { + // return it.Key; + // } + // } + + // localMap[++lastUID] = item; + // return lastUID; + //} + + //public void InsertItem(uint uid, std.shared_ptr item) + //{ + // var result = localMap.TryAdd(uid, item); + // if (!result) + // { + // g_logger().error("Thing uid already taken: {}", uid); + // } + //} + + //public std.shared_ptr GetThingByUID(uint uid) + //{ + // if (uid >= 0x10000000) + // { + // return g_game().getCreatureByID(uid); + // } + + // if (uid <= std.numeric_limits.max()) + // { + // std.shared_ptr item = g_game().getUniqueItem(static_cast(uid)); + // if (item != null && !item.isRemoved()) + // { + // return item; + // } + // return null; + // } + + // if (localMap.TryGetValue(uid, out std.shared_ptr item)) + // { + // if (!item.isRemoved()) + // { + // return item; + // } + // } + // return null; + //} + + //public std.shared_ptr GetItemByUID(uint uid) + //{ + // std.shared_ptr thing = getThingByUID(uid); + // if (thing == null) + // { + // return null; + // } + // return thing.getItem(); + //} + + //public std.shared_ptr GetContainerByUID(uint uid) + //{ + // std.shared_ptr item = getItemByUID(uid); + // if (item == null) + // { + // return null; + // } + // return item.getContainer(); + //} + + //public void RemoveItemByUID(uint uid) + //{ + // if (uid <= std.numeric_limits.max()) + // { + // g_game().removeUniqueItem(static_cast(uid)); + // return; + // } + + // localMap.TryRemove(uid, out var _); + //} + + //public void AddTempItem(std.shared_ptr item) + //{ + // tempItems.Add(this, item); + //} + + //public void RemoveTempItem(std.shared_ptr item) + //{ + // foreach (var it in tempItems) + // { + // if (it.Value == item) + // { + // tempItems.Remove(it.Key); + // break; + // } + // } + //} + + //public uint AddResult(DBResult_ptr res) + //{ + // tempResults[++lastResultId] = res; + // return lastResultId; + //} + + //public bool RemoveResult(uint id) + //{ + // if (tempResults.TryGetValue(id, out var _)) + // { + // tempResults.Remove(id); + // return true; + // } + // return false; + //} + + //public DBResult_ptr GetResultByID(uint id) + //{ + // if (tempResults.TryGetValue(id, out var result)) + // { + // return result; + // } + // return null; + //} + + // public void SetNpc(Npc npc) + // { + // curNpc = npc; + // } + + // public Npc GetNpc() { + // return curNpc; + //} +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs new file mode 100644 index 000000000..52549c84d --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs @@ -0,0 +1,202 @@ +using Serilog; +using System; + +namespace NeoServer.Scripts.LuaJIT; + +public class Scripts : IScripts +{ + #region Instance + + private static Scripts _instance = null; + public static Scripts GetInstance() => _instance == null ? _instance = new Scripts() : _instance; + + #endregion + + #region Members + + private int _scriptId = 0; + private LuaScriptInterface _scriptInterface; + + #endregion + + #region Injection + + /// + /// A reference to the logger in use. + /// + private readonly ILogger _logger; + + /// + /// A reference to the config manager in use. + /// + private readonly IConfigManager _configManager; + + /// + /// A reference to the talk actions instance in use. + /// + private readonly ITalkActions _talkActions; + + #endregion + + public Scripts() + { + _logger = new LoggerConfiguration() + .WriteTo.Console() + .CreateLogger(); + + _scriptInterface = new LuaScriptInterface("Scripts Interface"); + _scriptInterface.InitState(); + } + + public Scripts( + ILogger logger, + IConfigManager configManager, + ITalkActions talkActions) + { + _instance = this; + + _logger = logger.ForContext(); + _configManager = configManager; + _talkActions = talkActions; + + _scriptInterface = new LuaScriptInterface("Scripts Interface"); + //_scriptInterface.InitState(); + } + + public void ClearAllScripts() + { + _talkActions.Clear(); + //_actions.Clear(); + //_globalEvents.Clear(); + //_creatureEvents.Clear(); + //gSpells.Clear(); + //gMoveEvents.Clear(); + //gWeapons.Clear(); + //gCallbacks.Clear(); + //gMonsters.Clear(); + } + + public bool LoadEventSchedulerScripts(string fileName) + { + var coreFolder = _configManager.GetString(StringConfigType.CORE_DIRECTORY); + + var dir = Directory.GetCurrentDirectory(); + + if (!string.IsNullOrEmpty(ArgManager.GetInstance().ExePath)) + dir = ArgManager.GetInstance().ExePath; + + dir = Path.Combine(dir, coreFolder, "events", "scripts", "scheduler"); + + if (!Directory.Exists(dir) || !Directory.GetDirectories(dir).Any()) + { + _logger.Warning($"{nameof(LoadEventSchedulerScripts)} - Can not load folder 'scheduler' on {coreFolder}/events/scripts'"); + return false; + } + + foreach (var filePath in Directory.GetFiles(dir, "*.lua", SearchOption.AllDirectories)) + { + var fileInfo = new FileInfo(filePath); + + if (fileInfo.Name == fileName) + { + if (!_scriptInterface.LoadFile(fileInfo.FullName, fileInfo.Name)) + { + _logger.Error(fileInfo.FullName); + _logger.Error(_scriptInterface.GetLastLuaError()); + continue; + } + + return true; + } + } + + return false; + } + + public bool LoadScripts(string loadPath, bool isLib, bool reload) + { + if (_scriptInterface.GetLuaState().pointer == 0) + _scriptInterface.InitState(); + + var dir = Directory.GetCurrentDirectory(); + + if (!string.IsNullOrEmpty(ArgManager.GetInstance().ExePath)) + dir = ArgManager.GetInstance().ExePath; + + dir = Path.Combine(dir, loadPath); + + if (!Directory.Exists(dir) || !Directory.GetDirectories(dir).Any()) + { + _logger.Error($"Can not load folder {loadPath}"); + return false; + } + + string lastDirectory = null; + + foreach (var filePath in Directory.GetFiles(dir, "*.lua", SearchOption.AllDirectories)) + { + var fileInfo = new FileInfo(filePath); + var fileFolder = fileInfo.DirectoryName?.Split(Path.DirectorySeparatorChar).LastOrDefault(); + var scriptFolder = fileInfo.DirectoryName; + + if (!File.Exists(filePath) || fileInfo.Extension != ".lua") + { + continue; + } + + if (fileInfo.Name.StartsWith("#")) + { + if (_configManager.GetBoolean(BooleanConfigType.SCRIPTS_CONSOLE_LOGS)) + { + _logger.Information("[script]: {} [disabled]", fileInfo.Name); + } + + continue; + } + + if (isLib || (fileFolder != "lib" && fileFolder != "events")) + { + if (_configManager.GetBoolean(BooleanConfigType.SCRIPTS_CONSOLE_LOGS)) + { + if (string.IsNullOrEmpty(lastDirectory) || lastDirectory != scriptFolder) + { + _logger.Information($"Loading folder: [{fileInfo.DirectoryName.Split(Path.DirectorySeparatorChar).LastOrDefault()}]"); + } + + lastDirectory = fileInfo.DirectoryName; + } + + if (!_scriptInterface.LoadFile(fileInfo.FullName, fileInfo.Name)) + { + _logger.Error(fileInfo.FullName); + _logger.Error(_scriptInterface.GetLastLuaError()); + continue; + } + } + + if (_configManager.GetBoolean(BooleanConfigType.SCRIPTS_CONSOLE_LOGS)) + { + if (!reload) + { + _logger.Information("[script loaded]: {0}", fileInfo.Name); + } + else + { + _logger.Information("[script reloaded]: {0}", fileInfo.Name); + } + } + } + + return true; + } + + public LuaScriptInterface GetScriptInterface() + { + return _scriptInterface; + } + + public int GetScriptId() + { + return _scriptId; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/UserDataStruct.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/UserDataStruct.cs new file mode 100644 index 000000000..971ca27c3 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/UserDataStruct.cs @@ -0,0 +1,8 @@ +namespace NeoServer.Scripts.LuaJIT; + +public struct UserDataStruct(int index, IntPtr ptr, int referenceIndex) +{ + public int Index { get; set; } = index; + public IntPtr Ptr { get; set; } = ptr; + public int ReferenceIndex { get; set; } = referenceIndex; +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs new file mode 100644 index 000000000..5942c9efe --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs @@ -0,0 +1,83 @@ +using NeoServer.Application.Common.Contracts.Scripts; +using NeoServer.Game.Common.Chats; +using NeoServer.Game.Common.Contracts.Creatures; + +namespace NeoServer.Scripts.LuaJIT; + +public class TalkAction : Script, ITalkAction +{ + private string words; + private string separator = "\""; + //private account.GroupType groupType = account.GroupType.GROUP_TYPE_NONE; + + public TalkAction(LuaScriptInterface context) : base(context) { } + + public string GetWords() + { + return words; + } + + public void SetWords(List newWords) + { + foreach (var word in newWords) + { + if (!string.IsNullOrEmpty(words)) + { + words += ", "; + } + words += word; + } + } + + public string GetSeparator() + { + return separator; + } + + public void SetSeparator(string sep) + { + separator = sep; + } + + public bool ExecuteSay(IPlayer player, string words, string param, SpeechType type) + { + // onSay(player, words, param, type) + if (!GetScriptInterface().InternalReserveScriptEnv()) + { + Console.WriteLine($"[TalkAction::ExecuteSay - Player {player.Name} words {GetWords()}] " + + $"Call stack overflow. Too many lua script calls being nested. Script name {GetScriptInterface().GetLoadingScriptName()}"); + return false; + } + + var scriptInterface = GetScriptInterface(); + var scriptEnvironment = scriptInterface.InternalGetScriptEnv(); + scriptEnvironment.SetScriptId(GetScriptId(), GetScriptInterface()); + + var L = GetScriptInterface().GetLuaState(); + GetScriptInterface().PushFunction(GetScriptId()); + + LuaScriptInterface.PushUserdata(L, player); + LuaScriptInterface.SetMetatable(L, -1, "Player"); + + LuaScriptInterface.PushString(L, words); + LuaScriptInterface.PushString(L, param); + //lua_pushnumber(L, (double)type); + + return GetScriptInterface().CallFunction(3); + } + + //public void SetGroupType(account.GroupType newGroupType) + //{ + // groupType = newGroupType; + //} + + //public account.GroupType GetGroupType() + //{ + // return groupType; + //} + + public override string GetScriptTypeName() + { + return "onSay"; + } +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActionFunctions.cs new file mode 100644 index 000000000..50474106a --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActionFunctions.cs @@ -0,0 +1,220 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT; + +public class TalkActionFunctions : LuaScriptInterface +{ + public TalkActionFunctions() : base(nameof(TalkActionFunctions)) + { + } + + public static void Init(LuaState L) + { + RegisterSharedClass(L, "TalkAction", "", LuaCreateTalkAction); + RegisterMethod(L, "TalkAction", "onSay", LuaTalkActionOnSay); + //RegisterMethod(L, "TalkAction", "groupType", LuaTalkActionGroupType); + RegisterMethod(L, "TalkAction", "register", LuaTalkActionRegister); + RegisterMethod(L, "TalkAction", "separator", LuaTalkActionSeparator); + //RegisterMethod(L, "TalkAction", "getName", LuaTalkActionGetName); + //RegisterMethod(L, "TalkAction", "getGroupType", LuaTalkActionGetGroupType); + } + + private static int LuaCreateTalkAction(LuaState L) + { + // TalkAction(words) or TalkAction(word1, word2, word3) + var wordsVector = new List(); + for (var i = 2; i <= Lua.GetTop(L); i++) + { + wordsVector.Add(GetString(L, i)); + } + + var talkActionSharedPtr = new TalkAction(GetScriptEnv().GetScriptInterface()); + //var talkActionSharedPtr = new TalkAction(); + talkActionSharedPtr.SetWords(wordsVector); + + ////IntPtr unmanagedAddr = Marshal.AllocHGlobal(Marshal.SizeOf(talkActionSharedPtr)); + ////Marshal.StructureToPtr(talkActionSharedPtr, unmanagedAddr, true); + + //var talkAction = new Structs.TalkAction(GetScriptEnv().GetScriptInterface()); + //var talkAction = new Structs.TalkAction(); + //talkAction.SetWords(wordsVector); + + PushUserdata(L, talkActionSharedPtr); + //PushUserdata(L, talkAction, 1); + //PushUserdata(L, test, 1); + + //var teste = GetUserdataShared(L, 3, 1); + + SetMetatable(L, -1, "TalkAction"); + //SetWeakMetatable(L, -1, "TalkAction"); + + return 1; + } + + private static int LuaTalkActionOnSay(LuaState L) + { + // talkAction:onSay(callback) + + var talkActionSharedPtr = GetUserdata(L, 1); + + if (talkActionSharedPtr == null) + { + ReportError(nameof(LuaTalkActionOnSay), GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); + PushBoolean(L, false); + return 1; + } + + if (!talkActionSharedPtr.LoadCallback()) + { + PushBoolean(L, false); + return 1; + } + + PushBoolean(L, true); + return 1; + } + + //private static int LuaTalkActionGroupType(lua_State L) + //{ + // // talkAction:groupType(GroupType = GROUP_TYPE_NORMAL) + // //var talkActionSharedPtr = GetUserdata(L, 1); + // //if (talkActionSharedPtr == null) + // //{ + // // ReportError(GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); + // // PushBoolean(L, false); + // // return 1; + // //} + + // //var groupType = Account.GroupType.None; + + // //var type = Lua.Type(L, 2); + // //if (type == LuaType.Number) + // //{ + // // groupType = (Account.GroupType)GetNumber(L, 2); + // //} + // //else if (type == LuaType.String) + // //{ + // // var strValue = GetString(L, 2); + // // switch (strValue.ToLower()) + // // { + // // case "normal": + // // groupType = Account.GroupType.Normal; + // // break; + // // case "tutor": + // // groupType = Account.GroupType.Tutor; + // // break; + // // case "seniortutor": + // // case "senior tutor": + // // groupType = Account.GroupType.SeniorTutor; + // // break; + // // case "gamemaster": + // // case "gm": + // // groupType = Account.GroupType.GameMaster; + // // break; + // // case "communitymanager": + // // case "cm": + // // case "community manager": + // // groupType = Account.GroupType.CommunityManager; + // // break; + // // case "god": + // // groupType = Account.GroupType.God; + // // break; + // // default: + // // var errorString = $"Invalid group type string value {strValue} for group type for script: {GetScriptEnv().GetScriptInterface().GetLoadingScriptName()}"; + // // ReportError(errorString); + // // PushBoolean(L, false); + // // return 1; + // // } + // //} + // //else + // //{ + // // var errorString = $"Expected number or string value for group type for script: {GetScriptEnv().GetScriptInterface().GetLoadingScriptName()}"; + // // ReportError(errorString); + // // PushBoolean(L, false); + // // return 1; + // //} + + // //talkActionSharedPtr.SetGroupType(groupType); + // //PushBoolean(L, true); + // return 1; + //} + + private static int LuaTalkActionRegister(LuaState L) + { + // talkAction:register() + var talkActionSharedPtr = GetUserdata(L, 1); + if (talkActionSharedPtr == null) + { + ReportError(nameof(LuaTalkActionRegister), GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); + PushBoolean(L, false); + return 1; + } + + if (!talkActionSharedPtr.IsLoadedCallback()) + { + PushBoolean(L, false); + return 1; + } + + //if (talkActionSharedPtr.GroupType == Account.GroupType.None) + //{ + // var errorString = $"TalkAction with name {talkActionSharedPtr.Words} does not have groupType"; + // ReportError(errorString); + // PushBoolean(L, false); + // return 1; + //} + + PushBoolean(L, TalkActions.GetInstance().RegisterLuaEvent(talkActionSharedPtr)); + //RemoveUserdata(); + + //talkActionSharedPtr.ExecuteSay(Game.GetInstance().GetCurrentPlayer(), "!help", "", SpeakClassesType.TALKTYPE_SAY); + + return 1; + } + + private static int LuaTalkActionSeparator(LuaState L) + { + // talkAction:separator(sep) + var talkActionSharedPtr = GetUserdata(L, 1); + if (talkActionSharedPtr == null) + { + ReportError(nameof(LuaTalkActionSeparator), GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); + PushBoolean(L, false); + return 1; + } + + talkActionSharedPtr.SetSeparator(GetString(L, 2)); + PushBoolean(L, true); + return 1; + } + + private static int LuaTalkActionGetName(LuaState L) + { + // local name = talkAction:getName() + var talkActionSharedPtr = GetUserdata(L, 1); + if (talkActionSharedPtr == null) + { + ReportError(nameof(LuaTalkActionGetName), GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); + PushBoolean(L, false); + return 1; + } + + PushString(L, talkActionSharedPtr.GetWords()); + return 1; + } + + //private static int LuaTalkActionGetGroupType(lua_State L) + //{ + // // local groupType = talkAction:getGroupType() + // var talkActionSharedPtr = GetUserdata(L, 1); + // if (talkActionSharedPtr == null) + // { + // ReportError(GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); + // PushBoolean(L, false); + // return 1; + // } + + // lua_pushnumber(L, (double)talkActionSharedPtr.GroupType); + // return 1; + //} +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs new file mode 100644 index 000000000..cbf7f82a0 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs @@ -0,0 +1,130 @@ +using NeoServer.Game.Common.Chats; +using NeoServer.Game.Common.Contracts.Creatures; + +namespace NeoServer.Scripts.LuaJIT; + +public class TalkActions : ITalkActions +{ + #region Injection + + private static TalkActions _instance; + public static TalkActions GetInstance() => _instance == null ? _instance = new TalkActions() : _instance; + + #endregion + + #region Members + + public readonly Dictionary _talkActions = new Dictionary(); + + #endregion + + #region Constructors + + public TalkActions() + { + _instance = this; + } + + #endregion + + #region Public Methods + + public bool CheckWord(IPlayer player, SpeechType type, string words, string word, TalkAction talkActionPtr) + { + var spacePos = words.IndexOfAny(new[] { ' ', '\t', '\n', '\r' }); + string firstWord = spacePos != -1 ? words.Substring(0, spacePos) : words; + + // Check for exact equality from saying word and talkaction stored word + if (firstWord != word) + { + return false; + } + + //var groupId = player.Group.Id; + //if (groupId < talkActionPtr.GroupType) + //{ + // return false; + //} + + string param = string.Empty; + int wordPos = words.IndexOf(word); + int talkactionLength = word.Length; + if (wordPos != -1 && wordPos + talkactionLength < words.Length) + { + param = words.Substring(wordPos + talkactionLength); + param = param.TrimStart(' '); + } + + string separator = talkActionPtr.GetSeparator(); + if (separator != " ") + { + if (!string.IsNullOrEmpty(param)) + { + if (param != separator) + { + return false; + } + else + { + param = param.Substring(1); + } + } + } + + return talkActionPtr.ExecuteSay(player, words, param, type); + } + + public TalkActionResultType CheckPlayerCanSayTalkAction(IPlayer player, SpeechType type, string words) + { + foreach (var talkActionPair in _talkActions) + { + var talkactionWords = talkActionPair.Key; + var talkActionPtr = talkActionPair.Value; + + if (talkactionWords.Contains(',')) + { + var wordsList = talkactionWords.Split(','); + foreach (var word in wordsList) + { + if (CheckWord(player, type, words, word.Trim(), talkActionPtr)) + { + return TalkActionResultType.TALKACTION_BREAK; + } + } + } + else + { + if (CheckWord(player, type, words, talkactionWords, talkActionPtr)) + { + return TalkActionResultType.TALKACTION_BREAK; + } + } + } + + return TalkActionResultType.TALKACTION_CONTINUE; + } + + public bool RegisterLuaEvent(TalkAction talkAction) + { + if (!_talkActions.ContainsKey(talkAction.GetWords())) + { + _talkActions.Add(talkAction.GetWords(), talkAction); + return true; + } + + return false; + } + + public void Clear() + { + _talkActions.Clear(); + } + + public TalkAction GetTalkAction(string word) + { + _talkActions.TryGetValue(word, out var talkAction); + return talkAction; + } + + #endregion +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs new file mode 100644 index 000000000..397cde577 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs @@ -0,0 +1,778 @@ +namespace NeoServer.Scripts.LuaJIT; + +// Enums +public enum IconsType +{ + ICON_POISON = 1 << 0, + ICON_BURN = 1 << 1, + ICON_ENERGY = 1 << 2, + ICON_DRUNK = 1 << 3, + ICON_MANASHIELD = 1 << 4, + ICON_PARALYZE = 1 << 5, + ICON_HASTE = 1 << 6, + ICON_SWORDS = 1 << 7, + ICON_DROWNING = 1 << 8, + ICON_FREEZING = 1 << 9, + ICON_DAZZLED = 1 << 10, + ICON_CURSED = 1 << 11, + ICON_PARTY_BUFF = 1 << 12, + ICON_REDSWORDS = 1 << 13, + ICON_PIGEON = 1 << 14, + ICON_BLEEDING = 1 << 15, + ICON_LESSERHEX = 1 << 16, + ICON_INTENSEHEX = 1 << 17, + ICON_GREATERHEX = 1 << 18, + ICON_ROOTED = 1 << 19, + ICON_FEARED = 1 << 20, + ICON_GOSHNAR1 = 1 << 21, + ICON_GOSHNAR2 = 1 << 22, + ICON_GOSHNAR3 = 1 << 23, + ICON_GOSHNAR4 = 1 << 24, + ICON_GOSHNAR5 = 1 << 25, + ICON_NEWMANASHIELD = 1 << 26, +}; + +public enum WieldInfoType +{ + WIELDINFO_NONE = 0, + WIELDINFO_LEVEL = 1 << 0, + WIELDINFO_MAGLV = 1 << 1, + WIELDINFO_VOCREQ = 1 << 2, + WIELDINFO_PREMIUM = 1 << 3, +}; + +public enum SpawnTypeType +{ + RESPAWN_IN_ALL = 0, + RESPAWN_IN_DAY = 1, + RESPAWN_IN_NIGHT = 2, + RESPAWN_IN_DAY_CAVE = 3, + RESPAWN_IN_NIGHT_CAVE = 4, +}; + +public enum Cipbia_ElementalsType : byte +{ + CIPBIA_ELEMENTAL_PHYSICAL = 0, + CIPBIA_ELEMENTAL_FIRE = 1, + CIPBIA_ELEMENTAL_EARTH = 2, + CIPBIA_ELEMENTAL_ENERGY = 3, + CIPBIA_ELEMENTAL_ICE = 4, + CIPBIA_ELEMENTAL_HOLY = 5, + CIPBIA_ELEMENTAL_DEATH = 6, + CIPBIA_ELEMENTAL_HEALING = 7, + CIPBIA_ELEMENTAL_DROWN = 8, + CIPBIA_ELEMENTAL_LIFEDRAIN = 9, + CIPBIA_ELEMENTAL_MANADRAIN = 10, + CIPBIA_ELEMENTAL_AGONY = 11, + CIPBIA_ELEMENTAL_UNDEFINED = 12, +}; + +public enum MagicEffectClasses : ushort +{ + CONST_ME_NONE, + + CONST_ME_DRAWBLOOD = 1, + CONST_ME_LOSEENERGY = 2, + CONST_ME_POFF = 3, + CONST_ME_BLOCKHIT = 4, + CONST_ME_EXPLOSIONAREA = 5, + CONST_ME_EXPLOSIONHIT = 6, + CONST_ME_FIREAREA = 7, + CONST_ME_YELLOW_RINGS = 8, + CONST_ME_GREEN_RINGS = 9, + CONST_ME_HITAREA = 10, + CONST_ME_TELEPORT = 11, + CONST_ME_ENERGYHIT = 12, + CONST_ME_MAGIC_BLUE = 13, + CONST_ME_MAGIC_RED = 14, + CONST_ME_MAGIC_GREEN = 15, + CONST_ME_HITBYFIRE = 16, + CONST_ME_HITBYPOISON = 17, + CONST_ME_MORTAREA = 18, + CONST_ME_SOUND_GREEN = 19, + CONST_ME_SOUND_RED = 20, + CONST_ME_POISONAREA = 21, + CONST_ME_SOUND_YELLOW = 22, + CONST_ME_SOUND_PURPLE = 23, + CONST_ME_SOUND_BLUE = 24, + CONST_ME_SOUND_WHITE = 25, + CONST_ME_BUBBLES = 26, + CONST_ME_CRAPS = 27, + CONST_ME_GIFT_WRAPS = 28, + CONST_ME_FIREWORK_YELLOW = 29, + CONST_ME_FIREWORK_RED = 30, + CONST_ME_FIREWORK_BLUE = 31, + CONST_ME_STUN = 32, + CONST_ME_SLEEP = 33, + CONST_ME_WATERCREATURE = 34, + CONST_ME_GROUNDSHAKER = 35, + CONST_ME_HEARTS = 36, + CONST_ME_FIREATTACK = 37, + CONST_ME_ENERGYAREA = 38, + CONST_ME_SMALLCLOUDS = 39, + CONST_ME_HOLYDAMAGE = 40, + CONST_ME_BIGCLOUDS = 41, + CONST_ME_ICEAREA = 42, + CONST_ME_ICETORNADO = 43, + CONST_ME_ICEATTACK = 44, + CONST_ME_STONES = 45, + CONST_ME_SMALLPLANTS = 46, + CONST_ME_CARNIPHILA = 47, + CONST_ME_PURPLEENERGY = 48, + CONST_ME_YELLOWENERGY = 49, + CONST_ME_HOLYAREA = 50, + CONST_ME_BIGPLANTS = 51, + CONST_ME_CAKE = 52, + CONST_ME_GIANTICE = 53, + CONST_ME_WATERSPLASH = 54, + CONST_ME_PLANTATTACK = 55, + CONST_ME_TUTORIALARROW = 56, + CONST_ME_TUTORIALSQUARE = 57, + CONST_ME_MIRRORHORIZONTAL = 58, + CONST_ME_MIRRORVERTICAL = 59, + CONST_ME_SKULLHORIZONTAL = 60, + CONST_ME_SKULLVERTICAL = 61, + CONST_ME_ASSASSIN = 62, + CONST_ME_STEPSHORIZONTAL = 63, + CONST_ME_BLOODYSTEPS = 64, + CONST_ME_STEPSVERTICAL = 65, + CONST_ME_YALAHARIGHOST = 66, + CONST_ME_BATS = 67, + CONST_ME_SMOKE = 68, + CONST_ME_INSECTS = 69, + CONST_ME_DRAGONHEAD = 70, + CONST_ME_ORCSHAMAN = 71, + CONST_ME_ORCSHAMAN_FIRE = 72, + CONST_ME_THUNDER = 73, + CONST_ME_FERUMBRAS = 74, + CONST_ME_CONFETTI_HORIZONTAL = 75, + CONST_ME_CONFETTI_VERTICAL = 76, + // 77-157 are empty + CONST_ME_BLACKSMOKE = 158, + // 159-166 are empty + CONST_ME_REDSMOKE = 167, + CONST_ME_YELLOWSMOKE = 168, + CONST_ME_GREENSMOKE = 169, + CONST_ME_PURPLESMOKE = 170, + CONST_ME_EARLY_THUNDER = 171, + CONST_ME_RAGIAZ_BONECAPSULE = 172, + CONST_ME_CRITICAL_DAMAGE = 173, + // 174 is empty + CONST_ME_PLUNGING_FISH = 175, + CONST_ME_BLUE_ENERGY_SPARK = 176, + CONST_ME_ORANGE_ENERGY_SPARK = 177, + CONST_ME_GREEN_ENERGY_SPARK = 178, + CONST_ME_PINK_ENERGY_SPARK = 179, + CONST_ME_WHITE_ENERGY_SPARK = 180, + CONST_ME_YELLOW_ENERGY_SPARK = 181, + CONST_ME_MAGIC_POWDER = 182, + // 183 is empty + CONST_ME_PIXIE_EXPLOSION = 184, + CONST_ME_PIXIE_COMING = 185, + CONST_ME_PIXIE_GOING = 186, + // 187 is empty + CONST_ME_STORM = 188, + CONST_ME_STONE_STORM = 189, + // 190 is empty + CONST_ME_BLUE_GHOST = 191, + // 192 is empty + CONST_ME_PINK_VORTEX = 193, + CONST_ME_TREASURE_MAP = 194, + CONST_ME_PINK_BEAM = 195, + CONST_ME_GREEN_FIREWORKS = 196, + CONST_ME_ORANGE_FIREWORKS = 197, + CONST_ME_PINK_FIREWORKS = 198, + CONST_ME_BLUE_FIREWORKS = 199, + // 200 is empty + CONST_ME_SUPREME_CUBE = 201, + CONST_ME_BLACK_BLOOD = 202, + CONST_ME_PRISMATIC_SPARK = 203, + CONST_ME_THAIAN = 204, + CONST_ME_THAIAN_GHOST = 205, + CONST_ME_GHOST_SMOKE = 206, + // 207 is empty + CONST_ME_WATER_BLOCK_FLOATING = 208, + CONST_ME_WATER_BLOCK = 209, + CONST_ME_ROOTS = 210, + // 211-212 is empty + CONST_ME_GHOSTLY_SCRATCH = 213, + CONST_ME_GHOSTLY_BITE = 214, + CONST_ME_BIG_SCRATCH = 215, + CONST_ME_SLASH = 216, + CONST_ME_BITE = 217, + // 218 empty or debug + CONST_ME_CHIVALRIOUS_CHALLENGE = 219, + CONST_ME_DIVINE_DAZZLE = 220, + CONST_ME_ELECTRICALSPARK = 221, + CONST_ME_PURPLETELEPORT = 222, + CONST_ME_REDTELEPORT = 223, + CONST_ME_ORANGETELEPORT = 224, + CONST_ME_GREYTELEPORT = 225, + CONST_ME_LIGHTBLUETELEPORT = 226, + // 227-229 are empty + CONST_ME_FATAL = 230, + CONST_ME_DODGE = 231, + CONST_ME_HOURGLASS = 232, + CONST_ME_DAZZLING = 233, + CONST_ME_SPARKLING = 234, + CONST_ME_FERUMBRAS_1 = 235, + CONST_ME_GAZHARAGOTH = 236, + CONST_ME_MAD_MAGE = 237, + CONST_ME_HORESTIS = 238, + CONST_ME_DEVOVORGA = 239, + CONST_ME_FERUMBRAS_2 = 240, + + CONST_ME_WHITE_SMOKE = 241, + CONST_ME_WHITE_SMOKES = 242, + CONST_ME_WATER_DROP = 243, + CONST_ME_AVATAR_APPEAR = 244, + CONST_ME_DIVINE_GRENADE = 245, // Permanent + CONST_ME_DIVINE_EMPOWERMENT = 246, // Permanent + CONST_ME_WATER_FLOATING_THRASH = 247, + + CONST_ME_AGONY = 249, + + CONST_ME_LAST = CONST_ME_AGONY +}; + +public enum ShootTypeType : byte +{ + CONST_ANI_NONE, + + CONST_ANI_SPEAR = 1, + CONST_ANI_BOLT = 2, + CONST_ANI_ARROW = 3, + CONST_ANI_FIRE = 4, + CONST_ANI_ENERGY = 5, + CONST_ANI_POISONARROW = 6, + CONST_ANI_BURSTARROW = 7, + CONST_ANI_THROWINGSTAR = 8, + CONST_ANI_THROWINGKNIFE = 9, + CONST_ANI_SMALLSTONE = 10, + CONST_ANI_DEATH = 11, + CONST_ANI_LARGEROCK = 12, + CONST_ANI_SNOWBALL = 13, + CONST_ANI_POWERBOLT = 14, + CONST_ANI_POISON = 15, + CONST_ANI_INFERNALBOLT = 16, + CONST_ANI_HUNTINGSPEAR = 17, + CONST_ANI_ENCHANTEDSPEAR = 18, + CONST_ANI_REDSTAR = 19, + CONST_ANI_GREENSTAR = 20, + CONST_ANI_ROYALSPEAR = 21, + CONST_ANI_SNIPERARROW = 22, + CONST_ANI_ONYXARROW = 23, + CONST_ANI_PIERCINGBOLT = 24, + CONST_ANI_WHIRLWINDSWORD = 25, + CONST_ANI_WHIRLWINDAXE = 26, + CONST_ANI_WHIRLWINDCLUB = 27, + CONST_ANI_ETHEREALSPEAR = 28, + CONST_ANI_ICE = 29, + CONST_ANI_EARTH = 30, + CONST_ANI_HOLY = 31, + CONST_ANI_SUDDENDEATH = 32, + CONST_ANI_FLASHARROW = 33, + CONST_ANI_FLAMMINGARROW = 34, + CONST_ANI_SHIVERARROW = 35, + CONST_ANI_ENERGYBALL = 36, + CONST_ANI_SMALLICE = 37, + CONST_ANI_SMALLHOLY = 38, + CONST_ANI_SMALLEARTH = 39, + CONST_ANI_EARTHARROW = 40, + CONST_ANI_EXPLOSION = 41, + CONST_ANI_CAKE = 42, + + CONST_ANI_TARSALARROW = 44, + CONST_ANI_VORTEXBOLT = 45, + + CONST_ANI_PRISMATICBOLT = 48, + CONST_ANI_CRYSTALLINEARROW = 49, + CONST_ANI_DRILLBOLT = 50, + CONST_ANI_ENVENOMEDARROW = 51, + + CONST_ANI_GLOOTHSPEAR = 53, + CONST_ANI_SIMPLEARROW = 54, + + CONST_ANI_LEAFSTAR = 56, + CONST_ANI_DIAMONDARROW = 57, + CONST_ANI_SPECTRALBOLT = 58, + CONST_ANI_ROYALSTAR = 59, + + CONST_ANI_LAST = CONST_ANI_ROYALSTAR, + + // for internal use, don't send to client + CONST_ANI_WEAPONTYPE = 0xFE, // 254 +}; + +//public enum SpeakClassesType : byte +//{ +// TALKTYPE_SAY = 1, +// TALKTYPE_WHISPER = 2, +// TALKTYPE_YELL = 3, +// TALKTYPE_PRIVATE_FROM = 4, +// TALKTYPE_PRIVATE_TO = 5, +// TALKTYPE_CHANNEL_MANAGER = 6, +// TALKTYPE_CHANNEL_Y = 7, +// TALKTYPE_CHANNEL_O = 8, +// TALKTYPE_SPELL_USE = 9, +// TALKTYPE_PRIVATE_NP = 10, +// TALKTYPE_NPC_UNKOWN = 11, /* no effect (?)*/ +// TALKTYPE_PRIVATE_PN = 12, +// TALKTYPE_BROADCAST = 13, +// TALKTYPE_CHANNEL_R1 = 14, // red - #c text +// TALKTYPE_PRIVATE_RED_FROM = 15, //@name@text +// TALKTYPE_PRIVATE_RED_TO = 16, //@name@text +// TALKTYPE_MONSTER_SAY = 36, +// TALKTYPE_MONSTER_YELL = 37, + +// TALKTYPE_MONSTER_LAST_OLDPROTOCOL = 38, /* Dont forget about the CHANNEL_R2*/ +// TALKTYPE_CHANNEL_R2 = 0xFF // #d +//}; + +public enum MessageClassesType : byte +{ + MESSAGE_NONE = 0, /* None */ + + MESSAGE_GAMEMASTER_CONSOLE = 13, + /* Red message in the console*/ /* TALKTYPE_BROADCAST */ + + MESSAGE_LOGIN = 17, /* White message at the bottom of the game window and in the console*/ + MESSAGE_ADMINISTRADOR = 18, /* Red message in game window and in the console*/ + MESSAGE_EVENT_ADVANCE = 19, /* White message in game window and in the console*/ + MESSAGE_GAME_HIGHLIGHT = 20, /* Red message in game window and in the console*/ + MESSAGE_FAILURE = 21, /* White message at the bottom of the game window"*/ + MESSAGE_LOOK = 22, /* Green message in game window and in the console*/ + MESSAGE_DAMAGE_DEALT = 23, /* White message on the console*/ + MESSAGE_DAMAGE_RECEIVED = 24, /* White message on the console*/ + MESSAGE_HEALED = 25, /* White message on the console*/ + MESSAGE_EXPERIENCE = 26, /* White message on the console*/ + MESSAGE_DAMAGE_OTHERS = 27, /* White message on the console*/ + MESSAGE_HEALED_OTHERS = 28, /* White message on the console*/ + MESSAGE_EXPERIENCE_OTHERS = 29, /* White message on the console*/ + MESSAGE_STATUS = 30, /* White message at the bottom of the game window and in the console*/ + MESSAGE_LOOT = 31, /* White message on the game window and in the console*/ + MESSAGE_TRADE = 32, /* Green message in game window and in the console*/ + MESSAGE_GUILD = 33, /* White message in channel (+ channelId)*/ + MESSAGE_PARTY_MANAGEMENT = 34, /* Green message in game window and in the console*/ + MESSAGE_PARTY = 35, /* White message on the console*/ + + MESSAGE_LAST_OLDPROTOCOL = 37, /* Last Message on old protocol*/ + + MESSAGE_REPORT = 38, /* White message on the game window and in the console*/ + MESSAGE_HOTKEY_PRESSED = 39, /* Green message in game window and in the console*/ + MESSAGE_TUTORIAL_HINT = 40, /* no effect (?)*/ + MESSAGE_THANK_YOU = 41, /* no effect (?)*/ + MESSAGE_MARKET = 42, /* Popout a modal window with the message and a 'ok' button*/ + MESSAGE_MANA = 43, /* no effect (?)*/ + MESSAGE_BEYOND_LAST = 44, /* White message on the game window and in the console*/ + MESSAGE_ATTENTION = 48, /* White message on the console*/ + MESSAGE_BOOSTED_CREATURE = 49, /* White message on the game window and in the console*/ + MESSAGE_OFFLINE_TRAINING = 50, /* White message on the game window and in the console*/ + MESSAGE_TRANSACTION = 51, /* White message on the game window and in the console*/ + MESSAGE_POTION = 52, /* Orange creature say*/ +}; + +public enum FluidsType : byte +{ + FLUID_NONE = 0, /* Blue */ + FLUID_WATER = 1, /* Blue */ + FLUID_WINE = 2, /* Purple */ + FLUID_BEER = 3, /* Brown */ + FLUID_MUD = 4, /* Brown */ + FLUID_BLOOD = 5, /* Red */ + FLUID_SLIME = 6, /* Green */ + FLUID_OIL = 7, /* Brown */ + FLUID_URINE = 8, /* Yellow */ + FLUID_MILK = 9, /* White */ + FLUID_MANA = 10, /* Purple */ + FLUID_LIFE = 11, /* Red */ + FLUID_LEMONADE = 12, /* Yellow */ + FLUID_RUM = 13, /* Brown */ + FLUID_FRUITJUICE = 14, /* Yellow */ + FLUID_COCONUTMILK = 15, /* White */ + FLUID_MEAD = 16, /* Brown */ + FLUID_TEA = 17, /* Brown */ + FLUID_INK = 18 /* Black */ + // 12.85 last fluid is 18, 19+ is a loop from 0 to 18 over and over again +}; + +public enum SquareColorType : byte +{ + SQ_COLOR_BLACK = 0, +}; + +public enum TextColorType : byte +{ + TEXTCOLOR_BLUE = 5, + TEXTCOLOR_LIGHTGREEN = 30, + TEXTCOLOR_LIGHTBLUE = 35, + TEXTCOLOR_DARKBROWN = 78, + TEXTCOLOR_DARKGREY = 86, + TEXTCOLOR_MAYABLUE = 95, + TEXTCOLOR_DARKRED = 108, + TEXTCOLOR_NEUTRALDAMAGE = 109, + TEXTCOLOR_LIGHTGREY = 129, + TEXTCOLOR_SKYBLUE = 143, + TEXTCOLOR_PURPLE = 154, + TEXTCOLOR_ELECTRICPURPLE = 155, + TEXTCOLOR_RED = 180, + TEXTCOLOR_PASTELRED = 194, + TEXTCOLOR_ORANGE = 198, + TEXTCOLOR_LIGHTPURPLE = 199, + TEXTCOLOR_YELLOW = 210, + TEXTCOLOR_WHITE_EXP = 215, + TEXTCOLOR_NONE = 255, +}; + +public enum WeaponTypeType : byte +{ + WEAPON_NONE, + WEAPON_SWORD, + WEAPON_CLUB, + WEAPON_AXE, + WEAPON_SHIELD, + WEAPON_DISTANCE, + WEAPON_WAND, + WEAPON_AMMO, + WEAPON_MISSILE, +}; + +public enum AmmoType : byte +{ + AMMO_NONE, + AMMO_BOLT, + AMMO_ARROW, + AMMO_SPEAR, + AMMO_THROWINGSTAR, + AMMO_THROWINGKNIFE, + AMMO_STONE, + AMMO_SNOWBALL, +}; + +public enum WeaponActionType : byte +{ + WEAPONACTION_NONE, + WEAPONACTION_REMOVECOUNT, + WEAPONACTION_REMOVECHARGE, + WEAPONACTION_MOVE, +}; + +public enum PartyAnalyzerActionType : byte +{ + PARTYANALYZERACTION_RESET = 0, + PARTYANALYZERACTION_PRICETYPE = 1, + PARTYANALYZERACTION_PRICEVALUE = 2, +}; + +public enum SkullsType : byte +{ + SKULL_NONE = 0, + SKULL_YELLOW = 1, + SKULL_GREEN = 2, + SKULL_WHITE = 3, + SKULL_RED = 4, + SKULL_BLACK = 5, + SKULL_ORANGE = 6, +}; + +public enum PartyShieldsType : byte +{ + SHIELD_NONE = 0, + SHIELD_WHITEYELLOW = 1, + SHIELD_WHITEBLUE = 2, + SHIELD_BLUE = 3, + SHIELD_YELLOW = 4, + SHIELD_BLUE_SHAREDEXP = 5, + SHIELD_YELLOW_SHAREDEXP = 6, + SHIELD_BLUE_NOSHAREDEXP_BLINK = 7, + SHIELD_YELLOW_NOSHAREDEXP_BLINK = 8, + SHIELD_BLUE_NOSHAREDEXP = 9, + SHIELD_YELLOW_NOSHAREDEXP = 10, + SHIELD_GRAY = 11, +}; + +public enum GuildEmblemsType : byte +{ + GUILDEMBLEM_NONE = 0, + GUILDEMBLEM_ALLY = 1, + GUILDEMBLEM_ENEMY = 2, + GUILDEMBLEM_NEUTRAL = 3, + GUILDEMBLEM_MEMBER = 4, + GUILDEMBLEM_OTHER = 5, +}; + +public enum NameEvalType : byte +{ + VALID, + INVALID, + INVALID_LENGTH, + INVALID_TOKEN_LENGTH, + INVALID_FORBIDDEN, + INVALID_CHARACTER +}; + +public enum ItemIDType : ushort +{ + ITEM_BROWSEFIELD = 470, // for internal use + ITEM_SUPPLY_STASH_INDEX = 1, // for internal use + ITEM_DEPOT_NULL = 22796, // for internal use - Actual Item ID: 168 + ITEM_DECORATION_KIT = 23398, // For internal use (wrap item) + ITEM_DOCUMENT_RO = 2834, // Read-only + + ITEM_GOLD_POUCH = 23721, + + ITEM_DEPOT_I = 22797, + ITEM_DEPOT_II = 22798, + ITEM_DEPOT_III = 22799, + ITEM_DEPOT_IV = 22800, + ITEM_DEPOT_V = 22801, + ITEM_DEPOT_VI = 22802, + ITEM_DEPOT_VII = 22803, + ITEM_DEPOT_VIII = 22804, + ITEM_DEPOT_IX = 22805, + ITEM_DEPOT_X = 22806, + ITEM_DEPOT_XI = 22807, + ITEM_DEPOT_XII = 22808, + ITEM_DEPOT_XIII = 22809, + ITEM_DEPOT_XIV = 22810, + ITEM_DEPOT_XV = 22811, + ITEM_DEPOT_XVI = 22812, + ITEM_DEPOT_XVII = 22813, + ITEM_DEPOT_XVIII = 31915, + ITEM_DEPOT_XIX = 39723, + ITEM_DEPOT_XX = 39724, + + ITEM_FIREFIELD_PVP_FULL = 2118, + ITEM_FIREFIELD_PVP_MEDIUM = 2119, + ITEM_FIREFIELD_PVP_SMALL = 2120, + ITEM_FIREFIELD_PERSISTENT_FULL = 2123, + ITEM_FIREFIELD_PERSISTENT_MEDIUM = 2124, + ITEM_FIREFIELD_PERSISTENT_SMALL = 2125, + ITEM_FIREFIELD_NOPVP = 21465, + + ITEM_POISONFIELD_PVP = 105, + ITEM_POISONFIELD_PERSISTENT = 2121, + ITEM_POISONFIELD_NOPVP = 2134, + + ITEM_ENERGYFIELD_PVP = 2122, + ITEM_ENERGYFIELD_PERSISTENT = 2126, + ITEM_ENERGYFIELD_NOPVP = 2135, + + ITEM_MAGICWALL = 2128, + ITEM_MAGICWALL_PERSISTENT = 2129, + ITEM_MAGICWALL_SAFE = 10181, + + ITEM_WILDGROWTH = 2130, + ITEM_WILDGROWTH_PERSISTENT = 3635, + ITEM_WILDGROWTH_SAFE = 10182, + + ITEM_BAG = 2853, + ITEM_SHOPPING_BAG = 21411, + + ITEM_GOLD_COIN = 3031, + ITEM_PLATINUM_COIN = 3035, + ITEM_CRYSTAL_COIN = 3043, + ITEM_STORE_COIN = 22118, + + ITEM_MINOR_CRYSTALLINE_TOKENS = 16128, + ITEM_MAJOR_CRYSTALLINE_TOKENS = 16129, + + ITEM_REWARD_CONTAINER = 19202, + ITEM_REWARD_CHEST = 19250, + + ITEM_DEPOT = 3502, + ITEM_LOCKER = 3497, + ITEM_INBOX = 12902, + ITEM_MARKET = 12903, + ITEM_STORE_INBOX = 23396, + ITEM_SUPPLY_STASH = 28750, + + ITEM_MALE_CORPSE = 4240, + ITEM_FEMALE_CORPSE = 4247, + + ITEM_FULLSPLASH = 2886, + ITEM_SMALLSPLASH = 2889, + + ITEM_PARCEL = 3503, + ITEM_PARCEL_STAMPED = 3504, + ITEM_LETTER = 3505, + ITEM_LETTER_STAMPED = 3506, + ITEM_LABEL = 3507, + + ITEM_AMULETOFLOSS = 3057, + + ITEM_EXERCISE_START = 28552, + ITEM_EXERCISE_END = 28557, + + /** Casks and Kegs **/ + ITEM_HEALTH_CASK_START = 25879, + ITEM_HEALTH_CASK_END = 25883, + + ITEM_MANA_CASK_START = 25889, + ITEM_MANA_CASK_END = 25893, + + ITEM_SPIRIT_CASK_START = 25899, + ITEM_SPIRIT_CASK_END = 25902, + + ITEM_KEG_START = 25903, // kegs ids are contiguous in item.otb + ITEM_KEG_END = 25914, + + // Walkable sea + ITEM_WALKABLE_SEA_START = 629, + ITEM_WALKABLE_SEA_END = 634, + + ITEM_STONE_SKIN_AMULET = 3081, + + ITEM_OLD_DIAMOND_ARROW = 25757, + ITEM_DIAMOND_ARROW = 35901, + + ITEM_FILLED_BATH_TUBE = 26077, + + ITEM_SWORD_RING = 3091, + ITEM_SWORD_RING_ACTIVATED = 3094, + + ITEM_CLUB_RING = 3093, + ITEM_CLUB_RING_ACTIVATED = 3096, + + ITEM_DWARVEN_RING = 3097, + ITEM_DWARVEN_RING_ACTIVATED = 3099, + + ITEM_RING_HEALING = 3098, + ITEM_RING_HEALING_ACTIVATED = 3100, + + ITEM_STEALTH_RING = 3049, + ITEM_STEALTH_RING_ACTIVATED = 3086, + + ITEM_TIME_RING = 3053, + ITEM_TIME_RING_ACTIVATED = 3090, + + ITEM_PAIR_SOFT_BOOTS = 6529, + ITEM_PAIR_SOFT_BOOTS_ACTIVATED = 3549, + + ITEM_DEATH_RING = 6299, + ITEM_DEATH_RING_ACTIVATED = 6300, + + ITEM_PRISMATIC_RING = 16114, + ITEM_PRISMATIC_RING_ACTIVATED = 16264, + + HIRELING_LAMP = 29432, + + ITEM_FORGE_SLIVER = 37109, + ITEM_FORGE_CORE = 37110, + ITEM_EXALTATION_CHEST = 37561, + ITEM_PODIUM_OF_RENOWN1 = 35973, + ITEM_PODIUM_OF_RENOWN2 = 35974, + ITEM_PODIUM_OF_VIGOUR = 38707, + ITEM_PRIMAL_POD = 39176, + ITEM_DIVINE_EMPOWERMENT = 40450, + + ITEM_BATHTUB_FILLED = 26077, + ITEM_BATHTUB_FILLED_NOTMOVABLE = 26100, + + ITEM_NONE = 0 +}; + +public enum PlayerFlagsType : byte +{ + CannotUseCombat, + CannotAttackPlayer, + CannotAttackMonster, + CannotBeAttacked, + CanConvinceAll, + CanSummonAll, + CanIllusionAll, + CanSenseInvisibility, + IgnoredByMonsters, + NotGainInFight, + HasInfiniteMana, + HasInfiniteSoul, + HasNoExhaustion, + CannotUseSpells, + CannotPickupItem, + CanAlwaysLogin, + CanBroadcast, + CanEditHouses, + CannotBeBanned, + CannotBePushed, + HasInfiniteCapacity, + CanPushAllCreatures, + CanTalkRedPrivate, + CanTalkRedChannel, + TalkOrangeHelpChannel, + NotGainExperience, + NotGainMana, + NotGainHealth, + NotGainSkill, + SetMaxSpeed, + SpecialVIP, + NotGenerateLoot, + CanTalkRedChannelAnonymous, + IgnoreProtectionZone, + IgnoreSpellCheck, + IgnoreWeaponCheck, + CannotBeMuted, + IsAlwaysPremium, + CanMapClickTeleport, + IgnoredByNpcs, + + // Must always be the last + FlagLast +}; + +public enum BlessingsType : byte +{ + TWIST_OF_FATE = 1, + WISDOM_OF_SOLITUDE = 2, + SPARK_OF_THE_PHOENIX = 3, + FIRE_OF_THE_SUNS = 4, + SPIRITUAL_SHIELDING = 5, + EMBRACE_OF_TIBIA = 6, + BLOOD_OF_THE_MOUNTAIN = 7, + HEARTH_OF_THE_MOUNTAIN = 8, +}; + +public enum BedItemPartType : byte +{ + BED_NONE_PART, + BED_PILLOW_PART, + BED_BLANKET_PART, +}; + +public enum AttrSubIdType +{ + None, + TrainParty, + ProtectParty, + EnchantParty, + JeanPierreMagic, + JeanPierreMelee, + JeanPierreDistance, + JeanPierreDefense, + JeanPierreFishing, + BloodRageProtector, + Sharpshooter, +}; + +public enum ConcoctionType : ushort +{ + KooldownAid = 36723, + StaminaExtension = 36725, + StrikeEnhancement = 36724, + CharmUpgrade = 36726, + WealthDuplex = 36727, + BestiaryBetterment = 36728, + FireResilience = 36729, + IceResilience = 36730, + EarthResilience = 36731, + EnergyResilience = 36732, + HolyResilience = 36733, + DeathResilience = 36734, + PhysicalResilience = 36735, + FireAmplification = 36736, + IceAmplification = 36737, + EarthAmplification = 36738, + EnergyAmplification = 36739, + HolyAmplification = 36740, + DeathAmplification = 36741, + PhysicalAmplification = 36742, +}; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua new file mode 100644 index 000000000..66767af56 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua @@ -0,0 +1,37 @@ +coreDirectory = "DataLuaJit" +showScriptsLogInConsole = true + +-- NOTE: true will allow the /reload command to be used +-- NOTE: Using this script might cause unwanted changes +-- This script forces a reload in the entire server, this means that everything that is stored in memory might stop to work properly and/or completely, this script should be used in test environments only +allowReload = true + +-- Save interval per time +-- NOTE: toggleSaveInterval: true = enable the save interval, false = disable the save interval +-- NOTE: saveIntervalType: "minute", "second" or "hour" +-- NOTE: toggleSaveIntervalCleanMap: true = enable the clean map, false = disable the clean map +-- NOTE: saveIntervalTime: time based on what was set in "saveIntervalType" +toggleSaveInterval = true +saveIntervalType = "minute" +toggleSaveIntervalCleanMap = true +saveIntervalTime = 1 + +-- Connection Config +-- NOTE: allowOldProtocol can allow login on 10x protocol. (11.00) +-- NOTE: maxPlayers set to 0 means no limit +-- NOTE: MaxPacketsPerSeconds if you change you will be subject to bugs by WPE, keep the default value of 25 +ip = "127.0.0.1" +allowOldProtocol = false +bindOnlyGlobalAddress = false +loginProtocolPort = 7171 +gameProtocolPort = 7172 +statusProtocolPort = 7171 +maxPlayers = 0 +serverName = "OTServBR-Global" +serverMotd = "Welcome to the OTServBR-Global!" +onePlayerOnlinePerAccount = true +statusTimeout = 5 * 1000 +replaceKickOnLogin = true +maxPacketsPerSecond = 25 +maxItem = 2000 +maxContainer = 100 \ No newline at end of file diff --git a/src/NeoServer.sln b/src/NeoServer.sln index 668160223..3ef5b44e3 100644 --- a/src/NeoServer.sln +++ b/src/NeoServer.sln @@ -77,8 +77,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeoServer.Loaders.Tests", " EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{EB46ABE5-1719-4A52-BACB-A05D6EACCAB5}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "loaders", "loaders", "{FA691E87-E79B-4778-9996-52077E6802B0}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "application server", "application server", "{83192B33-17E6-49EF-A698-3F34EAAE188B}" @@ -87,10 +85,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "extensions", "extensions", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "game", "game", "{4733614A-98B6-48C5-A449-046601727458}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeoServer.Game.Systems", "GameWorldSimulator\NeoServer.Game.Systems\NeoServer.Game.Systems.csproj", "{E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeoServer.Game.Systems.Tests", "..\tests\NeoServer.Game.Systems.Tests\NeoServer.Game.Systems.Tests.csproj", "{86704CC5-10AA-41D1-827E-ADD91D585696}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeoServer.Game.Systems", "GameWorldSimulator\NeoServer.Game.Systems\NeoServer.Game.Systems.csproj", "{E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}" +EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeoServer.Web.API", "WebAPI\NeoServer.Web.API.csproj", "{34EC6CED-7474-4ACC-9BEB-22C5DC159197}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests api", "tests api", "{3FA7BBBE-B9CB-490A-89C6-39203C294541}" @@ -105,6 +103,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeoServer.Shared.IoC", "Sha EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NeoServer.Aspire.AppHost", "NeoServer.Aspire\NeoServer.Aspire.AppHost\NeoServer.Aspire.AppHost.csproj", "{BC61E4DA-D4F3-46DC-9B1D-E563BDF45E02}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{EB46ABE5-1719-4A52-BACB-A05D6EACCAB5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NeoServer.Scripts.LuaJIT", "Extensions\NeoServer.Scripts.LuaJIT\NeoServer.Scripts.LuaJIT.csproj", "{79E18750-321E-4E02-897D-889A70530993}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -375,14 +377,6 @@ Global {BF47800C-BB14-4A24-9EB2-3BAE8B64EA5C}.Release|Any CPU.Build.0 = Release|Any CPU {BF47800C-BB14-4A24-9EB2-3BAE8B64EA5C}.Release|x64.ActiveCfg = Release|Any CPU {BF47800C-BB14-4A24-9EB2-3BAE8B64EA5C}.Release|x64.Build.0 = Release|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|x64.ActiveCfg = Debug|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|x64.Build.0 = Debug|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|Any CPU.Build.0 = Release|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|x64.ActiveCfg = Release|Any CPU - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|x64.Build.0 = Release|Any CPU {86704CC5-10AA-41D1-827E-ADD91D585696}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {86704CC5-10AA-41D1-827E-ADD91D585696}.Debug|Any CPU.Build.0 = Debug|Any CPU {86704CC5-10AA-41D1-827E-ADD91D585696}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -391,6 +385,14 @@ Global {86704CC5-10AA-41D1-827E-ADD91D585696}.Release|Any CPU.Build.0 = Release|Any CPU {86704CC5-10AA-41D1-827E-ADD91D585696}.Release|x64.ActiveCfg = Release|Any CPU {86704CC5-10AA-41D1-827E-ADD91D585696}.Release|x64.Build.0 = Release|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|x64.ActiveCfg = Debug|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Debug|x64.Build.0 = Debug|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|Any CPU.Build.0 = Release|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|x64.ActiveCfg = Release|Any CPU + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2}.Release|x64.Build.0 = Release|Any CPU {34EC6CED-7474-4ACC-9BEB-22C5DC159197}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {34EC6CED-7474-4ACC-9BEB-22C5DC159197}.Debug|Any CPU.Build.0 = Debug|Any CPU {34EC6CED-7474-4ACC-9BEB-22C5DC159197}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -431,6 +433,14 @@ Global {BC61E4DA-D4F3-46DC-9B1D-E563BDF45E02}.Release|Any CPU.Build.0 = Release|Any CPU {BC61E4DA-D4F3-46DC-9B1D-E563BDF45E02}.Release|x64.ActiveCfg = Release|Any CPU {BC61E4DA-D4F3-46DC-9B1D-E563BDF45E02}.Release|x64.Build.0 = Release|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Debug|x64.ActiveCfg = Debug|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Debug|x64.Build.0 = Debug|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Release|Any CPU.Build.0 = Release|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Release|x64.ActiveCfg = Release|Any CPU + {79E18750-321E-4E02-897D-889A70530993}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -471,19 +481,19 @@ Global {7588730A-5787-4D61-A3B2-31D63AC10355} = {4733614A-98B6-48C5-A449-046601727458} {E5A0A9B7-1E3B-45CD-AD1F-10B7488123C8} = {2D49C135-AE9B-4434-8CF3-2F3E43E2583A} {BF47800C-BB14-4A24-9EB2-3BAE8B64EA5C} = {9D4CBB34-8698-4A2E-BBC9-12586A98DC30} - {EB46ABE5-1719-4A52-BACB-A05D6EACCAB5} = {41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B} {FA691E87-E79B-4778-9996-52077E6802B0} = {41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B} {83192B33-17E6-49EF-A698-3F34EAAE188B} = {41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B} {2D49C135-AE9B-4434-8CF3-2F3E43E2583A} = {41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B} {4733614A-98B6-48C5-A449-046601727458} = {9D4CBB34-8698-4A2E-BBC9-12586A98DC30} - {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2} = {EB46ABE5-1719-4A52-BACB-A05D6EACCAB5} {86704CC5-10AA-41D1-827E-ADD91D585696} = {4733614A-98B6-48C5-A449-046601727458} + {E628930B-E1CB-4E6A-BA2E-6E27A93B6BC2} = {EB46ABE5-1719-4A52-BACB-A05D6EACCAB5} {34EC6CED-7474-4ACC-9BEB-22C5DC159197} = {41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B} {4C491878-BB1F-41A3-B4F1-CB78D1862A1B} = {3FA7BBBE-B9CB-490A-89C6-39203C294541} {ABF0692C-9C53-472D-B960-E4444FE46B2C} = {41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B} {94C3B154-2E2F-4787-9ECD-A4B217862EC9} = {ABF0692C-9C53-472D-B960-E4444FE46B2C} {B0353BE8-6DE2-4D8F-A518-6123B0A34124} = {ABF0692C-9C53-472D-B960-E4444FE46B2C} {BC61E4DA-D4F3-46DC-9B1D-E563BDF45E02} = {41D4D3B5-6FC6-48F5-9D05-EA8C293DB44B} + {79E18750-321E-4E02-897D-889A70530993} = {2D49C135-AE9B-4434-8CF3-2F3E43E2583A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3B3C7097-0071-4218-BE24-5EE9128763B6} diff --git a/src/Standalone/IoC/Modules/LuaInjection.cs b/src/Standalone/IoC/Modules/LuaInjection.cs index 94afe568d..03b262a71 100644 --- a/src/Standalone/IoC/Modules/LuaInjection.cs +++ b/src/Standalone/IoC/Modules/LuaInjection.cs @@ -1,5 +1,7 @@ using Microsoft.Extensions.DependencyInjection; +using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Scripts.Lua; +using NeoServer.Scripts.LuaJIT; using NLua; namespace NeoServer.Server.Standalone.IoC.Modules; @@ -10,6 +12,16 @@ public static IServiceCollection AddLua(this IServiceCollection builder) { builder.AddSingleton(new Lua()); builder.AddSingleton(); + + + //LuaJIT todo: move this to lUaScripts + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + // return builder; } } \ No newline at end of file diff --git a/src/Standalone/NeoServer.Server.Standalone.csproj b/src/Standalone/NeoServer.Server.Standalone.csproj index e6287e6b5..7c46c4065 100644 --- a/src/Standalone/NeoServer.Server.Standalone.csproj +++ b/src/Standalone/NeoServer.Server.Standalone.csproj @@ -7,46 +7,47 @@ false - - - + + + $(MSBuildProjectDirectory) d00c6b5a-86dc-4f58-b885-056c3322a45a - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/src/Standalone/Program.cs b/src/Standalone/Program.cs index 93a782033..1da1112d8 100644 --- a/src/Standalone/Program.cs +++ b/src/Standalone/Program.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Data.Contexts; using NeoServer.Game.Common; using NeoServer.Game.Common.Helpers; @@ -112,6 +113,8 @@ public static async Task Main() container.Resolve().Open(); + container.Resolve().Start(); + sw.Stop(); logger.Step("Running Garbage Collector", "Garbage collected", () => From 1a8a67020a3fe96f9a07981e096f4572157c12a6 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Sat, 21 Dec 2024 10:59:02 -0300 Subject: [PATCH 02/16] feature: move some things to LuaJit project, refact way how LuaGameManager is Called. --- .../Player/PlayerSayCommand.cs | 19 +++-------- .../Contracts/Scripts/ILuaGameManager.cs | 10 ++++++ .../Contracts/Scripts/ILuaManager.cs | 7 ---- .../NeoServer.Scripts.LuaJIT/ConfigManager.cs | 8 ++--- .../Interfaces}/ITalkAction.cs | 0 .../IoC/Modules/LuaJITInjection.cs | 19 +++++++++++ .../LuaFunctionsLoader.cs | 2 +- .../{LuaManager.cs => LuaGameManager.cs} | 32 +++++++++++++++++-- .../Structs}/Entities.cs | 5 +-- src/Standalone/IoC/IoC.cs | 1 + src/Standalone/IoC/Modules/LuaInjection.cs | 10 ++---- src/Standalone/Program.cs | 2 +- .../Commands/PlayerSayCommandTest.cs | 4 ++- 13 files changed, 75 insertions(+), 44 deletions(-) create mode 100644 src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs delete mode 100644 src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs rename src/{ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts => Extensions/NeoServer.Scripts.LuaJIT/Interfaces}/ITalkAction.cs (100%) create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaManager.cs => LuaGameManager.cs} (79%) rename src/{ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts => Extensions/NeoServer.Scripts.LuaJIT/Structs}/Entities.cs (98%) diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs index a0c786540..0d49904e3 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs @@ -16,9 +16,9 @@ public class PlayerSayCommand : ICommand { private readonly IChatChannelStore _chatChannelStore; private readonly IGameServer _game; - private readonly ILuaManager _luaManager; + private readonly ILuaGameManager _luaManager; - public PlayerSayCommand(IGameServer game, IChatChannelStore chatChannelStore, ILuaManager luaManager) + public PlayerSayCommand(IGameServer game, IChatChannelStore chatChannelStore, ILuaGameManager luaManager) { _game = game; _chatChannelStore = chatChannelStore; @@ -33,19 +33,8 @@ public void Execute(IPlayer player, IConnection connection, PlayerSayPacket play var message = playerSayPacket.Message?.Trim(); - //estava chamando aqui lua manager - var messageData = message.Split(" "); - - var talkAction = _luaManager.GetTalkAction(messageData[0]); - var parameter = ""; - - if (messageData.Count() > 1) - parameter = messageData[1]; - - if (talkAction != null) - { - talkAction.ExecuteSay(player, messageData[0], parameter, playerSayPacket.TalkType); - } + if (_luaManager.PlayerSaySpell(player, playerSayPacket.TalkType, message)) + return; if (player.CastSpell(message)) return; diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs new file mode 100644 index 000000000..d0342bb68 --- /dev/null +++ b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs @@ -0,0 +1,10 @@ +using NeoServer.Game.Common.Chats; +using NeoServer.Game.Common.Contracts.Creatures; + +namespace NeoServer.Application.Common.Contracts.Scripts; + +public interface ILuaGameManager +{ + void Start(); + bool PlayerSaySpell(IPlayer player, SpeechType type, string text); +} diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs deleted file mode 100644 index 6fee8ae97..000000000 --- a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaManager.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace NeoServer.Application.Common.Contracts.Scripts; - -public interface ILuaManager -{ - void Start(); - ITalkAction GetTalkAction(string name); -} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs index a8b2ba46a..2805d40bf 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs @@ -45,7 +45,7 @@ public bool Load(string file) var L = Lua.NewState(); if (L.pointer == 0) { - throw new System.IO.IOException("Failed to allocate memory"); + throw new IOException("Failed to allocate memory"); } Lua.OpenLibs(L); @@ -57,9 +57,9 @@ public bool Load(string file) return false; } -//#if !DEBUG_LOG -// g_logger().setLevel(getGlobalString(L, "logLevel", "info")); -//#endif + //#if !DEBUG_LOG + // g_logger().setLevel(getGlobalString(L, "logLevel", "info")); + //#endif // Parse config // Info that must be loaded one time (unless we reset the modules involved) diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ITalkAction.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkAction.cs similarity index 100% rename from src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ITalkAction.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkAction.cs diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs new file mode 100644 index 000000000..087912212 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.DependencyInjection; +using NeoServer.Scripts.LuaJIT; + +namespace NeoServer.Server.Standalone.IoC.Modules; + +public static class LuaJITInjection +{ + public static IServiceCollection AddLuaJIT(this IServiceCollection builder) + { + //LuaJIT todo: move this to lUaScripts + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + // + return builder; + } +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs index ff0103b9a..1d290e060 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs @@ -2,9 +2,9 @@ using System.Diagnostics; using System.Runtime.InteropServices; using LuaNET; -using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Scripts.LuaJIT.Structs; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs similarity index 79% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaManager.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index 1c3aacdc7..01391ee03 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -1,10 +1,13 @@ using LuaNET; using NeoServer.Application.Common.Contracts.Scripts; +using NeoServer.Game.Common.Chats; +using NeoServer.Game.Common.Contracts.Creatures; using Serilog; +using System.Reflection.Metadata; namespace NeoServer.Scripts.LuaJIT; -public class LuaManager : ILuaManager +public class LuaGameManager : ILuaGameManager { #region Members @@ -40,7 +43,7 @@ public class LuaManager : ILuaManager #region Constructors - public LuaManager( + public LuaGameManager( ILogger logger, ILuaEnvironment luaEnviroment, IConfigManager configManager, @@ -58,7 +61,7 @@ public LuaManager( #endregion - public ITalkAction GetTalkAction(string name) => _talkActions.GetTalkAction(name); + #region Public Methods public void Start() { @@ -110,6 +113,29 @@ public void Start() ModulesLoadHelper(_scripts.LoadScripts($"{dir}\\DataLuaJit/scripts", false, false), "/DataLuaJit/scripts"); } + public bool PlayerSaySpell(IPlayer player, SpeechType type, string words) + { + var wordsSeparator = " "; + var talkactionWords = words.Contains(wordsSeparator) ? words.Split(" ") : [words]; + + if (!talkactionWords.Any()) + return false; + + var talkAction = _talkActions.GetTalkAction(talkactionWords[0]); + + if (talkAction == null) + return false; + + var parameter = ""; + + if (talkactionWords.Count() > 1) + parameter = talkactionWords[1]; + + return talkAction.ExecuteSay(player, talkactionWords[0], parameter, type); + } + + #endregion + #region Private Methods private void ModulesLoadHelper(bool loaded, string moduleName) diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/Entities.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs similarity index 98% rename from src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/Entities.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs index 30dfde637..829f55eaf 100644 --- a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/Entities.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; - -namespace NeoServer.Application.Common.Contracts.Scripts +namespace NeoServer.Scripts.LuaJIT.Structs { public enum DirectionType : byte { diff --git a/src/Standalone/IoC/IoC.cs b/src/Standalone/IoC/IoC.cs index 9baeb6966..0490d31c5 100644 --- a/src/Standalone/IoC/IoC.cs +++ b/src/Standalone/IoC/IoC.cs @@ -78,6 +78,7 @@ public static IServiceProvider BuildAll() .AddLogger(configuration) .AddCommands() .AddLua() + .AddLuaJIT() .AddJobs() .AddCommands() .AddDataStores(); diff --git a/src/Standalone/IoC/Modules/LuaInjection.cs b/src/Standalone/IoC/Modules/LuaInjection.cs index 03b262a71..fdd58e85e 100644 --- a/src/Standalone/IoC/Modules/LuaInjection.cs +++ b/src/Standalone/IoC/Modules/LuaInjection.cs @@ -13,15 +13,9 @@ public static IServiceCollection AddLua(this IServiceCollection builder) builder.AddSingleton(new Lua()); builder.AddSingleton(); + //LuaJIT + builder.AddSingleton(); - //LuaJIT todo: move this to lUaScripts - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - // return builder; } } \ No newline at end of file diff --git a/src/Standalone/Program.cs b/src/Standalone/Program.cs index 1da1112d8..bb8b465be 100644 --- a/src/Standalone/Program.cs +++ b/src/Standalone/Program.cs @@ -113,7 +113,7 @@ public static async Task Main() container.Resolve().Open(); - container.Resolve().Start(); + container.Resolve().Start(); sw.Stop(); diff --git a/tests/NeoServer.Server.Tests/Commands/PlayerSayCommandTest.cs b/tests/NeoServer.Server.Tests/Commands/PlayerSayCommandTest.cs index 09b46e5a4..c3aa0827f 100644 --- a/tests/NeoServer.Server.Tests/Commands/PlayerSayCommandTest.cs +++ b/tests/NeoServer.Server.Tests/Commands/PlayerSayCommandTest.cs @@ -1,4 +1,5 @@ using Moq; +using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Data.InMemory.DataStores; using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; @@ -19,6 +20,7 @@ public void Execute_When_Player_Send_Message_To_Another_Player_Should_Call_Playe var player = new Mock(); var connection = new Mock(); var network = new Mock(); + var luaGameManager = new Mock(); var playerSayPacket = new Mock(network.Object); playerSayPacket.SetupGet(x => x.TalkType).Returns(SpeechType.Private); @@ -33,7 +35,7 @@ public void Execute_When_Player_Send_Message_To_Another_Player_Should_Call_Playe var game = new Mock(); game.Setup(x => x.CreatureManager.TryGetPlayer("receiver", out receiver)).Returns(true); - var sut = new PlayerSayCommand(game.Object, chatChannelStore); + var sut = new PlayerSayCommand(game.Object, chatChannelStore, luaGameManager.Object); //act sut.Execute(player.Object, connection.Object, playerSayPacket.Object); From 1b76dab69ef0d81587929017258c2d66d479e4e7 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Sat, 21 Dec 2024 11:49:19 -0300 Subject: [PATCH 03/16] feat: refact on all lua jit loaders functions, remove unused code, and adjustments on load lua jit files. --- .../Player/PlayerSayCommand.cs | 12 +- .../Contracts/Scripts/ILuaGameManager.cs | 1 - .../{DataLuaJit => Data/LuaJit}/core.lua | 8 +- .../{DataLuaJit => Data/LuaJit}/global.lua | 2 +- .../LuaJit}/libs/functions/load.lua | 1 - .../LuaJit}/libs/functions/revscriptsys.lua | 8 +- .../{DataLuaJit => Data/LuaJit}/libs/libs.lua | 0 .../LuaJit}/scripts/actions/Tools/shovel.lua | 0 .../scripts/talkactions/god/reload.lua | 0 .../scripts/talkactions/player/test.lua | 20 ++-- .../DataLuaJit/libs/functions/game.lua | 9 -- .../globalevents/Customs/save_interval.lua | 37 ------ .../scripts/globalevents/example_one.lua | 7 -- .../scripts/globalevents/example_two.lua | 8 -- .../scripts/reward_chest/boss_think.lua | 12 -- .../scripts/talkactions/god/benchmark.lua | 42 ------- .../scripts/talkactions/player/help.lua | 48 -------- .../{ => Functions}/ConfigFunctions.cs | 6 +- .../{ => Functions}/CreatureFunctions.cs | 2 +- .../{ => Functions}/GlobalFunctions.cs | 6 +- .../{ => Functions}/LoggerFunctions.cs | 20 ++-- .../{ => Functions}/PlayerFunctions.cs | 6 +- .../{ => Functions}/TalkActionFunctions.cs | 6 +- .../IBaseFunctions.cs} | 2 +- .../Interfaces/Functions/IConfigFunctions.cs | 5 + .../Functions/ICreatureFunctions.cs | 5 + .../Interfaces/Functions/IGlobalFunctions.cs | 5 + .../Interfaces/Functions/ILoggerFunctions.cs | 5 + .../Interfaces/Functions/IPlayerFunctions.cs | 5 + .../Functions/ITalkActionFunctions.cs | 5 + .../IoC/Modules/LuaJITInjection.cs | 6 + .../LuaGameManager.cs | 107 ++++++++++-------- .../NeoServer.Scripts.LuaJIT.csproj | 37 ++---- .../NeoServer.Scripts.LuaJIT/config.lua | 8 +- src/Standalone/Program.cs | 2 - 35 files changed, 158 insertions(+), 295 deletions(-) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/core.lua (56%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/global.lua (92%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/libs/functions/load.lua (60%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/libs/functions/revscriptsys.lua (98%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/libs/libs.lua (100%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/scripts/actions/Tools/shovel.lua (100%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/scripts/talkactions/god/reload.lua (100%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{DataLuaJit => Data/LuaJit}/scripts/talkactions/player/test.lua (53%) delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Functions}/ConfigFunctions.cs (93%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Functions}/CreatureFunctions.cs (98%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Functions}/GlobalFunctions.cs (70%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Functions}/LoggerFunctions.cs (72%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Functions}/PlayerFunctions.cs (96%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Functions}/TalkActionFunctions.cs (97%) rename src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/{ICreatureFunctions.cs => Functions/IBaseFunctions.cs} (70%) create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs index 0d49904e3..cdf3d828e 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/PlayerSayCommand.cs @@ -1,5 +1,4 @@ using System.Linq; -using Microsoft.VisualBasic; using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; @@ -16,13 +15,16 @@ public class PlayerSayCommand : ICommand { private readonly IChatChannelStore _chatChannelStore; private readonly IGameServer _game; - private readonly ILuaGameManager _luaManager; + private readonly ILuaGameManager _luaGameManager; - public PlayerSayCommand(IGameServer game, IChatChannelStore chatChannelStore, ILuaGameManager luaManager) + public PlayerSayCommand( + IGameServer game, + IChatChannelStore chatChannelStore, + ILuaGameManager luaGameManager) { _game = game; _chatChannelStore = chatChannelStore; - _luaManager = luaManager; + _luaGameManager = luaGameManager; } public void Execute(IPlayer player, IConnection connection, PlayerSayPacket playerSayPacket) @@ -33,7 +35,7 @@ public void Execute(IPlayer player, IConnection connection, PlayerSayPacket play var message = playerSayPacket.Message?.Trim(); - if (_luaManager.PlayerSaySpell(player, playerSayPacket.TalkType, message)) + if (_luaGameManager.PlayerSaySpell(player, playerSayPacket.TalkType, message)) return; if (player.CastSpell(message)) return; diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs index d0342bb68..a24f59ace 100644 --- a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs +++ b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs @@ -5,6 +5,5 @@ namespace NeoServer.Application.Common.Contracts.Scripts; public interface ILuaGameManager { - void Start(); bool PlayerSaySpell(IPlayer player, SpeechType type, string text); } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/core.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/core.lua similarity index 56% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/core.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/core.lua index 44c5194b9..a0da175f9 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/core.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/core.lua @@ -1,14 +1,14 @@ -print('print from core.lua') -print(configKeys.BASE_DIRECTORY) +logger.info('print from core.lua') +logger.info(configKeys.BASE_DIRECTORY) logger.debug('Starting lua.') -print(os.getenv('LOCAL_LUA_DEBUGGER_VSCODE')) +logger.debug(os.getenv('LOCAL_LUA_DEBUGGER_VSCODE')) if os.getenv('LOCAL_LUA_DEBUGGER_VSCODE') == '1' then require('lldebugger').start() logger.debug('Started LUA debugger.') end -CORE_DIRECTORY = configKeys.BASE_DIRECTORY .. 'DataLuaJit' +CORE_DIRECTORY = configKeys.BASE_DIRECTORY .. 'Data/LuaJit' dofile(CORE_DIRECTORY .. '/global.lua') dofile(CORE_DIRECTORY .. '/libs/libs.lua') \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/global.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua similarity index 92% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/global.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua index 35421a81e..dda54eac2 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/global.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua @@ -1,5 +1,5 @@ -- for use of: data\scripts\globalevents\customs\save_interval.lua -print('print from global.lua') +logger.info('print from global.lua') SAVE_INTERVAL_TYPE = configManager.getString(configKeys.SAVE_INTERVAL_TYPE) SAVE_INTERVAL_CONFIG_TIME = configManager.getNumber(configKeys.SAVE_INTERVAL_TIME) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/load.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua similarity index 60% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/load.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua index 331210007..1efa72cf0 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/load.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua @@ -1,3 +1,2 @@ -- Load core functions ---dofile(CORE_DIRECTORY .. "/libs/functions/game.lua") dofile(CORE_DIRECTORY .. "/libs/functions/revscriptsys.lua") \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/revscriptsys.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua similarity index 98% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/revscriptsys.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua index 67d0c374e..e1d66d8ee 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/revscriptsys.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua @@ -99,12 +99,12 @@ do end rawset(self, key, value) end - print('TalkAction revscriptsys') - print(configKeys.BASE_DIRECTORY) + logger.info('TalkAction revscriptsys') + logger.info(configKeys.BASE_DIRECTORY) local meta = rawgetmetatable("TalkAction") - print('meta') - print(meta) + logger.info('meta') + logger.info(meta) meta.__newindex = TalkActionNewIndex end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/libs.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/libs.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/libs.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/libs.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/actions/Tools/shovel.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/actions/Tools/shovel.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/reload.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/reload.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/test.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/player/test.lua similarity index 53% rename from src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/test.lua rename to src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/player/test.lua index a35b7203e..3ae0a9a87 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/test.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/player/test.lua @@ -1,31 +1,31 @@ local talkAction = TalkAction("!test") function talkAction.onSay(player, words, param) - print('executing talkAction from lua: '.. words .. ' ' .. param) + logger.info('executing talkAction from lua: '.. words .. ' ' .. param) - print(player) - print(player:getName()) - print(player:getId()) + logger.info(player) + logger.info(player:getName()) + logger.info(player:getId()) local creature = Creature("GOD") if creature then - print(creature) - print(creature:getName()) - print(creature:getId()) + logger.info(creature) + logger.info(creature:getName()) + logger.info(creature:getId()) if player == creature then - print('player == creature') + logger.info('player == creature') end end local showInConsole = configManager.getBoolean(configKeys.SCRIPTS_CONSOLE_LOGS); - print(showInConsole) + logger.info(showInConsole) player:sendTextMessage(0, param, 0); - print('end') + logger.info('end') return true end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua deleted file mode 100644 index 489740320..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/libs/functions/game.lua +++ /dev/null @@ -1,9 +0,0 @@ --- function Game.broadcastMessage(message, messageType) --- if not messageType then --- messageType = MESSAGE_GAME_HIGHLIGHT --- end - --- for _, player in ipairs(Game.getPlayers()) do --- player:sendTextMessage(messageType, message) --- end --- end \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua deleted file mode 100644 index c363f1ba3..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/Customs/save_interval.lua +++ /dev/null @@ -1,37 +0,0 @@ --- local function serverSave(interval) --- --if configManager.getBoolean(configKeys.TOGGLE_SAVE_INTERVAL_CLEAN_MAP) then --- -- cleanMap() --- --end - --- saveServer() --- local message = string.format(SAVE_INTERVAL_CONFIG_TIME > 1 and "Server save complete. Next save in %d %ss!" or "Server save complete. Next save in %d %s!", SAVE_INTERVAL_CONFIG_TIME, SAVE_INTERVAL_TYPE) --- Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) --- logger.info(message) --- --Webhook.sendMessage("Server save", message, WEBHOOK_COLOR_WARNING) --- end - --- local save = GlobalEvent("save") - --- function save.onTime(interval) --- local remainingTime = 60 * 1000 --- if configManager.getBoolean(configKeys.TOGGLE_SAVE_INTERVAL) then --- local message = "The server will save all accounts within " .. (remainingTime / 1000) .. " seconds. \z --- You might lag or freeze for 5 seconds, please find a safe place." --- Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) --- logger.info(string.format(message, SAVE_INTERVAL_CONFIG_TIME, SAVE_INTERVAL_TYPE)) --- addEvent(serverSave, remainingTime, interval) --- return true --- end --- return not configManager.getBoolean(configKeys.TOGGLE_SAVE_INTERVAL) --- end - --- if SAVE_INTERVAL_TIME ~= 0 then --- --print(SAVE_INTERVAL_CONFIG_TIME * SAVE_INTERVAL_TIME) --- save:interval(SAVE_INTERVAL_CONFIG_TIME * SAVE_INTERVAL_TIME) --- else --- return logger.error(string.format("[save.onTime] - Save interval type '%s' is not valid, use 'second', 'minute' or 'hour'", SAVE_INTERVAL_TYPE)) --- --print(string.format("[save.onTime] - Save interval type '%s' is not valid, use 'second', 'minute' or 'hour'", SAVE_INTERVAL_TYPE)) --- --return false --- end - --- save:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua deleted file mode 100644 index afa1a04f1..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_one.lua +++ /dev/null @@ -1,7 +0,0 @@ --- local Example_One = GlobalEvent("Example one") --- function Example_One.onStartup() --- print('GlobalEvent Example_One: onStartup') --- return true --- end - --- Example_One:register() \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua deleted file mode 100644 index f5072721e..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/globalevents/example_two.lua +++ /dev/null @@ -1,8 +0,0 @@ --- local Example_Two = GlobalEvent("Example two") --- function Example_Two.onThink(interval) --- print('GlobalEvent Example_Two: onThink') --- return true --- end - --- Example_Two:interval(10000) -- 10 seconds interval --- Example_Two:register() \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua deleted file mode 100644 index f9116b258..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/reward_chest/boss_think.lua +++ /dev/null @@ -1,12 +0,0 @@ --- local bossThink = CreatureEvent("BossThink") - --- function bossThink.onThink(creature, interval) --- print('bossThink.onThink from lua') --- if not creature then --- return true --- end - --- --ResetAndSetTargetList(creature) --- end - --- bossThink:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua deleted file mode 100644 index 39d30e1a4..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/god/benchmark.lua +++ /dev/null @@ -1,42 +0,0 @@ --- local talkAction = TalkAction("/benchmark") - --- function talkAction.onSay(player, words, param) --- do --- local units = { --- ['seconds'] = 1, --- ['milliseconds'] = 1000, --- ['microseconds'] = 1000000, --- ['nanoseconds'] = 1000000000 --- } - --- function benchmark(unit, decPlaces, n, f, ...) --- local elapsed = 0 --- local multiplier = units[unit] --- for i = 1, n do --- local now = os.clock() --- f(...) --- elapsed = elapsed + (os.clock() - now) --- end --- print( --- string.format('Benchmark results: %d calls | %.'.. decPlaces ..'f %s elapsed | %.'.. decPlaces ..'f %s avg execution time.', n, elapsed * multiplier, unit, (elapsed / n) * multiplier, unit)) --- end --- end - --- function test(n) --- local x = "x"; --- local str = "Minha string" .. x --- --local result = 10 + 20; --- -- print(n) --- --local t = {} --- --for i = 1, n do --- --[i] = i --- --local str = "Minha string" .. x --- -- print(i) --- --end --- end - --- benchmark('milliseconds', 2, 500000, test, 1) -- Benchmark results: 500 function calls | 254.96 milliseconds elapsed | 0.51 milliseconds avg execution time. --- end - --- talkAction:separator(" ") --- talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua deleted file mode 100644 index d152c8fb6..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/DataLuaJit/scripts/talkactions/player/help.lua +++ /dev/null @@ -1,48 +0,0 @@ --- local talkAction = TalkAction("!help") - --- function Player.teste(self, message) --- print('message from player:teste() ' .. self:getName() .. ' - ' .. message) --- end - --- local function testEvent1() --- print('testEvent1') --- end - --- local function testEvent2(param) --- print('testEvent2 param: ' .. param) --- end - --- function talkAction.onSay(player, words, param) --- print('executing talkAction from lua: '.. words .. ' ' .. param) - --- player:teste('aaaaaaaaaaaaaa') --- print(player:getName()) --- print(player:getId()) - --- local creature = Creature("Muniz") --- print(creature:getName()) --- print(creature:getId()) - --- if player == creature then --- print('player == creature') --- end - --- local showScriptsLogInConsole = configManager.getBoolean(configKeys.SCRIPTS_CONSOLE_LOGS); - --- print(showScriptsLogInConsole) - --- local evt1Id = addEvent(testEvent1, 1000) --- print('evt1Id: ' .. evt1Id) - --- local evt2Id = addEvent(testEvent1, 8000) --- print('evt2Id: ' .. evt2Id) --- stopEvent(evt2Id) - --- local evt3Id = addEvent(testEvent2, 10000, player:getName()) --- print('evt3Id: ' .. evt3Id) - --- return true --- end - --- talkAction:separator(" ") --- talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs similarity index 93% rename from src/Extensions/NeoServer.Scripts.LuaJIT/ConfigFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs index ccdd55ccc..080b219f3 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs @@ -1,13 +1,13 @@ using LuaNET; -namespace NeoServer.Scripts.LuaJIT; -public class ConfigFunctions : LuaScriptInterface +namespace NeoServer.Scripts.LuaJIT.Functions; +public class ConfigFunctions : LuaScriptInterface, IConfigFunctions { public ConfigFunctions() : base(nameof(ConfigFunctions)) { } - public static void Init(LuaState L) + public void Init(LuaState L) { RegisterTable(L, "configManager"); RegisterMethod(L, "configManager", "getString", LuaConfigManagerGetString); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/CreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs similarity index 98% rename from src/Extensions/NeoServer.Scripts.LuaJIT/CreatureFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs index 1ff41dfeb..c441e753d 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/CreatureFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs @@ -3,7 +3,7 @@ using NeoServer.Game.Creatures.Models.Bases; using NeoServer.Server.Common.Contracts; -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Functions; public class CreatureFunctions : LuaScriptInterface, ICreatureFunctions { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/GlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs similarity index 70% rename from src/Extensions/NeoServer.Scripts.LuaJIT/GlobalFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs index 1a808105d..58073c709 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/GlobalFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs @@ -1,14 +1,14 @@ using LuaNET; -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class GlobalFunctions : LuaScriptInterface +public class GlobalFunctions : LuaScriptInterface, IGlobalFunctions { public GlobalFunctions() : base(nameof(GlobalFunctions)) { } - public static void Init(LuaState L) + public void Init(LuaState L) { RegisterGlobalMethod(L, "rawgetmetatable", LuaRawGetMetatable); } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs similarity index 72% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LoggerFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs index 6fccca3f9..10d11998a 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LoggerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs @@ -1,14 +1,18 @@ using LuaNET; +using Serilog; -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class LoggerFunctions : LuaScriptInterface +public class LoggerFunctions : LuaScriptInterface, ILoggerFunctions { - public LoggerFunctions() : base(nameof(LoggerFunctions)) + private static ILogger _logger; + + public LoggerFunctions(ILogger logger) : base(nameof(LoggerFunctions)) { + _logger = logger; } - public static void Init(LuaState L) + public void Init(LuaState L) { RegisterTable(L, "logger"); RegisterMethod(L, "logger", "info", LuaLoggerInfo); @@ -22,7 +26,7 @@ private static int LuaLoggerInfo(LuaState L) // logger.info(text) if (IsString(L, 1)) { - Logger.GetInstance().Info(GetFormatedLoggerMessage(L)); + _logger.Information(GetFormatedLoggerMessage(L)); } else { @@ -36,7 +40,7 @@ private static int LuaLoggerWarn(LuaState L) // logger.info(text) if (IsString(L, 1)) { - Logger.GetInstance().Warn(GetFormatedLoggerMessage(L)); + _logger.Warning(GetFormatedLoggerMessage(L)); } else { @@ -50,7 +54,7 @@ private static int LuaLoggerError(LuaState L) // logger.info(text) if (IsString(L, 1)) { - Logger.GetInstance().Error(GetFormatedLoggerMessage(L)); + _logger.Error(GetFormatedLoggerMessage(L)); } else { @@ -64,7 +68,7 @@ private static int LuaLoggerDebug(LuaState L) // logger.info(text) if (IsString(L, 1)) { - Logger.GetInstance().Debug(GetFormatedLoggerMessage(L)); + _logger.Debug(GetFormatedLoggerMessage(L)); } else { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs similarity index 96% rename from src/Extensions/NeoServer.Scripts.LuaJIT/PlayerFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs index fbcd3795f..8bff8b717 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/PlayerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs @@ -2,15 +2,15 @@ using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class PlayerFunctions : LuaScriptInterface +public class PlayerFunctions : LuaScriptInterface, IPlayerFunctions { public PlayerFunctions() : base(nameof(PlayerFunctions)) { } - public static void Init(LuaState L) + public void Init(LuaState L) { RegisterSharedClass(L, "Player", "Creature", LuaPlayerCreate); RegisterMetaMethod(L, "Player", "__eq", LuaUserdataCompare); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs similarity index 97% rename from src/Extensions/NeoServer.Scripts.LuaJIT/TalkActionFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs index 50474106a..9bca51b9f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs @@ -1,14 +1,14 @@ using LuaNET; -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class TalkActionFunctions : LuaScriptInterface +public class TalkActionFunctions : LuaScriptInterface, ITalkActionFunctions { public TalkActionFunctions() : base(nameof(TalkActionFunctions)) { } - public static void Init(LuaState L) + public void Init(LuaState L) { RegisterSharedClass(L, "TalkAction", "", LuaCreateTalkAction); RegisterMethod(L, "TalkAction", "onSay", LuaTalkActionOnSay); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ICreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IBaseFunctions.cs similarity index 70% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ICreatureFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IBaseFunctions.cs index 25d01f903..20b6cedca 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ICreatureFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IBaseFunctions.cs @@ -2,7 +2,7 @@ namespace NeoServer.Scripts.LuaJIT; -public interface ICreatureFunctions +public interface IBaseFunctions { void Init(LuaState L); } \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs new file mode 100644 index 000000000..f52668898 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IConfigFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs new file mode 100644 index 000000000..34f831771 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface ICreatureFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs new file mode 100644 index 000000000..d399dc267 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IGlobalFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs new file mode 100644 index 000000000..fcc529669 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface ILoggerFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs new file mode 100644 index 000000000..d7e846088 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IPlayerFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs new file mode 100644 index 000000000..90fe55f07 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface ITalkActionFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs index 087912212..c4d16c0a9 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using NeoServer.Scripts.LuaJIT; +using NeoServer.Scripts.LuaJIT.Functions; namespace NeoServer.Server.Standalone.IoC.Modules; @@ -12,7 +13,12 @@ public static IServiceCollection AddLuaJIT(this IServiceCollection builder) builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); // return builder; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index 01391ee03..f68c47977 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -2,8 +2,8 @@ using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Scripts.LuaJIT.Functions; using Serilog; -using System.Reflection.Metadata; namespace NeoServer.Scripts.LuaJIT; @@ -35,10 +35,15 @@ public class LuaGameManager : ILuaGameManager /// private readonly IScripts _scripts; - private readonly ICreatureFunctions _creatureFunctions; - private readonly ITalkActions _talkActions; + private readonly IConfigFunctions _configFunctions; + private readonly ICreatureFunctions _creatureFunctions; + private readonly IGlobalFunctions _globalFunctions; + private readonly ILoggerFunctions _loggerFunctions; + private readonly IPlayerFunctions _playerFunctions; + private readonly ITalkActionFunctions _talkActionFunctions; + #endregion #region Constructors @@ -48,22 +53,60 @@ public LuaGameManager( ILuaEnvironment luaEnviroment, IConfigManager configManager, IScripts scripts, + ITalkActions talkActions, + IConfigFunctions configFunctions, ICreatureFunctions creatureFunctions, - ITalkActions talkActions) + IGlobalFunctions globalFunctions, + ILoggerFunctions loggerFunctions, + IPlayerFunctions playerFunctions, + ITalkActionFunctions talkActionFunctions) { _logger = logger; _luaEnviroment = luaEnviroment; _configManager = configManager; _scripts = scripts; - _creatureFunctions = creatureFunctions; _talkActions = talkActions; + + _configFunctions = configFunctions; + _creatureFunctions = creatureFunctions; + _globalFunctions = globalFunctions; + _loggerFunctions = loggerFunctions; + _playerFunctions = playerFunctions; + _talkActionFunctions = talkActionFunctions; + + Start(); } #endregion #region Public Methods - public void Start() + public bool PlayerSaySpell(IPlayer player, SpeechType type, string words) + { + var wordsSeparator = " "; + var talkactionWords = words.Contains(wordsSeparator) ? words.Split(" ") : [words]; + + if (!talkactionWords.Any()) + return false; + + var talkAction = _talkActions.GetTalkAction(talkactionWords[0]); + + if (talkAction == null) + return false; + + var parameter = ""; + + if (talkactionWords.Count() > 1) + parameter = talkactionWords[1]; + + return talkAction.ExecuteSay(player, talkactionWords[0], parameter, type); + } + + #endregion + + #region Private Methods + + private void Start() { var dir = AppContext.BaseDirectory; @@ -82,62 +125,30 @@ public void Start() Lua.OpenLibs(luaState); + //TODO: load all functions //CoreFunctions.Init(L); - //CreatureFunctions.Init(L); //EventFunctions.Init(L); //ItemFunctions.Init(L); //MapFunctions.Init(L); //ZoneFunctions.Init(L); - - LoggerFunctions.Init(luaState); - ConfigFunctions.Init(luaState); - GlobalFunctions.Init(luaState); - TalkActionFunctions.Init(luaState); - - //CreatureFunctions.Init(luaState); - _creatureFunctions.Init(luaState); - - PlayerFunctions.Init(luaState); - //GameFunctions.Init(L); - //CreatureFunctions.Init(L); - //PlayerFunctions.Init(L); - //ActionFunctions.Init(L); //GlobalEventFunctions.Init(L); //CreatureEventsFunctions.Init(L); - ModulesLoadHelper(_configManager.Load($"{dir}\\config.lua"), $"config.lua"); - - ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}\\DataLuaJit/core.lua", "core.lua"), "core.lua"); - - ModulesLoadHelper(_scripts.LoadScripts($"{dir}\\DataLuaJit/scripts", false, false), "/DataLuaJit/scripts"); - } - - public bool PlayerSaySpell(IPlayer player, SpeechType type, string words) - { - var wordsSeparator = " "; - var talkactionWords = words.Contains(wordsSeparator) ? words.Split(" ") : [words]; - - if (!talkactionWords.Any()) - return false; - - var talkAction = _talkActions.GetTalkAction(talkactionWords[0]); + _configFunctions.Init(luaState); + _creatureFunctions.Init(luaState); + _globalFunctions.Init(luaState); + _loggerFunctions.Init(luaState); + _playerFunctions.Init(luaState); + _talkActionFunctions.Init(luaState); - if (talkAction == null) - return false; + ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); - var parameter = ""; + ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}/Data/LuaJit/core.lua", "core.lua"), "core.lua"); - if (talkactionWords.Count() > 1) - parameter = talkactionWords[1]; - - return talkAction.ExecuteSay(player, talkactionWords[0], parameter, type); + ModulesLoadHelper(_scripts.LoadScripts($"{dir}/Data/LuaJit/scripts", false, false), "/Data/LuaJit/scripts"); } - #endregion - - #region Private Methods - private void ModulesLoadHelper(bool loaded, string moduleName) { _logger.Information($"Loaded {moduleName}"); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj index 4303335c2..c8eebb344 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj @@ -20,49 +20,28 @@ Always - + Always - + Always - + Always - + Always - + Always - + Always - + Always - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - + Always diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua index 66767af56..46df2da48 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua @@ -1,4 +1,4 @@ -coreDirectory = "DataLuaJit" +coreDirectory = "Data/LuaJit" showScriptsLogInConsole = true -- NOTE: true will allow the /reload command to be used @@ -17,18 +17,16 @@ toggleSaveIntervalCleanMap = true saveIntervalTime = 1 -- Connection Config --- NOTE: allowOldProtocol can allow login on 10x protocol. (11.00) -- NOTE: maxPlayers set to 0 means no limit -- NOTE: MaxPacketsPerSeconds if you change you will be subject to bugs by WPE, keep the default value of 25 ip = "127.0.0.1" -allowOldProtocol = false bindOnlyGlobalAddress = false loginProtocolPort = 7171 gameProtocolPort = 7172 statusProtocolPort = 7171 maxPlayers = 0 -serverName = "OTServBR-Global" -serverMotd = "Welcome to the OTServBR-Global!" +serverName = "OpenCoreMMO" +serverMotd = "Welcome to theOpenCoreMMO!" onePlayerOnlinePerAccount = true statusTimeout = 5 * 1000 replaceKickOnLogin = true diff --git a/src/Standalone/Program.cs b/src/Standalone/Program.cs index bb8b465be..d0fee7cbe 100644 --- a/src/Standalone/Program.cs +++ b/src/Standalone/Program.cs @@ -113,8 +113,6 @@ public static async Task Main() container.Resolve().Open(); - container.Resolve().Start(); - sw.Stop(); logger.Step("Running Garbage Collector", "Garbage collected", () => From 164f04ef4b857e20787d1797431a34973ed96c34 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Mon, 23 Dec 2024 21:19:23 -0300 Subject: [PATCH 04/16] feat: some functions implementations to load actions works from luajit. --- .../Player/UseItem/PlayerUseItemCommand.cs | 14 +- .../Player/UseItem/PlayerUseItemOnCommand.cs | 12 +- .../UseItem/PlayerUseItemOnCreatureCommand.cs | 16 +- .../Contracts/Scripts/ILuaGameManager.cs | 5 + .../Player/NotificationSentEventHandler.cs | 14 +- .../Subscribers/EventSubscriber.cs | 1 + .../NeoServer.Server/NeoServer.Server.csproj | 14 +- .../Services/NotificationSenderService.cs | 7 +- .../LuaGlobalRegister.cs | 1 + .../NeoServer.Scripts.LuaJIT/Action.cs | 164 +++++ .../NeoServer.Scripts.LuaJIT/Actions.cs | 634 ++++++++++++++++++ .../NeoServer.Scripts.LuaJIT/ConfigManager.cs | 2 +- .../Data/LuaJit/global.lua | 9 + .../Data/LuaJit/libs/functions/item.lua | 3 + .../Data/LuaJit/libs/functions/load.lua | 6 +- .../Data/LuaJit/libs/functions/player.lua | 7 + .../Data/LuaJit/libs/functions/position.lua | 72 ++ .../LuaJit/libs/functions/revscriptsys.lua | 56 +- .../Data/LuaJit/libs/functions/tile.lua | 32 + .../LuaJit/scripts/actions/Tools/shovel.lua | 51 +- .../scripts/actions/items/ladder_up.lua | 22 + .../LuaJit/scripts/actions/tools/rope.lua | 55 ++ .../LuaJit/scripts/talkactions/god/reload.lua | 170 +++-- .../Extensions/ReturnMessageExtensions.cs | 225 +++++++ .../Functions/ActionFunctions.cs | 108 +++ .../Functions/ConfigFunctions.cs | 5 +- .../Functions/CreatureFunctions.cs | 23 +- .../Functions/EnumFunctions.cs | 64 ++ .../Functions/GameFunctions.cs | 71 ++ .../Functions/GlobalFunctions.cs | 5 +- .../Functions/ItemFunctions.cs | 117 ++++ .../Functions/ItemTypeFunctions.cs | 97 +++ .../Functions/LoggerFunctions.cs | 3 +- .../Functions/PlayerFunctions.cs | 120 +++- .../Functions/PositionFunctions.cs | 91 +++ .../Functions/TalkActionFunctions.cs | 1 + .../Functions/TileFunctions.cs | 169 +++++ .../Interfaces/Functions/IActionFunctions.cs | 5 + .../Interfaces/Functions/IEnumFunctions.cs | 5 + .../Interfaces/Functions/IGameFunctions.cs | 5 + .../Interfaces/Functions/IItemFunctions.cs | 5 + .../Functions/IItemTypeFunctions.cs | 5 + .../Functions/IPositionFunctions.cs | 5 + .../Interfaces/Functions/ITileFunctions.cs | 5 + .../Interfaces/IActions.cs | 56 ++ .../Interfaces/ILuaManager.cs | 6 - .../IoC/Modules/LuaJITInjection.cs | 11 +- .../ItemsDefinitions.cs | 594 ++++++++++++++++ .../NeoServer.Scripts.LuaJIT/Logger.cs | 72 +- .../LuaDefinitions.cs | 70 +- .../LuaFunctionsLoader.cs | 79 ++- .../LuaGameManager.cs | 92 ++- .../NeoServer.Scripts.LuaJIT.csproj | 21 +- .../NeoServer.Scripts.LuaJIT/Script.cs | 6 +- .../ScriptEnvironment.cs | 19 +- .../NeoServer.Scripts.LuaJIT/Scripts.cs | 22 +- .../Structs/Entities.cs | 216 ------ .../NeoServer.Scripts.LuaJIT/TalkAction.cs | 1 + .../UtilsDefinitions.cs | 49 +- .../NeoServer.Scripts.LuaJIT/config.lua | 4 +- .../Contracts/Creatures/IPlayer.cs | 1 + .../Contracts/Items/IItem.cs | 17 + .../Location/Direction.cs | 4 +- .../NeoServer.Game.Creatures/Player/Player.cs | 2 + .../TileRule/TileRuleLoader.cs | 2 +- 65 files changed, 3231 insertions(+), 614 deletions(-) rename src/{GameWorldSimulator/NeoServer.Game.Common => ApplicationServer/NeoServer.Server}/Services/NotificationSenderService.cs (62%) create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/item.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/player.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/position.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/tile.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/items/ladder_up.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/tools/rope.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/ItemsDefinitions.cs diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs index 19022586b..52fe2d990 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs @@ -1,10 +1,12 @@ using System; +using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items.Types.Containers; using NeoServer.Game.Common.Contracts.Items.Types.Usable; using NeoServer.Game.Common.Contracts.Services; using NeoServer.Game.Common.Location; using NeoServer.Networking.Packets.Incoming; +using NeoServer.Networking.Packets.Incoming.Shop; using NeoServer.Server.Common.Contracts.Commands; namespace NeoServer.Server.Commands.Player.UseItem; @@ -14,19 +16,27 @@ public class PlayerUseItemCommand : ICommand private readonly ItemFinderService _itemFinderService; private readonly PlayerOpenDepotCommand _playerOpenDepotCommand; private readonly IPlayerUseService _playerUseService; + private readonly ILuaGameManager _luaGameManager; - public PlayerUseItemCommand(IPlayerUseService playerUseService, PlayerOpenDepotCommand playerOpenDepotCommand, - ItemFinderService itemFinderService) + public PlayerUseItemCommand( + IPlayerUseService playerUseService, + PlayerOpenDepotCommand playerOpenDepotCommand, + ItemFinderService itemFinderService, + ILuaGameManager luaGameManager) { _playerUseService = playerUseService; _playerOpenDepotCommand = playerOpenDepotCommand; _itemFinderService = itemFinderService; + _luaGameManager = luaGameManager; } public void Execute(IPlayer player, UseItemPacket useItemPacket) { var item = _itemFinderService.Find(player, useItemPacket.Location, useItemPacket.ClientId); + if (_luaGameManager.PlayerUseItem(player, useItemPacket.Location, useItemPacket.StackPosition, useItemPacket.Index, item)) + return; + Action action; switch (item) diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs index f2c371855..e0e35b804 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs @@ -1,4 +1,5 @@ using System; +using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Contracts.Items.Types.Usable; @@ -16,12 +17,18 @@ public class PlayerUseItemOnCommand : ICommand private readonly IPlayerUseService _playerUseService; private readonly IGameServer game; private readonly HotkeyService hotkeyService; + private readonly ILuaGameManager _luaGameManager; - public PlayerUseItemOnCommand(IGameServer game, HotkeyService hotkeyService, IPlayerUseService playerUseService) + public PlayerUseItemOnCommand( + IGameServer game, + HotkeyService hotkeyService, + IPlayerUseService playerUseService, + ILuaGameManager luaGameManager) { this.game = game; this.hotkeyService = hotkeyService; _playerUseService = playerUseService; + _luaGameManager = luaGameManager; } public void Execute(IPlayer player, UseItemOnPacket useItemPacket) @@ -75,6 +82,9 @@ public void Execute(IPlayer player, UseItemOnPacket useItemPacket) if (thingToUse is not IUsableOn itemToUse) return; + if (_luaGameManager.PlayerUseItemEx(player, player.Location, useItemPacket.Location, useItemPacket.ToStackPosition, itemToUse, useItemPacket.Location.IsHotkey, onItem != null ? onTile : onTile)) + return; + Action action = onTile is not null ? () => _playerUseService.Use(player, itemToUse, onTile) : () => _playerUseService.Use(player, itemToUse, onItem); diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs index fa48a7580..963f36266 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs @@ -1,8 +1,10 @@ -using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Application.Common.Contracts.Scripts; +using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Contracts.Items.Types.Usable; using NeoServer.Game.Common.Contracts.Services; using NeoServer.Game.Common.Location; +using NeoServer.Game.Items.Bases; using NeoServer.Networking.Packets.Incoming; using NeoServer.Server.Common.Contracts; using NeoServer.Server.Common.Contracts.Commands; @@ -14,13 +16,18 @@ public class PlayerUseItemOnCreatureCommand : ICommand private readonly IPlayerUseService _playerUseService; private readonly IGameServer game; private readonly HotkeyService hotKeyService; + private readonly ILuaGameManager _luaGameManager; - public PlayerUseItemOnCreatureCommand(IGameServer game, HotkeyService hotKeyCache, - IPlayerUseService playerUseService) + public PlayerUseItemOnCreatureCommand( + IGameServer game, + HotkeyService hotKeyCache, + IPlayerUseService playerUseService, + ILuaGameManager luaGameManager) { this.game = game; hotKeyService = hotKeyCache; _playerUseService = playerUseService; + _luaGameManager = luaGameManager; } public void Execute(IPlayer player, UseItemOnCreaturePacket useItemPacket) @@ -31,6 +38,9 @@ public void Execute(IPlayer player, UseItemOnCreaturePacket useItemPacket) if (itemToUse is not IUsableOn useableOn) return; + if (_luaGameManager.PlayerUseItemWithCreature(player, player.Location, useItemPacket.FromStackPosition, creature, useableOn)) + return; + var action = () => _playerUseService.Use(player, useableOn, creature); if (useItemPacket.FromLocation.Type == LocationType.Ground) diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs index a24f59ace..6d942eb81 100644 --- a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs +++ b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs @@ -1,9 +1,14 @@ using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Location.Structs; namespace NeoServer.Application.Common.Contracts.Scripts; public interface ILuaGameManager { bool PlayerSaySpell(IPlayer player, SpeechType type, string text); + bool PlayerUseItem(IPlayer player, Location pos, byte stackpos, byte index, IItem item); + bool PlayerUseItemWithCreature(IPlayer player, Location fromPos, byte fromStackPos, ICreature creature, IItem item); + bool PlayerUseItemEx(IPlayer player, Location fromPos, Location toPos, byte toStackPos, IItem item, bool isHotkey, IThing target); } diff --git a/src/ApplicationServer/NeoServer.Server.Events/Player/NotificationSentEventHandler.cs b/src/ApplicationServer/NeoServer.Server.Events/Player/NotificationSentEventHandler.cs index aa0944533..aa464a45c 100644 --- a/src/ApplicationServer/NeoServer.Server.Events/Player/NotificationSentEventHandler.cs +++ b/src/ApplicationServer/NeoServer.Server.Events/Player/NotificationSentEventHandler.cs @@ -1,5 +1,4 @@ -using NeoServer.Game.Common; -using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Networking.Packets.Outgoing; using NeoServer.Server.Common.Contracts; @@ -15,20 +14,13 @@ public NotificationSentEventHandler(IGameServer game) this.game = game; } - public void Execute(IThing thing, string message, NotificationType notificationType) + public void Execute(IThing thing, string message, TextMessageOutgoingType type) { if (thing is not IPlayer player) return; if (!game.CreatureManager.GetPlayerConnection(player.CreatureId, out var connection)) return; - var type = notificationType switch - { - NotificationType.Description => TextMessageOutgoingType.Description, - NotificationType.Information => TextMessageOutgoingType.MESSAGE_EVENT_LEVEL_CHANGE, - _ => TextMessageOutgoingType.Description - }; - connection.OutgoingPackets.Enqueue(new TextMessagePacket(message, type)); connection.Send(); } -} \ No newline at end of file +} diff --git a/src/ApplicationServer/NeoServer.Server.Events/Subscribers/EventSubscriber.cs b/src/ApplicationServer/NeoServer.Server.Events/Subscribers/EventSubscriber.cs index de1b0711a..1e84e7665 100644 --- a/src/ApplicationServer/NeoServer.Server.Events/Subscribers/EventSubscriber.cs +++ b/src/ApplicationServer/NeoServer.Server.Events/Subscribers/EventSubscriber.cs @@ -16,6 +16,7 @@ using NeoServer.Server.Events.Player.Trade; using NeoServer.Server.Events.Server; using NeoServer.Server.Events.Tiles; +using NeoServer.Server.Services; namespace NeoServer.Server.Events.Subscribers; diff --git a/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj b/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj index 27e2e49e7..870523d97 100644 --- a/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj +++ b/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj @@ -7,20 +7,20 @@ - - - + + + - - + + - - + + diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Services/NotificationSenderService.cs b/src/ApplicationServer/NeoServer.Server/Services/NotificationSenderService.cs similarity index 62% rename from src/GameWorldSimulator/NeoServer.Game.Common/Services/NotificationSenderService.cs rename to src/ApplicationServer/NeoServer.Server/Services/NotificationSenderService.cs index 95a18dc91..9dd014d62 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Services/NotificationSenderService.cs +++ b/src/ApplicationServer/NeoServer.Server/Services/NotificationSenderService.cs @@ -1,16 +1,17 @@ using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Networking.Packets.Outgoing; -namespace NeoServer.Game.Common.Services; +namespace NeoServer.Server.Services; public static class NotificationSenderService { public static event SendNotification OnNotificationSent; public static void Send(IPlayer to, string message, - NotificationType notificationType = NotificationType.Description) + TextMessageOutgoingType notificationType = TextMessageOutgoingType.Description) { OnNotificationSent?.Invoke(to, message, notificationType); } } -public delegate void SendNotification(IPlayer player, string message, NotificationType notificationType); \ No newline at end of file +public delegate void SendNotification(IPlayer player, string message, TextMessageOutgoingType notificationType); \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.Lua/LuaGlobalRegister.cs b/src/Extensions/NeoServer.Scripts.Lua/LuaGlobalRegister.cs index 2da95d78c..c0bb9532f 100644 --- a/src/Extensions/NeoServer.Scripts.Lua/LuaGlobalRegister.cs +++ b/src/Extensions/NeoServer.Scripts.Lua/LuaGlobalRegister.cs @@ -11,6 +11,7 @@ using NeoServer.Server.Common.Contracts; using NeoServer.Server.Configurations; using NeoServer.Server.Helpers.Extensions; +using NeoServer.Server.Services; using NLua; using Serilog; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs new file mode 100644 index 000000000..8d099be95 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs @@ -0,0 +1,164 @@ +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Structs; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT; + +public class Action : Script +{ + private bool _allowFarUse = false; + private bool _checkFloor = true; + private bool _checkLineOfSight = true; + + private List _itemIds = new List(); + private List _uniqueIds = new List(); + private List _actionIds = new List(); + private List _positions = new List(); + + //private Func useFunction = null; + + public Action(LuaScriptInterface scriptInterface) : base(scriptInterface) + { + } + + // Scripting + //public virtual bool ExecuteUse( + // Player player, Item item, Position fromPosition, Thing target, Position toPosition, bool isHotkey) + //{ + // //return useFunction?.Invoke(player, item, fromPosition, target, toPosition, isHotkey) ?? false; + //} + + public bool ExecuteUse(IPlayer player, IItem item, Location fromPosition, IThing target, Location toPosition, bool isHotkey) + { + // onUse(player, item, fromPosition, target, toPosition, isHotkey) + if (!GetScriptInterface().InternalReserveScriptEnv()) + { + Console.WriteLine($"[Action::executeUse - Player {player.Name}, on item {item.Name}] " + + $"Call stack overflow. Too many lua script calls being nested. Script name {GetScriptInterface().GetLoadingScriptName()}"); + return false; + } + + var scriptInterface = GetScriptInterface(); + var scriptEnvironment = scriptInterface.InternalGetScriptEnv(); + scriptEnvironment.SetScriptId(GetScriptId(), GetScriptInterface()); + + var L = GetScriptInterface().GetLuaState(); + GetScriptInterface().PushFunction(GetScriptId()); + + LuaScriptInterface.PushUserdata(L, player); + LuaScriptInterface.SetMetatable(L, -1, "Player"); + + LuaScriptInterface.PushThing(L, item); + LuaScriptInterface.PushPosition(L, fromPosition); + + LuaScriptInterface.PushThing(L, target); + LuaScriptInterface.PushPosition(L, toPosition); + + LuaScriptInterface.PushBoolean(L, isHotkey); + + return GetScriptInterface().CallFunction(6); + } + + public bool AllowFarUse + { + get { return _allowFarUse; } + set { _allowFarUse = value; } + } + + public bool CheckLineOfSight + { + get { return _checkLineOfSight; } + set { _checkLineOfSight = value; } + } + + public bool CheckFloor + { + get { return _checkFloor; } + set { _checkFloor = value; } + } + + public List ItemIdsVector + { + get { return _itemIds; } + } + + public void SetItemIdsVector(ushort id) + { + _itemIds.Add(id); + } + + public List UniqueIdsVector + { + get { return _uniqueIds; } + } + + public void SetUniqueIdsVector(ushort id) + { + _uniqueIds.Add(id); + } + + public List ActionIdsVector + { + get { return _actionIds; } + } + + public void SetActionIdsVector(ushort id) + { + _actionIds.Add(id); + } + + public List PositionsVector + { + get { return _positions; } + } + + public void SetPositionsVector(Location pos) + { + _positions.Add(pos); + } + + public bool HasPosition(Location position) + { + return _positions.Exists(p => p.Equals(position)); + } + + public List GetPositions() + { + return _positions; + } + + public void SetPositions(Location pos) + { + _positions.Add(pos); + } + + public virtual ReturnValueType CanExecuteAction(IPlayer player, Location toPos) + { + //if (!allowFarUse) + //{ + // return g_actions().canUse(player, toPos); + //} + + //return g_actions().canUseFar(player, toPos, checkLineOfSight, checkFloor); + + return default(ReturnValueType); + } + + //public override bool HasOwnErrorHandler() + //{ + // return false; + //} + + //public virtual Thing GetTarget(Player player, Creature targetCreature, Position toPosition, byte toStackPos) + //{ + // // Implement the logic for GetTarget + // return null; + //} + + private string GetScriptTypeName() + { + return "onUse"; + } +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs new file mode 100644 index 000000000..4a368a89c --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs @@ -0,0 +1,634 @@ +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Location.Structs; +using Serilog; +using System.Text; + +namespace NeoServer.Scripts.LuaJIT; + +public class Actions : Scripts, IActions +{ + #region Injection + + //private static Actions _instance; + //public static Actions GetInstance() => _instance == null ? _instance = new Actions() : _instance; + + #endregion + + #region Members + + private readonly Dictionary _useItemMap = new Dictionary(); + private readonly Dictionary _uniqueItemMap = new Dictionary(); + private readonly Dictionary _actionItemMap = new Dictionary(); + private readonly Dictionary _actionPositionMap = new Dictionary(); + + #endregion + + #region Constructors + + public Actions(ILogger logger) : base(logger) + { + //_instance = this; + } + + #endregion + + #region Public Methods + public bool RegisterLuaItemEvent(Action action) + { + var itemIdVector = action.ItemIdsVector; + if (!itemIdVector.Any()) + { + return false; + } + + var tmpVector = new List(itemIdVector.Count); + + foreach (var itemId in itemIdVector) + { + // Check if the item is already registered and prevent it from being registered again + if (HasItemId(itemId)) + { + _logger.Warning( + $"{nameof(RegisterLuaItemEvent)} - Duplicate registered item with id: {itemId} in range from id: {itemIdVector.First()}, to id: {itemIdVector.Last()}, for script: {action.GetScriptInterface().GetLoadingScriptName()}" + ); + continue; + } + + // Register item in the action item map + SetItemId(itemId, action); + tmpVector.Add(itemId); + } + + itemIdVector = tmpVector; + return itemIdVector.Count > 0; + } + + public bool RegisterLuaUniqueEvent(Action action) + { + var uniqueIdVector = action.UniqueIdsVector; + if (!uniqueIdVector.Any()) + { + return false; + } + + var tmpVector = new List(uniqueIdVector.Count); + + foreach (var uniqueId in uniqueIdVector) + { + // Check if the unique is already registered and prevent it from being registered again + if (!HasUniqueId(uniqueId)) + { + // Register unique id in the unique item map + SetUniqueId(uniqueId, action); + tmpVector.Add(uniqueId); + } + else + { + _logger.Warning( + $"{nameof(RegisterLuaUniqueEvent)} - Duplicate registered item with uid: {uniqueId} in range from uid: {uniqueIdVector.First()}, to uid: {uniqueIdVector.Last()}, for script: {action.GetScriptInterface().GetLoadingScriptName()}" + ); + } + } + + uniqueIdVector = tmpVector; + return uniqueIdVector.Count > 0; + } + + public bool RegisterLuaActionEvent(Action action) + { + var actionIdVector = action.ActionIdsVector; + if (!actionIdVector.Any()) + { + return false; + } + + var tmpVector = new List(actionIdVector.Count); + + foreach (var actionId in actionIdVector) + { + // Check if the action is already registered and prevent it from being registered again + if (!HasActionId(actionId)) + { + // Register action in the action item map + SetActionId(actionId, action); + tmpVector.Add(actionId); + } + else + { + _logger.Warning( + $"{nameof(RegisterLuaActionEvent)} - Duplicate registered item with aid: {actionId} in range from aid: {actionIdVector.First()}, to aid: {actionIdVector.Last()}, for script: {action.GetScriptInterface().GetLoadingScriptName()}" + ); + } + } + + actionIdVector = tmpVector; + return actionIdVector.Count > 0; + } + + public bool RegisterLuaPositionEvent(Action action) + { + var positionVector = action.PositionsVector; + if (!positionVector.Any()) + { + return false; + } + + var tmpVector = new List(positionVector.Count); + + foreach (var position in positionVector) + { + // Check if the position is already registered and prevent it from being registered again + if (!HasPosition(position)) + { + // Register position in the action position map + SetPosition(position, action); + tmpVector.Add(position); + } + else + { + _logger.Warning( + $"{nameof(RegisterLuaPositionEvent)} - Duplicate registered script with range position: {position.ToString()}, for script: {action.GetScriptInterface().GetLoadingScriptName()}" + ); + } + } + + positionVector = tmpVector; + return positionVector.Count > 0; + } + + public bool RegisterLuaEvent(Action action) + { + // Call all register lua events + if (RegisterLuaItemEvent(action) || RegisterLuaUniqueEvent(action) || RegisterLuaActionEvent(action) || RegisterLuaPositionEvent(action)) + { + return true; + } + else + { + _logger.Warning( + $"{nameof(RegisterLuaEvent)} - Missing id/aid/uid/position for one script event, for script: {action.GetScriptInterface().GetLoadingScriptName()}" + ); + return false; + } + _logger.Information($"{nameof(RegisterLuaEvent)} - Missing or incorrect script: {action.GetScriptInterface().GetLoadingScriptName()}"); + return false; + } + + public bool UseItem(IPlayer player, Location pos, byte index, IItem item, bool isHotkey) + { + //if (item == null) + // throw new ArgumentNullException(nameof(item)); + + //var it = Item.items[item.ID]; + + //if (it.isRune() || it.type == ItemType.Potion) + //{ + // if (player.walkExhausted()) + // { + // player.sendCancelMessage(ReturnValue.YOUAREEXHAUSTED); + // return false; + // } + //} + + //if (isHotkey) + //{ + // ushort subType = item.SubType; + // ShowUseHotkeyMessage(player, item, player.GetItemTypeCount(item.ID, subType != item.Count ? subType : -1)); + //} + + //ReturnValue ret = InternalUseItem(player, pos, index, item, isHotkey); + //if (ret != ReturnValue.NOERROR) + //{ + // player.sendCancelMessage(ret); + // return false; + //} + + //if (it.isRune() || it.type == ItemType.Potion) + //{ + // player.setNextPotionAction(OTSYS_TIME() + g_configManager().getNumber(ACTIONS_DELAY_INTERVAL, nameof(Actions))); + //} + //else + //{ + // player.setNextAction(OTSYS_TIME() + g_configManager().getNumber(ACTIONS_DELAY_INTERVAL, nameof(Actions))); + //} + + //// only send cooldown icon if it's a multi-use item + //if (it.isMultiUse()) + //{ + // player.sendUseItemCooldown(g_configManager().getNumber(ACTIONS_DELAY_INTERVAL, nameof(Actions))); + //} + + return true; + } + + public bool UseItemEx(IPlayer player, Location fromPos, Location toPos, byte toStackPos, IItem item, bool isHotkey, ICreature creature = null) + { + //var it = Item.items[item.ID]; + //if (it.isRune() || it.type == ItemType.Potion) + //{ + // if (player.walkExhausted()) + // { + // player.sendCancelMessage(ReturnValue.YOUAREEXHAUSTED); + // return false; + // } + //} + + //var action = GetAction(item); + //if (action == null) + //{ + // player.sendCancelMessage(ReturnValue.CANNOTUSETHISOBJECT); + // return false; + //} + + //ReturnValue ret = action.canExecuteAction(player, toPos); + //if (ret != ReturnValue.NOERROR) + //{ + // player.sendCancelMessage(ret); + // return false; + //} + + //if (isHotkey) + //{ + // ushort subType = item.SubType; + // ShowUseHotkeyMessage(player, item, player.GetItemTypeCount(item.ID, subType != item.Count ? subType : -1)); + //} + + //if (action.useFunction != null) + //{ + // if (action.useFunction(player, item, fromPos, action.getTarget(player, creature, toPos, toStackPos), toPos, isHotkey)) + // { + // return true; + // } + // return false; + //} + + //if (!action.executeUse(player, item, fromPos, action.getTarget(player, creature, toPos, toStackPos), toPos, isHotkey)) + //{ + // if (!action.hasOwnErrorHandler()) + // { + // player.sendCancelMessage(ReturnValue.CANNOTUSETHISOBJECT); + // } + // return false; + //} + + //if (it.isRune() || it.type == ItemType.Potion) + //{ + // player.setNextPotionAction(OTSYS_TIME() + g_configManager().getNumber(EX_ACTIONS_DELAY_INTERVAL, nameof(Actions))); + //} + //else + //{ + // player.setNextAction(OTSYS_TIME() + g_configManager().getNumber(EX_ACTIONS_DELAY_INTERVAL, nameof(Actions))); + //} + + //if (it.isMultiUse()) + //{ + // player.sendUseItemCooldown(g_configManager().getNumber(EX_ACTIONS_DELAY_INTERVAL, nameof(Actions))); + //} + return true; + } + + public ReturnValueType CanUse(IPlayer player, Location pos) + { + //if (pos.X != 0xFFFF) + //{ + // var playerPos = player.Location; + // if (playerPos.Z != pos.Z) + // { + // return playerPos.Z > pos.Z ? ReturnValue.FIRSTGOUPSTAIRS : ReturnValue.FIRSTGODOWNSTAIRS; + // } + + // if (!Location.areInRange < 1, 1 > (playerPos, pos)) + // { + // return ReturnValue.TOOFARAWAY; + // } + //} + return ReturnValueType.RETURNVALUE_NOERROR; + } + + public ReturnValueType CanUse(IPlayer player, Location pos, IItem item) + { + //var action = GetAction(item); + //if (action != null) + //{ + // return action.canExecuteAction(player, pos); + //} + return ReturnValueType.RETURNVALUE_NOERROR; + } + + public ReturnValueType CanUseFar(ICreature creature, Location toPos, bool checkLineOfSight, bool checkFloor) + { + //if (toPos.x == 0xFFFF) + //{ + // return ReturnValueType.RETURNVALUE_NOERROR; + //} + + //Position creaturePos = creature.getPosition(); + //if (checkFloor && creaturePos.z != toPos.z) + //{ + // return creaturePos.z > toPos.z ? ReturnValue.FIRSTGOUPSTAIRS : ReturnValue.FIRSTGODOWNSTAIRS; + //} + + //if (!Position.areInRange < 7, 5 > (toPos, creaturePos)) + //{ + // return ReturnValue.TOOFARAWAY; + //} + + //if (checkLineOfSight && !g_game().canThrowObjectTo(creaturePos, toPos)) + //{ + // return ReturnValueType.RETURNVALUE_CANNOTTHROW; + //} + + return ReturnValueType.RETURNVALUE_NOERROR; + } + + public Action GetAction(IItem item) + { + //if (item.hasAttribute(ItemAttribute_t.UNIQUEID)) + //{ + // var it = _uniqueItemMap.find(item.getAttribute(ItemAttribute_t.UNIQUEID)); + // if (it != _uniqueItemMap.end()) + // { + // return it->second; + // } + //} + + //if (item.hasAttribute(ItemAttribute_t.ACTIONID)) + //{ + // var it = _actionItemMap.find(item.getAttribute(ItemAttribute_t.ACTIONID)); + // if (it != _actionItemMap.end()) + // { + // return it->second; + // } + //} + + if (_useItemMap.TryGetValue(item.ServerId, out var action)) + return action; + + return null; + } + + public ReturnValueType InternalUseItem(IPlayer player, Location pos, byte index, IItem item, bool isHotkey) + { + //if (std.shared_ptr < Door > door = item.GetDoor()) + //{ + // if (!door.CanUse(player)) + // { + // return ReturnValue.CANNOTUSETHISOBJECT; + // } + //} + + //var itemId = item.ID; + //var itemType = Item.items[itemId]; + //var transformTo = itemType.m_transformOnUse; + //var action = GetAction(item); + //if (action == null && transformTo > 0 && itemId != transformTo) + //{ + // if (g_game().TransformItem(item, transformTo) == null) + // { + // Logger.GetInstance().Warn($"{nameof(InternalUseItem)} - item with id {itemId} failed to transform to item {transformTo}"); + // return ReturnValue.CANNOTUSETHISOBJECT; + // } + + // return ReturnValue.NOERROR; + //} + //else if (transformTo > 0 && action != null) + //{ + // Logger.GetInstance().Warn($"{nameof(InternalUseItem)} - item with id {itemId} already has an action registered and cannot use the transformTo tag"); + //} + + //if (action != null) + //{ + // if (action.IsLoadedCallback()) + // { + // if (action.ExecuteUse(player, item, pos, null, pos, isHotkey)) + // { + // return ReturnValue.NOERROR; + // } + + // if (item.IsRemoved()) + // { + // return ReturnValue.CANNOTUSETHISOBJECT; + // } + // } + // else if (action.UseFunction != null && action.UseFunction(player, item, pos, null, pos, isHotkey)) + // { + // return ReturnValue.NOERROR; + // } + //} + + //if (std.shared_ptr < BedItem > bed = item.GetBed()) + //{ + // if (!bed.CanUse(player)) + // { + // return ReturnValue.CANNOTUSETHISOBJECT; + // } + + // if (bed.TrySleep(player)) + // { + // player.SetBedItem(bed); + // g_game().SendOfflineTrainingDialog(player); + // } + // return ReturnValue.NOERROR; + //} + + //if (std.shared_ptr < Container > container = item.GetContainer()) + //{ + // std.shared_ptr openContainer; + + // // depot container + // if (std.shared_ptr < DepotLocker > depot = container.GetDepotLocker()) + // { + // std.shared_ptr myDepotLocker = player.GetDepotLocker(depot.GetDepotId()); + // myDepotLocker.SetParent(depot.GetParent().GetTile()); + // openContainer = myDepotLocker; + // player.SetLastDepotId(depot.GetDepotId()); + // } + // else + // { + // openContainer = container; + // } + + // // reward chest + // if (container.GetRewardChest() != null && container.GetParent() != null) + // { + // if (!player.HasOtherRewardContainerOpen(container.GetParent().GetContainer())) + // { + // player.RemoveEmptyRewards(); + // } + + // std.shared_ptr playerRewardChest = player.GetRewardChest(); + // if (playerRewardChest.Empty()) + // { + // return ReturnValue.REWARDCHESTISEMPTY; + // } + + // playerRewardChest.SetParent(container.GetParent().GetTile()); + // foreach (var (mapRewardId, reward) in player.rewardMap) + // { + // reward.SetParent(playerRewardChest); + // } + // openContainer = playerRewardChest; + // } + + // var rewardId = container.GetAttribute(ItemAttribute_t.DATE); + // // Reward container proxy created when the boss dies + // if (container.GetID() == ITEM_REWARD_CONTAINER && !container.GetReward()) + // { + // var reward = player.GetReward(rewardId, false); + // if (reward == null) + // { + // return ReturnValue.THISISIMPOSSIBLE; + // } + + // if (reward.Empty()) + // { + // return ReturnValue.REWARDCONTAINERISEMPTY; + // } + + // reward.SetParent(container.GetRealParent()); + // openContainer = reward; + // } + + // uint32_t corpseOwner = container.GetCorpseOwner(); + // if (container.IsRewardCorpse()) + // { + // // only players who participated in the fight can open the corpse + // if (player.GetGroup().id >= account.GROUP_TYPE_GAMEMASTER) + // { + // return ReturnValue.YOUCANTOPENCORPSEADM; + // } + + // var reward = player.GetReward(rewardId, false); + // if (reward == null) + // { + // return ReturnValue.YOUARENOTTHEOWNER; + // } + + // if (reward.Empty()) + // { + // return ReturnValue.REWARDCONTAINERISEMPTY; + // } + // } + // else if (corpseOwner != 0 && !player.CanOpenCorpse(corpseOwner)) + // { + // return ReturnValue.YOUARENOTTHEOWNER; + // } + + // // open/close container + // int32_t oldContainerId = player.GetContainerID(openContainer); + // if (oldContainerId != -1) + // { + // player.OnCloseContainer(openContainer); + // player.CloseContainer(oldContainerId); + // } + // else + // { + // player.AddContainer(index, openContainer); + // player.OnSendContainer(openContainer); + // } + + // return ReturnValue.NOERROR; + //} + + //const ItemType &it = Item.items[item.ID]; + //if (it.CanReadText) + //{ + // if (it.CanWriteText) + // { + // player.SetWriteItem(item, it.MaxTextLen); + // player.SendTextWindow(item, it.MaxTextLen, true); + // } + // else + // { + // player.SetWriteItem(null); + // player.SendTextWindow(item, 0, false); + // } + + // return ReturnValue.NOERROR; + //} + + return ReturnValueType.RETURNVALUE_CANNOTUSETHISOBJECT; + } + + public void ShowUseHotkeyMessage(IPlayer player, IItem item, uint count) + { + StringBuilder ss = new StringBuilder(); + + //const ItemType &it = Item.items[item.ID]; + //if (!it.ShowCount) + //{ + // ss.Append($"Using one of {item.GetName()}..."); + //} + //else if (count == 1) + //{ + // ss.Append($"Using the last {item.GetName()}..."); + //} + //else + //{ + // ss.Append($"Using one of {count} {item.GetPluralName()}..."); + //} + //player.SendTextMessage(MESSAGE_HOTKEY_PRESSED, ss.ToString()); + } + + public bool HasPosition(Location position) + { + return _actionPositionMap.ContainsKey(position); + } + + public Dictionary GetPositionsMap() + { + return _actionPositionMap; + } + + public void SetPosition(Location position, Action action) + { + _actionPositionMap.TryAdd(position, action); + } + + public bool HasItemId(ushort itemId) + { + return _useItemMap.ContainsKey(itemId); + } + + public void SetItemId(ushort itemId, Action action) + { + _useItemMap.TryAdd(itemId, action); + } + + public bool HasUniqueId(ushort uniqueId) + { + return _uniqueItemMap.ContainsKey(uniqueId); + } + + public void SetUniqueId(ushort uniqueId, Action action) + { + _uniqueItemMap.TryAdd(uniqueId, action); + } + + public bool HasActionId(ushort actionId) + { + return _actionItemMap.ContainsKey(actionId); + } + + public void SetActionId(ushort actionId, Action action) + { + _actionItemMap.TryAdd(actionId, action); + } + + public void Clear() + { + _useItemMap.Clear(); + _uniqueItemMap.Clear(); + _actionItemMap.Clear(); + _actionPositionMap.Clear(); + } + + public Action GetAction(ushort id) + { + _useItemMap.TryGetValue(id, out var action); + return action; + } + + #endregion +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs index 2805d40bf..0470a80fd 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs @@ -150,7 +150,7 @@ public bool Load(string file) booleanConfig[(int)BooleanConfigType.TOGGLE_SAVE_INTERVAL] = GetGlobalBoolean(L, "toggleSaveInterval", false); booleanConfig[(int)BooleanConfigType.TOGGLE_SAVE_INTERVAL_CLEAN_MAP] = GetGlobalBoolean(L, "toggleSaveIntervalCleanMap", false); //bool[TELEPORT_SUMMONS] = getGlobalBoolean(L, "teleportSummons", false); - booleanConfig[(int)BooleanConfigType.ALLOW_RELOAD] = GetGlobalBoolean(L, "allowReload", false); + booleanConfig[(int)BooleanConfigType.ALLOW_RELOAD] = GetGlobalBoolean(L, "allowReload", true); //bool[ONLY_PREMIUM_ACCOUNT] = getGlobalBoolean(L, "onlyPremiumAccount", false); //bool[RATE_USE_STAGES] = getGlobalBoolean(L, "rateUseStages", false); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua index dda54eac2..50b86d50b 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua @@ -10,4 +10,13 @@ elseif SAVE_INTERVAL_TYPE == "minute" then SAVE_INTERVAL_TIME = 60 * 1000 elseif SAVE_INTERVAL_TYPE == "hour" then SAVE_INTERVAL_TIME = 60 * 60 * 1000 +end + +table.contains = function(array, value) + for _, targetColumn in pairs(array) do + if targetColumn == value then + return true + end + end + return false end \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/item.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/item.lua new file mode 100644 index 000000000..5c0be4570 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/item.lua @@ -0,0 +1,3 @@ +function Item.getType(self) + return ItemType(self:getId()) +end \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua index 1efa72cf0..47d9a8374 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua @@ -1,2 +1,6 @@ -- Load core functions -dofile(CORE_DIRECTORY .. "/libs/functions/revscriptsys.lua") \ No newline at end of file +dofile(CORE_DIRECTORY .. "/libs/functions/item.lua") +dofile(CORE_DIRECTORY .. "/libs/functions/player.lua") +dofile(CORE_DIRECTORY .. "/libs/functions/position.lua") +dofile(CORE_DIRECTORY .. "/libs/functions/revscriptsys.lua") +dofile(CORE_DIRECTORY .. "/libs/functions/tile.lua") \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/player.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/player.lua new file mode 100644 index 000000000..527722a0e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/player.lua @@ -0,0 +1,7 @@ +function Player.sendCancelMessage(self, message) + print('type(message): ' .. type(message)) + if type(message) == "number" then + message = Game.getReturnMessage(message) + end + return self:sendTextMessage(MESSAGE_STATUS_SMALL, message) +end \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/position.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/position.lua new file mode 100644 index 000000000..7167102ee --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/position.lua @@ -0,0 +1,72 @@ +Position.directionOffset = { + [DIRECTION_NORTH] = {x = 0, y = -1}, + [DIRECTION_EAST] = {x = 1, y = 0}, + [DIRECTION_SOUTH] = {x = 0, y = 1}, + [DIRECTION_WEST] = {x = -1, y = 0}, + [DIRECTION_SOUTHWEST] = {x = -1, y = 1}, + [DIRECTION_SOUTHEAST] = {x = 1, y = 1}, + [DIRECTION_NORTHWEST] = {x = -1, y = -1}, + [DIRECTION_NORTHEAST] = {x = 1, y = -1} +} + +function Position:getNextPosition(direction, steps) + local offset = Position.directionOffset[direction] + if offset then + steps = steps or 1 + self.x = self.x + offset.x * steps + self.y = self.y + offset.y * steps + end +end + +function Position:moveUpstairs() + local swap = function (lhs, rhs) + lhs.x, rhs.x = rhs.x, lhs.x + lhs.y, rhs.y = rhs.y, lhs.y + lhs.z, rhs.z = rhs.z, lhs.z + end + + self.z = self.z - 1 + + local defaultPosition = self + Position.directionOffset[DIRECTION_SOUTH] + local toTile = Tile(defaultPosition) + if not toTile or not toTile:isWalkable() then + for direction = DIRECTION_NORTH, DIRECTION_NORTHEAST do + if direction == DIRECTION_SOUTH then + direction = DIRECTION_WEST + end + + local position = self + Position.directionOffset[direction] + toTile = Tile(position) + if toTile and toTile:isWalkable() then + swap(self, position) + return self + end + end + end + swap(self, defaultPosition) + return self +end + +function Position:isInRange(from, to) + -- No matter what corner from and to is, we want to make + -- life easier by calculating north-west and south-east + local zone = { + nW = { + x = (from.x < to.x and from.x or to.x), + y = (from.y < to.y and from.y or to.y), + z = (from.z < to.z and from.z or to.z) + }, + sE = { + x = (to.x > from.x and to.x or from.x), + y = (to.y > from.y and to.y or from.y), + z = (to.z > from.z and to.z or from.z) + } + } + + if self.x >= zone.nW.x and self.x <= zone.sE.x + and self.y >= zone.nW.y and self.y <= zone.sE.y + and self.z >= zone.nW.z and self.z <= zone.sE.z then + return true + end + return false +end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua index e1d66d8ee..00849488e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua @@ -59,36 +59,36 @@ do end -- Item index --- do --- local function ItemIndex(self, key) --- local methods = getmetatable(self) --- if key == "itemid" then --- return methods.getId(self) --- elseif key == "actionid" then --- return methods.getActionId(self) --- elseif key == "uid" then --- return methods.getUniqueId(self) --- elseif key == "type" then --- return methods.getSubType(self) --- end --- return methods[key] --- end --- rawgetmetatable("Item").__index = ItemIndex --- rawgetmetatable("Container").__index = ItemIndex --- rawgetmetatable("Teleport").__index = ItemIndex --- end +do + local function ItemIndex(self, key) + local methods = getmetatable(self) + if key == "itemid" then + return methods.getId(self) + elseif key == "actionid" then + return methods.getActionId(self) + elseif key == "uid" then + return methods.getUniqueId(self) + elseif key == "type" then + return methods.getSubType(self) + end + return methods[key] + end + rawgetmetatable("Item").__index = ItemIndex + --rawgetmetatable("Container").__index = ItemIndex + --rawgetmetatable("Teleport").__index = ItemIndex +end -- Action revscriptsys --- do --- local function ActionNewIndex(self, key, value) --- if key == "onUse" then --- self:onUse(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("Action").__newindex = ActionNewIndex --- end +do + local function ActionNewIndex(self, key, value) + if key == "onUse" then + self:onUse(value) + return + end + rawset(self, key, value) + end + rawgetmetatable("Action").__newindex = ActionNewIndex +end -- TalkAction revscriptsys do diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/tile.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/tile.lua new file mode 100644 index 000000000..7d7c87515 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/tile.lua @@ -0,0 +1,32 @@ +function Tile.isCreature(self) + return false +end + +function Tile.isItem(self) + return false +end + +function Tile.isTile(self) + return true +end + +function Tile.isContainer(self) + return false +end + +function Tile.isWalkable(self) + local ground = self:getGround() + if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then + return false + end + + local items = self:getItems() + for i = 1, self:getItemCount() do + local item = items[i] + local itemType = item:getType() + if itemType:getType() ~= ITEM_TYPE_MAGICFIELD and not itemType:isMovable() and item:hasProperty(CONST_PROP_BLOCKSOLID) then + return false + end + end + return true +end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua index ac69947d4..df1bff005 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua @@ -1,9 +1,46 @@ --- local shovel = Action() +local shovel = Action() --- function shovel.onUse(player, item, itemEx, fromPosition, target, toPosition, isHotkey) --- print('shovel.onUse from lua') --- --return onUseShovel(player, item, itemEx, fromPosition, target, toPosition, isHotkey) --- end +function shovel.onUse(player, item, fromPosition, target, toPosition, isHotkey) + local tile = Tile(toPosition) + if not tile then + return false + end --- shovel:id(3457, 5710) --- shovel:register() + local ground = tile:getGround() + if not ground then + return false + end + + local groundId = ground:getId() + if table.contains(holes, groundId) then + ground:transform(groundId + 1) + ground:decay() + + toPosition.z = toPosition.z + 1 + tile:relocateTo(toPosition) + player:addAchievementProgress("The Undertaker", 500) + elseif target.itemid == 7932 then -- large hole + target:transform(7933) + target:decay() + player:addAchievementProgress("The Undertaker", 500) + elseif table.contains(sandIds, groundId) then + local randomValue = math.random(1, 100) + if target.actionid == actionIds.sandHole and randomValue <= 20 then + ground:transform(489) + ground:decay() + elseif randomValue == 1 then + Game.createItem(2159, 1, toPosition) + player:addAchievementProgress("Gold Digger", 100) + elseif randomValue > 95 then + Game.createMonster("Scarab", toPosition) + end + toPosition:sendMagicEffect(CONST_ME_POFF) + else + return false + end + + return true +end + +shovel:id(2554, 5710) +shovel:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/items/ladder_up.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/items/ladder_up.lua new file mode 100644 index 000000000..ef446659d --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/items/ladder_up.lua @@ -0,0 +1,22 @@ +local ladder = Action() + +local upFloorIds = {1386, 3678, 5543} + +function ladder.onUse(player, item, fromPosition, target, toPosition, isHotkey) + if table.contains(upFloorIds, item.itemid) then + fromPosition:moveUpstairs() + else + fromPosition.z = fromPosition.z + 1 + end + + if player:isPzLocked() and Tile(fromPosition):hasFlag(TILESTATE_PROTECTIONZONE) then + player:sendCancelMessage(RETURNVALUE_PLAYERISPZLOCKED) + return true + end + + player:teleportTo(fromPosition, false) + return true +end + +ladder:id(430, 1369, 1386, 3678, 5543) +ladder:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/tools/rope.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/tools/rope.lua new file mode 100644 index 000000000..e7099bec3 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/tools/rope.lua @@ -0,0 +1,55 @@ +local rope = Action() + +function rope.onUse(player, item, fromPosition, target, toPosition, isHotkey) + local tile = Tile(toPosition) + if not tile then + return false + end + + local ground = tile:getGround() + + if ground and table.contains(ropeSpots, ground:getId()) then + tile = Tile(toPosition:moveUpstairs()) + if not tile then + return false + end + + if tile:hasFlag(TILESTATE_PROTECTIONZONE) and player:isPzLocked() then + player:sendCancelMessage(RETURNVALUE_PLAYERISPZLOCKED) + return true + end + + player:teleportTo(toPosition, false) + return true + end + + if table.contains(holeId, target.itemid) then + toPosition.z = toPosition.z + 1 + tile = Tile(toPosition) + if not tile then + return false + end + + local thing = tile:getTopVisibleThing() + if not thing then + return true + end + + if thing:isPlayer() then + if Tile(toPosition:moveUpstairs()):queryAdd(thing) ~= RETURNVALUE_NOERROR then + return false + end + + return thing:teleportTo(toPosition, false) + elseif thing:isItem() and thing:getType():isMovable() then + return thing:moveTo(toPosition:moveUpstairs()) + end + + return true + end + + return false +end + +rope:id(2120, 7731) +rope:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua index 838f6dcc0..58b0ccfde 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua @@ -1,86 +1,84 @@ --- local talkAction = TalkAction("/reload") - --- function talkAction.onSay(player, words, param) --- local reloadTypes = { --- ["all"] = RELOAD_TYPE_ALL, - --- ["chat"] = RELOAD_TYPE_CHAT, --- ["channel"] = RELOAD_TYPE_CHAT, --- ["chatchannels"] = RELOAD_TYPE_CHAT, - --- ["config"] = RELOAD_TYPE_CONFIG, --- ["configuration"] = RELOAD_TYPE_CONFIG, - --- ["events"] = RELOAD_TYPE_EVENTS, - --- ["items"] = RELOAD_TYPE_ITEMS, - --- ["module"] = RELOAD_TYPE_MODULES, --- ["modules"] = RELOAD_TYPE_MODULES, - --- ["monster"] = RELOAD_TYPE_MONSTERS, --- ["monsters"] = RELOAD_TYPE_MONSTERS, - --- ["mount"] = RELOAD_TYPE_MOUNTS, --- ["mounts"] = RELOAD_TYPE_MOUNTS, - --- ["npc"] = RELOAD_TYPE_NPCS, --- ["npcs"] = RELOAD_TYPE_NPCS, - --- ["raid"] = RELOAD_TYPE_RAIDS, --- ["raids"] = RELOAD_TYPE_RAIDS, - --- ["scripts"] = RELOAD_TYPE_SCRIPTS, --- ["script"] = RELOAD_TYPE_SCRIPTS, - --- ["rate"] = RELOAD_TYPE_CORE, --- ["rates"] = RELOAD_TYPE_CORE, --- ["stage"] = RELOAD_TYPE_CORE, --- ["stages"] = RELOAD_TYPE_CORE, --- ["global"] = RELOAD_TYPE_CORE, --- ["core"] = RELOAD_TYPE_CORE, --- ["lib"] = RELOAD_TYPE_CORE, --- ["libs"] = RELOAD_TYPE_CORE, - --- ["imbuements"] = RELOAD_TYPE_IMBUEMENTS, - --- ["group"] = RELOAD_TYPE_GROUPS, --- ["groups"] = RELOAD_TYPE_GROUPS, --- } - --- if not configManager.getBoolean(configKeys.ALLOW_RELOAD) then --- print("Reload command is disabled.") --- --self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload command is disabled.") --- return true --- end - --- if param == "" then --- print("Command param required.") --- --self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Command param required.") --- return true --- end - --- -- create log --- --logCommand(self, "/reload", param) - --- local reloadType = reloadTypes[param:lower()] --- if reloadType then --- -- Force save server before reload --- --saveServer() --- --SaveHirelings() --- --logger.info("Saved Hirelings") --- --self:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is saved.. Now will reload configs!") - --- Game.reload(reloadType) --- --self:sendTextMessage(MESSAGE_LOOK, string.format("Reloaded %s.", param:lower())) --- --logger.info("Reloaded {}", param:lower()) --- elseif not reloadType then --- --self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload type not found.") --- --logger.warn("[reload.onSay] - Reload type '{}' not found", param) --- end - --- return true --- end - --- talkAction:separator(" ") --- talkAction:register() +local talkAction = TalkAction("/reload") + +function talkAction.onSay(player, words, param) + local reloadTypes = { + ["all"] = RELOAD_TYPE_ALL, + + ["chat"] = RELOAD_TYPE_CHAT, + ["channel"] = RELOAD_TYPE_CHAT, + ["chatchannels"] = RELOAD_TYPE_CHAT, + ["config"] = RELOAD_TYPE_CONFIG, + ["configuration"] = RELOAD_TYPE_CONFIG, + + ["events"] = RELOAD_TYPE_EVENTS, + + ["items"] = RELOAD_TYPE_ITEMS, + ["module"] = RELOAD_TYPE_MODULES, + ["modules"] = RELOAD_TYPE_MODULES, + + ["monster"] = RELOAD_TYPE_MONSTERS, + ["monsters"] = RELOAD_TYPE_MONSTERS, + + ["mount"] = RELOAD_TYPE_MOUNTS, + ["mounts"] = RELOAD_TYPE_MOUNTS, + + ["npc"] = RELOAD_TYPE_NPCS, + ["npcs"] = RELOAD_TYPE_NPCS, + + ["raid"] = RELOAD_TYPE_RAIDS, + ["raids"] = RELOAD_TYPE_RAIDS, + + ["scripts"] = RELOAD_TYPE_SCRIPTS, + ["script"] = RELOAD_TYPE_SCRIPTS, + + ["rate"] = RELOAD_TYPE_CORE, + ["rates"] = RELOAD_TYPE_CORE, + ["stage"] = RELOAD_TYPE_CORE, + ["stages"] = RELOAD_TYPE_CORE, + ["global"] = RELOAD_TYPE_CORE, + ["core"] = RELOAD_TYPE_CORE, + ["lib"] = RELOAD_TYPE_CORE, + ["libs"] = RELOAD_TYPE_CORE, + + ["imbuements"] = RELOAD_TYPE_IMBUEMENTS, + + ["group"] = RELOAD_TYPE_GROUPS, + ["groups"] = RELOAD_TYPE_GROUPS, + } + + --if not configManager.getBoolean(configKeys.ALLOW_RELOAD) then + -- print("Reload command is disabled.") + -- player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload command is disabled.") + -- return true + --end + + if param == "" then + print("Command param required.") + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Command param required.") + return true + end + + -- create log + -- logCommand(self, "/reload", param) + + local reloadType = reloadTypes[param:lower()] + if reloadType then + -- Force save server before reload + --nsaveServer() + --bSaveHirelings() + logger.info("Saved Hirelings") + player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is saved.. Now will reload configs!") + + Game.reload(reloadType) + player:sendTextMessage(MESSAGE_LOOK, string.format("Reloaded %s.", param:lower())) + logger.info("Reloaded {}", param:lower()) + elseif not reloadType then + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload type not found.") + logger.warn("[reload.onSay] - Reload type '{}' not found", param) + end + + return true +end + +talkAction:separator(" ") +talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs new file mode 100644 index 000000000..15c48d15c --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs @@ -0,0 +1,225 @@ +namespace NeoServer.Scripts.LuaJIT.Extensions +{ + public static class ReturnMessageExtensions + { + public static string GetReturnMessage(this ReturnValueType value) + { + switch (value) + { + case ReturnValueType.RETURNVALUE_DESTINATIONOUTOFREACH: + return "Destination is out of range."; + + case ReturnValueType.RETURNVALUE_NOTMOVEABLE: + return "You cannot move this object."; + + case ReturnValueType.RETURNVALUE_DROPTWOHANDEDITEM: + return "Drop the double-handed object first."; + + case ReturnValueType.RETURNVALUE_BOTHHANDSNEEDTOBEFREE: + return "Both hands need to be free."; + + case ReturnValueType.RETURNVALUE_CANNOTBEDRESSED: + return "You cannot dress this object there."; + + case ReturnValueType.RETURNVALUE_PUTTHISOBJECTINYOURHAND: + return "Put this object in your hand."; + + case ReturnValueType.RETURNVALUE_PUTTHISOBJECTINBOTHHANDS: + return "Put this object in both hands."; + + case ReturnValueType.RETURNVALUE_CANONLYUSEONEWEAPON: + return "You may only use one weapon."; + + case ReturnValueType.RETURNVALUE_TOOFARAWAY: + return "You are too far away."; + + case ReturnValueType.RETURNVALUE_FIRSTGODOWNSTAIRS: + return "First go downstairs."; + + case ReturnValueType.RETURNVALUE_FIRSTGOUPSTAIRS: + return "First go upstairs."; + + case ReturnValueType.RETURNVALUE_NOTENOUGHCAPACITY: + return "This object is too heavy for you to carry."; + + case ReturnValueType.RETURNVALUE_CONTAINERNOTENOUGHROOM: + return "You cannot put more objects in this container."; + + case ReturnValueType.RETURNVALUE_NEEDEXCHANGE: + case ReturnValueType.RETURNVALUE_NOTENOUGHROOM: + return "There is not enough room."; + + case ReturnValueType.RETURNVALUE_CANNOTPICKUP: + return "You cannot take this object."; + + case ReturnValueType.RETURNVALUE_CANNOTTHROW: + return "You cannot throw there."; + + case ReturnValueType.RETURNVALUE_THEREISNOWAY: + return "There is no way."; + + case ReturnValueType.RETURNVALUE_THISISIMPOSSIBLE: + return "This is impossible."; + + case ReturnValueType.RETURNVALUE_PLAYERISPZLOCKED: + return "You can not enter a protection zone after attacking another player."; + + case ReturnValueType.RETURNVALUE_PLAYERISNOTINVITED: + return "You are not invited."; + + case ReturnValueType.RETURNVALUE_CREATUREDOESNOTEXIST: + return "Creature does not exist."; + + case ReturnValueType.RETURNVALUE_DEPOTISFULL: + return "You cannot put more items in this depot."; + + case ReturnValueType.RETURNVALUE_CANNOTUSETHISOBJECT: + return "You cannot use this object."; + + case ReturnValueType.RETURNVALUE_PLAYERWITHTHISNAMEISNOTONLINE: + return "A player with this name is not online."; + + case ReturnValueType.RETURNVALUE_NOTREQUIREDLEVELTOUSERUNE: + return "You do not have the required magic level to use this rune."; + + case ReturnValueType.RETURNVALUE_YOUAREALREADYTRADING: + return "You are already trading. Finish this trade first."; + + case ReturnValueType.RETURNVALUE_THISPLAYERISALREADYTRADING: + return "This player is already trading."; + + case ReturnValueType.RETURNVALUE_YOUMAYNOTLOGOUTDURINGAFIGHT: + return "You may not logout during or immediately after a fight!"; + + case ReturnValueType.RETURNVALUE_DIRECTPLAYERSHOOT: + return "You are not allowed to shoot directly on players."; + + case ReturnValueType.RETURNVALUE_NOTENOUGHLEVEL: + return "Your level is too low."; + + case ReturnValueType.RETURNVALUE_NOTENOUGHMAGICLEVEL: + return "You do not have enough magic level."; + + case ReturnValueType.RETURNVALUE_NOTENOUGHMANA: + return "You do not have enough mana."; + + case ReturnValueType.RETURNVALUE_NOTENOUGHSOUL: + return "You do not have enough soul."; + + case ReturnValueType.RETURNVALUE_YOUAREEXHAUSTED: + return "You are exhausted."; + + case ReturnValueType.RETURNVALUE_YOUCANNOTUSEOBJECTSTHATFAST: + return "You cannot use objects that fast."; + + case ReturnValueType.RETURNVALUE_CANONLYUSETHISRUNEONCREATURES: + return "You can only use it on creatures."; + + case ReturnValueType.RETURNVALUE_PLAYERISNOTREACHABLE: + return "Player is not reachable."; + + case ReturnValueType.RETURNVALUE_CREATUREISNOTREACHABLE: + return "Creature is not reachable."; + + case ReturnValueType.RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE: + return "This action is not permitted in a protection zone."; + + case ReturnValueType.RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER: + return "You may not attack this person."; + + case ReturnValueType.RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE: + return "You may not attack this creature."; + + case ReturnValueType.RETURNVALUE_YOUMAYNOTATTACKAPERSONINPROTECTIONZONE: + return "You may not attack a person in a protection zone."; + + case ReturnValueType.RETURNVALUE_YOUMAYNOTATTACKAPERSONWHILEINPROTECTIONZONE: + return "You may not attack a person while you are in a protection zone."; + + case ReturnValueType.RETURNVALUE_YOUCANONLYUSEITONCREATURES: + return "You can only use it on creatures."; + + case ReturnValueType.RETURNVALUE_TURNSECUREMODETOATTACKUNMARKEDPLAYERS: + return "Turn secure mode off if you really want to attack unmarked players."; + + case ReturnValueType.RETURNVALUE_YOUNEEDPREMIUMACCOUNT: + return "You need a premium account."; + + case ReturnValueType.RETURNVALUE_YOUNEEDTOLEARNTHISSPELL: + return "You must learn this spell first."; + + case ReturnValueType.RETURNVALUE_YOURVOCATIONCANNOTUSETHISSPELL: + return "You have the wrong vocation to cast this spell."; + + case ReturnValueType.RETURNVALUE_YOUNEEDAWEAPONTOUSETHISSPELL: + return "You need to equip a weapon to use this spell."; + + case ReturnValueType.RETURNVALUE_PLAYERISPZLOCKEDLEAVEPVPZONE: + return "You can not leave a pvp zone after attacking another player."; + + case ReturnValueType.RETURNVALUE_PLAYERISPZLOCKEDENTERPVPZONE: + return "You can not enter a pvp zone after attacking another player."; + + case ReturnValueType.RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE: + return "This action is not permitted in a non pvp zone."; + + case ReturnValueType.RETURNVALUE_YOUCANNOTLOGOUTHERE: + return "You can not logout here."; + + case ReturnValueType.RETURNVALUE_YOUNEEDAMAGICITEMTOCASTSPELL: + return "You need a magic item to cast this spell."; + + case ReturnValueType.RETURNVALUE_CANNOTCONJUREITEMHERE: + return "You cannot conjure items here."; + + case ReturnValueType.RETURNVALUE_YOUNEEDTOSPLITYOURSPEARS: + return "You need to split your spears first."; + + case ReturnValueType.RETURNVALUE_NAMEISTOOAMBIGUOUS: + return "Player name is ambiguous."; + + case ReturnValueType.RETURNVALUE_CANONLYUSEONESHIELD: + return "You may use only one shield."; + + case ReturnValueType.RETURNVALUE_NOPARTYMEMBERSINRANGE: + return "No party members in range."; + + case ReturnValueType.RETURNVALUE_YOUARENOTTHEOWNER: + return "You are not the owner."; + + case ReturnValueType.RETURNVALUE_NOSUCHRAIDEXISTS: + return "No such raid exists."; + + case ReturnValueType.RETURNVALUE_ANOTHERRAIDISALREADYEXECUTING: + return "Another raid is already executing."; + + case ReturnValueType.RETURNVALUE_TRADEPLAYERFARAWAY: + return "Trade player is too far away."; + + case ReturnValueType.RETURNVALUE_YOUDONTOWNTHISHOUSE: + return "You don't own this house."; + + case ReturnValueType.RETURNVALUE_TRADEPLAYERALREADYOWNSAHOUSE: + return "Trade player already owns a house."; + + case ReturnValueType.RETURNVALUE_TRADEPLAYERHIGHESTBIDDER: + return "Trade player is currently the highest bidder of an auctioned house."; + + case ReturnValueType.RETURNVALUE_YOUCANNOTTRADETHISHOUSE: + return "You can not trade this house."; + + case ReturnValueType.RETURNVALUE_YOUDONTHAVEREQUIREDPROFESSION: + return "You don't have the required profession."; + + case ReturnValueType.RETURNVALUE_ITEMCANNOTBEMOVEDTHERE: + return "This item cannot be moved there."; + + case ReturnValueType.RETURNVALUE_YOUCANNOTUSETHISBED: + return "This bed can't be used, but Premium Account players can rent houses and sleep in beds there to regain health and mana."; + + default: // ReturnValueType.RETURNVALUE_NOTPOSSIBLE, etc + return "Sorry, not possible."; + } + } + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs new file mode 100644 index 000000000..4e7748bfd --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs @@ -0,0 +1,108 @@ +using LuaNET; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class ActionFunctions : LuaScriptInterface, IActionFunctions +{ + private static IActions _actions; + + public ActionFunctions( + ILuaEnvironment luaEnvironment, + ILogger logger, + IActions actions) : base(nameof(ActionFunctions)) + { + _actions = actions; + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Action", "", LuaCreateAction); + RegisterMethod(L, "Action", "onUse", LuaActionOnUse); + RegisterMethod(L, "Action", "register", LuaActionRegister); + RegisterMethod(L, "Action", "id", LuaActionItemId); + } + + public static int LuaCreateAction(LuaState L) + { + // Action() + var action = new Action(GetScriptEnv().GetScriptInterface()); + PushUserdata(L, action); + SetMetatable(L, -1, "Action"); + return 1; + } + + public static int LuaActionOnUse(LuaState L) + { + // action:onUse(callback) + var action = GetUserdata(L, 1); + if (action != null) + { + if (!action.LoadCallback()) + { + PushBoolean(L, false); + return 1; + } + + action.SetLoadedCallback(true); + PushBoolean(L, true); + } + else + { + ReportError(nameof(LuaActionOnUse), GetErrorDesc(ErrorCodeType.LUA_ERROR_ACTION_NOT_FOUND)); + PushBoolean(L, false); + } + return 1; + } + + public static int LuaActionRegister(LuaState L) + { + // action:register() + var action = GetUserdata(L, 1); + if (action != null) + { + if (!action.IsLoadedCallback()) + { + PushBoolean(L, false); + return 1; + } + + PushBoolean(L, _actions.RegisterLuaEvent(action)); + PushBoolean(L, true); + } + else + { + ReportError(nameof(LuaActionRegister), GetErrorDesc(ErrorCodeType.LUA_ERROR_ACTION_NOT_FOUND)); + PushBoolean(L, false); + } + return 1; + } + + public static int LuaActionItemId(LuaState L) + { + // action:id(ids) + var action = GetUserdata(L, 1); + if (action != null) + { + int parameters = Lua.GetTop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc + if (parameters > 1) + { + for (int i = 0; i < parameters; ++i) + { + action.SetItemIdsVector(GetNumber(L, 2 + i)); + } + } + else + { + action.SetItemIdsVector(GetNumber(L, 2)); + } + PushBoolean(L, true); + } + else + { + ReportError(nameof(LuaActionItemId), GetErrorDesc(ErrorCodeType.LUA_ERROR_ACTION_NOT_FOUND)); + PushBoolean(L, false); + } + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs index 080b219f3..0bf5f76f7 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs @@ -1,9 +1,12 @@ using LuaNET; +using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class ConfigFunctions : LuaScriptInterface, IConfigFunctions { - public ConfigFunctions() : base(nameof(ConfigFunctions)) + public ConfigFunctions( + ILuaEnvironment luaEnvironment, + ILogger logger) : base(nameof(ConfigFunctions)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs index c441e753d..5d5fe513e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs @@ -1,7 +1,7 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; -using NeoServer.Game.Creatures.Models.Bases; using NeoServer.Server.Common.Contracts; +using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -9,7 +9,10 @@ public class CreatureFunctions : LuaScriptInterface, ICreatureFunctions { private static IGameCreatureManager _gameCreatureManager; - public CreatureFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(TalkActionFunctions)) + public CreatureFunctions( + ILuaEnvironment luaEnvironment, + ILogger logger, + IGameCreatureManager gameCreatureManager) : base(nameof(CreatureFunctions)) { _gameCreatureManager = gameCreatureManager; } @@ -36,17 +39,17 @@ private static int LuaCreatureCreate(LuaState L) else if (IsString(L, 2)) { var name = GetString(L, 2); - creature = (Creature)_gameCreatureManager.GetCreatures().FirstOrDefault(c => c.Name.Equals(name)); + creature = (ICreature)_gameCreatureManager.GetCreatures().FirstOrDefault(c => c.Name.Equals(name)); } else if (IsUserdata(L, 2)) { - //LuaData_t type = getUserdataType(L, 2); - //if (type != LuaData_t::Player && type != LuaData_t::Monster && type != LuaData_t::Npc) - //{ - // lua_pushnil(L); - // return 1; - //} - //creature = getUserdataShared(L, 2); + var type = GetUserdataType(L, 2); + if (type != LuaDataType.Player && type != LuaDataType.Monster && type != LuaDataType.Npc) + { + Lua.PushNil(L); + return 1; + } + creature = GetUserdata(L, 2); } else { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs new file mode 100644 index 000000000..5b2a82243 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs @@ -0,0 +1,64 @@ +using LuaNET; +using NeoServer.Game.Common.Location; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT.Functions; +public class EnumFunctions : LuaScriptInterface, IEnumFunctions +{ + public EnumFunctions( + ILuaEnvironment luaEnvironment, + ILogger logger) : base(nameof(ConfigFunctions)) + { + } + + public void Init(LuaState L) + { + //InitDirectionEnums(L); + RegisterEnumEx(L); + //RegisterEnum(L); + RegisterEnum(L); + RegisterEnum(L); + RegisterEnum(L); + RegisterEnum(L); + } + + public void InitDirectionEnums(LuaState L) + { + RegisterEnum(L, "DIRECTION_NORTH", Direction.North); + RegisterEnum(L, "DIRECTION_EAST", Direction.East); + RegisterEnum(L, "DIRECTION_SOUTH", Direction.South); + RegisterEnum(L, "DIRECTION_WEST", Direction.West); + RegisterEnum(L, "DIRECTION_SOUTHWEST", Direction.SouthWest); + RegisterEnum(L, "DIRECTION_SOUTHEAST", Direction.SouthEast); + RegisterEnum(L, "DIRECTION_NORTHWEST", Direction.NorthWest); + RegisterEnum(L, "DIRECTION_NORTHEAST", Direction.NorthEast); + RegisterEnum(L, "DIRECTION_NONE", Direction.None); + } + + public void InitReturnValueEnums(LuaState L) + { + //RegisterEnum(L, "RETURNVALUE_PLAYERISPZLOCKED", InvalidOperation.PlayerIsProtectionZoneLocked); + } + + private static void RegisterEnum(LuaState L, string name, Enum value) + { + var enumValue = (uint)Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType())); + RegisterGlobalVariable(L, name, enumValue); + } + + private static void RegisterEnum(LuaState L) where T : Enum + { + foreach (var item in Enum.GetValues(typeof(T))) + RegisterGlobalVariable(L, item.ToString(), Convert.ToUInt32(item)); + } + + private static void RegisterEnumEx(LuaState L) where T : Enum + { + string prefix = typeof(T).Name.ToUpperInvariant() + "_"; + foreach (var item in Enum.GetValues(typeof(T))) + { + string name = prefix + item.ToString().ToUpperInvariant(); + RegisterGlobalVariable(L, name, Convert.ToUInt32(item)); + } + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs new file mode 100644 index 000000000..2683cb62b --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs @@ -0,0 +1,71 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Scripts.LuaJIT.Extensions; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class GameFunctions : LuaScriptInterface, IGameFunctions +{ + private static IScripts _scripts; + private static ILogger _logger; + + public GameFunctions( + ILuaEnvironment luaEnvironment, + ILogger logger, + IScripts scripts) : base(nameof(GameFunctions)) + { + _scripts = scripts; + _logger = logger; + } + + public void Init(LuaState L) + { + RegisterTable(L, "Game"); + RegisterMethod(L, "Game", "getReturnMessage", LuaGameGetReturnMessage); + RegisterMethod(L, "Game", "reload", LuaGameReload); + } + + private static int LuaGameGetReturnMessage(LuaState L) + { + // Game.getReturnMessage(value) + var returnValue = GetNumber(L, 1); + PushString(L, returnValue.GetReturnMessage()); + return 1; + } + + private static int LuaGameReload(LuaState L) + { + // Game.reload(reloadType) + var reloadType = GetNumber(L, 1); + if (reloadType == ReloadType.RELOAD_TYPE_NONE) + { + //reportErrorFunc("Reload type is none"); + PushBoolean(L, false); + return 0; + } + + if (reloadType >= ReloadType.RELOAD_TYPE_LAST) + { + //reportErrorFunc("Reload type not exist"); + PushBoolean(L, false); + return 0; + } + try + { + _logger.Information("Reloading Scripts"); + var dir = AppContext.BaseDirectory; + _scripts.ClearAllScripts(); + _scripts.LoadScripts($"{dir}/Data/LuaJit/scripts", false, true); + _logger.Information("Reloaded Scripts"); + } + catch (Exception e) + { + PushBoolean(L, false); + return 0; + } + + PushBoolean(L, true); + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs index 58073c709..1a6ec3a22 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs @@ -1,10 +1,13 @@ using LuaNET; +using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class GlobalFunctions : LuaScriptInterface, IGlobalFunctions { - public GlobalFunctions() : base(nameof(GlobalFunctions)) + public GlobalFunctions( + ILuaEnvironment luaEnvironment, + ILogger logger) : base(nameof(GlobalFunctions)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs new file mode 100644 index 000000000..3465b6f90 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs @@ -0,0 +1,117 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Item; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class ItemFunctions : LuaScriptInterface, IItemFunctions +{ + public ItemFunctions( + ILuaEnvironment luaEnvironment, + ILogger logger) : base(nameof(ItemFunctions)) + { + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Item", "", LuaCreateItem); + RegisterMetaMethod(L, "Item", "__eq", LuaUserdataCompare); + RegisterMethod(L, "Item", "getId", LuaGetItemId); + RegisterMethod(L, "Item", "getActionId", LuaGetActionId); + RegisterMethod(L, "Item", "getUniqueId", LuaGetUniqueId); + RegisterMethod(L, "Item", "getSubType", LuaGetSubType); + RegisterMethod(L, "Item", "hasProperty", LuaItemHasProperty); + } + + //todo: implements this + public static int LuaCreateItem(LuaState L) + { + // Item(uid) + var id = GetNumber(L, 2); + + //const auto &item = GetScriptEnv().GetItemByUID(id); + //if (item) + //{ + // Lua::pushUserdata(L, item); + // Lua::setItemMetatable(L, -1, item); + //} + //else + //{ + // lua_pushnil(L); + //} + //return 1; + + + // Item(uid) + IItem item = null; + PushUserdata(L, item); + SetMetatable(L, -1, "Item"); + + return 1; + + } + + public static int LuaGetItemId(LuaState L) + { + // item:getId() + var item = GetUserdata(L, 1); + if (item != null) + Lua.PushNumber(L, item.ServerId); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaGetActionId(LuaState L) + { + // item:getActionId() + var item = GetUserdata(L, 1); + if (item != null) + Lua.PushNumber(L, item.ActionId); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaGetUniqueId(LuaState L) + { + // item:getUniqueId() + var item = GetUserdata(L, 1); + if (item != null) + Lua.PushNumber(L, item.UniqueId); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaGetSubType(LuaState L) + { + // item:getSubType() + var item = GetUserdata(L, 1); + if (item != null) + Lua.PushNumber(L, item.GetSubType()); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaItemHasProperty(LuaState L) + { + // item:hasProperty() + var item = GetUserdata(L, 1); + if (item != null) + { + var property = GetNumber(L, 2); + Lua.PushBoolean(L, item.Metadata.HasFlag(property)); + } + else + Lua.PushNil(L); + + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs new file mode 100644 index 000000000..48d336ed2 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs @@ -0,0 +1,97 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.DataStores; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Item; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class ItemTypeFunctions : LuaScriptInterface, IItemTypeFunctions +{ + private static IItemTypeStore _itemTypeStore; + public ItemTypeFunctions( + ILuaEnvironment luaEnvironment, IItemTypeStore itemTypeStore, ILogger logger) : base(nameof(ItemTypeFunctions)) + { + _itemTypeStore = itemTypeStore; + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "ItemType", "", LuaCreateItemType); + RegisterMetaMethod(L, "ItemType", "__eq", LuaUserdataCompare); + RegisterMethod(L, "ItemType", "getType", LuaGetItemTypeType); + RegisterMethod(L, "ItemType", "getId", LuaGetItemTypeId); + RegisterMethod(L, "ItemType", "getName", LuaGetItemTypeName); + RegisterMethod(L, "ItemType", "isMovable", LuaGetItemTypeIsMoveable); + } + + public static int LuaCreateItemType(LuaState L) + { + // ItemType(id or name) + ushort id = 0; + IItemType itemType = null; + if(Lua.IsNumber(L, 2)) + { + id = GetNumber(L, 2); + itemType = _itemTypeStore.Get(id); + } + else + { + var name = GetString(L, 2); + itemType = _itemTypeStore.All.FirstOrDefault(c => c.Name.ToLower().Equals(name.ToLower())); + } + + PushUserdata(L, itemType); + SetMetatable(L, -1, "ItemType"); + + return 1; + } + + public static int LuaGetItemTypeType(LuaState L) + { + // itemType:getType() + var itemType = GetUserdata(L, 1); + if (itemType != null) + Lua.PushNumber(L, itemType.TypeId); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaGetItemTypeId(LuaState L) + { + // itemType:getId() + var itemType = GetUserdata(L, 1); + if (itemType != null) + Lua.PushNumber(L, itemType.TypeId); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaGetItemTypeName(LuaState L) + { + // item:getName() + var itemType = GetUserdata(L, 1); + if (itemType != null) + Lua.PushString(L, itemType.Name); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaGetItemTypeIsMoveable(LuaState L) + { + // item:isMovable() + var itemType = GetUserdata(L, 1); + if (itemType != null) + Lua.PushBoolean(L, itemType.HasFlag(ItemFlag.MovementEvent)); + else + Lua.PushNil(L); + + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs index 10d11998a..95b21ab49 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs @@ -7,7 +7,8 @@ public class LoggerFunctions : LuaScriptInterface, ILoggerFunctions { private static ILogger _logger; - public LoggerFunctions(ILogger logger) : base(nameof(LoggerFunctions)) + public LoggerFunctions( + ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(LoggerFunctions)) { _logger = logger; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs index 8bff8b717..26b397315 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs @@ -1,22 +1,32 @@ using LuaNET; -using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Networking.Packets.Outgoing; +using NeoServer.Server.Common.Contracts; +using NeoServer.Server.Services; +using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class PlayerFunctions : LuaScriptInterface, IPlayerFunctions { - public PlayerFunctions() : base(nameof(PlayerFunctions)) + private static IGameCreatureManager _gameCreatureManager; + + public PlayerFunctions( + ILogger logger, + ILuaEnvironment luaEnvironment, + IGameCreatureManager gameCreatureManager) : base(nameof(PlayerFunctions)) { + _gameCreatureManager = gameCreatureManager; } public void Init(LuaState L) { RegisterSharedClass(L, "Player", "Creature", LuaPlayerCreate); RegisterMetaMethod(L, "Player", "__eq", LuaUserdataCompare); - //RegisterMethod(L, "Player", "teleportTo", LuaTeleportTo); + RegisterMethod(L, "Player", "teleportTo", LuaTeleportTo); RegisterMethod(L, "Player", "sendTextMessage", LuaPlayerSendTextMessage); + RegisterMethod(L, "Player", "isPzLocked", LuaPlayerIsPzLocked); } private static int LuaPlayerCreate(LuaState L) @@ -25,7 +35,8 @@ private static int LuaPlayerCreate(LuaState L) IPlayer player = null; if (IsNumber(L, 2)) { - var id = GetNumber(L, 2); + var id = GetNumber(L, 2); + _gameCreatureManager.TryGetPlayer(id, out player); //if (id >= Player::getFirstID() && id <= Player::getLastID()) //{ // player = g_game().getPlayerByID(id); @@ -37,6 +48,16 @@ private static int LuaPlayerCreate(LuaState L) } else if (IsString(L, 2)) { + var name = GetString(L, 2); + _gameCreatureManager.TryGetPlayer(name, out player); + + if (player == null) + { + Lua.PushNil(L); + Lua.PushNumber(L, (int)ReturnValueType.RETURNVALUE_PLAYERWITHTHISNAMEISNOTONLINE); + return 2; + } + //ReturnValue ret = g_game().getPlayerByNameWildcard(getString(L, 2), player); //if (ret != RETURNVALUE_NOERROR) //{ @@ -47,12 +68,12 @@ private static int LuaPlayerCreate(LuaState L) } else if (IsUserdata(L, 2)) { - //if (GetUserdataType(L, 2) != LuaData_t::Player) - //{ - // lua_pushnil(L); - // return 1; - //} - //player = GetUserdata(L, 2); + if (GetUserdataType(L, 2) != LuaDataType.Player) + { + Lua.PushNil(L); + return 1; + } + player = GetUserdata(L, 2); } else { @@ -71,33 +92,35 @@ private static int LuaPlayerCreate(LuaState L) return 1; } - //private static int LuaTeleportTo(LuaState L) - //{ - // // creature:teleportTo(position[, pushMovement = false]) - // bool pushMovement = GetBoolean(L, 3, false); + private static int LuaTeleportTo(LuaState L) + { + // creature:teleportTo(position[, pushMovement = false]) + bool pushMovement = GetBoolean(L, 3, false); - // var position = GetPosition(L, 2); + var position = GetPosition(L, 2); - // var creature = GetUserdata(L, 1); + var creature = GetUserdata(L, 1); - // if (creature == null) - // { - // ReportError(nameof(LuaTeleportTo), GetErrorDesc(ErrorCodeType.LUA_ERROR_CREATURE_NOT_FOUND)); - // PushBoolean(L, false); - // return 1; - // } + if (creature == null) + { + ReportError(nameof(LuaTeleportTo), GetErrorDesc(ErrorCodeType.LUA_ERROR_CREATURE_NOT_FOUND)); + PushBoolean(L, false); + return 1; + } - // var oldPosition = creature.Position; - // if (Game.GetInstance().InternalTeleport(creature, position, pushMovement)) - // { - // Logger.GetInstance().Error($"[{nameof(LuaTeleportTo)}] Failed to teleport creature {creature.Name}, on position {oldPosition}, error code: {0}");// GetReturnMessage(ret)); - // PushBoolean(L, false); - // return 1; - // } + //var oldPosition = creature.Location; + //if (Game.GetInstance().InternalTeleport(creature, position, pushMovement)) + //{ + // Logger.GetInstance().Error($"[{nameof(LuaTeleportTo)}] Failed to teleport creature {creature.Name}, on position {oldPosition}, error code: {0}");// GetReturnMessage(ret)); + // PushBoolean(L, false); + // return 1; + //} - // PushBoolean(L, true); - // return 1; - //} + creature.TeleportTo(position); + + PushBoolean(L, true); + return 1; + } private static int LuaPlayerSendTextMessage(LuaState L) { @@ -144,14 +167,39 @@ private static int LuaPlayerSendTextMessage(LuaState L) //} } - //player->sendTextMessage(message); + NotificationSenderService.Send(player, messageText, (TextMessageOutgoingType)messageType); + PushBoolean(L, true); - //Game.GetInstance().Logger.Information($"LuaPlayerSendTextMessage: player {player.Name}, message: {messageText}"); + return 1; + } - player.Say(messageText, SpeechType.Say); + private static int LuaPlayerIsPzLocked(LuaState L) + { + // player:isPzLocked() - PushBoolean(L, true); + var player = GetUserdata(L, 1); + if (player != null) + PushBoolean(L, player.IsProtectionZoneLocked); + else + Lua.PushNil(L); return 1; } + + //private static int LuaPlayerSendCancelMessage(LuaState L) + //{ + // // player:sendCancelMessage() + + // var player = GetUserdata(L, 1); + + // if (player == null) + // Lua.PushNil(L); + + // var messageText = GetString(L, 2); + + // //InvalidOperation.NotAPartyLeader + // OperationFailService.Send(player.Id, messageText); + + // return 1; + //} } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs new file mode 100644 index 000000000..c20826888 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs @@ -0,0 +1,91 @@ +using LuaNET; +using NeoServer.Game.Common.Location.Structs; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class PositionFunctions : LuaScriptInterface, IPositionFunctions +{ + public PositionFunctions( + ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(PositionFunctions)) + { + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Position", "", LuaCreatePosition); + RegisterMetaMethod(L, "Position", "__add", LuaPositionAdd); + RegisterMetaMethod(L, "Position", "__sub", LuaPositionSub); + RegisterMetaMethod(L, "Position", "__eq", LuaUserdataCompareStruct); + } + + public static int LuaCreatePosition(LuaState L) + { + // Position([x = 0[, y = 0[, z = 0[, stackpos = 0]]]]) + // Position([position]) + if (Lua.GetTop(L) <= 1) + { + PushPosition(L, new Location()); + return 1; + } + + int stackpos = 0; + + if (Lua.IsTable(L, 2)) + { + var position = GetPosition(L, 2, out stackpos); + PushPosition(L, position); + } + else + { + var x = GetNumber(L, 2, 0); + var y = GetNumber(L, 3, 0); + var z = GetNumber(L, 4, 0); + stackpos = GetNumber(L, 5, 0); + + var position = new Location(x, y, z); + + PushPosition(L, position); + } + + return 1; + } + + public static int LuaPositionAdd(LuaState L) + { + // positionValue = position + positionEx + var position = GetPosition(L, 1, out var stackpos); + Location positionEx; + if (stackpos == 0) + positionEx = GetPosition(L, 2, out stackpos); + else + positionEx = GetPosition(L, 2); + + position.X += positionEx.X; + position.Y += positionEx.Y; + position.Z += positionEx.Z; + + PushPosition(L, position, stackpos); + + return 1; + } + + public static int LuaPositionSub(LuaState L) + { + // positionValue = position - positionEx + var position = GetPosition(L, 1, out var stackpos); + Location positionEx; + if (stackpos == 0) + positionEx = GetPosition(L, 2, out stackpos); + else + positionEx = GetPosition(L, 2); + + position.X -= positionEx.X; + position.Y -= positionEx.Y; + position.Z -= positionEx.Z; + + PushPosition(L, position, stackpos); + + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs index 9bca51b9f..b0b58fd1d 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs @@ -1,4 +1,5 @@ using LuaNET; +using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs new file mode 100644 index 000000000..f65f7fdd0 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs @@ -0,0 +1,169 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Contracts.World.Tiles; +using NeoServer.Game.Common.Item; +using NeoServer.Game.Common.Location; +using NeoServer.Game.Common.Location.Structs; +using NeoServer.Server.Common.Contracts; +using Serilog; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class TileFunctions : LuaScriptInterface, ITileFunctions +{ + private static IGameServer _gameServer; + + public TileFunctions( + ILuaEnvironment luaEnvironment, IGameServer gameServer, ILogger logger) : base(nameof(TileFunctions)) + { + _gameServer = gameServer; + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Tile", "", LuaCreateTile); + RegisterMetaMethod(L, "Tile", "__eq", LuaUserdataCompare); + RegisterMethod(L, "Tile", "getPosition", LuaGetPosition); + RegisterMethod(L, "Tile", "getGround", LuaGetGround); + RegisterMethod(L, "Tile", "getItems", LuaTileGetItems); + RegisterMethod(L, "Tile", "getItemCount", LuaTileGetItemCount); + RegisterMethod(L, "Tile", "hasProperty", LuaTileHasProperty); + RegisterMethod(L, "Tile", "hasFlag", LuaTileHasFlag); + } + + public static int LuaCreateTile(LuaState L) + { + // Tile(x, y, z) + // Tile(position) + ITile tile = null; + + if (Lua.IsTable(L, 2)) + { + var position = GetPosition(L, 2); + tile = _gameServer.Map.GetTile(position); + } + else + { + var z = GetNumber(L, 4); + var y = GetNumber(L, 3); + var x = GetNumber(L, 2); + + var position = new Location(x, y, z); + + tile = _gameServer.Map.GetTile(position); + } + + PushUserdata(L, tile); + SetMetatable(L, -1, "Tile"); + return 1; + } + + public static int LuaGetPosition(LuaState L) + { + // tile:getPosition() + var tile = GetUserdata(L, 1); + if (tile != null) + { + PushPosition(L, tile.Location); + } + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaGetGround(LuaState L) + { + // tile:getGround() + var tile = GetUserdata(L, 1); + if (tile != null && tile.TopItemOnStack != null) + { + PushUserdata(L, tile.TopItemOnStack); + SetItemMetatable(L, -1, tile.TopItemOnStack); + } + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaTileGetItems(LuaState L) + { + // tile:getItems() + var tile = GetUserdata(L, 1); + + if (!tile.HasThings || tile is not IDynamicTile dynamicTile) + { + Lua.PushNil(L); + return 1; + } + + Lua.CreateTable(L, tile.ThingsCount, 0); + + int index = 0; + foreach (var item in dynamicTile.AllItems) + { + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + Lua.RawSetI(L, -2, ++index); + } + + return 1; + } + + public static int LuaTileGetItemCount(LuaState L) + { + // tile:getItemCount() + var tile = GetUserdata(L, 1); + if (tile != null) + { + Lua.PushNumber(L, tile.ThingsCount); + } + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaTileHasProperty(LuaState L) + { + // tile:hasProperty(property[, item]) + var tile = GetUserdata(L, 1); + + if(tile == null) + { + Lua.PushNil(L); + return 1; + } + + IItem? item = null; + if (Lua.GetTop(L) >= 3) + item = GetUserdata(L, 3); + + var property = GetNumber(L, 2); + + if (item != null) + Lua.PushBoolean(L, item.Metadata.HasFlag(property)); + else if (tile is IDynamicTile dynamicTile) + Lua.PushBoolean(L, dynamicTile.AllItems.Any(c => c.Metadata.HasFlag(property))); + else if (tile is IStaticTile staticTile) + Lua.PushBoolean(L, staticTile.TopItemOnStack.Metadata.HasFlag(property)); + + return 1; + } + + public static int LuaTileHasFlag(LuaState L) + { + // tile:hasFlag(flag) + var tile = GetUserdata(L, 1); + if (tile != null) + { + var flag = GetNumber(L, 2); + Lua.PushBoolean(L, tile.HasFlag(flag)); + } + else + Lua.PushNil(L); + + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs new file mode 100644 index 000000000..9c7d77e26 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IActionFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs new file mode 100644 index 000000000..61e0cff8d --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IEnumFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs new file mode 100644 index 000000000..111427ba7 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IGameFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs new file mode 100644 index 000000000..13500e4b6 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IItemFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs new file mode 100644 index 000000000..b8654392b --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IItemTypeFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs new file mode 100644 index 000000000..c1c764861 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface IPositionFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs new file mode 100644 index 000000000..4b8b1d20e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT; + +public interface ITileFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs new file mode 100644 index 000000000..26d0ee3ab --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs @@ -0,0 +1,56 @@ +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Location.Structs; + +namespace NeoServer.Scripts.LuaJIT; + +public interface IActions +{ + public bool RegisterLuaItemEvent(Action action); + + public bool RegisterLuaUniqueEvent(Action action); + + public bool RegisterLuaActionEvent(Action action); + + public bool RegisterLuaPositionEvent(Action action); + + public bool RegisterLuaEvent(Action action); + + public bool UseItem(IPlayer player, Location pos, byte index, IItem item, bool isHotkey); + + public bool UseItemEx(IPlayer player, Location fromPos, Location toPos, byte toStackPos, IItem item, bool isHotkey, ICreature creature = null); + + public ReturnValueType CanUse(IPlayer player, Location pos); + + public ReturnValueType CanUse(IPlayer player, Location pos, IItem item); + + public ReturnValueType CanUseFar(ICreature creature, Location toPos, bool checkLineOfSight, bool checkFloor); + + public Action GetAction(IItem item); + + public ReturnValueType InternalUseItem(IPlayer player, Location pos, byte index, IItem item, bool isHotkey); + + public void ShowUseHotkeyMessage(IPlayer player, IItem item, uint count); + + public bool HasPosition(Location position); + + public Dictionary GetPositionsMap(); + + public void SetPosition(Location position, Action action); + + public bool HasItemId(ushort itemId); + + public void SetItemId(ushort itemId, Action action); + + public bool HasUniqueId(ushort uniqueId); + + public void SetUniqueId(ushort uniqueId, Action action); + + public bool HasActionId(ushort actionId); + + public void SetActionId(ushort actionId, Action action); + + public void Clear(); + + public Action GetAction(ushort id); +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs deleted file mode 100644 index f6c5e576d..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaManager.cs +++ /dev/null @@ -1,6 +0,0 @@ -//namespace NeoServer.Scripts.LuaJIT; - -//public interface ILuaManager -//{ -// void Start(); -//} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs index c4d16c0a9..5e8966e51 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs @@ -12,14 +12,23 @@ public static IServiceCollection AddLuaJIT(this IServiceCollection builder) builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); + + builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); - // + builder.AddSingleton(); + return builder; } } \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ItemsDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ItemsDefinitions.cs new file mode 100644 index 000000000..26fb62727 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ItemsDefinitions.cs @@ -0,0 +1,594 @@ +namespace NeoServer.Scripts.LuaJIT; + +// Enums +public enum ItemPropertyType +{ + CONST_PROP_BLOCKSOLID = 0, + CONST_PROP_HASHEIGHT, + CONST_PROP_BLOCKPROJECTILE, + CONST_PROP_BLOCKPATH, + CONST_PROP_ISVERTICAL, + CONST_PROP_ISHORIZONTAL, + CONST_PROP_MOVEABLE, + CONST_PROP_IMMOVABLEBLOCKSOLID, + CONST_PROP_IMMOVABLEBLOCKPATH, + CONST_PROP_IMMOVABLENOFIELDBLOCKPATH, + CONST_PROP_NOFIELDBLOCKPATH, + CONST_PROP_SUPPORTHANGABLE, +}; + +public enum Attr_ReadValueType +{ + ATTR_READ_CONTINUE, + ATTR_READ_ERROR, + ATTR_READ_END, +}; + +public enum ReturnValueType : byte +{ + RETURNVALUE_NOERROR, + RETURNVALUE_NOTPOSSIBLE, + RETURNVALUE_NOTENOUGHROOM, + RETURNVALUE_PLAYERISPZLOCKED, + RETURNVALUE_PLAYERISNOTINVITED, + RETURNVALUE_CANNOTTHROW, + RETURNVALUE_THEREISNOWAY, + RETURNVALUE_DESTINATIONOUTOFREACH, + RETURNVALUE_CREATUREBLOCK, + RETURNVALUE_NOTMOVEABLE, + RETURNVALUE_DROPTWOHANDEDITEM, + RETURNVALUE_BOTHHANDSNEEDTOBEFREE, + RETURNVALUE_CANONLYUSEONEWEAPON, + RETURNVALUE_NEEDEXCHANGE, + RETURNVALUE_CANNOTBEDRESSED, + RETURNVALUE_PUTTHISOBJECTINYOURHAND, + RETURNVALUE_PUTTHISOBJECTINBOTHHANDS, + RETURNVALUE_TOOFARAWAY, + RETURNVALUE_FIRSTGODOWNSTAIRS, + RETURNVALUE_FIRSTGOUPSTAIRS, + RETURNVALUE_CONTAINERNOTENOUGHROOM, + RETURNVALUE_NOTENOUGHCAPACITY, + RETURNVALUE_CANNOTPICKUP, + RETURNVALUE_THISISIMPOSSIBLE, + RETURNVALUE_DEPOTISFULL, + RETURNVALUE_CREATUREDOESNOTEXIST, + RETURNVALUE_CANNOTUSETHISOBJECT, + RETURNVALUE_PLAYERWITHTHISNAMEISNOTONLINE, + RETURNVALUE_NOTREQUIREDLEVELTOUSERUNE, + RETURNVALUE_YOUAREALREADYTRADING, + RETURNVALUE_THISPLAYERISALREADYTRADING, + RETURNVALUE_YOUMAYNOTLOGOUTDURINGAFIGHT, + RETURNVALUE_DIRECTPLAYERSHOOT, + RETURNVALUE_NOTENOUGHLEVEL, + RETURNVALUE_NOTENOUGHMAGICLEVEL, + RETURNVALUE_NOTENOUGHMANA, + RETURNVALUE_NOTENOUGHSOUL, + RETURNVALUE_YOUAREEXHAUSTED, + RETURNVALUE_YOUCANNOTUSEOBJECTSTHATFAST, + RETURNVALUE_PLAYERISNOTREACHABLE, + RETURNVALUE_CANONLYUSETHISRUNEONCREATURES, + RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE, + RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER, + RETURNVALUE_YOUMAYNOTATTACKAPERSONINPROTECTIONZONE, + RETURNVALUE_YOUMAYNOTATTACKAPERSONWHILEINPROTECTIONZONE, + RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE, + RETURNVALUE_YOUCANONLYUSEITONCREATURES, + RETURNVALUE_CREATUREISNOTREACHABLE, + RETURNVALUE_TURNSECUREMODETOATTACKUNMARKEDPLAYERS, + RETURNVALUE_YOUNEEDPREMIUMACCOUNT, + RETURNVALUE_YOUNEEDTOLEARNTHISSPELL, + RETURNVALUE_YOURVOCATIONCANNOTUSETHISSPELL, + RETURNVALUE_YOUNEEDAWEAPONTOUSETHISSPELL, + RETURNVALUE_PLAYERISPZLOCKEDLEAVEPVPZONE, + RETURNVALUE_PLAYERISPZLOCKEDENTERPVPZONE, + RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE, + RETURNVALUE_YOUCANNOTLOGOUTHERE, + RETURNVALUE_YOUNEEDAMAGICITEMTOCASTSPELL, + RETURNVALUE_CANNOTCONJUREITEMHERE, + RETURNVALUE_YOUNEEDTOSPLITYOURSPEARS, + RETURNVALUE_NAMEISTOOAMBIGUOUS, + RETURNVALUE_CANONLYUSEONESHIELD, + RETURNVALUE_NOPARTYMEMBERSINRANGE, + RETURNVALUE_YOUARENOTTHEOWNER, + RETURNVALUE_NOSUCHRAIDEXISTS, + RETURNVALUE_ANOTHERRAIDISALREADYEXECUTING, + RETURNVALUE_TRADEPLAYERFARAWAY, + RETURNVALUE_YOUDONTOWNTHISHOUSE, + RETURNVALUE_TRADEPLAYERALREADYOWNSAHOUSE, + RETURNVALUE_TRADEPLAYERHIGHESTBIDDER, + RETURNVALUE_YOUCANNOTTRADETHISHOUSE, + RETURNVALUE_YOUDONTHAVEREQUIREDPROFESSION, + RETURNVALUE_ITEMCANNOTBEMOVEDTHERE, + RETURNVALUE_YOUCANNOTUSETHISBED, +}; + +public enum ItemGroupType +{ + ITEM_GROUP_NONE, + + ITEM_GROUP_GROUND, + ITEM_GROUP_CONTAINER, + ITEM_GROUP_SPLASH, + ITEM_GROUP_FLUID, + + ITEM_GROUP_LAST +}; + +public enum ItemType +{ + ITEMTypeYPE_NONE, + + // Odered to make the cast from protobuf::itemCategory to ItemTypesType easier. + // Do not edit it from Start-End + // Start + ITEMTypeYPE_ARMOR, + ITEMTypeYPE_AMULET, + ITEMTypeYPE_BOOTS, + ITEMTypeYPE_CONTAINER, + ITEMTypeYPE_DECORATION, + ITEMTypeYPE_FOOD, + ITEMTypeYPE_HELMET, + ITEMTypeYPE_LEGS, + ITEMTypeYPE_OTHER, + ITEMTypeYPE_POTION, + ITEMTypeYPE_RING, + ITEMTypeYPE_RUNE, + ITEMTypeYPE_SHIELD, + ITEMTypeYPETypeOOLS, + ITEMTypeYPE_VALUABLE, + ITEMTypeYPE_AMMO, + ITEMTypeYPE_AXE, + ITEMTypeYPE_CLUB, + ITEMTypeYPE_DISTANCE, + ITEMTypeYPE_SWORD, + ITEMTypeYPE_WAND, + ITEMTypeYPE_PREMIUMSCROLL, + ITEMTypeYPETypeIBIACOIN, + ITEMTypeYPE_CREATUREPRODUCT, + ITEMTypeYPE_QUIVER, + // End + + ITEMTypeYPE_DEPOT, + ITEMTypeYPE_MAILBOX, + ITEMTypeYPETypeRASHHOLDER, + ITEMTypeYPE_DOOR, + ITEMTypeYPE_MAGICFIELD, + ITEMTypeYPETypeELEPORT, + ITEMTypeYPE_BED, + ITEMTypeYPE_KEY, + ITEMTypeYPE_SUPPLY, + ITEMTypeYPE_REWARDCHEST, + ITEMTypeYPE_CARPET, + ITEMTypeYPE_RETRIEVE, + ITEMTypeYPE_GOLD, + ITEMTypeYPE_UNASSIGNED, + + // Other types + ITEMTypeYPE_LADDER, + ITEMTypeYPE_DUMMY, + + ITEMTypeYPE_LAST, +}; + +public enum TradeEventsType +{ + ONTypeRADETypeRANSFER, + ONTypeRADE_CANCEL, +}; + +public enum AttrType +{ + // ATTR_NONE = 0 (last enum) + ATTR_STORE = 1, + // ATTR_EXT_FILE = 2, + ATTRTypeILE_FLAGS = 3, + ATTR_ACTION_ID = 4, + ATTR_UNIQUE_ID = 5, + ATTRTypeEXT = 6, + ATTR_DESC = 7, + ATTRTypeELE_DEST = 8, + ATTR_ITEM = 9, + ATTR_DEPOT_ID = 10, + // ATTR_EXT_SPAWN_FILE = 11, + ATTR_RUNE_CHARGES = 12, + // ATTR_EXT_HOUSE_FILE = 13, + ATTR_HOUSEDOORID = 14, + ATTR_COUNT = 15, + ATTR_DURATION = 16, + ATTR_DECAYING_STATE = 17, + ATTR_WRITTENDATE = 18, + ATTR_WRITTENBY = 19, + ATTR_SLEEPERGUID = 20, + ATTR_SLEEPSTART = 21, + ATTR_CHARGES = 22, + ATTR_CONTAINER_ITEMS = 23, + ATTR_NAME = 24, + ATTR_ARTICLE = 25, + ATTR_PLURALNAME = 26, + ATTR_WEIGHT = 27, + ATTR_ATTACK = 28, + ATTR_DEFENSE = 29, + ATTR_EXTRADEFENSE = 30, + ATTR_ARMOR = 31, + ATTR_HITCHANCE = 32, + ATTR_SHOOTRANGE = 33, + ATTR_SPECIAL = 34, + ATTR_IMBUEMENT_SLOT = 35, + ATTR_OPENCONTAINER = 36, + ATTR_CUSTOM_ATTRIBUTES = 37, // Deprecated (override by ATTR_CUSTOM) + ATTR_QUICKLOOTCONTAINER = 38, + ATTR_AMOUNT = 39, + ATTRTypeIER = 40, + ATTR_CUSTOM = 41, + ATTR_STORE_INBOX_CATEGORY = 42, + + // Always the last + ATTR_NONE = 0 +}; + +public enum ImbuementType : long +{ + IMBUEMENT_NONE = -1, + IMBUEMENT_ELEMENTAL_DAMAGE = 0, + IMBUEMENT_LIFE_LEECH = 1, + IMBUEMENT_MANA_LEECH = 2, + IMBUEMENT_CRITICAL_HIT = 3, + IMBUEMENT_ELEMENTAL_PROTECTION_DEATH = 4, + IMBUEMENT_ELEMENTAL_PROTECTION_EARTH = 5, + IMBUEMENT_ELEMENTAL_PROTECTION_FIRE = 6, + IMBUEMENT_ELEMENTAL_PROTECTION_ICE = 7, + IMBUEMENT_ELEMENTAL_PROTECTION_ENERGY = 8, + IMBUEMENT_ELEMENTAL_PROTECTION_HOLY = 9, + IMBUEMENT_INCREASE_SPEED = 10, + IMBUEMENT_SKILLBOOST_AXE = 11, + IMBUEMENT_SKILLBOOST_SWORD = 12, + IMBUEMENT_SKILLBOOST_CLUB = 13, + IMBUEMENT_SKILLBOOST_SHIELDING = 14, + IMBUEMENT_SKILLBOOST_DISTANCE = 15, + IMBUEMENT_SKILLBOOST_MAGIC_LEVEL = 16, + IMBUEMENT_INCREASE_CAPACITY = 17 +}; + +public enum ContainerCategoryType : byte +{ + All, + Ammunition, + AmuletsAndNecklaces, + Animals, + Annelids, + Arachnids, + Armors, + ArtificialTiles, + AstralShapers, + AttackRunes, + AxeWeapons, + Bats, + Bears, + Birds, + BlessingCharms, + Blobs, + Books, + Boots, + Bushes, + Cactuses, + Canines, + Casks, + Closets, + ClothingAccessories, + ClubWeapons, + Coffins, + Constructions, + Containers, + ContestPrizes, + CreatureProducts, + Decoration, + Demons, + DistanceWeapons, + DocumentsAndPapers, + DollsAndBears, + Doors, + Dragons, + Dreamhaunters, + Dropdowns, + EnchantedItems, + EventCreatures, + ExerciseWeapons, + FansiteItems, + Ferns, + Fields, + Flags, + FloorDecorations, + FloraAndMinerals, + Flowers, + FluidContainers, + Food, + Furniture, + GameTokens, + Ghosts, + Glires, + Grass, + HealingRunes, + Helmets, + HiveBorn, + Illumination, + Keys, + KitchenTools, + Ladders, + Legs, + LightSources, + Liquids, + MachinesObjects, + Machines, + MagicalItems, + Metals, + Mollusks, + Mushrooms, + MusicalInstruments, + NaturalProducts, + NaturalTiles, + OtherItems, + Outlaws, + PaintingEquipment, + PartyItems, + Pillars, + PlantsAndHerbs, + Plants, + Portals, + QuestItems, + QuestObjects, + Quivers, + Refuse, + Remains, + Rings, + Rocks, + Rods, + Rubbish, + Shields, + ShrinesAndAltars, + Signs, + Skeletons, + Spellbooks, + Stairs, + Statues, + SupportRunes, + SwordWeapons, + Tables, + TamingItems, + Teleporters, + ToolsObjects, + Tools, + TortureInstruments, + TournamentRewards, + TrainingWeapons, + Transportation, + Traps, + Trees, + Trophies, + UndeadHumanoids, + Ungulates, + Utilities, + Valuables, + WallHangings, + Walls, + Wands, + Windows +}; + +public enum SlotPositionBits : uint +{ + SLOTP_WHEREEVER = 0xFFFFFFFF, + SLOTP_HEAD = 1 << 0, + SLOTP_NECKLACE = 1 << 1, + SLOTP_BACKPACK = 1 << 2, + SLOTP_ARMOR = 1 << 3, + SLOTP_RIGHT = 1 << 4, + SLOTP_LEFT = 1 << 5, + SLOTP_LEGS = 1 << 6, + SLOTP_FEET = 1 << 7, + SLOTP_RING = 1 << 8, + SLOTP_AMMO = 1 << 9, + SLOTP_DEPOT = 1 << 10, + SLOTPTypeWO_HAND = 1 << 11, + SLOTP_HAND = (SLOTP_LEFT | SLOTP_RIGHT) +}; + +public enum TileFlagsType : uint +{ + TILESTATE_NONE = 0, + + TILESTATE_FLOORCHANGE_DOWN = 1 << 0, + TILESTATE_FLOORCHANGE_NORTH = 1 << 1, + TILESTATE_FLOORCHANGE_SOUTH = 1 << 2, + TILESTATE_FLOORCHANGE_EAST = 1 << 3, + TILESTATE_FLOORCHANGE_WEST = 1 << 4, + TILESTATE_FLOORCHANGE_SOUTH_ALT = 1 << 5, + TILESTATE_FLOORCHANGE_EAST_ALT = 1 << 6, + TILESTATE_PROTECTIONZONE = 1 << 7, + TILESTATE_NOPVPZONE = 1 << 8, + TILESTATE_NOLOGOUT = 1 << 9, + TILESTATE_PVPZONE = 1 << 10, + TILESTATE_TELEPORT = 1 << 11, + TILESTATE_MAGICFIELD = 1 << 12, + TILESTATE_MAILBOX = 1 << 13, + TILESTATE_TRASHHOLDER = 1 << 14, + TILESTATE_BED = 1 << 15, + TILESTATE_DEPOT = 1 << 16, + TILESTATE_BLOCKSOLID = 1 << 17, + TILESTATE_BLOCKPATH = 1 << 18, + TILESTATE_IMMOVABLEBLOCKSOLID = 1 << 19, + TILESTATE_IMMOVABLEBLOCKPATH = 1 << 20, + TILESTATE_IMMOVABLENOFIELDBLOCKPATH = 1 << 21, + TILESTATE_NOFIELDBLOCKPATH = 1 << 22, + TILESTATE_SUPPORTS_HANGABLE = 1 << 23, + + TILESTATE_FLOORCHANGE = TILESTATE_FLOORCHANGE_DOWN | TILESTATE_FLOORCHANGE_NORTH | TILESTATE_FLOORCHANGE_SOUTH | TILESTATE_FLOORCHANGE_EAST | TILESTATE_FLOORCHANGE_WEST | TILESTATE_FLOORCHANGE_SOUTH_ALT | TILESTATE_FLOORCHANGE_EAST_ALT, +}; + +public enum ZoneTypeType +{ + ZONE_PROTECTION, + ZONE_NOPVP, + ZONE_PVP, + ZONE_NOLOGOUT, + ZONE_NORMAL, +}; + +public enum CylinderFlagsType +{ + FLAG_NOLIMIT = 1 << 0, // Bypass limits like capacity/container limits, blocking items/creatures etc. + FLAG_IGNOREBLOCKITEM = 1 << 1, // Bypass movable blocking item checks + FLAG_IGNOREBLOCKCREATURE = 1 << 2, // Bypass creature checks + FLAG_CHILDISOWNER = 1 << 3, // Used by containers to query capacity of the carrier (player) + FLAG_PATHFINDING = 1 << 4, // An additional check is done for floor changing/teleport items + FLAG_IGNOREFIELDDAMAGE = 1 << 5, // Bypass field damage checks + FLAG_IGNORENOTMOVEABLE = 1 << 6, // Bypass check for mobility + FLAG_IGNOREAUTOSTACK = 1 << 7, // queryDestination will not try to stack items together +}; + +public enum CylinderLinkType +{ + LINK_OWNER, + LINK_PARENT, + LINKTypeOPPARENT, + LINK_NEAR, +}; + +public enum ItemParseAttributesType +{ + ITEM_PARSETypeYPE, + ITEM_PARSE_DESCRIPTION, + ITEM_PARSE_RUNESPELLNAME, + ITEM_PARSE_WEIGHT, + ITEM_PARSE_SHOWCOUNT, + ITEM_PARSE_ARMOR, + ITEM_PARSE_DEFENSE, + ITEM_PARSE_EXTRADEF, + ITEM_PARSE_ATTACK, + ITEM_PARSE_ROTATETO, + ITEM_PARSE_WRAPCONTAINER, + ITEM_PARSE_IMBUEMENT, + ITEM_PARSE_WRAPABLETO, + ITEM_PARSE_MOVEABLE, + ITEM_PARSE_BLOCKPROJECTILE, + ITEM_PARSE_PICKUPABLE, + ITEM_PARSE_FLOORCHANGE, + ITEM_PARSE_CONTAINERSIZE, + ITEM_PARSE_FLUIDSOURCE, + ITEM_PARSE_READABLE, + ITEM_PARSE_WRITEABLE, + ITEM_PARSE_MAXTEXTLEN, + ITEM_PARSE_WRITEONCEITEMID, + ITEM_PARSE_WEAPONTYPE, + ITEM_PARSE_SLOTTYPE, + ITEM_PARSE_AMMOTYPE, + ITEM_PARSE_SHOOTTYPE, + ITEM_PARSE_EFFECT, + ITEM_PARSE_LOOTTYPE, + ITEM_PARSE_RANGE, + ITEM_PARSE_STOPDURATION, + ITEM_PARSE_DECAYTO, + ITEM_PARSETypeRANSFORMEQUIPTO, + ITEM_PARSETypeRANSFORMDEEQUIPTO, + ITEM_PARSE_DURATION, + ITEM_PARSE_SHOWDURATION, + ITEM_PARSE_CHARGES, + ITEM_PARSE_SHOWCHARGES, + ITEM_PARSE_SHOWATTRIBUTES, + ITEM_PARSE_HITCHANCE, + ITEM_PARSE_MAXHITCHANCE, + ITEM_PARSE_INVISIBLE, + ITEM_PARSE_SPEED, + ITEM_PARSE_HEALTHGAIN, + ITEM_PARSE_HEALTHTICKS, + ITEM_PARSE_MANAGAIN, + ITEM_PARSE_MANATICKS, + ITEM_PARSE_MANASHIELD, + ITEM_PARSE_SKILLSWORD, + ITEM_PARSE_SKILLAXE, + ITEM_PARSE_SKILLCLUB, + ITEM_PARSE_SKILLDIST, + ITEM_PARSE_SKILLFISH, + ITEM_PARSE_SKILLSHIELD, + ITEM_PARSE_SKILLFIST, + ITEM_PARSE_CRITICALHITCHANCE, + ITEM_PARSE_CRITICALHITDAMAGE, + ITEM_PARSE_LIFELEECHCHANCE, + ITEM_PARSE_LIFELEECHAMOUNT, + ITEM_PARSE_MANALEECHCHANCE, + ITEM_PARSE_MANALEECHAMOUNT, + ITEM_PARSE_MAXHITPOINTS, + ITEM_PARSE_MAXHITPOINTSPERCENT, + ITEM_PARSE_MAXMANAPOINTS, + ITEM_PARSE_MAXMANAPOINTSPERCENT, + ITEM_PARSE_MAGICLEVELPOINTS, + ITEM_PARSE_MAGICLEVELPOINTSPERCENT, + ITEM_PARSE_FIELDABSORBPERCENTENERGY, + ITEM_PARSE_FIELDABSORBPERCENTFIRE, + ITEM_PARSE_FIELDABSORBPERCENTPOISON, + ITEM_PARSE_ABSORBPERCENTALL, + ITEM_PARSE_ABSORBPERCENTELEMENTS, + ITEM_PARSE_ABSORBPERCENTMAGIC, + ITEM_PARSE_ABSORBPERCENTENERGY, + ITEM_PARSE_ABSORBPERCENTFIRE, + ITEM_PARSE_ABSORBPERCENTPOISON, + ITEM_PARSE_ABSORBPERCENTICE, + ITEM_PARSE_ABSORBPERCENTHOLY, + ITEM_PARSE_ABSORBPERCENTDEATH, + ITEM_PARSE_ABSORBPERCENTLIFEDRAIN, + ITEM_PARSE_ABSORBPERCENTMANADRAIN, + ITEM_PARSE_ABSORBPERCENTDROWN, + ITEM_PARSE_ABSORBPERCENTPHYSICAL, + ITEM_PARSE_ABSORBPERCENTHEALING, + ITEM_PARSE_SUPPRESSDRUNK, + ITEM_PARSE_SUPPRESSENERGY, + ITEM_PARSE_SUPPRESSFIRE, + ITEM_PARSE_SUPPRESSPOISON, + ITEM_PARSE_SUPPRESSDROWN, + ITEM_PARSE_SUPPRESSPHYSICAL, + ITEM_PARSE_SUPPRESSFREEZE, + ITEM_PARSE_SUPPRESSDAZZLE, + ITEM_PARSE_SUPPRESSCURSE, + ITEM_PARSE_FIELD, + ITEM_PARSE_REPLACEABLE, + ITEM_PARSE_PARTNERDIRECTION, + ITEM_PARSE_LEVELDOOR, + ITEM_PARSE_MALETRANSFORMTO, + ITEM_PARSE_FEMALETRANSFORMTO, + ITEM_PARSE_BEDPART, + ITEM_PARSE_BEDPARTOF, + ITEM_PARSETypeRANSFORMONUSE, + ITEM_PARSE_DESTROYTO, + ITEM_PARSE_ELEMENTICE, + ITEM_PARSE_ELEMENTEARTH, + ITEM_PARSE_ELEMENTFIRE, + ITEM_PARSE_ELEMENTENERGY, + ITEM_PARSE_ELEMENTDEATH, + ITEM_PARSE_ELEMENTHOLY, + ITEM_PARSE_WALKSTACK, + ITEM_PARSE_BLOCK_SOLID, + ITEM_PARSE_ALLOWDISTREAD, + ITEM_PARSE_STACKSIZE, + // 12.72 modifiers + ITEM_PARSE_DEATHMAGICLEVELPOINTS, + ITEM_PARSE_ENERGYMAGICLEVELPOINTS, + ITEM_PARSE_EARTHMAGICLEVELPOINTS, + ITEM_PARSE_FIREMAGICLEVELPOINTS, + ITEM_PARSE_ICEMAGICLEVELPOINTS, + ITEM_PARSE_HEALINGMAGICLEVELPOINTS, + ITEM_PARSE_HOLYMAGICLEVELPOINTS, + ITEM_PARSE_PHYSICALMAGICLEVELPOINTS, + ITEM_PARSE_MAGICSHIELDCAPACITYPERCENT, + ITEM_PARSE_MAGICSHIELDCAPACITYFLAT, + ITEM_PARSE_PERFECTSHOTDAMAGE, + ITEM_PARSE_PERFECTSHOTRANGE, + ITEM_PARSE_CLEAVEPERCENT, + ITEM_PARSE_REFLECTPERCENTALL, + ITEM_PARSE_REFLECTDAMAGE, + ITEM_PARSE_PRIMARYTYPE, +}; + +public struct ImbuementInfo +{ + //Imbuement* imbuement; + //uint duration = 0; +}; \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs index 814c5ecb0..b2bd5dc49 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs @@ -1,36 +1,36 @@ -using Serilog; - -namespace NeoServer.Scripts.LuaJIT; - -public class Logger -{ - private static Logger _instance; - public static Logger GetInstance() => _instance == null ? _instance = new Logger() : _instance; - - private ILogger _logger; - - public Logger() - { - _logger = new LoggerConfiguration().CreateLogger(); - } - - public void Debug(string message, string args = "") - { - _logger.Debug(message, args); - } - - public void Info(string message, string args = "") - { - _logger.Information(message, args); - } - - public void Warn(string message, string args = "") - { - _logger.Warning(message, args); - } - - public void Error(string message, string args = "") - { - _logger.Error(message, args); - } -} +//using Serilog; + +//namespace NeoServer.Scripts.LuaJIT; + +//public class Logger +//{ +// private static Logger _instance; +// public static Logger GetInstance() => _instance == null ? _instance = new Logger() : _instance; + +// private ILogger _logger; + +// public Logger() +// { +// _logger = new LoggerConfiguration().CreateLogger(); +// } + +// public void Debug(string message, string args = "") +// { +// _logger.Debug(message, args); +// } + +// public void Info(string message, string args = "") +// { +// _logger.Information(message, args); +// } + +// public void Warn(string message, string args = "") +// { +// _logger.Warning(message, args); +// } + +// public void Error(string message, string args = "") +// { +// _logger.Error(message, args); +// } +//} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs index 26a8afc19..85aa74abf 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs @@ -4,41 +4,41 @@ public enum LuaDataType : byte { Unknown, - Item, - Container, - Teleport, - Player, - Monster, - Npc, - MonsterType, - NpcType, - Tile, - Variant, - Position, - NetworkMessage, - ModalWindow, - Guild, - Group, - Vocation, - Town, - House, - ItemType, - Combat, - Condition, - Charm, - Loot, - MonsterSpell, - Spell, - Party, - Action, - TalkAction, - CreatureEvent, - MoveEvent, - GlobalEvent, - Weapon, - Imbuement, - Mount, - ItemClassification, + Item, + Container, + Teleport, + Player, + Monster, + Npc, + MonsterType, + NpcType, + Tile, + Variant, + Position, + NetworkMessage, + ModalWindow, + Guild, + Group, + Vocation, + Town, + House, + ItemType, + Combat, + Condition, + Charm, + Loot, + MonsterSpell, + Spell, + Party, + Action, + TalkAction, + CreatureEvent, + MoveEvent, + GlobalEvent, + Weapon, + Imbuement, + Mount, + ItemClassification, }; public enum CreatureEventType diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs index 1d290e060..dd570cc72 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs @@ -4,6 +4,7 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Structs; namespace NeoServer.Scripts.LuaJIT; @@ -174,7 +175,7 @@ public static void PushThing(LuaState L, IThing thing) return; } - if (thing is ItemLua item) + if (thing is IItem item) { PushUserdata(L, item); SetItemMetatable(L, -1, item); @@ -317,7 +318,7 @@ public static void SetWeakMetatable(LuaState L, int index, string name) Lua.SetMetaTable(L, index - 1); } - public static void SetItemMetatable(LuaState L, int index, ItemLua item) + public static void SetItemMetatable(LuaState L, int index, IItem item) { if (ValidateDispatcherContext(nameof(SetItemMetatable))) { @@ -445,13 +446,13 @@ public static string GetString(LuaState L, int arg) return c_str; } - public static PositionLua GetPosition(LuaState L, int arg, out int stackpos) + public static Location GetPosition(LuaState L, int arg, out int stackpos) { - PositionLua position = new PositionLua + Location position = new Location { - x = GetField(L, arg, "x"), - y = GetField(L, arg, "y"), - z = GetField(L, arg, "z") + X = GetField(L, arg, "x"), + Y = GetField(L, arg, "y"), + Z = GetField(L, arg, "z") }; Lua.GetField(L, arg, "stackpos"); @@ -468,13 +469,13 @@ public static PositionLua GetPosition(LuaState L, int arg, out int stackpos) return position; } - public static PositionLua GetPosition(LuaState L, int arg) + public static Location GetPosition(LuaState L, int arg) { - PositionLua position = new PositionLua + Location position = new Location { - x = GetField(L, arg, "x"), - y = GetField(L, arg, "y"), - z = GetField(L, arg, "z") + X = GetField(L, arg, "x"), + Y = GetField(L, arg, "y"), + Z = GetField(L, arg, "z") }; Lua.Pop(L, 3); @@ -698,7 +699,7 @@ public static void PushBoolean(LuaState L, bool value) // SetMetatable(L, -1, "Spell"); //} - public static void PushPosition(LuaState L, PositionLua position, int stackpos = 0) + public static void PushPosition(LuaState L, Location position, int stackpos = 0) { if (ValidateDispatcherContext(nameof(PushPosition))) { @@ -707,9 +708,9 @@ public static void PushPosition(LuaState L, PositionLua position, int stackpos = Lua.CreateTable(L, 0, 4); - SetField(L, "x", position.x); - SetField(L, "y", position.y); - SetField(L, "z", position.z); + SetField(L, "x", position.X); + SetField(L, "y", position.Y); + SetField(L, "z", position.Z); SetField(L, "stackpos", stackpos); SetMetatable(L, -1, "Position"); @@ -937,6 +938,12 @@ public static int LuaUserdataCompare(LuaState L) where T : class return 1; } + public static int LuaUserdataCompareStruct(LuaState L) where T : struct + { + PushBoolean(L, EqualityComparer.Default.Equals(GetUserdataStruct(L, 1), GetUserdataStruct(L, 2))); + return 1; + } + public static void RegisterSharedClass(LuaState L, string className, string baseClass, LuaFunction newFunction) { RegisterClass(L, className, baseClass, newFunction); @@ -963,7 +970,7 @@ public static int LuaGarbageCollection(LuaState L) } catch (Exception e) { - Logger.GetInstance().Error($"Exception in GarbageCollection: {e.InnerException}"); + //Logger.GetInstance().Error($"Exception in GarbageCollection: {e.InnerException}"); } return 0; @@ -1115,32 +1122,19 @@ public static T GetUserdata(LuaState L, int arg) where T : class var stru = (UserDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(UserDataStruct)); return (T)_objects[stru.Index]; + } - //var type = (LuaType)Lua.Type(L, arg); - - //switch (type) - //{ - // case LuaType.UserData: - // { - // int udata = ToNetObject(L, arg, 0); - // return (T)_objects[udata]; - // //return udata != -1 ? _objects[udata] : GetUserData(luaState, index); - // } - // default: - // return null; - //} - - //if (_userData.TryGetValue(typeof(T), out var @object)) - // return (T)@object; + public static T GetUserdataStruct(LuaState L, int arg) where T : struct + { + var userdata = GetRawUserdataStruct(L, arg); + if (userdata == IntPtr.Zero) + { + return default; + } - //return null; + var stru = (UserDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(UserDataStruct)); - //IntPtr userdata = GetRawUserdata(L, arg); - //if (userdata == IntPtr.Zero) - //{ - // return null; - //} - //return (T)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(T)); + return (T)_objects[stru.Index]; } //public static void RemoveUserdata() where T : class @@ -1162,6 +1156,11 @@ public static IntPtr GetRawUserdata(LuaState L, int arg) where T : class return (nint)Lua.ToUserData(L, arg); } + public static IntPtr GetRawUserdataStruct(LuaState L, int arg) where T : struct + { + return (nint)Lua.ToUserData(L, arg); + } + public static bool GetBoolean(LuaState L, int arg) { return Lua.ToBoolean(L, arg); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index f68c47977..82beb35c8 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -2,6 +2,8 @@ using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Functions; using Serilog; @@ -35,14 +37,22 @@ public class LuaGameManager : ILuaGameManager /// private readonly IScripts _scripts; + private readonly IActions _actions; private readonly ITalkActions _talkActions; + private readonly IActionFunctions _actionFunctions; private readonly IConfigFunctions _configFunctions; private readonly ICreatureFunctions _creatureFunctions; + private readonly IEnumFunctions _enumFunctions; + private readonly IGameFunctions _gameFunctions; private readonly IGlobalFunctions _globalFunctions; + private readonly IItemFunctions _itemFunctions; + private readonly IItemTypeFunctions _itemTypeFunctions; private readonly ILoggerFunctions _loggerFunctions; private readonly IPlayerFunctions _playerFunctions; + private readonly IPositionFunctions _positionFunctions; private readonly ITalkActionFunctions _talkActionFunctions; + private readonly ITileFunctions _tileFunctions; #endregion @@ -53,26 +63,43 @@ public LuaGameManager( ILuaEnvironment luaEnviroment, IConfigManager configManager, IScripts scripts, + IActions actions, ITalkActions talkActions, + IActionFunctions actionFunctions, IConfigFunctions configFunctions, ICreatureFunctions creatureFunctions, + IEnumFunctions enumFunctions, + IGameFunctions gameFunctions, IGlobalFunctions globalFunctions, + IItemFunctions itemFunctions, + IItemTypeFunctions itemTypeFunctions, ILoggerFunctions loggerFunctions, IPlayerFunctions playerFunctions, - ITalkActionFunctions talkActionFunctions) + IPositionFunctions positionFunctions, + ITalkActionFunctions talkActionFunctions, + ITileFunctions tileFunctions) { _logger = logger; _luaEnviroment = luaEnviroment; _configManager = configManager; _scripts = scripts; + + _actions = actions; _talkActions = talkActions; + _actionFunctions = actionFunctions; _configFunctions = configFunctions; _creatureFunctions = creatureFunctions; + _enumFunctions = enumFunctions; + _gameFunctions = gameFunctions; _globalFunctions = globalFunctions; + _itemFunctions = itemFunctions; + _itemTypeFunctions = itemTypeFunctions; _loggerFunctions = loggerFunctions; _playerFunctions = playerFunctions; + _positionFunctions = positionFunctions; _talkActionFunctions = talkActionFunctions; + _tileFunctions = tileFunctions; Start(); } @@ -102,6 +129,47 @@ public bool PlayerSaySpell(IPlayer player, SpeechType type, string words) return talkAction.ExecuteSay(player, talkactionWords[0], parameter, type); } + public bool PlayerUseItem(IPlayer player, Location pos, byte stackpos, byte index, IItem item) + { + var action = _actions.GetAction(item); + + if (action != null) + return action.ExecuteUse( + player, + item, + pos, + null, + pos, + false); + else + _logger.Warning($"Action with item id {item.ServerId} not found."); + + return false; + } + + public bool PlayerUseItemWithCreature(IPlayer player, Location fromPos, byte fromStackPos, ICreature creature, IItem item) + { + return PlayerUseItemEx(player, fromPos, creature.Location, creature.Tile.GetCreatureStackPositionIndex(player), item, false, creature); + } + + public bool PlayerUseItemEx(IPlayer player, Location fromPos, Location toPos, byte toStackPos, IItem item, bool isHotkey, IThing target) + { + var action = _actions.GetAction(item); + + if (action != null) + return action.ExecuteUse( + player, + item, + player.Location, + target, + toPos, + isHotkey); + else + _logger.Warning($"Action with item id {item.ServerId} not found."); + + return false; + } + #endregion #region Private Methods @@ -118,29 +186,23 @@ private void Start() var luaState = _luaEnviroment.GetLuaState(); if (luaState.IsNull) - { - //Game.DieSafely("Invalid lua state, cannot load lua functions."); - Console.WriteLine("Invalid lua state, cannot load lua functions."); - } + _logger.Error("Invalid lua state, cannot load lua functions."); Lua.OpenLibs(luaState); - //TODO: load all functions - //CoreFunctions.Init(L); - //EventFunctions.Init(L); - //ItemFunctions.Init(L); - //MapFunctions.Init(L); - //ZoneFunctions.Init(L); - //GameFunctions.Init(L); - //GlobalEventFunctions.Init(L); - //CreatureEventsFunctions.Init(L); - + _actionFunctions.Init(luaState); _configFunctions.Init(luaState); _creatureFunctions.Init(luaState); + _enumFunctions.Init(luaState); + _gameFunctions.Init(luaState); _globalFunctions.Init(luaState); + _itemFunctions.Init(luaState); + _itemTypeFunctions.Init(luaState); _loggerFunctions.Init(luaState); _playerFunctions.Init(luaState); + _positionFunctions.Init(luaState); _talkActionFunctions.Init(luaState); + _tileFunctions.Init(luaState); ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj index c8eebb344..811a8e14b 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj @@ -14,6 +14,7 @@ + @@ -26,6 +27,18 @@ Always + + Always + + + Always + + + Always + + + Always + Always @@ -35,7 +48,13 @@ Always - + + Always + + + Always + + Always diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs index 50cfbc228..e9c62e179 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs @@ -34,20 +34,20 @@ public bool LoadCallback() { if (_scriptInterface == null) { - Logger.GetInstance().Error($"[Script.LoadCallback] ScriptInterface is null, scriptId = {ScriptId}"); + //Logger.GetInstance().Error($"[Script.LoadCallback] ScriptInterface is null, scriptId = {ScriptId}"); return false; } if (ScriptId != 0) { - Logger.GetInstance().Error($"[Script.LoadCallback] ScriptId is not zero, scriptId = {ScriptId}, scriptName {_scriptInterface.GetLoadingScriptName()}"); + //Logger.GetInstance().Error($"[Script.LoadCallback] ScriptId is not zero, scriptId = {ScriptId}, scriptName {_scriptInterface.GetLoadingScriptName()}"); return false; } int id = _scriptInterface.GetEvent(); if (id == -1) { - Logger.GetInstance().Error($"[Script.LoadCallback] Event {GetScriptTypeName()} not found for script with name {_scriptInterface.GetLoadingScriptName()}"); + //Logger.GetInstance().Error($"[Script.LoadCallback] Event {GetScriptTypeName()} not found for script with name {_scriptInterface.GetLoadingScriptName()}"); return false; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs index 5d734a470..5c0286046 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs @@ -1,4 +1,5 @@ -using System; +using NeoServer.Game.Common.Contracts.Items; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -171,7 +172,7 @@ public void GetEventInfo(out int retScriptId, out LuaScriptInterface retScriptIn // return null; //} - //public std.shared_ptr GetItemByUID(uint uid) + //public IItem GetItemByUID(uint uid) //{ // std.shared_ptr thing = getThingByUID(uid); // if (thing == null) @@ -244,12 +245,12 @@ public void GetEventInfo(out int retScriptId, out LuaScriptInterface retScriptIn // return null; //} - // public void SetNpc(Npc npc) - // { - // curNpc = npc; - // } + // public void SetNpc(Npc npc) + // { + // curNpc = npc; + // } - // public Npc GetNpc() { - // return curNpc; - //} + // public Npc GetNpc() { + // return curNpc; + //} } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs index 52549c84d..e141dface 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs @@ -7,8 +7,8 @@ public class Scripts : IScripts { #region Instance - private static Scripts _instance = null; - public static Scripts GetInstance() => _instance == null ? _instance = new Scripts() : _instance; + //private static Scripts _instance = null; + //public static Scripts GetInstance() => _instance == null ? _instance = new Scripts() : _instance; #endregion @@ -24,7 +24,7 @@ public class Scripts : IScripts /// /// A reference to the logger in use. /// - private readonly ILogger _logger; + protected readonly ILogger _logger; /// /// A reference to the config manager in use. @@ -36,13 +36,13 @@ public class Scripts : IScripts /// private readonly ITalkActions _talkActions; + private readonly IActions _actions; + #endregion - public Scripts() + public Scripts(ILogger logger) { - _logger = new LoggerConfiguration() - .WriteTo.Console() - .CreateLogger(); + _logger = logger; _scriptInterface = new LuaScriptInterface("Scripts Interface"); _scriptInterface.InitState(); @@ -51,13 +51,15 @@ public Scripts() public Scripts( ILogger logger, IConfigManager configManager, - ITalkActions talkActions) + ITalkActions talkActions, + IActions actions) { - _instance = this; + //_instance = this; _logger = logger.ForContext(); _configManager = configManager; _talkActions = talkActions; + _actions = actions; _scriptInterface = new LuaScriptInterface("Scripts Interface"); //_scriptInterface.InitState(); @@ -66,7 +68,7 @@ public Scripts( public void ClearAllScripts() { _talkActions.Clear(); - //_actions.Clear(); + _actions.Clear(); //_globalEvents.Clear(); //_creatureEvents.Clear(); //gSpells.Clear(); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs index 829f55eaf..b9adfa51e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs @@ -17,118 +17,6 @@ public enum DirectionType : byte DIRECTION_NONE = 8 } - public struct PositionLua - { - public ushort x; - public ushort y; - public byte z; - - public PositionLua(ushort initX, ushort initY, byte initZ) - { - x = initX; - y = initY; - z = initZ; - } - - public static bool AreInRange(PositionLua p1, PositionLua p2, int deltax, int deltay) - { - return GetDistanceX(p1, p2) <= deltax && GetDistanceY(p1, p2) <= deltay; - } - - public static bool AreInRange(PositionLua p1, PositionLua p2, int deltax, int deltay, int deltaz) - { - return GetDistanceX(p1, p2) <= deltax && GetDistanceY(p1, p2) <= deltay && GetDistanceZ(p1, p2) <= deltaz; - } - - public static int GetOffsetX(PositionLua p1, PositionLua p2) - { - return p1.x - p2.x; - } - - public static int GetOffsetY(PositionLua p1, PositionLua p2) - { - return p1.y - p2.y; - } - - public static int GetOffsetZ(PositionLua p1, PositionLua p2) - { - return p1.z - p2.z; - } - - public static int GetDistanceX(PositionLua p1, PositionLua p2) - { - return Math.Abs(GetOffsetX(p1, p2)); - } - - public static int GetDistanceY(PositionLua p1, PositionLua p2) - { - return Math.Abs(GetOffsetY(p1, p2)); - } - - public static int GetDistanceZ(PositionLua p1, PositionLua p2) - { - return Math.Abs(GetOffsetZ(p1, p2)); - } - - public static int GetDiagonalDistance(PositionLua p1, PositionLua p2) - { - return Math.Max(GetDistanceX(p1, p2), GetDistanceY(p1, p2)); - } - - public static double GetEuclideanDistance(PositionLua p1, PositionLua p2) - { - int dx = GetDistanceX(p1, p2); - int dy = GetDistanceY(p1, p2); - return Math.Sqrt(dx * dx + dy * dy); - } - - public override string ToString() - { - return $"( {GetX()}, {GetY()}, {GetZ()} )"; - } - - public int GetX() - { - return x; - } - - public int GetY() - { - return y; - } - - public int GetZ() - { - return z; - } - } - - public static class PositionHasher - { - public static int GetHashCode(PositionLua p) - { - return (int)p.x | (p.y << 16) | (p.z << 32); - } - } - - public static class PositionExtensions - { - public static bool Equals(PositionLua p1, PositionLua p2) - { - return p1.x == p2.x && p1.y == p2.y && p1.z == p2.z; - } - - public static PositionLua Add(PositionLua p1, PositionLua p2) - { - return new PositionLua((ushort)(p1.x + p2.x), (ushort)(p1.y + p2.y), (byte)(p1.z + p2.z)); - } - - public static PositionLua Subtract(PositionLua p1, PositionLua p2) - { - return new PositionLua((ushort)(p1.x - p2.x), (ushort)(p1.y - p2.y), (byte)(p1.z - p2.z)); - } - } - public static class DirectionExtensions { private static readonly Dictionary DirectionStrings = new Dictionary @@ -153,108 +41,4 @@ public static string ToDirectionString(this DirectionType dir) return dir.ToString(); } } - - public class ThingLua() - { - #region Members - - public PositionLua Position { get; set; } - - #endregion - - #region Properties - - #endregion - - #region Public Methods - - #endregion - } - - public class CreatureLua : ThingLua - { - #region Constructors - - public CreatureLua(string name, PositionLua position) - { - SetId(); - Name = name; Position = position; - } - - #endregion - - #region Properties - - public int Id { get; set; } - public string Name { get; set; } - - #endregion - - #region Public Methods - - public virtual void SetId() { } - - public virtual CreatureLua GetPlayer() => null; - public virtual CreatureLua GetNpc() => null; - public virtual CreatureLua GetMonster() => null; - - #endregion - } - - public class PlayerLua(int guid, string name, int level, PositionLua position) : CreatureLua(name, position) - { - #region Members - - #endregion - - #region Properties - - public int PlayerFirstID => 0x10000000; - public int PlayerLastID => 0x50000000; - - public int Guid { get; set; } = guid; - - public int Level { get; set; } = level; - - #endregion - - #region Public Methods - - public override void SetId() - { - base.SetId(); - - // guid = player id from database - if (Id == 0 && Guid != 0) - { - Id = PlayerFirstID + guid; - if (Id == int.MaxValue) - { - //Logger.GetInstance().Error($"[SetId] Player {Name} has max 'id' value of uint32_t"); - } - } - } - - public override PlayerLua GetPlayer() => this; - - #endregion - } - - public class ItemLua() : ThingLua - { - #region Members - - #endregion - - #region Properties - - public ushort Id { get; set; } - public string Name { get; set; } - - #endregion - - #region Public Methods - - #endregion - } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs index 5942c9efe..6c24c7cbf 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs @@ -1,6 +1,7 @@ using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; +using Serilog; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs index 397cde577..752bbde57 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs @@ -331,45 +331,16 @@ public enum ShootTypeType : byte public enum MessageClassesType : byte { - MESSAGE_NONE = 0, /* None */ - - MESSAGE_GAMEMASTER_CONSOLE = 13, - /* Red message in the console*/ /* TALKTYPE_BROADCAST */ - - MESSAGE_LOGIN = 17, /* White message at the bottom of the game window and in the console*/ - MESSAGE_ADMINISTRADOR = 18, /* Red message in game window and in the console*/ - MESSAGE_EVENT_ADVANCE = 19, /* White message in game window and in the console*/ - MESSAGE_GAME_HIGHLIGHT = 20, /* Red message in game window and in the console*/ - MESSAGE_FAILURE = 21, /* White message at the bottom of the game window"*/ - MESSAGE_LOOK = 22, /* Green message in game window and in the console*/ - MESSAGE_DAMAGE_DEALT = 23, /* White message on the console*/ - MESSAGE_DAMAGE_RECEIVED = 24, /* White message on the console*/ - MESSAGE_HEALED = 25, /* White message on the console*/ - MESSAGE_EXPERIENCE = 26, /* White message on the console*/ - MESSAGE_DAMAGE_OTHERS = 27, /* White message on the console*/ - MESSAGE_HEALED_OTHERS = 28, /* White message on the console*/ - MESSAGE_EXPERIENCE_OTHERS = 29, /* White message on the console*/ - MESSAGE_STATUS = 30, /* White message at the bottom of the game window and in the console*/ - MESSAGE_LOOT = 31, /* White message on the game window and in the console*/ - MESSAGE_TRADE = 32, /* Green message in game window and in the console*/ - MESSAGE_GUILD = 33, /* White message in channel (+ channelId)*/ - MESSAGE_PARTY_MANAGEMENT = 34, /* Green message in game window and in the console*/ - MESSAGE_PARTY = 35, /* White message on the console*/ - - MESSAGE_LAST_OLDPROTOCOL = 37, /* Last Message on old protocol*/ - - MESSAGE_REPORT = 38, /* White message on the game window and in the console*/ - MESSAGE_HOTKEY_PRESSED = 39, /* Green message in game window and in the console*/ - MESSAGE_TUTORIAL_HINT = 40, /* no effect (?)*/ - MESSAGE_THANK_YOU = 41, /* no effect (?)*/ - MESSAGE_MARKET = 42, /* Popout a modal window with the message and a 'ok' button*/ - MESSAGE_MANA = 43, /* no effect (?)*/ - MESSAGE_BEYOND_LAST = 44, /* White message on the game window and in the console*/ - MESSAGE_ATTENTION = 48, /* White message on the console*/ - MESSAGE_BOOSTED_CREATURE = 49, /* White message on the game window and in the console*/ - MESSAGE_OFFLINE_TRAINING = 50, /* White message on the game window and in the console*/ - MESSAGE_TRANSACTION = 51, /* White message on the game window and in the console*/ - MESSAGE_POTION = 52, /* Orange creature say*/ + MESSAGE_STATUS_CONSOLE_RED = 18, /*Red message in the console*/ + MESSAGE_EVENT_ORANGE = 19, /*Orange message in the console*/ + MESSAGE_STATUS_CONSOLE_ORANGE = 20, /*Orange message in the console*/ + MESSAGE_STATUS_WARNING = 21, /*Red message in game window and in the console*/ + MESSAGE_EVENT_ADVANCE = 22, /*White message in game window and in the console*/ + MESSAGE_EVENT_DEFAULT = 23, /*White message at the bottom of the game window and in the console*/ + MESSAGE_STATUS_DEFAULT = 24, /*White message at the bottom of the game window and in the console*/ + MESSAGE_INFO_DESCR = 25, /*Green message in game window and in the console*/ + MESSAGE_STATUS_SMALL = 26, /*White message at the bottom of the game window"*/ + MESSAGE_STATUS_CONSOLE_BLUE = 27, /*FIXME Blue message in the console*/ }; public enum FluidsType : byte diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua index 46df2da48..5c228b918 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua @@ -1,4 +1,6 @@ -coreDirectory = "Data/LuaJit" +print('print from config.lua') + +coreDirectory = "Data/LuaJit" showScriptsLogInConsole = true -- NOTE: true will allow the /reload command to be used diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IPlayer.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IPlayer.cs index 8cca499c7..c3d5428a1 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IPlayer.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IPlayer.cs @@ -87,6 +87,7 @@ public interface IPlayer : ICombatActor, ISociableCreature ushort MaxMana { get; } SkillType SkillInUse { get; } bool CannotLogout { get; } + bool IsProtectionZoneLocked { get; } uint Id { get; } bool HasDepotOpened { get; } uint TotalCapacity { get; } diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs index 17389ace7..e322cd712 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs @@ -74,4 +74,21 @@ Span GetRaw() void SetParent(IThing parent); event ItemRemove OnRemoved; void OnItemRemoved(IThing from); + + ushort GetSubType() + { + if (Metadata.HasFlag(ItemFlag.LiquidContainer)) + { + var fluidType = Metadata.Attributes.GetAttribute(ItemAttribute.ContainerLiquidType); + return ushort.Parse(fluidType); + } + else if (Metadata.HasFlag(ItemFlag.Stackable)) + { + var count = Metadata.Attributes.GetAttribute(ItemAttribute.Count); + return ushort.Parse(count); + } + + var charges = Metadata.Attributes.GetAttribute(ItemAttribute.Charges); + return ushort.Parse(charges); + } } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Location/Direction.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Location/Direction.cs index 84832073b..cb89a6586 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Location/Direction.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Location/Direction.cs @@ -6,9 +6,9 @@ public enum Direction : byte East = 1, South = 2, West = 3, - NorthEast, + SouthWest, SouthEast, NorthWest, - SouthWest, + NorthEast, None } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs index 24081e576..56c180101 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs @@ -206,6 +206,8 @@ public override void LoseExperience(long exp) public virtual bool CannotLogout => !(Tile?.ProtectionZone ?? false) && InFight; + public virtual bool IsProtectionZoneLocked => false; + public SkillType SkillInUse { get diff --git a/src/Loaders/NeoServer.Loaders/TileRule/TileRuleLoader.cs b/src/Loaders/NeoServer.Loaders/TileRule/TileRuleLoader.cs index 53c42aef2..ebdfdeb49 100644 --- a/src/Loaders/NeoServer.Loaders/TileRule/TileRuleLoader.cs +++ b/src/Loaders/NeoServer.Loaders/TileRule/TileRuleLoader.cs @@ -5,10 +5,10 @@ using NeoServer.Game.Common.Contracts.World; using NeoServer.Game.Common.Contracts.World.Tiles; using NeoServer.Game.Common.Location.Structs; -using NeoServer.Game.Common.Services; using NeoServer.Loaders.Interfaces; using NeoServer.Server.Configurations; using NeoServer.Server.Helpers.Extensions; +using NeoServer.Server.Services; using Newtonsoft.Json; using Serilog; From 54cc20cd3a4b925c4feea61befdf26c8f4b376d3 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Tue, 24 Dec 2024 01:04:12 -0300 Subject: [PATCH 05/16] feat: some refacts, moved luajit data to data folder, added link from data folder to LuaJit project, removed some commented codes, implementation of some commented methods, add luajit folder on appsettings. --- .../Data => data}/LuaJit/core.lua | 7 +- .../Data => data}/LuaJit/global.lua | 1 - .../LuaJit/libs/functions/item.lua | 0 .../LuaJit/libs/functions/load.lua | 0 .../LuaJit/libs/functions/player.lua | 1 - .../LuaJit/libs/functions/position.lua | 0 data/LuaJit/libs/functions/revscriptsys.lua | 104 ++++ .../LuaJit/libs/functions/tile.lua | 0 .../Data => data}/LuaJit/libs/libs.lua | 0 .../LuaJit/scripts/actions/Tools}/rope.lua | 0 .../LuaJit/scripts/actions/Tools/shovel.lua | 0 .../scripts/actions/items/ladder_up.lua | 0 .../LuaJit/scripts/talkactions/god/reload.lua | 22 +- .../scripts/talkactions/player/test.lua | 0 .../CreatureDroppedLootEventHandler.cs | 2 +- data/extensions/Players/Loaders/GodLoader.cs | 4 +- .../extensions/Players/Loaders/TutorLoader.cs | 5 +- .../Contracts/Scripts/ILuaGameManager.cs | 1 + .../Configurations/ServerConfiguration.cs | 3 +- .../NeoServer.Scripts.LuaJIT/Action.cs | 33 +- .../NeoServer.Scripts.LuaJIT/Actions.cs | 61 +- .../LuaJit/libs/functions/revscriptsys.lua | 344 ----------- .../Functions/ConfigFunctions.cs | 7 +- .../Functions/CreatureFunctions.cs | 3 +- .../Functions/EnumFunctions.cs | 23 +- .../Functions/GameFunctions.cs | 14 +- .../Functions/ItemFunctions.cs | 30 +- .../Functions/PlayerFunctions.cs | 96 +-- .../Functions/TalkActionFunctions.cs | 110 +--- .../NeoServer.Scripts.LuaJIT/Logger.cs | 36 -- .../LuaDefinitions.cs | 6 +- .../LuaFunctionsLoader.cs | 560 ++---------------- .../LuaGameManager.cs | 15 +- .../LuaScriptInterface.cs | 11 + .../NeoServer.Scripts.LuaJIT.csproj | 49 +- .../ScriptEnvironment.cs | 308 ++++------ .../NeoServer.Scripts.LuaJIT/config.lua | 34 +- .../IoC/Modules/ConfigurationInjection.cs | 5 +- src/Standalone/Program.cs | 1 + src/Standalone/appsettings.Local.json | 3 +- src/Standalone/appsettings.json | 3 +- 41 files changed, 451 insertions(+), 1451 deletions(-) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/core.lua (61%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/global.lua (94%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/libs/functions/item.lua (100%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/libs/functions/load.lua (100%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/libs/functions/player.lua (82%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/libs/functions/position.lua (100%) create mode 100644 data/LuaJit/libs/functions/revscriptsys.lua rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/libs/functions/tile.lua (100%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/libs/libs.lua (100%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/tools => data/LuaJit/scripts/actions/Tools}/rope.lua (100%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/scripts/actions/Tools/shovel.lua (100%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/scripts/actions/items/ladder_up.lua (100%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/scripts/talkactions/god/reload.lua (70%) rename {src/Extensions/NeoServer.Scripts.LuaJIT/Data => data}/LuaJit/scripts/talkactions/player/test.lua (100%) delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/core.lua b/data/LuaJit/core.lua similarity index 61% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/core.lua rename to data/LuaJit/core.lua index a0da175f9..433df3313 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/core.lua +++ b/data/LuaJit/core.lua @@ -1,14 +1,11 @@ -logger.info('print from core.lua') -logger.info(configKeys.BASE_DIRECTORY) - -logger.debug('Starting lua.') +logger.debug('Starting lua.') logger.debug(os.getenv('LOCAL_LUA_DEBUGGER_VSCODE')) if os.getenv('LOCAL_LUA_DEBUGGER_VSCODE') == '1' then require('lldebugger').start() logger.debug('Started LUA debugger.') end -CORE_DIRECTORY = configKeys.BASE_DIRECTORY .. 'Data/LuaJit' +CORE_DIRECTORY = configKeys.BASE_DIRECTORY dofile(CORE_DIRECTORY .. '/global.lua') dofile(CORE_DIRECTORY .. '/libs/libs.lua') \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua b/data/LuaJit/global.lua similarity index 94% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua rename to data/LuaJit/global.lua index 50b86d50b..0ba7d7ed1 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/global.lua +++ b/data/LuaJit/global.lua @@ -1,5 +1,4 @@ -- for use of: data\scripts\globalevents\customs\save_interval.lua -logger.info('print from global.lua') SAVE_INTERVAL_TYPE = configManager.getString(configKeys.SAVE_INTERVAL_TYPE) SAVE_INTERVAL_CONFIG_TIME = configManager.getNumber(configKeys.SAVE_INTERVAL_TIME) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/item.lua b/data/LuaJit/libs/functions/item.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/item.lua rename to data/LuaJit/libs/functions/item.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua b/data/LuaJit/libs/functions/load.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/load.lua rename to data/LuaJit/libs/functions/load.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/player.lua b/data/LuaJit/libs/functions/player.lua similarity index 82% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/player.lua rename to data/LuaJit/libs/functions/player.lua index 527722a0e..ea2d71d75 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/player.lua +++ b/data/LuaJit/libs/functions/player.lua @@ -1,5 +1,4 @@ function Player.sendCancelMessage(self, message) - print('type(message): ' .. type(message)) if type(message) == "number" then message = Game.getReturnMessage(message) end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/position.lua b/data/LuaJit/libs/functions/position.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/position.lua rename to data/LuaJit/libs/functions/position.lua diff --git a/data/LuaJit/libs/functions/revscriptsys.lua b/data/LuaJit/libs/functions/revscriptsys.lua new file mode 100644 index 000000000..1c8494076 --- /dev/null +++ b/data/LuaJit/libs/functions/revscriptsys.lua @@ -0,0 +1,104 @@ +-- Create functions revscriptsys +function createFunctions(class) + local exclude = { [2] = { "is" }, [3] = { "get", "set", "add", "can" }, [4] = { "need" } } + local temp = {} + for name, func in pairs(class) do + local add = true + for strLen, strTable in pairs(exclude) do + if table.contains(strTable, name:sub(1, strLen)) then + add = false + end + end + if add then + local str = name:sub(1, 1):upper() .. name:sub(2) + local getFunc = function(self) + return func(self) + end + local setFunc = function(self, ...) + return func(self, ...) + end + local get = "get" .. str + local set = "set" .. str + if not (rawget(class, get) and rawget(class, set)) then + table.insert(temp, { set, setFunc, get, getFunc }) + end + end + end + for _, func in ipairs(temp) do + rawset(class, func[1], func[2]) + rawset(class, func[3], func[4]) + end +end + +-- Creature index +do + local function CreatureIndex(self, key) + local methods = getmetatable(self) + if key == "uid" then + return methods.getId(self) + elseif key == "type" then + local creatureType = 0 + if methods.isPlayer(self) then + creatureType = THING_TYPE_PLAYER + elseif methods.isMonster(self) then + creatureType = THING_TYPE_MONSTER + elseif methods.isNpc(self) then + creatureType = THING_TYPE_NPC + end + return creatureType + elseif key == "itemid" then + return 1 + elseif key == "actionid" then + return 0 + end + return methods[key] + end + rawgetmetatable("Player").__index = CreatureIndex + -- rawgetmetatable("Monster").__index = CreatureIndex + -- rawgetmetatable("Npc").__index = CreatureIndex +end + +-- Item index +do + local function ItemIndex(self, key) + local methods = getmetatable(self) + if key == "itemid" then + return methods.getId(self) + elseif key == "actionid" then + return methods.getActionId(self) + elseif key == "uid" then + return methods.getUniqueId(self) + elseif key == "type" then + return methods.getSubType(self) + end + return methods[key] + end + rawgetmetatable("Item").__index = ItemIndex + --rawgetmetatable("Container").__index = ItemIndex + --rawgetmetatable("Teleport").__index = ItemIndex +end + +-- Action revscriptsys +do + local function ActionNewIndex(self, key, value) + if key == "onUse" then + self:onUse(value) + return + end + rawset(self, key, value) + end + rawgetmetatable("Action").__newindex = ActionNewIndex +end + +-- TalkAction revscriptsys +do + local function TalkActionNewIndex(self, key, value) + if key == "onSay" then + self:onSay(value) + return + end + rawset(self, key, value) + end + local meta = rawgetmetatable("TalkAction") + meta.__newindex = TalkActionNewIndex +end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/tile.lua b/data/LuaJit/libs/functions/tile.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/tile.lua rename to data/LuaJit/libs/functions/tile.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/libs.lua b/data/LuaJit/libs/libs.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/libs.lua rename to data/LuaJit/libs/libs.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/tools/rope.lua b/data/LuaJit/scripts/actions/Tools/rope.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/tools/rope.lua rename to data/LuaJit/scripts/actions/Tools/rope.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua b/data/LuaJit/scripts/actions/Tools/shovel.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/Tools/shovel.lua rename to data/LuaJit/scripts/actions/Tools/shovel.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/items/ladder_up.lua b/data/LuaJit/scripts/actions/items/ladder_up.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/actions/items/ladder_up.lua rename to data/LuaJit/scripts/actions/items/ladder_up.lua diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua b/data/LuaJit/scripts/talkactions/god/reload.lua similarity index 70% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua rename to data/LuaJit/scripts/talkactions/god/reload.lua index 58b0ccfde..cd72ba3d7 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/god/reload.lua +++ b/data/LuaJit/scripts/talkactions/god/reload.lua @@ -14,7 +14,7 @@ function talkAction.onSay(player, words, param) ["items"] = RELOAD_TYPE_ITEMS, ["module"] = RELOAD_TYPE_MODULES, - ["modules"] = RELOAD_TYPE_MODULES, + ["modules"] = RELOAD_TYPE_MODULES, ["monster"] = RELOAD_TYPE_MONSTERS, ["monsters"] = RELOAD_TYPE_MONSTERS, @@ -48,13 +48,13 @@ function talkAction.onSay(player, words, param) --if not configManager.getBoolean(configKeys.ALLOW_RELOAD) then -- print("Reload command is disabled.") - -- player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload command is disabled.") + -- player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Reload command is disabled.") -- return true --end if param == "" then - print("Command param required.") - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Command param required.") + logger.warn("Command param required.") + player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.") return true end @@ -66,14 +66,18 @@ function talkAction.onSay(player, words, param) -- Force save server before reload --nsaveServer() --bSaveHirelings() - logger.info("Saved Hirelings") - player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is saved.. Now will reload configs!") + local reloadingMessage = string.format("Server is saved.. Now will reload %s!", param:lower()) + logger.info(reloadingMessage) + player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, reloadingMessage) + + local reloadedMessage = string.format("Reloaded %s.", param:lower()) Game.reload(reloadType) - player:sendTextMessage(MESSAGE_LOOK, string.format("Reloaded %s.", param:lower())) - logger.info("Reloaded {}", param:lower()) + player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, reloadedMessage) + logger.info(reloadedMessage) + elseif not reloadType then - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload type not found.") + player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Reload type not found.") logger.warn("[reload.onSay] - Reload type '{}' not found", param) end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/player/test.lua b/data/LuaJit/scripts/talkactions/player/test.lua similarity index 100% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/scripts/talkactions/player/test.lua rename to data/LuaJit/scripts/talkactions/player/test.lua diff --git a/data/extensions/Events/Creatures/CreatureDroppedLootEventHandler.cs b/data/extensions/Events/Creatures/CreatureDroppedLootEventHandler.cs index 707e1edac..d0d9a3428 100644 --- a/data/extensions/Events/Creatures/CreatureDroppedLootEventHandler.cs +++ b/data/extensions/Events/Creatures/CreatureDroppedLootEventHandler.cs @@ -2,8 +2,8 @@ using NeoServer.Game.Common.Contracts; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; -using NeoServer.Game.Common.Services; using NeoServer.Game.Creatures.Monster.Summon; +using NeoServer.Server.Services; namespace NeoServer.Extensions.Events.Creatures; diff --git a/data/extensions/Players/Loaders/GodLoader.cs b/data/extensions/Players/Loaders/GodLoader.cs index e30df56d6..b562b4200 100644 --- a/data/extensions/Players/Loaders/GodLoader.cs +++ b/data/extensions/Players/Loaders/GodLoader.cs @@ -62,7 +62,9 @@ public override IPlayer Load(PlayerEntity playerEntity) GuildLevel = (ushort)(playerEntity.GuildMember?.RankId ?? 0) }; - SetCurrentTile(newPlayer); + //SetCurrentTile(newPlayer); + var currentTile = GetCurrentTile(newPlayer.Location); + newPlayer.SetCurrentTile(currentTile); newPlayer.AddInventory(ConvertToInventory(newPlayer, playerEntity)); var god = CreatureFactory.CreatePlayer(newPlayer); diff --git a/data/extensions/Players/Loaders/TutorLoader.cs b/data/extensions/Players/Loaders/TutorLoader.cs index 9841eba91..1ca18b931 100644 --- a/data/extensions/Players/Loaders/TutorLoader.cs +++ b/data/extensions/Players/Loaders/TutorLoader.cs @@ -63,7 +63,10 @@ public override IPlayer Load(PlayerEntity playerEntity) }; newPlayer.AddInventory(ConvertToInventory(newPlayer, playerEntity)); - SetCurrentTile(newPlayer); + + //SetCurrentTile(newPlayer); + var currentTile = GetCurrentTile(newPlayer.Location); + newPlayer.SetCurrentTile(currentTile); var tutor = CreatureFactory.CreatePlayer(newPlayer); return tutor; diff --git a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs index 6d942eb81..4a50e1d8d 100644 --- a/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs +++ b/src/ApplicationServer/NeoServer.Server.Contracts/Contracts/Scripts/ILuaGameManager.cs @@ -7,6 +7,7 @@ namespace NeoServer.Application.Common.Contracts.Scripts; public interface ILuaGameManager { + void Start(); bool PlayerSaySpell(IPlayer player, SpeechType type, string text); bool PlayerUseItem(IPlayer player, Location pos, byte stackpos, byte index, IItem item); bool PlayerUseItemWithCreature(IPlayer player, Location fromPos, byte fromStackPos, ICreature creature, IItem item); diff --git a/src/ApplicationServer/NeoServer.Server/Configurations/ServerConfiguration.cs b/src/ApplicationServer/NeoServer.Server/Configurations/ServerConfiguration.cs index 8faf481d0..1f32b6762 100644 --- a/src/ApplicationServer/NeoServer.Server/Configurations/ServerConfiguration.cs +++ b/src/ApplicationServer/NeoServer.Server/Configurations/ServerConfiguration.cs @@ -22,7 +22,8 @@ public record ServerConfiguration( string Extensions, int ServerLoginPort, int ServerGamePort, - SaveConfiguration Save) + SaveConfiguration Save, + string DataLuaJit) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs index 8d099be95..b89ee7fa9 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs @@ -1,7 +1,8 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Contracts.World; using NeoServer.Game.Common.Location.Structs; -using NeoServer.Scripts.LuaJIT.Structs; +using NeoServer.Server.Helpers; using Serilog; namespace NeoServer.Scripts.LuaJIT; @@ -17,25 +18,21 @@ public class Action : Script private List _actionIds = new List(); private List _positions = new List(); - //private Func useFunction = null; + private ILogger _logger; public Action(LuaScriptInterface scriptInterface) : base(scriptInterface) { } - // Scripting - //public virtual bool ExecuteUse( - // Player player, Item item, Position fromPosition, Thing target, Position toPosition, bool isHotkey) - //{ - // //return useFunction?.Invoke(player, item, fromPosition, target, toPosition, isHotkey) ?? false; - //} - public bool ExecuteUse(IPlayer player, IItem item, Location fromPosition, IThing target, Location toPosition, bool isHotkey) { // onUse(player, item, fromPosition, target, toPosition, isHotkey) if (!GetScriptInterface().InternalReserveScriptEnv()) { - Console.WriteLine($"[Action::executeUse - Player {player.Name}, on item {item.Name}] " + + if (_logger == null) + _logger = IoC.GetInstance(); + + _logger.Error($"[Action::executeUse - Player {player.Name}, on item {item.Name}] " + $"Call stack overflow. Too many lua script calls being nested. Script name {GetScriptInterface().GetLoadingScriptName()}"); return false; } @@ -145,20 +142,4 @@ public virtual ReturnValueType CanExecuteAction(IPlayer player, Location toPos) return default(ReturnValueType); } - - //public override bool HasOwnErrorHandler() - //{ - // return false; - //} - - //public virtual Thing GetTarget(Player player, Creature targetCreature, Position toPosition, byte toStackPos) - //{ - // // Implement the logic for GetTarget - // return null; - //} - - private string GetScriptTypeName() - { - return "onUse"; - } } \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs index 4a368a89c..0d99d5e58 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs @@ -1,5 +1,6 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Item; using NeoServer.Game.Common.Location.Structs; using Serilog; using System.Text; @@ -10,9 +11,6 @@ public class Actions : Scripts, IActions { #region Injection - //private static Actions _instance; - //public static Actions GetInstance() => _instance == null ? _instance = new Actions() : _instance; - #endregion #region Members @@ -28,7 +26,6 @@ public class Actions : Scripts, IActions public Actions(ILogger logger) : base(logger) { - //_instance = this; } #endregion @@ -290,29 +287,29 @@ public bool UseItemEx(IPlayer player, Location fromPos, Location toPos, byte toS public ReturnValueType CanUse(IPlayer player, Location pos) { - //if (pos.X != 0xFFFF) - //{ - // var playerPos = player.Location; - // if (playerPos.Z != pos.Z) - // { - // return playerPos.Z > pos.Z ? ReturnValue.FIRSTGOUPSTAIRS : ReturnValue.FIRSTGODOWNSTAIRS; - // } + if (pos.X != 0xFFFF) + { + var playerPos = player.Location; + if (playerPos.Z != pos.Z) + { + return playerPos.Z > pos.Z ? ReturnValueType.RETURNVALUE_FIRSTGOUPSTAIRS : ReturnValueType.RETURNVALUE_FIRSTGODOWNSTAIRS; + } - // if (!Location.areInRange < 1, 1 > (playerPos, pos)) - // { - // return ReturnValue.TOOFARAWAY; - // } - //} + //if (!Location.areInRange < 1, 1 > (playerPos, pos)) + //{ + // return ReturnValueType.RETURNVALUE_TOOFARAWAY; + //} + } return ReturnValueType.RETURNVALUE_NOERROR; } public ReturnValueType CanUse(IPlayer player, Location pos, IItem item) { - //var action = GetAction(item); - //if (action != null) - //{ - // return action.canExecuteAction(player, pos); - //} + var action = GetAction(item); + if (action != null) + { + return action.CanExecuteAction(player, pos); + } return ReturnValueType.RETURNVALUE_NOERROR; } @@ -344,23 +341,13 @@ public ReturnValueType CanUseFar(ICreature creature, Location toPos, bool checkL public Action GetAction(IItem item) { - //if (item.hasAttribute(ItemAttribute_t.UNIQUEID)) - //{ - // var it = _uniqueItemMap.find(item.getAttribute(ItemAttribute_t.UNIQUEID)); - // if (it != _uniqueItemMap.end()) - // { - // return it->second; - // } - //} + if (item.Metadata.Attributes.HasAttribute(ItemAttribute.UniqueId) && + _uniqueItemMap.TryGetValue((ushort)item.UniqueId, out var uniqueIdAction)) + return uniqueIdAction; - //if (item.hasAttribute(ItemAttribute_t.ACTIONID)) - //{ - // var it = _actionItemMap.find(item.getAttribute(ItemAttribute_t.ACTIONID)); - // if (it != _actionItemMap.end()) - // { - // return it->second; - // } - //} + if (item.Metadata.Attributes.HasAttribute(ItemAttribute.ActionId) && + _actionItemMap.TryGetValue((ushort)item.UniqueId, out var actionIdAction)) + return actionIdAction; if (_useItemMap.TryGetValue(item.ServerId, out var action)) return action; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua deleted file mode 100644 index 00849488e..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Data/LuaJit/libs/functions/revscriptsys.lua +++ /dev/null @@ -1,344 +0,0 @@ --- Create functions revscriptsys -function createFunctions(class) - local exclude = { [2] = { "is" }, [3] = { "get", "set", "add", "can" }, [4] = { "need" } } - local temp = {} - for name, func in pairs(class) do - local add = true - for strLen, strTable in pairs(exclude) do - if table.contains(strTable, name:sub(1, strLen)) then - add = false - end - end - if add then - local str = name:sub(1, 1):upper() .. name:sub(2) - local getFunc = function(self) - return func(self) - end - local setFunc = function(self, ...) - return func(self, ...) - end - local get = "get" .. str - local set = "set" .. str - if not (rawget(class, get) and rawget(class, set)) then - table.insert(temp, { set, setFunc, get, getFunc }) - end - end - end - for _, func in ipairs(temp) do - rawset(class, func[1], func[2]) - rawset(class, func[3], func[4]) - end -end - --- Creature index -do - local function CreatureIndex(self, key) - local methods = getmetatable(self) - if key == "uid" then - return methods.getId(self) - elseif key == "type" then - local creatureType = 0 - if methods.isPlayer(self) then - creatureType = THING_TYPE_PLAYER - elseif methods.isMonster(self) then - creatureType = THING_TYPE_MONSTER - elseif methods.isNpc(self) then - creatureType = THING_TYPE_NPC - end - return creatureType - elseif key == "itemid" then - return 1 - elseif key == "actionid" then - return 0 - end - return methods[key] - end - rawgetmetatable("Player").__index = CreatureIndex - -- rawgetmetatable("Monster").__index = CreatureIndex - -- rawgetmetatable("Npc").__index = CreatureIndex -end - --- Item index -do - local function ItemIndex(self, key) - local methods = getmetatable(self) - if key == "itemid" then - return methods.getId(self) - elseif key == "actionid" then - return methods.getActionId(self) - elseif key == "uid" then - return methods.getUniqueId(self) - elseif key == "type" then - return methods.getSubType(self) - end - return methods[key] - end - rawgetmetatable("Item").__index = ItemIndex - --rawgetmetatable("Container").__index = ItemIndex - --rawgetmetatable("Teleport").__index = ItemIndex -end - --- Action revscriptsys -do - local function ActionNewIndex(self, key, value) - if key == "onUse" then - self:onUse(value) - return - end - rawset(self, key, value) - end - rawgetmetatable("Action").__newindex = ActionNewIndex -end - --- TalkAction revscriptsys -do - local function TalkActionNewIndex(self, key, value) - if key == "onSay" then - self:onSay(value) - return - end - rawset(self, key, value) - end - logger.info('TalkAction revscriptsys') - logger.info(configKeys.BASE_DIRECTORY) - - local meta = rawgetmetatable("TalkAction") - logger.info('meta') - logger.info(meta) - meta.__newindex = TalkActionNewIndex -end - --- -- Sets a custom __newindex behavior for the EventCallback class's metatable. It dynamically maps certain keys to predefined callback methods within the EventCallback class. When a key matching a method name is added, it triggers the associated function, sets the event type, and logs the registration. This allows for flexible, runtime assignment of various event handlers through Lua scripts. --- local eventCallbacks = Game.getEventCallbacks() --- do --- local function EventCallbackNewIndex(self, key, value) --- local func = eventCallbacks[key] --- if func and type(func) == "function" then --- logger.debug("[Registering EventCallback: {}", key) --- func(self, value) --- self:type(key) --- else --- reportError("Invalid EventCallback with name: {}", tostring(key)) --- end --- end --- rawgetmetatable("EventCallback").__newindex = EventCallbackNewIndex --- end - - -- CreatureEvent revscriptsys --- do --- local function CreatureEventNewIndex(self, key, value) --- if key == "onLogin" then --- self:type("login") --- self:onLogin(value) --- return --- elseif key == "onLogout" then --- self:type("logout") --- self:onLogout(value) --- return --- elseif key == "onThink" then --- self:type("think") --- self:onThink(value) --- return --- elseif key == "onPrepareDeath" then --- self:type("preparedeath") --- self:onPrepareDeath(value) --- return --- elseif key == "onDeath" then --- self:type("death") --- self:onDeath(value) --- return --- elseif key == "onKill" then --- self:type("kill") --- self:onKill(value) --- return --- elseif key == "onAdvance" then --- self:type("advance") --- self:onAdvance(value) --- return --- elseif key == "onModalWindow" then --- self:type("modalwindow") --- self:onModalWindow(value) --- return --- elseif key == "onTextEdit" then --- self:type("textedit") --- self:onTextEdit(value) --- return --- elseif key == "onHealthChange" then --- self:type("healthchange") --- self:onHealthChange(value) --- return --- elseif key == "onManaChange" then --- self:type("manachange") --- self:onManaChange(value) --- return --- elseif key == "onExtendedOpcode" then --- self:type("extendedopcode") --- self:onExtendedOpcode(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("CreatureEvent").__newindex = CreatureEventNewIndex --- end - --- -- MoveEvent revscriptsys --- do --- local function MoveEventNewIndex(self, key, value) --- if key == "onEquip" then --- self:type("equip") --- self:onEquip(value) --- return --- elseif key == "onDeEquip" then --- self:type("deequip") --- self:onDeEquip(value) --- return --- elseif key == "onAddItem" then --- self:type("additem") --- self:onAddItem(value) --- return --- elseif key == "onRemoveItem" then --- self:type("removeitem") --- self:onRemoveItem(value) --- return --- elseif key == "onStepIn" then --- self:type("stepin") --- self:onStepIn(value) --- return --- elseif key == "onStepOut" then --- self:type("stepout") --- self:onStepOut(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("MoveEvent").__newindex = MoveEventNewIndex --- end - --- -- GlobalEvent revscriptsys --- do --- local function GlobalEventNewIndex(self, key, value) --- if key == "onThink" then --- self:onThink(value) --- return --- elseif key == "onTime" then --- self:onTime(value) --- return --- elseif key == "onStartup" then --- self:type("startup") --- self:onStartup(value) --- return --- elseif key == "onShutdown" then --- self:type("shutdown") --- self:onShutdown(value) --- return --- elseif key == "onRecord" then --- self:type("record") --- self:onRecord(value) --- return --- elseif key == "onPeriodChange" then --- self:type("periodchange") --- self:onPeriodChange(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("GlobalEvent").__newindex = GlobalEventNewIndex --- end - --- -- Weapons revscriptsys --- do --- local function WeaponNewIndex(self, key, value) --- if key == "onUseWeapon" then --- self:onUseWeapon(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("Weapon").__newindex = WeaponNewIndex --- end - --- -- Spells revscriptsys --- do --- local function SpellNewIndex(self, key, value) --- if key == "onCastSpell" then --- self:onCastSpell(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("Spell").__newindex = SpellNewIndex --- end - --- -- Monsters revscriptsys --- do --- local function MonsterTypeNewIndex(self, key, value) --- if key == "onThink" then --- self:eventType(MONSTERS_EVENT_THINK) --- self:onThink(value) --- return --- elseif key == "onAppear" then --- self:eventType(MONSTERS_EVENT_APPEAR) --- self:onAppear(value) --- return --- elseif key == "onDisappear" then --- self:eventType(MONSTERS_EVENT_DISAPPEAR) --- self:onDisappear(value) --- return --- elseif key == "onMove" then --- self:eventType(MONSTERS_EVENT_MOVE) --- self:onMove(value) --- return --- elseif key == "onSay" then --- self:eventType(MONSTERS_EVENT_SAY) --- self:onSay(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("MonsterType").__newindex = MonsterTypeNewIndex --- end - --- -- Npcs revscriptsys --- do --- local function NpcTypeNewIndex(self, key, value) --- if key == "onThink" then --- self:eventType(NPCS_EVENT_THINK) --- self:onThink(value) --- return --- elseif key == "onAppear" then --- self:eventType(NPCS_EVENT_APPEAR) --- self:onAppear(value) --- return --- elseif key == "onDisappear" then --- self:eventType(NPCS_EVENT_DISAPPEAR) --- self:onDisappear(value) --- return --- elseif key == "onMove" then --- self:eventType(NPCS_EVENT_MOVE) --- self:onMove(value) --- return --- elseif key == "onSay" then --- self:eventType(NPCS_EVENT_SAY) --- self:onSay(value) --- return --- elseif key == "onBuyItem" then --- self:eventType(NPCS_EVENT_PLAYER_BUY) --- self:onBuyItem(value) --- return --- elseif key == "onSellItem" then --- self:eventType(NPCS_EVENT_PLAYER_SELL) --- self:onSellItem(value) --- return --- elseif key == "onCheckItem" then --- self:eventType(NPCS_EVENT_PLAYER_CHECK_ITEM) --- self:onCheckItem(value) --- return --- elseif key == "onCloseChannel" then --- self:eventType(NPCS_EVENT_PLAYER_CLOSE_CHANNEL) --- self:onBuyItem(value) --- return --- end --- rawset(self, key, value) --- end --- rawgetmetatable("NpcType").__newindex = NpcTypeNewIndex --- end diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs index 0bf5f76f7..6c7c00d02 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs @@ -1,13 +1,18 @@ using LuaNET; +using NeoServer.Server.Configurations; using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class ConfigFunctions : LuaScriptInterface, IConfigFunctions { + private ServerConfiguration _serverConfiguration; + public ConfigFunctions( + ServerConfiguration serverConfiguration, ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(ConfigFunctions)) { + _serverConfiguration = serverConfiguration; } public void Init(LuaState L) @@ -34,7 +39,7 @@ public void Init(LuaState L) foreach (var item in Enum.GetValues()) RegisterVariable(L, "configKeys", item.ToString(), item); - RegisterVariable(L, "configKeys", "BASE_DIRECTORY", AppContext.BaseDirectory); + RegisterVariable(L, "configKeys", "BASE_DIRECTORY", AppContext.BaseDirectory + _serverConfiguration.DataLuaJit); } private static int LuaConfigManagerGetString(LuaState L) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs index 5d5fe513e..9a549bebe 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs @@ -32,7 +32,6 @@ private static int LuaCreatureCreate(LuaState L) ICreature creature = null; if (IsNumber(L, 2)) { - //creature = Game.GetInstance().GetCreatureByID(GetNumber(L, 2)); var id = GetNumber(L, 2); creature = _gameCreatureManager.GetCreatures().FirstOrDefault(c => c.CreatureId == id); } @@ -86,7 +85,7 @@ private static int LuaGetId(LuaState L) private static int LuaGetName(LuaState L) { - // player:getName() + // creature:getName() var creature = GetUserdata(L, 1); if (creature == null) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs index 5b2a82243..92e97c49a 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs @@ -13,8 +13,7 @@ public EnumFunctions( public void Init(LuaState L) { - //InitDirectionEnums(L); - RegisterEnumEx(L); + RegisterEnumCustom(L); //RegisterEnum(L); RegisterEnum(L); RegisterEnum(L); @@ -22,24 +21,6 @@ public void Init(LuaState L) RegisterEnum(L); } - public void InitDirectionEnums(LuaState L) - { - RegisterEnum(L, "DIRECTION_NORTH", Direction.North); - RegisterEnum(L, "DIRECTION_EAST", Direction.East); - RegisterEnum(L, "DIRECTION_SOUTH", Direction.South); - RegisterEnum(L, "DIRECTION_WEST", Direction.West); - RegisterEnum(L, "DIRECTION_SOUTHWEST", Direction.SouthWest); - RegisterEnum(L, "DIRECTION_SOUTHEAST", Direction.SouthEast); - RegisterEnum(L, "DIRECTION_NORTHWEST", Direction.NorthWest); - RegisterEnum(L, "DIRECTION_NORTHEAST", Direction.NorthEast); - RegisterEnum(L, "DIRECTION_NONE", Direction.None); - } - - public void InitReturnValueEnums(LuaState L) - { - //RegisterEnum(L, "RETURNVALUE_PLAYERISPZLOCKED", InvalidOperation.PlayerIsProtectionZoneLocked); - } - private static void RegisterEnum(LuaState L, string name, Enum value) { var enumValue = (uint)Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType())); @@ -52,7 +33,7 @@ private static void RegisterEnum(LuaState L) where T : Enum RegisterGlobalVariable(L, item.ToString(), Convert.ToUInt32(item)); } - private static void RegisterEnumEx(LuaState L) where T : Enum + private static void RegisterEnumCustom(LuaState L) where T : Enum { string prefix = typeof(T).Name.ToUpperInvariant() + "_"; foreach (var item in Enum.GetValues(typeof(T))) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs index 2683cb62b..bc7334eb7 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs @@ -1,6 +1,6 @@ using LuaNET; -using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Scripts.LuaJIT.Extensions; +using NeoServer.Server.Configurations; using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -9,14 +9,17 @@ public class GameFunctions : LuaScriptInterface, IGameFunctions { private static IScripts _scripts; private static ILogger _logger; + private static ServerConfiguration _serverConfiguration; public GameFunctions( + ServerConfiguration serverConfiguration, ILuaEnvironment luaEnvironment, ILogger logger, IScripts scripts) : base(nameof(GameFunctions)) { _scripts = scripts; _logger = logger; + _serverConfiguration = serverConfiguration; } public void Init(LuaState L) @@ -53,11 +56,12 @@ private static int LuaGameReload(LuaState L) } try { - _logger.Information("Reloading Scripts"); - var dir = AppContext.BaseDirectory; + var dir = AppContext.BaseDirectory + _serverConfiguration.DataLuaJit; _scripts.ClearAllScripts(); - _scripts.LoadScripts($"{dir}/Data/LuaJit/scripts", false, true); - _logger.Information("Reloaded Scripts"); + _scripts.LoadScripts($"{dir}/scripts", false, true); + + Lua.GC(LuaEnvironment.GetInstance().GetLuaState(), LuaGCParam.Collect, 0); + } catch (Exception e) { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs index 3465b6f90..f661495a4 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs @@ -28,28 +28,20 @@ public void Init(LuaState L) public static int LuaCreateItem(LuaState L) { // Item(uid) - var id = GetNumber(L, 2); + var id = GetNumber(L, 2); - //const auto &item = GetScriptEnv().GetItemByUID(id); - //if (item) - //{ - // Lua::pushUserdata(L, item); - // Lua::setItemMetatable(L, -1, item); - //} - //else - //{ - // lua_pushnil(L); - //} - //return 1; - - - // Item(uid) - IItem item = null; - PushUserdata(L, item); - SetMetatable(L, -1, "Item"); + var item = GetScriptEnv().GetItemByUID(id); + if (item != null) + { + PushUserdata(L, item); + SetMetatable(L, -1, "Item"); + } + else + { + Lua.PushNil(L); + } return 1; - } public static int LuaGetItemId(LuaState L) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs index 26b397315..76e5f4cfc 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs @@ -37,14 +37,6 @@ private static int LuaPlayerCreate(LuaState L) { var id = GetNumber(L, 2); _gameCreatureManager.TryGetPlayer(id, out player); - //if (id >= Player::getFirstID() && id <= Player::getLastID()) - //{ - // player = g_game().getPlayerByID(id); - //} - //else - //{ - // player = g_game().getPlayerByGUID(id); - //} } else if (IsString(L, 2)) { @@ -57,14 +49,6 @@ private static int LuaPlayerCreate(LuaState L) Lua.PushNumber(L, (int)ReturnValueType.RETURNVALUE_PLAYERWITHTHISNAMEISNOTONLINE); return 2; } - - //ReturnValue ret = g_game().getPlayerByNameWildcard(getString(L, 2), player); - //if (ret != RETURNVALUE_NOERROR) - //{ - // lua_pushnil(L); - // lua_pushnumber(L, ret); - // return 2; - //} } else if (IsUserdata(L, 2)) { @@ -108,14 +92,6 @@ private static int LuaTeleportTo(LuaState L) return 1; } - //var oldPosition = creature.Location; - //if (Game.GetInstance().InternalTeleport(creature, position, pushMovement)) - //{ - // Logger.GetInstance().Error($"[{nameof(LuaTeleportTo)}] Failed to teleport creature {creature.Name}, on position {oldPosition}, error code: {0}");// GetReturnMessage(ret)); - // PushBoolean(L, false); - // return 1; - //} - creature.TeleportTo(position); PushBoolean(L, true); @@ -136,36 +112,37 @@ private static int LuaPlayerSendTextMessage(LuaState L) int parameters = Lua.GetTop(L); - //TextMessage message(GetNumber(L, 2), GetString(L, 3)); var messageType = GetNumber(L, 2); var messageText = GetString(L, 3); - if (parameters == 4) - { - //uint16_t channelId = getNumber(L, 4); - //const auto &channel = g_chat().getChannel(player, channelId); - //if (!channel || !channel->hasUser(player)) - //{ - // pushBoolean(L, false); - // return 1; - //} - //message.channelId = channelId; - } - else - { - //if (parameters >= 6) - //{ - // message.position = getPosition(L, 4); - // message.primary.value = getNumber(L, 5); - // message.primary.color = getNumber(L, 6); - //} - - //if (parameters >= 8) - //{ - // message.secondary.value = getNumber(L, 7); - // message.secondary.color = getNumber(L, 8); - //} - } + //its not used + //if (parameters == 4) + //{ + // var channelId = GetNumber(L, 4); + + // const auto &channel = g_chat().getChannel(player, channelId); + // if (!channel || !channel->hasUser(player)) + // { + // pushBoolean(L, false); + // return 1; + // } + // message.channelId = channelId; + //} + //else + //{ + // if (parameters >= 6) + // { + // message.position = getPosition(L, 4); + // message.primary.value = getNumber(L, 5); + // message.primary.color = getNumber(L, 6); + // } + + // if (parameters >= 8) + // { + // message.secondary.value = getNumber(L, 7); + // message.secondary.color = getNumber(L, 8); + // } + //} NotificationSenderService.Send(player, messageText, (TextMessageOutgoingType)messageType); PushBoolean(L, true); @@ -185,21 +162,4 @@ private static int LuaPlayerIsPzLocked(LuaState L) return 1; } - - //private static int LuaPlayerSendCancelMessage(LuaState L) - //{ - // // player:sendCancelMessage() - - // var player = GetUserdata(L, 1); - - // if (player == null) - // Lua.PushNil(L); - - // var messageText = GetString(L, 2); - - // //InvalidOperation.NotAPartyLeader - // OperationFailService.Send(player.Id, messageText); - - // return 1; - //} } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs index b0b58fd1d..d4188b978 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs @@ -1,5 +1,4 @@ using LuaNET; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -13,11 +12,8 @@ public void Init(LuaState L) { RegisterSharedClass(L, "TalkAction", "", LuaCreateTalkAction); RegisterMethod(L, "TalkAction", "onSay", LuaTalkActionOnSay); - //RegisterMethod(L, "TalkAction", "groupType", LuaTalkActionGroupType); RegisterMethod(L, "TalkAction", "register", LuaTalkActionRegister); RegisterMethod(L, "TalkAction", "separator", LuaTalkActionSeparator); - //RegisterMethod(L, "TalkAction", "getName", LuaTalkActionGetName); - //RegisterMethod(L, "TalkAction", "getGroupType", LuaTalkActionGetGroupType); } private static int LuaCreateTalkAction(LuaState L) @@ -30,24 +26,10 @@ private static int LuaCreateTalkAction(LuaState L) } var talkActionSharedPtr = new TalkAction(GetScriptEnv().GetScriptInterface()); - //var talkActionSharedPtr = new TalkAction(); talkActionSharedPtr.SetWords(wordsVector); - ////IntPtr unmanagedAddr = Marshal.AllocHGlobal(Marshal.SizeOf(talkActionSharedPtr)); - ////Marshal.StructureToPtr(talkActionSharedPtr, unmanagedAddr, true); - - //var talkAction = new Structs.TalkAction(GetScriptEnv().GetScriptInterface()); - //var talkAction = new Structs.TalkAction(); - //talkAction.SetWords(wordsVector); - PushUserdata(L, talkActionSharedPtr); - //PushUserdata(L, talkAction, 1); - //PushUserdata(L, test, 1); - - //var teste = GetUserdataShared(L, 3, 1); - SetMetatable(L, -1, "TalkAction"); - //SetWeakMetatable(L, -1, "TalkAction"); return 1; } @@ -75,89 +57,25 @@ private static int LuaTalkActionOnSay(LuaState L) return 1; } - //private static int LuaTalkActionGroupType(lua_State L) - //{ - // // talkAction:groupType(GroupType = GROUP_TYPE_NORMAL) - // //var talkActionSharedPtr = GetUserdata(L, 1); - // //if (talkActionSharedPtr == null) - // //{ - // // ReportError(GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); - // // PushBoolean(L, false); - // // return 1; - // //} - - // //var groupType = Account.GroupType.None; - - // //var type = Lua.Type(L, 2); - // //if (type == LuaType.Number) - // //{ - // // groupType = (Account.GroupType)GetNumber(L, 2); - // //} - // //else if (type == LuaType.String) - // //{ - // // var strValue = GetString(L, 2); - // // switch (strValue.ToLower()) - // // { - // // case "normal": - // // groupType = Account.GroupType.Normal; - // // break; - // // case "tutor": - // // groupType = Account.GroupType.Tutor; - // // break; - // // case "seniortutor": - // // case "senior tutor": - // // groupType = Account.GroupType.SeniorTutor; - // // break; - // // case "gamemaster": - // // case "gm": - // // groupType = Account.GroupType.GameMaster; - // // break; - // // case "communitymanager": - // // case "cm": - // // case "community manager": - // // groupType = Account.GroupType.CommunityManager; - // // break; - // // case "god": - // // groupType = Account.GroupType.God; - // // break; - // // default: - // // var errorString = $"Invalid group type string value {strValue} for group type for script: {GetScriptEnv().GetScriptInterface().GetLoadingScriptName()}"; - // // ReportError(errorString); - // // PushBoolean(L, false); - // // return 1; - // // } - // //} - // //else - // //{ - // // var errorString = $"Expected number or string value for group type for script: {GetScriptEnv().GetScriptInterface().GetLoadingScriptName()}"; - // // ReportError(errorString); - // // PushBoolean(L, false); - // // return 1; - // //} - - // //talkActionSharedPtr.SetGroupType(groupType); - // //PushBoolean(L, true); - // return 1; - //} - + private static int LuaTalkActionRegister(LuaState L) { // talkAction:register() - var talkActionSharedPtr = GetUserdata(L, 1); - if (talkActionSharedPtr == null) + var talkAction = GetUserdata(L, 1); + if (talkAction == null) { ReportError(nameof(LuaTalkActionRegister), GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); PushBoolean(L, false); return 1; } - if (!talkActionSharedPtr.IsLoadedCallback()) + if (!talkAction.IsLoadedCallback()) { PushBoolean(L, false); return 1; } - //if (talkActionSharedPtr.GroupType == Account.GroupType.None) + //if (talkAction.GroupType == Account.GroupType.None) //{ // var errorString = $"TalkAction with name {talkActionSharedPtr.Words} does not have groupType"; // ReportError(errorString); @@ -165,8 +83,7 @@ private static int LuaTalkActionRegister(LuaState L) // return 1; //} - PushBoolean(L, TalkActions.GetInstance().RegisterLuaEvent(talkActionSharedPtr)); - //RemoveUserdata(); + PushBoolean(L, TalkActions.GetInstance().RegisterLuaEvent(talkAction)); //talkActionSharedPtr.ExecuteSay(Game.GetInstance().GetCurrentPlayer(), "!help", "", SpeakClassesType.TALKTYPE_SAY); @@ -203,19 +120,4 @@ private static int LuaTalkActionGetName(LuaState L) PushString(L, talkActionSharedPtr.GetWords()); return 1; } - - //private static int LuaTalkActionGetGroupType(lua_State L) - //{ - // // local groupType = talkAction:getGroupType() - // var talkActionSharedPtr = GetUserdata(L, 1); - // if (talkActionSharedPtr == null) - // { - // ReportError(GetErrorDesc(ErrorCodeType.LUA_ERROR_TALK_ACTION_NOT_FOUND)); - // PushBoolean(L, false); - // return 1; - // } - - // lua_pushnumber(L, (double)talkActionSharedPtr.GroupType); - // return 1; - //} } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs deleted file mode 100644 index b2bd5dc49..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Logger.cs +++ /dev/null @@ -1,36 +0,0 @@ -//using Serilog; - -//namespace NeoServer.Scripts.LuaJIT; - -//public class Logger -//{ -// private static Logger _instance; -// public static Logger GetInstance() => _instance == null ? _instance = new Logger() : _instance; - -// private ILogger _logger; - -// public Logger() -// { -// _logger = new LoggerConfiguration().CreateLogger(); -// } - -// public void Debug(string message, string args = "") -// { -// _logger.Debug(message, args); -// } - -// public void Info(string message, string args = "") -// { -// _logger.Information(message, args); -// } - -// public void Warn(string message, string args = "") -// { -// _logger.Warning(message, args); -// } - -// public void Error(string message, string args = "") -// { -// _logger.Error(message, args); -// } -//} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs index 85aa74abf..7997d1504 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs @@ -1,4 +1,6 @@ -namespace NeoServer.Scripts.LuaJIT; +using NeoServer.Game.Common.Location.Structs; + +namespace NeoServer.Scripts.LuaJIT; // Enums public enum LuaDataType : byte @@ -219,7 +221,7 @@ public struct LuaVariant public string Text; public string InstantName; public string RuneName; - //public Position Pos; + public Location Pos; public uint Number = 0; public LuaVariant() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs index dd570cc72..7e8568ada 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs @@ -5,7 +5,8 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Location.Structs; -using NeoServer.Scripts.LuaJIT.Structs; +using NeoServer.Server.Helpers; +using Serilog; namespace NeoServer.Scripts.LuaJIT; @@ -15,6 +16,20 @@ public class LuaFunctionsLoader public const int LUA_ENVIRONINDEX = (-10001); public const int LUA_GLOBALSINDEX = (-10002); + protected static ILogger _logger; + + public LuaFunctionsLoader() + { + //_logger = IoC.GetInstance(); + + //_logger.Information("Log from LuaFunctionsLoader"); + + for (int i = 0; i < scriptEnv.Length; i++) + { + scriptEnv[i] = new ScriptEnvironment(); + } + } + public static string GetErrorDesc(ErrorCodeType code) { switch (code) @@ -97,15 +112,10 @@ public static void ReportError(string function, string errorDesc, bool stackTrac GetScriptEnv().GetEventInfo(out scriptId, out scriptInterface, out callbackId, out timerEvent); - //Logger.Error("Lua script error: \nscriptInterface: [{0}]\nscriptId: [{1}]\ntimerEvent: [{2}]\n callbackId:[{3}]\nfunction: [{4}]\nerror [{5}]", - // scriptInterface != null ? scriptInterface.GetInterfaceName() : "", - // scriptId != 0 ? scriptInterface.GetFileById(scriptId) : "", - // timerEvent ? "in a timer event called from:" : "", - // callbackId != 0 ? scriptInterface.GetFileById(callbackId) : "", - // function ?? "", - // (stackTrace && scriptInterface != null) ? scriptInterface.GetStackTrace(errorDesc) : errorDesc); + if (_logger == null) + _logger = IoC.GetInstance(); - Console.WriteLine(string.Format("Lua script error: \nscriptInterface: [{0}]\nscriptId: [{1}]\ntimerEvent: [{2}]\n callbackId:[{3}]\nfunction: [{4}]\nerror [{5}]", + _logger.Error(string.Format("Lua script error: \nscriptInterface: [{0}]\nscriptId: [{1}]\ntimerEvent: [{2}]\n callbackId:[{3}]\nfunction: [{4}]\nerror [{5}]", scriptInterface != null ? scriptInterface.GetInterfaceName() : "", scriptId != 0 ? scriptInterface.GetFileById(scriptId) : "", timerEvent ? "in a timer event called from:" : "", @@ -144,9 +154,8 @@ public static void PushVariant(LuaState L, LuaVariant var) case LuaVariantType.VARIANT_TARGETPOSITION: case LuaVariantType.VARIANT_POSITION: { - //TODO: Muniz - //PushPosition(L, var.Po); - //Lua.SetField(L, -2, "pos"); + PushPosition(L, var.Pos); + Lua.SetField(L, -2, "pos"); break; } default: @@ -191,38 +200,6 @@ public static void PushThing(LuaState L, IThing thing) } } - //public static void PushCylinder(LuaState L, Cylinder cylinder) - //{ - // if (ValidateDispatcherContext(nameof(PushCylinder))) - // { - // return; - // } - - // if (cylinder.GetCreature() is Creature creature) - // { - // PushUserdata(L, creature); - // SetCreatureMetatable(L, -1, creature); - // } - // else if (cylinder.GetItem() is Item parentItem) - // { - // PushUserdata(L, parentItem); - // SetItemMetatable(L, -1, parentItem); - // } - // else if (cylinder.GetTile() is Tile tile) - // { - // PushUserdata(L, tile); - // SetMetatable(L, -1, "Tile"); - // } - // else if (cylinder == VirtualCylinder.VirtualCylinder) - // { - // PushBoolean(L, true); - // } - // else - // { - // Lua.PushNil(L); - // } - //} - public static void PushString(LuaState L, string value) { if (ValidateDispatcherContext(nameof(PushString))) @@ -364,18 +341,6 @@ public static void SetCreatureMetatable(LuaState L, int index, ICreature creatur Lua.SetMetaTable(L, index - 1); } - //public static CombatDamage GetCombatDamage(LuaState L) - //{ - // CombatDamage damage = new CombatDamage - // { - // Primary = { Value = GetNumber(L, -4), Type = GetNumber(L, -3) }, - // Secondary = { Value = GetNumber(L, -2), Type = GetNumber(L, -1) } - // }; - - // Lua.Pop(L, 4); - // return damage; - //} - public static string GetFormatedLoggerMessage(LuaState L) { string format = GetString(L, 1); @@ -415,8 +380,7 @@ public static string GetFormatedLoggerMessage(LuaState L) } else { - //g_logger().warn("[{}] invalid param type", nameof(GetFormatedLoggerMessage)); - Console.WriteLine("[{0}] invalid param type", nameof(GetFormatedLoggerMessage)); + _logger.Warning("[{0}] invalid param type", nameof(GetFormatedLoggerMessage)); } } @@ -429,7 +393,7 @@ public static string GetFormatedLoggerMessage(LuaState L) } catch (Exception e) { - //g_logger().error("[{}] format error: {}", nameof(GetFormatedLoggerMessage), e.what()); + _logger.Error("[{}] format error: {}", nameof(GetFormatedLoggerMessage), e.Message); } return string.Empty; @@ -482,31 +446,6 @@ public static Location GetPosition(LuaState L, int arg) return position; } - //public static Outfit_t GetOutfit(LuaState L, int arg) - //{ - // Outfit_t outfit = new Outfit_t - // { - // lookMountFeet = GetField(L, arg, "lookMountFeet"), - // lookMountLegs = GetField(L, arg, "lookMountLegs"), - // lookMountBody = GetField(L, arg, "lookMountBody"), - // lookMountHead = GetField(L, arg, "lookMountHead"), - // lookFamiliarsType = GetField(L, arg, "lookFamiliarsType"), - // lookMount = GetField(L, arg, "lookMount"), - // lookAddons = GetField(L, arg, "lookAddons"), - - // lookFeet = GetField(L, arg, "lookFeet"), - // lookLegs = GetField(L, arg, "lookLegs"), - // lookBody = GetField(L, arg, "lookBody"), - // lookHead = GetField(L, arg, "lookHead"), - - // lookTypeEx = GetField(L, arg, "lookTypeEx"), - // lookType = GetField(L, arg, "lookType") - // }; - - // Lua.Pop(L, 13); - // return outfit; - //} - public static LuaVariant GetVariant(LuaState L, int arg) { LuaVariant var = new LuaVariant @@ -531,9 +470,7 @@ public static LuaVariant GetVariant(LuaState L, int arg) case LuaVariantType.VARIANT_POSITION: case LuaVariantType.VARIANT_TARGETPOSITION: Lua.GetField(L, arg, "pos"); - //TODO: MUNIZ - //var.Pos = GetPosition(L, Lua.GetTop(L)); - Lua.Pop(L, 4); + var.Pos = GetPosition(L, Lua.GetTop(L)); break; default: @@ -545,90 +482,6 @@ public static LuaVariant GetVariant(LuaState L, int arg) return var; } - //public static Thing GetThing(LuaState L, int arg) - //{ - // Thing thing; - // if (Lua.GetMetaTable(L, arg) != 0) - // { - // Lua.RawGetI(L, -1, 't'); - // switch (GetNumber(L, -1)) - // { - // case LuaDataType.Item: - // thing = GetUserdataShared(L, arg); - // break; - // case LuaDataType.Container: - // thing = GetUserdataShared(L, arg); - // break; - // case LuaDataType.Teleport: - // thing = GetUserdataShared(L, arg); - // break; - // case LuaDataType.Player: - // thing = GetUserdataShared(L, arg); - // break; - // case LuaDataType.Monster: - // thing = GetUserdataShared(L, arg); - // break; - // case LuaDataType.Npc: - // thing = GetUserdataShared(L, arg); - // break; - // default: - // thing = null; - // break; - // } - // Lua.Pop(L, 2); - // } - // else - // { - // thing = GetScriptEnv().GetThingByUID(GetNumber(L, arg)); - // } - // return thing; - //} - - //public static shared_ptr GetCreature(LuaState L, int arg) - //{ - // if (IsUserdata(L, arg)) - // { - // return GetUserdataShared(L, arg); - // } - // return g_game().GetCreatureByID(GetNumber(L, arg)); - //} - - //public static shared_ptr GetPlayer(LuaState L, int arg, bool allowOffline = false) - //{ - // if (IsUserdata(L, arg)) - // { - // return GetUserdataShared(L, arg); - // } - // else if (IsNumber(L, arg)) - // { - // return g_game().GetPlayerByID(GetNumber(L, arg), allowOffline); - // } - // else if (IsString(L, arg)) - // { - // return g_game().GetPlayerByName(GetString(L, arg), allowOffline); - // } - // g_logger().Warn("LuaFunctionsLoader::GetPlayer: Invalid argument."); - // return null; - //} - - //public static shared_ptr GetGuild(LuaState L, int arg, bool allowOffline = false) - //{ - // if (IsUserdata(L, arg)) - // { - // return GetUserdataShared(L, arg); - // } - // else if (IsNumber(L, arg)) - // { - // return g_game().GetGuild(GetNumber(L, arg), allowOffline); - // } - // else if (IsString(L, arg)) - // { - // return g_game().GetGuildByName(GetString(L, arg), allowOffline); - // } - // g_logger().Warn("LuaFunctionsLoader::GetGuild: Invalid argument."); - // return null; - //} - public static string GetFieldString(LuaState L, int arg, string key) { Lua.GetField(L, arg, key); @@ -666,39 +519,6 @@ public static void PushBoolean(LuaState L, bool value) Lua.PushBoolean(L, value); } - //public static void PushCombatDamage(LuaState L, CombatDamage damage) - //{ - // if (ValidateDispatcherContext(nameof(PushCombatDamage))) - // { - // return; - // } - - // Lua.PushNumber(L, damage.primary.value); - // Lua.PushNumber(L, damage.primary.type); - // Lua.PushNumber(L, damage.secondary.value); - // Lua.PushNumber(L, damage.secondary.type); - // Lua.PushNumber(L, damage.origin); - //} - - //public static void PushInstantSpell(LuaState L, InstantSpell spell) - //{ - // if (ValidateDispatcherContext(__FUNCTION__)) - // { - // return; - // } - - // Lua.CreateTable(L, 0, 6); - - // SetField(L, "name", spell.GetName()); - // SetField(L, "words", spell.GetWords()); - // SetField(L, "level", spell.GetLevel()); - // SetField(L, "mlevel", spell.GetMagicLevel()); - // SetField(L, "mana", spell.GetMana()); - // SetField(L, "manapercent", spell.GetManaPercent()); - - // SetMetatable(L, -1, "Spell"); - //} - public static void PushPosition(LuaState L, Location position, int stackpos = 0) { if (ValidateDispatcherContext(nameof(PushPosition))) @@ -716,29 +536,6 @@ public static void PushPosition(LuaState L, Location position, int stackpos = 0) SetMetatable(L, -1, "Position"); } - //public static void PushOutfit(LuaState L, Outfit_t outfit) - //{ - // if (ValidateDispatcherContext(__FUNCTION__)) - // { - // return; - // } - - // Lua.CreateTable(L, 0, 13); - // SetField(L, "lookType", outfit.lookType); - // SetField(L, "lookTypeEx", outfit.lookTypeEx); - // SetField(L, "lookHead", outfit.lookHead); - // SetField(L, "lookBody", outfit.lookBody); - // SetField(L, "lookLegs", outfit.lookLegs); - // SetField(L, "lookFeet", outfit.lookFeet); - // SetField(L, "lookAddons", outfit.lookAddons); - // SetField(L, "lookMount", outfit.lookMount); - // SetField(L, "lookMountHead", outfit.lookMountHead); - // SetField(L, "lookMountBody", outfit.lookMountBody); - // SetField(L, "lookMountLegs", outfit.lookMountLegs); - // SetField(L, "lookMountFeet", outfit.lookMountFeet); - // SetField(L, "lookFamiliarsType", outfit.lookFamiliarsType); - //} - public static void RegisterClass(LuaState L, string className, string baseClass, LuaFunction newFunction = null) { // className = {} @@ -925,13 +722,6 @@ public static string EscapeString(string str) return s; } - //public static int LuaUserdataCompare(LuaState L) - //{ - // PushBoolean(L, GetUserdata(L, 1) == GetUserdata(L, 2)); - // return 1; - //} - - public static int LuaUserdataCompare(LuaState L) where T : class { PushBoolean(L, GetUserdata(L, 1) == GetUserdata(L, 2)); @@ -954,19 +744,12 @@ public static int LuaGarbageCollection(LuaState L) { try { - IntPtr userdata = (IntPtr)Lua.ToUserData(L, 1); - var stru = (UserDataStruct)Marshal.PtrToStructure(Marshal.ReadIntPtr(userdata), typeof(UserDataStruct)); - - if (_objects.ContainsKey(stru.Index)) - { - var obj = _objects[stru.Index]; - _objects.Remove(stru.Index); - _objectsBackMap.Remove(obj); - } - - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); + //const auto objPtr = static_cast*>(lua_touserdata(L, 1)); + //if (objPtr) + //{ + // objPtr->reset(); + //} + //return 0; } catch (Exception e) { @@ -992,12 +775,6 @@ public static bool ValidateDispatcherContext(string fncName) private static int scriptEnvIndex = 0; private static ScriptEnvironment[] scriptEnv = new ScriptEnvironment[16]; - //public static void pushUserdata(LuaState L, T value) where T : class - //{ - // IntPtr userdata = (nint)Lua.NewUserData(L, (uint)IntPtr.Size); - // System.Runtime.InteropServices.Marshal.WriteIntPtr(userdata, System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0)); - //} - public static T GetNumber(LuaState L, int arg) where T : struct { if (typeof(T).IsEnum) @@ -1037,80 +814,6 @@ public static T GetUserdataShared(LuaState L, int arg) where T : struct return (T)Marshal.PtrToStructure(ptr, typeof(T)); } - public enum LuaType - { - /// - /// - /// - None = -1, - /// - /// LUA_TNIL - /// - Nil = 0, - /// - /// LUA_TBOOLEAN - /// - Boolean = 1, - /// - /// LUA_TLIGHTUSERDATA - /// - LightUserData = 2, - /// - /// LUA_TNUMBER - /// - Number = 3, - /// - /// LUA_TSTRING - /// - String = 4, - /// - /// LUA_TTABLE - /// - Table = 5, - /// - /// LUA_TFUNCTION - /// - Function = 6, - /// - /// LUA_TUSERDATA - /// - UserData = 7, - /// - /// LUA_TTHREAD - /// - /// // - Thread = 8, - } - - public static int ToNetObject(LuaState state, int index, IntPtr tag) - { - //if (state.Type(index) != LuaType.UserData) - // return -1; - - IntPtr userData; - - if (Lua.GetMetaTable(state, index) != 0) - { - userData = (IntPtr)Lua.ToUserData(state, index); - if (userData != IntPtr.Zero) - return Marshal.ReadInt32(userData); - } - - //userData = state.CheckUData(index, "luaNet_class"); - //if (userData != IntPtr.Zero) - // return Marshal.ReadInt32(userData); - - //userData = state.CheckUData(index, "luaNet_searchbase"); - //if (userData != IntPtr.Zero) - // return Marshal.ReadInt32(userData); - - //userData = state.CheckUData(index, "luaNet_function"); - //if (userData != IntPtr.Zero) - // return Marshal.ReadInt32(userData); - - return -1; - } - public static T GetUserdata(LuaState L, int arg) where T : class { var userdata = GetRawUserdata(L, arg); @@ -1121,7 +824,13 @@ public static T GetUserdata(LuaState L, int arg) where T : class var stru = (UserDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(UserDataStruct)); - return (T)_objects[stru.Index]; + lock (_objects) + { + if (_objects.TryGetValue(stru.Index, out var value)) + return (T)value; + } + + return null; } public static T GetUserdataStruct(LuaState L, int arg) where T : struct @@ -1133,24 +842,16 @@ public static T GetUserdataStruct(LuaState L, int arg) where T : struct } var stru = (UserDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(UserDataStruct)); + + lock (_objects) + { + if (_objects.TryGetValue(stru.Index, out var value)) + return (T)value; + } - return (T)_objects[stru.Index]; + return default; } - //public static void RemoveUserdata() where T : class - //{ - // var key = typeof(T); - // if (_userData.ContainsKey(key)) - // _userData.Remove(key); - - // //IntPtr userdata = GetRawUserdata(L, arg); - // //if (userdata == IntPtr.Zero) - // //{ - // // return null; - // //} - // //return (T)System.Runtime.InteropServices.Marshal.PtrToStructure(System.Runtime.InteropServices.Marshal.ReadIntPtr(userdata), typeof(T)); - //} - public static IntPtr GetRawUserdata(LuaState L, int arg) where T : class { return (nint)Lua.ToUserData(L, arg); @@ -1239,14 +940,6 @@ public static void SetField(LuaState L, string index, string value) Lua.SetField(L, -2, index); } - public LuaFunctionsLoader() - { - for (int i = 0; i < scriptEnv.Length; i++) - { - scriptEnv[i] = new ScriptEnvironment(); - } - } - public ScriptEnvironment InternalGetScriptEnv() { if (scriptEnvIndex < 0 || scriptEnvIndex >= 16) @@ -1302,8 +995,8 @@ public static void NewUData(LuaState L, int val) public static T ToObject(LuaState L, int index, bool freeGCHandle = true) { - //if (IsNil(index) || !IsLightUserData(index)) - // return default(T); + if (IsNil(L, index)/* || !IsLightUserData(index)*/) + return default(T); IntPtr data = (IntPtr)Lua.ToUserData(L, index); if (data == IntPtr.Zero) @@ -1321,84 +1014,6 @@ public static T ToObject(LuaState L, int index, bool freeGCHandle = true) return reference; } - public static void PushUserdata2(LuaState L, T value, int a) where T : class - { - IntPtr ptr = IntPtr.Zero; - try - { - IntPtr userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); - - ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); - Marshal.WriteIntPtr(userdata, ptr); - } - catch (Exception ex) - { - - } - finally - { - if (ptr != IntPtr.Zero) - { - try - { - Marshal.FreeHGlobal(ptr); - } - catch (Exception ex) - { - } - } - } - } - - public static void PushUserdata(LuaState L, T value, int a) where T : struct - { - IntPtr ptr = IntPtr.Zero; - try - { - //IntPtr userdata2 = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); - - //ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); - //Marshal.WriteIntPtr(userdata2, ptr); - - //TextWriter tw = Console.Out; - //GCHandle gch = GCHandle.Alloc(tw); - GCHandle gch2 = GCHandle.Alloc(value, GCHandleType.Pinned); - //gch.Free(); - - IntPtr userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); - - //var a = GCHandle.Alloc(value, GCHandleType.Pinned); - - //fixed (T* aptr = &value) - //{ - // Console.WriteLine(*aptr); - //} - - //ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); - //ptr = GCHandle.Alloc(value).AddrOfPinnedObject(); - Marshal.WriteIntPtr(userdata, gch2.AddrOfPinnedObject()); - //Marshal.WriteIntPtr(userdata, ptr); - gch2.Free(); - } - catch (Exception ex) - { - - } - finally - { - if (ptr != IntPtr.Zero) - { - try - { - //Marshal.FreeHGlobal(ptr); - } - catch (Exception ex) - { - } - } - } - } - // Compare cache entries by exact reference to avoid unwanted aliases private class ReferenceComparer : IEqualityComparer { @@ -1416,9 +1031,9 @@ public int GetHashCode(object obj) } // object to object # - static readonly Dictionary _objectsBackMap = new Dictionary(new ReferenceComparer()); + static readonly ConcurrentDictionary _objectsBackMap = new ConcurrentDictionary(new ReferenceComparer()); // object # to object (FIXME - it should be possible to get object address as an object #) - static readonly Dictionary _objects = new Dictionary(); + static readonly ConcurrentDictionary _objects = new ConcurrentDictionary(); static readonly List _structuresToReuse = new List(); static readonly ConcurrentQueue finalizedReferences = new ConcurrentQueue(); @@ -1434,11 +1049,15 @@ private static int AddObject(object obj) { // New object: inserts it in the list int index = _nextObj++; - _objects[index] = obj; - if (!obj.GetType().IsValueType || obj.GetType().IsEnum) - _objectsBackMap[obj] = index; + lock (_objects) + { + _objects[index] = obj; + if (!obj.GetType().IsValueType || obj.GetType().IsEnum) + _objectsBackMap[obj] = index; + } + return index; } @@ -1473,11 +1092,6 @@ public static void PushUserdata(LuaState L, object o) else { userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); - - // Registre o userdata no registry Lua - //Lua.PushValue(L, -1); // Duplicar o userdata - //int referenceIndex = Lua.Ref(L, LUA_REGISTRYINDEX); - stru = new UserDataStruct(index, userdata, 0); } @@ -1485,69 +1099,5 @@ public static void PushUserdata(LuaState L, object o) Marshal.WriteIntPtr(userdata, gch2.AddrOfPinnedObject()); gch2.Free(); - - //lua_setupvalue - - //var obj1 = _objects[index]; - //var obj2 = _objects[userdata]; - - //IntPtr pointer = (IntPtr)Lua.NewUserData(L, (ulong)Marshal.SizeOf(typeof(int))); - //Marshal.WriteInt32(pointer, index); - - //_userData.Add(typeof(T), value); - - //IntPtr ptr = IntPtr.Zero; - //try - //{ - // IntPtr userdata = (IntPtr)Lua.NewUserData(L, (ulong)IntPtr.Size); - - // ptr = Marshal.UnsafeAddrOfPinnedArrayElement(new T[] { value }, 0); - // Marshal.WriteIntPtr(userdata, ptr); - //} - //catch(Exception ex) - //{ - - //} - //finally - //{ - // if (ptr != IntPtr.Zero) - // { - // try - // { - // Marshal.FreeHGlobal(ptr); - // } - // catch (Exception ex) - // { - // } - // } - //} } - - //public static void PushUserdata(LuaState L, TalkAction value) - //{ - // var userdata = Lua.NewUserData(L, (ulong)IntPtr.Size); - // Marshal.StructureToPtr(value, (nint)userdata, false); - - // //System.Runtime.InteropServices.Marshal.StructureToPtr(value, userData, false); - // //var userData = (IntPtr)Lua.NewUserData(L, sizeof(T)); - //} - - //public static void PushUserdata(LuaState L, T value) where T : class - //{ - // var userdata = Lua.NewUserData(L, (ulong)IntPtr.Size); - // //Marshal.StructureToPtr(value, userdata, false); - - // //System.Runtime.InteropServices.Marshal.StructureToPtr(value, userData, false); - // //var userData = (IntPtr)Lua.NewUserData(L, sizeof(T)); - //} - - //public static void PushUserdata(LuaState L, IntPtr value) - //{ - // var userData = (IntPtr)Lua.NewUserData(L, sizeof(value)); - //} - - //public static void PushString(LuaState L, string value) - //{ - // lua_pushstring(L, value); - //} } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index 82beb35c8..91aefe2fb 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -4,7 +4,7 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Location.Structs; -using NeoServer.Scripts.LuaJIT.Functions; +using NeoServer.Server.Configurations; using Serilog; namespace NeoServer.Scripts.LuaJIT; @@ -54,6 +54,8 @@ public class LuaGameManager : ILuaGameManager private readonly ITalkActionFunctions _talkActionFunctions; private readonly ITileFunctions _tileFunctions; + private readonly ServerConfiguration _serverConfiguration; + #endregion #region Constructors @@ -77,7 +79,8 @@ public LuaGameManager( IPlayerFunctions playerFunctions, IPositionFunctions positionFunctions, ITalkActionFunctions talkActionFunctions, - ITileFunctions tileFunctions) + ITileFunctions tileFunctions, + ServerConfiguration serverConfiguration) { _logger = logger; _luaEnviroment = luaEnviroment; @@ -101,7 +104,7 @@ public LuaGameManager( _talkActionFunctions = talkActionFunctions; _tileFunctions = tileFunctions; - Start(); + _serverConfiguration = serverConfiguration; } #endregion @@ -174,7 +177,7 @@ public bool PlayerUseItemEx(IPlayer player, Location fromPos, Location toPos, by #region Private Methods - private void Start() + public void Start() { var dir = AppContext.BaseDirectory; @@ -206,9 +209,9 @@ private void Start() ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); - ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}/Data/LuaJit/core.lua", "core.lua"), "core.lua"); + ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}{_serverConfiguration.DataLuaJit}/core.lua", "core.lua"), "core.lua"); - ModulesLoadHelper(_scripts.LoadScripts($"{dir}/Data/LuaJit/scripts", false, false), "/Data/LuaJit/scripts"); + ModulesLoadHelper(_scripts.LoadScripts($"{dir}{_serverConfiguration.DataLuaJit}/scripts", false, false), "/Data/LuaJit/scripts"); } private void ModulesLoadHelper(bool loaded, string moduleName) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs index 600f390d9..170052f05 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs @@ -1,4 +1,5 @@ using LuaNET; +using NeoServer.Server.Helpers; namespace NeoServer.Scripts.LuaJIT; @@ -20,6 +21,16 @@ public class LuaScriptInterface : LuaFunctionsLoader, ILuaScriptInterface private LuaEnvironment g_luaEnvironment() => LuaEnvironment.GetInstance(); + //private ILuaEnvironment _luaEnvironment = null; + + //private ILuaEnvironment g_luaEnvironment() + //{ + // if(_luaEnvironment == null) + // _luaEnvironment = IoC.GetInstance(); + + // return _luaEnvironment; + //} + public LuaScriptInterface(string initInterfaceName) { interfaceName = initInterfaceName; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj index 811a8e14b..18c743537 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/NeoServer.Scripts.LuaJIT.csproj @@ -6,6 +6,12 @@ enable + + + Data\LuaJit\%(RecursiveDir)%(FileName) + + + @@ -15,54 +21,13 @@ + Always - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs index 5c0286046..a1c2137bf 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs @@ -1,11 +1,6 @@ -using NeoServer.Game.Common.Contracts.Items; -using System; -using System.Collections.Generic; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.Items; using System.ComponentModel; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; namespace NeoServer.Scripts.LuaJIT; @@ -19,18 +14,14 @@ public class ScriptEnvironment private LuaScriptInterface luaScriptInterface; // local item map - //private Dictionary> localMap; - //private uint lastUID; + private Dictionary localMap = new Dictionary(); + private uint lastUID; // temporary item list - //private Dictionary> tempItems; + private Dictionary tempItems = new Dictionary(); // for npc scripts - //private Npc curNpc = nullptr; - - // result map - //private uint lastResultId; - //private Dictionary tempResults; + private INpc curNpc = null; public ScriptEnvironment() { @@ -48,23 +39,15 @@ public void ResetEnv() callbackId = 0; timerEvent = false; luaScriptInterface = null; - //localMap.Clear(); - //tempResults.Clear(); - - //var pair = tempItems.equal_range(this); - //var it = pair.first; - //while (it != pair.second) - //{ - // std.shared_ptr item = it->second; - // it = tempItems.erase(it); - //} + localMap.Clear(); + tempItems.Clear(); } public void SetScriptId(int newScriptId, LuaScriptInterface newScriptInterface) { scriptId = newScriptId; luaScriptInterface = newScriptInterface; - } + } public bool SetCallbackId(int newCallbackId, LuaScriptInterface scriptInterface) { @@ -83,9 +66,10 @@ public bool SetCallbackId(int newCallbackId, LuaScriptInterface scriptInterface) return true; } - public int GetScriptId() { - return scriptId; - } + public int GetScriptId() + { + return scriptId; + } public LuaScriptInterface GetScriptInterface() { @@ -105,152 +89,122 @@ public void GetEventInfo(out int retScriptId, out LuaScriptInterface retScriptIn retTimerEvent = timerEvent; } - //public uint AddThing(std.shared_ptr thing) - //{ - // if (thing == null || thing.isRemoved()) - // { - // return 0; - // } - - // std.shared_ptr creature = thing.getCreature(); - // if (creature != null) - // { - // return creature.getID(); - // } - - // std.shared_ptr item = thing.getItem(); - // if (item != null && item.hasAttribute(ItemAttribute_t.UNIQUEID)) - // { - // return item.getAttribute(ItemAttribute_t.UNIQUEID); - // } - - // foreach (var it in localMap) - // { - // if (it.Value == item) - // { - // return it.Key; - // } - // } - - // localMap[++lastUID] = item; - // return lastUID; - //} - - //public void InsertItem(uint uid, std.shared_ptr item) - //{ - // var result = localMap.TryAdd(uid, item); - // if (!result) - // { - // g_logger().error("Thing uid already taken: {}", uid); - // } - //} - - //public std.shared_ptr GetThingByUID(uint uid) - //{ - // if (uid >= 0x10000000) - // { - // return g_game().getCreatureByID(uid); - // } - - // if (uid <= std.numeric_limits.max()) - // { - // std.shared_ptr item = g_game().getUniqueItem(static_cast(uid)); - // if (item != null && !item.isRemoved()) - // { - // return item; - // } - // return null; - // } - - // if (localMap.TryGetValue(uid, out std.shared_ptr item)) - // { - // if (!item.isRemoved()) - // { - // return item; - // } - // } - // return null; - //} - - //public IItem GetItemByUID(uint uid) - //{ - // std.shared_ptr thing = getThingByUID(uid); - // if (thing == null) - // { - // return null; - // } - // return thing.getItem(); - //} - - //public std.shared_ptr GetContainerByUID(uint uid) - //{ - // std.shared_ptr item = getItemByUID(uid); - // if (item == null) - // { - // return null; - // } - // return item.getContainer(); - //} - - //public void RemoveItemByUID(uint uid) - //{ - // if (uid <= std.numeric_limits.max()) - // { - // g_game().removeUniqueItem(static_cast(uid)); - // return; - // } - - // localMap.TryRemove(uid, out var _); - //} - - //public void AddTempItem(std.shared_ptr item) - //{ - // tempItems.Add(this, item); - //} - - //public void RemoveTempItem(std.shared_ptr item) - //{ - // foreach (var it in tempItems) - // { - // if (it.Value == item) - // { - // tempItems.Remove(it.Key); - // break; - // } - // } - //} - - //public uint AddResult(DBResult_ptr res) - //{ - // tempResults[++lastResultId] = res; - // return lastResultId; - //} - - //public bool RemoveResult(uint id) - //{ - // if (tempResults.TryGetValue(id, out var _)) - // { - // tempResults.Remove(id); - // return true; - // } - // return false; - //} - - //public DBResult_ptr GetResultByID(uint id) - //{ - // if (tempResults.TryGetValue(id, out var result)) - // { - // return result; - // } - // return null; - //} - - // public void SetNpc(Npc npc) - // { - // curNpc = npc; - // } - - // public Npc GetNpc() { - // return curNpc; - //} + public uint AddThing(IThing thing) + { + if (thing == null) + { + return 0; + } + + if (thing is ICreature creature) + { + return creature.CreatureId; + } + + if (thing is IItem item && item.Metadata.Attributes.HasAttribute(Game.Common.Item.ItemAttribute.UniqueId)) + { + return item.UniqueId; + } + + foreach (var it in localMap) + { + if (it.Value == thing) + { + return it.Key; + } + } + + localMap[++lastUID] = thing; + return lastUID; + } + + public void InsertItem(uint uid, IItem item) + { + var result = localMap.TryAdd(uid, item); + if (!result) + { + //g_logger().error("Thing uid already taken: {}", uid); + } + } + + public IThing GetThingByUID(uint uid) + { + //if (uid >= 0x10000000) + //{ + // return g_game().getCreatureByID(uid); + //} + + if (uid <= ushort.MaxValue) + { + //std.shared_ptr item = g_game().getUniqueItem(static_cast(uid)); + //if (item != null && !item.isRemoved()) + //{ + // return item; + //} + //return null; + } + + if (localMap.TryGetValue(uid, out IThing thing)) + return thing; + + return null; + } + + public IItem GetItemByUID(uint uid) + { + var thing = GetThingByUID(uid); + + if (thing != null && thing is IItem item) + return item; + + return null; + } + + public IContainer GetContainerByUID(uint uid) + { + var thing = GetThingByUID(uid); + + if (thing != null && thing is IContainer container) + return container; + + return null; + } + + public void RemoveItemByUID(uint uid) + { + if (uid <= ushort.MaxValue) + { + //g_game().removeUniqueItem(static_cast(uid)); + return; + } + + localMap.Remove(uid, out var _); + } + + public void AddTempItem(IItem item) + { + tempItems.Add(this, item); + } + + public void RemoveTempItem(IItem item) + { + foreach (var it in tempItems) + { + if (it.Value == item) + { + tempItems.Remove(it.Key); + break; + } + } + } + + public void SetNpc(INpc npc) + { + curNpc = npc; + } + + public INpc GetNpc() + { + return curNpc; + } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua index 5c228b918..8debbb103 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/config.lua @@ -1,37 +1,7 @@ -print('print from config.lua') - -coreDirectory = "Data/LuaJit" +coreDirectory = "Data/LuaJit" showScriptsLogInConsole = true -- NOTE: true will allow the /reload command to be used -- NOTE: Using this script might cause unwanted changes -- This script forces a reload in the entire server, this means that everything that is stored in memory might stop to work properly and/or completely, this script should be used in test environments only -allowReload = true - --- Save interval per time --- NOTE: toggleSaveInterval: true = enable the save interval, false = disable the save interval --- NOTE: saveIntervalType: "minute", "second" or "hour" --- NOTE: toggleSaveIntervalCleanMap: true = enable the clean map, false = disable the clean map --- NOTE: saveIntervalTime: time based on what was set in "saveIntervalType" -toggleSaveInterval = true -saveIntervalType = "minute" -toggleSaveIntervalCleanMap = true -saveIntervalTime = 1 - --- Connection Config --- NOTE: maxPlayers set to 0 means no limit --- NOTE: MaxPacketsPerSeconds if you change you will be subject to bugs by WPE, keep the default value of 25 -ip = "127.0.0.1" -bindOnlyGlobalAddress = false -loginProtocolPort = 7171 -gameProtocolPort = 7172 -statusProtocolPort = 7171 -maxPlayers = 0 -serverName = "OpenCoreMMO" -serverMotd = "Welcome to theOpenCoreMMO!" -onePlayerOnlinePerAccount = true -statusTimeout = 5 * 1000 -replaceKickOnLogin = true -maxPacketsPerSecond = 25 -maxItem = 2000 -maxContainer = 100 \ No newline at end of file +allowReload = true \ No newline at end of file diff --git a/src/Standalone/IoC/Modules/ConfigurationInjection.cs b/src/Standalone/IoC/Modules/ConfigurationInjection.cs index d591438e5..491378e8e 100644 --- a/src/Standalone/IoC/Modules/ConfigurationInjection.cs +++ b/src/Standalone/IoC/Modules/ConfigurationInjection.cs @@ -29,7 +29,7 @@ public static IServiceCollection AddConfigurations(this IServiceCollection build IConfigurationRoot configuration) { ServerConfiguration serverConfiguration = - new(0, null, null, null, string.Empty, string.Empty, string.Empty, 7171, 7172,new SaveConfiguration(3600)); + new(0, null, null, null, string.Empty, string.Empty, string.Empty, 7171, 7172,new SaveConfiguration(3600), string.Empty); GameConfiguration gameConfiguration = new(); LogConfiguration logConfiguration = new(null); @@ -63,6 +63,7 @@ private static void LoadEnvironmentVariables(ref ServerConfiguration serverConfi serverConfiguration.Extensions, string.IsNullOrEmpty(serverLoginPort) ? serverConfiguration.ServerLoginPort : int.Parse(serverLoginPort), string.IsNullOrEmpty(serverGamePort) ? serverConfiguration.ServerGamePort : int.Parse(serverGamePort), - serverConfiguration.Save); + serverConfiguration.Save, + serverConfiguration.DataLuaJit); } } \ No newline at end of file diff --git a/src/Standalone/Program.cs b/src/Standalone/Program.cs index d0fee7cbe..77b298d45 100644 --- a/src/Standalone/Program.cs +++ b/src/Standalone/Program.cs @@ -108,6 +108,7 @@ public static async Task Main() container.Resolve>().ToList().ForEach(x => x.Run()); container.Resolve().Register(); + container.Resolve().Start(); StartListening(container, cancellationToken); diff --git a/src/Standalone/appsettings.Local.json b/src/Standalone/appsettings.Local.json index 93f1f993d..6e888c52f 100644 --- a/src/Standalone/appsettings.Local.json +++ b/src/Standalone/appsettings.Local.json @@ -1,6 +1,7 @@ { "server": { - "data": "../../data" + "data": "../../data", + "DataLuaJit": "../../../../../data/LuaJit" }, "database": { "connections": { diff --git a/src/Standalone/appsettings.json b/src/Standalone/appsettings.json index 649752b51..e0e38f850 100644 --- a/src/Standalone/appsettings.json +++ b/src/Standalone/appsettings.json @@ -12,7 +12,8 @@ "save": { "players": 60 //seconds - } + }, + "DataLuaJit": "data/LuaJit" }, "game": { "experienceRate": 20000, From 65786ada32c95076342aafe17d015df4b6a3d5e6 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Tue, 24 Dec 2024 14:12:32 -0300 Subject: [PATCH 06/16] feat: some refacts, removed unused/commented code, and some lua functions methods implementations, some implementstions on NeoServer to provide methods and properties to LuaJit and moved EffectService from NeoServer.Extensions project to NeoServer.Server project. --- data/LuaJit/core.lua | 2 +- data/LuaJit/libs/functions/tile.lua | 23 ++ data/LuaJit/scripts/actions/Tools/rope.lua | 9 + data/LuaJit/scripts/actions/Tools/shovel.lua | 17 +- .../scripts/talkactions/player/test.lua | 14 +- data/extensions/Items/Lever.cs | 2 +- data/extensions/NeoServer.Extensions.csproj | 38 +- data/extensions/Players/Loaders/GodLoader.cs | 1 - .../extensions/Players/Loaders/TutorLoader.cs | 1 - .../Commands/TeleportToTempleCommand.cs | 2 +- data/extensions/Startup.cs | 2 +- .../Player/UseItem/PlayerUseItemOnCommand.cs | 2 +- .../NeoServer.Server/NeoServer.Server.csproj | 1 + .../Services/EffectService.cs | 14 +- .../DataStore.cs | 1 + .../Parsers/ItemEntityParser.cs | 2 +- .../Repositories/Player/InventoryManager.cs | 2 +- .../Functions/Libs/LeverFunctions.cs | 2 +- .../NeoServer.Scripts.LuaJIT/Action.cs | 2 + .../NeoServer.Scripts.LuaJIT/Actions.cs | 6 + .../NeoServer.Scripts.LuaJIT/ConfigManager.cs | 336 +----------------- .../{ => Enums}/ConfigDefinitions.cs | 2 +- .../{ => Enums}/ItemsDefinitions.cs | 4 +- .../{ => Enums}/LuaDefinitions.cs | 12 +- .../Extensions/ReturnMessageExtensions.cs | 4 +- .../Functions/ActionFunctions.cs | 7 +- .../Functions/ConfigFunctions.cs | 17 +- .../Functions/CreatureFunctions.cs | 24 +- .../Functions/EnumFunctions.cs | 6 +- .../Functions/GameFunctions.cs | 173 ++++++++- .../Functions/GlobalFunctions.cs | 5 +- .../Functions/ItemFunctions.cs | 103 +++++- .../Functions/ItemTypeFunctions.cs | 10 +- .../Functions/LoggerFunctions.cs | 8 +- .../Functions/PlayerFunctions.cs | 39 +- .../Functions/PositionFunctions.cs | 55 ++- .../Functions/TalkActionFunctions.cs | 4 +- .../Functions/TileFunctions.cs | 60 +++- .../Interfaces/IActions.cs | 1 + .../Interfaces/IConfigManager.cs | 1 + .../Interfaces/ILuaEnvironment.cs | 20 +- .../Interfaces/ITalkActions.cs | 1 + .../LuaEnvironment.cs | 117 +----- .../LuaFunctionsLoader.cs | 1 + .../LuaScriptInterface.cs | 2 +- .../NeoServer.Scripts.LuaJIT/Script.cs | 4 - .../ScriptEnvironment.cs | 2 + .../NeoServer.Scripts.LuaJIT/Scripts.cs | 10 +- .../NeoServer.Scripts.LuaJIT/TalkAction.cs | 1 - .../NeoServer.Scripts.LuaJIT/TalkActions.cs | 1 + .../UtilsDefinitions.cs | 96 +---- .../Contracts/DataStores/IItemTypeStore.cs | 3 + .../Contracts/Items/IItem.cs | 14 +- .../Contracts/Items/IItemFactory.cs | 3 + .../Contracts/Items/IItemType.cs | 9 +- .../Npcs/Shop/ShopperNpc.cs | 4 +- .../Calculations/InventoryMoneyCalculation.cs | 2 +- .../Player/Inventory/InventoryMap.cs | 6 +- .../Player/Inventory/Rules/AddToSlotRule.cs | 2 +- .../NeoServer.Game.Creatures/Player/Player.cs | 2 +- .../Services/DealTransaction.cs | 2 +- .../Factories/ItemFactory.cs | 12 +- .../Helpers/OverridenFunctionQuery.cs | 4 +- .../NeoServer.Game.Items/ItemType.cs | 24 +- .../Container/Builders/ContainerMapBuilder.cs | 4 +- .../Container/Queries/FindItemByTypeQuery.cs | 2 +- .../Rules/CanAddItemToContainerRule.cs | 2 +- .../UsableItems/FloorChangerUsableItem.cs | 2 +- .../Items/UsableItems/UsableOnItem.cs | 4 +- .../Items/Weapons/MeleeWeapon.cs | 4 +- .../NeoServer.Loaders/Items/ItemTypeLoader.cs | 2 +- .../NeoServer.Loaders/Npcs/NpcLoader.cs | 2 +- .../Outgoing/Npc/SaleItemListPacket.cs | 4 +- .../Items/EquipmentTests.cs | 4 +- .../Server/ItemTypeStoreTestBuilder.cs | 2 +- 75 files changed, 622 insertions(+), 766 deletions(-) rename {data/extensions => src/ApplicationServer/NeoServer.Server}/Services/EffectService.cs (57%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Enums}/ConfigDefinitions.cs (99%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Enums}/ItemsDefinitions.cs (99%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{ => Enums}/LuaDefinitions.cs (96%) diff --git a/data/LuaJit/core.lua b/data/LuaJit/core.lua index 433df3313..6ae0222b0 100644 --- a/data/LuaJit/core.lua +++ b/data/LuaJit/core.lua @@ -1,5 +1,5 @@ logger.debug('Starting lua.') -logger.debug(os.getenv('LOCAL_LUA_DEBUGGER_VSCODE')) +logger.debug(tostring(os.getenv('LOCAL_LUA_DEBUGGER_VSCODE'))) if os.getenv('LOCAL_LUA_DEBUGGER_VSCODE') == '1' then require('lldebugger').start() logger.debug('Started LUA debugger.') diff --git a/data/LuaJit/libs/functions/tile.lua b/data/LuaJit/libs/functions/tile.lua index 7d7c87515..4e567980b 100644 --- a/data/LuaJit/libs/functions/tile.lua +++ b/data/LuaJit/libs/functions/tile.lua @@ -14,6 +14,29 @@ function Tile.isContainer(self) return false end +function Tile.relocateTo(self, toPosition) + if self:getPosition() == toPosition or not Tile(toPosition) then + return false + end + + for i = self:getThingCount() - 1, 0, -1 do + local thing = self:getThing(i) + if thing then + if thing:isItem() then + if thing:getFluidType() ~= 0 then + thing:remove() + elseif ItemType(thing:getId()):isMovable() then + thing:moveTo(toPosition) + end + elseif thing:isCreature() then + thing:teleportTo(toPosition) + end + end + end + + return true +end + function Tile.isWalkable(self) local ground = self:getGround() if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then diff --git a/data/LuaJit/scripts/actions/Tools/rope.lua b/data/LuaJit/scripts/actions/Tools/rope.lua index e7099bec3..39027ffd8 100644 --- a/data/LuaJit/scripts/actions/Tools/rope.lua +++ b/data/LuaJit/scripts/actions/Tools/rope.lua @@ -1,5 +1,14 @@ local rope = Action() +local ropeSpots = { 384, 418, 8278, 8592 } + +local holeId = { -- usable rope holes, for rope spots see global.lua + 294, 369, 370, 383, 392, 408, 409, 410, 427, 428, 429, 430, 462, 469, 470, 482, + 484, 485, 489, 924, 1369, 3135, 3136, 4835, 4837, 7933, 7938, 8170, 8249, 8250, + 8251, 8252, 8254, 8255, 8256, 8276, 8277, 8279, 8281, 8284, 8285, 8286, 8323, + 8567, 8585, 8595, 8596, 8972, 9606, 9625 +} + function rope.onUse(player, item, fromPosition, target, toPosition, isHotkey) local tile = Tile(toPosition) if not tile then diff --git a/data/LuaJit/scripts/actions/Tools/shovel.lua b/data/LuaJit/scripts/actions/Tools/shovel.lua index df1bff005..27ca951cc 100644 --- a/data/LuaJit/scripts/actions/Tools/shovel.lua +++ b/data/LuaJit/scripts/actions/Tools/shovel.lua @@ -1,5 +1,16 @@ local shovel = Action() +local sandIds = {231, 9059} -- desert sand +local holes = {468, 481, 483, 7932} -- holes opened by shovel + +local actionIds = { + sandHole = 100, -- hidden sand hole + pickHole = 105, -- hidden mud hole + levelDoor = 1000, -- level door + citizenship = 30020, -- citizenship teleport + citizenshipLast = 30050, -- citizenship teleport last +} + function shovel.onUse(player, item, fromPosition, target, toPosition, isHotkey) local tile = Tile(toPosition) if not tile then @@ -18,11 +29,11 @@ function shovel.onUse(player, item, fromPosition, target, toPosition, isHotkey) toPosition.z = toPosition.z + 1 tile:relocateTo(toPosition) - player:addAchievementProgress("The Undertaker", 500) + --player:addAchievementProgress("The Undertaker", 500) elseif target.itemid == 7932 then -- large hole target:transform(7933) target:decay() - player:addAchievementProgress("The Undertaker", 500) + --player:addAchievementProgress("The Undertaker", 500) elseif table.contains(sandIds, groundId) then local randomValue = math.random(1, 100) if target.actionid == actionIds.sandHole and randomValue <= 20 then @@ -30,7 +41,7 @@ function shovel.onUse(player, item, fromPosition, target, toPosition, isHotkey) ground:decay() elseif randomValue == 1 then Game.createItem(2159, 1, toPosition) - player:addAchievementProgress("Gold Digger", 100) + --player:addAchievementProgress("Gold Digger", 100) elseif randomValue > 95 then Game.createMonster("Scarab", toPosition) end diff --git a/data/LuaJit/scripts/talkactions/player/test.lua b/data/LuaJit/scripts/talkactions/player/test.lua index 3ae0a9a87..21977bd7f 100644 --- a/data/LuaJit/scripts/talkactions/player/test.lua +++ b/data/LuaJit/scripts/talkactions/player/test.lua @@ -3,14 +3,12 @@ function talkAction.onSay(player, words, param) logger.info('executing talkAction from lua: '.. words .. ' ' .. param) - logger.info(player) logger.info(player:getName()) logger.info(player:getId()) local creature = Creature("GOD") if creature then - logger.info(creature) logger.info(creature:getName()) logger.info(creature:getId()) @@ -21,10 +19,18 @@ function talkAction.onSay(player, words, param) local showInConsole = configManager.getBoolean(configKeys.SCRIPTS_CONSOLE_LOGS); - logger.info(showInConsole) + logger.info(tostring(showInConsole)) - player:sendTextMessage(0, param, 0); + player:sendTextMessage(0, param, 0) + local position = player:getPosition() + + logger.info(position:toString()) + + position:sendMagicEffect(CONST_ME_POFF) + + Game.createItem(2159, 1, position) + logger.info('end') return true diff --git a/data/extensions/Items/Lever.cs b/data/extensions/Items/Lever.cs index 434a840f6..25bd8ac4a 100644 --- a/data/extensions/Items/Lever.cs +++ b/data/extensions/Items/Lever.cs @@ -27,7 +27,7 @@ public void SwitchLever() { if (Map.Instance[Location] is not DynamicTile dynamicTile) return; - var newLeverId = (ushort)(Metadata.TypeId == 1946 ? 1945 : 1946); + var newLeverId = (ushort)(Metadata.ServerId == 1946 ? 1945 : 1946); var newLever = ItemFactory.Instance.Create(newLeverId, Location, Metadata.Attributes.ToDictionary()); diff --git a/data/extensions/NeoServer.Extensions.csproj b/data/extensions/NeoServer.Extensions.csproj index c9cf81a34..3c0a615c8 100644 --- a/data/extensions/NeoServer.Extensions.csproj +++ b/data/extensions/NeoServer.Extensions.csproj @@ -6,29 +6,29 @@ - - + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - + diff --git a/data/extensions/Players/Loaders/GodLoader.cs b/data/extensions/Players/Loaders/GodLoader.cs index b562b4200..a7eb7c238 100644 --- a/data/extensions/Players/Loaders/GodLoader.cs +++ b/data/extensions/Players/Loaders/GodLoader.cs @@ -62,7 +62,6 @@ public override IPlayer Load(PlayerEntity playerEntity) GuildLevel = (ushort)(playerEntity.GuildMember?.RankId ?? 0) }; - //SetCurrentTile(newPlayer); var currentTile = GetCurrentTile(newPlayer.Location); newPlayer.SetCurrentTile(currentTile); diff --git a/data/extensions/Players/Loaders/TutorLoader.cs b/data/extensions/Players/Loaders/TutorLoader.cs index 1ca18b931..675955021 100644 --- a/data/extensions/Players/Loaders/TutorLoader.cs +++ b/data/extensions/Players/Loaders/TutorLoader.cs @@ -64,7 +64,6 @@ public override IPlayer Load(PlayerEntity playerEntity) newPlayer.AddInventory(ConvertToInventory(newPlayer, playerEntity)); - //SetCurrentTile(newPlayer); var currentTile = GetCurrentTile(newPlayer.Location); newPlayer.SetCurrentTile(currentTile); diff --git a/data/extensions/Spells/Commands/TeleportToTempleCommand.cs b/data/extensions/Spells/Commands/TeleportToTempleCommand.cs index fa7b5bc1a..51a03c327 100644 --- a/data/extensions/Spells/Commands/TeleportToTempleCommand.cs +++ b/data/extensions/Spells/Commands/TeleportToTempleCommand.cs @@ -1,4 +1,4 @@ -using NeoServer.Extensions.Services; +using NeoServer.Server.Services; using NeoServer.Game.Combat.Spells; using NeoServer.Game.Common; using NeoServer.Game.Common.Contracts.Creatures; diff --git a/data/extensions/Startup.cs b/data/extensions/Startup.cs index 4913024bb..719b60bde 100644 --- a/data/extensions/Startup.cs +++ b/data/extensions/Startup.cs @@ -1,4 +1,4 @@ -using NeoServer.Extensions.Services; +using NeoServer.Server.Services; using NeoServer.Server.Common.Contracts; using NLua; diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs index e0e35b804..fedd0db37 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs @@ -82,7 +82,7 @@ public void Execute(IPlayer player, UseItemOnPacket useItemPacket) if (thingToUse is not IUsableOn itemToUse) return; - if (_luaGameManager.PlayerUseItemEx(player, player.Location, useItemPacket.Location, useItemPacket.ToStackPosition, itemToUse, useItemPacket.Location.IsHotkey, onItem != null ? onTile : onTile)) + if (_luaGameManager.PlayerUseItemEx(player, player.Location, useItemPacket.ToLocation, useItemPacket.ToStackPosition, itemToUse, useItemPacket.Location.IsHotkey, onItem != null ? onTile : onTile)) return; Action action = onTile is not null diff --git a/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj b/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj index 870523d97..688630e37 100644 --- a/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj +++ b/src/ApplicationServer/NeoServer.Server/NeoServer.Server.csproj @@ -21,6 +21,7 @@ + diff --git a/data/extensions/Services/EffectService.cs b/src/ApplicationServer/NeoServer.Server/Services/EffectService.cs similarity index 57% rename from data/extensions/Services/EffectService.cs rename to src/ApplicationServer/NeoServer.Server/Services/EffectService.cs index 932d78ea1..b1192b49b 100644 --- a/data/extensions/Services/EffectService.cs +++ b/src/ApplicationServer/NeoServer.Server/Services/EffectService.cs @@ -1,20 +1,26 @@ -using NeoServer.Game.Common.Contracts.World; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.World; using NeoServer.Game.Common.Creatures; using NeoServer.Game.Common.Location.Structs; using NeoServer.Networking.Packets.Outgoing.Effect; using NeoServer.Server.Common.Contracts; using NeoServer.Server.Helpers; +using System.Collections.Generic; +using System.Linq; -namespace NeoServer.Extensions.Services; +namespace NeoServer.Server.Services; public static class EffectService { - public static void Send(Location location, EffectT effect) + public static void Send(Location location, EffectT effect, IEnumerable spectators = null) { var map = IoC.GetInstance(); var game = IoC.GetInstance(); - foreach (var spectator in map.GetPlayersAtPositionZone(location)) + if (spectators == null || !spectators.Any()) + spectators = map.GetPlayersAtPositionZone(location); + + foreach (var spectator in spectators) { if (!game.CreatureManager.GetPlayerConnection(spectator.CreatureId, out var connection)) continue; diff --git a/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs b/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs index 0f0d2206c..1324c3fee 100644 --- a/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs +++ b/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using NeoServer.Game.Common.Contracts.DataStores; namespace NeoServer.Data.InMemory.DataStores; diff --git a/src/Database/NeoServer.Data/Parsers/ItemEntityParser.cs b/src/Database/NeoServer.Data/Parsers/ItemEntityParser.cs index 92336b1d7..e77493c04 100644 --- a/src/Database/NeoServer.Data/Parsers/ItemEntityParser.cs +++ b/src/Database/NeoServer.Data/Parsers/ItemEntityParser.cs @@ -15,7 +15,7 @@ public static class ItemEntityParser { var itemModel = new T { - ServerId = (short)item.Metadata.TypeId, + ServerId = (short)item.Metadata.ServerId, Amount = item is ICumulative cumulative ? cumulative.Amount : (short)1, DecayTo = item.Decay?.DecaysTo, DecayDuration = item.Decay?.Duration, diff --git a/src/Database/NeoServer.Data/Repositories/Player/InventoryManager.cs b/src/Database/NeoServer.Data/Repositories/Player/InventoryManager.cs index de57fafe2..3508b1603 100644 --- a/src/Database/NeoServer.Data/Repositories/Player/InventoryManager.cs +++ b/src/Database/NeoServer.Data/Repositories/Player/InventoryManager.cs @@ -39,7 +39,7 @@ public static async Task SavePlayerInventory(IPlayer player, NeoContext neoConte if (playerInventory.TryGetValue((int)slot, out var playerInventoryItemEntity)) { - playerInventoryItemEntity.ServerId = item?.Metadata?.TypeId ?? 0; + playerInventoryItemEntity.ServerId = item?.Metadata?.ServerId ?? 0; playerInventoryItemEntity.Amount = item?.Amount ?? 0; playerInventoryItemEntity.PlayerId = (int)player.Id; playerInventoryItemEntity.SlotId = (int)slot; diff --git a/src/Extensions/NeoServer.Scripts.Lua/Functions/Libs/LeverFunctions.cs b/src/Extensions/NeoServer.Scripts.Lua/Functions/Libs/LeverFunctions.cs index 4910e3a4d..9fef595fc 100644 --- a/src/Extensions/NeoServer.Scripts.Lua/Functions/Libs/LeverFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.Lua/Functions/Libs/LeverFunctions.cs @@ -22,7 +22,7 @@ private static void SwitchLever(IItem item) if (map[item.Location] is not DynamicTile dynamicTile) return; - var newLeverId = (ushort)(item.Metadata.TypeId == 1946 ? 1945 : 1946); + var newLeverId = (ushort)(item.Metadata.ServerId == 1946 ? 1945 : 1946); var newLever = ItemFactory.Instance.Create(newLeverId, item.Location, item.Metadata.Attributes.ToDictionary()); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs index b89ee7fa9..7394e9397 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs @@ -2,6 +2,7 @@ using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Contracts.World; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Server.Helpers; using Serilog; @@ -133,6 +134,7 @@ public void SetPositions(Location pos) public virtual ReturnValueType CanExecuteAction(IPlayer player, Location toPos) { + //todo: implement this? //if (!allowFarUse) //{ // return g_actions().canUse(player, toPos); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs index 0d99d5e58..572d43f28 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Actions.cs @@ -2,6 +2,7 @@ using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Item; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Enums; using Serilog; using System.Text; @@ -174,6 +175,7 @@ public bool RegisterLuaEvent(Action action) public bool UseItem(IPlayer player, Location pos, byte index, IItem item, bool isHotkey) { + //todo: implement this? //if (item == null) // throw new ArgumentNullException(nameof(item)); @@ -221,6 +223,7 @@ public bool UseItem(IPlayer player, Location pos, byte index, IItem item, bool i public bool UseItemEx(IPlayer player, Location fromPos, Location toPos, byte toStackPos, IItem item, bool isHotkey, ICreature creature = null) { + //todo: implement this? //var it = Item.items[item.ID]; //if (it.isRune() || it.type == ItemType.Potion) //{ @@ -315,6 +318,7 @@ public ReturnValueType CanUse(IPlayer player, Location pos, IItem item) public ReturnValueType CanUseFar(ICreature creature, Location toPos, bool checkLineOfSight, bool checkFloor) { + //todo: implement this? //if (toPos.x == 0xFFFF) //{ // return ReturnValueType.RETURNVALUE_NOERROR; @@ -357,6 +361,7 @@ public Action GetAction(IItem item) public ReturnValueType InternalUseItem(IPlayer player, Location pos, byte index, IItem item, bool isHotkey) { + //todo: implement this? //if (std.shared_ptr < Door > door = item.GetDoor()) //{ // if (!door.CanUse(player)) @@ -540,6 +545,7 @@ public ReturnValueType InternalUseItem(IPlayer player, Location pos, byte index, public void ShowUseHotkeyMessage(IPlayer player, IItem item, uint count) { + //todo: implement this? StringBuilder ss = new StringBuilder(); //const ItemType &it = Item.items[item.ID]; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs index 0470a80fd..7988f4f35 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigManager.cs @@ -1,17 +1,11 @@ using LuaNET; +using NeoServer.Scripts.LuaJIT.Enums; using Serilog; namespace NeoServer.Scripts.LuaJIT; public class ConfigManager : IConfigManager { - #region Instance - - private static ConfigManager _instance; - public static ConfigManager GetInstance() => _instance == null ? _instance = new ConfigManager() : _instance; - - #endregion - #region Members private readonly string[] stringConfig = new string[(int)StringConfigType.LAST_STRING_CONFIG]; @@ -34,8 +28,6 @@ public class ConfigManager : IConfigManager public ConfigManager(ILogger logger) { - _instance = this; - _logger = logger.ForContext(); } @@ -57,335 +49,32 @@ public bool Load(string file) return false; } - //#if !DEBUG_LOG - // g_logger().setLevel(getGlobalString(L, "logLevel", "info")); - //#endif - // Parse config // Info that must be loaded one time (unless we reset the modules involved) if (!loaded) { - //booleanConfig[(int)BooleanConfig.BIND_ONLY_GLOBAL_ADDRESS] = GetGlobalBoolean(L, "bindOnlyGlobalAddress", false); - //bool[OPTIMIZE_DATABASE] = getGlobalBoolean(L, "startupDatabaseOptimization", true); - //bool[TOGGLE_MAP_CUSTOM] = getGlobalBoolean(L, "toggleMapCustom", true); - //bool[TOGGLE_MAINTAIN_MODE] = getGlobalBoolean(L, "toggleMaintainMode", false); - //string[MAINTAIN_MODE_MESSAGE] = getGlobalString(L, "maintainModeMessage", ""); - - //string[IP] = getGlobalString(L, "ip", "127.0.0.1"); - //string[MAP_NAME] = getGlobalString(L, "mapName", "canary"); - //string[MAP_DOWNLOAD_URL] = getGlobalString(L, "mapDownloadUrl", ""); - //string[MAP_AUTHOR] = getGlobalString(L, "mapAuthor", "Eduardo Dantas"); - - //string[MAP_CUSTOM_NAME] = getGlobalString(L, "mapCustomName", ""); - //string[MAP_CUSTOM_AUTHOR] = getGlobalString(L, "mapCustomAuthor", "OTServBR"); - - //string[HOUSE_RENT_PERIOD] = getGlobalString(L, "houseRentPeriod", "never"); - //float[HOUSE_PRICE_RENT_MULTIPLIER] = getGlobalFloat(L, "housePriceRentMultiplier", 1.0); - //float[HOUSE_RENT_RATE] = getGlobalFloat(L, "houseRentRate", 1.0); - //string[MYSQL_HOST] = getGlobalString(L, "mysqlHost", "127.0.0.1"); - //string[MYSQL_USER] = getGlobalString(L, "mysqlUser", "root"); - //string[MYSQL_PASS] = getGlobalString(L, "mysqlPass", ""); - //string[MYSQL_DB] = getGlobalString(L, "mysqlDatabase", "canary"); - //string[MYSQL_SOCK] = getGlobalString(L, "mysqlSock", ""); - - //string[AUTH_TYPE] = getGlobalString(L, "authType", "password"); - //bool[RESET_SESSIONS_ON_STARTUP] = getGlobalBoolean(L, "resetSessionsOnStartup", false); - - //integer[SQL_PORT] = getGlobalNumber(L, "mysqlPort", 3306); - integerConfig[(int)IntegerConfigType.GAME_PORT] = GetGlobalNumber(L, "gameProtocolPort", 7172); integerConfig[(int)IntegerConfigType.LOGIN_PORT] = GetGlobalNumber(L, "loginProtocolPort", 7171); integerConfig[(int)IntegerConfigType.STATUS_PORT] = GetGlobalNumber(L, "statusProtocolPort", 7171); - - //integer[MARKET_OFFER_DURATION] = getGlobalNumber(L, "marketOfferDuration", 30 * 24 * 60 * 60); - - //integer[FREE_DEPOT_LIMIT] = getGlobalNumber(L, "freeDepotLimit", 2000); - //integer[PREMIUM_DEPOT_LIMIT] = getGlobalNumber(L, "premiumDepotLimit", 8000); - //integer[DEPOT_BOXES] = getGlobalNumber(L, "depotBoxes", 20); - //integer[STASH_ITEMS] = getGlobalNumber(L, "stashItemCount", 5000); - - //bool[OLD_PROTOCOL] = getGlobalBoolean(L, "allowOldProtocol", true); } - //bool[ALLOW_CHANGEOUTFIT] = getGlobalBoolean(L, "allowChangeOutfit", true); - //bool[ONE_PLAYER_ON_ACCOUNT] = getGlobalBoolean(L, "onePlayerOnlinePerAccount", true); - //bool[AIMBOT_HOTKEY_ENABLED] = getGlobalBoolean(L, "hotkeyAimbotEnabled", true); - //bool[REMOVE_RUNE_CHARGES] = getGlobalBoolean(L, "removeChargesFromRunes", true); - //bool[EXPERIENCE_FROM_PLAYERS] = getGlobalBoolean(L, "experienceByKillingPlayers", false); - //bool[FREE_PREMIUM] = getGlobalBoolean(L, "freePremium", false); - //bool[REPLACE_KICK_ON_LOGIN] = getGlobalBoolean(L, "replaceKickOnLogin", true); - //bool[MARKET_PREMIUM] = getGlobalBoolean(L, "premiumToCreateMarketOffer", true); - //bool[EMOTE_SPELLS] = getGlobalBoolean(L, "emoteSpells", false); - //bool[STAMINA_SYSTEM] = getGlobalBoolean(L, "staminaSystem", true); - //bool[WARN_UNSAFE_SCRIPTS] = getGlobalBoolean(L, "warnUnsafeScripts", true); - //bool[CONVERT_UNSAFE_SCRIPTS] = getGlobalBoolean(L, "convertUnsafeScripts", true); - //bool[CLASSIC_ATTACK_SPEED] = getGlobalBoolean(L, "classicAttackSpeed", false); - //bool[TOGGLE_ATTACK_SPEED_ONFIST] = getGlobalBoolean(L, "toggleAttackSpeedOnFist", false); - //integer[MULTIPLIER_ATTACKONFIST] = getGlobalNumber(L, "multiplierSpeedOnFist", 5); - //integer[MAX_SPEED_ATTACKONFIST] = getGlobalNumber(L, "maxSpeedOnFist", 500); booleanConfig[(int)BooleanConfigType.SCRIPTS_CONSOLE_LOGS] = GetGlobalBoolean(L, "showScriptsLogInConsole", true); - //bool[STASH_MOVING] = getGlobalBoolean(L, "stashMoving", false); - //bool[ALLOW_BLOCK_SPAWN] = getGlobalBoolean(L, "allowBlockSpawn", true); - //bool[REMOVE_WEAPON_AMMO] = getGlobalBoolean(L, "removeWeaponAmmunition", true); - //bool[REMOVE_WEAPON_CHARGES] = getGlobalBoolean(L, "removeWeaponCharges", true); - //bool[REMOVE_POTION_CHARGES] = getGlobalBoolean(L, "removeChargesFromPotions", true); - //bool[GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE] = getGlobalBoolean(L, "globalServerSaveNotifyMessage", true); - //bool[GLOBAL_SERVER_SAVE_CLEAN_MAP] = getGlobalBoolean(L, "globalServerSaveCleanMap", false); - //bool[GLOBAL_SERVER_SAVE_CLOSE] = getGlobalBoolean(L, "globalServerSaveClose", false); - //bool[FORCE_MONSTERTYPE_LOAD] = getGlobalBoolean(L, "forceMonsterTypesOnLoad", true); - //bool[HOUSE_OWNED_BY_ACCOUNT] = getGlobalBoolean(L, "houseOwnedByAccount", false); - //bool[CLEAN_PROTECTION_ZONES] = getGlobalBoolean(L, "cleanProtectionZones", false); - //bool[GLOBAL_SERVER_SAVE_SHUTDOWN] = getGlobalBoolean(L, "globalServerSaveShutdown", true); - //bool[PUSH_WHEN_ATTACKING] = getGlobalBoolean(L, "pushWhenAttacking", false); - - //bool[WEATHER_RAIN] = getGlobalBoolean(L, "weatherRain", false); - //bool[WEATHER_THUNDER] = getGlobalBoolean(L, "thunderEffect", false); - //bool[ALL_CONSOLE_LOG] = getGlobalBoolean(L, "allConsoleLog", false); - //bool[TOGGLE_FREE_QUEST] = getGlobalBoolean(L, "toggleFreeQuest", true); - //bool[AUTOLOOT] = getGlobalBoolean(L, "autoLoot", false); - //bool[AUTOBANK] = getGlobalBoolean(L, "autoBank", false); - //bool[STAMINA_TRAINER] = getGlobalBoolean(L, "staminaTrainer", false); - //bool[STAMINA_PZ] = getGlobalBoolean(L, "staminaPz", false); - //bool[SORT_LOOT_BY_CHANCE] = getGlobalBoolean(L, "sortLootByChance", false); + booleanConfig[(int)BooleanConfigType.TOGGLE_SAVE_INTERVAL] = GetGlobalBoolean(L, "toggleSaveInterval", false); booleanConfig[(int)BooleanConfigType.TOGGLE_SAVE_INTERVAL_CLEAN_MAP] = GetGlobalBoolean(L, "toggleSaveIntervalCleanMap", false); - //bool[TELEPORT_SUMMONS] = getGlobalBoolean(L, "teleportSummons", false); booleanConfig[(int)BooleanConfigType.ALLOW_RELOAD] = GetGlobalBoolean(L, "allowReload", true); - //bool[ONLY_PREMIUM_ACCOUNT] = getGlobalBoolean(L, "onlyPremiumAccount", false); - //bool[RATE_USE_STAGES] = getGlobalBoolean(L, "rateUseStages", false); - //bool[TOGGLE_IMBUEMENT_SHRINE_STORAGE] = getGlobalBoolean(L, "toggleImbuementShrineStorage", true); - //bool[TOGGLE_IMBUEMENT_NON_AGGRESSIVE_FIGHT_ONLY] = getGlobalBoolean(L, "toggleImbuementNonAggressiveFightOnly", false); - - //bool[TOGGLE_DOWNLOAD_MAP] = getGlobalBoolean(L, "toggleDownloadMap", false); - //bool[USE_ANY_DATAPACK_FOLDER] = getGlobalBoolean(L, "useAnyDatapackFolder", false); - //bool[INVENTORY_GLOW] = getGlobalBoolean(L, "inventoryGlowOnFiveBless", false); - //bool[XP_DISPLAY_MODE] = getGlobalBoolean(L, "experienceDisplayRates", true); - - //string[DEFAULT_PRIORITY] = getGlobalString(L, "defaultPriority", "high"); - //string[SERVER_NAME] = getGlobalString(L, "serverName", ""); - //string[SERVER_MOTD] = getGlobalString(L, "serverMotd", ""); - //string[OWNER_NAME] = getGlobalString(L, "ownerName", ""); - //string[OWNER_EMAIL] = getGlobalString(L, "ownerEmail", ""); - //string[URL] = getGlobalString(L, "url", ""); - //string[LOCATION] = getGlobalString(L, "location", ""); - //string[WORLD_TYPE] = getGlobalString(L, "worldType", "pvp"); - //string[STORE_IMAGES_URL] = getGlobalString(L, "coinImagesURL", ""); - //string[DISCORD_WEBHOOK_URL] = getGlobalString(L, "discordWebhookURL", ""); stringConfig[(int)StringConfigType.SAVE_INTERVAL_TYPE] = GetGlobalString(L, "saveIntervalType", ""); - //string[GLOBAL_SERVER_SAVE_TIME] = getGlobalString(L, "globalServerSaveTime", "06:00"); - //string[DATA_DIRECTORY] = getGlobalString(L, "dataPackDirectory", "data-otservbr-global"); + stringConfig[(int)StringConfigType.CORE_DIRECTORY] = GetGlobalString(L, "coreDirectory", "data"); - //string[FORGE_FIENDISH_INTERVAL_TYPE] = getGlobalString(L, "forgeFiendishIntervalType", "hour"); - //string[FORGE_FIENDISH_INTERVAL_TIME] = getGlobalString(L, "forgeFiendishIntervalTime", "1"); - - //integer[MAX_PLAYERS] = getGlobalNumber(L, "maxPlayers"); - //integer[PZ_LOCKED] = getGlobalNumber(L, "pzLocked", 60000); - //integer[DEFAULT_DESPAWNRANGE] = getGlobalNumber(L, "deSpawnRange", 2); - //integer[DEFAULT_DESPAWNRADIUS] = getGlobalNumber(L, "deSpawnRadius", 50); - //integer[RATE_EXPERIENCE] = getGlobalNumber(L, "rateExp", 1); - //integer[RATE_SKILL] = getGlobalNumber(L, "rateSkill", 1); - //integer[RATE_LOOT] = getGlobalNumber(L, "rateLoot", 1); - //integer[RATE_MAGIC] = getGlobalNumber(L, "rateMagic", 1); - //integer[RATE_SPAWN] = getGlobalNumber(L, "rateSpawn", 1); - //integer[RATE_KILLING_IN_THE_NAME_OF_POINTS] = getGlobalNumber(L, "rateKillingInTheNameOfPoints", 1); - - //integer[HOUSE_PRICE_PER_SQM] = getGlobalNumber(L, "housePriceEachSQM", 1000); - //integer[HOUSE_BUY_LEVEL] = getGlobalNumber(L, "houseBuyLevel", 0); - //bool[HOUSE_PURSHASED_SHOW_PRICE] = getGlobalBoolean(L, "housePurchasedShowPrice", false); - //bool[ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS] = getGlobalBoolean(L, "onlyInvitedCanMoveHouseItems", true); - - //integer[ACTIONS_DELAY_INTERVAL] = getGlobalNumber(L, "timeBetweenActions", 200); - //integer[EX_ACTIONS_DELAY_INTERVAL] = getGlobalNumber(L, "timeBetweenExActions", 1000); - //integer[MAX_MESSAGEBUFFER] = getGlobalNumber(L, "maxMessageBuffer", 4); - //integer[KICK_AFTER_MINUTES] = getGlobalNumber(L, "kickIdlePlayerAfterMinutes", 15); - //integer[PROTECTION_LEVEL] = getGlobalNumber(L, "protectionLevel", 1); - //integer[DEATH_LOSE_PERCENT] = getGlobalNumber(L, "deathLosePercent", -1); - //integer[STATUSQUERY_TIMEOUT] = getGlobalNumber(L, "statusTimeout", 5000); - //integer[FRAG_TIME] = getGlobalNumber(L, "timeToDecreaseFrags", 24 * 60 * 60 * 1000); - //integer[WHITE_SKULL_TIME] = getGlobalNumber(L, "whiteSkullTime", 15 * 60 * 1000); - //integer[STAIRHOP_DELAY] = getGlobalNumber(L, "stairJumpExhaustion", 2000); - //integer[MAX_CONTAINER] = getGlobalNumber(L, "maxContainer", 500); - //integer[MAX_CONTAINER_ITEM] = getGlobalNumber(L, "maxItem", 5000); - //integer[EXP_FROM_PLAYERS_LEVEL_RANGE] = getGlobalNumber(L, "expFromPlayersLevelRange", 75); - //integer[CHECK_EXPIRED_MARKET_OFFERS_EACH_MINUTES] = getGlobalNumber(L, "checkExpiredMarketOffersEachMinutes", 60); - //integer[MAX_MARKET_OFFERS_AT_A_TIME_PER_PLAYER] = getGlobalNumber(L, "maxMarketOffersAtATimePerPlayer", 100); - //integer[MAX_PACKETS_PER_SECOND] = getGlobalNumber(L, "maxPacketsPerSecond", 25); - //integer[COMPRESSION_LEVEL] = getGlobalNumber(L, "packetCompressionLevel", 6); - //integer[STORE_COIN_PACKET] = getGlobalNumber(L, "coinPacketSize", 25); - //integer[DAY_KILLS_TO_RED] = getGlobalNumber(L, "dayKillsToRedSkull", 3); - //integer[WEEK_KILLS_TO_RED] = getGlobalNumber(L, "weekKillsToRedSkull", 5); - //integer[MONTH_KILLS_TO_RED] = getGlobalNumber(L, "monthKillsToRedSkull", 10); - //integer[RED_SKULL_DURATION] = getGlobalNumber(L, "redSkullDuration", 30); - //integer[BLACK_SKULL_DURATION] = getGlobalNumber(L, "blackSkullDuration", 45); - //integer[ORANGE_SKULL_DURATION] = getGlobalNumber(L, "orangeSkullDuration", 7); - //integer[GLOBAL_SERVER_SAVE_NOTIFY_DURATION] = getGlobalNumber(L, "globalServerSaveNotifyDuration", 5); - - //integer[PARTY_LIST_MAX_DISTANCE] = getGlobalNumber(L, "partyListMaxDistance", 0); - - //integer[PUSH_DELAY] = getGlobalNumber(L, "pushDelay", 1000); - //integer[PUSH_DISTANCE_DELAY] = getGlobalNumber(L, "pushDistanceDelay", 1500); - - //integer[STAMINA_ORANGE_DELAY] = getGlobalNumber(L, "staminaOrangeDelay", 1); - //integer[STAMINA_GREEN_DELAY] = getGlobalNumber(L, "staminaGreenDelay", 5); - //integer[STAMINA_PZ_GAIN] = getGlobalNumber(L, "staminaPzGain", 1); - //integer[STAMINA_TRAINER_DELAY] = getGlobalNumber(L, "staminaTrainerDelay", 5); - //integer[STAMINA_TRAINER_GAIN] = getGlobalNumber(L, "staminaTrainerGain", 1); integerConfig[(int)IntegerConfigType.SAVE_INTERVAL_TIME] = GetGlobalNumber(L, "saveIntervalTime", 1); - //integer[MAX_ALLOWED_ON_A_DUMMY] = getGlobalNumber(L, "maxAllowedOnADummy", 1); - //integer[FREE_QUEST_STAGE] = getGlobalNumber(L, "freeQuestStage", 1); - //integer[DEPOTCHEST] = getGlobalNumber(L, "depotChest", 4); - //integer[CRITICALCHANCE] = getGlobalNumber(L, "criticalChance", 10); - - //integer[ADVENTURERSBLESSING_LEVEL] = getGlobalNumber(L, "adventurersBlessingLevel", 21); - //integer[FORGE_MAX_ITEM_TIER] = getGlobalNumber(L, "forgeMaxItemTier", 10); - //integer[FORGE_COST_ONE_SLIVER] = getGlobalNumber(L, "forgeCostOneSliver", 20); - //integer[FORGE_SLIVER_AMOUNT] = getGlobalNumber(L, "forgeSliverAmount", 3); - //integer[FORGE_CORE_COST] = getGlobalNumber(L, "forgeCoreCost", 50); - //integer[FORGE_MAX_DUST] = getGlobalNumber(L, "forgeMaxDust", 225); - //integer[FORGE_FUSION_DUST_COST] = getGlobalNumber(L, "forgeFusionCost", 100); - //integer[FORGE_TRANSFER_DUST_COST] = getGlobalNumber(L, "forgeTransferCost", 100); - //integer[FORGE_BASE_SUCCESS_RATE] = getGlobalNumber(L, "forgeBaseSuccessRate", 50); - //integer[FORGE_BONUS_SUCCESS_RATE] = getGlobalNumber(L, "forgeBonusSuccessRate", 15); - //integer[FORGE_TIER_LOSS_REDUCTION] = getGlobalNumber(L, "forgeTierLossReduction", 50); - //integer[FORGE_AMOUNT_MULTIPLIER] = getGlobalNumber(L, "forgeAmountMultiplier", 3); - //integer[FORGE_MIN_SLIVERS] = getGlobalNumber(L, "forgeMinSlivers", 3); - //integer[FORGE_MAX_SLIVERS] = getGlobalNumber(L, "forgeMaxSlivers", 7); - //integer[FORGE_INFLUENCED_CREATURES_LIMIT] = getGlobalNumber(L, "forgeInfluencedLimit", 300); - //integer[FORGE_FIENDISH_CREATURES_LIMIT] = getGlobalNumber(L, "forgeFiendishLimit", 3); - //integer[DISCORD_WEBHOOK_DELAY_MS] = getGlobalNumber(L, "discordWebhookDelayMs", Webhook::DEFAULT_DELAY_MS); - - //float[BESTIARY_RATE_CHARM_SHOP_PRICE] = getGlobalFloat(L, "bestiaryRateCharmShopPrice", 1.0); - //float[RATE_HEALTH_REGEN] = getGlobalFloat(L, "rateHealthRegen", 1.0); - //float[RATE_HEALTH_REGEN_SPEED] = getGlobalFloat(L, "rateHealthRegenSpeed", 1.0); - //float[RATE_MANA_REGEN] = getGlobalFloat(L, "rateManaRegen", 1.0); - //float[RATE_MANA_REGEN_SPEED] = getGlobalFloat(L, "rateManaRegenSpeed", 1.0); - //float[RATE_SOUL_REGEN] = getGlobalFloat(L, "rateSoulRegen", 1.0); - //float[RATE_SOUL_REGEN_SPEED] = getGlobalFloat(L, "rateSoulRegenSpeed", 1.0); - //float[RATE_SPELL_COOLDOWN] = getGlobalFloat(L, "rateSpellCooldown", 1.0); - //float[RATE_ATTACK_SPEED] = getGlobalFloat(L, "rateAttackSpeed", 1.0); - //float[RATE_OFFLINE_TRAINING_SPEED] = getGlobalFloat(L, "rateOfflineTrainingSpeed", 1.0); - //float[RATE_EXERCISE_TRAINING_SPEED] = getGlobalFloat(L, "rateExerciseTrainingSpeed", 1.0); - - //float[RATE_MONSTER_HEALTH] = getGlobalFloat(L, "rateMonsterHealth", 1.0); - //float[RATE_MONSTER_ATTACK] = getGlobalFloat(L, "rateMonsterAttack", 1.0); - //float[RATE_MONSTER_DEFENSE] = getGlobalFloat(L, "rateMonsterDefense", 1.0); - //float[RATE_BOSS_HEALTH] = getGlobalFloat(L, "rateBossHealth", 1.0); - //float[RATE_BOSS_ATTACK] = getGlobalFloat(L, "rateBossAttack", 1.0); - //float[RATE_BOSS_DEFENSE] = getGlobalFloat(L, "rateBossDefense", 1.0); - //integer[BOSS_DEFAULT_TIME_TO_FIGHT_AGAIN] = getGlobalNumber(L, "bossDefaultTimeToFightAgain", 20 * 60 * 60); - //integer[BOSS_DEFAULT_TIME_TO_DEFEAT] = getGlobalNumber(L, "bossDefaultTimeToDefeat", 20 * 60); - - //float[RATE_NPC_HEALTH] = getGlobalFloat(L, "rateNpcHealth", 1.0); - //float[RATE_NPC_ATTACK] = getGlobalFloat(L, "rateNpcAttack", 1.0); - //float[RATE_NPC_DEFENSE] = getGlobalFloat(L, "rateNpcDefense", 1.0); - - //bool[PREY_ENABLED] = getGlobalBoolean(L, "preySystemEnabled", true); - //bool[PREY_FREE_THIRD_SLOT] = getGlobalBoolean(L, "preyFreeThirdSlot", false); - //integer[PREY_REROLL_PRICE_LEVEL] = getGlobalNumber(L, "preyRerollPricePerLevel", 200); - //integer[PREY_SELECTION_LIST_PRICE] = getGlobalNumber(L, "preySelectListPrice", 5); - //integer[PREY_BONUS_TIME] = getGlobalNumber(L, "preyBonusTime", 7200); - //integer[PREY_BONUS_REROLL_PRICE] = getGlobalNumber(L, "preyBonusRerollPrice", 1); - //integer[PREY_FREE_REROLL_TIME] = getGlobalNumber(L, "preyFreeRerollTime", 72000); - - //bool[TASK_HUNTING_ENABLED] = getGlobalBoolean(L, "taskHuntingSystemEnabled", true); - //bool[TASK_HUNTING_FREE_THIRD_SLOT] = getGlobalBoolean(L, "taskHuntingFreeThirdSlot", false); - //integer[TASK_HUNTING_LIMIT_EXHAUST] = getGlobalNumber(L, "taskHuntingLimitedTasksExhaust", 72000); - //integer[TASK_HUNTING_REROLL_PRICE_LEVEL] = getGlobalNumber(L, "taskHuntingRerollPricePerLevel", 200); - //integer[TASK_HUNTING_SELECTION_LIST_PRICE] = getGlobalNumber(L, "taskHuntingSelectListPrice", 5); - //integer[TASK_HUNTING_BONUS_REROLL_PRICE] = getGlobalNumber(L, "taskHuntingBonusRerollPrice", 1); - //integer[TASK_HUNTING_FREE_REROLL_TIME] = getGlobalNumber(L, "taskHuntingFreeRerollTime", 72000); - - //integer[BESTIARY_KILL_MULTIPLIER] = getGlobalNumber(L, "bestiaryKillMultiplier", 1); - //integer[BOSSTIARY_KILL_MULTIPLIER] = getGlobalNumber(L, "bosstiaryKillMultiplier", 1); - //bool[BOOSTED_BOSS_SLOT] = getGlobalBoolean(L, "boostedBossSlot", true); - //integer[BOOSTED_BOSS_LOOT_BONUS] = getGlobalNumber(L, "boostedBossLootBonus", 250); - //integer[BOOSTED_BOSS_KILL_BONUS] = getGlobalNumber(L, "boostedBossKillBonus", 3); - - //integer[FAMILIAR_TIME] = getGlobalNumber(L, "familiarTime", 30); - - //bool[TOGGLE_GOLD_POUCH_ALLOW_ANYTHING] = getGlobalBoolean(L, "toggleGoldPouchAllowAnything", false); - //bool[TOGGLE_GOLD_POUCH_QUICKLOOT_ONLY] = getGlobalBoolean(L, "toggleGoldPouchQuickLootOnly", false); - //bool[TOGGLE_SERVER_IS_RETRO] = getGlobalBoolean(L, "toggleServerIsRetroPVP", false); - //bool[TOGGLE_TRAVELS_FREE] = getGlobalBoolean(L, "toggleTravelsFree", false); - //integer[BUY_AOL_COMMAND_FEE] = getGlobalNumber(L, "buyAolCommandFee", 0); - //integer[BUY_BLESS_COMMAND_FEE] = getGlobalNumber(L, "buyBlessCommandFee", 0); - //bool[TELEPORT_PLAYER_TO_VOCATION_ROOM] = getGlobalBoolean(L, "teleportPlayerToVocationRoom", true); - - //bool[TOGGLE_HAZARDSYSTEM] = getGlobalBoolean(L, "toogleHazardSystem", true); - //integer[HAZARD_CRITICAL_INTERVAL] = getGlobalNumber(L, "hazardCriticalInterval", 2000); - //integer[HAZARD_CRITICAL_CHANCE] = getGlobalNumber(L, "hazardCriticalChance", 750); - //integer[HAZARD_CRITICAL_MULTIPLIER] = getGlobalNumber(L, "hazardCriticalMultiplier", 25); - //integer[HAZARD_DAMAGE_MULTIPLIER] = getGlobalNumber(L, "hazardDamageMultiplier", 200); - //integer[HAZARD_DODGE_MULTIPLIER] = getGlobalNumber(L, "hazardDodgeMultiplier", 85); - //integer[HAZARD_PODS_DROP_MULTIPLIER] = getGlobalNumber(L, "hazardPodsDropMultiplier", 87); - //integer[HAZARD_PODS_TIME_TO_DAMAGE] = getGlobalNumber(L, "hazardPodsTimeToDamage", 2000); - //integer[HAZARD_PODS_TIME_TO_SPAWN] = getGlobalNumber(L, "hazardPodsTimeToSpawn", 4000); - //integer[HAZARD_EXP_BONUS_MULTIPLIER] = getGlobalNumber(L, "hazardExpBonusMultiplier", 2); - //integer[HAZARD_LOOT_BONUS_MULTIPLIER] = getGlobalNumber(L, "hazardLootBonusMultiplier", 2); - //integer[HAZARD_PODS_DAMAGE] = getGlobalNumber(L, "hazardPodsDamage", 5); - //integer[HAZARD_SPAWN_PLUNDER_MULTIPLIER] = getGlobalNumber(L, "hazardSpawnPlunderMultiplier", 25); - //integer[LOW_LEVEL_BONUS_EXP] = getGlobalNumber(L, "lowLevelBonusExp", 50); - - //bool[LOYALTY_ENABLED] = getGlobalBoolean(L, "loyaltyEnabled", true); - //integer[LOYALTY_POINTS_PER_CREATION_DAY] = getGlobalNumber(L, "loyaltyPointsPerCreationDay", 1); - //integer[LOYALTY_POINTS_PER_PREMIUM_DAY_SPENT] = getGlobalNumber(L, "loyaltyPointsPerPremiumDaySpent", 0); - //integer[LOYALTY_POINTS_PER_PREMIUM_DAY_PURCHASED] = getGlobalNumber(L, "loyaltyPointsPerPremiumDayPurchased", 0); - //float[LOYALTY_BONUS_PERCENTAGE_MULTIPLIER] = getGlobalFloat(L, "loyaltyBonusPercentageMultiplier", 1.0); - - //bool[TOGGLE_WHEELSYSTEM] = getGlobalBoolean(L, "wheelSystemEnabled", true); - //integer[WHEEL_POINTS_PER_LEVEL] = getGlobalNumber(L, "wheelPointsPerLevel", 1); - - //bool[PARTY_AUTO_SHARE_EXPERIENCE] = getGlobalBoolean(L, "partyAutoShareExperience", true); - //bool[PARTY_SHARE_LOOT_BOOSTS] = getGlobalBoolean(L, "partyShareLootBoosts", true); - //float[PARTY_SHARE_LOOT_BOOSTS_DIMINISHING_FACTOR] = getGlobalFloat(L, "partyShareLootBoostsDimishingFactor", 0.7f); - //integer[TIBIADROME_CONCOCTION_COOLDOWN] = getGlobalNumber(L, "tibiadromeConcoctionCooldown", 24 * 60 * 60); - //integer[TIBIADROME_CONCOCTION_DURATION] = getGlobalNumber(L, "tibiadromeConcoctionDuration", 1 * 60 * 60); - //string[TIBIADROME_CONCOCTION_TICK_TYPE] = getGlobalString(L, "tibiadromeConcoctionTickType", "online"); - - //string[M_CONST] = getGlobalString(L, "memoryConst", "1<<16"); - //integer[T_CONST] = getGlobalNumber(L, "temporaryConst", 2); - //integer[PARALLELISM] = getGlobalNumber(L, "parallelism", 2); - - //// Vip System - //bool[VIP_SYSTEM_ENABLED] = getGlobalBoolean(L, "vipSystemEnabled", false); - //integer[VIP_BONUS_EXP] = getGlobalNumber(L, "vipBonusExp", 0); - //integer[VIP_BONUS_LOOT] = getGlobalNumber(L, "vipBonusLoot", 0); - //integer[VIP_BONUS_SKILL] = getGlobalNumber(L, "vipBonusSkill", 0); - //bool[VIP_AUTOLOOT_VIP_ONLY] = getGlobalBoolean(L, "vipAutoLootVipOnly", false); - //bool[VIP_STAY_ONLINE] = getGlobalBoolean(L, "vipStayOnline", false); - //integer[VIP_FAMILIAR_TIME_COOLDOWN_REDUCTION] = getGlobalNumber(L, "vipFamiliarTimeCooldownReduction", 0); - - //bool[REWARD_CHEST_COLLECT_ENABLED] = getGlobalBoolean(L, "rewardChestCollectEnabled", true); - //integer[REWARD_CHEST_MAX_COLLECT_ITEMS] = getGlobalNumber(L, "rewardChestMaxCollectItems", 200); - - //// PVP System - //float[PVP_RATE_DAMAGE_TAKEN_PER_LEVEL] = getGlobalFloat(L, "pvpRateDamageTakenPerLevel", 0.0); - //float[PVP_RATE_DAMAGE_REDUCTION_PER_LEVEL] = getGlobalFloat(L, "pvpRateDamageReductionPerLevel", 0.0); - //integer[PVP_MAX_LEVEL_DIFFERENCE] = getGlobalNumber(L, "pvpMaxLevelDifference", 0); - - //bool[TOGGLE_MOUNT_IN_PZ] = getGlobalBoolean(L, "toggleMountInProtectionZone", false); - - //bool[TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART] = getGlobalBoolean(L, "togglehouseTransferOnRestart", false); - - //bool[TOGGLE_RECEIVE_REWARD] = getGlobalBoolean(L, "toggleReceiveReward", false); loaded = true; Lua.Close(L); return true; } - //public bool Reload() - //{ - // //bool result = Load(); - // //if (TransformToSHA1(GetString(StringConfig.ServerMotd)) != Game.Instance.GetMotdHash()) - // //{ - // // Game.Instance.IncrementMotdNum(); - // //} - // //return result; - //} - public string GetString(StringConfigType what) { if (what >= StringConfigType.LAST_STRING_CONFIG) @@ -450,27 +139,8 @@ public string GetConfigFileLua() private string configFileLua = ""; - private ConfigManager() - { - // Implementar lógica do construtor aqui - } - private static readonly string DummyStr = string.Empty; - //private string TransformToSHA1(string input) - //{ - // using (SHA1 sha1 = SHA1.Create()) - // { - // byte[] hashBytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); - // StringBuilder sb = new StringBuilder(); - // foreach (byte b in hashBytes) - // { - // sb.Append(b.ToString("x2")); - // } - // return sb.ToString(); - // } - //} - public string GetGlobalString(LuaState L, string identifier, string defaultValue) { Lua.GetGlobal(L, identifier); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ConfigDefinitions.cs similarity index 99% rename from src/Extensions/NeoServer.Scripts.LuaJIT/ConfigDefinitions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ConfigDefinitions.cs index d8a2b22d7..a838b43c9 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ConfigDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ConfigDefinitions.cs @@ -1,4 +1,4 @@ -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Enums; // Enum public enum BooleanConfigType : int diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ItemsDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ItemsDefinitions.cs similarity index 99% rename from src/Extensions/NeoServer.Scripts.LuaJIT/ItemsDefinitions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ItemsDefinitions.cs index 26fb62727..c7ffe28eb 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ItemsDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ItemsDefinitions.cs @@ -1,4 +1,4 @@ -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Enums; // Enums public enum ItemPropertyType @@ -389,7 +389,7 @@ public enum SlotPositionBits : uint SLOTP_AMMO = 1 << 9, SLOTP_DEPOT = 1 << 10, SLOTPTypeWO_HAND = 1 << 11, - SLOTP_HAND = (SLOTP_LEFT | SLOTP_RIGHT) + SLOTP_HAND = SLOTP_LEFT | SLOTP_RIGHT }; public enum TileFlagsType : uint diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/LuaDefinitions.cs similarity index 96% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Enums/LuaDefinitions.cs index 7997d1504..b3175d3d8 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/LuaDefinitions.cs @@ -1,6 +1,6 @@ using NeoServer.Game.Common.Location.Structs; -namespace NeoServer.Scripts.LuaJIT; +namespace NeoServer.Scripts.LuaJIT.Enums; // Enums public enum LuaDataType : byte @@ -244,10 +244,10 @@ public LuaTimerEventDesc() public LuaTimerEventDesc(int scriptId, string scriptName, int function, List parameters, string eventId) { - this.ScriptId = scriptId; - this.ScriptName = scriptName; - this.Function = function; - this.Parameters = parameters; - this.EventId = eventId; + ScriptId = scriptId; + ScriptName = scriptName; + Function = function; + Parameters = parameters; + EventId = eventId; } } \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs index 15c48d15c..e26db1ee4 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Extensions/ReturnMessageExtensions.cs @@ -1,4 +1,6 @@ -namespace NeoServer.Scripts.LuaJIT.Extensions +using NeoServer.Scripts.LuaJIT.Enums; + +namespace NeoServer.Scripts.LuaJIT.Extensions { public static class ReturnMessageExtensions { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs index 4e7748bfd..fb13d46a8 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs @@ -1,5 +1,5 @@ using LuaNET; -using Serilog; +using NeoServer.Scripts.LuaJIT.Enums; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -7,10 +7,7 @@ public class ActionFunctions : LuaScriptInterface, IActionFunctions { private static IActions _actions; - public ActionFunctions( - ILuaEnvironment luaEnvironment, - ILogger logger, - IActions actions) : base(nameof(ActionFunctions)) + public ActionFunctions(IActions actions) : base(nameof(ActionFunctions)) { _actions = actions; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs index 6c7c00d02..86ecc0ca4 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs @@ -1,17 +1,18 @@ using LuaNET; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Server.Configurations; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class ConfigFunctions : LuaScriptInterface, IConfigFunctions { + private static IConfigManager _configManager; private ServerConfiguration _serverConfiguration; public ConfigFunctions( - ServerConfiguration serverConfiguration, - ILuaEnvironment luaEnvironment, - ILogger logger) : base(nameof(ConfigFunctions)) + IConfigManager configManager, + ServerConfiguration serverConfiguration) : base(nameof(ConfigFunctions)) { + _configManager = configManager; _serverConfiguration = serverConfiguration; } @@ -45,28 +46,28 @@ public void Init(LuaState L) private static int LuaConfigManagerGetString(LuaState L) { // configManager:getString() - PushString(L, ConfigManager.GetInstance().GetString(GetNumber(L, -1))); + PushString(L, _configManager.GetString(GetNumber(L, -1))); return 1; } private static int LuaConfigManagerGetNumber(LuaState L) { // configManager:getNumber() - Lua.PushNumber(L, ConfigManager.GetInstance().GetNumber(GetNumber(L, -1))); + Lua.PushNumber(L, _configManager.GetNumber(GetNumber(L, -1))); return 1; } private static int LuaConfigManagerGetBoolean(LuaState L) { // configManager:getBoolean() - PushBoolean(L, ConfigManager.GetInstance().GetBoolean(GetNumber(L, -1))); + PushBoolean(L, _configManager.GetBoolean(GetNumber(L, -1))); return 1; } private static int LuaConfigManagerGetFloat(LuaState L) { // configManager:getFloat() - Lua.PushNumber(L, ConfigManager.GetInstance().GetFloat(GetNumber(L, -1))); + Lua.PushNumber(L, _configManager.GetFloat(GetNumber(L, -1))); return 1; } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs index 9a549bebe..e0243e7f0 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs @@ -1,7 +1,7 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Server.Common.Contracts; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -9,10 +9,7 @@ public class CreatureFunctions : LuaScriptInterface, ICreatureFunctions { private static IGameCreatureManager _gameCreatureManager; - public CreatureFunctions( - ILuaEnvironment luaEnvironment, - ILogger logger, - IGameCreatureManager gameCreatureManager) : base(nameof(CreatureFunctions)) + public CreatureFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(CreatureFunctions)) { _gameCreatureManager = gameCreatureManager; } @@ -23,6 +20,7 @@ public void Init(LuaState L) RegisterMetaMethod(L, "Creature", "__eq", LuaUserdataCompare); RegisterMethod(L, "Creature", "getId", LuaGetId); RegisterMethod(L, "Creature", "getName", LuaGetName); + RegisterMethod(L, "Creature", "getPosition", LuaCreatureGetPosition); } private static int LuaCreatureCreate(LuaState L) @@ -71,15 +69,10 @@ private static int LuaGetId(LuaState L) { // creature:getId() var creature = GetUserdata(L, 1); - if (creature != null) - { Lua.PushNumber(L, creature.CreatureId); - } else - { Lua.PushNil(L); - } return 1; } @@ -98,4 +91,15 @@ private static int LuaGetName(LuaState L) PushString(L, creature.Name); return 1; } + + private static int LuaCreatureGetPosition(LuaState L) + { + // creature:getPosition() + var creature = GetUserdata(L, 1); + if (creature != null) + PushPosition(L, creature.Location); + else + Lua.PushNil(L); + return 1; + } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs index 92e97c49a..b674eb52f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs @@ -1,5 +1,6 @@ using LuaNET; using NeoServer.Game.Common.Location; +using NeoServer.Scripts.LuaJIT.Enums; using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -15,10 +16,11 @@ public void Init(LuaState L) { RegisterEnumCustom(L); //RegisterEnum(L); - RegisterEnum(L); - RegisterEnum(L); + RegisterEnum(L); RegisterEnum(L); RegisterEnum(L); + RegisterEnum(L); + RegisterEnum(L); } private static void RegisterEnum(LuaState L, string name, Enum value) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs index bc7334eb7..5cd13ff9b 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs @@ -1,24 +1,32 @@ using LuaNET; +using NeoServer.Game.Common.Contracts.DataStores; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Contracts.World; +using NeoServer.Game.Common.Contracts.World.Tiles; +using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.Extensions; using NeoServer.Server.Configurations; -using Serilog; +using NeoServer.Server.Helpers; namespace NeoServer.Scripts.LuaJIT.Functions; public class GameFunctions : LuaScriptInterface, IGameFunctions { private static IScripts _scripts; - private static ILogger _logger; + private static IItemTypeStore _itemTypeStore; + private static IItemFactory _itemFactory; private static ServerConfiguration _serverConfiguration; public GameFunctions( - ServerConfiguration serverConfiguration, - ILuaEnvironment luaEnvironment, - ILogger logger, - IScripts scripts) : base(nameof(GameFunctions)) + IScripts scripts, + IItemTypeStore itemTypeStore, + IItemFactory itemFactory, + ServerConfiguration serverConfiguration) : base(nameof(GameFunctions)) { _scripts = scripts; - _logger = logger; + _itemTypeStore = itemTypeStore; + _itemFactory = itemFactory; _serverConfiguration = serverConfiguration; } @@ -27,6 +35,7 @@ public void Init(LuaState L) RegisterTable(L, "Game"); RegisterMethod(L, "Game", "getReturnMessage", LuaGameGetReturnMessage); RegisterMethod(L, "Game", "reload", LuaGameReload); + RegisterMethod(L, "Game", "createItem", LuaGameCreateItem); } private static int LuaGameGetReturnMessage(LuaState L) @@ -43,25 +52,35 @@ private static int LuaGameReload(LuaState L) var reloadType = GetNumber(L, 1); if (reloadType == ReloadType.RELOAD_TYPE_NONE) { - //reportErrorFunc("Reload type is none"); + ReportError(nameof(LuaGameReload), "Reload type is none"); PushBoolean(L, false); return 0; } if (reloadType >= ReloadType.RELOAD_TYPE_LAST) { - //reportErrorFunc("Reload type not exist"); + ReportError(nameof(LuaGameReload), "Reload type not exist"); PushBoolean(L, false); return 0; } try { - var dir = AppContext.BaseDirectory + _serverConfiguration.DataLuaJit; - _scripts.ClearAllScripts(); - _scripts.LoadScripts($"{dir}/scripts", false, true); + switch (reloadType) + { + case ReloadType.RELOAD_TYPE_SCRIPTS: + { + var dir = AppContext.BaseDirectory + _serverConfiguration.DataLuaJit; + _scripts.ClearAllScripts(); + _scripts.LoadScripts($"{dir}/scripts", false, true); - Lua.GC(LuaEnvironment.GetInstance().GetLuaState(), LuaGCParam.Collect, 0); + Lua.GC(LuaEnvironment.GetInstance().GetLuaState(), LuaGCParam.Collect, 0); + } + break; + default: + ReportError(nameof(LuaGameReload), "Reload type not implemented"); + break; + } } catch (Exception e) { @@ -72,4 +91,132 @@ private static int LuaGameReload(LuaState L) PushBoolean(L, true); return 1; } + + private static int LuaGameCreateItem(LuaState L) + { + // Game.createItem(itemId or name[, count[, position]]) + + ushort itemId; + if (Lua.IsNumber(L, 1)) + { + itemId = GetNumber(L, 1); + } + else + { + var itemName = GetString(L, 1); + + var itemTypeByName = _itemTypeStore.GetByName(itemName); + + if (itemTypeByName == null) + { + Lua.PushNil(L); + return 1; + } + + itemId = itemTypeByName.ServerId; + } + + var count = GetNumber(L, 2, 1); + var itemCount = 1; + var subType = 1; + + var it = _itemTypeStore.Get(itemId); + if (it.HasSubType()) + { + if (it.IsStackable()) + { + itemCount = (int)Math.Ceiling(count / (float)it.Count); + } + + subType = count; + } + else + { + itemCount = int.Max(1, count); + } + + var position = new Location(); + if (Lua.GetTop(L) >= 3) + { + position = GetPosition(L, 3); + } + + var hasTable = itemCount > 1; + if (hasTable) + { + Lua.NewTable(L); + } + else if (itemCount == 0) + { + Lua.PushNil(L); + return 1; + } + + var map = IoC.GetInstance(); + + for (int i = 1; i <= itemCount; ++i) + { + var stackCount = subType; + if (it.IsStackable()) + { + stackCount = int.Max(stackCount, it.Count); + subType -= stackCount; + } + + var item = _itemFactory.Create(itemId, position, stackCount); + if (item == null) + { + if (!hasTable) + { + Lua.PushNil(L); + } + return 1; + } + + if (position.X != 0) + { + var tile = map.GetTile(position); + if (tile == null) + { + if (!hasTable) + { + Lua.PushNil(L); + } + return 1; + } + + var result = ((IDynamicTile)tile).AddItem(item); + + if (result.Succeeded) + { + if (!hasTable) + { + Lua.PushNil(L); + } + return 1; + } + } + else + { + GetScriptEnv().AddTempItem(item); + //todo: check if need this + //item->setParent(VirtualCylinder::virtualCylinder); + } + + if (hasTable) + { + Lua.PushNumber(L, i); + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + Lua.SetTable(L, -3); + } + else + { + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + } + } + + return 1; + } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs index 1a6ec3a22..58073c709 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs @@ -1,13 +1,10 @@ using LuaNET; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class GlobalFunctions : LuaScriptInterface, IGlobalFunctions { - public GlobalFunctions( - ILuaEnvironment luaEnvironment, - ILogger logger) : base(nameof(GlobalFunctions)) + public GlobalFunctions() : base(nameof(GlobalFunctions)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs index f661495a4..5b37c2b18 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs @@ -1,16 +1,22 @@ using LuaNET; +using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Contracts.Services; using NeoServer.Game.Common.Item; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class ItemFunctions : LuaScriptInterface, IItemFunctions { + private static IItemTransformService _itemTransformService; + private static IItemTypeStore _itemTypeStore; + public ItemFunctions( - ILuaEnvironment luaEnvironment, - ILogger logger) : base(nameof(ItemFunctions)) + IItemTransformService itemTransformService, + IItemTypeStore itemTypeStore) : base(nameof(ItemFunctions)) { + _itemTransformService = itemTransformService; + _itemTypeStore = itemTypeStore; } public void Init(LuaState L) @@ -22,9 +28,10 @@ public void Init(LuaState L) RegisterMethod(L, "Item", "getUniqueId", LuaGetUniqueId); RegisterMethod(L, "Item", "getSubType", LuaGetSubType); RegisterMethod(L, "Item", "hasProperty", LuaItemHasProperty); + RegisterMethod(L, "Item", "transform", LuaItemTransform); + RegisterMethod(L, "Item", "decay", LuaItemDecay); } - //todo: implements this public static int LuaCreateItem(LuaState L) { // Item(uid) @@ -106,4 +113,92 @@ public static int LuaItemHasProperty(LuaState L) return 1; } + + public static int LuaItemTransform(LuaState L) + { + // item:transform(itemId[, count/subType = -1]) + var item = GetUserdata(L, 1); + if (item == null) + { + Lua.PushNil(L); + return 1; + } + + ushort itemId = 0; + if (Lua.IsNumber(L, 2)) + { + itemId = GetNumber(L, 2); + } + else + { + var itemName = GetString(L, 2); + var itemTypeByName = _itemTypeStore.GetByName(itemName); + + if (itemTypeByName == null) + { + Lua.PushNil(L); + return 1; + } + + itemId = itemTypeByName.ServerId; + } + + var subType = GetNumber(L, 3, -1); + + if (item.ServerId == itemId && (subType == -1 || subType == item.GetSubType())) + { + Lua.PushBoolean(L, true); + return 1; + } + + var it = _itemTypeStore.Get(itemId); + if (it.IsStackable()) + { + subType = int.Min(subType, it.Count); + } + + var env = GetScriptEnv(); + var uid = env.AddThing(item); + + var result = _itemTransformService.Transform(item, itemId); + var newItem = result.Value; + + if (result.Succeeded) + { + env.RemoveItemByUID(uid); + } + + if (newItem != null && newItem != item) + { + env.InsertItem(uid, newItem); + } + + item = newItem; + Lua.PushBoolean(L, true); + return 1; + } + + public static int LuaItemDecay(LuaState L) + { + // item:decay(decayId) + var item = GetUserdata(L, 1); + if (item != null && item.Decay != null) + { + if (Lua.IsNumber(L, 2)) + { + var it = _itemTypeStore.Get(item.ServerId); + var decayTo = GetNumber(L, 2); + it.Attributes.SetAttribute(ItemAttribute.DecayTo, decayTo); + item.UpdateMetadata(it); + } + + item.Decay.StartDecay(); + Lua.PushBoolean(L, true); + } + else + { + Lua.PushNil(L); + } + return 1; + } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs index 48d336ed2..17b2b2804 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs @@ -2,15 +2,13 @@ using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Item; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class ItemTypeFunctions : LuaScriptInterface, IItemTypeFunctions { private static IItemTypeStore _itemTypeStore; - public ItemTypeFunctions( - ILuaEnvironment luaEnvironment, IItemTypeStore itemTypeStore, ILogger logger) : base(nameof(ItemTypeFunctions)) + public ItemTypeFunctions(IItemTypeStore itemTypeStore) : base(nameof(ItemTypeFunctions)) { _itemTypeStore = itemTypeStore; } @@ -38,7 +36,7 @@ public static int LuaCreateItemType(LuaState L) else { var name = GetString(L, 2); - itemType = _itemTypeStore.All.FirstOrDefault(c => c.Name.ToLower().Equals(name.ToLower())); + itemType = _itemTypeStore.GetByName(name); } PushUserdata(L, itemType); @@ -52,7 +50,7 @@ public static int LuaGetItemTypeType(LuaState L) // itemType:getType() var itemType = GetUserdata(L, 1); if (itemType != null) - Lua.PushNumber(L, itemType.TypeId); + Lua.PushNumber(L, itemType.ServerId); else Lua.PushNil(L); @@ -64,7 +62,7 @@ public static int LuaGetItemTypeId(LuaState L) // itemType:getId() var itemType = GetUserdata(L, 1); if (itemType != null) - Lua.PushNumber(L, itemType.TypeId); + Lua.PushNumber(L, itemType.ServerId); else Lua.PushNil(L); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs index 95b21ab49..a70b4ce36 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs @@ -31,7 +31,7 @@ private static int LuaLoggerInfo(LuaState L) } else { - //reportErrorFunc("First parameter needs to be a string"); + ReportError(nameof(LuaLoggerWarn), "First parameter needs to be a string"); } return 1; } @@ -45,7 +45,7 @@ private static int LuaLoggerWarn(LuaState L) } else { - //reportErrorFunc("First parameter needs to be a string"); + ReportError(nameof(LuaLoggerWarn), "First parameter needs to be a string"); } return 1; } @@ -59,7 +59,7 @@ private static int LuaLoggerError(LuaState L) } else { - //reportErrorFunc("First parameter needs to be a string"); + ReportError(nameof(LuaLoggerError), "First parameter needs to be a string"); } return 1; } @@ -73,7 +73,7 @@ private static int LuaLoggerDebug(LuaState L) } else { - //reportErrorFunc("First parameter needs to be a string"); + ReportError(nameof(LuaLoggerDebug), "First parameter needs to be a string"); } return 1; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs index 76e5f4cfc..b5b3d3841 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs @@ -1,9 +1,9 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Networking.Packets.Outgoing; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Server.Common.Contracts; using NeoServer.Server.Services; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -11,10 +11,7 @@ public class PlayerFunctions : LuaScriptInterface, IPlayerFunctions { private static IGameCreatureManager _gameCreatureManager; - public PlayerFunctions( - ILogger logger, - ILuaEnvironment luaEnvironment, - IGameCreatureManager gameCreatureManager) : base(nameof(PlayerFunctions)) + public PlayerFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(PlayerFunctions)) { _gameCreatureManager = gameCreatureManager; } @@ -100,8 +97,7 @@ private static int LuaTeleportTo(LuaState L) private static int LuaPlayerSendTextMessage(LuaState L) { - // player:sendTextMessage(type, text[, position, primaryValue = 0, primaryColor = TEXTCOLOR_NONE[, secondaryValue = 0, secondaryColor = TEXTCOLOR_NONE]]) - // player:sendTextMessage(type, text, channelId) + // player:sendTextMessage(type, text) var player = GetUserdata(L, 1); if (player == null) @@ -115,35 +111,6 @@ private static int LuaPlayerSendTextMessage(LuaState L) var messageType = GetNumber(L, 2); var messageText = GetString(L, 3); - //its not used - //if (parameters == 4) - //{ - // var channelId = GetNumber(L, 4); - - // const auto &channel = g_chat().getChannel(player, channelId); - // if (!channel || !channel->hasUser(player)) - // { - // pushBoolean(L, false); - // return 1; - // } - // message.channelId = channelId; - //} - //else - //{ - // if (parameters >= 6) - // { - // message.position = getPosition(L, 4); - // message.primary.value = getNumber(L, 5); - // message.primary.color = getNumber(L, 6); - // } - - // if (parameters >= 8) - // { - // message.secondary.value = getNumber(L, 7); - // message.secondary.color = getNumber(L, 8); - // } - //} - NotificationSenderService.Send(player, messageText, (TextMessageOutgoingType)messageType); PushBoolean(L, true); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs index c20826888..2fc63d44d 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs @@ -1,14 +1,22 @@ using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Creatures; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Server.Services; using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; public class PositionFunctions : LuaScriptInterface, IPositionFunctions { - public PositionFunctions( - ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(PositionFunctions)) + private static ILogger _logger; + private static IConfigManager _configManager; + + public PositionFunctions(ILogger logger, IConfigManager configManager) : base(nameof(PositionFunctions)) { + _logger = logger; + _configManager = configManager; } public void Init(LuaState L) @@ -17,6 +25,8 @@ public void Init(LuaState L) RegisterMetaMethod(L, "Position", "__add", LuaPositionAdd); RegisterMetaMethod(L, "Position", "__sub", LuaPositionSub); RegisterMetaMethod(L, "Position", "__eq", LuaUserdataCompareStruct); + RegisterMethod(L, "Position", "sendMagicEffect", LuaPositionSendMagicEffect); + RegisterMethod(L, "Position", "toString", LuaPositionToString); } public static int LuaCreatePosition(LuaState L) @@ -88,4 +98,45 @@ public static int LuaPositionSub(LuaState L) return 1; } + + public static int LuaPositionSendMagicEffect(LuaState L) + { + // position:sendMagicEffect(magicEffect[, player = nullptr]) + var spectators = new List(); + if (Lua.GetTop(L) >= 3) + { + var player = GetUserdata(L, 3); + if (player == null) + { + ReportError(nameof(LuaPositionSendMagicEffect), GetErrorDesc(ErrorCodeType.LUA_ERROR_PLAYER_NOT_FOUND)); + return 1; + } + + spectators.Add(player); + } + + var magicEffect = GetNumber(L, 2); + if (_configManager.GetBoolean(BooleanConfigType.WARN_UNSAFE_SCRIPTS)/* &&*/ + /*!g_game().isMagicEffectRegistered(magicEffect)*/) + { + _logger.Warning("[PositionFunctions::luaPositionSendMagicEffect] An unregistered magic effect type with id '{}' was blocked to prevent client crash.", magicEffect); + Lua.PushBoolean(L, false); + return 1; + } + + var position = GetPosition(L, 1); + + EffectService.Send(position, (EffectT)magicEffect, spectators); + + Lua.PushBoolean(L, true); + return 1; + } + + public static int LuaPositionToString(LuaState L) + { + // position:toString() + var position = GetPosition(L, 1); + PushString(L, position.ToString()); + return 1; + } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs index d4188b978..82047ba9f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs @@ -1,4 +1,5 @@ using LuaNET; +using NeoServer.Scripts.LuaJIT.Enums; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -75,6 +76,7 @@ private static int LuaTalkActionRegister(LuaState L) return 1; } + //todo: implement this //if (talkAction.GroupType == Account.GroupType.None) //{ // var errorString = $"TalkAction with name {talkActionSharedPtr.Words} does not have groupType"; @@ -85,8 +87,6 @@ private static int LuaTalkActionRegister(LuaState L) PushBoolean(L, TalkActions.GetInstance().RegisterLuaEvent(talkAction)); - //talkActionSharedPtr.ExecuteSay(Game.GetInstance().GetCurrentPlayer(), "!help", "", SpeakClassesType.TALKTYPE_SAY); - return 1; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs index f65f7fdd0..e2722b5bd 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs @@ -5,7 +5,6 @@ using NeoServer.Game.Common.Location; using NeoServer.Game.Common.Location.Structs; using NeoServer.Server.Common.Contracts; -using Serilog; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -13,8 +12,7 @@ public class TileFunctions : LuaScriptInterface, ITileFunctions { private static IGameServer _gameServer; - public TileFunctions( - ILuaEnvironment luaEnvironment, IGameServer gameServer, ILogger logger) : base(nameof(TileFunctions)) + public TileFunctions(IGameServer gameServer) : base(nameof(TileFunctions)) { _gameServer = gameServer; } @@ -29,6 +27,8 @@ public void Init(LuaState L) RegisterMethod(L, "Tile", "getItemCount", LuaTileGetItemCount); RegisterMethod(L, "Tile", "hasProperty", LuaTileHasProperty); RegisterMethod(L, "Tile", "hasFlag", LuaTileHasFlag); + RegisterMethod(L, "Tile", "getThing", LuaTileGetThing); + RegisterMethod(L, "Tile", "getThingCount", LuaTileGetThingCount); } public static int LuaCreateTile(LuaState L) @@ -63,9 +63,7 @@ public static int LuaGetPosition(LuaState L) // tile:getPosition() var tile = GetUserdata(L, 1); if (tile != null) - { PushPosition(L, tile.Location); - } else Lua.PushNil(L); @@ -166,4 +164,56 @@ public static int LuaTileHasFlag(LuaState L) return 1; } + + public static int LuaTileGetThing(LuaState L) + { + // tile:getThing(index) + var tile = GetUserdata(L, 1); + var index = GetNumber(L, 2); + + if (tile == null) + { + Lua.PushNil(L); + return 1; + } + + var dynamicTile = (IDynamicTile)tile; + + if (dynamicTile.Creatures.Count >= index + 1) + { + var creature = dynamicTile.Creatures[index]; + if (creature != null) + { + PushUserdata(L, creature); + SetCreatureMetatable(L, -1, creature); + return 1; + } + } + else if (dynamicTile.AllItems.Count() >= index + 1) + { + var item = dynamicTile.AllItems[index]; + + if (item != null) + { + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + return 1; + } + } + + Lua.PushNil(L); + + return 1; + } + + public static int LuaTileGetThingCount(LuaState L) + { + // tile:getThingCount() + var tile = GetUserdata(L, 1); + if (tile != null) + Lua.PushNumber(L, tile.ThingsCount); + else + Lua.PushNil(L); + return 1; + } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs index 26d0ee3ab..eb4e43354 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IActions.cs @@ -1,6 +1,7 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Enums; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs index db810eef2..ada3f2388 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/IConfigManager.cs @@ -1,4 +1,5 @@ using LuaNET; +using NeoServer.Scripts.LuaJIT.Enums; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs index 6a45e39c7..4c48f5762 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ILuaEnvironment.cs @@ -14,25 +14,7 @@ public interface ILuaEnvironment : ILuaScriptInterface public LuaScriptInterface GetTestInterface(); - //public Combat GetCombatObject(uint id); - - //public Combat CreateCombatObject(LuaScriptInterface @interface); - - //public void ClearCombatObjects(LuaScriptInterface @interface); - - //public AreaCombat GetAreaObject(uint id); - - //public uint CreateAreaObject(LuaScriptInterface @interface); - - //public void ClearAreaObjects(LuaScriptInterface @interface); - - //public static T CreateWeaponObject(LuaScriptInterface interfaceInstance) where T : YourWeaponType, new(); - - //public static T GetWeaponObject(uint id) where T : YourWeaponType; - - //public static void ClearWeaponObjects(LuaScriptInterface interfaceInstance); - - //public bool IsShuttingDown(); + public bool IsShuttingDown(); public void ExecuteTimerEvent(uint eventIndex); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs index b3d75f152..15a1b0f89 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/ITalkActions.cs @@ -1,5 +1,6 @@ using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Scripts.LuaJIT.Enums; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs index f4796fdea..9fdd70565 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs @@ -1,4 +1,5 @@ using LuaNET; +using NeoServer.Scripts.LuaJIT.Enums; using Serilog; namespace NeoServer.Scripts.LuaJIT; @@ -11,18 +12,6 @@ public class LuaEnvironment : LuaScriptInterface, ILuaEnvironment private static bool shuttingDown = false; - //private static readonly Dictionary> combatIdMap = new Dictionary>(); - //private static readonly Dictionary combatMap = new Dictionary(); - //private static uint lastCombatId = 0; - - //private static readonly Dictionary> areaIdMap = new Dictionary>(); - //private static readonly Dictionary areaMap = new Dictionary(); - //private static uint lastAreaId = 0; - - //private static uint lastWeaponId; - //private static Dictionary weaponMap = new Dictionary(); - //private static Dictionary> weaponIdMap = new Dictionary>(); - public readonly Dictionary timerEvents = new Dictionary(); public uint LastEventTimerId = 1; @@ -155,109 +144,7 @@ public LuaScriptInterface GetTestInterface() return testInterface; } - //public Combat GetCombatObject(uint id) - //{ - // if (combatMap.TryGetValue(id, out var combat)) - // { - // return combat; - // } - // return null; - //} - - //public Combat CreateCombatObject(LuaScriptInterface @interface) - //{ - // var combat = new Combat(); - // combatMap[++lastCombatId] = combat; - // if (!combatIdMap.ContainsKey(@interface)) - // { - // combatIdMap[@interface] = new List(); - // } - // combatIdMap[@interface].Add(lastCombatId); - // return combat; - //} - - //public void ClearCombatObjects(LuaScriptInterface @interface) - //{ - // if (combatIdMap.TryGetValue(@interface, out var combatIds)) - // { - // foreach (var id in combatIds) - // { - // if (combatMap.TryGetValue(id, out var combat)) - // { - // combatMap.Remove(id); - // } - // } - // combatIds.Clear(); - // } - //} - - //public AreaCombat GetAreaObject(uint id) - //{ - // if (areaMap.TryGetValue(id, out var areaCombat)) - // { - // return areaCombat; - // } - // return null; - //} - - //public uint CreateAreaObject(LuaScriptInterface @interface) - //{ - // areaMap[++lastAreaId] = new AreaCombat(); - // if (!areaIdMap.ContainsKey(@interface)) - // { - // areaIdMap[@interface] = new List(); - // } - // areaIdMap[@interface].Add(lastAreaId); - // return lastAreaId; - //} - - //public void ClearAreaObjects(LuaScriptInterface @interface) - //{ - // if (areaIdMap.TryGetValue(@interface, out var areaIds)) - // { - // foreach (var id in areaIds) - // { - // if (areaMap.TryGetValue(id, out var areaCombat)) - // { - // areaMap.Remove(id); - // } - // } - // areaIds.Clear(); - // } - //} - - //public static T CreateWeaponObject(LuaScriptInterface interfaceInstance) where T : YourWeaponType, new() - //{ - // var weapon = new T(interfaceInstance); - // var weaponId = ++lastWeaponId; - // weaponMap[weaponId] = weapon; - // if (!weaponIdMap.ContainsKey(interfaceInstance)) - // { - // weaponIdMap[interfaceInstance] = new List(); - // } - // weaponIdMap[interfaceInstance].Add(weaponId); - // return weapon; - //} - - //public static T GetWeaponObject(uint id) where T : YourWeaponType - //{ - // if (weaponMap.TryGetValue(id, out var weapon)) - // { - // return weapon; - // } - // return null; - //} - - //public static void ClearWeaponObjects(LuaScriptInterface interfaceInstance) - //{ - // if (weaponIdMap.TryGetValue(interfaceInstance, out var weaponIds)) - // { - // weaponIds.Clear(); - // } - // weaponMap.Clear(); - //} - - public static bool IsShuttingDown() + public bool IsShuttingDown() { return shuttingDown; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs index 7e8568ada..d546bda36 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs @@ -5,6 +5,7 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Server.Helpers; using Serilog; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs index 170052f05..bef2a2e7a 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs @@ -237,7 +237,7 @@ public bool InitState() public bool CloseState() { - if (LuaEnvironment.IsShuttingDown()) + if (g_luaEnvironment().IsShuttingDown()) { luaState.pointer = 0; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs index e9c62e179..3db4d88f1 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Script.cs @@ -90,8 +90,4 @@ public virtual void SetScriptId(int newScriptId) private int ScriptId = 0; protected LuaScriptInterface _scriptInterface = null; - - // You may need to replace Logger.Error with the appropriate logging mechanism in your C# code. - // Additionally, consider using properties instead of public fields for encapsulation. - // The 'virtual' keyword is used to allow overriding these methods in derived classes. } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs index a1c2137bf..6f57caaaa 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/ScriptEnvironment.cs @@ -123,12 +123,14 @@ public void InsertItem(uint uid, IItem item) var result = localMap.TryAdd(uid, item); if (!result) { + //todo: implement this //g_logger().error("Thing uid already taken: {}", uid); } } public IThing GetThingByUID(uint uid) { + //todo: implement this //if (uid >= 0x10000000) //{ // return g_game().getCreatureByID(uid); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs index e141dface..1e38a09ac 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs @@ -1,4 +1,5 @@ -using Serilog; +using NeoServer.Scripts.LuaJIT.Enums; +using Serilog; using System; namespace NeoServer.Scripts.LuaJIT; @@ -69,13 +70,6 @@ public void ClearAllScripts() { _talkActions.Clear(); _actions.Clear(); - //_globalEvents.Clear(); - //_creatureEvents.Clear(); - //gSpells.Clear(); - //gMoveEvents.Clear(); - //gWeapons.Clear(); - //gCallbacks.Clear(); - //gMonsters.Clear(); } public bool LoadEventSchedulerScripts(string fileName) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs index 6c24c7cbf..5942c9efe 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkAction.cs @@ -1,7 +1,6 @@ using NeoServer.Application.Common.Contracts.Scripts; using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; -using Serilog; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs index cbf7f82a0..62a81885e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs @@ -1,5 +1,6 @@ using NeoServer.Game.Common.Chats; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Scripts.LuaJIT.Enums; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs index 752bbde57..0d81dca9c 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/UtilsDefinitions.cs @@ -67,7 +67,7 @@ public enum Cipbia_ElementalsType : byte CIPBIA_ELEMENTAL_UNDEFINED = 12, }; -public enum MagicEffectClasses : ushort +public enum MagicEffectClassesType : byte { CONST_ME_NONE, @@ -140,99 +140,7 @@ public enum MagicEffectClasses : ushort CONST_ME_BATS = 67, CONST_ME_SMOKE = 68, CONST_ME_INSECTS = 69, - CONST_ME_DRAGONHEAD = 70, - CONST_ME_ORCSHAMAN = 71, - CONST_ME_ORCSHAMAN_FIRE = 72, - CONST_ME_THUNDER = 73, - CONST_ME_FERUMBRAS = 74, - CONST_ME_CONFETTI_HORIZONTAL = 75, - CONST_ME_CONFETTI_VERTICAL = 76, - // 77-157 are empty - CONST_ME_BLACKSMOKE = 158, - // 159-166 are empty - CONST_ME_REDSMOKE = 167, - CONST_ME_YELLOWSMOKE = 168, - CONST_ME_GREENSMOKE = 169, - CONST_ME_PURPLESMOKE = 170, - CONST_ME_EARLY_THUNDER = 171, - CONST_ME_RAGIAZ_BONECAPSULE = 172, - CONST_ME_CRITICAL_DAMAGE = 173, - // 174 is empty - CONST_ME_PLUNGING_FISH = 175, - CONST_ME_BLUE_ENERGY_SPARK = 176, - CONST_ME_ORANGE_ENERGY_SPARK = 177, - CONST_ME_GREEN_ENERGY_SPARK = 178, - CONST_ME_PINK_ENERGY_SPARK = 179, - CONST_ME_WHITE_ENERGY_SPARK = 180, - CONST_ME_YELLOW_ENERGY_SPARK = 181, - CONST_ME_MAGIC_POWDER = 182, - // 183 is empty - CONST_ME_PIXIE_EXPLOSION = 184, - CONST_ME_PIXIE_COMING = 185, - CONST_ME_PIXIE_GOING = 186, - // 187 is empty - CONST_ME_STORM = 188, - CONST_ME_STONE_STORM = 189, - // 190 is empty - CONST_ME_BLUE_GHOST = 191, - // 192 is empty - CONST_ME_PINK_VORTEX = 193, - CONST_ME_TREASURE_MAP = 194, - CONST_ME_PINK_BEAM = 195, - CONST_ME_GREEN_FIREWORKS = 196, - CONST_ME_ORANGE_FIREWORKS = 197, - CONST_ME_PINK_FIREWORKS = 198, - CONST_ME_BLUE_FIREWORKS = 199, - // 200 is empty - CONST_ME_SUPREME_CUBE = 201, - CONST_ME_BLACK_BLOOD = 202, - CONST_ME_PRISMATIC_SPARK = 203, - CONST_ME_THAIAN = 204, - CONST_ME_THAIAN_GHOST = 205, - CONST_ME_GHOST_SMOKE = 206, - // 207 is empty - CONST_ME_WATER_BLOCK_FLOATING = 208, - CONST_ME_WATER_BLOCK = 209, - CONST_ME_ROOTS = 210, - // 211-212 is empty - CONST_ME_GHOSTLY_SCRATCH = 213, - CONST_ME_GHOSTLY_BITE = 214, - CONST_ME_BIG_SCRATCH = 215, - CONST_ME_SLASH = 216, - CONST_ME_BITE = 217, - // 218 empty or debug - CONST_ME_CHIVALRIOUS_CHALLENGE = 219, - CONST_ME_DIVINE_DAZZLE = 220, - CONST_ME_ELECTRICALSPARK = 221, - CONST_ME_PURPLETELEPORT = 222, - CONST_ME_REDTELEPORT = 223, - CONST_ME_ORANGETELEPORT = 224, - CONST_ME_GREYTELEPORT = 225, - CONST_ME_LIGHTBLUETELEPORT = 226, - // 227-229 are empty - CONST_ME_FATAL = 230, - CONST_ME_DODGE = 231, - CONST_ME_HOURGLASS = 232, - CONST_ME_DAZZLING = 233, - CONST_ME_SPARKLING = 234, - CONST_ME_FERUMBRAS_1 = 235, - CONST_ME_GAZHARAGOTH = 236, - CONST_ME_MAD_MAGE = 237, - CONST_ME_HORESTIS = 238, - CONST_ME_DEVOVORGA = 239, - CONST_ME_FERUMBRAS_2 = 240, - - CONST_ME_WHITE_SMOKE = 241, - CONST_ME_WHITE_SMOKES = 242, - CONST_ME_WATER_DROP = 243, - CONST_ME_AVATAR_APPEAR = 244, - CONST_ME_DIVINE_GRENADE = 245, // Permanent - CONST_ME_DIVINE_EMPOWERMENT = 246, // Permanent - CONST_ME_WATER_FLOATING_THRASH = 247, - - CONST_ME_AGONY = 249, - - CONST_ME_LAST = CONST_ME_AGONY + CONST_ME_DRAGONHEAD = 70 }; public enum ShootTypeType : byte diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/DataStores/IItemTypeStore.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/DataStores/IItemTypeStore.cs index 9389af71d..8dc3a289d 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/DataStores/IItemTypeStore.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/DataStores/IItemTypeStore.cs @@ -1,7 +1,10 @@ using NeoServer.Game.Common.Contracts.Items; +using System.Linq; namespace NeoServer.Game.Common.Contracts.DataStores; public interface IItemTypeStore : IDataStore { + public virtual IItemType GetByName(string name) + => All.FirstOrDefault(c => c.Name.ToLower().Equals(name.ToLower())); } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs index e322cd712..c88d346b2 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs @@ -20,7 +20,7 @@ public interface IItem : IThing, IHasDecay string Plural => Metadata.Plural; ushort ClientId => Metadata.ClientId; - ushort ServerId => Metadata.TypeId; + ushort ServerId => Metadata.ServerId; ushort CanTransformTo => Metadata.Attributes.GetTransformationItem(); bool CanBeMoved => Metadata.HasFlag(ItemFlag.Movable); bool IsBlockeable => Metadata.HasFlag(ItemFlag.Unpassable); @@ -79,16 +79,16 @@ ushort GetSubType() { if (Metadata.HasFlag(ItemFlag.LiquidContainer)) { - var fluidType = Metadata.Attributes.GetAttribute(ItemAttribute.ContainerLiquidType); - return ushort.Parse(fluidType); + var fluidType = Metadata.Attributes.GetAttribute(ItemAttribute.ContainerLiquidType); + return fluidType; } else if (Metadata.HasFlag(ItemFlag.Stackable)) { - var count = Metadata.Attributes.GetAttribute(ItemAttribute.Count); - return ushort.Parse(count); + var count = Metadata.Attributes.GetAttribute(ItemAttribute.Count); + return count; } - var charges = Metadata.Attributes.GetAttribute(ItemAttribute.Charges); - return ushort.Parse(charges); + var charges = Metadata.Attributes.GetAttribute(ItemAttribute.Charges); + return charges; } } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemFactory.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemFactory.cs index 8e6aa67d1..b9a505b53 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemFactory.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemFactory.cs @@ -10,6 +10,9 @@ namespace NeoServer.Game.Common.Contracts.Items; public interface IItemFactory : IFactory { + IItem Create(ushort typeId, Location.Structs.Location location, int count = 1, + IEnumerable children = null); + IItem Create(ushort typeId, Location.Structs.Location location, IDictionary attributes, IEnumerable children = null); diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs index 618b30062..4bda10bcb 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs @@ -7,7 +7,7 @@ namespace NeoServer.Game.Common.Contracts.Items; public interface IItemType { - ushort TypeId { get; } + ushort ServerId { get; } string Name { get; } string FullName { get; } @@ -42,4 +42,11 @@ public interface IItemType void SetOnUse(); bool HasAtLeastOneFlag(params ItemFlag[] flags); void SetGroupIfNone(); + + bool IsFluidContainer(); + bool IsSplash(); + bool IsStackable(); + ushort Charges { get; } + ushort Count { get; } + bool HasSubType(); } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Npcs/Shop/ShopperNpc.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Npcs/Shop/ShopperNpc.cs index d8cd93320..a36ff3680 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Npcs/Shop/ShopperNpc.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Npcs/Shop/ShopperNpc.cs @@ -45,7 +45,7 @@ public bool BuyFromCustomer(ISociableCreature creature, IItemType item, byte amo var shopItems = ShopItems; if (shopItems is null) return false; - if (!shopItems.TryGetValue(item.TypeId, out var shopItem)) return false; + if (!shopItems.TryGetValue(item.ServerId, out var shopItem)) return false; return Pay(creature, shopItem.SellPrice * amount); } @@ -55,7 +55,7 @@ public ulong CalculateCost(IItemType itemType, byte amount) var shopItems = ShopItems; if (shopItems is null) return 0; - if (!shopItems.TryGetValue(itemType.TypeId, out var shopItem)) return 0; + if (!shopItems.TryGetValue(itemType.ServerId, out var shopItem)) return 0; return (shopItem?.BuyPrice ?? 0) * amount; } diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Calculations/InventoryMoneyCalculation.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Calculations/InventoryMoneyCalculation.cs index dcd6c2f2f..25f05cd9a 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Calculations/InventoryMoneyCalculation.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Calculations/InventoryMoneyCalculation.cs @@ -18,7 +18,7 @@ internal static ulong CalculateTotalMoney(this Inventory inventory, ICoinTypeSto foreach (var coinType in coinTypes) { if (coinType is null) continue; - if (!inventoryMap.TryGetValue(coinType.TypeId, out var coinAmount)) continue; + if (!inventoryMap.TryGetValue(coinType.ServerId, out var coinAmount)) continue; var worthMultiplier = coinType?.Attributes?.GetAttribute(ItemAttribute.Worth) ?? 0; total += worthMultiplier * coinAmount; diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/InventoryMap.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/InventoryMap.cs index 3417a3eae..e0d8d0df3 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/InventoryMap.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/InventoryMap.cs @@ -25,10 +25,10 @@ internal IDictionary Map void AddOrUpdate(IItem item) { if (item is null) return; - if (map.TryGetValue(item.Metadata.TypeId, out var val)) - map[item.Metadata.TypeId] = val + item.Amount; + if (map.TryGetValue(item.Metadata.ServerId, out var val)) + map[item.Metadata.ServerId] = val + item.Amount; else - map.Add(item.Metadata.TypeId, item.Amount); + map.Add(item.Metadata.ServerId, item.Amount); } AddOrUpdate(Inventory[Slot.Head]); diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Rules/AddToSlotRule.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Rules/AddToSlotRule.cs index f2cacdb4b..098b54df0 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Rules/AddToSlotRule.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Inventory/Rules/AddToSlotRule.cs @@ -73,7 +73,7 @@ public static Result CanAddItem(Inventory inventory, IItemType itemType) if (itemType.BodyPosition == Slot.None) return new Result(InvalidOperation.NotEnoughRoom); var itemOnSlot = inventory[itemType.BodyPosition]; - if (itemOnSlot is not null && itemType.TypeId != itemOnSlot.Metadata.TypeId) + if (itemOnSlot is not null && itemType.ServerId != itemOnSlot.Metadata.ServerId) return new Result(InvalidOperation.NotEnoughRoom); byte possibleAmountToAdd; diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs index 56c180101..45650c9b6 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Player.cs @@ -756,7 +756,7 @@ public bool Sell(IItemType item, byte amount, bool ignoreEquipped) { if (!ignoreEquipped) return true; if (Inventory.BackpackSlot?.Map is null) return false; - if (!Inventory.BackpackSlot.Map.TryGetValue(item.TypeId, out var itemTotalAmount)) return false; + if (!Inventory.BackpackSlot.Map.TryGetValue(item.ServerId, out var itemTotalAmount)) return false; if (itemTotalAmount < amount) return false; diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/DealTransaction.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/DealTransaction.cs index bdb012c34..7cbf55444 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/DealTransaction.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/DealTransaction.cs @@ -40,7 +40,7 @@ public bool Buy(IPlayer buyer, IShopperNpc seller, IItemType itemType, byte amou if (removedAmount < cost) buyer.WithdrawFromBank(cost - removedAmount); - var saleContract = new SaleContract(itemType.TypeId, amount, amountToAddToInventory, amountToAddToBackpack); + var saleContract = new SaleContract(itemType.ServerId, amount, amountToAddToInventory, amountToAddToBackpack); AddItems(buyer, seller, saleContract); diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs index bc17432ee..870558770 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Principal; using NeoServer.Game.Common.Contracts; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.DataStores; @@ -70,6 +71,13 @@ public IItem CreateLootCorpse(ushort typeId, Location location, ILoot loot) return createdItem; } + public IItem Create(ushort typeId, Location location, int count = 1, + IEnumerable children = null) + { + var attributes = new Dictionary { { ItemAttribute.Count, count } }; + return Create(typeId, location, attributes, children); + } + public IItem Create(ushort typeId, Location location, IDictionary attributes, IEnumerable children = null) { @@ -123,7 +131,7 @@ public IItem Create(string name, Location location, IDictionary x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); - return item is null ? null : Create(item.TypeId, location, attributes, children); + return item is null ? null : Create(item.ServerId, location, attributes, children); } private static void SetItemIds(IDictionary attributes, IItem createdItem) @@ -158,7 +166,7 @@ private IItem CreateItem(IItemType itemType, Location location, { itemType.Attributes.SetAttribute(attributes); - if (itemType.TypeId < 100) return null; + if (itemType.ServerId < 100) return null; if (itemType.Group == ItemGroup.Deprecated) return null; diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Helpers/OverridenFunctionQuery.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Helpers/OverridenFunctionQuery.cs index 1a75a24c9..14008339d 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Helpers/OverridenFunctionQuery.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Helpers/OverridenFunctionQuery.cs @@ -9,10 +9,10 @@ public static List Find(IItem item, Dictionary useFunctionMap) { var functions = new List(3); - if (useFunctionMap.TryGetValue($"id:{item.Metadata.TypeId}", out var useFunc)) functions.Add(useFunc); + if (useFunctionMap.TryGetValue($"id:{item.Metadata.ServerId}", out var useFunc)) functions.Add(useFunc); if (item.ActionId != 0 && (useFunctionMap.TryGetValue($"aid:{item.ActionId}", out useFunc) || - useFunctionMap.TryGetValue($"id:{item.Metadata.TypeId}-aid:{item.ActionId}", + useFunctionMap.TryGetValue($"id:{item.Metadata.ServerId}-aid:{item.ActionId}", out useFunc))) functions.Add(useFunc); diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs b/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs index 0fc913c57..6116a231a 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs @@ -15,7 +15,7 @@ public class ItemType : IItemType { public ItemType() { - TypeId = 0; + ServerId = 0; Name = string.Empty; Flags = new HashSet(); Attributes = new ItemAttributeList(); @@ -29,7 +29,7 @@ public ItemType() /// /// Server Id /// - public ushort TypeId { get; private set; } + public ushort ServerId { get; private set; } /// /// ItemType's name @@ -98,6 +98,24 @@ public bool HasAtLeastOneFlag(params ItemFlag[] flags) return false; } + public bool IsFluidContainer() + => Flags.Contains(ItemFlag.LiquidContainer); + + public bool IsSplash() + => Group == ItemGroup.Splash; + + public bool IsStackable() + => Group == ItemGroup.Splash; + + public ushort Charges + => Attributes.GetAttribute(ItemAttribute.Charges); + + public ushort Count + => Attributes.GetAttribute(ItemAttribute.Count); + + public bool HasSubType() + => IsFluidContainer() || IsSplash() || IsStackable() || Charges != 0; + public AmmoType AmmoType => Attributes?.GetAttribute(ItemAttribute.AmmoType) switch { "bolt" => AmmoType.Bolt, @@ -150,7 +168,7 @@ public IItemType SetId(ushort typeId) { ThrowIfLocked(); - TypeId = typeId; + ServerId = typeId; return this; } diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Builders/ContainerMapBuilder.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Builders/ContainerMapBuilder.cs index d8340f4fa..6eee7aa4f 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Builders/ContainerMapBuilder.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Builders/ContainerMapBuilder.cs @@ -12,8 +12,8 @@ public static IDictionary Build(IContainer container, foreach (var item in container.Items) { - if (map.TryGetValue(item.Metadata.TypeId, out var val)) map[item.Metadata.TypeId] = val + item.Amount; - else map.Add(item.Metadata.TypeId, item.Amount); + if (map.TryGetValue(item.Metadata.ServerId, out var val)) map[item.Metadata.ServerId] = val + item.Amount; + else map.Add(item.Metadata.ServerId, item.Amount); if (item is IContainer child) Build(child, map); } diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Queries/FindItemByTypeQuery.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Queries/FindItemByTypeQuery.cs index 84b239e34..7c8462420 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Queries/FindItemByTypeQuery.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Queries/FindItemByTypeQuery.cs @@ -19,7 +19,7 @@ internal static class FindItemByTypeQuery slotIndex++; - if (item.Metadata.TypeId != itemToRemove.TypeId) continue; + if (item.Metadata.ServerId != itemToRemove.ServerId) continue; if (amount == 0) break; slotsToRemove.Push((item, (byte)slotIndex, Math.Min(item.Amount, amount))); diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Rules/CanAddItemToContainerRule.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Rules/CanAddItemToContainerRule.cs index 5ffdc27cd..1a37a7e18 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Rules/CanAddItemToContainerRule.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Containers/Container/Rules/CanAddItemToContainerRule.cs @@ -47,7 +47,7 @@ private static uint CalculateAmountPossibleToAdd(IContainer toContainer, IItemTy { var amountPossibleToAdd = toContainer.TotalOfFreeSlots * 100; - if (!toContainer.Map.TryGetValue(itemType.TypeId, out var totalAmount)) return amountPossibleToAdd; + if (!toContainer.Map.TryGetValue(itemType.ServerId, out var totalAmount)) return amountPossibleToAdd; var next100 = Math.Ceiling((decimal)totalAmount / 100) * 100; amountPossibleToAdd += (uint)(next100 - totalAmount); diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/FloorChangerUsableItem.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/FloorChangerUsableItem.cs index 6711e5f06..d497471b4 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/FloorChangerUsableItem.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/FloorChangerUsableItem.cs @@ -21,7 +21,7 @@ public virtual bool Use(ICreature usedBy, IItem onItem) if (usedBy is not IPlayer player) return false; var canUseOnItems = Metadata.OnUse?.GetAttributeArray(ItemAttribute.UseOn) ?? Array.Empty(); - if (!canUseOnItems.Contains(onItem.Metadata.TypeId)) return false; + if (!canUseOnItems.Contains(onItem.Metadata.ServerId)) return false; if (Metadata.OnUse?.GetAttribute(ItemAttribute.FloorChange) != "up") return false; diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/UsableOnItem.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/UsableOnItem.cs index e14d413a7..5b51ecc9a 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/UsableOnItem.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Items/UsableItems/UsableOnItem.cs @@ -21,12 +21,12 @@ public virtual bool CanUseOn(IItem onItem) { var useOnItems = Metadata.OnUse?.GetAttributeArray(ItemAttribute.UseOn); - return useOnItems is not null && useOnItems.Contains(onItem.Metadata.TypeId); + return useOnItems is not null && useOnItems.Contains(onItem.Metadata.ServerId); } public bool CanUseOn(ushort[] items, IItem onItem) { - return ((IList)items)?.Contains(onItem.Metadata.TypeId) ?? false; + return ((IList)items)?.Contains(onItem.Metadata.ServerId) ?? false; } public virtual bool CanUse(ICreature usedBy, IItem onItem) diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Weapons/MeleeWeapon.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Weapons/MeleeWeapon.cs index 5eb0e0b8d..fc2ac176f 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Items/Weapons/MeleeWeapon.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Items/Weapons/MeleeWeapon.cs @@ -43,14 +43,14 @@ protected override string PartialInspectionText public virtual bool CanUseOn(ushort[] items, IItem onItem) { - return items is not null && ((IList)items).Contains(onItem.Metadata.TypeId); + return items is not null && ((IList)items).Contains(onItem.Metadata.ServerId); } public virtual bool CanUseOn(IItem onItem) { var useOnItems = Metadata.OnUse?.GetAttributeArray(ItemAttribute.UseOn); - return useOnItems is not null && ((IList)useOnItems).Contains(onItem.Metadata.TypeId); + return useOnItems is not null && ((IList)useOnItems).Contains(onItem.Metadata.ServerId); } public override bool CanBeDressed(IPlayer player) diff --git a/src/Loaders/NeoServer.Loaders/Items/ItemTypeLoader.cs b/src/Loaders/NeoServer.Loaders/Items/ItemTypeLoader.cs index 83e29866b..622b8a79d 100644 --- a/src/Loaders/NeoServer.Loaders/Items/ItemTypeLoader.cs +++ b/src/Loaders/NeoServer.Loaders/Items/ItemTypeLoader.cs @@ -67,7 +67,7 @@ private Dictionary LoadOtb(string basePath) var otbNode = OtbBinaryTreeBuilder.Deserialize(fileStream); var otb = new Otb(otbNode); - var itemTypes = otb.ItemNodes.AsParallel().Select(ItemNodeParser.Parse).ToDictionary(x => x.TypeId); + var itemTypes = otb.ItemNodes.AsParallel().Select(ItemNodeParser.Parse).ToDictionary(x => x.ServerId); return itemTypes; } diff --git a/src/Loaders/NeoServer.Loaders/Npcs/NpcLoader.cs b/src/Loaders/NeoServer.Loaders/Npcs/NpcLoader.cs index abe7dac01..3e5f5fc91 100644 --- a/src/Loaders/NeoServer.Loaders/Npcs/NpcLoader.cs +++ b/src/Loaders/NeoServer.Loaders/Npcs/NpcLoader.cs @@ -106,7 +106,7 @@ private void LoadShopData(INpcType type, NpcJsonData npcData) foreach (var item in npcData.Shop) { if (!_itemTypeStore.TryGetValue(item.Item, out var itemType)) continue; - items.Add(itemType.TypeId, new ShopItem(itemType, item.Buy, item.Sell)); + items.Add(itemType.ServerId, new ShopItem(itemType, item.Buy, item.Sell)); } type.CustomAttributes.Add("shop", items); diff --git a/src/NetworkingServer/NeoServer.Networking.Packets/Outgoing/Npc/SaleItemListPacket.cs b/src/NetworkingServer/NeoServer.Networking.Packets/Outgoing/Npc/SaleItemListPacket.cs index 115ee99dc..031257f77 100644 --- a/src/NetworkingServer/NeoServer.Networking.Packets/Outgoing/Npc/SaleItemListPacket.cs +++ b/src/NetworkingServer/NeoServer.Networking.Packets/Outgoing/Npc/SaleItemListPacket.cs @@ -37,13 +37,13 @@ public override void WriteToMessage(INetworkMessage message) { if (shopItem.SellPrice == 0) continue; - var index = (int)shopItem.Item.TypeId; + var index = (int)shopItem.Item.ServerId; //if (Item::items[shopInfo.itemId].isFluidContainer()) //todo //{ // index |= (static_cast(shopInfo.subType) << 16); //} - if (!map.TryGetValue(shopItem.Item.TypeId, out var itemAmount)) continue; + if (!map.TryGetValue(shopItem.Item.ServerId, out var itemAmount)) continue; temp.AddRange(BitConverter.GetBytes(shopItem.Item.ClientId)); temp.Add((byte)Math.Min(itemAmount, byte.MaxValue)); diff --git a/tests/NeoServer.Game.Items.Tests/Items/EquipmentTests.cs b/tests/NeoServer.Game.Items.Tests/Items/EquipmentTests.cs index b2deeb5fa..4a772ed70 100644 --- a/tests/NeoServer.Game.Items.Tests/Items/EquipmentTests.cs +++ b/tests/NeoServer.Game.Items.Tests/Items/EquipmentTests.cs @@ -757,7 +757,7 @@ IEquipment GetSlotItem() await Task.Delay(2050); decayableItemManager.DecayExpiredItems(); - player.Inventory[Slot.Ring].Metadata.TypeId.Should().Be(400); + player.Inventory[Slot.Ring].Metadata.ServerId.Should().Be(400); //assert second item equipped GetSlotItem().Decay?.Duration.Should().Be(2); @@ -765,7 +765,7 @@ IEquipment GetSlotItem() //act await Task.Delay(2050); decayableItemManager.DecayExpiredItems(); - player.Inventory[Slot.Ring].Metadata.TypeId.Should().Be(600); + player.Inventory[Slot.Ring].Metadata.ServerId.Should().Be(600); //assert third item equipped GetSlotItem().Decay?.Duration.Should().Be(2); diff --git a/tests/NeoServer.Game.Tests/Server/ItemTypeStoreTestBuilder.cs b/tests/NeoServer.Game.Tests/Server/ItemTypeStoreTestBuilder.cs index 8fdc1f24d..05c1c8199 100644 --- a/tests/NeoServer.Game.Tests/Server/ItemTypeStoreTestBuilder.cs +++ b/tests/NeoServer.Game.Tests/Server/ItemTypeStoreTestBuilder.cs @@ -16,7 +16,7 @@ public static ItemTypeStore Build(params IItem[] items) public static ItemTypeStore Build(params IItemType[] itemsTypes) { var itemTypeStore = new ItemTypeStore(); - foreach (var item in itemsTypes) itemTypeStore.Add(item.TypeId, item); + foreach (var item in itemsTypes) itemTypeStore.Add(item.ServerId, item); return itemTypeStore; } From 07ae4814935a919425d2b11f14338f89fe75a813 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Tue, 24 Dec 2024 14:39:14 -0300 Subject: [PATCH 07/16] feat: renamed and moved all Function classes to LuaMapping --- .../Interfaces/Functions/IActionFunctions.cs | 5 - .../Interfaces/Functions/IBaseFunctions.cs | 8 -- .../Interfaces/Functions/IConfigFunctions.cs | 5 - .../Functions/ICreatureFunctions.cs | 5 - .../Interfaces/Functions/IEnumFunctions.cs | 5 - .../Interfaces/Functions/IGameFunctions.cs | 5 - .../Interfaces/Functions/IGlobalFunctions.cs | 5 - .../Interfaces/Functions/IItemFunctions.cs | 5 - .../Functions/IItemTypeFunctions.cs | 5 - .../Interfaces/Functions/ILoggerFunctions.cs | 5 - .../Interfaces/Functions/IPlayerFunctions.cs | 5 - .../Functions/IPositionFunctions.cs | 5 - .../Functions/ITalkActionFunctions.cs | 5 - .../Interfaces/Functions/ITileFunctions.cs | 5 - .../IoC/Modules/LuaJITInjection.cs | 29 ++--- .../LuaGameManager.cs | 107 +++++++++--------- .../ActionLuaMapping.cs} | 7 +- .../ConfigLuaMappingr.cs} | 10 +- .../CreatureLuaMapping.cs} | 7 +- .../EnumLuaMapping.cs} | 9 +- .../GameLuaMapping.cs} | 9 +- .../GlobalLuaMapping.cs} | 7 +- .../Interfaces/IActionLuaMapping.cs | 5 + .../LuaMappings/Interfaces/IBaseLuaMapping.cs | 8 ++ .../Interfaces/IConfigLuaMapping.cs | 5 + .../Interfaces/ICreatureLuaMapping.cs | 5 + .../LuaMappings/Interfaces/IEnumLuaMapping.cs | 5 + .../LuaMappings/Interfaces/IGameLuaMapping.cs | 5 + .../Interfaces/IGlobalLuaMapping.cs | 5 + .../LuaMappings/Interfaces/IItemLuaMapping.cs | 5 + .../Interfaces/IItemTypeLuaMapping.cs | 5 + .../Interfaces/ILoggerLuaMapping.cs | 5 + .../Interfaces/IPlayerLuaMapping.cs | 5 + .../Interfaces/IPositionLuaMapping.cs | 5 + .../Interfaces/ITalkActionLuaMapping.cs | 5 + .../LuaMappings/Interfaces/ITileLuaMapping.cs | 5 + .../ItemLuaMapping.cs} | 9 +- .../ItemTypeLuaMapping.cs} | 7 +- .../LoggerLuaMapping.cs} | 9 +- .../PlayerLuaMapping.cs} | 7 +- .../PositionLuaMapping.cs} | 7 +- .../TalkActionLuaMapping.cs} | 7 +- .../TileLuaMapping.cs} | 7 +- 43 files changed, 200 insertions(+), 184 deletions(-) delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IBaseFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/ActionFunctions.cs => LuaMappings/ActionLuaMapping.cs} (91%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/ConfigFunctions.cs => LuaMappings/ConfigLuaMappingr.cs} (91%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/CreatureFunctions.cs => LuaMappings/CreatureLuaMapping.cs} (90%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/EnumFunctions.cs => LuaMappings/EnumLuaMapping.cs} (83%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/GameFunctions.cs => LuaMappings/GameLuaMapping.cs} (96%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/GlobalFunctions.cs => LuaMappings/GlobalLuaMapping.cs} (58%) create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/ItemFunctions.cs => LuaMappings/ItemLuaMapping.cs} (95%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/ItemTypeFunctions.cs => LuaMappings/ItemTypeLuaMapping.cs} (89%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/LoggerFunctions.cs => LuaMappings/LoggerLuaMapping.cs} (89%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/PlayerFunctions.cs => LuaMappings/PlayerLuaMapping.cs} (92%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/PositionFunctions.cs => LuaMappings/PositionLuaMapping.cs} (93%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/TalkActionFunctions.cs => LuaMappings/TalkActionLuaMapping.cs} (93%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{Functions/TileFunctions.cs => LuaMappings/TileLuaMapping.cs} (95%) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs deleted file mode 100644 index 9c7d77e26..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IActionFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IActionFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IBaseFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IBaseFunctions.cs deleted file mode 100644 index 20b6cedca..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IBaseFunctions.cs +++ /dev/null @@ -1,8 +0,0 @@ -using LuaNET; - -namespace NeoServer.Scripts.LuaJIT; - -public interface IBaseFunctions -{ - void Init(LuaState L); -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs deleted file mode 100644 index f52668898..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IConfigFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IConfigFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs deleted file mode 100644 index 34f831771..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ICreatureFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface ICreatureFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs deleted file mode 100644 index 61e0cff8d..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IEnumFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IEnumFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs deleted file mode 100644 index 111427ba7..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGameFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IGameFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs deleted file mode 100644 index d399dc267..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IGlobalFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IGlobalFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs deleted file mode 100644 index 13500e4b6..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IItemFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs deleted file mode 100644 index b8654392b..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IItemTypeFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IItemTypeFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs deleted file mode 100644 index fcc529669..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ILoggerFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface ILoggerFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs deleted file mode 100644 index d7e846088..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPlayerFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IPlayerFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs deleted file mode 100644 index c1c764861..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/IPositionFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface IPositionFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs deleted file mode 100644 index 90fe55f07..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITalkActionFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface ITalkActionFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs deleted file mode 100644 index 4b8b1d20e..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Interfaces/Functions/ITileFunctions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT; - -public interface ITileFunctions : IBaseFunctions -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs index 5e8966e51..05555f2dd 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using NeoServer.Scripts.LuaJIT; -using NeoServer.Scripts.LuaJIT.Functions; +using NeoServer.Scripts.LuaJIT.LuaMappings; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; namespace NeoServer.Server.Standalone.IoC.Modules; @@ -15,19 +16,19 @@ public static IServiceCollection AddLuaJIT(this IServiceCollection builder) builder.AddSingleton(); builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); return builder; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index 91aefe2fb..81f2a0816 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -4,6 +4,7 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Configurations; using Serilog; @@ -40,19 +41,19 @@ public class LuaGameManager : ILuaGameManager private readonly IActions _actions; private readonly ITalkActions _talkActions; - private readonly IActionFunctions _actionFunctions; - private readonly IConfigFunctions _configFunctions; - private readonly ICreatureFunctions _creatureFunctions; - private readonly IEnumFunctions _enumFunctions; - private readonly IGameFunctions _gameFunctions; - private readonly IGlobalFunctions _globalFunctions; - private readonly IItemFunctions _itemFunctions; - private readonly IItemTypeFunctions _itemTypeFunctions; - private readonly ILoggerFunctions _loggerFunctions; - private readonly IPlayerFunctions _playerFunctions; - private readonly IPositionFunctions _positionFunctions; - private readonly ITalkActionFunctions _talkActionFunctions; - private readonly ITileFunctions _tileFunctions; + private readonly IActionLuaMapping _actionLuaMapping; + private readonly IConfigLuaMapping _configLuaMapping; + private readonly ICreatureLuaMapping _creatureLuaMapping; + private readonly IEnumLuaMapping _enumLuaMapping; + private readonly IGameLuaMapping _gameLuaMapping; + private readonly IGlobalLuaMapping _globalLuaMapping; + private readonly IItemLuaMapping _itemLuaMapping; + private readonly IItemTypeLuaMapping _itemTypeLuaMapping; + private readonly ILoggerLuaMapping _loggerLuaMapping; + private readonly IPlayerLuaMapping _playerLuaMapping; + private readonly IPositionLuaMapping _positionLuaMapping; + private readonly ITalkActionLuaMapping _talkActionLuaMapping; + private readonly ITileLuaMapping _tileLuaMapping; private readonly ServerConfiguration _serverConfiguration; @@ -67,19 +68,19 @@ public LuaGameManager( IScripts scripts, IActions actions, ITalkActions talkActions, - IActionFunctions actionFunctions, - IConfigFunctions configFunctions, - ICreatureFunctions creatureFunctions, - IEnumFunctions enumFunctions, - IGameFunctions gameFunctions, - IGlobalFunctions globalFunctions, - IItemFunctions itemFunctions, - IItemTypeFunctions itemTypeFunctions, - ILoggerFunctions loggerFunctions, - IPlayerFunctions playerFunctions, - IPositionFunctions positionFunctions, - ITalkActionFunctions talkActionFunctions, - ITileFunctions tileFunctions, + IActionLuaMapping actionLuaMapping, + IConfigLuaMapping configLuaMapping, + ICreatureLuaMapping creatureLuaMapping, + IEnumLuaMapping enumLuaMapping, + IGameLuaMapping gameLuaMapping, + IGlobalLuaMapping globalLuaMapping, + IItemLuaMapping itemLuaMapping, + IItemTypeLuaMapping itemTypeLuaMapping, + ILoggerLuaMapping loggerLuaMapping, + IPlayerLuaMapping playerLuaMapping, + IPositionLuaMapping positionLuaMapping, + ITalkActionLuaMapping talkActionLuaMapping, + ITileLuaMapping tileLuaMapping, ServerConfiguration serverConfiguration) { _logger = logger; @@ -90,19 +91,19 @@ public LuaGameManager( _actions = actions; _talkActions = talkActions; - _actionFunctions = actionFunctions; - _configFunctions = configFunctions; - _creatureFunctions = creatureFunctions; - _enumFunctions = enumFunctions; - _gameFunctions = gameFunctions; - _globalFunctions = globalFunctions; - _itemFunctions = itemFunctions; - _itemTypeFunctions = itemTypeFunctions; - _loggerFunctions = loggerFunctions; - _playerFunctions = playerFunctions; - _positionFunctions = positionFunctions; - _talkActionFunctions = talkActionFunctions; - _tileFunctions = tileFunctions; + _actionLuaMapping = actionLuaMapping; + _configLuaMapping = configLuaMapping; + _creatureLuaMapping = creatureLuaMapping; + _enumLuaMapping = enumLuaMapping; + _gameLuaMapping = gameLuaMapping; + _globalLuaMapping = globalLuaMapping; + _itemLuaMapping = itemLuaMapping; + _itemTypeLuaMapping = itemTypeLuaMapping; + _loggerLuaMapping = loggerLuaMapping; + _playerLuaMapping = playerLuaMapping; + _positionLuaMapping = positionLuaMapping; + _talkActionLuaMapping = talkActionLuaMapping; + _tileLuaMapping = tileLuaMapping; _serverConfiguration = serverConfiguration; } @@ -189,23 +190,23 @@ public void Start() var luaState = _luaEnviroment.GetLuaState(); if (luaState.IsNull) - _logger.Error("Invalid lua state, cannot load lua functions."); + _logger.Error("Invalid lua state, cannot load lua LuaMapping."); Lua.OpenLibs(luaState); - _actionFunctions.Init(luaState); - _configFunctions.Init(luaState); - _creatureFunctions.Init(luaState); - _enumFunctions.Init(luaState); - _gameFunctions.Init(luaState); - _globalFunctions.Init(luaState); - _itemFunctions.Init(luaState); - _itemTypeFunctions.Init(luaState); - _loggerFunctions.Init(luaState); - _playerFunctions.Init(luaState); - _positionFunctions.Init(luaState); - _talkActionFunctions.Init(luaState); - _tileFunctions.Init(luaState); + _actionLuaMapping.Init(luaState); + _configLuaMapping.Init(luaState); + _creatureLuaMapping.Init(luaState); + _enumLuaMapping.Init(luaState); + _gameLuaMapping.Init(luaState); + _globalLuaMapping.Init(luaState); + _itemLuaMapping.Init(luaState); + _itemTypeLuaMapping.Init(luaState); + _loggerLuaMapping.Init(luaState); + _playerLuaMapping.Init(luaState); + _positionLuaMapping.Init(luaState); + _talkActionLuaMapping.Init(luaState); + _tileLuaMapping.Init(luaState); ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ActionLuaMapping.cs similarity index 91% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ActionLuaMapping.cs index fb13d46a8..63d353dd3 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ActionLuaMapping.cs @@ -1,13 +1,14 @@ using LuaNET; using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class ActionFunctions : LuaScriptInterface, IActionFunctions +public class ActionLuaMapping : LuaScriptInterface, IActionLuaMapping { private static IActions _actions; - public ActionFunctions(IActions actions) : base(nameof(ActionFunctions)) + public ActionLuaMapping(IActions actions) : base(nameof(ActionLuaMapping)) { _actions = actions; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ConfigLuaMappingr.cs similarity index 91% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ConfigLuaMappingr.cs index 86ecc0ca4..c8bbcfc61 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ConfigLuaMappingr.cs @@ -1,16 +1,18 @@ using LuaNET; using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Configurations; -namespace NeoServer.Scripts.LuaJIT.Functions; -public class ConfigFunctions : LuaScriptInterface, IConfigFunctions +namespace NeoServer.Scripts.LuaJIT.LuaMappings; + +public class ConfigLuaMappingr : LuaScriptInterface, IConfigLuaMapping { private static IConfigManager _configManager; private ServerConfiguration _serverConfiguration; - public ConfigFunctions( + public ConfigLuaMappingr( IConfigManager configManager, - ServerConfiguration serverConfiguration) : base(nameof(ConfigFunctions)) + ServerConfiguration serverConfiguration) : base(nameof(ConfigLuaMappingr)) { _configManager = configManager; _serverConfiguration = serverConfiguration; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs similarity index 90% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs index e0243e7f0..41912077e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs @@ -1,15 +1,16 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Common.Contracts; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class CreatureFunctions : LuaScriptInterface, ICreatureFunctions +public class CreatureLuaMapping : LuaScriptInterface, ICreatureLuaMapping { private static IGameCreatureManager _gameCreatureManager; - public CreatureFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(CreatureFunctions)) + public CreatureLuaMapping(IGameCreatureManager gameCreatureManager) : base(nameof(CreatureLuaMapping)) { _gameCreatureManager = gameCreatureManager; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/EnumLuaMapping.cs similarity index 83% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/EnumLuaMapping.cs index b674eb52f..741057dff 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/EnumLuaMapping.cs @@ -1,14 +1,15 @@ using LuaNET; using NeoServer.Game.Common.Location; using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using Serilog; -namespace NeoServer.Scripts.LuaJIT.Functions; -public class EnumFunctions : LuaScriptInterface, IEnumFunctions +namespace NeoServer.Scripts.LuaJIT.LuaMappings; +public class EnumLuaMapping : LuaScriptInterface, IEnumLuaMapping { - public EnumFunctions( + public EnumLuaMapping( ILuaEnvironment luaEnvironment, - ILogger logger) : base(nameof(ConfigFunctions)) + ILogger logger) : base(nameof(ConfigLuaMappingr)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs similarity index 96% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs index 5cd13ff9b..319ae4d90 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs @@ -6,23 +6,24 @@ using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.Extensions; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Configurations; using NeoServer.Server.Helpers; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class GameFunctions : LuaScriptInterface, IGameFunctions +public class GameLuaMapping : LuaScriptInterface, IGameLuaMapping { private static IScripts _scripts; private static IItemTypeStore _itemTypeStore; private static IItemFactory _itemFactory; private static ServerConfiguration _serverConfiguration; - public GameFunctions( + public GameLuaMapping( IScripts scripts, IItemTypeStore itemTypeStore, IItemFactory itemFactory, - ServerConfiguration serverConfiguration) : base(nameof(GameFunctions)) + ServerConfiguration serverConfiguration) : base(nameof(GameLuaMapping)) { _scripts = scripts; _itemTypeStore = itemTypeStore; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GlobalLuaMapping.cs similarity index 58% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GlobalLuaMapping.cs index 58073c709..542ac3c9c 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GlobalLuaMapping.cs @@ -1,10 +1,11 @@ using LuaNET; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class GlobalFunctions : LuaScriptInterface, IGlobalFunctions +public class GlobalLuaMapping : LuaScriptInterface, IGlobalLuaMapping { - public GlobalFunctions() : base(nameof(GlobalFunctions)) + public GlobalLuaMapping() : base(nameof(GlobalLuaMapping)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs new file mode 100644 index 000000000..eb1a263da --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IActionLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs new file mode 100644 index 000000000..ee89af2f9 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs @@ -0,0 +1,8 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IBaseLuaMapping +{ + void Init(LuaState L); +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs new file mode 100644 index 000000000..0c6ff85b0 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IConfigLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs new file mode 100644 index 000000000..2425161ff --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface ICreatureLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs new file mode 100644 index 000000000..72605329b --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IEnumLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs new file mode 100644 index 000000000..4ae614399 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IGameLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs new file mode 100644 index 000000000..b25b8efc1 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IGlobalLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs new file mode 100644 index 000000000..02697cc80 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IItemLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs new file mode 100644 index 000000000..d3d134e8e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IItemTypeLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs new file mode 100644 index 000000000..b28efe7d3 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface ILoggerLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs new file mode 100644 index 000000000..6886fb2e7 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IPlayerLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs new file mode 100644 index 000000000..9e6923f0b --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IPositionLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs new file mode 100644 index 000000000..70e4a7a94 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface ITalkActionLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs new file mode 100644 index 000000000..28ff22851 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface ITileLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs similarity index 95% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs index 5b37c2b18..bc77a7ad8 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs @@ -3,17 +3,18 @@ using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Contracts.Services; using NeoServer.Game.Common.Item; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class ItemFunctions : LuaScriptInterface, IItemFunctions +public class ItemLuaMapping : LuaScriptInterface, IItemLuaMapping { private static IItemTransformService _itemTransformService; private static IItemTypeStore _itemTypeStore; - public ItemFunctions( + public ItemLuaMapping( IItemTransformService itemTransformService, - IItemTypeStore itemTypeStore) : base(nameof(ItemFunctions)) + IItemTypeStore itemTypeStore) : base(nameof(ItemLuaMapping)) { _itemTransformService = itemTransformService; _itemTypeStore = itemTypeStore; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemTypeLuaMapping.cs similarity index 89% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemTypeLuaMapping.cs index 17b2b2804..1ce7b018b 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemTypeLuaMapping.cs @@ -2,13 +2,14 @@ using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Item; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class ItemTypeFunctions : LuaScriptInterface, IItemTypeFunctions +public class ItemTypeLuaMapping : LuaScriptInterface, IItemTypeLuaMapping { private static IItemTypeStore _itemTypeStore; - public ItemTypeFunctions(IItemTypeStore itemTypeStore) : base(nameof(ItemTypeFunctions)) + public ItemTypeLuaMapping(IItemTypeStore itemTypeStore) : base(nameof(ItemTypeLuaMapping)) { _itemTypeStore = itemTypeStore; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/LoggerLuaMapping.cs similarity index 89% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/LoggerLuaMapping.cs index a70b4ce36..c7a14ad9e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/LoggerLuaMapping.cs @@ -1,14 +1,15 @@ using LuaNET; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using Serilog; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class LoggerFunctions : LuaScriptInterface, ILoggerFunctions +public class LoggerLuaMapping : LuaScriptInterface, ILoggerLuaMapping { private static ILogger _logger; - public LoggerFunctions( - ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(LoggerFunctions)) + public LoggerLuaMapping( + ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(LoggerLuaMapping)) { _logger = logger; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PlayerLuaMapping.cs similarity index 92% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PlayerLuaMapping.cs index b5b3d3841..1fa40a204 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PlayerLuaMapping.cs @@ -2,16 +2,17 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Networking.Packets.Outgoing; using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Common.Contracts; using NeoServer.Server.Services; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class PlayerFunctions : LuaScriptInterface, IPlayerFunctions +public class PlayerLuaMapping : LuaScriptInterface, IPlayerLuaMapping { private static IGameCreatureManager _gameCreatureManager; - public PlayerFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(PlayerFunctions)) + public PlayerLuaMapping(IGameCreatureManager gameCreatureManager) : base(nameof(PlayerLuaMapping)) { _gameCreatureManager = gameCreatureManager; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PositionLuaMapping.cs similarity index 93% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PositionLuaMapping.cs index 2fc63d44d..d2f67c188 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PositionLuaMapping.cs @@ -3,17 +3,18 @@ using NeoServer.Game.Common.Creatures; using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Services; using Serilog; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class PositionFunctions : LuaScriptInterface, IPositionFunctions +public class PositionLuaMapping : LuaScriptInterface, IPositionLuaMapping { private static ILogger _logger; private static IConfigManager _configManager; - public PositionFunctions(ILogger logger, IConfigManager configManager) : base(nameof(PositionFunctions)) + public PositionLuaMapping(ILogger logger, IConfigManager configManager) : base(nameof(PositionLuaMapping)) { _logger = logger; _configManager = configManager; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TalkActionLuaMapping.cs similarity index 93% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TalkActionLuaMapping.cs index 82047ba9f..908221154 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TalkActionLuaMapping.cs @@ -1,11 +1,12 @@ using LuaNET; using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class TalkActionFunctions : LuaScriptInterface, ITalkActionFunctions +public class TalkActionLuaMapping : LuaScriptInterface, ITalkActionLuaMapping { - public TalkActionFunctions() : base(nameof(TalkActionFunctions)) + public TalkActionLuaMapping() : base(nameof(TalkActionLuaMapping)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs similarity index 95% rename from src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs index e2722b5bd..2a9ac3058 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs @@ -4,15 +4,16 @@ using NeoServer.Game.Common.Item; using NeoServer.Game.Common.Location; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Common.Contracts; -namespace NeoServer.Scripts.LuaJIT.Functions; +namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class TileFunctions : LuaScriptInterface, ITileFunctions +public class TileLuaMapping : LuaScriptInterface, ITileLuaMapping { private static IGameServer _gameServer; - public TileFunctions(IGameServer gameServer) : base(nameof(TileFunctions)) + public TileLuaMapping(IGameServer gameServer) : base(nameof(TileLuaMapping)) { _gameServer = gameServer; } From d5813b14fdcf35c51d2a7930476ac3bac5fce647 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Tue, 24 Dec 2024 16:47:53 -0300 Subject: [PATCH 08/16] feat: removed all unused usings --- .../Player/UseItem/PlayerUseItemCommand.cs | 1 - .../Player/UseItem/PlayerUseItemOnCreatureCommand.cs | 1 - src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs | 1 - src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs | 1 - src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs | 1 - src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs | 1 - .../NeoServer.Game.Items/Factories/ItemFactory.cs | 1 - 7 files changed, 7 deletions(-) diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs index 52fe2d990..0fbd6ec18 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemCommand.cs @@ -6,7 +6,6 @@ using NeoServer.Game.Common.Contracts.Services; using NeoServer.Game.Common.Location; using NeoServer.Networking.Packets.Incoming; -using NeoServer.Networking.Packets.Incoming.Shop; using NeoServer.Server.Common.Contracts.Commands; namespace NeoServer.Server.Commands.Player.UseItem; diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs index 963f36266..d1d6db99e 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCreatureCommand.cs @@ -4,7 +4,6 @@ using NeoServer.Game.Common.Contracts.Items.Types.Usable; using NeoServer.Game.Common.Contracts.Services; using NeoServer.Game.Common.Location; -using NeoServer.Game.Items.Bases; using NeoServer.Networking.Packets.Incoming; using NeoServer.Server.Common.Contracts; using NeoServer.Server.Common.Contracts.Commands; diff --git a/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs b/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs index 1324c3fee..0f0d2206c 100644 --- a/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs +++ b/src/Database/NeoServer.Data.InMemory.DataStores/DataStore.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using NeoServer.Game.Common.Contracts.DataStores; namespace NeoServer.Data.InMemory.DataStores; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs index 7394e9397..ec8543972 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs @@ -1,6 +1,5 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; -using NeoServer.Game.Common.Contracts.World; using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Server.Helpers; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs index bef2a2e7a..be2df351f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaScriptInterface.cs @@ -1,5 +1,4 @@ using LuaNET; -using NeoServer.Server.Helpers; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs index 1e38a09ac..cc1433ff6 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Scripts.cs @@ -1,6 +1,5 @@ using NeoServer.Scripts.LuaJIT.Enums; using Serilog; -using System; namespace NeoServer.Scripts.LuaJIT; diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs b/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs index 870558770..a585129be 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/Factories/ItemFactory.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Security.Principal; using NeoServer.Game.Common.Contracts; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.DataStores; From 230de4b1f4211a98c4ab43fb4c4fb9d645a35e17 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Fri, 27 Dec 2024 18:58:46 -0300 Subject: [PATCH 09/16] feat: some lua funcions mapping implementations. --- data/LuaJit/libs/functions/creature.lua | 35 ++++ data/LuaJit/libs/functions/item.lua | 28 +++ data/LuaJit/libs/functions/load.lua | 1 + data/LuaJit/libs/functions/revscriptsys.lua | 2 +- .../scripts/talkactions/player/test.lua | 13 ++ .../Enums/ItemsDefinitions.cs | 2 +- .../IoC/Modules/LuaJITInjection.cs | 2 +- .../LuaGameManager.cs | 153 ++++++++++++----- .../LuaMappings/CreatureLuaMapping.cs | 13 ++ .../LuaMappings/GameLuaMapping.cs | 161 +++++++++++++----- .../Interfaces/IMonsterLuaMapping.cs | 5 + .../LuaMappings/ItemLuaMapping.cs | 106 +++++++++++- .../LuaMappings/MonsterLuaMapping.cs | 68 ++++++++ .../LuaMappings/TileLuaMapping.cs | 155 +++++++++++++---- .../Contracts/Creatures/IMonster.cs | 2 +- .../Contracts/Items/IThing.cs | 3 + .../Services/IItemMovementService.cs | 4 + .../Contracts/World/Tiles/IDynamicTile.cs | 2 + .../Location/Structs/Location.cs | 27 +++ .../Services/ItemMovementService.cs | 72 +++++++- 20 files changed, 721 insertions(+), 133 deletions(-) create mode 100644 data/LuaJit/libs/functions/creature.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/MonsterLuaMapping.cs diff --git a/data/LuaJit/libs/functions/creature.lua b/data/LuaJit/libs/functions/creature.lua new file mode 100644 index 000000000..a29d226cf --- /dev/null +++ b/data/LuaJit/libs/functions/creature.lua @@ -0,0 +1,35 @@ +function Creature.getMonster(self) + return self:isMonster() and self or nil +end + +function Creature.getPlayer(self) + return self:isPlayer() and self or nil +end + +function Creature.isContainer(self) + return false +end + +function Creature.isItem(self) + return false +end + +function Creature.isMonster(self) + return false +end + +function Creature.isNpc(self) + return false +end + +function Creature.isPlayer(self) + return false +end + +function Creature.isTeleport(self) + return false +end + +function Creature.isTile(self) + return false +end diff --git a/data/LuaJit/libs/functions/item.lua b/data/LuaJit/libs/functions/item.lua index 5c0be4570..df6eee392 100644 --- a/data/LuaJit/libs/functions/item.lua +++ b/data/LuaJit/libs/functions/item.lua @@ -1,3 +1,31 @@ function Item.getType(self) return ItemType(self:getId()) +end + +function Item.isContainer(self) + return false +end + +function Item.isCreature(self) + return false +end + +function Item.isMonster(self) + return false +end + +function Item.isNpc(self) + return false +end + +function Item.isPlayer(self) + return false +end + +function Item.isTeleport(self) + return false +end + +function Item.isTile(self) + return false end \ No newline at end of file diff --git a/data/LuaJit/libs/functions/load.lua b/data/LuaJit/libs/functions/load.lua index 47d9a8374..84cac8af9 100644 --- a/data/LuaJit/libs/functions/load.lua +++ b/data/LuaJit/libs/functions/load.lua @@ -1,4 +1,5 @@ -- Load core functions +dofile(CORE_DIRECTORY .. "/libs/functions/creature.lua") dofile(CORE_DIRECTORY .. "/libs/functions/item.lua") dofile(CORE_DIRECTORY .. "/libs/functions/player.lua") dofile(CORE_DIRECTORY .. "/libs/functions/position.lua") diff --git a/data/LuaJit/libs/functions/revscriptsys.lua b/data/LuaJit/libs/functions/revscriptsys.lua index 1c8494076..ffc888457 100644 --- a/data/LuaJit/libs/functions/revscriptsys.lua +++ b/data/LuaJit/libs/functions/revscriptsys.lua @@ -54,7 +54,7 @@ do return methods[key] end rawgetmetatable("Player").__index = CreatureIndex - -- rawgetmetatable("Monster").__index = CreatureIndex + rawgetmetatable("Monster").__index = CreatureIndex -- rawgetmetatable("Npc").__index = CreatureIndex end diff --git a/data/LuaJit/scripts/talkactions/player/test.lua b/data/LuaJit/scripts/talkactions/player/test.lua index 21977bd7f..cd155dc5b 100644 --- a/data/LuaJit/scripts/talkactions/player/test.lua +++ b/data/LuaJit/scripts/talkactions/player/test.lua @@ -31,6 +31,19 @@ function talkAction.onSay(player, words, param) Game.createItem(2159, 1, position) + local direction = player:getDirection() + + Game.createMonster("Rat", position, true, true, player) + + logger.info('direction: ' .. direction) + + local nextPosition = position + nextPosition:getNextPosition(direction) + + logger.info('nextPosition: ' .. nextPosition:toString()) + + Game.createMonster("Scarab", nextPosition) + logger.info('end') return true diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ItemsDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ItemsDefinitions.cs index c7ffe28eb..74b90863b 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ItemsDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/ItemsDefinitions.cs @@ -433,7 +433,7 @@ public enum ZoneTypeType ZONE_NORMAL, }; -public enum CylinderFlagsType +public enum CylinderFlagsType : uint { FLAG_NOLIMIT = 1 << 0, // Bypass limits like capacity/container limits, blocking items/creatures etc. FLAG_IGNOREBLOCKITEM = 1 << 1, // Bypass movable blocking item checks diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs index 05555f2dd..1130e1118 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs @@ -9,7 +9,6 @@ public static class LuaJITInjection { public static IServiceCollection AddLuaJIT(this IServiceCollection builder) { - //LuaJIT todo: move this to lUaScripts builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); @@ -25,6 +24,7 @@ public static IServiceCollection AddLuaJIT(this IServiceCollection builder) builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index 81f2a0816..fcc36ca00 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -16,45 +16,111 @@ public class LuaGameManager : ILuaGameManager #endregion - #region Injection + #region Dependency Injections /// - /// A reference to the logger instance in use. + /// A reference to the instance in use. /// private readonly ILogger _logger; /// - /// A reference to the lua enviroment instance in use. + /// A reference to the instance in use. /// private readonly ILuaEnvironment _luaEnviroment; /// - /// A reference to the config manager instance in use. + /// A reference to the instance in use. /// private readonly IConfigManager _configManager; /// - /// A reference to the scripts instance in use. + /// A reference to the instance in use. /// private readonly IScripts _scripts; + /// + /// A reference to the instance in use. + /// private readonly IActions _actions; + + /// + /// A reference to the instance in use. + /// private readonly ITalkActions _talkActions; + /// + /// A reference to the instance in use. + /// private readonly IActionLuaMapping _actionLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IConfigLuaMapping _configLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly ICreatureLuaMapping _creatureLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IEnumLuaMapping _enumLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IGameLuaMapping _gameLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IGlobalLuaMapping _globalLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IItemLuaMapping _itemLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IItemTypeLuaMapping _itemTypeLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly ILoggerLuaMapping _loggerLuaMapping; + + /// + /// A reference to the instance in use. + /// + private readonly IMonsterLuaMapping _monsterLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IPlayerLuaMapping _playerLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly IPositionLuaMapping _positionLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly ITalkActionLuaMapping _talkActionLuaMapping; + + /// + /// A reference to the instance in use. + /// private readonly ITileLuaMapping _tileLuaMapping; + /// + /// A reference to the instance in use. + /// private readonly ServerConfiguration _serverConfiguration; #endregion @@ -77,6 +143,7 @@ public LuaGameManager( IItemLuaMapping itemLuaMapping, IItemTypeLuaMapping itemTypeLuaMapping, ILoggerLuaMapping loggerLuaMapping, + IMonsterLuaMapping monsterLuaMapping, IPlayerLuaMapping playerLuaMapping, IPositionLuaMapping positionLuaMapping, ITalkActionLuaMapping talkActionLuaMapping, @@ -101,6 +168,7 @@ public LuaGameManager( _itemTypeLuaMapping = itemTypeLuaMapping; _loggerLuaMapping = loggerLuaMapping; _playerLuaMapping = playerLuaMapping; + _monsterLuaMapping = monsterLuaMapping; _positionLuaMapping = positionLuaMapping; _talkActionLuaMapping = talkActionLuaMapping; _tileLuaMapping = tileLuaMapping; @@ -112,6 +180,44 @@ public LuaGameManager( #region Public Methods + public void Start() + { + var dir = AppContext.BaseDirectory; + + if (!string.IsNullOrEmpty(ArgManager.GetInstance().ExePath)) + dir = ArgManager.GetInstance().ExePath; + + ModulesLoadHelper(_luaEnviroment.InitState(), "luaEnviroment"); + + var luaState = _luaEnviroment.GetLuaState(); + + if (luaState.IsNull) + _logger.Error("Invalid lua state, cannot load lua LuaMapping."); + + Lua.OpenLibs(luaState); + + _actionLuaMapping.Init(luaState); + _configLuaMapping.Init(luaState); + _creatureLuaMapping.Init(luaState); + _enumLuaMapping.Init(luaState); + _gameLuaMapping.Init(luaState); + _globalLuaMapping.Init(luaState); + _itemLuaMapping.Init(luaState); + _itemTypeLuaMapping.Init(luaState); + _loggerLuaMapping.Init(luaState); + _monsterLuaMapping.Init(luaState); + _playerLuaMapping.Init(luaState); + _positionLuaMapping.Init(luaState); + _talkActionLuaMapping.Init(luaState); + _tileLuaMapping.Init(luaState); + + ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); + + ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}{_serverConfiguration.DataLuaJit}/core.lua", "core.lua"), "core.lua"); + + ModulesLoadHelper(_scripts.LoadScripts($"{dir}{_serverConfiguration.DataLuaJit}/scripts", false, false), "/Data/LuaJit/scripts"); + } + public bool PlayerSaySpell(IPlayer player, SpeechType type, string words) { var wordsSeparator = " "; @@ -178,43 +284,6 @@ public bool PlayerUseItemEx(IPlayer player, Location fromPos, Location toPos, by #region Private Methods - public void Start() - { - var dir = AppContext.BaseDirectory; - - if (!string.IsNullOrEmpty(ArgManager.GetInstance().ExePath)) - dir = ArgManager.GetInstance().ExePath; - - ModulesLoadHelper(_luaEnviroment.InitState(), "luaEnviroment"); - - var luaState = _luaEnviroment.GetLuaState(); - - if (luaState.IsNull) - _logger.Error("Invalid lua state, cannot load lua LuaMapping."); - - Lua.OpenLibs(luaState); - - _actionLuaMapping.Init(luaState); - _configLuaMapping.Init(luaState); - _creatureLuaMapping.Init(luaState); - _enumLuaMapping.Init(luaState); - _gameLuaMapping.Init(luaState); - _globalLuaMapping.Init(luaState); - _itemLuaMapping.Init(luaState); - _itemTypeLuaMapping.Init(luaState); - _loggerLuaMapping.Init(luaState); - _playerLuaMapping.Init(luaState); - _positionLuaMapping.Init(luaState); - _talkActionLuaMapping.Init(luaState); - _tileLuaMapping.Init(luaState); - - ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); - - ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}{_serverConfiguration.DataLuaJit}/core.lua", "core.lua"), "core.lua"); - - ModulesLoadHelper(_scripts.LoadScripts($"{dir}{_serverConfiguration.DataLuaJit}/scripts", false, false), "/Data/LuaJit/scripts"); - } - private void ModulesLoadHelper(bool loaded, string moduleName) { _logger.Information($"Loaded {moduleName}"); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs index 41912077e..d4e269084 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs @@ -19,9 +19,11 @@ public void Init(LuaState L) { RegisterSharedClass(L, "Creature", "", LuaCreatureCreate); RegisterMetaMethod(L, "Creature", "__eq", LuaUserdataCompare); + RegisterMethod(L, "Creature", "getId", LuaGetId); RegisterMethod(L, "Creature", "getName", LuaGetName); RegisterMethod(L, "Creature", "getPosition", LuaCreatureGetPosition); + RegisterMethod(L, "Creature", "getDirection", LuaCreatureGetDirection); } private static int LuaCreatureCreate(LuaState L) @@ -103,4 +105,15 @@ private static int LuaCreatureGetPosition(LuaState L) Lua.PushNil(L); return 1; } + + private static int LuaCreatureGetDirection(LuaState L) + { + // creature:getDirection() + var creature = GetUserdata(L, 1); + if (creature != null) + Lua.PushNumber(L, (byte)creature.Direction); + else + Lua.PushNil(L); + return 1; + } } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs index 319ae4d90..4bbeb0e85 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs @@ -1,8 +1,10 @@ using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Contracts.World; using NeoServer.Game.Common.Contracts.World.Tiles; +using NeoServer.Game.Common.Helpers; using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.Extensions; @@ -34,9 +36,13 @@ public GameLuaMapping( public void Init(LuaState L) { RegisterTable(L, "Game"); + RegisterMethod(L, "Game", "getReturnMessage", LuaGameGetReturnMessage); - RegisterMethod(L, "Game", "reload", LuaGameReload); + RegisterMethod(L, "Game", "createItem", LuaGameCreateItem); + RegisterMethod(L, "Game", "createMonster", LuaGameCreateMonster); + + RegisterMethod(L, "Game", "reload", LuaGameReload); } private static int LuaGameGetReturnMessage(LuaState L) @@ -47,52 +53,6 @@ private static int LuaGameGetReturnMessage(LuaState L) return 1; } - private static int LuaGameReload(LuaState L) - { - // Game.reload(reloadType) - var reloadType = GetNumber(L, 1); - if (reloadType == ReloadType.RELOAD_TYPE_NONE) - { - ReportError(nameof(LuaGameReload), "Reload type is none"); - PushBoolean(L, false); - return 0; - } - - if (reloadType >= ReloadType.RELOAD_TYPE_LAST) - { - ReportError(nameof(LuaGameReload), "Reload type not exist"); - PushBoolean(L, false); - return 0; - } - try - { - switch (reloadType) - { - case ReloadType.RELOAD_TYPE_SCRIPTS: - { - var dir = AppContext.BaseDirectory + _serverConfiguration.DataLuaJit; - _scripts.ClearAllScripts(); - _scripts.LoadScripts($"{dir}/scripts", false, true); - - Lua.GC(LuaEnvironment.GetInstance().GetLuaState(), LuaGCParam.Collect, 0); - } - - break; - default: - ReportError(nameof(LuaGameReload), "Reload type not implemented"); - break; - } - } - catch (Exception e) - { - PushBoolean(L, false); - return 0; - } - - PushBoolean(L, true); - return 1; - } - private static int LuaGameCreateItem(LuaState L) { // Game.createItem(itemId or name[, count[, position]]) @@ -220,4 +180,111 @@ private static int LuaGameCreateItem(LuaState L) return 1; } + + private static int LuaGameCreateMonster(LuaState L) + { + // Game.createMonster(monsterName, position[, extended = false[, force = false[, master = nil]]]) + //todo: implements force parameter + + IMonsterFactory _monsterFactory = IoC.GetInstance(); + IMap _map = IoC.GetInstance(); + + var monsterName = GetString(L, 1); + + var position = GetPosition(L, 2); + var extended = GetBoolean(L, 3, false); + var force = GetBoolean(L, 4, false); + + ICreature master = null; + + bool isSummon = false; + if (Lua.GetTop(L) >= 5) + { + master = GetUserdata(L, 5); + if (master.IsNotNull()) + { + isSummon = true; + } + } + + IMonster monster = null; + if (isSummon && master != null) + { + monster = _monsterFactory.CreateSummon(monsterName, master as IMonster); + monster.SetNewLocation(master.Location); + } + else + { + monster = _monsterFactory.Create(monsterName); + monster.SetNewLocation(position); + } + + if (!monster) + { + Lua.PushNil(L); + return 1; + } + + foreach (var neighbour in extended ? monster.Location.ExtendedNeighbours : monster.Location.Neighbours) + if (_map[neighbour] is IDynamicTile { HasCreature: false }) + { + monster.SetNewLocation(neighbour); + _map.PlaceCreature(monster); + + PushUserdata(L, monster); + SetMetatable(L, -1, "Monster"); + + return 1; + } + + Lua.PushNil(L); + return 1; + } + + private static int LuaGameReload(LuaState L) + { + // Game.reload(reloadType) + var reloadType = GetNumber(L, 1); + if (reloadType == ReloadType.RELOAD_TYPE_NONE) + { + ReportError(nameof(LuaGameReload), "Reload type is none"); + PushBoolean(L, false); + return 0; + } + + if (reloadType >= ReloadType.RELOAD_TYPE_LAST) + { + ReportError(nameof(LuaGameReload), "Reload type not exist"); + PushBoolean(L, false); + return 0; + } + try + { + switch (reloadType) + { + case ReloadType.RELOAD_TYPE_SCRIPTS: + { + var dir = AppContext.BaseDirectory + _serverConfiguration.DataLuaJit; + _scripts.ClearAllScripts(); + _scripts.LoadScripts($"{dir}/scripts", false, true); + + Lua.GC(LuaEnvironment.GetInstance().GetLuaState(), LuaGCParam.Collect, 0); + } + + break; + default: + ReportError(nameof(LuaGameReload), "Reload type not implemented"); + break; + } + } + catch (Exception e) + { + PushBoolean(L, false); + return 0; + } + + PushBoolean(L, true); + return 1; + } + } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs new file mode 100644 index 000000000..09097a4a4 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; + +public interface IMonsterLuaMapping : IBaseLuaMapping +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs index bc77a7ad8..bc470afe3 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs @@ -1,8 +1,13 @@ using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Contracts.Items.Types.Containers; using NeoServer.Game.Common.Contracts.Services; +using NeoServer.Game.Common.Contracts.World; +using NeoServer.Game.Common.Contracts.World.Tiles; using NeoServer.Game.Common.Item; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; namespace NeoServer.Scripts.LuaJIT.LuaMappings; @@ -11,24 +16,37 @@ public class ItemLuaMapping : LuaScriptInterface, IItemLuaMapping { private static IItemTransformService _itemTransformService; private static IItemTypeStore _itemTypeStore; + private static IMap _map; + private static IItemMovementService _itemMovementService; public ItemLuaMapping( IItemTransformService itemTransformService, - IItemTypeStore itemTypeStore) : base(nameof(ItemLuaMapping)) + IItemTypeStore itemTypeStore, + IMap map, + IItemMovementService itemMovementService) : base(nameof(ItemLuaMapping)) + { _itemTransformService = itemTransformService; _itemTypeStore = itemTypeStore; + _map = map; + _itemMovementService = itemMovementService; } public void Init(LuaState L) { RegisterSharedClass(L, "Item", "", LuaCreateItem); RegisterMetaMethod(L, "Item", "__eq", LuaUserdataCompare); + RegisterMethod(L, "Item", "getId", LuaGetItemId); + RegisterMethod(L, "Item", "getActionId", LuaGetActionId); RegisterMethod(L, "Item", "getUniqueId", LuaGetUniqueId); + RegisterMethod(L, "Item", "getSubType", LuaGetSubType); + RegisterMethod(L, "Item", "hasProperty", LuaItemHasProperty); + + RegisterMethod(L, "Item", "moveTo", LuaItemMoveTo); RegisterMethod(L, "Item", "transform", LuaItemTransform); RegisterMethod(L, "Item", "decay", LuaItemDecay); } @@ -115,6 +133,90 @@ public static int LuaItemHasProperty(LuaState L) return 1; } + public static int LuaItemMoveTo(LuaState L) + { + // item:moveTo(position or cylinder[, flags]) + //todo: implements flags + var item = GetUserdata(L, 1); + if (item == null) + { + Lua.PushNil(L); + return 1; + } + + //const auto &item = *itemPtr; + //if (!item || item->isRemoved()) + //{ + // lua_pushnil(L); + // return 1; + //} + + IContainer toContainer = null; + IPlayer toPlayer = null; + ITile toTile = null; + + ushort itemId = 0; + if (Lua.IsUserData(L, 2)) + { + LuaDataType type = GetUserdataType(L, 2); + switch (type) + { + case LuaDataType.Container: + toContainer = GetUserdata(L, 2); + break; + case LuaDataType.Player: + toPlayer = GetUserdata(L, 2); + break; + case LuaDataType.Tile: + toTile = GetUserdata(L, 2); + break; + default: + break; + } + } + else + { + toTile = _map.GetTile(GetPosition(L, 2)); + } + + if (toContainer == null && + toPlayer == null && + toTile == null) + { + Lua.PushNil(L); + return 1; + } + + if (item.Parent != null && + (item.Parent == toContainer || + item.Parent == toPlayer || + item.Parent == toTile)) + { + Lua.PushBoolean(L, true); + return 1; + } + + var fromTile = _map.GetTile(item.Location); + + if (toTile is not IDynamicTile dynamicToTile || + fromTile is not IDynamicTile dynamicFromTile) + { + Lua.PushBoolean(L, true); + return 1; + } + + dynamicFromTile.TryGetStackPositionOfItem(item, out var stackPosition); + dynamicToTile.TryGetStackPositionOfItem(dynamicToTile.TopItemOnStack, out var stackPositionTopItem); + var result = _itemMovementService.Move(item, dynamicFromTile, dynamicToTile, item.Amount, stackPosition, (byte)(dynamicToTile.ItemsCount + 1)); + + if (result.Succeeded) + Lua.PushBoolean(L, true); + else + Lua.PushBoolean(L, false); + + return 1; + } + public static int LuaItemTransform(LuaState L) { // item:transform(itemId[, count/subType = -1]) @@ -134,7 +236,7 @@ public static int LuaItemTransform(LuaState L) { var itemName = GetString(L, 2); var itemTypeByName = _itemTypeStore.GetByName(itemName); - + if (itemTypeByName == null) { Lua.PushNil(L); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/MonsterLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/MonsterLuaMapping.cs new file mode 100644 index 000000000..ff70076c7 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/MonsterLuaMapping.cs @@ -0,0 +1,68 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Server.Common.Contracts; + +namespace NeoServer.Scripts.LuaJIT.LuaMappings; + +public class MonsterLuaMapping : LuaScriptInterface, IMonsterLuaMapping +{ + private static IGameCreatureManager _gameCreatureManager; + + public MonsterLuaMapping(IGameCreatureManager gameCreatureManager) : base(nameof(MonsterLuaMapping)) + { + _gameCreatureManager = gameCreatureManager; + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Monster", "Creature", LuaMonsterCreate); + RegisterMetaMethod(L, "Monster", "__eq", LuaUserdataCompare); + } + + private static int LuaMonsterCreate(LuaState L) + { + // Monster(id or userdata) + IMonster monster = null; + if (IsNumber(L, 2)) + { + var id = GetNumber(L, 2); + _gameCreatureManager.TryGetCreature(id, out var creature); + + if (creature != null && creature is IMonster) + { + monster = creature as IMonster; + } + else + { + Lua.PushNil(L); + return 1; + } + } + else if (IsUserdata(L, 2)) + { + if (GetUserdataType(L, 2) != LuaDataType.Monster) + { + Lua.PushNil(L); + return 1; + } + monster = GetUserdata(L, 2); + } + else + { + monster = null; + } + + if (monster is not null) + { + PushUserdata(L, monster); + SetMetatable(L, -1, "Monster"); + } + else + { + Lua.PushNil(L); + } + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs index 2a9ac3058..8d4870878 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs @@ -1,9 +1,11 @@ using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Contracts.World.Tiles; using NeoServer.Game.Common.Item; using NeoServer.Game.Common.Location; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; using NeoServer.Server.Common.Contracts; @@ -22,14 +24,21 @@ public void Init(LuaState L) { RegisterSharedClass(L, "Tile", "", LuaCreateTile); RegisterMetaMethod(L, "Tile", "__eq", LuaUserdataCompare); + RegisterMethod(L, "Tile", "getPosition", LuaGetPosition); RegisterMethod(L, "Tile", "getGround", LuaGetGround); + RegisterMethod(L, "Tile", "getThing", LuaTileGetThing); + RegisterMethod(L, "Tile", "getThingCount", LuaTileGetThingCount); + RegisterMethod(L, "Tile", "getTopVisibleThing", LuaTileGetTopVisibleThing); + RegisterMethod(L, "Tile", "getItems", LuaTileGetItems); RegisterMethod(L, "Tile", "getItemCount", LuaTileGetItemCount); + RegisterMethod(L, "Tile", "hasProperty", LuaTileHasProperty); RegisterMethod(L, "Tile", "hasFlag", LuaTileHasFlag); - RegisterMethod(L, "Tile", "getThing", LuaTileGetThing); - RegisterMethod(L, "Tile", "getThingCount", LuaTileGetThingCount); + + RegisterMethod(L, "Tile", "queryAdd", LuaTileQueryAdd); + } public static int LuaCreateTile(LuaState L) @@ -59,6 +68,90 @@ public static int LuaCreateTile(LuaState L) return 1; } + public static int LuaTileGetThing(LuaState L) + { + // tile:getThing(index) + var tile = GetUserdata(L, 1); + var index = GetNumber(L, 2); + + if (tile == null || tile is not IDynamicTile dynamicTile) + { + Lua.PushNil(L); + return 1; + } + + if (dynamicTile.Creatures.Count >= index + 1) + { + var creature = dynamicTile.Creatures[index]; + if (creature != null) + { + PushUserdata(L, creature); + SetCreatureMetatable(L, -1, creature); + return 1; + } + } + else if (dynamicTile.AllItems.Count() >= index + 1) + { + var item = dynamicTile.AllItems[index]; + + if (item != null) + { + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + return 1; + } + } + + Lua.PushNil(L); + + return 1; + } + + public static int LuaTileGetThingCount(LuaState L) + { + // tile:getThingCount() + var tile = GetUserdata(L, 1); + if (tile != null) + Lua.PushNumber(L, tile.ThingsCount); + else + Lua.PushNil(L); + return 1; + } + + public static int LuaTileGetTopVisibleThing(LuaState L) + { + // tile:getTopVisibleThing(creature) + var creature = GetUserdata(L, 2); + var tile = GetUserdata(L, 1); + + if (tile == null || tile is not IDynamicTile dynamicTile) + { + Lua.PushNil(L); + return 1; + } + + var visibleCreature = dynamicTile.Creatures.FirstOrDefault(c => c.CreatureId == creature.CreatureId); + if (visibleCreature != null) + { + PushUserdata(L, visibleCreature); + SetCreatureMetatable(L, -1, visibleCreature); + return 1; + } + + var visibleItem = dynamicTile.TopItemOnStack; + + if (visibleItem != null) + { + PushUserdata(L, visibleItem); + SetItemMetatable(L, -1, visibleItem); + return 1; + } + + Lua.PushNil(L); + + return 1; + } + public static int LuaGetPosition(LuaState L) { // tile:getPosition() @@ -129,7 +222,7 @@ public static int LuaTileHasProperty(LuaState L) // tile:hasProperty(property[, item]) var tile = GetUserdata(L, 1); - if(tile == null) + if (tile == null) { Lua.PushNil(L); return 1; @@ -166,55 +259,43 @@ public static int LuaTileHasFlag(LuaState L) return 1; } - public static int LuaTileGetThing(LuaState L) + public static int LuaTileQueryAdd(LuaState L) { - // tile:getThing(index) - var tile = GetUserdata(L, 1); - var index = GetNumber(L, 2); + // tile:queryAdd(thing[, flags]) + //todo: implements flags - if (tile == null) + var tile = GetUserdata(L, 1); + if (!tile) { Lua.PushNil(L); return 1; } - var dynamicTile = (IDynamicTile)tile; - - if (dynamicTile.Creatures.Count >= index + 1) + var thing = GetUserdata(L, 2); + if (thing is not null && tile is IDynamicTile dynamicTile) { - var creature = dynamicTile.Creatures[index]; - if (creature != null) + var flags = GetNumber(L, 3, 0); + + var returnValue = ReturnValueType.RETURNVALUE_NOTPOSSIBLE; + + if (thing is ICreature creature) { - PushUserdata(L, creature); - SetCreatureMetatable(L, -1, creature); - return 1; + if (dynamicTile.CanEnter(creature)) + returnValue = ReturnValueType.RETURNVALUE_NOERROR; } - } - else if (dynamicTile.AllItems.Count() >= index + 1) - { - var item = dynamicTile.AllItems[index]; - - if (item != null) + else if (thing is IItem item) { - PushUserdata(L, item); - SetItemMetatable(L, -1, item); - return 1; + if (dynamicTile.CanAddItem(item, item.Amount).Succeeded) + returnValue = ReturnValueType.RETURNVALUE_NOERROR; } - } - Lua.PushNil(L); - - return 1; - } - - public static int LuaTileGetThingCount(LuaState L) - { - // tile:getThingCount() - var tile = GetUserdata(L, 1); - if (tile != null) - Lua.PushNumber(L, tile.ThingsCount); + Lua.PushNumber(L, (byte)returnValue); + } else + { Lua.PushNil(L); + } + return 1; } } diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonster.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonster.cs index d7ab28dcd..0f234eb28 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonster.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonster.cs @@ -88,4 +88,4 @@ public interface IMonster : IWalkableMonster, ICombatActor void Escape(); void Born(Location.Structs.Location location); void Summon(ISummonService summonService); -} \ No newline at end of file +} diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IThing.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IThing.cs index 0ddd2188e..0e97163bc 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IThing.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IThing.cs @@ -24,4 +24,7 @@ public bool IsCloseTo(IThing thing) } void SetNewLocation(Location.Structs.Location location); + + static bool operator !(IThing thing) + => thing is null; } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Services/IItemMovementService.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Services/IItemMovementService.cs index 26a3d0d82..f001b0b02 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Services/IItemMovementService.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Services/IItemMovementService.cs @@ -9,4 +9,8 @@ public interface IItemMovementService Result> Move(IPlayer player, IItem item, IHasItem from, IHasItem destination, byte amount, byte fromPosition, byte? toPosition); + + Result> Move(IItem item, IHasItem from, IHasItem destination, + byte amount, + byte fromPosition, byte? toPosition); } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/World/Tiles/IDynamicTile.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/World/Tiles/IDynamicTile.cs index ef1ef616d..fb10eeb69 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/World/Tiles/IDynamicTile.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/World/Tiles/IDynamicTile.cs @@ -25,11 +25,13 @@ public interface IDynamicTile : ITile, IHasItem List Players { get; } Func CanEnterFunction { get; set; } IItem[] AllItems { get; } + int ItemsCount { get; } bool HasTeleport(out ITeleport teleport); byte[] GetRaw(IPlayer playerRequesting = null); ICreature GetTopVisibleCreature(ICreature creature); bool TryGetStackPositionOfItem(IItem item, out byte stackPosition); + event AddCreatureToTile CreatureAdded; IItem[] RemoveAllItems(); ICreature[] RemoveAllCreatures(); diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Location/Structs/Location.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Location/Structs/Location.cs index 74e89eb47..d88500b16 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Location/Structs/Location.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Location/Structs/Location.cs @@ -305,6 +305,33 @@ public Location[] Neighbours } } + public Location[] ExtendedNeighbours + { + get + { + var pool = ArrayPool.Shared; + var locations = pool.Rent(12); + + locations[0] = (Translate() + new Coordinate(0, -2, 0)).Location; + locations[1] = (Translate() + new Coordinate(-1, -1, 0)).Location; + locations[2] = (Translate() + new Coordinate(0, -1, 0)).Location; + locations[3] = (Translate() + new Coordinate(1, -1, 0)).Location; + locations[4] = (Translate() + new Coordinate(-2, 0, 0)).Location; + locations[5] = (Translate() + new Coordinate(-1, 0, 0)).Location; + locations[6] = (Translate() + new Coordinate(1, 0, 0)).Location; + locations[7] = (Translate() + new Coordinate(2, 0, 0)).Location; + locations[8] = (Translate() + new Coordinate(-1, 1, 0)).Location; + locations[9] = (Translate() + new Coordinate(0, 1, 0)).Location; + locations[10] = (Translate() + new Coordinate(1, 1, 0)).Location; + locations[11] = (Translate() + new Coordinate(0, 2, 0)).Location; + + pool.Return(locations); + + return locations[..12]; + } + } + + public static Location Zero => new(0, 0, 0); public static Location Inventory(Slot slot) diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/ItemMovementService.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/ItemMovementService.cs index b105cb436..45dc1703e 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/ItemMovementService.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Services/ItemMovementService.cs @@ -1,11 +1,15 @@ -using NeoServer.Game.Common.Contracts; +using NeoServer.Game.Common; +using NeoServer.Game.Common.Contracts; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Contracts.Items.Types; +using NeoServer.Game.Common.Contracts.Items.Types.Containers; using NeoServer.Game.Common.Contracts.Services; using NeoServer.Game.Common.Location; using NeoServer.Game.Common.Results; using NeoServer.Game.Common.Services; using NeoServer.Game.Common.Texts; +using System; namespace NeoServer.Game.Creatures.Services; @@ -48,4 +52,70 @@ public Result> Move(IPlayer player, IItem item, IHasI return player.MoveItem(item, from, destination, amount, fromPosition, toPosition); } + + + public Result> Move(IItem item, IHasItem from, IHasItem destination, byte amount, + byte fromPosition, byte? toPosition) + { + if (!item.CanBeMoved) return Result>.NotPossible; + + var canAdd = destination.CanAddItem(item, amount, toPosition); + if (!canAdd.Succeeded) return new Result>(canAdd.Error); + + (destination, toPosition) = GetDestination(from, destination, toPosition); + + var possibleAmountToAdd = destination.PossibleAmountToAdd(item, toPosition); + if (possibleAmountToAdd == 0) return new Result>(InvalidOperation.NotEnoughRoom); + + var removedItem = RemoveItem(item, from, amount, fromPosition, possibleAmountToAdd); + + var result = AddToDestination(removedItem, from, destination, toPosition); + + if (result.Succeeded && item is IMovableThing movableThing && destination is IThing destinationThing) + movableThing.OnMoved(destinationThing); + + var amountResult = (byte)Math.Max(0, amount - (int)possibleAmountToAdd); + return amountResult > 0 ? Move(item, from, destination, amountResult, fromPosition, toPosition) : result; + } + + private static IItem RemoveItem(IItem item, IHasItem from, byte amount, byte fromPosition, uint possibleAmountToAdd) + { + var amountToRemove = item is not ICumulative ? (byte)1 : (byte)Math.Min(amount, possibleAmountToAdd); + + from.RemoveItem(item, amountToRemove, fromPosition, out var removedThing); + + return removedThing; + } + + private static Result> AddToDestination(IItem thing, IHasItem source, + IHasItem destination, + byte? toPosition) + { + var canAdd = destination.CanAddItem(thing, thing.Amount, toPosition); + if (!canAdd.Succeeded) return new Result>(canAdd.Error); + + var result = destination.AddItem(thing, toPosition); + + if (!result.Value.HasAnyOperation) return result; + + foreach (var operation in result.Value.Operations) + if (operation.Item2 == Operation.Removed) + source.AddItem(operation.Item1); + + return result; + } + + + private (IHasItem, byte?) GetDestination(IHasItem source, IHasItem destination, + byte? toPosition) + { + if (source is not IContainer sourceContainer) return (destination, toPosition); + if (destination is not IContainer) return (destination, toPosition); + + if (destination == source && toPosition is not null && + sourceContainer.GetContainerAt(toPosition.Value, out var container)) + return (container, null); + + return (destination, toPosition); + } } \ No newline at end of file From b09f2c36bc748cea593efe587641477eed325013 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Fri, 27 Dec 2024 19:14:50 -0300 Subject: [PATCH 10/16] feat: refact renaming LuaMappings to Functions --- .../ActionFunctions.cs} | 8 +- .../ConfigFunctions.cs} | 10 +- .../CreatureFunctions.cs} | 8 +- .../EnumFunctions.cs} | 10 +- .../GameFunctions.cs} | 10 +- .../GlobalFunctions.cs} | 8 +- .../Functions/Interfaces/IActionFunctions.cs | 5 + .../Functions/Interfaces/IBaseFunctions.cs | 8 + .../Functions/Interfaces/IConfigFunctions.cs | 5 + .../Interfaces/ICreatureFunctions.cs | 5 + .../Functions/Interfaces/IEnumFunctions.cs | 5 + .../Functions/Interfaces/IGameFunctions.cs | 5 + .../Functions/Interfaces/IGlobalFunctions.cs | 5 + .../Functions/Interfaces/IItemFunctions.cs | 5 + .../Interfaces/IItemTypeFunctions.cs | 5 + .../Functions/Interfaces/ILoggerFunctions.cs | 5 + .../Functions/Interfaces/IMonsterFunctions.cs | 5 + .../Functions/Interfaces/IPlayerFunctions.cs | 5 + .../Interfaces/IPositionFunctions.cs | 5 + .../Interfaces/ITalkActionFunctions.cs | 5 + .../Functions/Interfaces/ITileFunctions.cs | 5 + .../ItemFunctions.cs} | 10 +- .../ItemTypeFunctions.cs} | 8 +- .../LoggerFunctions.cs} | 10 +- .../MonsterFunctions.cs} | 8 +- .../PlayerFunctions.cs} | 8 +- .../PositionFunctions.cs} | 8 +- .../TalkActionFunctions.cs} | 8 +- .../TileFunctions.cs} | 8 +- .../IoC/Modules/LuaJITInjection.cs | 32 ++-- .../LuaGameManager.cs | 144 +++++++++--------- .../Interfaces/IActionLuaMapping.cs | 5 - .../LuaMappings/Interfaces/IBaseLuaMapping.cs | 8 - .../Interfaces/IConfigLuaMapping.cs | 5 - .../Interfaces/ICreatureLuaMapping.cs | 5 - .../LuaMappings/Interfaces/IEnumLuaMapping.cs | 5 - .../LuaMappings/Interfaces/IGameLuaMapping.cs | 5 - .../Interfaces/IGlobalLuaMapping.cs | 5 - .../LuaMappings/Interfaces/IItemLuaMapping.cs | 5 - .../Interfaces/IItemTypeLuaMapping.cs | 5 - .../Interfaces/ILoggerLuaMapping.cs | 5 - .../Interfaces/IMonsterLuaMapping.cs | 5 - .../Interfaces/IPlayerLuaMapping.cs | 5 - .../Interfaces/IPositionLuaMapping.cs | 5 - .../Interfaces/ITalkActionLuaMapping.cs | 5 - .../LuaMappings/Interfaces/ITileLuaMapping.cs | 5 - 46 files changed, 227 insertions(+), 227 deletions(-) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/ActionLuaMapping.cs => Functions/ActionFunctions.cs} (91%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/ConfigLuaMappingr.cs => Functions/ConfigFunctions.cs} (91%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/CreatureLuaMapping.cs => Functions/CreatureFunctions.cs} (91%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/EnumLuaMapping.cs => Functions/EnumFunctions.cs} (83%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/GameLuaMapping.cs => Functions/GameFunctions.cs} (97%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/GlobalLuaMapping.cs => Functions/GlobalFunctions.cs} (58%) create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IActionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IBaseFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IConfigFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ICreatureFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IEnumFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGameFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGlobalFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemTypeFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ILoggerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IMonsterFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPlayerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPositionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITalkActionFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITileFunctions.cs rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/ItemLuaMapping.cs => Functions/ItemFunctions.cs} (97%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/ItemTypeLuaMapping.cs => Functions/ItemTypeFunctions.cs} (89%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/LoggerLuaMapping.cs => Functions/LoggerFunctions.cs} (89%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/MonsterLuaMapping.cs => Functions/MonsterFunctions.cs} (84%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/PlayerLuaMapping.cs => Functions/PlayerFunctions.cs} (92%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/PositionLuaMapping.cs => Functions/PositionFunctions.cs} (93%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/TalkActionLuaMapping.cs => Functions/TalkActionFunctions.cs} (93%) rename src/Extensions/NeoServer.Scripts.LuaJIT/{LuaMappings/TileLuaMapping.cs => Functions/TileFunctions.cs} (96%) delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs delete mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ActionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs similarity index 91% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ActionLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs index 63d353dd3..b0313a39a 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ActionLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ActionFunctions.cs @@ -1,14 +1,14 @@ using LuaNET; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class ActionLuaMapping : LuaScriptInterface, IActionLuaMapping +public class ActionFunctions : LuaScriptInterface, IActionFunctions { private static IActions _actions; - public ActionLuaMapping(IActions actions) : base(nameof(ActionLuaMapping)) + public ActionFunctions(IActions actions) : base(nameof(ActionFunctions)) { _actions = actions; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ConfigLuaMappingr.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs similarity index 91% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ConfigLuaMappingr.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs index c8bbcfc61..ba47628cb 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ConfigLuaMappingr.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ConfigFunctions.cs @@ -1,18 +1,18 @@ using LuaNET; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Configurations; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class ConfigLuaMappingr : LuaScriptInterface, IConfigLuaMapping +public class ConfigFunctions : LuaScriptInterface, IConfigFunctions { private static IConfigManager _configManager; private ServerConfiguration _serverConfiguration; - public ConfigLuaMappingr( + public ConfigFunctions( IConfigManager configManager, - ServerConfiguration serverConfiguration) : base(nameof(ConfigLuaMappingr)) + ServerConfiguration serverConfiguration) : base(nameof(ConfigFunctions)) { _configManager = configManager; _serverConfiguration = serverConfiguration; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs similarity index 91% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs index d4e269084..1ad19377a 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/CreatureLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs @@ -1,16 +1,16 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Common.Contracts; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class CreatureLuaMapping : LuaScriptInterface, ICreatureLuaMapping +public class CreatureFunctions : LuaScriptInterface, ICreatureFunctions { private static IGameCreatureManager _gameCreatureManager; - public CreatureLuaMapping(IGameCreatureManager gameCreatureManager) : base(nameof(CreatureLuaMapping)) + public CreatureFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(CreatureFunctions)) { _gameCreatureManager = gameCreatureManager; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/EnumLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs similarity index 83% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/EnumLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs index 741057dff..2b6f09655 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/EnumLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/EnumFunctions.cs @@ -1,15 +1,15 @@ using LuaNET; using NeoServer.Game.Common.Location; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using Serilog; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; -public class EnumLuaMapping : LuaScriptInterface, IEnumLuaMapping +namespace NeoServer.Scripts.LuaJIT.Functions; +public class EnumFunctions : LuaScriptInterface, IEnumFunctions { - public EnumLuaMapping( + public EnumFunctions( ILuaEnvironment luaEnvironment, - ILogger logger) : base(nameof(ConfigLuaMappingr)) + ILogger logger) : base(nameof(ConfigFunctions)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs similarity index 97% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs index 4bbeb0e85..90016748f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GameLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs @@ -8,24 +8,24 @@ using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.Extensions; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Configurations; using NeoServer.Server.Helpers; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class GameLuaMapping : LuaScriptInterface, IGameLuaMapping +public class GameFunctions : LuaScriptInterface, IGameFunctions { private static IScripts _scripts; private static IItemTypeStore _itemTypeStore; private static IItemFactory _itemFactory; private static ServerConfiguration _serverConfiguration; - public GameLuaMapping( + public GameFunctions( IScripts scripts, IItemTypeStore itemTypeStore, IItemFactory itemFactory, - ServerConfiguration serverConfiguration) : base(nameof(GameLuaMapping)) + ServerConfiguration serverConfiguration) : base(nameof(GameFunctions)) { _scripts = scripts; _itemTypeStore = itemTypeStore; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GlobalLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs similarity index 58% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GlobalLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs index 542ac3c9c..31df317b8 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/GlobalLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GlobalFunctions.cs @@ -1,11 +1,11 @@ using LuaNET; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class GlobalLuaMapping : LuaScriptInterface, IGlobalLuaMapping +public class GlobalFunctions : LuaScriptInterface, IGlobalFunctions { - public GlobalLuaMapping() : base(nameof(GlobalLuaMapping)) + public GlobalFunctions() : base(nameof(GlobalFunctions)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IActionFunctions.cs new file mode 100644 index 000000000..ea1dd4bb4 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IActionFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IActionFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IBaseFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IBaseFunctions.cs new file mode 100644 index 000000000..319fe5e31 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IBaseFunctions.cs @@ -0,0 +1,8 @@ +using LuaNET; + +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IBaseFunctions +{ + void Init(LuaState L); +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IConfigFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IConfigFunctions.cs new file mode 100644 index 000000000..fd5d68217 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IConfigFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IConfigFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ICreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ICreatureFunctions.cs new file mode 100644 index 000000000..782f3dd4f --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ICreatureFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface ICreatureFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IEnumFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IEnumFunctions.cs new file mode 100644 index 000000000..79179b5af --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IEnumFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IEnumFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGameFunctions.cs new file mode 100644 index 000000000..0d101e738 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGameFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IGameFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGlobalFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGlobalFunctions.cs new file mode 100644 index 000000000..8bf3b9a51 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IGlobalFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IGlobalFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemFunctions.cs new file mode 100644 index 000000000..c8d3feaac --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IItemFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemTypeFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemTypeFunctions.cs new file mode 100644 index 000000000..b2dc7d961 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IItemTypeFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IItemTypeFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ILoggerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ILoggerFunctions.cs new file mode 100644 index 000000000..14228778c --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ILoggerFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface ILoggerFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IMonsterFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IMonsterFunctions.cs new file mode 100644 index 000000000..3d0b54b07 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IMonsterFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IMonsterFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPlayerFunctions.cs new file mode 100644 index 000000000..42df13b65 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPlayerFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IPlayerFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPositionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPositionFunctions.cs new file mode 100644 index 000000000..f5008f311 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IPositionFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IPositionFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITalkActionFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITalkActionFunctions.cs new file mode 100644 index 000000000..b60d5a889 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITalkActionFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface ITalkActionFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITileFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITileFunctions.cs new file mode 100644 index 000000000..3949c2642 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITileFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface ITileFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs similarity index 97% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs index bc470afe3..4339608ba 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemFunctions.cs @@ -8,22 +8,22 @@ using NeoServer.Game.Common.Contracts.World.Tiles; using NeoServer.Game.Common.Item; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class ItemLuaMapping : LuaScriptInterface, IItemLuaMapping +public class ItemFunctions : LuaScriptInterface, IItemFunctions { private static IItemTransformService _itemTransformService; private static IItemTypeStore _itemTypeStore; private static IMap _map; private static IItemMovementService _itemMovementService; - public ItemLuaMapping( + public ItemFunctions( IItemTransformService itemTransformService, IItemTypeStore itemTypeStore, IMap map, - IItemMovementService itemMovementService) : base(nameof(ItemLuaMapping)) + IItemMovementService itemMovementService) : base(nameof(ItemFunctions)) { _itemTransformService = itemTransformService; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemTypeLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs similarity index 89% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemTypeLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs index 1ce7b018b..faa7b818b 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/ItemTypeLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs @@ -2,14 +2,14 @@ using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Item; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class ItemTypeLuaMapping : LuaScriptInterface, IItemTypeLuaMapping +public class ItemTypeFunctions : LuaScriptInterface, IItemTypeFunctions { private static IItemTypeStore _itemTypeStore; - public ItemTypeLuaMapping(IItemTypeStore itemTypeStore) : base(nameof(ItemTypeLuaMapping)) + public ItemTypeFunctions(IItemTypeStore itemTypeStore) : base(nameof(ItemTypeFunctions)) { _itemTypeStore = itemTypeStore; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/LoggerLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs similarity index 89% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/LoggerLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs index c7a14ad9e..f55985834 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/LoggerLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/LoggerFunctions.cs @@ -1,15 +1,15 @@ using LuaNET; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using Serilog; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class LoggerLuaMapping : LuaScriptInterface, ILoggerLuaMapping +public class LoggerFunctions : LuaScriptInterface, ILoggerFunctions { private static ILogger _logger; - public LoggerLuaMapping( - ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(LoggerLuaMapping)) + public LoggerFunctions( + ILuaEnvironment luaEnvironment, ILogger logger) : base(nameof(LoggerFunctions)) { _logger = logger; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/MonsterLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/MonsterFunctions.cs similarity index 84% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/MonsterLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/MonsterFunctions.cs index ff70076c7..d6982392d 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/MonsterLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/MonsterFunctions.cs @@ -1,16 +1,16 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Common.Contracts; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class MonsterLuaMapping : LuaScriptInterface, IMonsterLuaMapping +public class MonsterFunctions : LuaScriptInterface, IMonsterFunctions { private static IGameCreatureManager _gameCreatureManager; - public MonsterLuaMapping(IGameCreatureManager gameCreatureManager) : base(nameof(MonsterLuaMapping)) + public MonsterFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(MonsterFunctions)) { _gameCreatureManager = gameCreatureManager; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PlayerLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs similarity index 92% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PlayerLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs index 1fa40a204..e1463b824 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PlayerLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs @@ -2,17 +2,17 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Networking.Packets.Outgoing; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Common.Contracts; using NeoServer.Server.Services; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class PlayerLuaMapping : LuaScriptInterface, IPlayerLuaMapping +public class PlayerFunctions : LuaScriptInterface, IPlayerFunctions { private static IGameCreatureManager _gameCreatureManager; - public PlayerLuaMapping(IGameCreatureManager gameCreatureManager) : base(nameof(PlayerLuaMapping)) + public PlayerFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(PlayerFunctions)) { _gameCreatureManager = gameCreatureManager; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PositionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs similarity index 93% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PositionLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs index d2f67c188..22651b6a0 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/PositionLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PositionFunctions.cs @@ -3,18 +3,18 @@ using NeoServer.Game.Common.Creatures; using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Services; using Serilog; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class PositionLuaMapping : LuaScriptInterface, IPositionLuaMapping +public class PositionFunctions : LuaScriptInterface, IPositionFunctions { private static ILogger _logger; private static IConfigManager _configManager; - public PositionLuaMapping(ILogger logger, IConfigManager configManager) : base(nameof(PositionLuaMapping)) + public PositionFunctions(ILogger logger, IConfigManager configManager) : base(nameof(PositionFunctions)) { _logger = logger; _configManager = configManager; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TalkActionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs similarity index 93% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TalkActionLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs index 908221154..168c33d6f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TalkActionLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TalkActionFunctions.cs @@ -1,12 +1,12 @@ using LuaNET; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class TalkActionLuaMapping : LuaScriptInterface, ITalkActionLuaMapping +public class TalkActionFunctions : LuaScriptInterface, ITalkActionFunctions { - public TalkActionLuaMapping() : base(nameof(TalkActionLuaMapping)) + public TalkActionFunctions() : base(nameof(TalkActionFunctions)) { } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs similarity index 96% rename from src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs rename to src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs index 8d4870878..dccf35bb4 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/TileLuaMapping.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TileFunctions.cs @@ -6,16 +6,16 @@ using NeoServer.Game.Common.Location; using NeoServer.Game.Common.Location.Structs; using NeoServer.Scripts.LuaJIT.Enums; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Common.Contracts; -namespace NeoServer.Scripts.LuaJIT.LuaMappings; +namespace NeoServer.Scripts.LuaJIT.Functions; -public class TileLuaMapping : LuaScriptInterface, ITileLuaMapping +public class TileFunctions : LuaScriptInterface, ITileFunctions { private static IGameServer _gameServer; - public TileLuaMapping(IGameServer gameServer) : base(nameof(TileLuaMapping)) + public TileFunctions(IGameServer gameServer) : base(nameof(TileFunctions)) { _gameServer = gameServer; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs index 1130e1118..be1b10790 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using NeoServer.Scripts.LuaJIT; -using NeoServer.Scripts.LuaJIT.LuaMappings; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; namespace NeoServer.Server.Standalone.IoC.Modules; @@ -15,20 +15,20 @@ public static IServiceCollection AddLuaJIT(this IServiceCollection builder) builder.AddSingleton(); builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); - builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); return builder; } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index fcc36ca00..529a4167f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -4,7 +4,7 @@ using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Location.Structs; -using NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Configurations; using Serilog; @@ -49,74 +49,74 @@ public class LuaGameManager : ILuaGameManager private readonly ITalkActions _talkActions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IActionLuaMapping _actionLuaMapping; + private readonly IActionFunctions _actionFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IConfigLuaMapping _configLuaMapping; + private readonly IConfigFunctions _configFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly ICreatureLuaMapping _creatureLuaMapping; + private readonly ICreatureFunctions _creatureFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IEnumLuaMapping _enumLuaMapping; + private readonly IEnumFunctions _enumFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IGameLuaMapping _gameLuaMapping; + private readonly IGameFunctions _gameFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IGlobalLuaMapping _globalLuaMapping; + private readonly IGlobalFunctions _globalFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IItemLuaMapping _itemLuaMapping; + private readonly IItemFunctions _itemFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IItemTypeLuaMapping _itemTypeLuaMapping; + private readonly IItemTypeFunctions _itemTypeFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly ILoggerLuaMapping _loggerLuaMapping; + private readonly ILoggerFunctions _loggerFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IMonsterLuaMapping _monsterLuaMapping; + private readonly IMonsterFunctions _monsterFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IPlayerLuaMapping _playerLuaMapping; + private readonly IPlayerFunctions _playerFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly IPositionLuaMapping _positionLuaMapping; + private readonly IPositionFunctions _positionFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly ITalkActionLuaMapping _talkActionLuaMapping; + private readonly ITalkActionFunctions _talkActionFunctions; /// - /// A reference to the instance in use. + /// A reference to the instance in use. /// - private readonly ITileLuaMapping _tileLuaMapping; + private readonly ITileFunctions _tileFunctions; /// /// A reference to the instance in use. @@ -134,20 +134,20 @@ public LuaGameManager( IScripts scripts, IActions actions, ITalkActions talkActions, - IActionLuaMapping actionLuaMapping, - IConfigLuaMapping configLuaMapping, - ICreatureLuaMapping creatureLuaMapping, - IEnumLuaMapping enumLuaMapping, - IGameLuaMapping gameLuaMapping, - IGlobalLuaMapping globalLuaMapping, - IItemLuaMapping itemLuaMapping, - IItemTypeLuaMapping itemTypeLuaMapping, - ILoggerLuaMapping loggerLuaMapping, - IMonsterLuaMapping monsterLuaMapping, - IPlayerLuaMapping playerLuaMapping, - IPositionLuaMapping positionLuaMapping, - ITalkActionLuaMapping talkActionLuaMapping, - ITileLuaMapping tileLuaMapping, + IActionFunctions actionFunctions, + IConfigFunctions configFunctions, + ICreatureFunctions creatureFunctions, + IEnumFunctions enumFunctions, + IGameFunctions gameFunctions, + IGlobalFunctions globalFunctions, + IItemFunctions itemFunctions, + IItemTypeFunctions itemTypeFunctions, + ILoggerFunctions loggerFunctions, + IMonsterFunctions monsterFunctions, + IPlayerFunctions playerFunctions, + IPositionFunctions positionFunctions, + ITalkActionFunctions talkActionFunctions, + ITileFunctions tileFunctions, ServerConfiguration serverConfiguration) { _logger = logger; @@ -158,20 +158,20 @@ public LuaGameManager( _actions = actions; _talkActions = talkActions; - _actionLuaMapping = actionLuaMapping; - _configLuaMapping = configLuaMapping; - _creatureLuaMapping = creatureLuaMapping; - _enumLuaMapping = enumLuaMapping; - _gameLuaMapping = gameLuaMapping; - _globalLuaMapping = globalLuaMapping; - _itemLuaMapping = itemLuaMapping; - _itemTypeLuaMapping = itemTypeLuaMapping; - _loggerLuaMapping = loggerLuaMapping; - _playerLuaMapping = playerLuaMapping; - _monsterLuaMapping = monsterLuaMapping; - _positionLuaMapping = positionLuaMapping; - _talkActionLuaMapping = talkActionLuaMapping; - _tileLuaMapping = tileLuaMapping; + _actionFunctions = actionFunctions; + _configFunctions = configFunctions; + _creatureFunctions = creatureFunctions; + _enumFunctions = enumFunctions; + _gameFunctions = gameFunctions; + _globalFunctions = globalFunctions; + _itemFunctions = itemFunctions; + _itemTypeFunctions = itemTypeFunctions; + _loggerFunctions = loggerFunctions; + _playerFunctions = playerFunctions; + _monsterFunctions = monsterFunctions; + _positionFunctions = positionFunctions; + _talkActionFunctions = talkActionFunctions; + _tileFunctions = tileFunctions; _serverConfiguration = serverConfiguration; } @@ -192,24 +192,24 @@ public void Start() var luaState = _luaEnviroment.GetLuaState(); if (luaState.IsNull) - _logger.Error("Invalid lua state, cannot load lua LuaMapping."); + _logger.Error("Invalid lua state, cannot load lua Functions."); Lua.OpenLibs(luaState); - _actionLuaMapping.Init(luaState); - _configLuaMapping.Init(luaState); - _creatureLuaMapping.Init(luaState); - _enumLuaMapping.Init(luaState); - _gameLuaMapping.Init(luaState); - _globalLuaMapping.Init(luaState); - _itemLuaMapping.Init(luaState); - _itemTypeLuaMapping.Init(luaState); - _loggerLuaMapping.Init(luaState); - _monsterLuaMapping.Init(luaState); - _playerLuaMapping.Init(luaState); - _positionLuaMapping.Init(luaState); - _talkActionLuaMapping.Init(luaState); - _tileLuaMapping.Init(luaState); + _actionFunctions.Init(luaState); + _configFunctions.Init(luaState); + _creatureFunctions.Init(luaState); + _enumFunctions.Init(luaState); + _gameFunctions.Init(luaState); + _globalFunctions.Init(luaState); + _itemFunctions.Init(luaState); + _itemTypeFunctions.Init(luaState); + _loggerFunctions.Init(luaState); + _monsterFunctions.Init(luaState); + _playerFunctions.Init(luaState); + _positionFunctions.Init(luaState); + _talkActionFunctions.Init(luaState); + _tileFunctions.Init(luaState); ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs deleted file mode 100644 index eb1a263da..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IActionLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IActionLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs deleted file mode 100644 index ee89af2f9..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IBaseLuaMapping.cs +++ /dev/null @@ -1,8 +0,0 @@ -using LuaNET; - -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IBaseLuaMapping -{ - void Init(LuaState L); -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs deleted file mode 100644 index 0c6ff85b0..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IConfigLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IConfigLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs deleted file mode 100644 index 2425161ff..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ICreatureLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface ICreatureLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs deleted file mode 100644 index 72605329b..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IEnumLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IEnumLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs deleted file mode 100644 index 4ae614399..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGameLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IGameLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs deleted file mode 100644 index b25b8efc1..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IGlobalLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IGlobalLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs deleted file mode 100644 index 02697cc80..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IItemLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs deleted file mode 100644 index d3d134e8e..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IItemTypeLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IItemTypeLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs deleted file mode 100644 index b28efe7d3..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ILoggerLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface ILoggerLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs deleted file mode 100644 index 09097a4a4..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IMonsterLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IMonsterLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs deleted file mode 100644 index 6886fb2e7..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPlayerLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IPlayerLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs deleted file mode 100644 index 9e6923f0b..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/IPositionLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface IPositionLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs deleted file mode 100644 index 70e4a7a94..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITalkActionLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface ITalkActionLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs deleted file mode 100644 index 28ff22851..000000000 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaMappings/Interfaces/ITileLuaMapping.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace NeoServer.Scripts.LuaJIT.LuaMappings.Interfaces; - -public interface ITileLuaMapping : IBaseLuaMapping -{ -} \ No newline at end of file From 6444d8760a09867b8eccf0af8f6199073bf17026 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Fri, 27 Dec 2024 21:59:00 -0300 Subject: [PATCH 11/16] fix: adjustments on code --- .../Player/UseItem/PlayerUseItemOnCommand.cs | 2 +- .../NeoServer.Scripts.LuaJIT/Action.cs | 31 +++++-------------- .../Structs/Entities.cs | 2 +- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs index fedd0db37..896d43cb2 100644 --- a/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs +++ b/src/ApplicationServer/NeoServer.Server.Commands/Player/UseItem/PlayerUseItemOnCommand.cs @@ -82,7 +82,7 @@ public void Execute(IPlayer player, UseItemOnPacket useItemPacket) if (thingToUse is not IUsableOn itemToUse) return; - if (_luaGameManager.PlayerUseItemEx(player, player.Location, useItemPacket.ToLocation, useItemPacket.ToStackPosition, itemToUse, useItemPacket.Location.IsHotkey, onItem != null ? onTile : onTile)) + if (_luaGameManager.PlayerUseItemEx(player, player.Location, useItemPacket.ToLocation, useItemPacket.ToStackPosition, itemToUse, useItemPacket.Location.IsHotkey, !onItem ? onTile : onItem)) return; Action action = onTile is not null diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs index ec8543972..87b5e6b35 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Action.cs @@ -9,14 +9,10 @@ namespace NeoServer.Scripts.LuaJIT; public class Action : Script { - private bool _allowFarUse = false; - private bool _checkFloor = true; - private bool _checkLineOfSight = true; - - private List _itemIds = new List(); - private List _uniqueIds = new List(); - private List _actionIds = new List(); - private List _positions = new List(); + private readonly List _itemIds = new List(); + private readonly List _uniqueIds = new List(); + private readonly List _actionIds = new List(); + private readonly List _positions = new List(); private ILogger _logger; @@ -58,23 +54,10 @@ public bool ExecuteUse(IPlayer player, IItem item, Location fromPosition, IThing return GetScriptInterface().CallFunction(6); } - public bool AllowFarUse - { - get { return _allowFarUse; } - set { _allowFarUse = value; } - } - - public bool CheckLineOfSight - { - get { return _checkLineOfSight; } - set { _checkLineOfSight = value; } - } + public bool AllowFarUse { get; set; } + public bool CheckLineOfSight { get; set; } - public bool CheckFloor - { - get { return _checkFloor; } - set { _checkFloor = value; } - } + public bool CheckFloor { get; set; } public List ItemIdsVector { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs index b9adfa51e..bce9fdb85 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Structs/Entities.cs @@ -8,7 +8,7 @@ public enum DirectionType : byte DIRECTION_WEST = 3, DIRECTION_DIAGONAL_MASK = 4, - DIRECTION_SOUTHWEST = DIRECTION_DIAGONAL_MASK | 0, + DIRECTION_SOUTHWEST = DIRECTION_DIAGONAL_MASK, DIRECTION_SOUTHEAST = DIRECTION_DIAGONAL_MASK | 1, DIRECTION_NORTHWEST = DIRECTION_DIAGONAL_MASK | 2, DIRECTION_NORTHEAST = DIRECTION_DIAGONAL_MASK | 3, From 7dba3d551ff99be218eb9a7fdf52ce53dc91fefd Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Fri, 27 Dec 2024 22:09:28 -0300 Subject: [PATCH 12/16] fix: code adjustments --- .../LuaEnvironment.cs | 36 +++++++++---------- .../LuaFunctionsLoader.cs | 28 ++++++--------- .../NeoServer.Scripts.LuaJIT/TalkActions.cs | 2 +- 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs index 9fdd70565..562fa98fa 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaEnvironment.cs @@ -10,16 +10,14 @@ public class LuaEnvironment : LuaScriptInterface, ILuaEnvironment private static LuaEnvironment _instance = null; - private static bool shuttingDown = false; + private static bool _shuttingDown = false; - public readonly Dictionary timerEvents = new Dictionary(); + private readonly Dictionary _timerEvents = new Dictionary(); public uint LastEventTimerId = 1; - private static readonly List cacheFiles = new List(); + private static readonly List _cacheFiles = new List(); - private static LuaScriptInterface testInterface; - - private static int runningEventId = EVENT_ID_USER; + private static LuaScriptInterface _testInterface; #endregion @@ -54,11 +52,11 @@ public LuaEnvironment(ILogger logger) : base("Main Interface") ~LuaEnvironment() { - if (testInterface == null) + if (_testInterface == null) { } - shuttingDown = true; + _shuttingDown = true; CloseState(); } @@ -68,7 +66,7 @@ public LuaEnvironment(ILogger logger) : base("Main Interface") public LuaState GetLuaState() { - if (shuttingDown) + if (_shuttingDown) { return luaState; } @@ -114,7 +112,7 @@ public bool CloseState() // ClearAreaObjects(areaEntry.Key); //} - foreach (var timerEntry in timerEvents) + foreach (var timerEntry in _timerEvents) { var timerEventDesc = timerEntry.Value; foreach (var parameter in timerEventDesc.Parameters) @@ -126,8 +124,8 @@ public bool CloseState() //combatIdMap.Clear(); //areaIdMap.Clear(); - timerEvents.Clear(); - cacheFiles.Clear(); + _timerEvents.Clear(); + _cacheFiles.Clear(); Lua.Close(luaState); luaState.pointer = 0; @@ -136,24 +134,24 @@ public bool CloseState() public LuaScriptInterface GetTestInterface() { - if (testInterface == null) + if (_testInterface == null) { - testInterface = new LuaScriptInterface("Test Interface"); - testInterface.InitState(); + _testInterface = new LuaScriptInterface("Test Interface"); + _testInterface.InitState(); } - return testInterface; + return _testInterface; } public bool IsShuttingDown() { - return shuttingDown; + return _shuttingDown; } public void ExecuteTimerEvent(uint eventIndex) { - if (timerEvents.TryGetValue(eventIndex, out var timerEventDesc)) + if (_timerEvents.TryGetValue(eventIndex, out var timerEventDesc)) { - timerEvents.Remove(eventIndex); + _timerEvents.Remove(eventIndex); Lua.RawGetI(luaState, LUA_REGISTRYINDEX, timerEventDesc.Function); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs index d546bda36..0a0943482 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs @@ -118,9 +118,9 @@ public static void ReportError(string function, string errorDesc, bool stackTrac _logger.Error(string.Format("Lua script error: \nscriptInterface: [{0}]\nscriptId: [{1}]\ntimerEvent: [{2}]\n callbackId:[{3}]\nfunction: [{4}]\nerror [{5}]", scriptInterface != null ? scriptInterface.GetInterfaceName() : "", - scriptId != 0 ? scriptInterface.GetFileById(scriptId) : "", + scriptId != 0 ? scriptInterface?.GetFileById(scriptId) : "", timerEvent ? "in a timer event called from:" : "", - callbackId != 0 ? scriptInterface.GetFileById(callbackId) : "", + callbackId != 0 ? scriptInterface?.GetFileById(callbackId) : "", function ?? "", (stackTrace && scriptInterface != null) ? scriptInterface.GetStackTrace(errorDesc) : errorDesc)); } @@ -591,14 +591,9 @@ public static void RegisterClass(LuaState L, string className, string baseClass, // className.metatable['t'] = type Enum.TryParse(className, true, out var userTypeEnum); - if (userTypeEnum != null) - { - Lua.PushNumber(L, (double)userTypeEnum); - } - else - { - Lua.PushNumber(L, (double)LuaDataType.Unknown); - } + + Lua.PushNumber(L, (double)userTypeEnum); + Lua.RawSetI(L, metatable, 't'); // pop className, className.metatable @@ -714,14 +709,11 @@ public static void RegisterGlobalString(LuaState L, string variable, string name } public static string EscapeString(string str) - { - string s = str; - s.Replace("\\", "\\\\"); - s.Replace("\"", "\\\""); - s.Replace("'", "\\'"); - s.Replace("[[", "\\[["); - return s; - } + => str + .Replace("\\", "\\\\") + .Replace("\"", "\\\"") + .Replace("'", "\\'") + .Replace("[[", "\\[["); public static int LuaUserdataCompare(LuaState L) where T : class { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs index 62a81885e..0cf1e3234 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/TalkActions.cs @@ -15,7 +15,7 @@ public class TalkActions : ITalkActions #region Members - public readonly Dictionary _talkActions = new Dictionary(); + private readonly Dictionary _talkActions = new Dictionary(); #endregion From f45a6ed9a3777c9b94862db42f4a74ae9192754d Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Sat, 28 Dec 2024 00:08:10 -0300 Subject: [PATCH 13/16] feat: create more talkactions (/up, /down, /goto, /m, /s, !position). Fix GameFunctions on Game.CreateMonster. --- data/LuaJit/scripts/talkactions/god/down.lua | 14 +++++ .../scripts/talkactions/god/place_monster.lua | 27 ++++++++ .../scripts/talkactions/god/place_summon.lua | 25 ++++++++ .../talkactions/god/teleport_to_creature.lua | 17 +++++ data/LuaJit/scripts/talkactions/god/up.lua | 14 +++++ .../scripts/talkactions/player/position.lua | 9 +++ .../Spells/Commands/SummonCreator.cs | 2 +- .../Functions/CreatureFunctions.cs | 2 +- .../Functions/GameFunctions.cs | 63 ++++++++++++++----- .../Contracts/Creatures/ICreatureFactory.cs | 2 +- .../Contracts/Creatures/IMonsterFactory.cs | 2 +- .../Factories/CreatureFactory.cs | 2 +- .../Factories/MonsterFactory.cs | 2 +- 13 files changed, 161 insertions(+), 20 deletions(-) create mode 100644 data/LuaJit/scripts/talkactions/god/down.lua create mode 100644 data/LuaJit/scripts/talkactions/god/place_monster.lua create mode 100644 data/LuaJit/scripts/talkactions/god/place_summon.lua create mode 100644 data/LuaJit/scripts/talkactions/god/teleport_to_creature.lua create mode 100644 data/LuaJit/scripts/talkactions/god/up.lua create mode 100644 data/LuaJit/scripts/talkactions/player/position.lua diff --git a/data/LuaJit/scripts/talkactions/god/down.lua b/data/LuaJit/scripts/talkactions/god/down.lua new file mode 100644 index 000000000..2c2698297 --- /dev/null +++ b/data/LuaJit/scripts/talkactions/god/down.lua @@ -0,0 +1,14 @@ +local talkAction = TalkAction("/down") + +function talkAction.onSay(player, words, param) + -- if not player:getGroup():getAccess() then + -- return true + -- end + + local position = player:getPosition() + position.z = position.z - 1 + player:teleportTo(position) + return true +end + +talkAction:register() diff --git a/data/LuaJit/scripts/talkactions/god/place_monster.lua b/data/LuaJit/scripts/talkactions/god/place_monster.lua new file mode 100644 index 000000000..c7ebfc247 --- /dev/null +++ b/data/LuaJit/scripts/talkactions/god/place_monster.lua @@ -0,0 +1,27 @@ +local talkAction = TalkAction("/m") + +function talkAction.onSay(player, words, param) + -- if not player:getGroup():getAccess() then + -- return true + -- end + + -- if player:getAccountType() < ACCOUNT_TYPE_GOD then + -- return false + -- end + + logger.info('/m onSay') + + local position = player:getPosition() + local monster = Game.createMonster(param, position) + if monster then + monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT) + position:sendMagicEffect(CONST_ME_MAGIC_RED) + else + player:sendCancelMessage("There is not enough room.") + position:sendMagicEffect(CONST_ME_POFF) + end + return true +end + +talkAction:separator(" ") +talkAction:register() diff --git a/data/LuaJit/scripts/talkactions/god/place_summon.lua b/data/LuaJit/scripts/talkactions/god/place_summon.lua new file mode 100644 index 000000000..cfa4db89f --- /dev/null +++ b/data/LuaJit/scripts/talkactions/god/place_summon.lua @@ -0,0 +1,25 @@ +local talkAction = TalkAction("/s") + +function talkAction.onSay(player, words, param) + -- if not player:getGroup():getAccess() then + -- return true + -- end + + -- if player:getAccountType() < ACCOUNT_TYPE_GOD then + -- return false + -- end + + local position = player:getPosition() + local monster = Game.createMonster(param, position, false, false, player) + if monster then + monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT) + position:sendMagicEffect(CONST_ME_MAGIC_RED) + else + player:sendCancelMessage("There is not enough room.") + position:sendMagicEffect(CONST_ME_POFF) + end + return true +end + +talkAction:separator(" ") +talkAction:register() diff --git a/data/LuaJit/scripts/talkactions/god/teleport_to_creature.lua b/data/LuaJit/scripts/talkactions/god/teleport_to_creature.lua new file mode 100644 index 000000000..03c85fd02 --- /dev/null +++ b/data/LuaJit/scripts/talkactions/god/teleport_to_creature.lua @@ -0,0 +1,17 @@ +local talkAction = TalkAction("/goto") + +function talkAction.onSay(player, words, param) + -- if not player:getGroup():getAccess() then + -- return true + -- end + + local target = Creature(param) + if target then + player:teleportTo(target:getPosition()) + else + player:sendCancelMessage("Creature not found.") + end + return true +end + +talkAction:register() diff --git a/data/LuaJit/scripts/talkactions/god/up.lua b/data/LuaJit/scripts/talkactions/god/up.lua new file mode 100644 index 000000000..472bc9e97 --- /dev/null +++ b/data/LuaJit/scripts/talkactions/god/up.lua @@ -0,0 +1,14 @@ +local talkAction = TalkAction("/up") + +function talkAction.onSay(player, words, param) + -- if not player:getGroup():getAccess() then + -- return true + -- end + + local position = player:getPosition() + position.z = position.z - 1 + player:teleportTo(position) + return true +end + +talkAction:register() diff --git a/data/LuaJit/scripts/talkactions/player/position.lua b/data/LuaJit/scripts/talkactions/player/position.lua new file mode 100644 index 000000000..c834524b7 --- /dev/null +++ b/data/LuaJit/scripts/talkactions/player/position.lua @@ -0,0 +1,9 @@ +local talkAction = TalkAction("!position") + +function talkAction.onSay(player, words, param) + local position = player:getPosition() + player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Your current position is: " .. position.x .. ", " .. position.y .. ", " .. position.z .. ".") + return true +end + +talkAction:register() diff --git a/data/extensions/Spells/Commands/SummonCreator.cs b/data/extensions/Spells/Commands/SummonCreator.cs index dd0e3ef44..f9f40face 100644 --- a/data/extensions/Spells/Commands/SummonCreator.cs +++ b/data/extensions/Spells/Commands/SummonCreator.cs @@ -15,7 +15,7 @@ public override bool OnCast(ICombatActor actor, string words, out InvalidOperati error = InvalidOperation.NotPossible; if (Params?.Length == 0) return false; - var summon = CreatureFactory.Instance.CreateSummon(Params[0].ToString(), actor as IMonster); + var summon = CreatureFactory.Instance.CreateSummon(Params[0].ToString(), actor); if (summon is null) return false; var map = Map.Instance; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs index 1ad19377a..282b49774 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/CreatureFunctions.cs @@ -39,7 +39,7 @@ private static int LuaCreatureCreate(LuaState L) else if (IsString(L, 2)) { var name = GetString(L, 2); - creature = (ICreature)_gameCreatureManager.GetCreatures().FirstOrDefault(c => c.Name.Equals(name)); + creature = _gameCreatureManager.GetCreatures().FirstOrDefault(c => c.Name.Equals(name)); } else if (IsUserdata(L, 2)) { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs index 90016748f..d785f32ba 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs @@ -1,16 +1,21 @@ using LuaNET; +using NeoServer.Game.Common; using NeoServer.Game.Common.Contracts.Creatures; using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; using NeoServer.Game.Common.Contracts.World; using NeoServer.Game.Common.Contracts.World.Tiles; using NeoServer.Game.Common.Helpers; +using NeoServer.Game.Common.Location; using NeoServer.Game.Common.Location.Structs; +using NeoServer.Game.Creatures.Monster.Summon; +using NeoServer.Game.World.Map; using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.Extensions; using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Configurations; using NeoServer.Server.Helpers; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -19,17 +24,23 @@ public class GameFunctions : LuaScriptInterface, IGameFunctions private static IScripts _scripts; private static IItemTypeStore _itemTypeStore; private static IItemFactory _itemFactory; + private static IMap _map; + private static ICreatureFactory _creatureFactory; private static ServerConfiguration _serverConfiguration; public GameFunctions( IScripts scripts, IItemTypeStore itemTypeStore, IItemFactory itemFactory, + IMap map, + ICreatureFactory creatureFactory, ServerConfiguration serverConfiguration) : base(nameof(GameFunctions)) { _scripts = scripts; _itemTypeStore = itemTypeStore; _itemFactory = itemFactory; + _map = map; + _creatureFactory = creatureFactory; _serverConfiguration = serverConfiguration; } @@ -186,9 +197,6 @@ private static int LuaGameCreateMonster(LuaState L) // Game.createMonster(monsterName, position[, extended = false[, force = false[, master = nil]]]) //todo: implements force parameter - IMonsterFactory _monsterFactory = IoC.GetInstance(); - IMap _map = IoC.GetInstance(); - var monsterName = GetString(L, 1); var position = GetPosition(L, 2); @@ -209,15 +217,9 @@ private static int LuaGameCreateMonster(LuaState L) IMonster monster = null; if (isSummon && master != null) - { - monster = _monsterFactory.CreateSummon(monsterName, master as IMonster); - monster.SetNewLocation(master.Location); - } + monster = _creatureFactory.CreateSummon(monsterName, master); else - { - monster = _monsterFactory.Create(monsterName); - monster.SetNewLocation(position); - } + monster = _creatureFactory.CreateMonster(monsterName); if (!monster) { @@ -225,11 +227,44 @@ private static int LuaGameCreateMonster(LuaState L) return 1; } - foreach (var neighbour in extended ? monster.Location.ExtendedNeighbours : monster.Location.Neighbours) - if (_map[neighbour] is IDynamicTile { HasCreature: false }) + var tileToBorn = _map[position]; + + if (tileToBorn is IDynamicTile { HasCreature: false }) + { + if (tileToBorn.HasFlag(TileFlags.ProtectionZone)) { - monster.SetNewLocation(neighbour); + Lua.PushNil(L); + return 1; + } + + if (isSummon) + { + monster.SetNewLocation(tileToBorn.Location); _map.PlaceCreature(monster); + } + else + { + monster.Born(position); + } + + PushUserdata(L, monster); + SetMetatable(L, -1, "Monster"); + + return 1; + } + + foreach (var neighbour in extended ? position.ExtendedNeighbours : position.Neighbours) + if (_map[neighbour] is IDynamicTile { HasCreature: false }) + { + if (isSummon) + { + monster.SetNewLocation(neighbour); + _map.PlaceCreature(monster); + } + else + { + monster.Born(neighbour); + } PushUserdata(L, monster); SetMetatable(L, -1, "Monster"); diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/ICreatureFactory.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/ICreatureFactory.cs index 0fad07d98..189852fb3 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/ICreatureFactory.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/ICreatureFactory.cs @@ -9,6 +9,6 @@ public interface ICreatureFactory IMonster CreateMonster(string name, ISpawnPoint spawn = null); INpc CreateNpc(string name, ISpawnPoint spawn = null); IPlayer CreatePlayer(IPlayer playerModel); - IMonster CreateSummon(string name, IMonster master); + IMonster CreateSummon(string name, ICreature master); event CreatureCreated OnCreatureCreated; } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonsterFactory.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonsterFactory.cs index cc96b0488..010c1c7a5 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonsterFactory.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Creatures/IMonsterFactory.cs @@ -5,5 +5,5 @@ namespace NeoServer.Game.Common.Contracts.Creatures; public interface IMonsterFactory { IMonster Create(string name, ISpawnPoint spawn = null); - IMonster CreateSummon(string name, IMonster master); + IMonster CreateSummon(string name, ICreature master); } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/CreatureFactory.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/CreatureFactory.cs index a1220bc54..d9a012ab6 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/CreatureFactory.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/CreatureFactory.cs @@ -39,7 +39,7 @@ public IMonster CreateMonster(string name, ISpawnPoint spawn = null) return monster; } - public IMonster CreateSummon(string name, IMonster master) + public IMonster CreateSummon(string name, ICreature master) { var monster = _monsterFactory.CreateSummon(name, master); if (monster is null) return null; diff --git a/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/MonsterFactory.cs b/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/MonsterFactory.cs index 70e853571..e11adffce 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/MonsterFactory.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Creatures/Factories/MonsterFactory.cs @@ -24,7 +24,7 @@ public MonsterFactory(IMonsterDataManager monsterManager, public static IMonsterFactory Instance { get; private set; } - public IMonster CreateSummon(string name, IMonster master) + public IMonster CreateSummon(string name, ICreature master) { var result = _monsterManager.TryGetMonster(name, out var monsterType); if (result == false) From a5dceaa5a730eb21316cb85eba7ff12e803a2e75 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Sat, 28 Dec 2024 01:20:11 -0300 Subject: [PATCH 14/16] feat: create Container, Npc and Teleport functions. Create npc (/n) TalkAction Implementation. --- data/LuaJit/libs/functions/revscriptsys.lua | 6 +- .../scripts/talkactions/god/place_monster.lua | 2 - .../scripts/talkactions/god/place_npc.lua | 25 +++++++ .../Functions/ContainerFunctions.cs | 37 ++++++++++ .../Functions/GameFunctions.cs | 55 +++++++++++++++ .../Interfaces/IContainerFunctions.cs | 5 ++ .../Functions/Interfaces/INpcFunctions.cs | 5 ++ .../Interfaces/ITeleportFunctions.cs | 5 ++ .../Functions/NpcFunctions.cs | 68 +++++++++++++++++++ .../Functions/PlayerFunctions.cs | 2 +- .../Functions/TeleportFunctions.cs | 37 ++++++++++ .../IoC/Modules/LuaJITInjection.cs | 3 + .../LuaFunctionsLoader.cs | 24 +++---- .../LuaGameManager.cs | 29 +++++++- .../Contracts/Items/IItem.cs | 2 + 15 files changed, 285 insertions(+), 20 deletions(-) create mode 100644 data/LuaJit/scripts/talkactions/god/place_npc.lua create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ContainerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IContainerFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/INpcFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITeleportFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/NpcFunctions.cs create mode 100644 src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TeleportFunctions.cs diff --git a/data/LuaJit/libs/functions/revscriptsys.lua b/data/LuaJit/libs/functions/revscriptsys.lua index ffc888457..16689b498 100644 --- a/data/LuaJit/libs/functions/revscriptsys.lua +++ b/data/LuaJit/libs/functions/revscriptsys.lua @@ -55,7 +55,7 @@ do end rawgetmetatable("Player").__index = CreatureIndex rawgetmetatable("Monster").__index = CreatureIndex - -- rawgetmetatable("Npc").__index = CreatureIndex + rawgetmetatable("Npc").__index = CreatureIndex end -- Item index @@ -74,8 +74,8 @@ do return methods[key] end rawgetmetatable("Item").__index = ItemIndex - --rawgetmetatable("Container").__index = ItemIndex - --rawgetmetatable("Teleport").__index = ItemIndex + rawgetmetatable("Container").__index = ItemIndex + rawgetmetatable("Teleport").__index = ItemIndex end -- Action revscriptsys diff --git a/data/LuaJit/scripts/talkactions/god/place_monster.lua b/data/LuaJit/scripts/talkactions/god/place_monster.lua index c7ebfc247..070770dc0 100644 --- a/data/LuaJit/scripts/talkactions/god/place_monster.lua +++ b/data/LuaJit/scripts/talkactions/god/place_monster.lua @@ -9,8 +9,6 @@ function talkAction.onSay(player, words, param) -- return false -- end - logger.info('/m onSay') - local position = player:getPosition() local monster = Game.createMonster(param, position) if monster then diff --git a/data/LuaJit/scripts/talkactions/god/place_npc.lua b/data/LuaJit/scripts/talkactions/god/place_npc.lua new file mode 100644 index 000000000..3ba6e8788 --- /dev/null +++ b/data/LuaJit/scripts/talkactions/god/place_npc.lua @@ -0,0 +1,25 @@ +local talkAction = TalkAction("/n") + +function talkAction.onSay(player, words, param) + -- if not player:getGroup():getAccess() then + -- return true + -- end + + -- if player:getAccountType() < ACCOUNT_TYPE_GOD then + -- return false + -- end + + local position = player:getPosition() + local npc = Game.createNpc(param, position) + if npc then + npc:getPosition():sendMagicEffect(CONST_ME_TELEPORT) + position:sendMagicEffect(CONST_ME_MAGIC_RED) + else + player:sendCancelMessage("There is not enough room.") + position:sendMagicEffect(CONST_ME_POFF) + end + return true +end + +talkAction:separator(" ") +talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ContainerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ContainerFunctions.cs new file mode 100644 index 000000000..7cceae1b1 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ContainerFunctions.cs @@ -0,0 +1,37 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Items.Types.Containers; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class ContainerFunctions : LuaScriptInterface, IContainerFunctions +{ + public ContainerFunctions() : base(nameof(ContainerFunctions)) + { + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Container", "Item", LuaContainerCreate); + RegisterMetaMethod(L, "Container", "__eq", LuaUserdataCompare); + } + + private static int LuaContainerCreate(LuaState L) + { + // Container(uid) + var id = GetNumber(L, 2); + + var container = GetScriptEnv().GetContainerByUID(id); + if (container != null) + { + PushUserdata(L, container); + SetMetatable(L, -1, "Container"); + } + else + { + Lua.PushNil(L); + } + + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs index d785f32ba..9c5025c11 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/GameFunctions.cs @@ -52,6 +52,7 @@ public void Init(LuaState L) RegisterMethod(L, "Game", "createItem", LuaGameCreateItem); RegisterMethod(L, "Game", "createMonster", LuaGameCreateMonster); + RegisterMethod(L, "Game", "createNpc", LuaGameCreateNpc); RegisterMethod(L, "Game", "reload", LuaGameReload); } @@ -276,6 +277,60 @@ private static int LuaGameCreateMonster(LuaState L) return 1; } + private static int LuaGameCreateNpc(LuaState L) + { + // Game.createNpc(npcName, position[, extended = false[, force = false]]) + //todo: implements force parameter + + var ncpName = GetString(L, 1); + + var position = GetPosition(L, 2); + var extended = GetBoolean(L, 3, false); + var force = GetBoolean(L, 4, false); + + var npc = _creatureFactory.CreateNpc(ncpName); + + if (!npc) + { + Lua.PushNil(L); + return 1; + } + + var tileToBorn = _map[position]; + + if (tileToBorn is IDynamicTile { HasCreature: false }) + { + if (tileToBorn.HasFlag(TileFlags.ProtectionZone)) + { + Lua.PushNil(L); + return 1; + } + + npc.SetNewLocation(tileToBorn.Location); + _map.PlaceCreature(npc); + + PushUserdata(L, npc); + SetMetatable(L, -1, "Npc"); + + return 1; + } + + foreach (var neighbour in extended ? position.ExtendedNeighbours : position.Neighbours) + if (_map[neighbour] is IDynamicTile { HasCreature: false }) + { + npc.SetNewLocation(neighbour); + _map.PlaceCreature(npc); + + PushUserdata(L, npc); + SetMetatable(L, -1, "Npc"); + + return 1; + } + + Lua.PushNil(L); + return 1; + } + private static int LuaGameReload(LuaState L) { // Game.reload(reloadType) diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IContainerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IContainerFunctions.cs new file mode 100644 index 000000000..ee929b645 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/IContainerFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface IContainerFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/INpcFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/INpcFunctions.cs new file mode 100644 index 000000000..b617620ce --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/INpcFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface INpcFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITeleportFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITeleportFunctions.cs new file mode 100644 index 000000000..a88942013 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/Interfaces/ITeleportFunctions.cs @@ -0,0 +1,5 @@ +namespace NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +public interface ITeleportFunctions : IBaseFunctions +{ +} \ No newline at end of file diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/NpcFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/NpcFunctions.cs new file mode 100644 index 000000000..a09889a6e --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/NpcFunctions.cs @@ -0,0 +1,68 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Scripts.LuaJIT.Enums; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; +using NeoServer.Server.Common.Contracts; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class NpcFunctions : LuaScriptInterface, INpcFunctions +{ + private static IGameCreatureManager _gameCreatureManager; + + public NpcFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(NpcFunctions)) + { + _gameCreatureManager = gameCreatureManager; + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Npc", "Creature", LuaNpcCreate); + RegisterMetaMethod(L, "Npc", "__eq", LuaUserdataCompare); + } + + private static int LuaNpcCreate(LuaState L) + { + // Npc([id or name or userdata]) + INpc npc = null; + if (IsNumber(L, 2)) + { + var id = GetNumber(L, 2); + _gameCreatureManager.TryGetCreature(id, out var creature); + + if (creature != null && creature is IMonster) + { + npc = creature as INpc; + } + else + { + Lua.PushNil(L); + return 1; + } + } + else if (IsUserdata(L, 2)) + { + if (GetUserdataType(L, 2) != LuaDataType.Npc) + { + Lua.PushNil(L); + return 1; + } + npc = GetUserdata(L, 2); + } + else + { + npc = null; + } + + if (npc is not null) + { + PushUserdata(L, npc); + SetMetatable(L, -1, "Npc"); + } + else + { + Lua.PushNil(L); + } + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs index e1463b824..4444461e7 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs @@ -76,7 +76,7 @@ private static int LuaPlayerCreate(LuaState L) private static int LuaTeleportTo(LuaState L) { - // creature:teleportTo(position[, pushMovement = false]) + // player:teleportTo(position[, pushMovement = false]) bool pushMovement = GetBoolean(L, 3, false); var position = GetPosition(L, 2); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TeleportFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TeleportFunctions.cs new file mode 100644 index 000000000..b927e3e03 --- /dev/null +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/TeleportFunctions.cs @@ -0,0 +1,37 @@ +using LuaNET; +using NeoServer.Game.Common.Contracts.Items.Types; +using NeoServer.Scripts.LuaJIT.Functions.Interfaces; + +namespace NeoServer.Scripts.LuaJIT.Functions; + +public class TeleportFunctions : LuaScriptInterface, ITeleportFunctions +{ + public TeleportFunctions() : base(nameof(TeleportFunctions)) + { + } + + public void Init(LuaState L) + { + RegisterSharedClass(L, "Teleport", "Item", LuaTeleportCreate); + RegisterMetaMethod(L, "Teleport", "__eq", LuaUserdataCompare); + } + + private static int LuaTeleportCreate(LuaState L) + { + // Teleport(uid) + var id = GetNumber(L, 2); + + var item = GetScriptEnv().GetItemByUID(id); + if (item != null) + { + PushUserdata(L, item); + SetMetatable(L, -1, "Teleport"); + } + else + { + Lua.PushNil(L); + } + + return 1; + } +} diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs index be1b10790..40182769a 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/IoC/Modules/LuaJITInjection.cs @@ -17,6 +17,7 @@ public static IServiceCollection AddLuaJIT(this IServiceCollection builder) builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); @@ -25,9 +26,11 @@ public static IServiceCollection AddLuaJIT(this IServiceCollection builder) builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); builder.AddSingleton(); + builder.AddSingleton(); builder.AddSingleton(); return builder; diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs index 0a0943482..c600a93f0 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaFunctionsLoader.cs @@ -303,18 +303,18 @@ public static void SetItemMetatable(LuaState L, int index, IItem item) return; } - //if (item != null && item.GetContainer() != null) - //{ - // Lua.GetMetaTable(L, "Container"); - //} - //else if (item != null && item.GetTeleport() != null) - //{ - // Lua.GetMetaTable(L, "Teleport"); - //} - //else - //{ - Lua.GetMetaTable(L, "Item"); - //} + if (item != null && item.IsContainer) + { + Lua.GetMetaTable(L, "Container"); + } + else if (item != null && item.IsTeleport) + { + Lua.GetMetaTable(L, "Teleport"); + } + else + { + Lua.GetMetaTable(L, "Item"); + } Lua.SetMetaTable(L, index - 1); } diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs index 529a4167f..407674287 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/LuaGameManager.cs @@ -58,6 +58,11 @@ public class LuaGameManager : ILuaGameManager /// private readonly IConfigFunctions _configFunctions; + /// + /// A reference to the instance in use. + /// + private readonly IContainerFunctions _containerFunctions; + /// /// A reference to the instance in use. /// @@ -98,6 +103,11 @@ public class LuaGameManager : ILuaGameManager /// private readonly IMonsterFunctions _monsterFunctions; + /// + /// A reference to the instance in use. + /// + private readonly INpcFunctions _npcFunctions; + /// /// A reference to the instance in use. /// @@ -113,6 +123,11 @@ public class LuaGameManager : ILuaGameManager /// private readonly ITalkActionFunctions _talkActionFunctions; + /// + /// A reference to the instance in use. + /// + private readonly ITeleportFunctions _teleportFunctions; + /// /// A reference to the instance in use. /// @@ -136,6 +151,7 @@ public LuaGameManager( ITalkActions talkActions, IActionFunctions actionFunctions, IConfigFunctions configFunctions, + IContainerFunctions containerFunctions, ICreatureFunctions creatureFunctions, IEnumFunctions enumFunctions, IGameFunctions gameFunctions, @@ -144,9 +160,11 @@ public LuaGameManager( IItemTypeFunctions itemTypeFunctions, ILoggerFunctions loggerFunctions, IMonsterFunctions monsterFunctions, + INpcFunctions npcFunctions, IPlayerFunctions playerFunctions, IPositionFunctions positionFunctions, ITalkActionFunctions talkActionFunctions, + ITeleportFunctions teleportFunctions, ITileFunctions tileFunctions, ServerConfiguration serverConfiguration) { @@ -160,6 +178,7 @@ public LuaGameManager( _actionFunctions = actionFunctions; _configFunctions = configFunctions; + _containerFunctions = containerFunctions; _creatureFunctions = creatureFunctions; _enumFunctions = enumFunctions; _gameFunctions = gameFunctions; @@ -169,8 +188,10 @@ public LuaGameManager( _loggerFunctions = loggerFunctions; _playerFunctions = playerFunctions; _monsterFunctions = monsterFunctions; + _npcFunctions = npcFunctions; _positionFunctions = positionFunctions; _talkActionFunctions = talkActionFunctions; + _teleportFunctions = teleportFunctions; _tileFunctions = tileFunctions; _serverConfiguration = serverConfiguration; @@ -205,12 +226,16 @@ public void Start() _itemFunctions.Init(luaState); _itemTypeFunctions.Init(luaState); _loggerFunctions.Init(luaState); - _monsterFunctions.Init(luaState); - _playerFunctions.Init(luaState); _positionFunctions.Init(luaState); _talkActionFunctions.Init(luaState); _tileFunctions.Init(luaState); + _containerFunctions.Init(luaState); + _monsterFunctions.Init(luaState); + _npcFunctions.Init(luaState); + _playerFunctions.Init(luaState); + _teleportFunctions.Init(luaState); + ModulesLoadHelper(_configManager.Load($"{dir}/config.lua"), $"config.lua"); ModulesLoadHelper(_luaEnviroment.LoadFile($"{dir}{_serverConfiguration.DataLuaJit}/core.lua", "core.lua"), "core.lua"); diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs index c88d346b2..f1e919797 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItem.cs @@ -37,6 +37,8 @@ public interface IItem : IThing, IHasDecay bool IsUsable => Metadata.HasFlag(ItemFlag.Usable); bool IsAntiProjectile => Metadata.HasFlag(ItemFlag.BlockProjectTile); bool IsContainer => Metadata.Group == ItemGroup.Container; + bool IsTeleport => Metadata.Group == ItemGroup.Teleport; + FloorChangeDirection FloorDirection => Metadata.Attributes.GetFloorChangeDirection(); bool HasDecayBehavior From 6d96129fa72a5f7a69df2ced81d4a37dfbd58417 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Sat, 28 Dec 2024 01:26:45 -0300 Subject: [PATCH 15/16] feat: renamed some lua script files --- .../talkactions/god/{place_monster.lua => create_monster.lua} | 0 .../scripts/talkactions/god/{place_npc.lua => create_npc.lua} | 0 .../talkactions/god/{place_summon.lua => create_summon.lua} | 0 .../talkactions/god/{teleport_to_creature.lua => goto.lua} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename data/LuaJit/scripts/talkactions/god/{place_monster.lua => create_monster.lua} (100%) rename data/LuaJit/scripts/talkactions/god/{place_npc.lua => create_npc.lua} (100%) rename data/LuaJit/scripts/talkactions/god/{place_summon.lua => create_summon.lua} (100%) rename data/LuaJit/scripts/talkactions/god/{teleport_to_creature.lua => goto.lua} (100%) diff --git a/data/LuaJit/scripts/talkactions/god/place_monster.lua b/data/LuaJit/scripts/talkactions/god/create_monster.lua similarity index 100% rename from data/LuaJit/scripts/talkactions/god/place_monster.lua rename to data/LuaJit/scripts/talkactions/god/create_monster.lua diff --git a/data/LuaJit/scripts/talkactions/god/place_npc.lua b/data/LuaJit/scripts/talkactions/god/create_npc.lua similarity index 100% rename from data/LuaJit/scripts/talkactions/god/place_npc.lua rename to data/LuaJit/scripts/talkactions/god/create_npc.lua diff --git a/data/LuaJit/scripts/talkactions/god/place_summon.lua b/data/LuaJit/scripts/talkactions/god/create_summon.lua similarity index 100% rename from data/LuaJit/scripts/talkactions/god/place_summon.lua rename to data/LuaJit/scripts/talkactions/god/create_summon.lua diff --git a/data/LuaJit/scripts/talkactions/god/teleport_to_creature.lua b/data/LuaJit/scripts/talkactions/god/goto.lua similarity index 100% rename from data/LuaJit/scripts/talkactions/god/teleport_to_creature.lua rename to data/LuaJit/scripts/talkactions/god/goto.lua From 66c1159cefd34f5d1af2edecf4a294307e612fd5 Mon Sep 17 00:00:00 2001 From: Felipe Muniz Date: Sat, 28 Dec 2024 02:36:28 -0300 Subject: [PATCH 16/16] feat: create item talkaction implementation. new functions (Player.AddItem, ItemType.Iskey, ItemType.IsFluidContainer); --- data/LuaJit/global.lua | 20 +++ .../scripts/talkactions/god/create_item.lua | 73 +++++++++ .../Enums/LuaDefinitions.cs | 18 +++ .../Functions/ItemTypeFunctions.cs | 72 +++++++-- .../Functions/PlayerFunctions.cs | 145 +++++++++++++++++- .../Contracts/Items/IItemType.cs | 29 +++- .../NeoServer.Game.Items/ItemType.cs | 18 --- 7 files changed, 334 insertions(+), 41 deletions(-) create mode 100644 data/LuaJit/scripts/talkactions/god/create_item.lua diff --git a/data/LuaJit/global.lua b/data/LuaJit/global.lua index 0ba7d7ed1..449c03170 100644 --- a/data/LuaJit/global.lua +++ b/data/LuaJit/global.lua @@ -18,4 +18,24 @@ table.contains = function(array, value) end end return false +end + +string.split = function(str, sep) + local res = {} + for v in str:gmatch("([^" .. sep .. "]+)") do + res[#res + 1] = v + end + return res +end + +string.splitTrimmed = function(str, sep) + local res = {} + for v in str:gmatch("([^" .. sep .. "]+)") do + res[#res + 1] = v:trim() + end + return res +end + +string.trim = function(str) + return str:match'^()%s*$' and '' or str:match'^%s*(.*%S)' end \ No newline at end of file diff --git a/data/LuaJit/scripts/talkactions/god/create_item.lua b/data/LuaJit/scripts/talkactions/god/create_item.lua new file mode 100644 index 000000000..9c0e90da1 --- /dev/null +++ b/data/LuaJit/scripts/talkactions/god/create_item.lua @@ -0,0 +1,73 @@ +local talkAction = TalkAction("/i") + +local invalidIds = { + 1, 2, 3, 4, 5, 6, 7, 10, 11, 13, 14, 15, 19, 21, 26, 27, 28, 35, 43 +} + +function talkAction.onSay(player, words, param) + -- if not player:getGroup():getAccess() then + -- return true + -- end + + -- if player:getAccountType() < ACCOUNT_TYPE_GOD then + -- return false + -- end + + local split = param:splitTrimmed(",") + + local itemType = ItemType(split[1]) + if itemType:getId() == 0 then + itemType = ItemType(tonumber(split[1])) + if not tonumber(split[1]) or itemType:getId() == 0 then + player:sendCancelMessage("There is no item with that id or name.") + return false + end + end + + if table.contains(invalidIds, itemType:getId()) then + return false + end + + local keyNumber = 0 + local count = tonumber(split[2]) + if count then + if itemType:isStackable() then + count = math.min(10000, math.max(1, count)) + elseif itemType:isKey() then + keyNumber = count + count = 1 + elseif not itemType:isFluidContainer() then + count = math.min(100, math.max(1, count)) + else + count = math.max(0, count) + end + else + if not itemType:isFluidContainer() then + count = 1 + else + count = 0 + end + end + + local result = player:addItem(itemType:getId(), count) + if result then + if not itemType:isStackable() then + if type(result) == "table" then + for _, item in ipairs(result) do + item:decay() + end + else + if itemType:isKey() then + result:setAttribute(ITEM_ATTRIBUTE_ACTIONID, keyNumber) + end + result:decay() + end + end + player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN) + end + + return true +end + +talkAction:separator(" ") +talkAction:register() diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/LuaDefinitions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/LuaDefinitions.cs index b3175d3d8..684318a8e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/LuaDefinitions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Enums/LuaDefinitions.cs @@ -2,6 +2,24 @@ namespace NeoServer.Scripts.LuaJIT.Enums; +enum SlotsType : byte +{ + CONST_SLOT_WHEREEVER = 0, + CONST_SLOT_HEAD = 1, + CONST_SLOT_NECKLACE = 2, + CONST_SLOT_BACKPACK = 3, + CONST_SLOT_ARMOR = 4, + CONST_SLOT_RIGHT = 5, + CONST_SLOT_LEFT = 6, + CONST_SLOT_LEGS = 7, + CONST_SLOT_FEET = 8, + CONST_SLOT_RING = 9, + CONST_SLOT_AMMO = 10, + + CONST_SLOT_FIRST = CONST_SLOT_HEAD, + CONST_SLOT_LAST = CONST_SLOT_AMMO, +}; + // Enums public enum LuaDataType : byte { diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs index faa7b818b..cad091e8f 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/ItemTypeFunctions.cs @@ -1,7 +1,6 @@ using LuaNET; using NeoServer.Game.Common.Contracts.DataStores; using NeoServer.Game.Common.Contracts.Items; -using NeoServer.Game.Common.Item; using NeoServer.Scripts.LuaJIT.Functions.Interfaces; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -18,10 +17,15 @@ public void Init(LuaState L) { RegisterSharedClass(L, "ItemType", "", LuaCreateItemType); RegisterMetaMethod(L, "ItemType", "__eq", LuaUserdataCompare); - RegisterMethod(L, "ItemType", "getType", LuaGetItemTypeType); - RegisterMethod(L, "ItemType", "getId", LuaGetItemTypeId); - RegisterMethod(L, "ItemType", "getName", LuaGetItemTypeName); - RegisterMethod(L, "ItemType", "isMovable", LuaGetItemTypeIsMoveable); + + RegisterMethod(L, "ItemType", "isMovable", LuaItemTypeIsMoveable); + RegisterMethod(L, "ItemType", "isStackable", LuaItemTypeIsStackable); + RegisterMethod(L, "ItemType", "isFluidContainer", LuaItemTypeIsFluidContainer); + RegisterMethod(L, "ItemType", "isKey", LuaItemTypeIsKey); + + RegisterMethod(L, "ItemType", "getType", LuaItemTypeGetType); + RegisterMethod(L, "ItemType", "getId", LuaItemTypeGetId); + RegisterMethod(L, "ItemType", "getName", LuaItemTypeGetName); } public static int LuaCreateItemType(LuaState L) @@ -46,21 +50,57 @@ public static int LuaCreateItemType(LuaState L) return 1; } - public static int LuaGetItemTypeType(LuaState L) + public static int LuaItemTypeIsMoveable(LuaState L) { - // itemType:getType() + // item:isMovable() var itemType = GetUserdata(L, 1); if (itemType != null) - Lua.PushNumber(L, itemType.ServerId); + Lua.PushBoolean(L, itemType.IsMovable()); else Lua.PushNil(L); return 1; } - public static int LuaGetItemTypeId(LuaState L) + public static int LuaItemTypeIsStackable(LuaState L) { - // itemType:getId() + // item:isStackable() + var itemType = GetUserdata(L, 1); + if (itemType != null) + Lua.PushBoolean(L, itemType.IsStackable()); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaItemTypeIsFluidContainer(LuaState L) + { + // item:isFluidContainer() + var itemType = GetUserdata(L, 1); + if (itemType != null) + Lua.PushBoolean(L, itemType.IsFluidContainer()); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaItemTypeIsKey(LuaState L) + { + // item:isKey() + var itemType = GetUserdata(L, 1); + if (itemType != null) + Lua.PushBoolean(L, itemType.IsKey()); + else + Lua.PushNil(L); + + return 1; + } + + public static int LuaItemTypeGetType(LuaState L) + { + // itemType:getType() var itemType = GetUserdata(L, 1); if (itemType != null) Lua.PushNumber(L, itemType.ServerId); @@ -70,24 +110,24 @@ public static int LuaGetItemTypeId(LuaState L) return 1; } - public static int LuaGetItemTypeName(LuaState L) + public static int LuaItemTypeGetId(LuaState L) { - // item:getName() + // itemType:getId() var itemType = GetUserdata(L, 1); if (itemType != null) - Lua.PushString(L, itemType.Name); + Lua.PushNumber(L, itemType.ServerId); else Lua.PushNil(L); return 1; } - public static int LuaGetItemTypeIsMoveable(LuaState L) + public static int LuaItemTypeGetName(LuaState L) { - // item:isMovable() + // item:getName() var itemType = GetUserdata(L, 1); if (itemType != null) - Lua.PushBoolean(L, itemType.HasFlag(ItemFlag.MovementEvent)); + Lua.PushString(L, itemType.Name); else Lua.PushNil(L); diff --git a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs index 4444461e7..da7fc0d4e 100644 --- a/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs +++ b/src/Extensions/NeoServer.Scripts.LuaJIT/Functions/PlayerFunctions.cs @@ -1,9 +1,13 @@ using LuaNET; using NeoServer.Game.Common.Contracts.Creatures; +using NeoServer.Game.Common.Contracts.DataStores; +using NeoServer.Game.Common.Contracts.Items; +using NeoServer.Game.Common.Creatures.Players; using NeoServer.Networking.Packets.Outgoing; using NeoServer.Scripts.LuaJIT.Enums; using NeoServer.Scripts.LuaJIT.Functions.Interfaces; using NeoServer.Server.Common.Contracts; +using NeoServer.Server.Helpers; using NeoServer.Server.Services; namespace NeoServer.Scripts.LuaJIT.Functions; @@ -11,10 +15,18 @@ namespace NeoServer.Scripts.LuaJIT.Functions; public class PlayerFunctions : LuaScriptInterface, IPlayerFunctions { private static IGameCreatureManager _gameCreatureManager; + private static IItemFactory _itemFactory; + private static IItemTypeStore _itemTypeStore; - public PlayerFunctions(IGameCreatureManager gameCreatureManager) : base(nameof(PlayerFunctions)) + + public PlayerFunctions( + IGameCreatureManager gameCreatureManager, + IItemFactory itemFactory, + IItemTypeStore itemTypeStore) : base(nameof(PlayerFunctions)) { _gameCreatureManager = gameCreatureManager; + _itemFactory = itemFactory; + _itemTypeStore = itemTypeStore; } public void Init(LuaState L) @@ -25,6 +37,8 @@ public void Init(LuaState L) RegisterMethod(L, "Player", "sendTextMessage", LuaPlayerSendTextMessage); RegisterMethod(L, "Player", "isPzLocked", LuaPlayerIsPzLocked); + + RegisterMethod(L, "Player", "addItem", LuaPlayerAddItem); } private static int LuaPlayerCreate(LuaState L) @@ -130,4 +144,133 @@ private static int LuaPlayerIsPzLocked(LuaState L) return 1; } + + private static int LuaPlayerAddItem(LuaState L) + { + // player:addItem(itemId, count = 1, canDropOnMap = true, subType = 1, slot = CONST_SLOT_WHEREEVER) + + var player = GetUserdata(L, 1); + if (!player) + { + Lua.PushBoolean(L, false); + return 1; + } + + ushort itemId = 0; + if (Lua.IsNumber(L, 2)) + { + itemId = GetNumber(L, 2); + } + else + { + var itemName = GetString(L, 2); + var itemTypeByName = _itemTypeStore.GetByName(itemName); + + if (itemTypeByName == null || string.IsNullOrEmpty(itemTypeByName.Name) || itemTypeByName.ServerId == 0) + { + Lua.PushNil(L); + return 1; + } + + itemId = itemTypeByName.ServerId; + } + + var count = GetNumber(L, 3, 1); + var subType = GetNumber(L, 5, 1); + + IItemType it = _itemTypeStore.Get(itemId); + + var itemCount = 1; + int parameters = Lua.GetTop(L); + if (parameters >= 4) + { + itemCount = int.Max(1, count); + } + else if (it.HasSubType()) + { + if (it.IsStackable()) + { + itemCount = (int)Math.Ceiling((float)(count / it.Count)); + } + + subType = count; + } + else + { + itemCount = int.Max(1, count); + } + + bool hasTable = itemCount > 1; + if (hasTable) + { + Lua.NewTable(L); + } + else if (itemCount == 0) + { + Lua.PushNil(L); + return 1; + } + + var canDropOnMap = GetBoolean(L, 4, true); + var slot = GetNumber(L, 6, SlotsType.CONST_SLOT_BACKPACK); + + for (int i = 1; i <= itemCount; ++i) + { + int stackCount = subType; + if (it.IsStackable()) + { + stackCount = int.Max(stackCount, it.Count); + subType -= stackCount; + } + + IItem item = _itemFactory.Create(itemId, player.Location, stackCount); + + if (!item) + { + if (!hasTable) + Lua.PushNil(L); + + return 1; + } + + var isSuccess = false; + + if (!item.IsPickupable && player.Tile is { } tile && tile.AddItem(item).Succeeded) + { + item.Decay?.StartDecay(); + isSuccess = true; + } + else + { + isSuccess = player.Inventory.AddItem(item, (Slot)slot).Succeeded; + + if (!isSuccess && canDropOnMap && player.Tile is { } playerTile && playerTile.AddItem(item).Succeeded) + { + item.Decay?.StartDecay(); + isSuccess = true; + } + } + + if (isSuccess) + { + if (hasTable) + { + Lua.PushNumber(L, i); + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + Lua.SetTable(L, -3); + } + else + { + PushUserdata(L, item); + SetItemMetatable(L, -1, item); + } + } + else if (hasTable) + { + Lua.PushNil(L); + } + } + return 1; + } } diff --git a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs index 4bda10bcb..435083ee9 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Common/Contracts/Items/IItemType.cs @@ -43,10 +43,27 @@ public interface IItemType bool HasAtLeastOneFlag(params ItemFlag[] flags); void SetGroupIfNone(); - bool IsFluidContainer(); - bool IsSplash(); - bool IsStackable(); - ushort Charges { get; } - ushort Count { get; } - bool HasSubType(); + bool IsMovable() + => Flags.Contains(ItemFlag.Movable); + + bool IsFluidContainer() + => Flags.Contains(ItemFlag.LiquidContainer); + + bool IsSplash() + => Group == ItemGroup.Splash; + + bool IsStackable() + => Group == ItemGroup.Splash; + + bool IsKey() + => Flags.Contains(ItemFlag.Key); + + ushort Charges + => Attributes.GetAttribute(ItemAttribute.Charges); + + ushort Count + => Attributes.GetAttribute(ItemAttribute.Count); + + bool HasSubType() + => IsFluidContainer() || IsSplash() || IsStackable() || Charges != 0; } \ No newline at end of file diff --git a/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs b/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs index 6116a231a..7b6f61cb3 100644 --- a/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs +++ b/src/GameWorldSimulator/NeoServer.Game.Items/ItemType.cs @@ -98,24 +98,6 @@ public bool HasAtLeastOneFlag(params ItemFlag[] flags) return false; } - public bool IsFluidContainer() - => Flags.Contains(ItemFlag.LiquidContainer); - - public bool IsSplash() - => Group == ItemGroup.Splash; - - public bool IsStackable() - => Group == ItemGroup.Splash; - - public ushort Charges - => Attributes.GetAttribute(ItemAttribute.Charges); - - public ushort Count - => Attributes.GetAttribute(ItemAttribute.Count); - - public bool HasSubType() - => IsFluidContainer() || IsSplash() || IsStackable() || Charges != 0; - public AmmoType AmmoType => Attributes?.GetAttribute(ItemAttribute.AmmoType) switch { "bolt" => AmmoType.Bolt,