Tanoda
pb_ComponentDiff.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Linq;
3using System.Collections;
4using System.Collections.Generic;
5using System.Runtime.Serialization;
6
8{
14 public class pb_ComponentDiff : ISerializable
15 {
16 public Dictionary<Component, Dictionary<string, object>> modifiedValues;
17
19 {
20 modifiedValues = new Dictionary<Component, Dictionary<string, object>>();
21 }
22
23 List<System.Type> keys;
24 List<Dictionary<string, object>> values;
25
29 public void GetObjectData(SerializationInfo info, StreamingContext context)
30 {
31 if(modifiedValues == null)
32 modifiedValues = new Dictionary<Component, Dictionary<string, object>>();
33
34 List<System.Type> keys = modifiedValues.Keys.Select(x => x.GetType()).ToList();
35
36 info.AddValue("components", keys, typeof(List<System.Type>));
37 info.AddValue("values", modifiedValues.Values.ToList(), typeof(List<Dictionary<string, object>>));
38 }
39
43 public pb_ComponentDiff(SerializationInfo info, StreamingContext context)
44 {
45 modifiedValues = new Dictionary<Component, Dictionary<string, object>>();
46 keys = (List<System.Type>) info.GetValue("components", typeof(List<System.Type>));
47 values = (List<Dictionary<string, object>>) info.GetValue("values", typeof(List<Dictionary<string, object>>));
48 }
49
54 public static void AddDiff(Component component, string name, object value)
55 {
56 GameObject go = component.gameObject;
57
58 pb_MetaDataComponent md_component = go.GetComponent<pb_MetaDataComponent>();
59
60 if(md_component == null)
61 md_component = go.AddComponent<pb_MetaDataComponent>();
62
63 pb_ComponentDiff diff = md_component.metadata.componentDiff;
64
65 Dictionary<string, object> v;
66
67 if(diff.modifiedValues.TryGetValue(component, out v))
68 {
69 if(v.ContainsKey(name))
70 v[name] = value;
71 else
72 v.Add(name, value);
73 }
74 else
75 {
76 diff.modifiedValues.Add(component, new Dictionary<string, object>() { {name, value} } );
77 }
78 }
79
80
85 public void ApplyPatch(GameObject target)
86 {
87 if(keys.Count < 1 || values.Count != keys.Count)
88 return;
89
90 modifiedValues = new Dictionary<Component, Dictionary<string, object>>();
91
92 int entries = keys.Count;
93
95 Dictionary<System.Type, int> dup_components = new Dictionary<System.Type, int>();
96
97 for(int i = 0; i < entries; i++)
98 {
99 Component[] components = target.GetComponents(keys[i]);
100
101 if( dup_components.ContainsKey(keys[i]) )
102 dup_components[keys[i]]++;
103 else
104 dup_components.Add(keys[i], 0);
105
106 int index = System.Math.Min(dup_components[keys[i]], components.Length-1);
107
108 modifiedValues.Add(components[index], values[i]);
109
110 foreach(KeyValuePair<string, object> kvp in values[i])
111 {
112 pb_Reflection.SetValue(components[index], kvp.Key, kvp.Value);
113 // Debug.LogWarning("Failed patching property: " + kvp.Key + " Value (" + kvp.Value.GetType() + "): " + kvp.Value.ToString());
114 }
115 }
116 }
117 }
118}
UnityEngine.Component Component
pb_ComponentDiff(SerializationInfo info, StreamingContext context)
void ApplyPatch(GameObject target)
void GetObjectData(SerializationInfo info, StreamingContext context)
static void AddDiff(Component component, string name, object value)
Dictionary< Component, Dictionary< string, object > > modifiedValues
pb_ComponentDiff componentDiff
Definition: pb_MetaData.cs:26