Ipquarx

Brick Creator (C#) (WIP)

Jan 9th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using Ionic.Zip;
  7.  
  8. namespace Brick_Creator
  9. {
  10.     class Program
  11.     {
  12.         static void Main(String[] args)
  13.         {
  14.             Double Version = 2.68;
  15.             Console.Title = "Brick Creator v" + Version;
  16.             //Show the user some info.
  17.             Console.WriteLine("Brick Creator v" + Version);
  18.             Console.WriteLine("Created by Ipquarx");
  19.             Console.WriteLine("Licensed under GNU GPL v3.");
  20.             Console.WriteLine();
  21.             UInt16 Num = 1;
  22.             //Pretty obvious.
  23.  
  24.             Console.WriteLine("How many Bricks would you like to Create?:");
  25.  
  26.             //For the people who are stupid enough to enter a hugeass number.
  27.             try
  28.             {
  29.                 Num = UInt16.Parse(Console.ReadLine());
  30.             }
  31.             catch(Exception ex) //OMGWTFBBQERROR
  32.             {
  33.                 if(ex is OverflowException || ex is FormatException)
  34.                 {
  35.                     Console.WriteLine("You did not enter a valid number!");
  36.                     Console.WriteLine("Please enter a number between 1 and 500."); //Because I dont see why someone would make 501 bricks with this.
  37.                     Console.Write("Press any key to restart.");
  38.                     Console.ReadKey();
  39.                     Console.Clear();
  40.                     Main(args);
  41.                 }
  42.                 else
  43.                     throw;
  44.             }
  45.  
  46.             //Create the info array for later use and Create the path array.
  47.             String[] Info = new String[7];
  48.             String[] Zipping = new String[Num + 1];
  49.  
  50.             Console.WriteLine();
  51.  
  52.             //Delete previous files for a fresh run.
  53.             File.Delete("server.cs");
  54.             File.Delete("bricks.zip");
  55.  
  56.             //Start making bricks!
  57.             UInt32 L, W, H;
  58.             for(Int32 a = 0; a < Num; a++)
  59.             {
  60.                 //Ask for Width, Length, and Height, and catch any errors like a argument of 0 or really big.
  61.                 try
  62.                 {
  63.                     Console.WriteLine("Width:");
  64.                     W = UInt16.Parse(Console.ReadLine());
  65.  
  66.                     if (W == 0)
  67.                         throw new OverflowException();
  68.  
  69.                     Console.WriteLine();
  70.                     Console.WriteLine("Length:");
  71.                     L = UInt16.Parse(Console.ReadLine());
  72.                     if (L == 0)
  73.                         throw new OverflowException();
  74.  
  75.                     if (L < W) //If length is less than Width, try again.
  76.                     {
  77.                         Console.WriteLine("Error: Length must be Greater than or Equal to the Width!");
  78.                         Console.Write("Press any key to try again.");
  79.                         Console.ReadKey();
  80.                         Console.Clear();
  81.                         Console.WriteLine("Brick Creator v" + Version); //Redisplay the info.
  82.                         Console.WriteLine("Created by Ipquarx");
  83.                         Console.WriteLine("Licensed under GNU GPL v3.");
  84.                         Console.WriteLine();
  85.                         a--;
  86.                         continue;
  87.                     }
  88.  
  89.                     Console.WriteLine();
  90.                     Console.WriteLine("Height: (Remember, 3 is 1 stud high!)");
  91.                     H = UInt16.Parse(Console.ReadLine());
  92.                     if (H == 0)
  93.                         throw new OverflowException();
  94.  
  95.                 }
  96.                 catch (Exception ex2)
  97.                 {
  98.                     if(ex2 is OverflowException || ex2 is FormatException)
  99.                     {
  100.                         Console.WriteLine();
  101.                         Console.WriteLine("You entered an invalid number!");
  102.                         Console.WriteLine("All numbers must be above 0.");
  103.                         Console.Write("Press any key to try again.");
  104.                         Console.ReadKey();
  105.                         Console.Clear();
  106.                         Console.WriteLine("Brick Creator v" + Version); //Redisplay the info.
  107.                         Console.WriteLine("Created by Ipquarx");
  108.                         Console.WriteLine("Licensed under GNU GPL v3.");
  109.                         Console.WriteLine();
  110.                         a--;
  111.                         continue;
  112.                     }
  113.                 }
  114.                 Console.WriteLine();
  115.  
  116.                 Console.WriteLine("Getting Information..."); //Get information on the brick (filename, categories, etc.)
  117.                
  118.                 //For people who are dumb enough to enter something that would cause an error
  119.                 try
  120.                 {
  121.                     Info = getBrickInfo(L, W, H);
  122.                 }
  123.                 catch (ArgumentException)
  124.                 {
  125.                     Console.WriteLine("You entered 0 for one of the arguments!");
  126.                     Console.WriteLine("Please enter a number above 0 for all bricks.");
  127.                     a--;
  128.                     continue;
  129.                 }
  130.  
  131.                 Console.WriteLine("Done!");
  132.                 Console.WriteLine();
  133.  
  134.                 Console.WriteLine("Brick Information:"); //Show the user the information.
  135.                 Console.WriteLine("FileName:    " + Info[0]);
  136.                 Console.WriteLine("UIName:      " + Info[1]);
  137.                 Console.WriteLine("Category:    " + Info[2]);
  138.                 Console.WriteLine("SubCategory: " + Info[3]);
  139.                 Console.WriteLine();
  140.  
  141.                 Console.WriteLine("Creating .blb File..."); //Create the brick file,
  142.                 writeBrick(Info);
  143.  
  144.                 Console.WriteLine("Adding brick to zipping list..."); //Add the brick to the file zipping list,
  145.                 Zipping[a] = Info[0] + ".blb";
  146.  
  147.                 Console.WriteLine("Creating/Updating server.cs File..."); //Change the server.cs accordingly.
  148.                 Console.WriteLine();
  149.                 createCSFile(Info);
  150.  
  151.                 if (a != Num - 1) //Only display these messages if it's not the last brick.
  152.                 {
  153.                     Console.WriteLine("Done!");
  154.                     Console.Write("Press any key to go onto the next brick."); //Move onto the next brick,
  155.                     Console.ReadKey();
  156.                     Console.Clear(); //Clear the console,
  157.  
  158.                     Console.WriteLine("Brick Creator v" + Version); //Redisplay the info.
  159.                     Console.WriteLine("Created by Ipquarx");
  160.                     Console.WriteLine("Licensed under GNU GPL v3.");
  161.                     Console.WriteLine();
  162.                 }
  163.             }
  164.  
  165.             Zipping[Num] = "server.cs"; //Add server.cs to the file zipping list.
  166.  
  167.             Console.WriteLine("Zipping Files..."); //Package the files.
  168.             zipFile(Zipping);
  169.  
  170.             Console.WriteLine("Done!");
  171.             Console.Write("Press any key to exit."); //Exit when the user presses a key.
  172.             Console.ReadKey();
  173.         }
  174.  
  175.         public static String[] getBrickInfo(UInt32 Length, UInt32 Width, UInt32 Height) //Get info on the brick.
  176.         {
  177.             //Check if any of the arguments are 0, which is invalid.
  178.             if (Length == 0)
  179.                 throw new System.ArgumentException("Parameter must be > 0", "Length");
  180.             else if (Width == 0)
  181.                 throw new System.ArgumentException("Parameter must be > 0", "Width");
  182.             else if (Height == 0)
  183.                 throw new System.ArgumentException("Parameter must be > 0", "Height");
  184.  
  185.             String UiName, FileName, Category, subCategory; //Create info strings
  186.             String[] Info = new String[7]; //Create info array
  187.  
  188.             FileName = "If you see this, something has gone horribly wrong."; //It's true. If you see that, add it to the bug list.
  189.  
  190.             UiName = Width + "x" + Length; //UIName should always be Width x Length with this name decision engine.
  191.  
  192.             //Info Generator Decision Tree
  193.             if (Height != 1) //Check if the brick is not a plate
  194.             {
  195.                 Category = "Bricks"; //Set the category to bricks (Because it's definetly NOT a plate!)
  196.  
  197.                 if (Height == 3) //1x height doesnt need the 1x height prefix.
  198.                 {
  199.                     FileName = Width + "x" + Length; //Set the filename and subcategory.
  200.                     subCategory = Width + "x";
  201.                 }
  202.                 else if (Math.Floor(Height / 3f) != Height / 3f) //Check if it's not divisible by 3. (In other words, is it a "halfbrick.")
  203.                 {
  204.                     if (Math.Floor(Height / 3f) == 0) //Check if the height is below 1 stud
  205.                         subCategory = Height - Math.Floor(Height / 3f) + "/3x Height";
  206.                     else
  207.                         subCategory = Math.Floor(Height / 3f) + " & " + (Height - (3 * Math.Floor(Height / 3f))) + "/3x Height"; //Splits the height into a full number and a fraction out of 3.
  208.                 }
  209.                 else //The brick is a even-studded height.
  210.                     subCategory = Height / 3 + "x Height";
  211.                 FileName = Width + "x" + Length + "x" + Height;
  212.             }
  213.             else //The brick is a plate
  214.             {
  215.                 UiName += "f"; //Add the "flat" extension to the ui name,
  216.                 FileName = Width + "x" + Length + "f"; // Since its a plate, instead of a height it just displays "f."
  217.                 Category = "Plates";
  218.                 subCategory = Width + "x";
  219.             }
  220.  
  221.             //Put the information of the brick into a array,
  222.             Info[0] = FileName;
  223.             Info[1] = UiName;
  224.             Info[2] = Category;
  225.             Info[3] = subCategory;
  226.             Info[4] = Length.ToString();
  227.             Info[5] = Width.ToString();
  228.             Info[6] = Height.ToString();
  229.  
  230.             return Info; //Return the array.
  231.         }
  232.  
  233.         public static Boolean writeBrick(String[] Info) //Function to write brick files
  234.         {
  235.             //Throw an error if any of the info parts are blank.
  236.             for (UInt32 i = 0; i < 7; i++)
  237.             {
  238.                 if (Info[i] == "")
  239.                     throw new System.ArgumentException("The " + (i + 1) + "th info array was null.", "Info");
  240.             }
  241.  
  242.             //Create the brick file
  243.             var brickFile = File.Create(Info[0] + ".blb");
  244.             var brickWriter = new StreamWriter(brickFile); //Create a file writer for the brick file.
  245.  
  246.             //Write the BLB code
  247.             brickWriter.WriteLine(Info[4] + " " + Info[5] + " " + Info[6]);
  248.             brickWriter.Write("BRICK");
  249.  
  250.             //Get rid of the file and filewriter.
  251.             brickWriter.Close();
  252.             brickWriter.Dispose();
  253.  
  254.             return true; //return.
  255.         }
  256.  
  257.         public static Boolean createCSFile(String[] Info) //Create the server.cs file for the info givin.
  258.         {
  259.             //Throw an error if any of the info parts are blank.
  260.             for (UInt32 i = 0; i < 7; i++)
  261.             {
  262.                 if (Info[i] == "")
  263.                     throw new System.ArgumentException("The " + (i + 1) + "th info array was null.", "Info");
  264.             }
  265.  
  266.             //Create the filewriter to add to the existing (or create) server.cs file.
  267.             StreamWriter csWriter = File.AppendText("server.cs");
  268.  
  269.             //Create the lines to write
  270.             String[] Lines = new String[7];
  271.  
  272.             //Write the code from the info array to the line array
  273.             Lines[0] = "datablock fxDTSBrickData(brick" + Info[4] + "x" + Info[5] + "x" + Info[6] + "Data)";
  274.             Lines[1] = "{";
  275.             Lines[2] = "    brickFile = \"./" + Info[0] + ".blb\";";
  276.             Lines[3] = "    category = \"" + Info[2] + "\";";
  277.             Lines[4] = "    subCategory = \"" + Info[3] + "\";";
  278.             Lines[5] = "    uiName = \"" + Info[1] + "\";";
  279.             Lines[6] = "};";
  280.  
  281.             //Write the lines to the file
  282.             for (UInt32 i = 0; i < 7; i++)
  283.                 csWriter.WriteLine(Lines[i]);
  284.  
  285.             //Get rid of the filewriter.
  286.             csWriter.Close();
  287.             csWriter.Dispose();
  288.  
  289.             return true; //return.
  290.         }
  291.  
  292.         public static Boolean createDescription(String Title, String Author, String Description) //Unused for now, will be used in the next version.
  293.         {
  294.             var desFile = File.Create("description.txt");
  295.             var desWriter = new StreamWriter(desFile);
  296.             desWriter.WriteLine("Title: " + Title);
  297.             desWriter.WriteLine("Author " + Author);
  298.             desWriter.WriteLine(Description);
  299.             desWriter.Close();
  300.             desWriter.Dispose();
  301.             return true;
  302.         }
  303.  
  304.         public static Boolean zipFile(String[] Paths) //Zip the files listed in a path array.
  305.         {
  306.             //Create the zip file,
  307.             ZipFile AddonZipper = new ZipFile();
  308.  
  309.             //Zip every single file in the path array,
  310.             for (Int32 a = 0; a < Paths.Length; a++)
  311.                 AddonZipper.AddFile(Paths[a]);
  312.  
  313.             //Save the zip file,
  314.             AddonZipper.Save("bricks.zip");
  315.  
  316.             return true; //return.
  317.         }
  318.     }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment