Skip to content

Commit

Permalink
Merge pull request #12 from evinjaff/dockerized-http-pkhex
Browse files Browse the repository at this point in the history
Dockerized http pkhex (in progress)
  • Loading branch information
evinjaff authored Jul 9, 2024
2 parents d98fde6 + 7c546cf commit 13bb25a
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 10 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/docker-pkhex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test pkhex Docker Building

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build the Docker image
run: docker build . --file dockerfile --tag my-image-name:$(date +%s)
working-directory: ./pkhex
3 changes: 3 additions & 0 deletions pkhex/docker_command.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker build -t pkhex .

docker run -d -it -p 8000:8000 pkhex
27 changes: 27 additions & 0 deletions pkhex/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM ubuntu:22.04 AS builder

# install the .NET 8 SDK
RUN apt-get update && apt-get install -y dotnet-sdk-8.0 ca-certificates

# add your application code
WORKDIR /source

# copy pkhex-egglocke folder to the container
COPY pkhex-egglocke .


# Install pkhex dependency
RUN dotnet restore pkhex-egglocke

# Compile the app to test
RUN dotnet build --no-restore pkhex-egglocke

# Publish
RUN dotnet publish -c release -r linux-x64 --self-contained


ENV PORT 1234
EXPOSE 1234

WORKDIR /source/pkhex-egglocke
ENTRYPOINT ["dotnet", "run"]
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Grapevine" Version="5.0.0-rc.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
Expand Down
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);

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

namespace pkhex_egglocke
{
internal class JSONDecoder
internal class HTTPServer
{

// Very annoying code that decodes strings to class-defined integers

}
}
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.
}

}

}
32 changes: 27 additions & 5 deletions pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
// See https://aka.ms/new-console-template for more information


using PKHeX.Core;
using pkhexEgglocke;
using System.Reflection;
using Grapevine;
using System;



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 @@ -24,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
7 changes: 7 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public void addEgg( EggCreator pokemon, int boxIndex) {
mew.OriginalTrainerName = pokemon.OT;
mew.OriginalTrainerGender = pokemon.OTGender;

mew.HeldItem =



mew.Ability = pokemon.Ability;
mew.Nature = pokemon.Nature;
Expand Down Expand Up @@ -143,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
3 changes: 2 additions & 1 deletion pkhex/pkhex-egglocke/pkhex-egglocke/pkhexEgglocke.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grapevine" Version="5.0.0-rc.10" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PKHeX.Core" Version="24.5.5" />
<PackageReference Include="PKHeX.Core" Version="24.5.5" />
</ItemGroup>

</Project>

0 comments on commit 13bb25a

Please sign in to comment.