Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: Scrangos
- * Date: 6/26/2015
- * Time: 3:34 PM
- *
- * To change this template use Tools | Options | Coding | Edit Standard Headers.
- */
- using System;
- using System.IO;
- namespace RF4TextPackUnpacker
- {
- class Program
- {
- public static void Main(string[] args)
- {
- Console.WriteLine("Hello World!");
- //open incoming file
- byte[] readBytes = new byte[4];
- int dum = args.Length;
- //Console.WriteLine(dum);
- //check if its .eng/jpn or if its an unpacked file
- if(dum!=0){
- var infile = new FileStream(args[0], FileMode.Open, FileAccess.Read);
- dum = infile.Read(readBytes,0,4);
- int fileType = BitConverter.ToInt32(readBytes,0);
- //unpack
- if(fileType==1415071060){
- //make output file
- var outfile = new FileStream(args[0]+".txt",FileMode.Create,FileAccess.ReadWrite);
- dum = infile.Read(readBytes,0,4);
- int numEntries = BitConverter.ToInt32(readBytes,0);
- //read index
- int[] fileIndex = new int[numEntries*2];
- for (int i = 0; i < numEntries*2; i++) {
- dum = infile.Read(readBytes,0,4);
- fileIndex[i] = BitConverter.ToInt32(readBytes,0);
- }
- //read and write data
- for (int i = 0; i < numEntries*2; i++) {
- int currByte = 0;
- infile.Seek(fileIndex[i+1],SeekOrigin.Begin);
- for (int j = 0; j < fileIndex[i]; j++) {
- currByte = infile.ReadByte();
- //check for newlines and convert to FF
- if(currByte==10){
- outfile.WriteByte(255);
- }
- else{
- outfile.WriteByte((byte)currByte);
- }
- }
- outfile.WriteByte(10);
- i++;
- //Console.WriteLine(dum);
- }
- outfile.Flush();
- outfile.Dispose();
- }
- else{ //repack
- var outfile = new FileStream(args[0].Substring(0,args[0].Length-7)+"jpn",FileMode.Create,FileAccess.ReadWrite);
- int entrySize = 0;
- int numEntries = 0;
- int currByte = 0;
- //Console.WriteLine(dum);
- //check num of entries
- infile.Seek(0,SeekOrigin.Begin);
- while(infile.Position!=infile.Length){
- currByte = infile.ReadByte();
- if(currByte==10){
- numEntries++;
- }
- }
- //check size of entries
- int[] fileIndex = new int[numEntries*2];
- int currEntry = 0;
- infile.Seek(0,SeekOrigin.Begin);
- currByte = infile.ReadByte();
- entrySize++;
- for (int i = 0; i < numEntries*2; i++) {
- currByte = infile.ReadByte();
- while(currByte!=10){
- entrySize++;
- currByte = infile.ReadByte();
- }
- fileIndex[currEntry]=entrySize;
- currEntry=currEntry+2;
- //Console.WriteLine(entrySize);
- entrySize=0;
- i++;
- }
- //Console.WriteLine(numEntries);
- //calculate address locations for each entry
- int endOfIndex = numEntries*8+8;
- for (int i = 0; i < numEntries*2; i++) {
- fileIndex[i+1]=endOfIndex;
- endOfIndex = endOfIndex + fileIndex[i]+1;
- i++;
- }
- // for (int i = 0; i < numEntries; i++) {
- // Console.WriteLine(fileIndex[i]);
- // }
- //write Data
- //write header
- infile.Seek(0,SeekOrigin.Begin);
- outfile.WriteByte(84);
- outfile.WriteByte(69);
- outfile.WriteByte(88);
- outfile.WriteByte(84);
- readBytes = BitConverter.GetBytes(numEntries);
- outfile.Write(readBytes,0,4);
- //Console.WriteLine(outfile.Position);
- //write index
- for (int i = 0; i < numEntries*2; i++) {
- readBytes = BitConverter.GetBytes(fileIndex[i]);
- outfile.Write(readBytes,0,4);
- readBytes = BitConverter.GetBytes(fileIndex[i+1]);
- outfile.Write(readBytes,0,4);
- i++;
- }
- //write entry text
- for (int i = 0; i < numEntries*2; i++) {
- outfile.Seek(fileIndex[i+1],SeekOrigin.Begin);
- //Console.WriteLine(fileIndex[i]);
- for (int j = 0; j < fileIndex[i]; j++) {
- //outfile.WriteByte((byte)infile.ReadByte());
- currByte = infile.ReadByte();
- if(currByte==255){
- outfile.WriteByte(10);
- }
- else{
- outfile.WriteByte((byte)currByte);
- }
- }
- outfile.WriteByte(0);
- dum = infile.ReadByte();
- i++;
- }
- outfile.Flush();
- outfile.Dispose();
- }
- infile.Dispose();
- // Console.Write("Press any key to continue . . . ");
- //Console.ReadKey(true);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement