Skip to content

Commit

Permalink
Added "Save Data" to json function menu;
Browse files Browse the repository at this point in the history
Replace the menu button image to keep the style consistent;
  • Loading branch information
LonelyWindG committed Mar 16, 2023
1 parent cf05cec commit 978c6c0
Show file tree
Hide file tree
Showing 22 changed files with 1,004 additions and 78 deletions.
5 changes: 5 additions & 0 deletions OctopathTraveler/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;

namespace OctopathTraveler
Expand Down Expand Up @@ -120,6 +121,10 @@ public DataContext()
continue;

uint treausreAddress = treasures[i] + 100;
if (tid == 1277013)
{
Trace.Write($"{i + 1}/{treasures.Count}");
}

//var data = gvas.Key("TreasureStateArray_" + i);
var treasure = new TreasureState(treausreAddress, info);
Expand Down
8 changes: 8 additions & 0 deletions OctopathTraveler/GvasConverter/CustomFormatData.cs
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;
}
}
10 changes: 10 additions & 0 deletions OctopathTraveler/GvasConverter/CustomFormatDataEntry.cs
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;
}
}
11 changes: 11 additions & 0 deletions OctopathTraveler/GvasConverter/EngineVersion.cs
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;
}
}
32 changes: 32 additions & 0 deletions OctopathTraveler/GvasConverter/Gvas.cs
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>();
}
}
49 changes: 49 additions & 0 deletions OctopathTraveler/GvasConverter/GvasConverter.cs
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 OctopathTraveler/GvasConverter/Serialization/BinaryReaderEx.cs
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;
}
}
}
Loading

0 comments on commit 978c6c0

Please sign in to comment.