Skip to content

Commit e827ca8

Browse files
authored
Merge pull request #3 from matthe815s-projects/file-cleanup
File structure and functional refactor/overhaul
2 parents 02ee34d + a9ba7fc commit e827ca8

File tree

5 files changed

+114
-122
lines changed

5 files changed

+114
-122
lines changed

JPKDecoder.cs renamed to JPK/JPKDecoder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Huge credit to https://github.com/Chakratos/ReFrontier/blob/master/ReFrontier/jpk/JPKDecodeLz.cs for the initial code base to work with in regards to JPK encoding.
99
*/
10-
namespace MHFQuestEditor
10+
namespace MHFQuestEditor.JPK
1111
{
1212
class JPKDecoder
1313
{
@@ -56,7 +56,7 @@ public byte jpkbit_lz(Stream s)
5656
//Debug.WriteLine("flag read from {0:X8}", s.Position);
5757
m_flag = ReadByte(s);
5858
}
59-
return (byte)((m_flag >> m_shiftIndex) & 1);
59+
return (byte)(m_flag >> m_shiftIndex & 1);
6060
}
6161

6262
public void ProcessOnDecode(Stream inStream, byte[] outBuffer)//implements jpkdec_lz
@@ -74,7 +74,7 @@ public void ProcessOnDecode(Stream inStream, byte[] outBuffer)//implements jpkde
7474
if (jpkbit_lz(inStream) == 0)
7575
{
7676
//Debug.WriteLine("case 0");
77-
byte len = (byte)((jpkbit_lz(inStream) << 1) | jpkbit_lz(inStream));
77+
byte len = (byte)(jpkbit_lz(inStream) << 1 | jpkbit_lz(inStream));
7878
byte off = ReadByte(inStream);
7979
jpkcpy_lz(outBuffer, off, len + 3, ref outIndex);
8080
continue;
@@ -85,7 +85,7 @@ public void ProcessOnDecode(Stream inStream, byte[] outBuffer)//implements jpkde
8585
byte hi = ReadByte(inStream);
8686
byte lo = ReadByte(inStream);
8787
int len = (hi & 0xE0) >> 5;
88-
int off = ((hi & 0x1F) << 8) | lo;
88+
int off = (hi & 0x1F) << 8 | lo;
8989
if (len != 0)
9090
{
9191
//Debug.WriteLine("case 1");
@@ -97,7 +97,7 @@ public void ProcessOnDecode(Stream inStream, byte[] outBuffer)//implements jpkde
9797
if (jpkbit_lz(inStream) == 0)
9898
{
9999
//Debug.WriteLine("case 2");
100-
len = (byte)((jpkbit_lz(inStream) << 3) | (jpkbit_lz(inStream) << 2) | (jpkbit_lz(inStream) << 1) | jpkbit_lz(inStream));
100+
len = (byte)(jpkbit_lz(inStream) << 3 | jpkbit_lz(inStream) << 2 | jpkbit_lz(inStream) << 1 | jpkbit_lz(inStream));
101101
jpkcpy_lz(outBuffer, off, len + 2 + 8, ref outIndex);
102102
continue;
103103
}

JPKEncoder.cs renamed to JPK/JPKEncoder.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Huge credit to https://github.com/Chakratos/ReFrontier/blob/master/ReFrontier/jpk/JPKDecodeLz.cs for the initial code base to work with in regards to JPK encoding.
99
*/
10-
namespace MHFQuestEditor
10+
namespace MHFQuestEditor.JPK
1111
{
1212
class JPKEncoder
1313
{
@@ -21,16 +21,16 @@ class JPKEncoder
2121
public byte[] m_towrite = new byte[1000];
2222
public int m_itowrite;
2323

24-
public void JPKEncode(byte[] data, UInt16 atype, string otPath, int level)
24+
public void JPKEncode(byte[] data, ushort atype, string otPath, int level)
2525
{
26-
UInt16 type = atype;
26+
ushort type = atype;
2727
byte[] buffer = data;
2828
int insize = buffer.Length;
2929
if (File.Exists(otPath)) File.Delete(otPath);
3030
FileStream fsot = File.Create(otPath);
3131
BinaryWriter br = new BinaryWriter(fsot);
32-
UInt32 u32 = 0x1A524B4A;
33-
UInt16 u16 = 0x108;
32+
uint u32 = 0x1A524B4A;
33+
ushort u16 = 0x108;
3434
br.Write(u32);
3535
br.Write(u16);
3636
br.Write(type);
@@ -102,7 +102,7 @@ public void SetFlagsL(byte b, int cnt)
102102
{
103103
for (int i = cnt - 1; i >= 0; i--)
104104
{
105-
SetFlag((byte)((b >> i) & 1));
105+
SetFlag((byte)(b >> i & 1));
106106
}
107107
}
108108
public void ProcessOnEncode(byte[] inBuffer, Stream outStream, int level = 1000)
@@ -120,7 +120,7 @@ public void ProcessOnEncode(byte[] inBuffer, Stream outStream, int level = 1000)
120120
m_ind = 0;
121121
while (m_ind < inBuffer.Length)
122122
{
123-
perc = percbord + (100 - percbord) * (Int64)m_ind / inBuffer.Length;
123+
perc = percbord + (100 - percbord) * m_ind / inBuffer.Length;
124124
if (perc > perc0)
125125
{
126126
perc0 = perc;
@@ -140,16 +140,16 @@ public void ProcessOnEncode(byte[] inBuffer, Stream outStream, int level = 1000)
140140
if (len <= 6 && ofs <= 0xff)
141141
{
142142
SetFlag(0);
143-
SetFlagsL((byte)((len - 3)), 2);
143+
SetFlagsL((byte)(len - 3), 2);
144144
m_towrite[m_itowrite++] = (byte)ofs;
145145
m_ind += len;
146146
}
147147
else
148148
{
149149
SetFlag(1);
150-
UInt16 u16 = (UInt16)ofs;
150+
ushort u16 = (ushort)ofs;
151151
byte hi, lo;
152-
if (len <= 9) u16 |= (UInt16)((len - 2) << 13);
152+
if (len <= 9) u16 |= (ushort)(len - 2 << 13);
153153
hi = (byte)(u16 >> 8);
154154
lo = (byte)(u16 & 0xff);
155155
m_towrite[m_itowrite++] = hi;
@@ -160,7 +160,7 @@ public void ProcessOnEncode(byte[] inBuffer, Stream outStream, int level = 1000)
160160
if (len <= 25)
161161
{
162162
SetFlag(0);
163-
SetFlagsL((byte)((len - 10)), 4);
163+
SetFlagsL((byte)(len - 10), 4);
164164
}
165165
else
166166
{

Quest.cs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using MHFQuestEditor.JPK;
2+
using MHFQuestEditor.Utils;
63

74
namespace MHFQuestEditor
85
{
@@ -19,31 +16,53 @@ class Quest
1916
public string questContractor = "";
2017
public string questDescription = "";
2118

19+
public string extraText = "";
20+
21+
private MemoryStream decryptedData;
2222
public byte[] questCode = new byte[0];
2323

24+
public Quest (string questId, string meta)
25+
{
26+
Stream questData = LoadFileAndInitializeData(questId, meta);
27+
DecryptData(questData);
28+
LocatePointersAndReadBody();
29+
ReadQuestStrings();
30+
questData.Close();
31+
}
2432

25-
public void Load (string questId, string meta)
33+
Stream LoadFileAndInitializeData (string questId, string meta)
2634
{
2735
Stream file = File.Open(string.Format("{0}{1}.bin", questId, meta), FileMode.Open);
2836
fileName = string.Format("{0}{1}.bin", questId, meta);
37+
return file;
38+
}
2939

40+
void DecryptData (Stream file)
41+
{
3042
byte[] data = new byte[file.Length];
3143
file.Read(data, 0, data.Length);
3244

3345
data = new JPKDecoder().UnpackSimple(data);
34-
MemoryStream decryptedData = new MemoryStream(data);
46+
decryptedData = new MemoryStream(data);
47+
}
3548

49+
void LocatePointersAndReadBody ()
50+
{
3651
int mainPropsPointer = decryptedData.ReadByte();
3752
decryptedData.Seek(mainPropsPointer, SeekOrigin.Begin);
3853
decryptedData.Seek(40, SeekOrigin.Current);
54+
3955
byte[] stringPointer = new byte[4];
4056
decryptedData.Read(stringPointer, 0, 4);
41-
decryptedData.Seek(0, SeekOrigin.Begin);
4257

58+
// Seek to zero and read all the way to the string pointer - this will be copied one to one.
59+
decryptedData.Seek(0, SeekOrigin.Begin);
4360
questCode = new byte[BitConverter.ToInt32(stringPointer)];
44-
4561
decryptedData.Read(questCode, 0, BitConverter.ToInt32(stringPointer));
62+
}
4663

64+
void ReadQuestStrings ()
65+
{
4766
byte[] titlePointer = new byte[4];
4867
decryptedData.Read(titlePointer, 0, 4);
4968
byte[] mainObjectivePointer = new byte[4];
@@ -61,39 +80,37 @@ public void Load (string questId, string meta)
6180
byte[] questDescriptionPointer = new byte[4];
6281
decryptedData.Read(questDescriptionPointer, 0, 4);
6382

83+
// Quest title
6484
decryptedData.Seek(BitConverter.ToInt32(titlePointer), SeekOrigin.Begin);
85+
title = Utility.ReadNullTerminated(decryptedData);
6586

66-
title = QuestSelector.ReadNullTerminated(decryptedData);
67-
87+
// Quest main
6888
decryptedData.Seek(BitConverter.ToInt32(mainObjectivePointer), SeekOrigin.Begin);
89+
mainObjective = Utility.ReadNullTerminated(decryptedData);
6990

70-
mainObjective = QuestSelector.ReadNullTerminated(decryptedData);
71-
91+
// Quest subobjective 1
7292
decryptedData.Seek(BitConverter.ToInt32(subObjectiveAPointer), SeekOrigin.Begin);
93+
subObjective1 = Utility.ReadNullTerminated(decryptedData);
7394

74-
subObjective1 = QuestSelector.ReadNullTerminated(decryptedData);
75-
95+
// Quest subobjective 2
7696
decryptedData.Seek(BitConverter.ToInt32(subObjectiveBPointer), SeekOrigin.Begin);
97+
subObjective2 = Utility.ReadNullTerminated(decryptedData);
7798

78-
subObjective2 = QuestSelector.ReadNullTerminated(decryptedData);
79-
99+
// Quest clear conditions
80100
decryptedData.Seek(BitConverter.ToInt32(clearConditionsPointer), SeekOrigin.Begin);
101+
clearConditions = Utility.ReadNullTerminated(decryptedData);
81102

82-
clearConditions = QuestSelector.ReadNullTerminated(decryptedData);
83-
103+
// Quest failure conditions
84104
decryptedData.Seek(BitConverter.ToInt32(failConditionsPointer), SeekOrigin.Begin);
105+
failureConditions = Utility.ReadNullTerminated(decryptedData);
85106

86-
failureConditions = QuestSelector.ReadNullTerminated(decryptedData);
87-
107+
// Quest contractor
88108
decryptedData.Seek(BitConverter.ToInt32(questContractorPointer), SeekOrigin.Begin);
109+
questContractor = Utility.ReadNullTerminated(decryptedData);
89110

90-
questContractor = QuestSelector.ReadNullTerminated(decryptedData);
91-
111+
// Quest description
92112
decryptedData.Seek(BitConverter.ToInt32(questDescriptionPointer), SeekOrigin.Begin);
93-
94-
questDescription = QuestSelector.ReadNullTerminated(decryptedData);
95-
96-
file.Close();
113+
questDescription = Utility.ReadNullTerminated(decryptedData);
97114
}
98115
}
99116
}

QuestEditor.cs

Lines changed: 17 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Text;
2+
using MHFQuestEditor.JPK;
3+
using MHFQuestEditor.Utils;
24

35
namespace MHFQuestEditor
46
{
57
public partial class QuestSelector : Form
68
{
7-
byte[] originalStrings;
8-
string fileName;
9-
9+
string fileName = "";
1010
List<Quest> files = new List<Quest>();
1111

1212
public QuestSelector()
@@ -58,87 +58,28 @@ private void LoadIfExists(string questId, string meta)
5858
{
5959
if (File.Exists(string.Format("{0}{1}.bin", questId, meta)))
6060
{
61-
Quest quest = new Quest();
62-
quest.Load(questId, meta);
63-
files.Add(quest);
64-
}
65-
}
66-
67-
byte[] ReadOriginal(StreamReader rdr)
68-
{
69-
byte[] bytes = new byte[rdr.BaseStream.Length - rdr.BaseStream.Position];
70-
int length = 0;
71-
int nc;
72-
73-
while ((nc = rdr.Read()) != 0x01 && rdr.BaseStream.Position < rdr.BaseStream.Length)
74-
{
75-
length++;
76-
}
77-
78-
byte[] final = new byte[length];
79-
CopyOriginal(bytes, final);
80-
81-
return final;
82-
}
83-
84-
void CopyOriginal(byte[] old, byte[] notold)
85-
{
86-
for (int i = 0; i < notold.Length; i++)
87-
{
88-
notold[i] = old[i];
61+
files.Add(new Quest(questId, meta));
8962
}
9063
}
9164

92-
public static string ReadNullTerminated(Stream file)
93-
{
94-
StreamReader reader = new StreamReader(file, Encoding.GetEncoding(932));
95-
var bldr = new System.Text.StringBuilder();
96-
97-
int nc;
98-
while ((nc = reader.Read()) > 0)
99-
{
100-
Console.WriteLine(nc);
101-
if ((byte)nc == 10)
102-
{
103-
Console.WriteLine("New line");
104-
bldr.AppendLine("\n");
105-
}
106-
else
107-
{
108-
bldr.Append((char)nc);
109-
}
110-
}
111-
112-
return bldr.ToString();
113-
}
114-
115-
string ReformatString(string text)
116-
{
117-
text = text.Replace("\n\r\n", "\n");
118-
text = text.Replace("\r\n", "\n");
119-
text = text.Replace("", "");
120-
return text;
121-
}
122-
12365
void RewriteFile()
12466
{
125-
126-
files.ForEach(f =>
67+
files.ForEach(quest =>
12768
{
128-
BinaryWriter writer = new BinaryWriter(File.Open(f.fileName + ".backup", FileMode.Create), Encoding.GetEncoding(932));
69+
BinaryWriter writer = new BinaryWriter(File.Open(quest.fileName + ".backup", FileMode.Create), Encoding.GetEncoding(932));
12970

130-
writer.Write(f.questCode);
71+
writer.Write(quest.questCode);
13172

132-
var pointerStarter = f.questCode.Length + 32 + 28;
73+
var pointerStarter = quest.questCode.Length + 32 + 28;
13374

134-
var title = ReformatString(textBox1.Text);
135-
var main = ReformatString(textBox2.Text);
136-
var sub1 = ReformatString(textBox3.Text);
137-
var sub2 = ReformatString(textBox4.Text);
138-
var clear = ReformatString(textBox5.Text);
139-
var fail = ReformatString(textBox6.Text);
140-
var giver = ReformatString(textBox7.Text);
141-
var descrip = ReformatString(textBox8.Text);
75+
var title = Utility.ReformatString(textBox1.Text);
76+
var main = Utility.ReformatString(textBox2.Text);
77+
var sub1 = Utility.ReformatString(textBox3.Text);
78+
var sub2 = Utility.ReformatString(textBox4.Text);
79+
var clear = Utility.ReformatString(textBox5.Text);
80+
var fail = Utility.ReformatString(textBox6.Text);
81+
var giver = Utility.ReformatString(textBox7.Text);
82+
var descrip = Utility.ReformatString(textBox8.Text);
14283

14384
writer.Write((Int32)pointerStarter);
14485
writer.Write((Int32)pointerStarter + Encoding.GetEncoding(932).GetBytes(title).Length + 1);
@@ -155,9 +96,6 @@ void RewriteFile()
15596
+ Encoding.GetEncoding(932).GetBytes(sub1).Length + Encoding.GetEncoding(932).GetBytes(sub2).Length + Encoding.GetEncoding(932).GetBytes(clear).Length + Encoding.GetEncoding(932).GetBytes(fail).Length
15697
+ Encoding.GetEncoding(932).GetBytes(giver).Length + 7);
15798

158-
//writer.Write(originalStrings);
159-
//writer.Write(0x69);
160-
16199
writer.Write("依頼の品を全て納品しました");
162100
writer.Write((byte)0x00);
163101
writer.Write(Encoding.GetEncoding(932).GetBytes(title));
@@ -186,7 +124,7 @@ void RewriteFile()
186124

187125
writer.Close();
188126

189-
new JPKEncoder().JPKEncode(data, 3, f.fileName, 100);
127+
new JPKEncoder().JPKEncode(data, 3, quest.fileName, 100);
190128
});
191129
}
192130

0 commit comments

Comments
 (0)