Skip to content

Commit

Permalink
Merge pull request #356 from BUTR/dev
Browse files Browse the repository at this point in the history
v2.8.14
  • Loading branch information
Aragas authored Nov 13, 2023
2 parents 65da602 + 1bede0c commit 9449659
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
shell: pwsh

- name: ReportGenerator
uses: danielpalme/[email protected].25
uses: danielpalme/[email protected].26
with:
reports: '*.xml'
targetdir: 'coveragereport'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
shell: pwsh

- name: ReportGenerator
uses: danielpalme/[email protected].25
uses: danielpalme/[email protected].26
with:
reports: 'coverage_butterlib_stable_debug.xml;coverage_butterlib_stable_release.xml;coverage_butterlib_impl_stable_debug.xml;coverage_butterlib_impl_stable_release.xml;coverage_butterlib_impl_beta_debug.xml;coverage_butterlib_impl_beta_release.xml;'
targetdir: 'coveragereport'
Expand Down
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!--Development Variables-->
<PropertyGroup>
<!--Module Version-->
<Version>2.8.13</Version>
<Version>2.8.14</Version>
<!--Harmony Version-->
<HarmonyVersion>2.2.2</HarmonyVersion>
<HarmonyExtensionsVersion>3.2.0.77</HarmonyExtensionsVersion>
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---------------------------------------------------------------------------------------------------
Version: 2.8.14
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.1.6,v1.2.4
* Added a SyncDataAsJson overload that takes a JsonSerializer
---------------------------------------------------------------------------------------------------
Version: 2.8.13
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1,v1.1.2,v1.1.3,v1.1.4,v1.1.5,v1.2.0,v1.2.1,v1.2.2,v1.2.3
* Might have fixed a reported save issue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

Expand All @@ -28,15 +30,24 @@ private static string ChunksToString(IReadOnlyList<string> chunks)
return strBuilder.ToString();
}

public static bool SyncDataAsJson<T>(this IDataStore dataStore, string key, ref T? data, JsonSerializerSettings? settings = null)
private static string Serialize<T>(ref T? data, JsonSerializer serializer)
{
// If the type we're synchronizing is a string or string array, then it's ambiguous
// with our own internal storage types, which imply that the strings contain valid
// JSON. Standard binary serialization can handle these types just fine, so we avoid
// the ambiguity by passing this data straight to the game's binary serializer.
if (typeof(T) == typeof(string) || typeof(T) == typeof(string[]))
return dataStore.SyncData(key, ref data);
var sb = new StringBuilder(256);
var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
using var jsonWriter = new JsonTextWriter(sw);
jsonWriter.Formatting = Formatting.None;
serializer.Serialize(jsonWriter, data, typeof(T));
return sb.ToString();
}

private static T? Deserialize<T>(string json, JsonSerializer serializer)
{
using var reader = new JsonTextReader(new StringReader(json));
return serializer.Deserialize<T>(reader);
}

public static bool SyncDataAsJson<T>(this IDataStore dataStore, string key, ref T? data, JsonSerializerSettings? settings = null)
{
settings ??= new JsonSerializerSettings
{
ContractResolver = new TaleWorldsContractResolver(),
Expand All @@ -46,11 +57,24 @@ public static bool SyncDataAsJson<T>(this IDataStore dataStore, string key, ref
//PreserveReferencesHandling = PreserveReferencesHandling.Objects
};

return SyncDataAsJson(dataStore, key, ref data, JsonSerializer.Create(settings));
}

public static bool SyncDataAsJson<T>(this IDataStore dataStore, string key, ref T? data, JsonSerializer serializer)
{
// If the type we're synchronizing is a string or string array, then it's ambiguous
// with our own internal storage types, which imply that the strings contain valid
// JSON. Standard binary serialization can handle these types just fine, so we avoid
// the ambiguity by passing this data straight to the game's binary serializer.
if (typeof(T) == typeof(string) || typeof(T) == typeof(string[]))
return dataStore.SyncData(key, ref data);

if (dataStore.IsSaving)
{
var dataJson = JsonConvert.SerializeObject(data, Formatting.None, settings);
var jsonData = JsonConvert.SerializeObject(new JsonData(2, dataJson));
var chunks = ToChunks(jsonData, short.MaxValue - 1024).ToArray();
var dataJson = Serialize(ref data, serializer);
var jsonData = new JsonData(2, dataJson);
var jsonDataJson = Serialize(ref jsonData, serializer);
var chunks = ToChunks(jsonDataJson, short.MaxValue - 1024).ToArray();
return dataStore.SyncData(key, ref chunks);
}

Expand All @@ -63,10 +87,10 @@ public static bool SyncDataAsJson<T>(this IDataStore dataStore, string key, ref
var jsonDataChunks = Array.Empty<string>();
if (dataStore.SyncData(key, ref jsonDataChunks))
{
var (format, jsonData) = JsonConvert.DeserializeObject<JsonData?>(ChunksToString(jsonDataChunks ?? Array.Empty<string>()), settings) ?? new(-1, string.Empty);
var (format, jsonData) = Deserialize<JsonData?>(ChunksToString(jsonDataChunks ?? Array.Empty<string>()), serializer) ?? new(-1, string.Empty);
data = format switch
{
2 => JsonConvert.DeserializeObject<T>(jsonData, settings),
2 => Deserialize<T>(jsonData, serializer),
_ => data
};
return true;
Expand All @@ -80,7 +104,7 @@ public static bool SyncDataAsJson<T>(this IDataStore dataStore, string key, ref
var jsonData = "";
if (dataStore.SyncData(key, ref jsonData)) // try to get as JSON string
{
data = JsonConvert.DeserializeObject<T>(jsonData, settings);
data = Deserialize<T>(jsonData, serializer);
return true;
}
}
Expand Down

0 comments on commit 9449659

Please sign in to comment.