-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopiedBuildingContext.cs
More file actions
137 lines (123 loc) · 5.73 KB
/
Copy pathCopiedBuildingContext.cs
File metadata and controls
137 lines (123 loc) · 5.73 KB
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
using System.Collections.Generic;
using System.Linq;
using SpaceCraft;
using UnityEngine;
namespace CopyBuildingMod
{
public sealed partial class Plugin
{
private sealed class CopiedBuildingContext
{
public int GroupHash { get; private set; }
public int Setting { get; private set; }
public string Text { get; private set; }
public Color Color { get; private set; }
public int LinkedPlanetHash { get; private set; }
public List<Group> LinkedGroups { get; private set; }
public List<Group> LogisticDemandGroups { get; private set; }
public List<Group> LogisticSupplyGroups { get; private set; }
public int LogisticPriority { get; private set; }
public bool HasLogisticData { get; private set; }
public static CopiedBuildingContext From(WorldObject sourceWorldObject, GroupConstructible group, bool includeSettings)
{
var context = new CopiedBuildingContext
{
GroupHash = group.stableHashCode,
Setting = 0,
Text = string.Empty,
Color = default,
LinkedPlanetHash = 0,
LinkedGroups = null,
LogisticDemandGroups = null,
LogisticSupplyGroups = null,
LogisticPriority = 0,
HasLogisticData = false
};
if (!includeSettings || sourceWorldObject == null)
{
return context;
}
context.Setting = sourceWorldObject.GetSetting();
context.Text = sourceWorldObject.GetText() ?? string.Empty;
context.Color = sourceWorldObject.GetColor();
context.LinkedPlanetHash = sourceWorldObject.GetPlanetLinkedHash();
var sourceGroups = sourceWorldObject.GetLinkedGroups();
if (sourceGroups != null && sourceGroups.Count > 0)
{
context.LinkedGroups = new List<Group>(sourceGroups);
}
int sourceInventoryId = sourceWorldObject.GetLinkedInventoryId();
if (sourceInventoryId > 0 && InventoriesHandler.Instance != null)
{
var sourceInventory = InventoriesHandler.Instance.GetInventoryById(sourceInventoryId);
var sourceLogistic = sourceInventory?.GetLogisticEntity();
if (sourceLogistic != null)
{
context.LogisticDemandGroups = sourceLogistic.GetDemandGroups()?.ToList();
context.LogisticSupplyGroups = sourceLogistic.GetSupplyGroups()?.ToList();
context.LogisticPriority = sourceLogistic.GetPriority();
context.HasLogisticData = true;
}
}
return context;
}
public void ApplyTo(GameObject targetGo, WorldObject targetWo)
{
if (targetGo == null || targetWo == null)
{
return;
}
targetWo.SetSetting(Setting);
targetWo.SetText(Text);
targetWo.SetColor(Color);
targetWo.SetPlanetLinkedHash(LinkedPlanetHash);
targetWo.SetLinkedGroups(LinkedGroups);
var settingProxy = targetGo.GetComponentInChildren<SettingProxy>(true);
if (settingProxy != null)
{
settingProxy.SetSetting(Setting);
}
var textProxy = targetGo.GetComponentInChildren<TextProxy>(true);
if (textProxy != null)
{
textProxy.SetText(Text);
}
var colorProxy = targetGo.GetComponentInChildren<ColorProxy>(true);
if (colorProxy != null)
{
colorProxy.SetColor(Color);
}
var linkedPlanetProxy = targetGo.GetComponentInChildren<LinkedPlanetProxy>(true);
if (linkedPlanetProxy != null)
{
linkedPlanetProxy.SetLinkedPlanet(LinkedPlanetHash);
}
var linkedGroupsProxy = targetGo.GetComponentInChildren<LinkedGroupsProxy>(true);
if (linkedGroupsProxy != null)
{
linkedGroupsProxy.SetLinkedGroups(LinkedGroups);
}
if (HasLogisticData && InventoriesHandler.Instance != null)
{
int targetInventoryId = targetWo.GetLinkedInventoryId();
if (targetInventoryId > 0)
{
var targetInventory = InventoriesHandler.Instance.GetInventoryById(targetInventoryId);
var targetLogistic = targetInventory?.GetLogisticEntity();
if (targetLogistic != null)
{
targetLogistic.SetDemandGroups(LogisticDemandGroups != null
? new HashSet<Group>(LogisticDemandGroups)
: new HashSet<Group>());
targetLogistic.SetSupplyGroups(LogisticSupplyGroups != null
? new HashSet<Group>(LogisticSupplyGroups)
: new HashSet<Group>());
targetLogistic.SetPriority(LogisticPriority);
InventoriesHandler.Instance.UpdateLogisticEntity(targetInventory);
}
}
}
}
}
}
}