diff --git a/DoorKeybind.sln b/DoorKeybind.sln deleted file mode 100644 index 03471bf..0000000 --- a/DoorKeybind.sln +++ /dev/null @@ -1,28 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.31903.59 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoorKeybind", "DoorKeybind\DoorKeybind.csproj", "{CB2100BC-F653-402A-9FBA-EA463C2BD4FC}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CakeBuild", "ZZCakeBuild\CakeBuild.csproj", "{BC68EDF6-C294-4819-B7F4-9EA99B73E69D}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {BC68EDF6-C294-4819-B7F4-9EA99B73E69D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {BC68EDF6-C294-4819-B7F4-9EA99B73E69D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {BC68EDF6-C294-4819-B7F4-9EA99B73E69D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {BC68EDF6-C294-4819-B7F4-9EA99B73E69D}.Release|Any CPU.Build.0 = Release|Any CPU - {CB2100BC-F653-402A-9FBA-EA463C2BD4FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CB2100BC-F653-402A-9FBA-EA463C2BD4FC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CB2100BC-F653-402A-9FBA-EA463C2BD4FC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CB2100BC-F653-402A-9FBA-EA463C2BD4FC}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal diff --git a/DoorKeybind.slnx b/DoorKeybind.slnx index 4e0bb38..96da2d4 100644 --- a/DoorKeybind.slnx +++ b/DoorKeybind.slnx @@ -1,4 +1,4 @@ - + diff --git a/DoorKeybind/DoorKeybind.csproj b/DoorKeybind/DoorKeybind.csproj index 5749ab8..e2c6776 100644 --- a/DoorKeybind/DoorKeybind.csproj +++ b/DoorKeybind/DoorKeybind.csproj @@ -5,6 +5,7 @@ false bin\$(Configuration)\Mods\mod enable + $(VINTAGE_STORY_21) diff --git a/DoorKeybind/DoorKeybindModSystem.cs b/DoorKeybind/DoorKeybindModSystem.cs index 37db8a1..a74de60 100644 --- a/DoorKeybind/DoorKeybindModSystem.cs +++ b/DoorKeybind/DoorKeybindModSystem.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using DoorKeybind.Utils; using Vintagestory.API.Client; using Vintagestory.API.Common; using Vintagestory.API.MathTools; @@ -50,7 +51,7 @@ public class DoorKeybindModSystem : ModSystem var closestDistance = max_door_range + 1; var range = (int)Math.Ceiling(max_door_range); - var centerPos = player.Pos.AsBlockPos; + var centerPos = player.GetPos().AsBlockPos; var minPos = new BlockPos(-range, -range, -range).Add(centerPos); var maxPos = new BlockPos(range, range, range).Add(centerPos); @@ -75,8 +76,8 @@ public class DoorKeybindModSystem : ModSystem var selectionBoxes = currentBlock.GetSelectionBoxes(capi.World.BlockAccessor, new(x, y, z)); var blockCenter = new Vec3d(x + 0.5, y + 0.5, z + 0.5); var distance = selectionBoxes.Length == 0 - ? player.Pos.XYZ.DistanceTo(blockCenter) - : selectionBoxes.Min(cuboid => player.Pos.XYZ.DistanceTo(cuboid.Center + new Vec3d(x, y, z))); + ? player.GetPos().XYZ.DistanceTo(blockCenter) + : selectionBoxes.Min(cuboid => player.GetPos().XYZ.DistanceTo(cuboid.Center + new Vec3d(x, y, z))); if (distance <= max_door_range && distance <= closestDistance) { @@ -105,7 +106,7 @@ public class DoorKeybindModSystem : ModSystem if (player == null) return; - var distance = player.Pos.XYZ.DistanceTo(new Vec3d(blockPos.X + 0.5, blockPos.Y + 0.5, blockPos.Z + 0.5)); + var distance = player.GetPos().XYZ.DistanceTo(new Vec3d(blockPos.X + 0.5, blockPos.Y + 0.5, blockPos.Z + 0.5)); if (distance > max_door_range) return; diff --git a/DoorKeybind/Utils/CompatibilityUtil.cs b/DoorKeybind/Utils/CompatibilityUtil.cs new file mode 100644 index 0000000..1500c56 --- /dev/null +++ b/DoorKeybind/Utils/CompatibilityUtil.cs @@ -0,0 +1,55 @@ +using System; +using Vintagestory.API.Common.Entities; + +namespace DoorKeybind.Utils; + +/// +/// When the mod is built for 1.21, and then you run it with 1.22, this happens when referring to Entity.Pos, +/// which was changed from a field to a property (and that being the reason this happens): +/// +/// System.MissingFieldException: Field not found: 'Vintagestory.API.Common.Entities.Entity.Pos'. +/// at MonoMod.Core.Interop.CoreCLR.V60.InvokeCompileMethod(IntPtr functionPtr, IntPtr thisPtr, IntPtr corJitInfo, CORINFO_METHOD_INFO* methodInfo, UInt32 flags, Byte** nativeEntry, UInt32* nativeSizeOfCode) +/// at MonoMod.Core.Platforms.Runtimes.Core60Runtime.JitHookDelegateHolder.CompileMethodHook(IntPtr jit, IntPtr corJitInfo, CORINFO_METHOD_INFO* methodInfo, UInt32 flags, Byte** nativeEntry, UInt32* nativeSizeOfCode) +/// at ChestPreview.PreviewTargetProvider.CanAccessContainer(BlockEntity blockEntity) in E:\Code\VintageStory\ChestPreview\ChestPreview\ChestPreview\PreviewTargetProvider.cs:line 237 +/// +/// the PreviewTargetProvider.cs: around line 237 looks like this: +/// +/// private bool CanAccessContainer(BlockEntity blockEntity) +/// { +/// if (blockEntity is not IBlockEntityContainer container || container.Inventory is not InventoryBase inventory) +/// return false; +/// +/// IPlayer? player = api.World.Player; +/// EntityPlayer? playerEntity = player?.Entity; +/// if (player == null || playerEntity == null) +/// return false; +/// +/// return inventory.CanPlayerAccess(player, playerEntity.Pos); +/// } +/// +/// it happens because of the last line where we refer to "playerEntity.Pos" - this field doesn't exist now, because it's a property. +/// +/// This class uses reflection to refer to either the property, or the field, whichever actually exists. +/// +internal static class CompatibilityUtil +{ +#if !VERSION22 + private static Func? _getPos; + + private static Func GetPosFunc => _getPos ??= BuildGetPos(); + + private static Func BuildGetPos() + { + var prop = typeof(Entity).GetProperty("Pos"); + if (prop != null) + return (Func)Delegate.CreateDelegate(typeof(Func), prop.GetGetMethod()!); + + var field = typeof(Entity).GetField("Pos")!; + return entity => (EntityPos)field.GetValue(entity)!; + } + + public static EntityPos GetPos(this Entity entity) => GetPosFunc(entity); +#else + public static EntityPos GetPos(this Entity entity) => entity.Pos; +#endif +} \ No newline at end of file diff --git a/DoorKeybind/modinfo.json b/DoorKeybind/modinfo.json index 1006aa6..cefc113 100644 --- a/DoorKeybind/modinfo.json +++ b/DoorKeybind/modinfo.json @@ -7,7 +7,7 @@ "OrekiWoof" ], "description": "Lets you open and close doors with a keybind, so you don't have to turn around to click them.", - "version": "1.0.0-rc.1", + "version": "1.0.1", "dependencies": { "game": "1.21.0" }, diff --git a/ZZCakeBuild/CakeBuild.csproj b/ZZCakeBuild/CakeBuild.csproj index e3faeeb..bd1b675 100644 --- a/ZZCakeBuild/CakeBuild.csproj +++ b/ZZCakeBuild/CakeBuild.csproj @@ -3,6 +3,7 @@ Exe net8.0 $(MSBuildProjectDirectory) + $(VINTAGE_STORY_21) diff --git a/build.sh b/build.sh old mode 100644 new mode 100755