Sharpii-NetCore/Sharpii/U8.cs
Shadow Eevee f9198444e2 Added custom exit codes.
Added custom exit codes, which allow headless scripts to use Sharpii easier. Exit Codes do NOT interfere with normal system exit codes. (System exit codes are 15999 and below, Custom exits are 16001+.)
2020-03-07 21:44:10 -08:00

245 lines
No EOL
8.9 KiB
C#

/* This file is part of Sharpii.
* Copyright (C) 2013 Person66
*
* Sharpii is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sharpii is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sharpii. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using libWiiSharp;
namespace Sharpii
{
partial class U8_Stuff
{
public static void U8(string[] args)
{
if (args.Length < 4)
{
U8_help();
return;
}
//********************* Pack *********************
if (args[1] == "-p")
{
Pack(args);
return;
}
//********************* Unpack *********************
if (args[1] == "-u")
{
Unpack(args);
return;
}
//If tuser gets here, they entered something wrong
Console.WriteLine("ERROR: The argument {0} is invalid", args[1]);
Console.WriteLine("Error: SHARPII_NET_CORE_U8_INVALID_ARG_01");
Environment.Exit(0x00000650);
return;
}
private static void Unpack(string[] args)
{
string input = args[2];
string output = args[3];
//Check if file exists
if (File.Exists(input) == false)
{
Console.WriteLine("ERROR: Unable to open file: {0}", input);
Console.WriteLine("Either the file doesn't exist, or Sharpii doesn't have permission to open it.");
Console.WriteLine("Error: SHARPII_NET_CORE_U8_FILE_ERR_01");
return;
}
//Check if file is U8
if (libWiiSharp.U8.IsU8(input) != true)
{
Console.WriteLine("ERROR: File {0} is not a U8 archive", input);
Console.WriteLine("Error: SHARPII_NET_CORE_U8_NON_U8_01");
Environment.Exit(0x00000653);
return;
}
//Run main part, and check for exceptions
try
{
//Load U8
U8 U8file = new U8();
if (BeQuiet.quiet > 2)
Console.Write("Loading file...");
U8file.LoadFile(input);
if (BeQuiet.quiet > 2)
Console.Write("Done!\n");
if (BeQuiet.quiet > 2)
Console.Write("Extracting file...");
U8file.Extract(output);
if (BeQuiet.quiet > 2)
Console.Write("Done!\n");
if (BeQuiet.quiet > 1)
Console.WriteLine("Operation completed succesfully!");
}
catch (Exception ex)
{
Console.WriteLine("An unknown error occured, please try again");
Console.WriteLine("");
Console.WriteLine("ERROR DETAILS: {0}", ex.Message);
Console.WriteLine("Error: SHARPII_NET_CORE_U8_UNKNOWN_01");
Environment.Exit(0x00000642);
return;
}
}
private static void Pack(string[] args)
{
//Setting up variables
string input = args[2];
string output = args[3];
bool lz77 = false;
bool imd5 = false;
string imet = "";
//Check if folder exists
if (Directory.Exists(input) == false)
{
Console.WriteLine("ERROR: Unable to open Folder: {0}", input);
Console.WriteLine("Either the folder doesn't exist, or Sharpii doesn't have permission to open it.");
Console.WriteLine("Error: SHARPII_NET_CORE_U8_FOLDER_ERR_01");
Environment.Exit(0x00000654);
return;
}
for (int i = 1; i < args.Length; i++)
{
switch (args[i].ToUpper())
{
case "-LZ77":
lz77 = true;
break;
case "-IMD5":
imd5 = true;
break;
case "-IMET":
if (i + 1 >= args.Length)
{
Console.WriteLine("ERROR: No title set");
Console.WriteLine("Error: SHARPII_NET_CORE_U8_NO_TITLE_01");
Environment.Exit(0x00000655);
return;
}
imet = args[i + 1];
break;
}
}
if (imd5 == true && imet != "")
{
Console.WriteLine("ERROR: Cannot use IMET and IMD5 at the same time.");
Console.WriteLine("Error: SHARPII_NET_CORE_U8_TWO_HEADERS_01");
Environment.Exit(0x00000656);
return;
}
//Run main part, and check for exceptions
try
{
U8 U8folder = new U8();
if (BeQuiet.quiet > 2)
Console.Write("Loading folder...");
U8folder.CreateFromDirectory(input);
if (BeQuiet.quiet > 2)
Console.Write("Done!\n");
if (imd5 == true)
{
if (BeQuiet.quiet > 2)
Console.WriteLine("Adding IMD5 Header");
U8folder.AddHeaderImd5();
}
if (imet != "")
{
if (BeQuiet.quiet > 2)
Console.WriteLine("Adding IMET header with title: {0}", imet);
U8folder.AddHeaderImet(false, imet);
}
if (lz77 == true)
{
//Yeah, I know this isnt where it actually compresses it
if (BeQuiet.quiet > 2)
Console.WriteLine("Compressing U8 archive");
U8folder.Lz77Compress = true;
}
if (BeQuiet.quiet > 2)
Console.WriteLine("Saving file");
U8folder.Save(output);
if (BeQuiet.quiet > 1)
Console.WriteLine("Operation completed succesfully!");
}
catch (Exception ex)
{
Console.WriteLine("An unknown error occured, please try again");
Console.WriteLine("");
Console.WriteLine("ERROR DETAILS: {0}", ex.Message);
Console.WriteLine("Error: SHARPII_NET_CORE_U8_UNKNOWN_01");
Environment.Exit(0x00000642);
return;
}
}
public static void U8_help()
{
Console.WriteLine("");
Console.WriteLine("Sharpii {0} - U8 - A tool by person66, using libWiiSharp.dll by leathl", ProgramVersion.version);
Console.WriteLine("Sharpii .Net Core Port by TheShadowEevee");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine(" Usage:");
Console.WriteLine("");
Console.WriteLine(" Sharpii.exe U8 [-p | -u] input output [arguments]");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine(" Arguments:");
Console.WriteLine("");
Console.WriteLine(" input The input file/folder");
Console.WriteLine(" output The output file/folder");
Console.WriteLine(" -p Pack");
Console.WriteLine(" -u Unpack");
Console.WriteLine("");
Console.WriteLine(" Arguments for Packing:");
Console.WriteLine("");
Console.WriteLine(" -imet [title] Pack with an IMET header (for 00000000.app)");
Console.WriteLine(" You MUST enter a channel title");
Console.WriteLine(" -imd5 Pack with an IMD5 header (for Banner/Icon.bin)");
Console.WriteLine(" -lz77 Compress with lz77");
}
}
}