Skip to content

Commit

Permalink
Add JSON export and import
Browse files Browse the repository at this point in the history
  • Loading branch information
Thealexbarney committed Jun 5, 2018
1 parent b80af3c commit 974e6af
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 7 deletions.
2 changes: 1 addition & 1 deletion PqSave/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Weavers>
<Costura />
<Costura CreateTemporaryAssemblies='true' />
</Weavers>
109 changes: 109 additions & 0 deletions PqSave/Json.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using ZeroFormatter;

namespace PqSave
{
public static class Json
{
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Converters = new List<JsonConverter>
{
new LazyDictionaryConverter1(),
new LazyDictionaryConverter2(),
new LazyDictionaryConverter3(),
new LazyDictionaryConverter4()
}
};

public static string Serialize(SaveManager save)
{
return JsonConvert.SerializeObject(save, Formatting.Indented, Settings);
}

public static SaveManager DeSerialize(string save)
{
return JsonConvert.DeserializeObject<SaveManager>(save, Settings);
}
}

public class LazyDictionaryConverter1 : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dict = ((ILazyDictionary<int, CharacterStorage.ManageData>)value).ToDictionary(x => x.Key, x => x.Value);
serializer.Serialize(writer, dict);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize<Dictionary<int, CharacterStorage.ManageData>>(reader).AsLazyDictionary();
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(ILazyDictionary<int, CharacterStorage.ManageData>);
}
}

public class LazyDictionaryConverter2 : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dict = ((ILazyDictionary<int, DungeonHighScore>)value).ToDictionary(x => x.Key, x => x.Value);
serializer.Serialize(writer, dict);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize<Dictionary<int, DungeonHighScore>>(reader).AsLazyDictionary();
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(ILazyDictionary<int, DungeonHighScore>);
}
}

public class LazyDictionaryConverter3 : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dict = ((ILazyDictionary<int, SaveCharacterData>)value).ToDictionary(x => x.Key, x => x.Value);
serializer.Serialize(writer, dict);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize<Dictionary<int, SaveCharacterData>>(reader).AsLazyDictionary();
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(ILazyDictionary<int, SaveCharacterData>);
}
}

public class LazyDictionaryConverter4 : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dict = ((ILazyDictionary<int, StoneData>)value).ToDictionary(x => x.Key, x => x.Value);
serializer.Serialize(writer, dict);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize<Dictionary<int, StoneData>>(reader).AsLazyDictionary();
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(ILazyDictionary<int, StoneData>);
}
}
}
1 change: 1 addition & 0 deletions PqSave/PqSave.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Costura.Fody" Version="2.0.1" Condition=" '$(TargetFramework)' == 'net46' " />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="ZeroFormatter" Version="1.6.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="2.8.2" />
</ItemGroup>
Expand Down
18 changes: 15 additions & 3 deletions PqSave/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ private static void Run(string[] args)
var decSave = File.ReadAllBytes(args[1]);
File.WriteAllBytes(args[2], Encryption.EncryptSave(decSave));
break;
case "x":
var savex = new SaveManager(File.ReadAllBytes(args[1]));
var output = Json.Serialize(savex);
File.WriteAllText(args[2], output);
break;
case "i":
string import = File.ReadAllText(args[1]);
SaveManager savei = Json.DeSerialize(import);
File.WriteAllBytes(args[2], savei.Export());
break;
case "s":
var save = new SaveManager(File.ReadAllBytes(args[1]));

Expand All @@ -54,10 +64,12 @@ private static void Run(string[] args)

private static void PrintUsage()
{
Console.WriteLine("Usage: pqsave mode input output [script1] [script2]...");
Console.WriteLine("Usage: pqsave mode input output [script1 (In script mode only)] [script2]...");
Console.WriteLine(" modes:");
Console.WriteLine(" d Decrypt");
Console.WriteLine(" e Encrypt");
Console.WriteLine(" d Decrypt save");
Console.WriteLine(" e Encrypt save");
Console.WriteLine(" x Export save to JSON");
Console.WriteLine(" i Import save from JSON");
Console.WriteLine(" s Script - Run scripts on an encrypted save");
}
}
Expand Down
2 changes: 2 additions & 0 deletions PqSave/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public SaveManager(byte[] saveEnc)
SerializeData = ZeroFormatterSerializer.Deserialize<SerializeData>(saveDec, 56);
}

public SaveManager() { }

public byte[] Export()
{
var head = ZeroFormatterSerializer.Serialize(CheckData);
Expand Down
2 changes: 2 additions & 0 deletions PqSave/Scripts/export_to_json.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PqSave.exe x user user.json
pause
2 changes: 2 additions & 0 deletions PqSave/Scripts/import_from_json.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PqSave.exe i user.json user_new
pause
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Pokémon Quest's title ID is `01005D100807A000`, and the save file is a single 5

### Usage
````
Usage: pqsave mode input output [script1] [script2]...
Usage: pqsave mode input output [script1 (In script mode only)] [script2]...
modes:
d Decrypt
e Encrypt
d Decrypt save
e Encrypt save
x Export save to JSON
i Import save from JSON
s Script - Run scripts on an encrypted save
````

Expand Down

0 comments on commit 974e6af

Please sign in to comment.