Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.44 KB | None | 0 0
  1. using ColossalFramework.IO;
  2. using ColossalFramework.Plugins;
  3. using ColossalFramework.UI;
  4. using ICities;
  5. using System;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Xml.Serialization;
  9. using UnityEngine;
  10.  
  11. namespace PloppableAsphalt
  12. {
  13. public class PloppableAsphaltMod : IUserMod
  14. {
  15. public string Name => "Ploppable Asphalt";
  16. public string Description => "Adds ploppable asphalt props.";
  17. private static Configuration settings;
  18. public static Configuration Settings
  19. {
  20. get
  21. {
  22. if (settings == null)
  23. {
  24. settings = Configuration.LoadConfiguration();
  25. if (settings == null)
  26. {
  27. settings = new Configuration();
  28. Configuration.SaveConfiguration();
  29. }
  30.  
  31. }
  32. return settings;
  33. }
  34. set
  35. {
  36. settings = value;
  37. }
  38. }
  39.  
  40. #region UserInterface
  41. private float sliderWidth = 700f;
  42. private float sliderHeight = 10f;
  43. private float labelSize = 1.2f;
  44. private string toolTipText = "Hold SHIFT to drag all sliders";
  45. private UISlider redSlider;
  46. private UISlider greenSlider;
  47. private UISlider blueSlider;
  48. private UITextField redLabel;
  49. private UITextField greenLabel;
  50. private UITextField blueLabel;
  51.  
  52. private static void UpdateSlider(UISlider slider, UITextField textField, float value)
  53. {
  54. slider.value = value;
  55. textField.text = value.ToString();
  56. }
  57.  
  58. public void OnSettingsUI(UIHelperBase helper)
  59. {
  60. UIHelperBase sliderGroup = helper.AddGroup("\t\t\t\t\t\t RGB Values");
  61. sliderGroup.AddSpace(40);
  62. redLabel = (UITextField)sliderGroup.AddTextfield(" ", Settings.AsphaltColor.r.ToString(), (t) => { }, (t) => { });
  63. redLabel.disabledTextColor = Color.red;
  64. redLabel.textScale = labelSize;
  65. redLabel.Disable();
  66.  
  67. redSlider = (UISlider)sliderGroup.AddSlider(" ", 0f, 255f, 1f, Settings.AsphaltColor.r, (f) =>
  68. {
  69. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  70. {
  71. var difference = f - Settings.AsphaltColor.r;
  72. var green = Settings.AsphaltColor.g;
  73. var blue = Settings.AsphaltColor.b;
  74. if (blue + difference >= 0 && blue + difference <= 255)
  75. {
  76. UpdateSlider(blueSlider, blueLabel, blue + difference);
  77. Settings.AsphaltColor.b = blue + difference;
  78. }
  79. if (green + difference >= 0 && green + difference <= 255)
  80. {
  81. UpdateSlider(greenSlider, greenLabel, green + difference);
  82. Settings.AsphaltColor.g = green + difference;
  83. }
  84. redSlider.tooltipBox.isVisible = false;
  85. }
  86. else redSlider.tooltipBox.isVisible = true;
  87. Settings.AsphaltColor.r = f;
  88. UpdateSlider(redSlider, redLabel, f);
  89. PloppableAsphalt.ApplyColors();
  90. });
  91.  
  92. redSlider.color = Color.red;
  93. redSlider.tooltip = toolTipText;
  94. redSlider.scrollWheelAmount = 1f;
  95. redSlider.width = sliderWidth;
  96. redSlider.height = sliderHeight;
  97. UpdateSlider(redSlider, redLabel, Settings.AsphaltColor.r);
  98. sliderGroup.AddSpace(65);
  99.  
  100. greenLabel = (UITextField)sliderGroup.AddTextfield(" ", Settings.AsphaltColor.g.ToString(), (t) => { }, (t) => { });
  101. greenLabel.disabledTextColor = Color.green;
  102. greenLabel.textScale = labelSize;
  103. greenLabel.Disable();
  104.  
  105. greenSlider = (UISlider)sliderGroup.AddSlider(" ", 0f, 255f, 1f, Settings.AsphaltColor.g, (f) =>
  106. {
  107.  
  108. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  109. {
  110.  
  111. var difference = f - Settings.AsphaltColor.g;
  112. var red = Settings.AsphaltColor.r;
  113. var blue = Settings.AsphaltColor.b;
  114. if (red + difference >= 0 && red + difference <= 255)
  115. {
  116. UpdateSlider(redSlider, redLabel, red + difference);
  117. Settings.AsphaltColor.r = red + difference;
  118. }
  119. if (blue + difference >= 0 && blue + difference <= 255)
  120. {
  121. UpdateSlider(blueSlider, blueLabel, blue + difference);
  122. Settings.AsphaltColor.b = blue + difference;
  123. }
  124. greenSlider.tooltipBox.isVisible = false;
  125. }
  126. else greenSlider.tooltipBox.isVisible = true;
  127. greenSlider.RefreshTooltip();
  128. Settings.AsphaltColor.g = f;
  129. UpdateSlider(greenSlider, greenLabel, f);
  130. PloppableAsphalt.ApplyColors();
  131. });
  132.  
  133. greenSlider.color = Color.green;
  134. greenSlider.tooltip = toolTipText;
  135. greenSlider.scrollWheelAmount = 1f;
  136. greenSlider.width = sliderWidth;
  137. greenSlider.height = sliderHeight;
  138. UpdateSlider(greenSlider, greenLabel, Settings.AsphaltColor.g);
  139. sliderGroup.AddSpace(65);
  140.  
  141. blueLabel = (UITextField)sliderGroup.AddTextfield(" ", Settings.AsphaltColor.b.ToString(), (t) => { }, (t) => { });
  142. blueLabel.disabledTextColor = Color.blue;
  143. blueLabel.textScale = labelSize;
  144. blueLabel.Disable();
  145.  
  146. blueSlider = (UISlider)sliderGroup.AddSlider(" ", 0f, 255f, 1f, Settings.AsphaltColor.b, (f) =>
  147. {
  148.  
  149. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  150. {
  151.  
  152. var difference = f - Settings.AsphaltColor.b;
  153. var red = Settings.AsphaltColor.r;
  154. var green = Settings.AsphaltColor.g;
  155. if (red + difference >= 0 && red + difference <= 255)
  156. {
  157. UpdateSlider(redSlider, redLabel, red + difference);
  158. Settings.AsphaltColor.r = red + difference;
  159. }
  160. if (green + difference >= 0 && green + difference <= 255)
  161. {
  162. UpdateSlider(greenSlider, greenLabel, green + difference);
  163. Settings.AsphaltColor.g = green + difference;
  164. }
  165. blueSlider.tooltipBox.isVisible = false;
  166. }
  167. else blueSlider.tooltipBox.isVisible = true;
  168. blueSlider.RefreshTooltip();
  169. Settings.AsphaltColor.b = f;
  170. UpdateSlider(blueSlider, blueLabel, f);
  171. PloppableAsphalt.ApplyColors();
  172. });
  173.  
  174. blueSlider.color = Color.blue;
  175. blueSlider.tooltip = toolTipText;
  176. blueSlider.scrollWheelAmount = 1f;
  177. blueSlider.width = sliderWidth;
  178. blueSlider.height = sliderHeight;
  179. UpdateSlider(blueSlider, blueLabel, Settings.AsphaltColor.b);
  180.  
  181. sliderGroup.AddSpace(143);
  182. }
  183.  
  184. #endregion
  185. }
  186.  
  187. public class Configuration
  188. {
  189. [XmlIgnore]
  190. private static readonly string configurationPath = Path.Combine(DataLocation.localApplicationData, "PloppableAsphalt.xml");
  191.  
  192. public Color AsphaltColor = new Color(128f, 128f, 128f, 1f);
  193. public bool AutoDeployed = false;
  194.  
  195. public Configuration() { }
  196. public void OnPreSerialize() { }
  197. public void OnPostDeserialize() { }
  198.  
  199. public static void SaveConfiguration()
  200. {
  201. var fileName = configurationPath;
  202. var config = PloppableAsphaltMod.Settings;
  203. var serializer = new XmlSerializer(typeof(Configuration));
  204. using (var writer = new StreamWriter(fileName))
  205. {
  206. config.OnPreSerialize();
  207. serializer.Serialize(writer, config);
  208. }
  209. }
  210.  
  211.  
  212. public static Configuration LoadConfiguration()
  213. {
  214. var fileName = configurationPath;
  215. var serializer = new XmlSerializer(typeof(Configuration));
  216. try
  217. {
  218. using (var reader = new StreamReader(fileName))
  219. {
  220. var config = serializer.Deserialize(reader) as Configuration;
  221. return config;
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. Debug.Log(string.Format("[Ploppable Asphalt]: Error Parsing {0}: {1}", fileName, ex.Message.ToString()));
  227. return null;
  228. }
  229. }
  230. }
  231.  
  232. public class PloppableAsphaltLoading : LoadingExtensionBase
  233. {
  234. public override void OnLevelLoaded(LoadMode mode)
  235. {
  236. base.OnLevelLoaded(mode);
  237. //register our event delegates
  238. UIView.library.Get<UIPanel>("OptionsPanel").eventVisibilityChanged += (c, i) => PloppableAsphalt.SetRenderPropertiesAll();
  239. UIView.GetAView().FindUIComponent<UIDropDown>("LevelOfDetail").eventSelectedIndexChanged += (c, i) => PloppableAsphalt.SetRenderPropertiesAll();
  240.  
  241. }
  242. public override void OnLevelUnloading()
  243. {
  244. base.OnLevelUnloading();
  245. PloppableAsphalt.Loaded = false;
  246. //unregister event delegates
  247. UIView.library.Get<UIPanel>("OptionsPanel").eventVisibilityChanged -= (c, i) => PloppableAsphalt.SetRenderPropertiesAll();
  248. UIView.GetAView().FindUIComponent<UIDropDown>("LevelOfDetail").eventSelectedIndexChanged -= (c, i) => PloppableAsphalt.SetRenderPropertiesAll();
  249. }
  250. }
  251.  
  252. public class PloppableAsphalt
  253. {
  254. public static bool Loaded;
  255.  
  256. private static Shader shader; //will be used to contain the magic shader, but also will be used as a marker once the colors get replaced
  257.  
  258. internal static void SetRenderProperties(PropInfo prefab)
  259. {
  260. prefab.m_lodRenderDistance = prefab.m_mesh.name == "ploppableasphalt-prop" ? 200 : 18000;
  261. prefab.m_maxRenderDistance = prefab.m_mesh.name == "ploppableasphalt-decal" ? prefab.m_maxRenderDistance : 18000;
  262. }
  263.  
  264. internal static void SetRenderPropertiesAll()
  265. {
  266. for (uint i = 0; i < PrefabCollection<PropInfo>.LoadedCount(); i++)
  267. {
  268. var prefab = PrefabCollection<PropInfo>.GetLoaded(i);
  269.  
  270. if (prefab == null) continue;
  271.  
  272. if (prefab.m_mesh.name == "ploppableasphalt-prop" || prefab.m_mesh.name == "ploppableasphalt-decal") SetRenderProperties(prefab);
  273. }
  274. }
  275.  
  276. internal static void ApplyProperties()
  277. {
  278. shader = Shader.Find("Custom/Net/RoadBridge"); //the magic shader
  279.  
  280. for (uint i = 0; i < PrefabCollection<PropInfo>.LoadedCount(); i++)
  281. {
  282. var prefab = PrefabCollection<PropInfo>.GetLoaded(i);
  283.  
  284. if (prefab == null) continue;
  285.  
  286. if (prefab.m_mesh.name == "ploppableasphalt-prop")
  287. {
  288. //get ACI textures, to be used as APR textures
  289. Texture texture2D = new Texture2D(1, 1);
  290. Texture texture2Dlod = new Texture2D(1, 1);
  291. texture2D = (prefab.m_material.GetTexture("_ACIMap") as Texture2D);
  292. texture2Dlod = (prefab.m_lodMaterial.GetTexture("_ACIMap") as Texture2D);
  293.  
  294. //change the shader
  295. if (prefab.m_material != null) prefab.m_material.shader = shader;
  296. if (prefab.m_lodMaterial != null) prefab.m_lodMaterial.shader = shader;
  297.  
  298. //add the ACI textures as APR textures
  299. prefab.m_material.SetTexture("_APRMap", texture2D);
  300. prefab.m_lodMaterial.SetTexture("_APRMap", texture2Dlod);
  301.  
  302. //set high render distance
  303. //this sets the render distance properties on this item
  304. SetRenderProperties(prefab);
  305.  
  306. prefab.m_lodMaterialCombined = null;
  307. prefab.m_generatedInfo.m_size.z = prefab.m_generatedInfo.m_size.z * 2.174f;
  308. if (prefab.m_generatedInfo.m_size.y < 16) prefab.m_generatedInfo.m_size.y = 16f;
  309. }
  310.  
  311. else if (prefab.m_mesh.name == "ploppableasphalt-decal")
  312. {
  313. prefab.m_material.SetTexture("_MainTex", UnityEngine.Object.FindObjectOfType<NetProperties>().m_upwardDiffuse);
  314. prefab.m_lodMaterial.SetTexture("_MainTex", UnityEngine.Object.FindObjectOfType<NetProperties>().m_upwardDiffuse);
  315. //this sets the render distance properties on this item
  316. SetRenderProperties(prefab);
  317. }
  318.  
  319. ApplyColors();
  320. }
  321. }
  322.  
  323. internal static void ApplyColors() //change the color variation for the props, slider adjustment should only call this
  324. {
  325.  
  326. for (uint i = 0; i < PrefabCollection<PropInfo>.LoadedCount(); i++)
  327. {
  328. var rgb = PloppableAsphaltMod.Settings.AsphaltColor;
  329. var prefab = PrefabCollection<PropInfo>.GetLoaded(i);
  330. var color = new Color(rgb.r / 255, rgb.g / 255, rgb.b / 255);
  331.  
  332. if (prefab == null) continue;
  333.  
  334. if (prefab.m_mesh.name == "ploppableasphalt-prop" || prefab.m_mesh.name == "ploppableasphalt-decal")
  335. {
  336. prefab.m_color0 = color; prefab.m_color1 = color;
  337. prefab.m_color2 = color; prefab.m_color3 = color;
  338. }
  339. }
  340. Configuration.SaveConfiguration();
  341. //this sets the render distance properties on all ploppable asphalt items
  342. //we no longer need to use ApplyColorsAgain I dont think.
  343. SetRenderPropertiesAll();
  344. }
  345. }
  346.  
  347. public class DisableBlur : ThreadingExtensionBase
  348. {
  349. private UIComponent component;
  350.  
  351. public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
  352. {
  353. //moved this here because we had duplicate ThreadingExtension class.
  354. if (!PloppableAsphalt.Loaded && LoadingManager.instance.m_loadingComplete)
  355. {
  356. PloppableAsphalt.ApplyProperties();
  357. PloppableAsphalt.Loaded = true;
  358. }
  359.  
  360. if (component == null) component = UIView.library.Get("OptionsPanel");
  361.  
  362. if (component != null && component.isVisible)
  363. {
  364. UITextureSprite uITextureSprite = Util.ReadPrivate<DesaturationControl, UITextureSprite>(UnityEngine.Object.FindObjectOfType<DesaturationControl>(), "m_Target");
  365. if (uITextureSprite.opacity != 0f)
  366. {
  367. uITextureSprite.opacity = 0f;
  368. Util.WritePrivate<DesaturationControl, UITextureSprite>(UnityEngine.Object.FindObjectOfType<DesaturationControl>(), "m_Target", uITextureSprite);
  369. }
  370. }
  371. }
  372. }
  373.  
  374. public static class Util
  375. {
  376. public static Q ReadPrivate<T, Q>(T o, string fieldName)
  377. {
  378. FieldInfo[] fields = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  379. FieldInfo fieldInfo = null;
  380. FieldInfo[] array = fields;
  381. for (int i = 0; i < array.Length; i++)
  382. {
  383. FieldInfo fieldInfo2 = array[i];
  384. if (fieldInfo2.Name == fieldName)
  385. {
  386. fieldInfo = fieldInfo2;
  387. break;
  388. }
  389. }
  390. return (Q)((object)fieldInfo.GetValue(o));
  391. }
  392.  
  393. public static void WritePrivate<T, Q>(T o, string fieldName, object value)
  394. {
  395. FieldInfo[] fields = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  396. FieldInfo fieldInfo = null;
  397. FieldInfo[] array = fields;
  398. for (int i = 0; i < array.Length; i++)
  399. {
  400. FieldInfo fieldInfo2 = array[i];
  401. if (fieldInfo2.Name == fieldName)
  402. {
  403. fieldInfo = fieldInfo2;
  404. break;
  405. }
  406. }
  407. fieldInfo.SetValue(o, value);
  408. }
  409. }
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement