From 4b26a088bf9584cb2ec98146456611edf12b6547 Mon Sep 17 00:00:00 2001 From: DArkHekRoMaNT Date: Fri, 26 Aug 2022 04:34:34 +0300 Subject: [PATCH] Update to 1.17 --- .gitignore | 4 +- .gitmodules | 3 - .vscode/launch.json | 107 ---------------- .vscode/settings.json | 3 - .vscode/tasks.json | 58 --------- HelveHammerExtensions.csproj | 119 ++++++++++++------ LICENSE | 2 +- Properties/launchSettings.json | 15 +++ .../patches/notworkable/anvil.json | 0 .../helvehammerext/patches/workable/part.json | 0 .../patches/workable/resource.json | 0 .../patches/workable/toolhead.json | 6 + resources/modicon.png => modicon.png | Bin resources/modinfo.json => modinfo.json | 6 +- src/Config.cs | 20 ++- src/Core.cs | 23 ++-- src/Extensions/ApiExtensions.cs | 91 ++++++++++++++ src/Extensions/LoggerExtensions.cs | 106 ++++++++++++++++ src/Extensions/Vec3dExtensions.cs | 16 +++ src/Patches/GetHelveWorkableModePatch.cs | 10 +- src/SharedUtils | 1 - src/VanillaPatches.cs | 2 +- 22 files changed, 364 insertions(+), 228 deletions(-) delete mode 100644 .gitmodules delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json delete mode 100644 .vscode/tasks.json create mode 100644 Properties/launchSettings.json rename {resources/assets => assets}/helvehammerext/patches/notworkable/anvil.json (100%) rename {resources/assets => assets}/helvehammerext/patches/workable/part.json (100%) rename {resources/assets => assets}/helvehammerext/patches/workable/resource.json (100%) rename {resources/assets => assets}/helvehammerext/patches/workable/toolhead.json (92%) rename resources/modicon.png => modicon.png (100%) rename resources/modinfo.json => modinfo.json (61%) create mode 100644 src/Extensions/ApiExtensions.cs create mode 100644 src/Extensions/LoggerExtensions.cs create mode 100644 src/Extensions/Vec3dExtensions.cs delete mode 160000 src/SharedUtils diff --git a/.gitignore b/.gitignore index 28a4927..2fd6aed 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ /obj /mods /releases -/.history \ No newline at end of file +/.vs +/*.csproj.user +/*.sln \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index aea26df..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "src/SharedUtils"] - path = src/SharedUtils - url = https://github.com/DArkHekRoMaNT/SharedUtils.git diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index a62b0db..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,107 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "Launch Client with TestWorld", - "type": "clr", - "request": "launch", - "preLaunchTask": "build (Debug)", - "program": "${env:VINTAGE_STORY}/${config:vsversion}/Vintagestory.exe", - "args": [ - "--playStyle", - "preset-surviveandbuild", - "--openWorld", - "modding test world", - "--dataPath", - "${env:VINTAGE_STORY_DATA}", - "--addModPath", - "${workspaceFolder}/mods" - ], - "console": "internalConsole", - "internalConsoleOptions": "openOnSessionStart", - "requireExactSource": false - }, - { - "name": "Launch Client (Mono)", - "type": "mono", - "request": "launch", - "preLaunchTask": "build (Debug)", - "program": "${env:VINTAGE_STORY}/${config:vsversion}/Vintagestory.exe", - "args": [ - "--playStyle", - "preset-surviveandbuild", - "--openWorld", - "modding test world", - "--dataPath", - "${env:VINTAGE_STORY_DATA}", - "--addModPath", - "${workspaceFolder}/mods" - ], - "console": "internalConsole", - "internalConsoleOptions": "openOnSessionStart" - }, - { - "name": "Launch Server", - "type": "clr", - "request": "launch", - "program": "${env:VINTAGE_STORY}/${config:vsversion}/VintagestoryServer.exe", - "args": [ - "--playStyle", - "preset-surviveandbuild", - "--openWorld", - "modding test world", - "--dataPath", - "${env:VINTAGE_STORY_DATA}", - "--addModPath", - "${workspaceFolder}/mods" - ], - "console": "internalConsole", - "internalConsoleOptions": "openOnSessionStart", - "requireExactSource": false - }, - { - "name": "Launch Client", - "type": "clr", - "request": "launch", - "program": "${env:VINTAGE_STORY}/${config:vsversion}/Vintagestory.exe", - "args": [ - "--dataPath", - "${env:VINTAGE_STORY_DATA}", - "--addModPath", - "${workspaceFolder}/mods" - ], - "console": "internalConsole", - "internalConsoleOptions": "openOnSessionStart", - "requireExactSource": false - }, - { - "name": "Launch Client 2", - "type": "clr", - "request": "launch", - "program": "${env:VINTAGE_STORY}/${config:vsversion}/Vintagestory.exe", - "args": [ - "--dataPath", - "${env:VINTAGE_STORY_DATA}2", - "--addModPath", - "${workspaceFolder}/mods" - ], - "console": "internalConsole", - "internalConsoleOptions": "openOnSessionStart", - "requireExactSource": false - } - ], - "compounds": [ - { - "name": "Launch Server/Client", - "configurations": ["Launch Server", "Launch Client"], - "preLaunchTask": "build (Debug)", - "stopAll": true - }, - { - "name": "Launch Server/Client х2", - "configurations": ["Launch Server", "Launch Client", "Launch Client 2"], - "preLaunchTask": "build (Debug)", - "stopAll": true - } - ] -} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 0b5e45a..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "vsversion": "1.16.5" -} diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index f12006c..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,58 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "pack", - "type": "shell", - "command": [ - "mkdir -p mods;", - "rm -r mods/${workspaceFolderBasename};", - "mkdir mods/${workspaceFolderBasename};", - "cp -r resources/* mods/${workspaceFolderBasename}" - ], - "presentation": { - "reveal": "silent" - }, - "problemMatcher": [] - }, - { - "label": "pack-release", - "dependsOn": "build (Debug)", - "type": "shell", - "command": [ - "mkdir -p releases;", - "git archive", - "-o releases/${workspaceFolderBasename}_v${config:vsversion}_last.zip", - "--add-file=mods/${workspaceFolderBasename}/${workspaceFolderBasename}.dll", - "--add-file=mods/${workspaceFolderBasename}/${workspaceFolderBasename}.pdb", - "\"HEAD:resources\"" - ], - "presentation": { - "reveal": "silent" - }, - "problemMatcher": [] - }, - { - "label": "build (Debug)", - "dependsOn": "pack", - "group": { - "kind": "build", - "isDefault": true - }, - "presentation": { - "reveal": "silent" - }, - "problemMatcher": "$msCompile", - "type": "shell", - "command": "dotnet", - "linux": { - "options": { - "env": { - "FrameworkPathOverride": "/lib/mono/4.6.1-api/" - } - } - }, - "args": ["build", "-c", "Debug"] - } - ] -} diff --git a/HelveHammerExtensions.csproj b/HelveHammerExtensions.csproj index cd9ce74..b76c97e 100644 --- a/HelveHammerExtensions.csproj +++ b/HelveHammerExtensions.csproj @@ -1,36 +1,85 @@ - - - net461 - 1.16.0 - - - - - $(VINTAGE_STORY)/$(VSVersion)/VintagestoryAPI.dll - - - $(VINTAGE_STORY)/$(VSVersion)/Mods/VSSurvivalMod.dll - - - $(VINTAGE_STORY)/$(VSVersion)/Lib/0Harmony.dll - - - $(VINTAGE_STORY)/$(VSVersion)/Lib/protobuf-net.dll - - - $(VINTAGE_STORY)/$(VSVersion)/Lib/Newtonsoft.Json.dll - - - $(VINTAGE_STORY)/$(VSVersion)/Lib/cairo-sharp.dll - - - - - - - - - - - + + + net461 + Debug;Release;Publish + true + true + CS1591 + 4 + 5 + + + + $(DefaultItemExcludes);modtools/** + + + + 1.17.0-rc.5 + 1.17.0-rc.5 + + + + + $(VINTAGE_STORY)/$(MinGameVersion)/VintagestoryAPI.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/VintagestoryLib.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/Mods/VSEssentials.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/Mods/VSCreativeMod.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/Mods/VSSurvivalMod.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/Lib/0Harmony.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/Lib/Newtonsoft.Json.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/Lib/protobuf-net.dll + + + $(VINTAGE_STORY)/$(MinGameVersion)/Lib/cairo-sharp.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE index dab9a42..be10c56 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 DArkHekRoMaNT +Copyright (c) 2022 DArkHekRoMaNT Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..baddbaa --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,15 @@ +{ + "profiles": { + "Launch Client": { + "commandName": "Executable", + "executablePath": "%VINTAGE_STORY%\\$(RunGameVersion)\\Vintagestory.exe", + "commandLineArgs": "--addModPath $(ProjectDir)mods --addOrigin $(ProjectDir)assets --tracelog" + }, + "Launch Client with Test World": { + "commandName": "Executable", + "executablePath": "%VINTAGE_STORY%\\$(RunGameVersion)\\Vintagestory.exe", + "commandLineArgs": "--playStyle \"preset-surviveandbuild\" --openWorld \"$(ProjectName) Test World\" --addModPath $(ProjectDir)mods --addOrigin $(ProjectDir)assets --tracelog", + "nativeDebugging": false + } + } +} \ No newline at end of file diff --git a/resources/assets/helvehammerext/patches/notworkable/anvil.json b/assets/helvehammerext/patches/notworkable/anvil.json similarity index 100% rename from resources/assets/helvehammerext/patches/notworkable/anvil.json rename to assets/helvehammerext/patches/notworkable/anvil.json diff --git a/resources/assets/helvehammerext/patches/workable/part.json b/assets/helvehammerext/patches/workable/part.json similarity index 100% rename from resources/assets/helvehammerext/patches/workable/part.json rename to assets/helvehammerext/patches/workable/part.json diff --git a/resources/assets/helvehammerext/patches/workable/resource.json b/assets/helvehammerext/patches/workable/resource.json similarity index 100% rename from resources/assets/helvehammerext/patches/workable/resource.json rename to assets/helvehammerext/patches/workable/resource.json diff --git a/resources/assets/helvehammerext/patches/workable/toolhead.json b/assets/helvehammerext/patches/workable/toolhead.json similarity index 92% rename from resources/assets/helvehammerext/patches/workable/toolhead.json rename to assets/helvehammerext/patches/workable/toolhead.json index bfc1dfc..c07b85c 100644 --- a/resources/assets/helvehammerext/patches/workable/toolhead.json +++ b/assets/helvehammerext/patches/workable/toolhead.json @@ -65,6 +65,12 @@ "path": "/attributes", "value": {} }, + { + "file": "game:itemtypes/toolhead/bladehead.json", + "op": "add", + "path": "/attributes", + "value": {} + }, { "file": "game:itemtypes/toolhead/*", "op": "add", diff --git a/resources/modicon.png b/modicon.png similarity index 100% rename from resources/modicon.png rename to modicon.png diff --git a/resources/modinfo.json b/modinfo.json similarity index 61% rename from resources/modinfo.json rename to modinfo.json index 25fcc39..3dab5b4 100644 --- a/resources/modinfo.json +++ b/modinfo.json @@ -2,10 +2,10 @@ "type": "code", "modid": "helvehammerext", "name": "Helve Hammer Extensions", - "authors": ["DArkHekRoMaNT"], + "authors": [ "DArkHekRoMaNT" ], "description": "", - "version": "1.2.1", + "version": "1.3.0", "dependencies": { - "game": "1.16.0" + "game": "1.17.0-rc.5" } } diff --git a/src/Config.cs b/src/Config.cs index 58e9658..680bb57 100644 --- a/src/Config.cs +++ b/src/Config.cs @@ -2,9 +2,21 @@ namespace HelveHammerExtensions { public class Config { - public static Config Current { get; set; } = new Config(); - public bool AllWorkable { get; set; } = false; - public bool DefaultWorkable { get; set; } = true; - public int AnvilTier { get; set; } = 3; + public static Config Current { get; set; } + public bool AllWorkable { get; set; } + public bool DefaultWorkable { get; set; } + public int AnvilTier { get; set; } + + public Config() + { + AllWorkable = false; + DefaultWorkable = true; + AnvilTier = 3; + } + + static Config() + { + Current = new Config(); + } } } \ No newline at end of file diff --git a/src/Core.cs b/src/Core.cs index 6e4cf5f..fd603f2 100644 --- a/src/Core.cs +++ b/src/Core.cs @@ -1,28 +1,35 @@ using System; -using SharedUtils; -using SharedUtils.Extensions; using Vintagestory.API.Common; namespace HelveHammerExtensions { public class Core : ModSystem { - ICoreAPI api; - public override void Start(ICoreAPI api) + public static string ModId { get; private set; } + public static ILogger ModLogger { get; private set; } + + private ICoreAPI _api; + + public override void StartPre(ICoreAPI api) { - this.api = api; + ModLogger = Mod.Logger; + } - Config.Current = api.LoadOrCreateConfig(ConstantsCore.ModId + ".json"); + public override void Start(ICoreAPI api) + { + _api = api; + Config.Current = api.LoadOrCreateConfig(Mod.Info.ModID + ".json"); } + public override void Dispose() { try { - api.StoreModConfig(Config.Current, ConstantsCore.ModId + ".json"); + _api.StoreModConfig(Config.Current, Mod.Info.ModID + ".json"); } catch (Exception e) { - api.Logger.ModError(e.ToString()); + Mod.Logger.Error(e.ToString()); } } } diff --git a/src/Extensions/ApiExtensions.cs b/src/Extensions/ApiExtensions.cs new file mode 100644 index 0000000..9101988 --- /dev/null +++ b/src/Extensions/ApiExtensions.cs @@ -0,0 +1,91 @@ +using System; +using System.IO; +using Vintagestory.API.Common; + +namespace HelveHammerExtensions +{ + public static class ApiExtensions + { + public static string GetWorldId(this ICoreAPI api) + { + if (api != null && api.World != null) + { + return api.World.SavegameIdentifier; + } + else + { + return null; + } + } + + public static TConfig LoadOrCreateConfig(this ICoreAPI api, string file, TConfig defaultConfig = null) where TConfig : class, new() + { + TConfig config = null; + + try + { + config = api.LoadModConfig(file); + } + catch (Exception e) + { + string format = "Failed loading config file ({0}), error {1}. Will initialize default config"; + Core.ModLogger.Error(string.Format(format), file, e); + } + + if (config == null) + { + Core.ModLogger.Notification("Will initialize default config"); + config = defaultConfig ?? new TConfig(); + } + + api.StoreModConfig(config, file); + return config; + } + + public static TData LoadDataFile(this ICoreAPI api, string file) where TData : class, new() + { + try + { + if (File.Exists(file)) + { + var content = File.ReadAllText(file); + return JsonUtil.FromString(content); + } + } + catch (Exception e) + { + string format = "Failed loading data file ({0}), error {1}. Will initialize new data file"; + Core.ModLogger.Error(string.Format(format, file, e)); + } + + return new TData(); + } + + public static TData LoadOrCreateDataFile(this ICoreAPI api, string file) where TData : class, new() + { + var data = api.LoadDataFile(file); + if (data == null) + { + Core.ModLogger.Notification("Will initialize new data file"); + data = new TData(); + SaveDataFile(api, file, data); + } + return data; + } + + public static void SaveDataFile(this ICoreAPI api, string file, T data) + { + try + { + Directory.CreateDirectory(Path.GetDirectoryName(file)); + var content = JsonUtil.ToString(data); + File.WriteAllText(file, content); + } + catch (Exception e) + { + string format = "Failed saving file ({0}), error {1}"; + Core.ModLogger.Error(string.Format(format, file, e)); + } + } + } +} \ No newline at end of file diff --git a/src/Extensions/LoggerExtensions.cs b/src/Extensions/LoggerExtensions.cs new file mode 100644 index 0000000..1a204eb --- /dev/null +++ b/src/Extensions/LoggerExtensions.cs @@ -0,0 +1,106 @@ +using ProtoBuf; +using Vintagestory.API.Client; +using Vintagestory.API.Common; +using Vintagestory.API.Common.Entities; +using Vintagestory.API.Config; +using Vintagestory.API.Server; + +namespace HelveHammerExtensions +{ + [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)] + public class NetworkSendCurrentMessage + { + public string msg; + public int chatGroup; + public string playerUID; + } + + public class LoggerNetwork : ModSystem + { + ICoreAPI api; + + public override void Start(ICoreAPI api) + { + this.api = api; + } + + public override void StartServerSide(ICoreServerAPI api) + { + api.Network + .RegisterChannel("loggerextensions") + .RegisterMessageType(typeof(NetworkSendCurrentMessage)); + } + + public override void StartClientSide(ICoreClientAPI api) + { + api.Network + .RegisterChannel("loggerextensions") + .RegisterMessageType(typeof(NetworkSendCurrentMessage)) + .SetMessageHandler(SendMessageFromServer); + } + + private void SendMessageFromServer(NetworkSendCurrentMessage netmsg) + { + IPlayer player = api.World.PlayerByUid(netmsg.playerUID); + (player as IClientPlayer).SendMessage(netmsg.msg, netmsg.chatGroup); + } + } + + public static class LoggerExtensions + { + public static void SendMessageAsClient(this IServerPlayer player, string msg, int chatGroup = -1) + { + (player.Entity.Api as ICoreServerAPI).Network + .GetChannel("loggerextensions") + .SendPacket(new NetworkSendCurrentMessage() + { + msg = msg, + chatGroup = chatGroup, + playerUID = player.PlayerUID + }, player); + } + + public static void SendMessage(this IServerPlayer player, string msg, int chatGroup = -1) + { + if (chatGroup == -1) chatGroup = GlobalConstants.CurrentChatGroup; + ICoreAPI api = player.Entity.Api; + player.SendMessage(chatGroup, msg, EnumChatType.Notification); + api.World.Logger.Chat(msg); + } + + public static void SendMessage(this IClientPlayer player, string msg, int chatGroup = -1) + { + if (chatGroup == -1) chatGroup = GlobalConstants.CurrentChatGroup; + ICoreAPI api = player.Entity.Api; + (api as ICoreClientAPI).SendChatMessage(msg, chatGroup); + api.World.Logger.Chat(msg); + + } + + public static void SendMessage(this IPlayer player, string msg, int chatGroup = -1) + { + ICoreAPI api = player.Entity.Api; + if (api.Side == EnumAppSide.Server) (player as IServerPlayer).SendMessage(msg, chatGroup); + else (player as IClientPlayer).SendMessage(msg, chatGroup); + } + + public static void SendMessage(this Entity playerEntity, string msg, int chatGroup = -1) + { + ICoreAPI api = playerEntity.Api; + EntityPlayer entityPlayer = playerEntity as EntityPlayer; + IPlayer player = entityPlayer == null ? null : api.World.PlayerByUid(entityPlayer.PlayerUID); + + if (player != null) player.SendMessage(msg, chatGroup); + else api.World.Logger.Chat(playerEntity.GetName() + " trying say: " + msg); + } + + public static void SendMessageAll(this ICoreAPI api, string msg, int chatGroup = -1) + { + IPlayer[] players = api.World.AllPlayers; + foreach (IPlayer player in players) + { + player.SendMessage(msg, chatGroup); + } + } + } +} \ No newline at end of file diff --git a/src/Extensions/Vec3dExtensions.cs b/src/Extensions/Vec3dExtensions.cs new file mode 100644 index 0000000..ce54415 --- /dev/null +++ b/src/Extensions/Vec3dExtensions.cs @@ -0,0 +1,16 @@ +using Vintagestory.API.Common; +using Vintagestory.API.MathTools; + +namespace HelveHammerExtensions +{ + public static class Vec3dExtensions + { + public static Vec3d RelativePos(this Vec3d pos, ICoreAPI api) + { + pos.X -= api.World.DefaultSpawnPosition.XYZ.X; + pos.Z -= api.World.DefaultSpawnPosition.XYZ.Z; + + return pos; + } + } +} \ No newline at end of file diff --git a/src/Patches/GetHelveWorkableModePatch.cs b/src/Patches/GetHelveWorkableModePatch.cs index 287f12c..d842a42 100644 --- a/src/Patches/GetHelveWorkableModePatch.cs +++ b/src/Patches/GetHelveWorkableModePatch.cs @@ -3,14 +3,18 @@ namespace HelveHammerExtensions { - [HarmonyPatch(typeof(ItemWorkItem))] - [HarmonyPatch(nameof(ItemWorkItem.GetHelveWorkableMode))] + [HarmonyPatch("GetHelveWorkableMode")] public class GetHelveWorkableModePatch { public static bool Prefix(ItemWorkItem __instance, ref EnumHelveWorkableMode __result, ref BlockEntityAnvil beAnvil) { - bool workable = beAnvil.SelectedRecipe.Output.Attributes?["helvehammerworkable"]?.AsBool() ?? Config.Current.DefaultWorkable; + bool workable = Config.Current.DefaultWorkable; + var attr = beAnvil.SelectedRecipe.Output.Attributes; + if (attr != null) + { + workable = attr["helvehammerworkable"].AsBool(); + } if ((workable || Config.Current.AllWorkable) && beAnvil.OwnMetalTier >= Config.Current.AnvilTier) { diff --git a/src/SharedUtils b/src/SharedUtils deleted file mode 160000 index 6ad87d0..0000000 --- a/src/SharedUtils +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6ad87d0fc39636f86ce22baf7c847854f4876db4 diff --git a/src/VanillaPatches.cs b/src/VanillaPatches.cs index 6660ce2..d4e98ca 100644 --- a/src/VanillaPatches.cs +++ b/src/VanillaPatches.cs @@ -18,7 +18,7 @@ public override void Start(ICoreAPI api) { builder.Append(val.Name + ", "); } - api.Logger.Notification(builder.ToString()); + Mod.Logger.Notification(builder.ToString()); } public override void Dispose()