Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using Ionic.Zip;
- namespace Brick_Creator
- {
- class Program
- {
- static void Main(String[] args)
- {
- Double Version = 2.68;
- Console.Title = "Brick Creator v" + Version;
- //Show the user some info.
- Console.WriteLine("Brick Creator v" + Version);
- Console.WriteLine("Created by Ipquarx");
- Console.WriteLine("Licensed under GNU GPL v3.");
- Console.WriteLine();
- UInt16 Num = 1;
- //Pretty obvious.
- Console.WriteLine("How many Bricks would you like to Create?:");
- //For the people who are stupid enough to enter a hugeass number.
- try
- {
- Num = UInt16.Parse(Console.ReadLine());
- }
- catch(Exception ex) //OMGWTFBBQERROR
- {
- if(ex is OverflowException || ex is FormatException)
- {
- Console.WriteLine("You did not enter a valid number!");
- Console.WriteLine("Please enter a number between 1 and 500."); //Because I dont see why someone would make 501 bricks with this.
- Console.Write("Press any key to restart.");
- Console.ReadKey();
- Console.Clear();
- Main(args);
- }
- else
- throw;
- }
- //Create the info array for later use and Create the path array.
- String[] Info = new String[7];
- String[] Zipping = new String[Num + 1];
- Console.WriteLine();
- //Delete previous files for a fresh run.
- File.Delete("server.cs");
- File.Delete("bricks.zip");
- //Start making bricks!
- UInt32 L, W, H;
- for(Int32 a = 0; a < Num; a++)
- {
- //Ask for Width, Length, and Height, and catch any errors like a argument of 0 or really big.
- try
- {
- Console.WriteLine("Width:");
- W = UInt16.Parse(Console.ReadLine());
- if (W == 0)
- throw new OverflowException();
- Console.WriteLine();
- Console.WriteLine("Length:");
- L = UInt16.Parse(Console.ReadLine());
- if (L == 0)
- throw new OverflowException();
- if (L < W) //If length is less than Width, try again.
- {
- Console.WriteLine("Error: Length must be Greater than or Equal to the Width!");
- Console.Write("Press any key to try again.");
- Console.ReadKey();
- Console.Clear();
- Console.WriteLine("Brick Creator v" + Version); //Redisplay the info.
- Console.WriteLine("Created by Ipquarx");
- Console.WriteLine("Licensed under GNU GPL v3.");
- Console.WriteLine();
- a--;
- continue;
- }
- Console.WriteLine();
- Console.WriteLine("Height: (Remember, 3 is 1 stud high!)");
- H = UInt16.Parse(Console.ReadLine());
- if (H == 0)
- throw new OverflowException();
- }
- catch (Exception ex2)
- {
- if(ex2 is OverflowException || ex2 is FormatException)
- {
- Console.WriteLine();
- Console.WriteLine("You entered an invalid number!");
- Console.WriteLine("All numbers must be above 0.");
- Console.Write("Press any key to try again.");
- Console.ReadKey();
- Console.Clear();
- Console.WriteLine("Brick Creator v" + Version); //Redisplay the info.
- Console.WriteLine("Created by Ipquarx");
- Console.WriteLine("Licensed under GNU GPL v3.");
- Console.WriteLine();
- a--;
- continue;
- }
- }
- Console.WriteLine();
- Console.WriteLine("Getting Information..."); //Get information on the brick (filename, categories, etc.)
- //For people who are dumb enough to enter something that would cause an error
- try
- {
- Info = getBrickInfo(L, W, H);
- }
- catch (ArgumentException)
- {
- Console.WriteLine("You entered 0 for one of the arguments!");
- Console.WriteLine("Please enter a number above 0 for all bricks.");
- a--;
- continue;
- }
- Console.WriteLine("Done!");
- Console.WriteLine();
- Console.WriteLine("Brick Information:"); //Show the user the information.
- Console.WriteLine("FileName: " + Info[0]);
- Console.WriteLine("UIName: " + Info[1]);
- Console.WriteLine("Category: " + Info[2]);
- Console.WriteLine("SubCategory: " + Info[3]);
- Console.WriteLine();
- Console.WriteLine("Creating .blb File..."); //Create the brick file,
- writeBrick(Info);
- Console.WriteLine("Adding brick to zipping list..."); //Add the brick to the file zipping list,
- Zipping[a] = Info[0] + ".blb";
- Console.WriteLine("Creating/Updating server.cs File..."); //Change the server.cs accordingly.
- Console.WriteLine();
- createCSFile(Info);
- if (a != Num - 1) //Only display these messages if it's not the last brick.
- {
- Console.WriteLine("Done!");
- Console.Write("Press any key to go onto the next brick."); //Move onto the next brick,
- Console.ReadKey();
- Console.Clear(); //Clear the console,
- Console.WriteLine("Brick Creator v" + Version); //Redisplay the info.
- Console.WriteLine("Created by Ipquarx");
- Console.WriteLine("Licensed under GNU GPL v3.");
- Console.WriteLine();
- }
- }
- Zipping[Num] = "server.cs"; //Add server.cs to the file zipping list.
- Console.WriteLine("Zipping Files..."); //Package the files.
- zipFile(Zipping);
- Console.WriteLine("Done!");
- Console.Write("Press any key to exit."); //Exit when the user presses a key.
- Console.ReadKey();
- }
- public static String[] getBrickInfo(UInt32 Length, UInt32 Width, UInt32 Height) //Get info on the brick.
- {
- //Check if any of the arguments are 0, which is invalid.
- if (Length == 0)
- throw new System.ArgumentException("Parameter must be > 0", "Length");
- else if (Width == 0)
- throw new System.ArgumentException("Parameter must be > 0", "Width");
- else if (Height == 0)
- throw new System.ArgumentException("Parameter must be > 0", "Height");
- String UiName, FileName, Category, subCategory; //Create info strings
- String[] Info = new String[7]; //Create info array
- FileName = "If you see this, something has gone horribly wrong."; //It's true. If you see that, add it to the bug list.
- UiName = Width + "x" + Length; //UIName should always be Width x Length with this name decision engine.
- //Info Generator Decision Tree
- if (Height != 1) //Check if the brick is not a plate
- {
- Category = "Bricks"; //Set the category to bricks (Because it's definetly NOT a plate!)
- if (Height == 3) //1x height doesnt need the 1x height prefix.
- {
- FileName = Width + "x" + Length; //Set the filename and subcategory.
- subCategory = Width + "x";
- }
- else if (Math.Floor(Height / 3f) != Height / 3f) //Check if it's not divisible by 3. (In other words, is it a "halfbrick.")
- {
- if (Math.Floor(Height / 3f) == 0) //Check if the height is below 1 stud
- subCategory = Height - Math.Floor(Height / 3f) + "/3x Height";
- else
- 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.
- }
- else //The brick is a even-studded height.
- subCategory = Height / 3 + "x Height";
- FileName = Width + "x" + Length + "x" + Height;
- }
- else //The brick is a plate
- {
- UiName += "f"; //Add the "flat" extension to the ui name,
- FileName = Width + "x" + Length + "f"; // Since its a plate, instead of a height it just displays "f."
- Category = "Plates";
- subCategory = Width + "x";
- }
- //Put the information of the brick into a array,
- Info[0] = FileName;
- Info[1] = UiName;
- Info[2] = Category;
- Info[3] = subCategory;
- Info[4] = Length.ToString();
- Info[5] = Width.ToString();
- Info[6] = Height.ToString();
- return Info; //Return the array.
- }
- public static Boolean writeBrick(String[] Info) //Function to write brick files
- {
- //Throw an error if any of the info parts are blank.
- for (UInt32 i = 0; i < 7; i++)
- {
- if (Info[i] == "")
- throw new System.ArgumentException("The " + (i + 1) + "th info array was null.", "Info");
- }
- //Create the brick file
- var brickFile = File.Create(Info[0] + ".blb");
- var brickWriter = new StreamWriter(brickFile); //Create a file writer for the brick file.
- //Write the BLB code
- brickWriter.WriteLine(Info[4] + " " + Info[5] + " " + Info[6]);
- brickWriter.Write("BRICK");
- //Get rid of the file and filewriter.
- brickWriter.Close();
- brickWriter.Dispose();
- return true; //return.
- }
- public static Boolean createCSFile(String[] Info) //Create the server.cs file for the info givin.
- {
- //Throw an error if any of the info parts are blank.
- for (UInt32 i = 0; i < 7; i++)
- {
- if (Info[i] == "")
- throw new System.ArgumentException("The " + (i + 1) + "th info array was null.", "Info");
- }
- //Create the filewriter to add to the existing (or create) server.cs file.
- StreamWriter csWriter = File.AppendText("server.cs");
- //Create the lines to write
- String[] Lines = new String[7];
- //Write the code from the info array to the line array
- Lines[0] = "datablock fxDTSBrickData(brick" + Info[4] + "x" + Info[5] + "x" + Info[6] + "Data)";
- Lines[1] = "{";
- Lines[2] = " brickFile = \"./" + Info[0] + ".blb\";";
- Lines[3] = " category = \"" + Info[2] + "\";";
- Lines[4] = " subCategory = \"" + Info[3] + "\";";
- Lines[5] = " uiName = \"" + Info[1] + "\";";
- Lines[6] = "};";
- //Write the lines to the file
- for (UInt32 i = 0; i < 7; i++)
- csWriter.WriteLine(Lines[i]);
- //Get rid of the filewriter.
- csWriter.Close();
- csWriter.Dispose();
- return true; //return.
- }
- public static Boolean createDescription(String Title, String Author, String Description) //Unused for now, will be used in the next version.
- {
- var desFile = File.Create("description.txt");
- var desWriter = new StreamWriter(desFile);
- desWriter.WriteLine("Title: " + Title);
- desWriter.WriteLine("Author " + Author);
- desWriter.WriteLine(Description);
- desWriter.Close();
- desWriter.Dispose();
- return true;
- }
- public static Boolean zipFile(String[] Paths) //Zip the files listed in a path array.
- {
- //Create the zip file,
- ZipFile AddonZipper = new ZipFile();
- //Zip every single file in the path array,
- for (Int32 a = 0; a < Paths.Length; a++)
- AddonZipper.AddFile(Paths[a]);
- //Save the zip file,
- AddonZipper.Save("bricks.zip");
- return true; //return.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment