Alloc

Untitled

Sep 29th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using AllocsFixes.PersistentData;
  4. using System.IO;
  5.  
  6. using System.Xml;
  7.  
  8. namespace AllocsFixes.CustomCommands
  9. {
  10.     public class LoadPrefabs: ConsoleCommand
  11.     {
  12.         // Generate once (compiler should optimize at least the string constants and the World-requests anyway but this makes the whole thing a bit cleaner)
  13.         private readonly string prefabsPath = UnityEngine.Application.dataPath + "/../Data/Prefabs";
  14.         private readonly string prefabsXMLpath =  UnityEngine.Application.dataPath + "/../Data/Worlds/Random Gen/PrefabsKFP.xml";
  15.         private readonly World world = CommonMappingFunctions.GetGameManager ().World;
  16.    
  17.         public LoadPrefabs(ConsoleSdtd cons) : base(cons)
  18.         {
  19.         }
  20.  
  21.         public override string Description ()
  22.         {
  23.             return "Load in game a prefab.";
  24.         }
  25.  
  26.         public override string[] Names ()
  27.         {
  28.             return new string[] { "addPrefab", "adp" };
  29.         }
  30.  
  31.         public override void Run (string[] _params)
  32.         {
  33.             try {
  34.                 if (_params.Length != 5 && _params.Length != 3 && _params.Length != 1) {
  35.                     m_Console.SendResult ("Usage: addPrefab <prefabname> <x> <y> <z> <rot>");
  36.                     m_Console.SendResult ("   or: addPrefab <prefabname> <target entityid|playername|steamid> <rot>");
  37.                     return;
  38.                 }
  39.                 if (_params.Length == 1 && _params[0].Equals("All")){
  40.                     LoadAllPrefabs ();
  41.                 }
  42. // x != 5 || x != 3 -> always true ;)
  43.                 if (_params.Length == 5 || _params.Length == 3) {
  44.                     LoadOnePrefab (_params);
  45.                 }
  46.             } catch (Exception ex) {
  47.                 Log.Out("LoadPrefab error: " + ex.Message);
  48.             }
  49.         }
  50.  
  51.         private void LoadAllPrefabs ()
  52.         {
  53.             if (!File.Exists (prefabsXMLpath))
  54.             {
  55.                 m_Console.SendResult ("Prefabs XML not found...");
  56.                 return;
  57.             }
  58.             XmlDocument xmlDocument = new XmlDocument();
  59.             xmlDocument.LoadXml(File.ReadAllText(prefabsXMLpath));
  60.             XmlNodeList xmlNodeList = xmlDocument.SelectNodes("prefabs");
  61.             XmlNode xmlNode = xmlNodeList[0];
  62. // "Speaking names"
  63.             foreach (XmlNode prefabNode in xmlNode.ChildNodes)
  64.             {
  65.                 if (prefabNode is XmlElement)
  66.                 {
  67.                     XmlElement prefabElement = (XmlElement)prefabNode;
  68.                     string prefabName = prefabElement.GetAttribute("name");
  69.                     string[] pos = prefabElement.GetAttribute("position").Split(',');
  70.                     string prefabRotation = prefabElement.GetAttribute("rotation");
  71.                                    
  72.                     Vector3i location =  new Vector3i(int.Parse(pos[0]), int.Parse(pos[1]), int.Parse(pos[2]));
  73.                     int rot = int.Parse(prefabRotation);
  74.  
  75.                     LoadSinglePrefab(prefabName, location, rot);
  76.                 }
  77.             }
  78.         }
  79.  
  80.         private void LoadOnePrefab (string[] _params){
  81.             int rot = 0;
  82.             string fileName = _params[0];
  83.  
  84.             Vector3i location = default(Vector3i);
  85.             if (_params.Length == 5) {
  86.                 int x, y, z;
  87. // I think I did that sanity check the same way somewhere but TryParse tells you if it was a valid integer anyway :)
  88. // Only required up here
  89.                 if (!int.TryParse (_params [1], out x) || !int.TryParse (_params [2], out y) || !int.TryParse (_params [3], out z)) {
  90.                     m_Console.SendResult ("At least one of the given coordinates is not a valid integer");
  91.                     return;
  92.                 }
  93.                 if (!int.TryParse (_params [4], out rot)) {
  94.                     m_Console.SendResult ("The given rotation is not a valid integer");
  95.                     return;
  96.                 }
  97.                 location.x = x;
  98.                 location.y = y;
  99.                 location.z = z;
  100.             }
  101.             if (_params.Length == 3) {
  102.                 Player p1 = PersistentContainer.Instance.Players.GetPlayerByNameOrId (_params [1], true);
  103.                 if (p1 == null) {
  104.                     m_Console.SendResult ("Target playername or entity/steamid id not found.");
  105.                     return;
  106.                 }
  107.                 if (!p1.IsOnline) {
  108.                     m_Console.SendResult ("Target player not online.");
  109.                     return;
  110.                 }
  111.  
  112.                 if (!int.TryParse (_params [2], out rot)) {
  113.                     m_Console.SendResult ("The given rotation is not a valid integer");
  114.                     return;
  115.                 }
  116.  
  117.                 location = p1.LastPosition;
  118.             }
  119.  
  120.             LoadSinglePrefab (fileName, location, rot);
  121.         }
  122.        
  123. // Reuse common code
  124.         private void LoadSinglePrefab (string filename, Vector3i position, int rotation) {
  125.             if (File.Exists(string.Concat(prefabsPath, "/" , fileName, ".tts"))){
  126.                 // Only instantiate objects if they are required (i.e. inside this if)
  127.                 List<BlockChangeInfo> list = new List<BlockChangeInfo>();
  128.                 Prefab prefab = new Prefab();
  129.  
  130.                 prefab.Load(directory,fileName);
  131.                 for (int a = 0; a < rot; a++)
  132.                 {
  133.                     prefab.RotateY(false);
  134.                 }
  135.                 m_Console.SendResult(string.Concat(
  136.                     "Prefab ",
  137.                     fileName,
  138.                     " is Loading. Please wait...")
  139.                 );
  140.                 Random rand = new Random(DateTime.Now.Millisecond);
  141.  
  142. // Var names again ;)
  143.                 Vector3i prefabPos = default(Vector3i);
  144.                 Vector3i blockPos = default(Vector3i);
  145.                 for (prefabPos.x = 0; prefabPos.x < prefab.size.x; prefabPos.x++)   {
  146.                     for (prefabPos.z = 0; prefabPos.z < prefab.size.z; prefabPos.z++)   {
  147.                         for (prefabPos.y = 0; prefabPos.y < prefab.size.y; prefabPos.y++)   {
  148.                             cl0008 block = prefab.GetBlock(prefabPos.x, prefabPos.y, prefabPos.z);
  149. // Use vector arithmetics (don't know why Prefab class does not support GetBlock with Vector3i :( )
  150.                             blockPos = location + prefabPos;
  151.                             cl0008 block2 = world.GetBlock(prefabPos);
  152.                             if (block.fd0031 != block2.fd0031 && (block.fd0031 != 0 || prefab.bCopyAirBlocks)) {
  153.                                 uint uint1 = LootContainer.lootPlaceholderMap.Replace(block, rand).fd0031;
  154.                                 BlockChangeInfo item = new BlockChangeInfo(blockPos, uint1, true);
  155.                                 list.Add(item);
  156.                             }
  157.                         }
  158.                     }
  159.                 }
  160.                 if (list.Count > 0) {
  161.                     CommonMappingFunctions.GetGameManager ().SetBlocksRPC (list);
  162.                     m_Console.SendResult(string.Concat("Your prefab is Loaded: ", fileName));
  163.                 }else {
  164. // More appropriate message
  165.                     m_Console.SendResult(string.Concat("Prefab ", fileName, " contains no blocks..."));
  166.                 }
  167.             } else {
  168.                 m_Console.SendResult(string.Concat("Prefab ", fileName , " not found..."));
  169.             }
  170.         }
  171.  
  172.  
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment