-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainSingleton.cs
79 lines (65 loc) · 2.05 KB
/
MainSingleton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Ceira.Classes;
using Godot;
using Newtonsoft.Json;
using System;
using System.IO;
public class MainSingleton : Node
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
public Settings AppSettings = new Settings();
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
//string ConfigurationFilePath = System.File
string ConfigDirectory = System.IO.Path.Combine(AppContext.BaseDirectory, ".conf");
string ConfigFilePath = System.IO.Path.Combine(ConfigDirectory, "Settings.json");
//
// Create .conf directory and file
//
if (!System.IO.Directory.Exists(ConfigDirectory))
{
System.IO.Directory.CreateDirectory(ConfigDirectory);
CreateNewConfigFile(ConfigFilePath);
}
else
{
// If the user deleted config file
if (!System.IO.File.Exists(ConfigFilePath))
{
CreateNewConfigFile(ConfigFilePath);
}
}
//
// Read config file
//
ReadConfigFile(ConfigFilePath);
}
void ReadConfigFile(string configFilePath)
{
string ConfigFileString = System.IO.File.ReadAllText(configFilePath);
try
{
Settings parsedSettings = JsonConvert.DeserializeObject<Settings>(ConfigFileString);
AppSettings = parsedSettings;
}
catch (Newtonsoft.Json.JsonException)
{
GD.Print("Could not read configuration file.");
AppSettings = new Settings();
CreateNewConfigFile(configFilePath);
}
ApplyConfigFile();
}
void ApplyConfigFile()
{
OS.WindowFullscreen = AppSettings.Fullscreen;
}
void CreateNewConfigFile(string configFilePath)
{
AppSettings.LoadDefaultSettings();
System.IO.File.WriteAllText(configFilePath, JsonConvert.SerializeObject(AppSettings));
AppSettings.FirstTime = true;
}
}