ketchu13

7dtd-server-fixes - Save Prefab Command

Sep 29th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. //7 Days To Die - Dedicated Server
  2. //@Author: Alloc => 7dtd-server-fixes => https://7dtd.illy.bz/browser/binary-improvements/7dtd-server-fixes
  3. //@Author: ketchu13 => SavePrefab console command => [email protected]
  4. using System;
  5. using System.IO;
  6. using AllocsFixes.PersistentData;
  7.  
  8.  
  9. namespace AllocsFixes.CustomCommands
  10. {
  11.     public class SavePrefab: ConsoleCommand
  12.     {
  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 SavePrefab(ConsoleSdtd cons) : base(cons)
  18.         {
  19.         }
  20.  
  21.         public override string Description ()
  22.         {
  23.             return "Save in game a prefab.";
  24.         }
  25.  
  26.         public override string[] Names ()
  27.         {
  28.             return new string[] { "savePrefab", "svp" };
  29.         }
  30.  
  31.         public override void Run (string[] _params)
  32.         {
  33.             try {
  34.                 if (_params.Length < 5 && _params.Length > 8) {
  35.                     m_Console.SendResult ("Usage: savePrefab <prefabname> <x> <y> <z> <x size> <y size> <z size> <overwrite*>");
  36.                     m_Console.SendResult ("   or: savePrefab <prefabname> <target entityid|playername|steamid> <x size> <y size> <z size> <overwrite*>");
  37.                     m_Console.SendResult ("Optional <overwrite*> = true or false (Default set to False)");  
  38.                     return;
  39.                 }
  40.                 if (_params.Length >= 5 && _params.Length <= 8) {
  41.                     SaveSinglePrefab (_params);
  42.                 }
  43.             } catch (Exception ex) {
  44.                 Log.Out ("savePrefab error: " + ex.Message);
  45.             }
  46.         }
  47.  
  48.         private void SaveSinglePrefab(string[] _params) {
  49.             string fileName = _params[0];
  50.             bool overwrite = false;
  51.  
  52.             if (_params.Length == 6 || _params.Length == 8) {
  53.                 if(!Boolean.TryParse(_params[_params.Length-1], out overwrite)){
  54.                     m_Console.SendResult ("Optional <overwrite*> = true or false (Default set to False)");  
  55.                     return;
  56.                 }
  57.             }
  58.             if (overwrite == false) {
  59.                 if (File.Exists (prefabsPath + "/" + fileName + ".tts" )) {
  60.                     m_Console.SendResult ("This prefab " + fileName + " already exist. Add true at the end of the command line to write on a existing file");
  61.                     return;
  62.                 }
  63.             }
  64.             int x, y, z;
  65.             int sizeX= 50;
  66.             int sizeY= 50;
  67.             int sizeZ = 50;
  68.  
  69.             Vector3i positionC = default(Vector3i);
  70.  
  71.             if (_params.Length >= 7) {
  72.                 if (!int.TryParse (_params [1], out x) ||
  73.                     !int.TryParse (_params [2], out y) ||
  74.                     !int.TryParse (_params [3], out z) ||
  75.                     !int.TryParse (_params [4], out sizeX) ||
  76.                     !int.TryParse (_params [5], out sizeY) ||
  77.                     !int.TryParse (_params [6], out sizeZ))  {
  78.                     m_Console.SendResult ("At least one of the given sizes or coordinates is not a valid integer");
  79.                     return;
  80.                 }
  81.                 positionC.x = x;
  82.                 positionC.y = y;
  83.                 positionC.z = z;
  84.             }
  85.  
  86.             if (_params.Length >= 5) {
  87.                 Player p1 = PersistentContainer.Instance.Players.GetPlayerByNameOrId (_params [1], true);
  88.                 if (p1 == null) {
  89.                     m_Console.SendResult ("Playername or entity/steamid id not found.");
  90.                     return;
  91.                 }
  92.                 if (!p1.IsOnline) {
  93.                     m_Console.SendResult ("Player not online.");
  94.                     return;
  95.                 }
  96.                 if (!int.TryParse (_params [2], out sizeX) ||
  97.                     !int.TryParse (_params [3], out sizeY) ||
  98.                     !int.TryParse (_params [4], out sizeZ)) {
  99.                     m_Console.SendResult ("At least one of the given sizes is not a valid integer");
  100.                     return;
  101.                 }
  102.                 UnityEngine.Vector3 vector3 = p1.Entity.GetPosition();
  103.                 positionC.x = (int)vector3.x;
  104.                 positionC.y = (int)vector3.y;
  105.                 positionC.z = (int)vector3.z;
  106.             }
  107.  
  108.             Vector3i positionStart = default(Vector3i);
  109.             Vector3i positionEnd = default(Vector3i);
  110.  
  111.             positionStart.x =(int)(positionC.x - sizeX / 2) ;
  112.             positionStart.y = (int)(positionC.y - sizeY / 2) ;
  113.             positionStart.z = (int)(positionC.z - sizeZ / 2) ;
  114.             positionEnd.x = (int)(positionC.x + sizeX / 2) ;
  115.             positionEnd.y =(int)(positionC.y + sizeY / 2) ;
  116.             positionEnd.z = (int)(positionC.z + sizeZ / 2) ;
  117.  
  118.             Prefab prefab = new Prefab(new Vector3i(sizeX, sizeY, sizeZ));
  119.             prefab.bCopyAirBlocks = true;
  120.             prefab.filename = fileName;
  121.             prefab.CopyFromWorld (world, positionStart, positionEnd);
  122.             prefab.Save (fileName);
  123.             m_Console.SendResult (string.Concat("Your prefab is saved: ", fileName));
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment