-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cs
181 lines (164 loc) · 7.75 KB
/
Settings.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace JSONExtension
{
public class Settings
{
public string projectPath;
public Dictionary<string, string> langFile; //contains keys and values of language file
public bool isLoaded = false; //flag after loading langFile set to true
public string languagePath;
public string languageCode;
private JObject data;
public void Initialize() //called at the start by JSONExtensionPackage
{
ThreadHelper.ThrowIfNotOnUIThread();
projectPath = GetProjectPath();
LoadLangFile();
}
private string GetProjectPath()
{
ThreadHelper.ThrowIfNotOnUIThread();
IVsSolution solution = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution; //get solution reference from a service provider
if (solution != null)
{
solution.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out object open);
bool isOpen = (bool)open; //if the solution open
if (isOpen)
{
solution.GetSolutionInfo(out string dir, out _, out _); //dir contains the solution's directory path
return dir;
}
}
return null;
}
public void LoadLangFile(bool forceReload = false, bool verbose = false) //load language file
{
ThreadHelper.ThrowIfNotOnUIThread();
if (forceReload)
isLoaded = false;
if (isLoaded)
return;
if (!string.IsNullOrEmpty(projectPath))
{
string settingsPath = Path.Combine(projectPath, ".JSONExSettings");
if (File.Exists(settingsPath))
{
string json = File.ReadAllText(settingsPath);
try
{
SettingsJSON settingsJSON = JsonConvert.DeserializeObject<SettingsJSON>(json); //Using JsonConvert to deserialize and check jsonPath
languagePath = settingsJSON.languagePath;
languageCode = settingsJSON.languageCode;
string temp = File.ReadAllText(languagePath);
data = (JObject)JsonConvert.DeserializeObject(temp);
string lang = data[languageCode].Value<JObject>().ToString();
langFile = JsonConvert.DeserializeObject<Dictionary<string, string>>(lang);
isLoaded = true;
}
catch (Exception ex)
{
ShowMessageAndStopExecution($"Invalid .JSONExSettings file: {ex.Message}");
}
}
else if (verbose)
{
ShowMessageAndStopExecution("Path not set!\nMake sure to set it using JSONEx settings in Tools menu.");
}
}
else if (verbose)
{
ShowMessageAndStopExecution("Error getting project path!\nOpen the project first.");
}
}
public void EditEntry(string oldKey, string newKey, string oldValue, string newValue)
{
ThreadHelper.JoinableTaskFactory.Run(async delegate
{ //switch to main thread
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (string.IsNullOrEmpty(newKey) || string.IsNullOrEmpty(newValue))
{
MessageBox.Show("Key edit failed!\nNew key/value cannot be empty.", "JSONEx");
return;
}
if (isLoaded)
{
if (string.IsNullOrEmpty(oldKey)) //if there was no key (key input in dialog)
{
langFile.Add(newKey, newValue); //create new key and value in json
Save();
}
else
{
if (string.IsNullOrEmpty(oldValue)) //if there was a key before a dialog, but it had no value - therefore it was not available in .json
{
if ((string.Compare(oldKey, newKey) == 0) || ((string.Compare(oldKey, newKey) != 0) && !langFile.ContainsKey(newKey)))
{
langFile.Add(newKey, newValue); //create new key and value in json
Save();
if (string.Compare(oldKey, newKey) != 0) //if key has changed
{
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
dte.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, "\"" + oldKey + "\"", (int)vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen | (int)vsFindOptions.vsFindOptionsMatchCase, "\"" + newKey + "\"", vsFindTarget.vsFindTargetCurrentProject);
}
}
else
{
MessageBox.Show("Key edit failed!\nYou cannot rename a key to existing key.", "JSONEx");
return;
}
}
else //if there was a key and it had a value
{
if (langFile.ContainsKey(oldKey)) //extra precaucion
{
if ((string.Compare(oldKey, newKey) == 0) || ((string.Compare(oldKey, newKey) != 0) && !langFile.ContainsKey(newKey)))
{
langFile.Remove(oldKey); //remove old key
langFile.Add(newKey, newValue); //create new key and value in json
Save();
if (string.Compare(oldKey, newKey) != 0) //if key has changed
{
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
dte.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, "\"" + oldKey + "\"", (int)vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen | (int)vsFindOptions.vsFindOptionsMatchCase, "\"" + newKey + "\"", vsFindTarget.vsFindTargetCurrentProject);
}
}
else
{
MessageBox.Show("Key edit failed!\nYou cannot rename a key to existing key.", "JSONEx");
return;
}
}
}
}
}
});
}
public void Save()
{
if (!isLoaded)
return;
data[languageCode] = JToken.FromObject(langFile);
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
File.WriteAllText(languagePath, json);
}
private void ShowMessageAndStopExecution(string message)
{
ThreadHelper.ThrowIfNotOnUIThread();
MessageBox.Show(message, "JSONEx");
return;
}
}
public class SettingsJSON //structure for JSON serialization
{
public string languagePath;
public string languageCode;
}
}