Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
DefinitionDocumentPersistence.cs
1using System.Reflection;
5using Newtonsoft.Json;
6using Newtonsoft.Json.Serialization;
7
9
11internal static class DefinitionDocumentPersistence
12{
13 private static readonly IContractResolver Resolver = new DocumentResolver();
14
19 internal static string Serialize(object document, JsonSerializerSettings settings)
20 {
21 var serializer = JsonSerializer.Create(settings);
22 serializer.ContractResolver = Resolver;
23 using var writer = new StringWriter();
24 serializer.Serialize(writer, document);
25 return writer.ToString();
26 }
27
32 internal static void WriteAtomic(string path, string json, Action<string>? beforeInstall = null)
33 {
34 var directory = Path.GetDirectoryName(path)!;
35 Directory.CreateDirectory(directory);
36 var temporary = Path.Combine(directory, $".{Path.GetFileName(path)}.{Guid.NewGuid():N}.tmp");
37
38 try
39 {
40 File.WriteAllText(temporary, json);
41
42 if (beforeInstall is null)
43 {
44 File.Move(temporary, path, overwrite: true);
45 return;
46 }
47
48 var replacing = File.Exists(path);
49 beforeInstall(temporary);
50
51 if (replacing)
52 File.Replace(temporary, path, destinationBackupFileName: null);
53 else
54 File.Move(temporary, path);
55 }
56 finally
57 {
58 if (File.Exists(temporary))
59 File.Delete(temporary);
60 }
61 }
62
63 private sealed class DocumentResolver : DefaultContractResolver
64 {
69 protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
70 {
71 var property = base.CreateProperty(member, memberSerialization);
72
73 if (member.Name == "Source")
74 property.ShouldSerialize = value => value switch
75 {
76 TilesheetDefinition definition => definition.Source.Kind == TilesheetDefinitionSourceKind.Generated,
77 AnimationDefinition definition => definition.Source.Kind == AnimationDefinitionSourceKind.Generated,
78 SceneDefinition definition => definition.Source.Kind == SceneDefinitionSourceKind.Generated,
79 _ => true
80 };
81
82 return property;
83 }
84 }
85}
@ TilesheetDefinition
Represents a tilesheet definition file (.gts) for Gondwana.Drawing.Tilesheets.GTS....
Definition AssetTypes.cs:47
@ AnimationDefinition
Represents an animation definition file (.gani) for Gondwana.Drawing.Animation.GANI....
Definition AssetTypes.cs:57
@ SceneDefinition
Represents a scene definition file (.gscn) for Gondwana.Scenes.GSCN.SceneDefinition.
Definition AssetTypes.cs:52
AnimationDefinitionSourceKind
Identifies where a GANI animation definition originated.
TilesheetDefinitionSourceKind
Indicates where a tilesheet definition came from.
SceneDefinitionSourceKind
Indicates where a scene definition came from.