-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConfigNodeStorage.cs
82 lines (71 loc) · 2.89 KB
/
ConfigNodeStorage.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
using System;
using Log = KSPPP.Logging.ConsoleLogger;
namespace KSPPP {
public abstract class ConfigNodeStorage : IPersistenceLoad, IPersistenceSave {
Func<string> timeNow = () => string.Format("{0:ddMMyyyy-HHmmss}", DateTime.Now);
public ConfigNodeStorage() { } // FIXME: should be private?
public ConfigNodeStorage(string filePath) { FilePath = filePath; } // FIXME: should be private?
private string _FilePath;
public string FilePath {
get { return _FilePath; }
set { _FilePath = System.IO.Path.Combine(_AssemblyFolder, value).Replace("\\","/"); }
}
public string FileName { get { return System.IO.Path.GetFileName(FilePath); } }
public bool FileExists { get { return System.IO.File.Exists(FilePath); } }
void IPersistenceLoad.PersistenceLoad() { OnDecodeFromConfigNode(); }
void IPersistenceSave.PersistenceSave() { OnEncodeToConfigNode(); }
public virtual void OnDecodeFromConfigNode() { }
public virtual void OnEncodeToConfigNode() { }
public bool Load() { return Load(FilePath); }
public bool Load(string fileFullName) {
try {
Log.Debug("Loading ConfigNode");
if (FileExists) {
ConfigNode cnToLoad = ConfigNode.Load(fileFullName);
ConfigNode cnUnwrapped = cnToLoad.GetNode(GetType().Name);
ConfigNode.LoadObjectFromConfig(this, cnUnwrapped);
return true;
} else {
Log.Now("File could not be found to load({0})", fileFullName);
return false;
}
} catch (Exception ex) {
Log.Now("Failed to Load ConfigNode from file ({0}) - Error:{1}", fileFullName, ex.Message);
Log.Now("Storing old config - {0}", fileFullName + ".err-" + timeNow);
System.IO.File.Copy(fileFullName, fileFullName + ".err-" + timeNow,true);
return false;
}
}
public bool Save() {
Log.Debug("Saving ConfigNode");
return Save(FilePath);
}
public bool Save(string fileFullName) {
try {
ConfigNode cnToSave = AsConfigNode;
ConfigNode cnSaveWrapper = new ConfigNode(GetType().Name);
cnSaveWrapper.AddNode(cnToSave);
cnSaveWrapper.Save(fileFullName);
return true;
} catch (Exception ex) {
Log.Now("Failed to Save ConfigNode to file({0})-Error:{1}", fileFullName, ex.Message);
return false;
}
}
public ConfigNode AsConfigNode {
get {
try {
ConfigNode cnTemp = new ConfigNode(GetType().Name);
cnTemp = ConfigNode.CreateConfigFromObject(this, cnTemp);
return cnTemp;
} catch (Exception ex) {
Log.Now("Failed to generate ConfigNode-Error;{0}", ex.Message);
return new ConfigNode(GetType ().Name);
}
}
}
internal static string _AssemblyName { get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; } }
internal static string _AssemblyLocation { get { return System.Reflection.Assembly.GetExecutingAssembly().Location; } }
internal static string _AssemblyFolder { get { return System.IO.Path.GetDirectoryName(_AssemblyLocation); } }
}
}