Advertisement
Guest User

Untitled

a guest
Jun 27th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. /*
  2. * Created by SharpDevelop.
  3. * User: Scrangos
  4. * Date: 6/26/2015
  5. * Time: 3:34 PM
  6. *
  7. * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. */
  9. using System;
  10. using System.IO;
  11.  
  12. namespace RF4TextPackUnpacker
  13. {
  14. class Program
  15. {
  16. public static void Main(string[] args)
  17. {
  18. Console.WriteLine("Hello World!");
  19. //open incoming file
  20.  
  21.  
  22. byte[] readBytes = new byte[4];
  23. int dum = args.Length;
  24. //Console.WriteLine(dum);
  25. //check if its .eng/jpn or if its an unpacked file
  26. if(dum!=0){
  27. var infile = new FileStream(args[0], FileMode.Open, FileAccess.Read);
  28. dum = infile.Read(readBytes,0,4);
  29. int fileType = BitConverter.ToInt32(readBytes,0);
  30.  
  31. //unpack
  32. if(fileType==1415071060){
  33.  
  34. //make output file
  35. var outfile = new FileStream(args[0]+".txt",FileMode.Create,FileAccess.ReadWrite);
  36. dum = infile.Read(readBytes,0,4);
  37. int numEntries = BitConverter.ToInt32(readBytes,0);
  38.  
  39. //read index
  40. int[] fileIndex = new int[numEntries*2];
  41. for (int i = 0; i < numEntries*2; i++) {
  42. dum = infile.Read(readBytes,0,4);
  43. fileIndex[i] = BitConverter.ToInt32(readBytes,0);
  44. }
  45.  
  46. //read and write data
  47. for (int i = 0; i < numEntries*2; i++) {
  48. int currByte = 0;
  49. infile.Seek(fileIndex[i+1],SeekOrigin.Begin);
  50. for (int j = 0; j < fileIndex[i]; j++) {
  51. currByte = infile.ReadByte();
  52. //check for newlines and convert to FF
  53. if(currByte==10){
  54. outfile.WriteByte(255);
  55. }
  56. else{
  57. outfile.WriteByte((byte)currByte);
  58. }
  59. }
  60. outfile.WriteByte(10);
  61. i++;
  62. //Console.WriteLine(dum);
  63. }
  64.  
  65. outfile.Flush();
  66. outfile.Dispose();
  67.  
  68. }
  69. else{ //repack
  70. var outfile = new FileStream(args[0].Substring(0,args[0].Length-7)+"jpn",FileMode.Create,FileAccess.ReadWrite);
  71. int entrySize = 0;
  72. int numEntries = 0;
  73. int currByte = 0;
  74. //Console.WriteLine(dum);
  75. //check num of entries
  76. infile.Seek(0,SeekOrigin.Begin);
  77. while(infile.Position!=infile.Length){
  78. currByte = infile.ReadByte();
  79. if(currByte==10){
  80. numEntries++;
  81. }
  82. }
  83. //check size of entries
  84. int[] fileIndex = new int[numEntries*2];
  85. int currEntry = 0;
  86. infile.Seek(0,SeekOrigin.Begin);
  87. currByte = infile.ReadByte();
  88. entrySize++;
  89. for (int i = 0; i < numEntries*2; i++) {
  90. currByte = infile.ReadByte();
  91. while(currByte!=10){
  92. entrySize++;
  93. currByte = infile.ReadByte();
  94. }
  95. fileIndex[currEntry]=entrySize;
  96. currEntry=currEntry+2;
  97. //Console.WriteLine(entrySize);
  98. entrySize=0;
  99. i++;
  100. }
  101. //Console.WriteLine(numEntries);
  102. //calculate address locations for each entry
  103. int endOfIndex = numEntries*8+8;
  104. for (int i = 0; i < numEntries*2; i++) {
  105. fileIndex[i+1]=endOfIndex;
  106. endOfIndex = endOfIndex + fileIndex[i]+1;
  107. i++;
  108. }
  109. // for (int i = 0; i < numEntries; i++) {
  110. // Console.WriteLine(fileIndex[i]);
  111. // }
  112. //write Data
  113. //write header
  114. infile.Seek(0,SeekOrigin.Begin);
  115. outfile.WriteByte(84);
  116. outfile.WriteByte(69);
  117. outfile.WriteByte(88);
  118. outfile.WriteByte(84);
  119. readBytes = BitConverter.GetBytes(numEntries);
  120. outfile.Write(readBytes,0,4);
  121. //Console.WriteLine(outfile.Position);
  122. //write index
  123. for (int i = 0; i < numEntries*2; i++) {
  124. readBytes = BitConverter.GetBytes(fileIndex[i]);
  125. outfile.Write(readBytes,0,4);
  126. readBytes = BitConverter.GetBytes(fileIndex[i+1]);
  127. outfile.Write(readBytes,0,4);
  128. i++;
  129. }
  130. //write entry text
  131. for (int i = 0; i < numEntries*2; i++) {
  132. outfile.Seek(fileIndex[i+1],SeekOrigin.Begin);
  133. //Console.WriteLine(fileIndex[i]);
  134. for (int j = 0; j < fileIndex[i]; j++) {
  135. //outfile.WriteByte((byte)infile.ReadByte());
  136. currByte = infile.ReadByte();
  137. if(currByte==255){
  138. outfile.WriteByte(10);
  139. }
  140. else{
  141. outfile.WriteByte((byte)currByte);
  142. }
  143. }
  144. outfile.WriteByte(0);
  145. dum = infile.ReadByte();
  146. i++;
  147. }
  148. outfile.Flush();
  149. outfile.Dispose();
  150.  
  151. }
  152.  
  153.  
  154. infile.Dispose();
  155. // Console.Write("Press any key to continue . . . ");
  156. //Console.ReadKey(true);
  157.  
  158. }
  159.  
  160.  
  161.  
  162.  
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement