Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
add replaceDragon tool, version 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rpaciorek committed Jan 30, 2024
1 parent 2cd9d9c commit f10934c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/CmdLineProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,35 @@ static async Task<int> 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<string>(
name: "xmlFile",
description: "Path to xml file. Root node should be <RaisedPetData>"
);
var replaceDragonImg1Argument = new Argument<string>(
name: "EggColor",
description: "Path to EggColor image file",
getDefaultValue: () => "-"
);
var replaceDragonImg2Argument = new Argument<string>(
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);
}
}
49 changes: 49 additions & 0 deletions src/Tools.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Xml;
using dragonrescue.Api;
using dragonrescue.Util;
using dragonrescue.Schema;
Expand Down Expand Up @@ -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<string, string>("apiKey", Config.APIKEY),
new KeyValuePair<string, string>("apiToken", apiToken),
})
);
Config.LogWriter($"Pet {oldPetStr} will be replaced");
var oldPet = XmlUtil.DeserializeXml<RaisedPetData[]>(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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <RPR xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <ptid>0</ptid> <rpd> " + dragon["RaisedPetData"].InnerXml + " </rpd> </RPR>";

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}");
}
}
}
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.2
0.6.0

0 comments on commit f10934c

Please sign in to comment.