Skip to content

Commit

Permalink
perf: Storing the JsonFile assets as byte[] instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed May 20, 2024
1 parent b0cec83 commit dbd0065
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,9 @@ internal abstract class LDtkJsonImporter<T> : LDtkJsonImporter where T : Scripta
{
protected T ReadAssetText()
{
LDtkProfiler.BeginSample("ReadAllText");
string jsonText = File.ReadAllText(assetPath);
//todo don't need to read all bytes twice. pass in a previous FromJson
LDtkProfiler.BeginSample("ReadAllBytes");
byte[] jsonText = File.ReadAllBytes(assetPath);
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("ScriptableObject.CreateInstance");
Expand Down
2 changes: 1 addition & 1 deletion Assets/LDtkUnity/Runtime/Data/Extensions/Level/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class Level : ILDtkUid, ILDtkIdentifier, ILDtkIid
/// <returns>
/// The deserialized level.
/// </returns>
public static Level FromJson(string json)
public static Level FromJson(byte[] json)
{
return Utf8Json.JsonSerializer.Deserialize<Level>(json);
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/LDtkUnity/Runtime/Data/Schema/LdtkJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,6 @@ public partial class LdtkJson

public partial class LdtkJson
{
public static LdtkJson FromJson(string json) => JsonSerializer.Deserialize<LdtkJson>(json);
public static LdtkJson FromJson(byte[] json) => JsonSerializer.Deserialize<LdtkJson>(json);
}
}
2 changes: 1 addition & 1 deletion Assets/LDtkUnity/Runtime/UnityAssets/Json/ILDtkJsonFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
internal interface ILDtkJsonFile
{
void SetJson(string json);
void SetJson(byte[] json);
}
}
6 changes: 3 additions & 3 deletions Assets/LDtkUnity/Runtime/UnityAssets/Json/LDtkJsonFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
namespace LDtkUnity
{
[ExcludeFromDocs]
public abstract class LDtkJsonFile<T> : ScriptableObject, ILDtkJsonFile //todo we really need to make this a TextAsset
public abstract class LDtkJsonFile<T> : ScriptableObject, ILDtkJsonFile
{
[SerializeField] protected string _json;
[SerializeField] protected byte[] _json;

public abstract T FromJson { get; }

public virtual void SetJson(string json)
public virtual void SetJson(byte[] json)
{
_json = json;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LDtkTilesetDefinitionWrapper
/// </summary>
public TilesetDefinition Def;

public static LDtkTilesetDefinitionWrapper FromJson(string json)
public static LDtkTilesetDefinitionWrapper FromJson(byte[] json)
{
return JsonSerializer.Deserialize<LDtkTilesetDefinitionWrapper>(json);
}
Expand Down

0 comments on commit dbd0065

Please sign in to comment.