From f10934c0d0ee09c10603bf08785947976a6667d5 Mon Sep 17 00:00:00 2001 From: Robert Paciorek Date: Tue, 30 Jan 2024 10:05:34 +0000 Subject: [PATCH] add replaceDragon tool, version 0.6.0 --- src/CmdLineProgram.cs | 29 +++++++++++++++++++++++++ src/Tools.cs | 49 +++++++++++++++++++++++++++++++++++++++++++ version.txt | 2 +- 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/CmdLineProgram.cs b/src/CmdLineProgram.cs index 99b3795..7c33e63 100644 --- a/src/CmdLineProgram.cs +++ b/src/CmdLineProgram.cs @@ -258,6 +258,35 @@ static async Task Main(string[] args) { ); toolsCommand.AddCommand(selectDragonCommand); + var replaceDragonCommand = new Command("replaceDragon", "replace XML and images of current pet\n" + + "WARNING: This may broke your account. Use at own risk!\n" + + "WARNING: Your current dragon will be destroyed!\n" + ); + var replaceDragonArgument = new Argument( + name: "xmlFile", + description: "Path to xml file. Root node should be " + ); + var replaceDragonImg1Argument = new Argument( + name: "EggColor", + description: "Path to EggColor image file", + getDefaultValue: () => "-" + ); + var replaceDragonImg2Argument = new Argument( + name: "Mythie", + description: "Path to Mythie image file", + getDefaultValue: () => "-" + ); + replaceDragonCommand.Add(replaceDragonArgument); + replaceDragonCommand.Add(replaceDragonImg1Argument); + replaceDragonCommand.Add(replaceDragonImg2Argument); + replaceDragonCommand.SetHandler( + async (replaceDragonID, img1, img2) => { + await Tools.ReplaceDragon(loginData, replaceDragonID, img1, img2); + }, + replaceDragonArgument, replaceDragonImg1Argument, replaceDragonImg2Argument + ); + toolsCommand.AddCommand(replaceDragonCommand); + return await rootCommand.InvokeAsync(args); } } diff --git a/src/Tools.cs b/src/Tools.cs index 61344b0..da98dc9 100644 --- a/src/Tools.cs +++ b/src/Tools.cs @@ -1,4 +1,5 @@ using System.Net; +using System.Xml; using dragonrescue.Api; using dragonrescue.Util; using dragonrescue.Schema; @@ -35,4 +36,52 @@ public static async System.Threading.Tasks.Task SelectDragon(LoginApi.Data login bodyRaw = await client.PostAndGetReplayOrThrow(Config.URL_CONT_API + "/ContentWebService.asmx/SetKeyValuePair", formContent); Config.LogWriter(string.Format("Set {0} to {1}: {2}", keyName, petTypeID, bodyRaw)); } + + public static async System.Threading.Tasks.Task ReplaceDragon(LoginApi.Data loginData, string path, string img1File = "-", string img2File = "-") { + XmlDocument dragon = new XmlDocument(); + dragon.PreserveWhitespace = true; + dragon.Load(path); + + (var client, var apiToken, var profile) = await LoginApi.DoVikingLogin(loginData); + + // get current pet + var oldPetStr = await client.PostAndGetReplayOrThrow( + Config.URL_CONT_API + "/ContentWebService.asmx/GetSelectedRaisedPet", + new FormUrlEncodedContent(new[] { + new KeyValuePair("apiKey", Config.APIKEY), + new KeyValuePair("apiToken", apiToken), + }) + ); + Config.LogWriter($"Pet {oldPetStr} will be replaced"); + var oldPet = XmlUtil.DeserializeXml(oldPetStr); + + // update input xml + dragon["RaisedPetData"]["uid"].InnerText = oldPet[0].UserID.ToString(); + dragon["RaisedPetData"]["eid"].InnerText = oldPet[0].EntityID.ToString(); + dragon["RaisedPetData"]["id"].InnerText = oldPet[0].RaisedPetID.ToString(); + dragon["RaisedPetData"]["ip"].InnerText = oldPet[0].ImagePosition.ToString(); + + // update xml on server + var setRaisedPetRequest = " 0 " + dragon["RaisedPetData"].InnerXml + " "; + + string res = await DragonApi.SetRaisedPet(client, apiToken, setRaisedPetRequest); + Config.LogWriter($"Replace pet xml: {res}"); + + // update images + if (oldPet[0].ImagePosition != null) { + int imagePosition = (int)(oldPet[0].ImagePosition); + + if (img1File != "-" && File.Exists(img1File)) { + string imgData = Convert.ToBase64String(System.IO.File.ReadAllBytes(img1File)); + res = await ImageApi.SetImage(client, apiToken, imagePosition, imgData, "EggColor"); + Config.LogWriter($"Replace pet EggColor (imagePosition={imagePosition}): {res}"); + } + + if (img2File != "-" && File.Exists(img1File)) { + string imgData = Convert.ToBase64String(System.IO.File.ReadAllBytes(img2File)); + res = await ImageApi.SetImage(client, apiToken, imagePosition, imgData, "Mythie"); + Config.LogWriter($"Replace pet Mythie (imagePosition={imagePosition}): {res}"); + } + } + } } diff --git a/version.txt b/version.txt index cb0c939..a918a2a 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.5.2 +0.6.0