Skip to content

Commit

Permalink
v2.6.3 Allows the patching of Physics values with a PHYSICSGLOBALS node
Browse files Browse the repository at this point in the history
  • Loading branch information
sarbian committed May 4, 2015
1 parent 58bb9ea commit 6f7add8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
17 changes: 12 additions & 5 deletions TechTreeManager.cs → CustomConfigsManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UnityEngine;

namespace ModuleManager
{
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class TechTreeManager: MonoBehaviour
public class CustomConfigsManager : MonoBehaviour
{
internal void Start()
{
Expand All @@ -17,11 +14,21 @@ internal void Start()
log("Setting moddeed tech tree as the active one");
HighLogic.CurrentGame.Parameters.Career.TechTreeUrl = MMPatchLoader.techTreeFile;
}

if (File.Exists(MMPatchLoader.physicsPath))
{
log("Setting moddeed physics as the active one");

PhysicsGlobals.PhysicsDatabaseFilename = MMPatchLoader.physicsFile;

if (!PhysicsGlobals.Instance.LoadDatabase())
log("Something went wrong while setting the active physics config.");
}
}

public static void log(String s)
{
print("[TechTreeManager] " + s);
print("[CustomConfigsManager] " + s);
}

}
Expand Down
2 changes: 1 addition & 1 deletion ModuleManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<Compile Include="moduleManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs" />
<Compile Include="TechTreeManager.cs" />
<Compile Include="CustomConfigsManager.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("2.6.2")]
[assembly: AssemblyVersion("2.6.3")]
[assembly: KSPAssembly("ModuleManager", 2, 5)]

// The following attributes are used to specify the signing key for the assembly,
Expand Down
64 changes: 55 additions & 9 deletions moduleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public class MMPatchLoader : LoadingSystem

public int needsUnsatisfiedCount = 0;

public int catEatenCount = 0;
private int catEatenCount = 0;

private Dictionary<String, int> errorFiles;

Expand All @@ -419,31 +419,37 @@ public class MMPatchLoader : LoadingSystem

public string errors = "";

public string activity = "Module Manager";
private string activity = "Module Manager";

private static Dictionary<string, Regex> regexCache = new Dictionary<string, Regex>();
private static readonly Dictionary<string, Regex> regexCache = new Dictionary<string, Regex>();

private static Stack<ConfigNode> nodeStack = new Stack<ConfigNode>();
private static readonly Stack<ConfigNode> nodeStack = new Stack<ConfigNode>();

private static ConfigNode topNode;

private static string cachePath = KSPUtil.ApplicationRootPath + Path.DirectorySeparatorChar + "GameData"
+ Path.DirectorySeparatorChar + "ModuleManager.ConfigCache";

internal static string techTreeFile = "GameData" + Path.DirectorySeparatorChar + "ModuleManager.TechTree";
internal static readonly string techTreeFile = "GameData" + Path.DirectorySeparatorChar + "ModuleManager.TechTree";
internal static readonly string techTreePath = KSPUtil.ApplicationRootPath + Path.DirectorySeparatorChar + techTreeFile;

internal static string techTreePath = KSPUtil.ApplicationRootPath + Path.DirectorySeparatorChar + techTreeFile;
internal static readonly string physicsFile = "GameData" + Path.DirectorySeparatorChar + "ModuleManager.Physics";
internal static readonly string physicsPath = KSPUtil.ApplicationRootPath + Path.DirectorySeparatorChar + physicsFile;
private static readonly string defaultPhysicsPath = KSPUtil.ApplicationRootPath + Path.DirectorySeparatorChar + "Physics.cfg";

private static string shaPath = KSPUtil.ApplicationRootPath + Path.DirectorySeparatorChar + "GameData"
private static readonly string shaPath = KSPUtil.ApplicationRootPath + Path.DirectorySeparatorChar + "GameData"
+ Path.DirectorySeparatorChar + "ModuleManager.ConfigSHA";

private UrlDir.UrlFile physicsUrlFile;


private static string configSha;

private static bool useCache = false;

private static Stopwatch patchSw = new Stopwatch();
private static readonly Stopwatch patchSw = new Stopwatch();

private static List<ModuleManagerPostPatchCallback> postPatchCallbacks =
private static readonly List<ModuleManagerPostPatchCallback> postPatchCallbacks =
new List<ModuleManagerPostPatchCallback>();

public static MMPatchLoader Instance { get; private set; }
Expand Down Expand Up @@ -656,6 +662,8 @@ private IEnumerator ProcessPatch(bool blocking)
List<String> excludePaths = PrePatchInit();

yield return null;

LoadPhysicsConfig();

// Do filtering with NEEDS
log("Checking NEEDS.");
Expand Down Expand Up @@ -705,6 +713,7 @@ private IEnumerator ProcessPatch(bool blocking)

CreateCache();
SaveModdedTechTree();
SaveModdedPhysics();
}
else
{
Expand Down Expand Up @@ -743,6 +752,42 @@ private IEnumerator ProcessPatch(bool blocking)
ready = true;
}

private void LoadPhysicsConfig()
{
log("Loading Physics.cfg");
UrlDir gameDataDir = GameDatabase.Instance.root.AllDirectories.First(d => d.path.EndsWith("GameData") && d.name == "" && d.url == "");
// need to use a file with a cfg extenssion to get the right fileType or you can't AddConfig on it
physicsUrlFile = new UrlDir.UrlFile(gameDataDir, new FileInfo(defaultPhysicsPath));
// Since it loaded the default config badly (sub node only) we clear it first
physicsUrlFile.configs.Clear();
// And reload it properly
ConfigNode physicsContent = ConfigNode.Load(defaultPhysicsPath);
physicsContent.name = "PHYSICSGLOBALS";
physicsUrlFile.AddConfig(physicsContent);
gameDataDir.files.Add(physicsUrlFile);
}


private void SaveModdedPhysics()
{
//UrlDir.UrlConfig[] configs = GameDatabase.Instance.GetConfigs("TechTree");

var configs = physicsUrlFile.configs;

if (configs.Count == 0)
{
log("No PHYSICSGLOBALS node found. No custom Physics config will be saved");
return;
}

if (configs.Count > 1)
{
log(configs.Count + " PHYSICSGLOBALS node found. A patch may be wrong. Using the first one");
}

configs[0].config.Save(physicsPath);
}


// DB check used to track the now fixed TextureReplacer corruption
public static void checkValues()
Expand Down Expand Up @@ -813,6 +858,7 @@ private void IsCacheUpToDate()
useCache = storedSHA.Equals(configSha);
useCache = useCache && version.Equals(Assembly.GetExecutingAssembly().GetName().Version.ToString());
useCache = useCache && File.Exists(cachePath);
useCache = useCache && File.Exists(physicsPath);
log("Cache SHA = " + storedSHA);
log("useCache = " + useCache);
}
Expand Down

0 comments on commit 6f7add8

Please sign in to comment.