Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/Data/Scripts/EvilSensor/EvilSensor.cs b/Data/Scripts/EvilSensor/EvilSensor.cs
- index cdf99f5..e851c4d 100644
- --- a/Data/Scripts/EvilSensor/EvilSensor.cs
- +++ b/Data/Scripts/EvilSensor/EvilSensor.cs
- @@ -4,23 +4,24 @@
- using Sandbox.ModAPI;
- using Sandbox.ModAPI.Ingame;
- using System;
- +using System.Collections;
- +using System.Collections.Generic;
- +using System.Linq;
- +using System.Text;
- namespace EvilSensor_1
- {
- [MyEntityComponentDescriptor(typeof(MyObjectBuilder_SensorBlock))]
- public class EvilSensor : MyGameLogicComponent
- {
- - // Initialize them variables
- - static String[] OreNames;
- IMySensorBlock Sensor;
- + StringBuilder logCache = new StringBuilder();
- +
- public override void Close()
- {
- - /* State changed! I'm not sure how this part works since sensor_StateChanged returns void?
- - * Maybe someone could enlighten me?
- - * I'm probably confused by the use of the -= operator.
- - */
- + // Remove sensor_StateChanged from the actions for Sensor.StateChanged
- Sensor.StateChanged -= sensor_StateChanged;
- }
- @@ -32,53 +33,86 @@ public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
- public override void Init(MyObjectBuilder_EntityBase objectBuilder)
- {
- - // If OreNames is empty, get the String[] of names from one of our references
- - if (OreNames == null)
- + try
- {
- - MyDefinitionManager.Static.GetOreTypeNames(out OreNames);
- + // Try to remove the old logs
- + RemoveLocalFile();
- + RemoveFile();
- + }
- + catch (Exception ex)
- + {
- + logCache.Append(ex + "\r\n");
- + }
- + try
- + {
- + // Go through all the ore & component names and put them in the logCache to be printed out later. Was having massive issues with writing to logs during Init.
- + String[] oreNames;
- + MyDefinitionManager.Static.GetOreTypeNames(out oreNames);
- + List<MyComponentDefinition> componentList = Enumerable.OfType<MyComponentDefinition>((IEnumerable)MyDefinitionManager.Static.GetAllDefinitions()).ToList();
- + foreach (var c in componentList)
- + {
- + if (c != null)
- + {
- + logCache.Append(String.Format("ComponentList: {0}\r\n", c.Id.SubtypeName));
- + }
- + }
- + foreach (var o in oreNames)
- + {
- + if (o != null)
- + {
- + logCache.Append(String.Format("OreList: {0}\r\n", o));
- + }
- + }
- + // Define that Sensor is a SensorBlock
- + Sensor = Entity as IMySensorBlock;
- + // Add sensor_StateChanged to the actions for Sensor.StateChanged
- + Sensor.StateChanged += sensor_StateChanged;
- + }
- + catch (Exception ex)
- + {
- + logCache.Append(ex + "\r\n");
- + return;
- }
- - // Define that Sensor is a SensorBloack
- - Sensor = Entity as IMySensorBlock;
- - /* State changed! I'm not sure how this part works since sensor_StateChanged returns void?
- - * Maybe someone could enlighten me?
- - * I'm probably confused by the use of the += operator.
- - */
- - Sensor.StateChanged += sensor_StateChanged;
- }
- void sensor_StateChanged(bool obj)
- {
- + // If the logCache has data, attempt to write and clear it. Append the exception otherwise.
- + if (logCache.Length > 0)
- + {
- + try
- + {
- + Logging.Instance.WriteLine(logCache.ToString());
- + logCache.Clear();
- + }
- + catch (Exception ex)
- + {
- + logCache.Append(ex + "\r\n");
- + }
- + }
- + /*
- + * Check to make sure this is a server instance of the mod (It runs on the client as well),
- + * otherwise a duplicate, non-functional object will be spawned by the client as well. (Duplicate bug)
- + */
- + if (!Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer) return;
- +
- // I think this part is depending on whether you entered or exited the range.
- // !obj would be when you exited the range, so nothing happens.
- if (!obj) return;
- // Define some mo' variables
- - string ore = null;
- + string objectName = "Iron";
- - string sensorName = Sensor.CustomName.ToLowerInvariant();
- + string spawnType = "Ore";
- +
- + string sensorName = Sensor.CustomName;
- VRageMath.Vector3 direction = Sensor.WorldMatrix.Forward;
- - var oreAmount = 100;
- + var amount = 100;
- var distance = 1.5f;
- - string dataString = "";
- -
- - //Loop through the ore names in the array and see if the sensor's name contains one of them
- - foreach (var o in OreNames)
- - {
- - if (sensorName.Contains(o.ToLowerInvariant()))
- - {
- - ore = o;
- - break;
- - }
- - }
- - // If the sensor name does not contain an ore name, we exit here
- - if (ore == null)
- - return;
- - // See if the sensor name contains the "data string" as defined
- - // by beginning with "[" and ending with "]"
- if (sensorName.Contains("[") && sensorName.Contains("]"))
- {
- /*
- @@ -88,27 +122,60 @@ void sensor_StateChanged(bool obj)
- */
- try
- {
- - dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).ToLowerInvariant().Replace(" ", "");
- + // Get the data string, convert it to lowercase, and remove all the spaces.
- + string dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).Replace(" ", "");
- string dataSection;
- bool control = true;
- + // Loop through the string till all the data is gone
- while (control)
- {
- - if (dataString.Contains(";"))
- + // Check to see if the string contains any separators
- + if (dataString.Contains(";") || dataString.Contains(","))
- {
- - dataSection = dataString.Slice(0, dataString.IndexOf(";"));
- - dataString = dataString.Substring(dataString.IndexOf(";") + 1);
- - string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1);
- + // Separate the data section and remove it from the data string
- + if (dataString.Contains(";"))
- + {
- + dataSection = dataString.Slice(0, dataString.IndexOf(";"));
- + dataString = dataString.Substring(dataString.IndexOf(";") + 1);
- + }
- + else
- + {
- + dataSection = dataString.Slice(0, dataString.IndexOf(","));
- + dataString = dataString.Substring(dataString.IndexOf(",") + 1);
- + }
- + // Get the tag and data from the section we just got from the original string
- + string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1).ToLowerInvariant();
- string data = dataSection.Substring(dataSection.IndexOf(":") + 1);
- + // Set the data depending on what it is
- switch (tag)
- {
- + case "type:":
- + // Check for / and \ as the separator
- + if (data.Contains("/"))
- + {
- + spawnType = data.Slice(0, data.IndexOf("/"));
- + objectName = data.Substring(data.IndexOf("/") + 1);
- + }
- + else if (data.Contains("\\"))
- + {
- + spawnType = data.Slice(0, data.IndexOf("\\"));
- + objectName = data.Substring(data.IndexOf("\\") + 1);
- + }
- + else
- + {
- + // Set the default since its missing a separator
- + spawnType = "Ore";
- + objectName = "Iron";
- + }
- + break;
- case "amt:":
- - oreAmount = Convert.ToInt32(data);
- + amount = Convert.ToInt32(data);
- break;
- case "dst:":
- distance = Convert.ToSingle(data);
- break;
- case "dir:":
- - switch (data)
- + switch (data.ToLowerInvariant())
- {
- case "up":
- direction = Sensor.WorldMatrix.Up;
- @@ -137,21 +204,34 @@ void sensor_StateChanged(bool obj)
- break;
- }
- }
- + // Just get the data from the string since there are no separators
- else
- {
- dataSection = dataString;
- - string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1);
- + string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1).ToLowerInvariant();
- string data = dataSection.Substring(dataSection.IndexOf(":") + 1);
- switch (tag)
- {
- + case "type:":
- + if (data.Contains("/"))
- + {
- + spawnType = data.Slice(0, data.IndexOf("/"));
- + objectName = data.Substring(data.IndexOf("/") + 1);
- + }
- + else if (data.Contains("\\"))
- + {
- + spawnType = data.Slice(0, data.IndexOf("\\"));
- + objectName = data.Substring(data.IndexOf("\\") + 1);
- + }
- + break;
- case "amt:":
- - oreAmount = Convert.ToInt32(data);
- + amount = Convert.ToInt32(data);
- break;
- case "dst:":
- distance = Convert.ToSingle(data);
- break;
- case "dir:":
- - switch (data)
- + switch (data.ToLowerInvariant())
- {
- case "up":
- direction = Sensor.WorldMatrix.Up;
- @@ -182,31 +262,13 @@ void sensor_StateChanged(bool obj)
- control = false;
- }
- }
- - /* Old string handling
- - if (dataString.Contains("amt:") && dataString.Contains("dst:"))
- - {
- - if (dataString.IndexOf("amt:") < dataString.IndexOf("dst:")) {
- - oreAmount = Convert.ToInt32(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));;
- - distance = Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- - } else {
- - distance = Convert.ToSingle(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));
- - oreAmount = Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- - }
- - }
- - else if (dataString.Contains("amt:"))
- - {
- - oreAmount = Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- - }
- - else if (dataString.Contains("dst:"))
- - {
- - distance = Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- - }
- - */
- }
- catch (Exception ex)
- {
- // If something got fubar'ed, set the defaults and try to continue without crashing the damn game
- - oreAmount = 100;
- + spawnType = "Ore";
- + objectName = "Iron";
- + amount = 100;
- distance = 1.5f;
- direction = Sensor.WorldMatrix.Forward;
- Logging.Instance.WriteLine(String.Format("sensor_StateChanged(), String Parser: {0}", ex.ToString()));
- @@ -216,17 +278,30 @@ void sensor_StateChanged(bool obj)
- {
- // Create the object to be spawned
- MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
- - // Specify that the object is an ore inventory item of the amount & type specified by the user
- - floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = oreAmount, Content = new MyObjectBuilder_Ore() { SubtypeName = ore } };
- +
- floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important, but I'm not sure why. I guess if not flagged as InScene, it won't be recognized as actually being there would be my guess.
- +
- // Set the postition and orientation of the object in the world.
- floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + direction * distance, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
- - // Check to make sure this is a server instance of the mod (I think it runs on client as well), otherwise a duplicate, non-functional object will be spawned by the client as well. (Duplicate bug)
- - if (Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer)
- +
- + // Specify that the object is an inventory item of the type and amount specified
- + switch (spawnType.ToLowerInvariant())
- {
- - // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
- - var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
- + case "component":
- + floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = amount, Content = new MyObjectBuilder_Component { SubtypeName = objectName } };
- + break;
- + case "ingot":
- + floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = amount, Content = new MyObjectBuilder_Ingot() { SubtypeName = objectName } };
- + break;
- + case "ore":
- + floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = amount, Content = new MyObjectBuilder_Ore() { SubtypeName = objectName } };
- + break;
- + default:
- + break;
- }
- +
- + // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
- + var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
- }
- catch (Exception ex)
- {
- @@ -248,6 +323,7 @@ public override void UpdateAfterSimulation10()
- public override void UpdateAfterSimulation100()
- {
- +
- }
- public override void UpdateBeforeSimulation()
- @@ -265,9 +341,25 @@ public override void UpdateBeforeSimulation100()
- public override void UpdateOnceBeforeFrame()
- {
- }
- +
- + internal void RemoveFile()
- + {
- + if (MyAPIGateway.Utilities.FileExistsInGlobalStorage("EvilSensor.log"))
- + {
- + MyAPIGateway.Utilities.DeleteFileInGlobalStorage("EvilSensor.log");
- + }
- + }
- +
- + internal void RemoveLocalFile()
- + {
- + if (MyAPIGateway.Utilities.FileExistsInLocalStorage("EvilSensor.log", typeof(EvilSensor)))
- + {
- + MyAPIGateway.Utilities.DeleteFileInLocalStorage("EvilSensor.log", typeof(EvilSensor));
- + }
- + }
- }
- - public static class Extensions
- + internal static class Extensions
- {
- /// <summary>
- /// Get the string slice between the two indexes.
- @@ -275,12 +367,20 @@ public static class Extensions
- /// </summary>
- public static string Slice(this string source, int start, int end)
- {
- - if (end < 0) // Keep this for negative end support
- + try
- + {
- + if (end < 0) // Keep this for negative end support
- + {
- + end = source.Length + end;
- + }
- + int len = end - start; // Calculate length
- + return source.Substring(start, len); // Return Substring of length
- + }
- + catch (Exception ex)
- {
- - end = source.Length + end;
- + Logging.Instance.WriteLine(String.Format("Slice(): {0}", ex.ToString()));
- + return null;
- }
- - int len = end - start; // Calculate length
- - return source.Substring(start, len); // Return Substring of length
- }
- }
- }
- diff --git a/Data/Scripts/EvilSensor/Logging.cs b/Data/Scripts/EvilSensor/Logging.cs
- index 93962a5..8bd054b 100644
- --- a/Data/Scripts/EvilSensor/Logging.cs
- +++ b/Data/Scripts/EvilSensor/Logging.cs
- @@ -37,7 +37,7 @@ public Logging(string logFile)
- {
- try
- {
- - m_writer = MyAPIGateway.Utilities.WriteFileInLocalStorage(logFile, typeof(Logging));
- + m_writer = MyAPIGateway.Utilities.WriteFileInGlobalStorage(logFile);
- m_instance = this;
- }
- catch { }
- @@ -74,7 +74,6 @@ public void Write(string text)
- m_cache.Append(text);
- }
- -
- internal void Close()
- {
- if (m_cache.Length > 0)
Advertisement
Add Comment
Please, Sign In to add comment