Skip to content

Commit

Permalink
Download endpoint works!!
Browse files Browse the repository at this point in the history
  • Loading branch information
evinjaff committed Jun 9, 2024
1 parent 48a950b commit 7c546cf
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 64 deletions.
8 changes: 4 additions & 4 deletions pkhex/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
WORKDIR /source/pkhex-egglocke
ENTRYPOINT ["dotnet", "run"]
5 changes: 5 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public EggCreator(byte ball, ushort dexNumber, int language, int ability, Nature


public static EggCreator decodeJSON(string filepath, bool is_filepath) {
/// <summary>
/// decodeJSON: Decodes a JSON object into a PokemonCreator object
/// </summary>
///

string json;
if (is_filepath)
{
Expand Down
15 changes: 15 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/HTTPModels.cs
Original file line number Diff line number Diff line change
@@ -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<dynamic>();
}
}
69 changes: 69 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/HTTPRoutes.cs
Original file line number Diff line number Diff line change
@@ -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<SaveFileGeneratorModel>(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);

}


}
}
15 changes: 0 additions & 15 deletions pkhex/pkhex-egglocke/pkhex-egglocke/JSONDecoder.cs

This file was deleted.

30 changes: 30 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/JSONDeserializer.cs
Original file line number Diff line number Diff line change
@@ -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<T> DeserializeAsync<T>(Stream stream)
{
return await DeserializeAsync<T>(stream, CancellationToken.None);
}

public async Task<T> DeserializeAsync<T>(Stream stream, CancellationToken token)
{
return await JsonSerializer.DeserializeAsync<T>(stream, SerializerOptions, token);

Check warning on line 25 in pkhex/pkhex-egglocke/pkhex-egglocke/JSONDeserializer.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

}

}
19 changes: 14 additions & 5 deletions pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Check warning on line 34 in pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Check warning on line 34 in pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'BLANK_SOULSILVER_SAVE' is assigned but its value is never used

SaveWriter sw = new SaveWriter(BLANK_SOULSILVER_SAVE);
// SaveWriter sw = new SaveWriter(BLANK_SOULSILVER_SAVE);

//EggCreator pc = new EggCreator();

Expand All @@ -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");

}

Expand Down
4 changes: 4 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public void export(string location) {

}

public byte[] exportRawBytes() {
return this.currentSave.Write();

Check warning on line 150 in pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}


// Getters and Setters

Expand Down
40 changes: 0 additions & 40 deletions pkhex/pkhex-egglocke/pkhex-egglocke/TestRoute.cs

This file was deleted.

0 comments on commit 7c546cf

Please sign in to comment.