Skip to content

Commit 2a68977

Browse files
author
Swamp Ig
committed
Improvements to logging
1 parent 495a8a5 commit 2a68977

1 file changed

Lines changed: 49 additions & 71 deletions

File tree

SaveGameFixer.cs

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,11 @@ private void UpdateCraft(string vabCraft)
126126
PushLogContext("Craft file: " + vabCraft.Substring(savesRoot.Length, vabCraft.Length-savesRoot.Length));
127127
ConfigNode craft = ConfigNode.Load(vabCraft);
128128

129-
bool modified = false;
129+
bool needsBackup = false, needsSave = false;
130130
foreach (ConfigNode part in craft.GetNodes("PART"))
131-
modified |= UpdatePart(part);
131+
UpdatePart(part, ref needsBackup, ref needsSave);
132132

133-
if (modified)
134-
BackupAndReplace(vabCraft, craft);
133+
BackupAndReplace(vabCraft, craft, needsBackup, needsSave);
135134
}
136135
finally
137136
{
@@ -147,14 +146,13 @@ private void UpdateSFS(string sfsFile)
147146
{
148147
PushLogContext("Save file: " + sfsFile.Substring(savesRoot.Length, sfsFile.Length-savesRoot.Length));
149148

150-
bool modified = false;
149+
bool needsBackup = false, needsSave = false;
151150
foreach (ConfigNode game in sfs.GetNodes("GAME"))
152151
foreach (ConfigNode flightState in game.GetNodes("FLIGHTSTATE"))
153152
foreach (ConfigNode vessel in flightState.GetNodes("VESSEL"))
154-
modified |= UpdateVessel(vessel);
153+
UpdateVessel(vessel, ref needsBackup, ref needsSave);
155154

156-
if (modified)
157-
BackupAndReplace(sfsFile, sfs);
155+
BackupAndReplace(sfsFile, sfs, needsBackup, needsSave);
158156
}
159157
finally
160158
{
@@ -163,16 +161,14 @@ private void UpdateSFS(string sfsFile)
163161

164162
}
165163

166-
private bool UpdateVessel(ConfigNode vessel)
164+
private void UpdateVessel(ConfigNode vessel, ref bool needsBackup, ref bool needsSave)
167165
{
168166
try
169167
{
170168
PushLogContext("Vessel: " + vessel.GetValue("name"));
171169

172-
bool modified = false;
173170
foreach (ConfigNode part in vessel.GetNodes("PART"))
174-
modified |= UpdatePart(part);
175-
return modified;
171+
UpdatePart(part, ref needsBackup, ref needsSave);
176172
}
177173
finally
178174
{
@@ -181,7 +177,7 @@ private bool UpdateVessel(ConfigNode vessel)
181177
}
182178
#endregion
183179

184-
private bool UpdatePart(ConfigNode part)
180+
private void UpdatePart(ConfigNode part, ref bool needsBackup, ref bool needsSave)
185181
{
186182
// The modules saved with the part
187183
ConfigNode[] savedModules = part.GetNodes("MODULE");
@@ -219,21 +215,21 @@ private bool UpdatePart(ConfigNode part)
219215
if (available == null)
220216
{
221217
WriteLogMessage("Backup created - part \"" + partName + "\" has been deleted and ship will be destroyed.");
222-
return false;
218+
needsBackup = true;
223219
}
224220

225221
PartModuleList prefabModules = available.partPrefab.Modules;
226222

227223
if (prefabModules == null)
228-
return false;
224+
return;
229225

230226
// Do we need to do anything?
231227
if (prefabModules.Count == savedModules.Length && backupModules.Length == 0)
232228
{
233229
for (int i = 0; i < savedModules.Length; ++i)
234230
if (savedModules[i] != null && savedModules[i].GetValue("name") != prefabModules[i].moduleName)
235231
goto needUpdate;
236-
return false;
232+
return;
237233
needUpdate: ;
238234
}
239235

@@ -250,16 +246,16 @@ private bool UpdatePart(ConfigNode part)
250246
//+ "\nConfig: \n" + part
251247
);
252248
#endif
253-
bool hasChanged = false;
254249

255250
// Discard any backups that are already in saved modules
256251
for (int i = 0; i < backupModules.Length; ++i)
257252
for (int j = 0; j < savedModules.Length; ++j)
258253
if (savedModules[j] != null && backupModules[i].GetValue("name") == savedModules[j].GetValue("name"))
259254
{
255+
WriteDebugMessage("Discarding module backup \"" + backupModules[i].GetValue("name") + "\" as exists in the save already.");
260256
backupModules[i] = null;
261257
backupRemain--;
262-
hasChanged = true;
258+
needsSave = true;
263259
}
264260

265261

@@ -282,8 +278,8 @@ private bool UpdatePart(ConfigNode part)
282278
// The module is saved normally
283279
if (i != j)
284280
{
285-
WriteLogMessage("Module \"" + savedModules[j].GetValue("name") + "\" has had order changed. " + j + "=>" + i);
286-
hasChanged = true;
281+
WriteDebugMessage("Module \"" + savedModules[j].GetValue("name") + "\" has had order changed. " + j + "=>" + i);
282+
needsSave = true;
287283
}
288284

289285
part.AddNode(savedModules[j]);
@@ -296,7 +292,8 @@ private bool UpdatePart(ConfigNode part)
296292
{
297293
// The module will be restored from backup
298294
WriteLogMessage("Module \"" + backupModules[j].GetValue("name") + "\" has been restored from backup. ");
299-
hasChanged = true;
295+
needsBackup = true;
296+
needsSave = true;
300297

301298
backupModules[j].AddValue("MM_RESTORED", "true");
302299
part.AddNode(backupModules[j]);
@@ -306,7 +303,8 @@ private bool UpdatePart(ConfigNode part)
306303
}
307304
// Can't find it anywhere, reinitialize
308305
WriteLogMessage("Module \"" + prefabModules[i].moduleName + "\" is not present in the save and will be reinitialized. ");
309-
hasChanged = true;
306+
needsBackup = true;
307+
needsSave = true;
310308

311309
ConfigNode newNode = new ConfigNode("MODULE");
312310
newNode.AddValue("name", prefabModules[i].moduleName);
@@ -350,13 +348,14 @@ private bool UpdatePart(ConfigNode part)
350348
moduleBackupConfig.AddNode(savedModules[i]);
351349

352350
WriteLogMessage("Module \"" + savedModules[i].GetValue("name") + "\" is present in the part but is no longer available. Saved config to backup, will be restored if you reinstall the mod.");
353-
hasChanged = true;
351+
needsSave = true;
352+
needsBackup = true;
354353
}
355354
}
356355
}
357356

