forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathPrefabDryCostWriter.cs
62 lines (57 loc) · 2.08 KB
/
PrefabDryCostWriter.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
using TweakScale.Annotations;
using UnityEngine;
using System.Linq;
using System;
namespace TweakScale
{
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
internal class PrefabDryCostWriter : SingletonBehavior<PrefabDryCostWriter>
{
[UsedImplicitly]
private void Start()
{
Debug.Log("TweakScale::PrefabDryCostWriter: Start");
WriteDryCost();
}
private void WriteDryCost()
{
var partsList = PartLoader.LoadedPartsList;
foreach (var p in partsList)
{
var prefab = p.partPrefab;
if (prefab == null)
{
Tools.LogWf("partPrefab is null: " + p.name);
continue;
}
try
{
if (prefab.Modules == null)
{
Tools.LogWf("partPrefab.Modules is null: " + p.name);
continue;
}
if (!prefab.Modules.Contains("TweakScale"))
continue;
var m = prefab.Modules["TweakScale"] as TweakScale;
m.DryCost = (float)(p.cost - prefab.Resources.Cast<PartResource>().Aggregate(0.0, (a, b) => a + b.maxAmount * b.info.unitCost));
if (prefab.Modules.Contains("FSfuelSwitch"))
m.ignoreResourcesForCost = true;
if (m.DryCost < 0)
{
if (m.DryCost < -0.5)
{
Debug.LogError("TweakScale::PrefabDryCostWriter: negative dryCost: part=" + p.name + ", DryCost=" + m.DryCost.ToString());
}
m.DryCost = 0;
}
}
catch (Exception e)
{
Debug.LogError("[TweakScale] Exception on writeDryCost: " +e.ToString());
Debug.Log("[TweakScale] part="+p.name +" ("+p.title+")");
}
}
}
}
}