-
Notifications
You must be signed in to change notification settings - Fork 45
AutoMovePetCenter #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| using DailyRoutines.Abstracts; | ||
| using DailyRoutines.Infos; | ||
| using DailyRoutines.Managers; | ||
| using Dalamud.Hooking; | ||
| using Dalamud.Utility; | ||
| using FFXIVClientStructs.FFXIV.Client.Game; | ||
| using Lumina.Excel.GeneratedSheets; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Numerics; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace DailyRoutines.Modules; | ||
|
|
||
| public unsafe class AutoMovePetCenter : DailyModuleBase | ||
| { | ||
| public override ModuleInfo Info => new() | ||
| { | ||
| Author = ["逆光"], | ||
| Title = GetLoc("AutoMovePetCenterTitle"), | ||
| Description = GetLoc("AutoMovePetCenterDescription"), | ||
| Category = ModuleCategories.Combat, | ||
| ModulesConflict = ["AutoMovePetPosition"] | ||
| }; | ||
|
|
||
| private static readonly CompSig NpcSpawnSig = new("48 89 5C 24 ?? 57 48 81 EC ?? ?? ?? ?? 48 8B DA 8B F9 E8 ?? ?? ?? ?? 3C ?? 75 ?? E8 ?? ?? ?? ?? 3C ?? 75 ?? 80 BB ?? ?? ?? ?? ?? 75 ?? 8B 05 ?? ?? ?? ?? 39 43 ?? 0F 85 ?? ?? ?? ?? 0F B6 53 ?? 48 8D 0D ?? ?? ?? ?? E8 ?? ?? ?? ?? 0F B6 53 ?? 48 8D 0D ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 8D 54 24 ?? C7 44 24 ?? ?? ?? ?? ?? B8 ?? ?? ?? ?? 66 90 48 8D 92 ?? ?? ?? ?? 0F 10 03 0F 10 4B ?? 48 8D 9B ?? ?? ?? ?? 0F 11 42 ?? 0F 10 43 ?? 0F 11 4A ?? 0F 10 4B ?? 0F 11 42 ?? 0F 10 43 ?? 0F 11 4A ?? 0F 10 4B ?? 0F 11 42 ?? 0F 10 43 ?? 0F 11 4A ?? 0F 10 4B ?? 0F 11 42 ?? 0F 11 4A ?? 48 83 E8 ?? 75 ?? 48 8B 03"); | ||
| private delegate void NpcSpawnDelegate(nint a1, nint packetData); | ||
| private static Hook<NpcSpawnDelegate>? NpcSpawnHook; | ||
|
|
||
| private static readonly Dictionary<string, List<MapInfo>> MMapInfoDict = []; | ||
|
|
||
| public override void Init() | ||
| { | ||
| NpcSpawnHook ??= NpcSpawnSig.GetHook<NpcSpawnDelegate>(ReviceNpcSpawn); | ||
| NpcSpawnHook.Enable(); | ||
|
|
||
| LoadZoneAndMapInfo(); | ||
| base.Init(); | ||
| } | ||
|
|
||
| private static void LoadZoneAndMapInfo() | ||
| { | ||
| MMapInfoDict.Clear(); | ||
| foreach (var map in DService.Data.GetExcelSheet<Map>()!) | ||
| { | ||
| var mapZoneKey = map.Id.RawString.Split('/')[0]; | ||
| if (!MMapInfoDict.TryGetValue(mapZoneKey, out var value)) | ||
| { | ||
| value = []; | ||
| MMapInfoDict[mapZoneKey] = value; | ||
| } | ||
| value.Add(new MapInfo(map.Id, map.SizeFactor, map.OffsetX, map.OffsetY, map.PlaceNameSub.Value!.Name.RawString)); | ||
| } | ||
| } | ||
|
|
||
| private void ReviceNpcSpawn(nint a1, nint packetData) | ||
| { | ||
| NpcSpawnHook.Original(a1, packetData); | ||
| try | ||
| { | ||
| var sourceCharacter = Marshal.PtrToStructure<NpcSpawn>(packetData); | ||
| if (DService.ClientState.LocalPlayer == null || GameMain.Instance()->CurrentContentFinderConditionId == 0) | ||
| return; | ||
|
|
||
| if (sourceCharacter.spawnerId == DService.ClientState.LocalPlayer.GameObjectId) | ||
| { | ||
| var pos = GetMapInfoFromTerritoryTypeID(GameMain.Instance()->CurrentTerritoryTypeId).GetMapCoordinates(new Vector2(1024f, 1024f)); | ||
| var center = new Vector3(pos.X, DService.ClientState.LocalPlayer.Position.Y, pos.Y); | ||
| DService.Log.Debug($"自动将召唤兽移动至: {center}"); | ||
| ExecuteCommandManager.ExecuteCommandComplexLocation(ExecuteCommandComplexFlag.PetAction, center, 3); | ||
|
|
||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| DService.Log.Error($"{e}"); | ||
| } | ||
| } | ||
|
|
||
| private static MapInfo GetMapInfoFromTerritoryTypeID(uint TerritoryType) | ||
| { | ||
| var mapBaseName = DService.Data.GetExcelSheet<TerritoryType>()?.GetRow(TerritoryType)?.Map.Value?.Id.RawString.Split('/')[0]; | ||
| if (mapBaseName.IsNullOrEmpty()) | ||
| return MapInfo.Unknown; | ||
|
|
||
| return MMapInfoDict.TryGetValue(mapBaseName!, out var value) ? | ||
| value.MinBy(item => Vector2.Distance(item.GetMapCoordinates(new Vector2(1024f, 1024f)), new Vector2(DService.ClientState.LocalPlayer!.Position.X, DService.ClientState.LocalPlayer!.Position.Z))) | ||
| : MapInfo.Unknown; | ||
| } | ||
|
|
||
| private struct MapInfo(string mapID, ushort sizeFactor, short offsetX, short offsetY, string placeNameSub) | ||
| { | ||
| public string MapID { readonly get; set; } = mapID; | ||
| public ushort SizeFactor { readonly get; set; } = sizeFactor; | ||
| public Vector2 Offset { readonly get; set; } = new Vector2(offsetX, offsetY); | ||
| public string PlaceNameSub { readonly get; set; } = placeNameSub; | ||
|
|
||
| public readonly Vector2 GetMapCoordinates(Vector2 pixelCoordinates) | ||
| { | ||
| return ((pixelCoordinates - new Vector2(1024f)) / SizeFactor * 100f) - Offset; | ||
| } | ||
|
|
||
| public static readonly MapInfo Unknown = new("default/00", 100, 0, 0, ""); | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit, Size = 672)] | ||
| private struct NpcSpawn | ||
| { | ||
| [FieldOffset(84)] | ||
| public uint spawnerId; | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码必要性:
AutoMovePetCenter,用于在特定条件下自动将召唤兽移动到地图中心。这是一个新功能,相比旧代码有明确的改进。逻辑错误或潜在运行时错误:
ReviceNpcSpawn方法中,DService.ClientState.LocalPlayer和GameMain.Instance()的返回值未进行空值检查,可能导致空引用异常。GetMapInfoFromTerritoryTypeID方法中,mapBaseName可能为空,但后续代码直接使用mapBaseName!,可能导致空引用异常。兼容性问题:
AutoMovePetPosition模块存在冲突,已在Info属性中明确声明。没有发现其他兼容性问题。代码结构优化:
LoadZoneAndMapInfo方法中的map.Id.RawString.Split('/')[0]可以提取为一个常量或方法,避免重复代码。GetMapInfoFromTerritoryTypeID方法中的MinBy操作可以优化,避免重复计算距离。潜在性能问题:
LoadZoneAndMapInfo方法在每次初始化时都会重新加载地图信息,如果地图数据较大,可能会影响性能。可以考虑缓存或延迟加载。安全性:
外部代码风险:
改进建议:
ReviceNpcSpawn方法中,增加对DService.ClientState.LocalPlayer和GameMain.Instance()的空值检查。GetMapInfoFromTerritoryTypeID方法中,增加对mapBaseName的空值检查,避免空引用异常。LoadZoneAndMapInfo方法中的代码结构,提取重复代码为常量或方法。LoadZoneAndMapInfo方法中实现缓存或延迟加载,以提高性能。