358-
if (!hasChanged)
359-
return false;
357+
if (!needsSave)
358+
return;
360359

361360
// Stick the resources back at the end just to be consistent
362361
ConfigNode[] resources = part.GetNodes("RESOURCE");
@@ -370,7 +369,6 @@ private bool UpdatePart(ConfigNode part)
370369
{
371370
PopLogContext();
372371
}
373-
return true;
374372
}
375373

376374
#region Backups
@@ -392,8 +390,19 @@ private void PopLogContext()
392390
logCtxCur = logContext.Count;
393391
}
394392

395-
private void WriteLogMessage(string logMessage)
393+
private void WriteDebugMessage(string logMessage)
394+
{
395+
WriteLogMessage(logMessage, true);
396+
}
397+
398+
private void WriteLogMessage(string logMessage, bool debugMsg = false)
396399
{
400+
#if DEBUG
401+
string dbg = debugMsg ? "[dbg]" : "[log]";
402+
403+
#else
404+
string dbg = string.Empty;
405+
#endif
397406
if (backupDir == null)
398407
{
399408
backupDir = Path.Combine(KSPUtil.ApplicationRootPath, string.Format("saves_backup{1}{0:yyyyMMdd-HHmmss}", DateTime.Now, Path.DirectorySeparatorChar));
@@ -407,65 +416,34 @@ private void WriteLogMessage(string logMessage)
407416
string indent;
408417
for (; logCtxCur < logContext.Count; logCtxCur++)
409418
{
410-
indent = new String(' ', 4 * logCtxCur);
419+
indent = new String(' ', 4 * logCtxCur + dbg.Length);
411420
sb.Append(indent).AppendLine(logContext[logCtxCur]);
412421
Debug.Log("[SaveGameFixer]" + indent + logContext[logCtxCur]);
413422
}
414423
indent = new String(' ', 4 * logCtxCur);
415-
sb.Append(indent).AppendLine(logMessage);
416-
Debug.Log("[SaveGameFixer]" + indent + logMessage);
424+
sb.Append(dbg).Append(indent).AppendLine(logMessage);
425+
Debug.Log("[SaveGameFixer]" + dbg + indent + logMessage);
417426

418427
File.AppendAllText(logFile, sb.ToString());
419428
}
420429

421-
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
430+
private void BackupAndReplace(string file, ConfigNode config, bool needsBackup, bool needsSave)
422431
{
423-
// Get the subdirectories for the specified directory.
424-
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
425-
DirectoryInfo[] dirs = dir.GetDirectories();
426-
427-
if (!dir.Exists)
432+
#if !DEBUG
433+
if (needsBackup)
434+
#endif
428435
{
429-
throw new DirectoryNotFoundException(
430-
"Source directory does not exist or could not be found: "
431-
+ sourceDirName);
432-
}
433436

434-
// If the destination directory doesn't exist, create it.
435-
if (!Directory.Exists(destDirName))
436-
{
437-
Directory.CreateDirectory(destDirName);
438-
}
437+
string relPath = file.Substring(savesRoot.Length, file.Length - savesRoot.Length);
439438

440-
// Get the files in the directory and copy them to the new location.
441-
FileInfo[] files = dir.GetFiles();
442-
foreach (FileInfo file in files)
443-
{
444-
string temppath = Path.Combine(destDirName, file.Name);
445-
file.CopyTo(temppath, false);
446-
}
439+
string backupTo = Path.Combine(backupDir, relPath);
440+
Directory.CreateDirectory(Path.GetDirectoryName(backupTo));
447441

448-
// If copying subdirectories, copy them and their contents to new location.
449-
if (copySubDirs)
450-
{
451-
foreach (DirectoryInfo subdir in dirs)
452-
{
453-
string temppath = Path.Combine(destDirName, subdir.Name);
454-
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
455-
}
442+
File.Copy(file, backupTo);
456443
}
457-
}
458-
459-
private void BackupAndReplace(string file, ConfigNode config)
460-
{
461-
string relPath = file.Substring(savesRoot.Length, file.Length - savesRoot.Length);
462-
463-
string backupTo = Path.Combine(backupDir, relPath);
464-
Directory.CreateDirectory(Path.GetDirectoryName(backupTo));
465-
466-
File.Copy(file, backupTo);
467444

468-
config.Save(file);
445+
if(needsSave)
446+
config.Save(file);
469447
}
470448

471449

0 commit comments

Comments
 (0)