forked from SUDALV92/OctopathTraveler-TreasureChests-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "Save Data" to json function menu;
Replace the menu button image to keep the style consistent;
- Loading branch information
1 parent
cf05cec
commit 978c6c0
Showing
22 changed files
with
1,004 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace GvasFormat | ||
{ | ||
public class CustomFormatData | ||
{ | ||
public int Count; | ||
public CustomFormatDataEntry[] Entries; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace GvasFormat | ||
{ | ||
public class CustomFormatDataEntry | ||
{ | ||
public Guid Id; | ||
public int Value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace GvasFormat | ||
{ | ||
public struct EngineVersion | ||
{ | ||
public short Major; | ||
public short Minor; | ||
public short Patch; | ||
public int Build; | ||
public string BuildId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using GvasFormat.Serialization.UETypes; | ||
|
||
namespace GvasFormat | ||
{ | ||
/* | ||
* General format notes: | ||
* Strings are 4-byte length + value + \0, length includes \0 | ||
* | ||
*/ | ||
[DataContract] | ||
public class Gvas | ||
{ | ||
public static readonly byte[] Header = Encoding.ASCII.GetBytes("GVAS"); | ||
[DataMember(Order = 0)] | ||
public int SaveGameVersion; | ||
[DataMember(Order = 1)] | ||
public int PackageVersion; | ||
[DataMember(Order = 2)] | ||
public EngineVersion EngineVersion = new EngineVersion(); | ||
[DataMember(Order = 3)] | ||
public int CustomFormatVersion; | ||
[DataMember(Order = 4)] | ||
public CustomFormatData CustomFormatData = new CustomFormatData(); | ||
[DataMember(Order = 5)] | ||
public string SaveGameType; | ||
[DataMember(Order = 6)] | ||
public List<UEProperty> Properties = new List<UEProperty>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using GvasFormat; | ||
using GvasFormat.Serialization; | ||
using GvasFormat.Serialization.UETypes; | ||
|
||
namespace OctopathTraveler.GvasFormat | ||
{ | ||
class GvasConverter | ||
{ | ||
public static (bool, Exception) Convert2JsonFile(string outputPath, Stream gvasStream) | ||
{ | ||
(Gvas save, Exception e) = UESerializer.Read(gvasStream); | ||
if (save == null) return (false, e); | ||
|
||
var jsonNode = JsonSerializer.SerializeToNode<Gvas>(save, new JsonSerializerOptions | ||
{ | ||
IncludeFields = true, | ||
MaxDepth = 64, | ||
Converters = { new UEPropJsonConvert() } | ||
}); | ||
File.WriteAllText(outputPath, jsonNode.ToJsonString(new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, | ||
})); | ||
return (true, e); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Optimize json output | ||
/// </summary> | ||
public class UEPropJsonConvert : JsonConverter<UEProperty> | ||
{ | ||
public override UEProperty? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, UEProperty value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteRawValue(JsonSerializer.SerializeToUtf8Bytes(value.ToObject(), options)); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
OctopathTraveler/GvasConverter/Serialization/BinaryReaderEx.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace GvasFormat.Serialization | ||
{ | ||
public static class BinaryReaderEx | ||
{ | ||
private static readonly Encoding Utf8 = new UTF8Encoding(false); | ||
|
||
public static void Terminator(this BinaryReader reader) | ||
{ | ||
var terminator = reader.ReadByte(); | ||
if (terminator != 0) | ||
{ | ||
throw new FormatException($"Offset: 0x{reader.BaseStream.Position - 1:x8}. Expected terminator (0x00), but was (0x{terminator:x2})"); | ||
} | ||
} | ||
|
||
public static string ReadUEString(this BinaryReader reader) | ||
{ | ||
if (reader.PeekChar() < 0) | ||
return null; | ||
|
||
// ue字符串通常以0结尾,length包含null | ||
var lengthOffset = reader.BaseStream.Position; | ||
var length = reader.ReadInt32(); | ||
if (length == 0) | ||
return null; | ||
|
||
if (length == 1) | ||
return ""; | ||
|
||
var valueBytes = new byte[length]; | ||
|
||
int i = 0; | ||
for (; i < length; i++) | ||
{ | ||
var b = reader.ReadByte(); | ||
if (b == 0) break; | ||
valueBytes[i] = b; | ||
} | ||
|
||
var str = Utf8.GetString(valueBytes, 0, length - 1); | ||
|
||
// 如果读出来和length不一样,那么肯定是哪里分析错了 | ||
if (length != str.Length + 1) | ||
throw new FormatException($"Offset: 0x{lengthOffset:x8} read string error."); | ||
|
||
return str; | ||
} | ||
} | ||
} |
Oops, something went wrong.