forked from malahx/ZeroMiniAVC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZeroMiniAVC.cs
177 lines (156 loc) · 4.69 KB
/
ZeroMiniAVC.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
/*
ZeroMiniAVC
Copyright 2016 Malah
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.IO;
using UnityEngine;
namespace ZeroMiniAVC {
[KSPAddon (KSPAddon.Startup.Instantly, true)]
public class ZeroMiniAVC : MonoBehaviour {
static ZeroMiniAVC Instance;
readonly string pruneExt = ".pruned";
readonly string cfgNode = "ZeroMiniAVC";
readonly string configPath = KSPUtil.ApplicationRootPath + "GameData/ZeroMiniAVC/Config.cfg";
ConfigNode config;
bool disabled = false;
bool prune = true;
bool delete = false;
bool noMessage = false;
ConfigNode loadConfig() {
ConfigNode[] _nodes = GameDatabase.Instance.GetConfigNodes (cfgNode);
if (_nodes.Length > 0) {
ConfigNode _node = _nodes[_nodes.Length - 1];
if (_node.HasValue ("disabled")) {
disabled = bool.Parse (_node.GetValue ("disabled"));
}
if (_node.HasValue ("prune")) {
prune = bool.Parse (_node.GetValue ("prune"));
}
if (_node.HasValue ("delete")) {
delete = bool.Parse (_node.GetValue ("delete"));
}
if (_node.HasValue ("noMessage")) {
noMessage = bool.Parse (_node.GetValue ("noMessage"));
}
return _node;
}
return new ConfigNode();
}
string mod(string path) {
string[] _splitedPath = path.Split (new char[2] { '/', '\\' });
string _mod = _splitedPath[_splitedPath.IndexOf ("GameData") + 1];
return _mod;
}
void Awake() {
if (Instance != null) {
Destroy (this);
return;
}
Instance = this;
DontDestroyOnLoad (Instance);
config = loadConfig ();
Debug.Log ("ZeroMiniAVC: Awake");
}
void Start() {
if (disabled) {
Debug.LogWarning ("ZeroMiniAVC: Disabled ... destroy.");
Destroy (this);
return;
}
screenMsg ("ZeroMiniAVC started ...");
cleanMiniAVC ();
if (!prune && !delete) {
cleanData ();
}
ConfigNode _config = new ConfigNode ();
_config.AddNode (config);
_config.Save (configPath);
screenMsg ("ZeroMiniAVC destroyed...");
Destroy (this);
}
void cleanMiniAVC() {
AssemblyLoader.LoadedAssembyList _assemblies = AssemblyLoader.loadedAssemblies;
for (int _i = _assemblies.Count - 1; _i >= 0; --_i) {
AssemblyLoader.LoadedAssembly _assembly = _assemblies[_i];
if (_assembly.name == "MiniAVC") {
_assembly.Unload ();
AssemblyLoader.loadedAssemblies.RemoveAt (_i);
string _mod = mod (_assembly.path);
string _prunePath = _assembly.path + pruneExt;
if (File.Exists (_prunePath)) {
File.Delete (_prunePath);
}
if (prune) {
File.Move (_assembly.path, _prunePath);
ConfigNode _cfgMod = config.AddNode ("mod");
_cfgMod.AddValue ("name", _mod);
_cfgMod.AddValue ("pruned", _prunePath);
screenMsg ("MiniAVC pruned for " + _mod);
}
else if (delete) {
File.Delete (_assembly.path);
screenMsg ("MiniAVC deleted for " + _mod);
}
else {
screenMsg ("MiniAVC disabled for " + _mod);
}
}
}
}
void cleanData() {
ConfigNode[] _cfgMods = config.GetNodes ("mod");
for (int _i = _cfgMods.Length - 1; _i >= 0; --_i) {
ConfigNode _cfgMod = _cfgMods[_i];
string _prunedPath = _cfgMod.GetValue ("pruned");
string _mod = _cfgMod.GetValue ("name");
if (File.Exists (_prunedPath)) {
string _unprunedPath = _prunedPath.Substring (0, _prunedPath.Length - pruneExt.Length);
if (File.Exists (_unprunedPath)) {
File.Delete (_prunedPath);
screenMsg ("MiniAVC deleted prune duplication for " + _mod);
}
else {
File.Move (_prunedPath, _unprunedPath);
screenMsg ("MiniAVC unpruned for " + _mod);
}
}
else {
screenMsg ("MiniAVC data removed for " + _mod);
}
config.RemoveNode (_cfgMod);
}
}
void screenMsg(string msg) {
Debug.LogWarning (msg);
if (noMessage) {
return;
}
ScreenMessages.PostScreenMessage (msg, 10);
}
}
}
// From MiniAVC GPLv3 Copyright (C) 2014 CYBUTEK
namespace MiniAVC {
public class Logger : MonoBehaviour {
void Awake() {
Debug.Log ("MiniAVC.Logger: Destroy");
Destroy (this);
}
}
public class Starter : MonoBehaviour {
void Awake() {
Debug.Log ("MiniAVC.Starter: Destroy");
Destroy (this);
}
}
}