forked from James231/GCode-Razor-PostProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializer.cs
More file actions
113 lines (102 loc) · 3.78 KB
/
Serializer.cs
File metadata and controls
113 lines (102 loc) · 3.78 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using GCodeRazor.Examples;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GCodeRazor
{
public static class Serializer
{
public static Settings GetSettings()
{
string settingsFilePath = GetSettingsFilePath();
if (!File.Exists(settingsFilePath))
{
return CreatNewSettings();
}
try
{
StreamReader reader = new StreamReader(settingsFilePath);
string settingsJson = reader.ReadToEnd();
reader.Close();
return JsonConvert.DeserializeObject<Settings>(settingsJson);
}
catch
{
return CreatNewSettings();
}
}
private static Settings CreatNewSettings()
{
Settings settings = new Settings();
settings.SetDefaults();
string settingsJson = JsonConvert.SerializeObject(settings, Formatting.Indented);
StreamWriter writer = new StreamWriter(GetSettingsFilePath(), false);
writer.Write(settingsJson);
writer.Close();
return settings;
}
private static string GetSettingsFilePath()
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
}
private static string GetTempFolder()
{
string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
return folderPath;
}
public static string SaveTemporaryFile(string content)
{
string temporaryFileName = Guid.NewGuid() + ".nc";
string filePath = Path.Combine(GetTempFolder(), temporaryFileName);
StreamWriter writer = new StreamWriter(filePath, false);
writer.Write(content);
writer.Close();
return filePath;
}
public static IExampleControlModel[] GetExamples()
{
string examplePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "examples.json");
if (!File.Exists(examplePath))
{
IExampleControlModel[] examples = JsonConvert.DeserializeObject<IExampleControlModel[]>(GetDefaultExamplesJson());
string json = JsonConvert.SerializeObject(examples, Formatting.Indented);
StreamWriter writer = new StreamWriter(examplePath, false);
int length = json.Length;
writer.Write(json);
writer.Close();
return examples;
}
try
{
StreamReader reader = new StreamReader(examplePath);
string jsonContent = reader.ReadToEnd();
reader.Close();
IExampleControlModel[] examplesArr = JsonConvert.DeserializeObject<IExampleControlModel[]>(jsonContent);
return examplesArr;
} catch
{
}
return JsonConvert.DeserializeObject<IExampleControlModel[]>(GetDefaultExamplesJson());
}
public static string GetDefaultExamplesJson()
{
var resourceName = "GCodeRazor.Resources.examples.json";
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}