From 7c546cf63d41120170b48833fd3a4c9d345f2538 Mon Sep 17 00:00:00 2001 From: Evin Jaff Date: Sun, 9 Jun 2024 02:16:34 -0500 Subject: [PATCH] Download endpoint works!! --- pkhex/dockerfile | 8 +-- .../pkhex-egglocke/EggCreator.cs | 5 ++ .../pkhex-egglocke/HTTPModels.cs | 15 ++++ .../pkhex-egglocke/HTTPRoutes.cs | 69 +++++++++++++++++++ .../pkhex-egglocke/JSONDecoder.cs | 15 ---- .../pkhex-egglocke/JSONDeserializer.cs | 30 ++++++++ .../pkhex-egglocke/pkhex-egglocke/Program.cs | 19 +++-- .../pkhex-egglocke/SaveWriter.cs | 4 ++ .../pkhex-egglocke/TestRoute.cs | 40 ----------- 9 files changed, 141 insertions(+), 64 deletions(-) create mode 100644 pkhex/pkhex-egglocke/pkhex-egglocke/HTTPModels.cs create mode 100644 pkhex/pkhex-egglocke/pkhex-egglocke/HTTPRoutes.cs delete mode 100644 pkhex/pkhex-egglocke/pkhex-egglocke/JSONDecoder.cs create mode 100644 pkhex/pkhex-egglocke/pkhex-egglocke/JSONDeserializer.cs delete mode 100644 pkhex/pkhex-egglocke/pkhex-egglocke/TestRoute.cs diff --git a/pkhex/dockerfile b/pkhex/dockerfile index 57d059a..13bc053 100644 --- a/pkhex/dockerfile +++ b/pkhex/dockerfile @@ -20,8 +20,8 @@ RUN dotnet build --no-restore pkhex-egglocke RUN dotnet publish -c release -r linux-x64 --self-contained +ENV PORT 1234 +EXPOSE 1234 -ENV PORT 8080 -EXPOSE 8080 - -# ENTRYPOINT ["dotnet", "/app/dotnetcoresample.dll"] \ No newline at end of file +WORKDIR /source/pkhex-egglocke +ENTRYPOINT ["dotnet", "run"] \ No newline at end of file diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs index 2f4b2eb..862b450 100644 --- a/pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs +++ b/pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs @@ -80,6 +80,11 @@ public EggCreator(byte ball, ushort dexNumber, int language, int ability, Nature public static EggCreator decodeJSON(string filepath, bool is_filepath) { + /// + /// decodeJSON: Decodes a JSON object into a PokemonCreator object + /// + /// + string json; if (is_filepath) { diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/HTTPModels.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/HTTPModels.cs new file mode 100644 index 0000000..7ae0686 --- /dev/null +++ b/pkhex/pkhex-egglocke/pkhex-egglocke/HTTPModels.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace pkhex_egglocke +{ + public class SaveFileGeneratorModel + { + public int generation { get; set; } = 4; + public string jsonRaw { get; set; } = ""; + public dynamic[] eggs { get; set; } =Array.Empty(); + } +} diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/HTTPRoutes.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/HTTPRoutes.cs new file mode 100644 index 0000000..da278ec --- /dev/null +++ b/pkhex/pkhex-egglocke/pkhex-egglocke/HTTPRoutes.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Grapevine; +using Newtonsoft.Json; +using PKHeX.Core; +using pkhexEgglocke; + +namespace pkhex_egglocke +{ + [RestResource] + public class MyResource: Resource + { + [RestRoute("Get", "/api/test")] + public async Task Test(IHttpContext context) + { + await context.Response.SendResponseAsync("Successfully hit the test route!"); + } + + [RestRoute("Get", "/api/startfile")] + public async Task StartFile(IHttpContext context) + { + await context.Response.SendResponseAsync("Successfully hit the startfile route!"); + } + + [RestRoute("Get", "/api/file1")] + public async Task DownloadFile(IHttpContext context) + { + string filePath = "C:\\Users\\Evin Jaff\\Downloads\\Pokemon.mp3"; + + context.Response.ContentType = "audio/mpeg"; + context.Response.AddHeader("Content-Disposition", "attachment; filename=blank.mp3"); + context.Response.ContentLength64 = new FileInfo(filePath).Length; + + await context.Response.SendResponseAsync(File.ReadAllBytes(filePath)); + + } + + [RestRoute("Post", "/api/buildSaveFile")] + public async Task BuildSaveFile(IHttpContext context) + { + // Decode the input stream + var model = await DeserializeAsync(context.Request.InputStream, context.CancellationToken); + + // yields array of dynamic objects + SaveWriter sw = new SaveWriter("C:\\Users\\Evin Jaff\\Downloads\\Blank_SoulSilver.sav"); + + EggCreator[] eggArray = new EggCreator[model.eggs.Length]; + for (int i = 0; i < model.eggs.Length; i++) { + EggCreator constructed_egg_creator = EggCreator.decodeJSON(model.eggs[i].ToString(), false); + sw.addEgg(constructed_egg_creator, i+1); + } + + byte[] saveFile = sw.exportRawBytes(); + + // Prepare the response as a downloadable file + context.Response.ContentType = "application/octet-stream"; + context.Response.AddHeader("Content-Disposition", "attachment; filename=blank.sav"); + context.Response.ContentLength64 = saveFile.Length; + + await context.Response.SendResponseAsync(saveFile); + + } + + + } +} diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/JSONDecoder.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/JSONDecoder.cs deleted file mode 100644 index 0e8230e..0000000 --- a/pkhex/pkhex-egglocke/pkhex-egglocke/JSONDecoder.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace pkhex_egglocke -{ - internal class JSONDecoder - { - - // Very annoying code that decodes strings to class-defined integers - - } -} diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/JSONDeserializer.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/JSONDeserializer.cs new file mode 100644 index 0000000..66eebbf --- /dev/null +++ b/pkhex/pkhex-egglocke/pkhex-egglocke/JSONDeserializer.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + + +namespace pkhex_egglocke +{ + public abstract class Resource + { + public static JsonSerializerOptions SerializerOptions = new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true + }; + + public async Task DeserializeAsync(Stream stream) + { + return await DeserializeAsync(stream, CancellationToken.None); + } + + public async Task DeserializeAsync(Stream stream, CancellationToken token) + { + return await JsonSerializer.DeserializeAsync(stream, SerializerOptions, token); + } + + } + +} diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs index 44cfaa4..70d2115 100644 --- a/pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs +++ b/pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs @@ -14,17 +14,26 @@ class Program { public static void Main(string[] args) { + RestServerBuilder restServerBuilder = RestServerBuilder.UseDefaults(); + + + + // Spin up the REST server using (var server = RestServerBuilder.UseDefaults().Build()) { server.Start(); Console.WriteLine("Press enter to stop the server"); Console.ReadLine(); + + // block the main thread forever + while (true) { } + } var BLANK_SOULSILVER_SAVE = @"C:\Users\Evin Jaff\Downloads\johtocomplete.sav"; - SaveWriter sw = new SaveWriter(BLANK_SOULSILVER_SAVE); + // SaveWriter sw = new SaveWriter(BLANK_SOULSILVER_SAVE); //EggCreator pc = new EggCreator(); @@ -37,13 +46,13 @@ public static void Main(string[] args) var BLANK_GEN4_MAREEP_VALID = Path.Combine("C:\\Users\\Evin Jaff\\Documents\\egglocke-maker\\pkhex\\pkhex-egglocke-tests\\pkhex-egglocke-tests\\testSources", "Mareep.json"); var BLANK_GEN4_LEGENDARY_TRIO = Path.Combine("C:\\Users\\Evin Jaff\\Documents\\egglocke-maker\\pkhex\\pkhex-egglocke-tests\\pkhex-egglocke-tests\\testSources", "LegendaryTrio.json"); - EggCreator ec = EggCreator.decodeJSON(BLANK_GEN4_MAREEP_VALID, true); + // EggCreator ec = EggCreator.decodeJSON(BLANK_GEN4_MAREEP_VALID, true); - sw.massAddEggs(BLANK_GEN4_LEGENDARY_TRIO); + // sw.massAddEggs(BLANK_GEN4_LEGENDARY_TRIO); - sw.export("testPROG.sav"); + // sw.export("testPROG.sav"); - Console.WriteLine("Dumped testPROG"); + // Console.WriteLine("Dumped testPROG"); } diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs index 4664707..41bfc5e 100644 --- a/pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs +++ b/pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs @@ -146,6 +146,10 @@ public void export(string location) { } + public byte[] exportRawBytes() { + return this.currentSave.Write(); + } + // Getters and Setters diff --git a/pkhex/pkhex-egglocke/pkhex-egglocke/TestRoute.cs b/pkhex/pkhex-egglocke/pkhex-egglocke/TestRoute.cs deleted file mode 100644 index 147e87b..0000000 --- a/pkhex/pkhex-egglocke/pkhex-egglocke/TestRoute.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Grapevine; -using PKHeX.Core; - -namespace pkhex_egglocke -{ - [RestResource] - public class MyResource - { - [RestRoute("Get", "/api/test")] - public async Task Test(IHttpContext context) - { - await context.Response.SendResponseAsync("Successfully hit the test route!"); - } - - [RestRoute("Get", "/api/startfile")] - public async Task StartFile(IHttpContext context) - { - await context.Response.SendResponseAsync("Successfully hit the startfile route!"); - } - - [RestRoute("Get", "/api/file1")] - public async Task DownloadFile(IHttpContext context) - { - string filePath = "C:\\Users\\Evin Jaff\\Downloads\\Pokemon.mp3"; - - context.Response.ContentType = "audio/mpeg"; - context.Response.AddHeader("Content-Disposition", "attachment; filename=blank.mp3"); - context.Response.ContentLength64 = new FileInfo(filePath).Length; - - await context.Response.SendResponseAsync(File.ReadAllBytes(filePath)); - - } - - } -}