Skip to content

Commit 6ad31e3

Browse files
committed
1.1.0: BatchExport refactored, various warnings fixed
1 parent b646258 commit 6ad31e3

File tree

7 files changed

+116
-52
lines changed

7 files changed

+116
-52
lines changed

src/BatchExport/Program.cs

Lines changed: 95 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,155 @@
11
using MADSPack.Compression;
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Drawing;
56
using System.IO;
6-
using System.Linq;
7-
using System.Text;
8-
using System.Threading.Tasks;
97

108
namespace BatchExport
119
{
1210
class Program
1311
{
14-
static string exportFolder = @"C:\Users\AyCe\Desktop\ColoExport\";
15-
static string coloFolder = @"E:\Programme\CGN\Colonization";
16-
1712
static void Main(string[] args)
1813
{
14+
try
15+
{
16+
Run(args.Length == 0 ? null : args[0]);
17+
}
18+
catch (Exception e)
19+
{
20+
Console.WriteLine($"Fatal error: {e}");
21+
}
22+
Console.WriteLine("\nPress RETURN to close.\n");
23+
Console.ReadLine();
24+
}
25+
26+
static void Run(string folderColo)
27+
{
28+
if (folderColo == null)
29+
{
30+
Console.WriteLine("Usage: drag & drop the Colonization folder on me. I will create 'ColoExport' in the current Working Directory. :)");
31+
return;
32+
}
33+
34+
if (!Directory.Exists(folderColo))
35+
{
36+
Console.WriteLine($"Not a folder: {folderColo}");
37+
return;
38+
}
39+
40+
var folderTarget = Path.Combine(Environment.CurrentDirectory, "ColoExport");
41+
42+
Directory.CreateDirectory(folderTarget);
43+
44+
new Program(folderColo, folderTarget);
45+
46+
// open in explorer
47+
Process.Start(folderTarget);
48+
}
49+
50+
private readonly string _folderColo, _folderTarget;
51+
private readonly List<string> _errorFiles = new List<string>();
52+
53+
private Program(string folderColo, string folderTarget)
54+
{
55+
_folderColo = folderColo;
56+
_folderTarget = folderTarget;
1957

20-
foreach (var file in Directory.EnumerateFiles(coloFolder))
58+
var anyFilesProcessed = false;
59+
60+
foreach (var file in Directory.EnumerateFiles(_folderColo))
2161
{
2262
var filenameLower = Path.GetFileName(file).ToLowerInvariant();
2363
try
2464
{
2565
if (filenameLower.EndsWith(".pik"))
2666
{
67+
anyFilesProcessed = true;
2768
ExportPik(file, filenameLower);
2869
}
2970
else if (filenameLower.EndsWith(".ss"))
3071
{
72+
anyFilesProcessed = true;
3173
ExportSs(file, filenameLower);
3274
}
33-
else if (filenameLower.EndsWith(".ff"))
34-
{
35-
ExportFf(file, filenameLower);
36-
}
75+
//else if (filenameLower.EndsWith(".ff"))
76+
//{
77+
// anyFilesProcessed = true;
78+
// ExportFf(file, filenameLower);
79+
//}
3780
}
3881
catch (Exception e)
3982
{
83+
_errorFiles.Add(filenameLower);
4084
Console.WriteLine($"Error for '{filenameLower}': {e}");
4185
}
4286
}
43-
Console.WriteLine("DONE");
87+
Console.WriteLine();
88+
if (!anyFilesProcessed)
89+
{
90+
Console.WriteLine("No files found. :|");
91+
}
92+
else if (_errorFiles.Count == 0)
93+
{
94+
Console.WriteLine("Finished successfully! :)");
95+
}
96+
else
97+
{
98+
Console.WriteLine("Finished with errors. Press RETURN to view all files with errors...");
99+
Console.ReadLine();
100+
foreach (var file in _errorFiles)
101+
{
102+
Console.WriteLine(file);
103+
}
104+
}
105+
Console.WriteLine("\nPress RETURN to view the folder.");
44106
Console.ReadLine();
45107
}
46108

47-
static void ExportPik(string file, string filenameLower)
109+
void ExportPik(string file, string filenameLower)
48110
{
49111
MadsPackReader r = new MadsPackReader(file);
50-
var pik = new MadsPackImagePIK(r.getItems());
51-
pik.pathtoCol = coloFolder;
112+
var pik = new MadsPackImagePIK(r.getItems())
113+
{
114+
pathtoCol = _folderColo
115+
};
52116
using (Bitmap bmp = pik.GetImage())
53117
{
54-
bmp.Save(exportFolder + filenameLower + ".png", System.Drawing.Imaging.ImageFormat.Png);
118+
bmp.Save(Path.Combine(_folderTarget, filenameLower + ".png"), System.Drawing.Imaging.ImageFormat.Png);
55119
}
56120
}
57121

58-
static void ExportSs(string file, string filenameLower)
122+
void ExportSs(string file, string filenameLower)
59123
{
60124
MadsPackReader r = new MadsPackReader(file);
61-
var ss = new MadsPackImageSS(r.getItems(), 0);
62-
ss.pathtoCol = coloFolder;
125+
var ss = new MadsPackImageSS(r.getItems(), 0)
126+
{
127+
pathtoCol = _folderColo
128+
};
63129
var numImages = ss.getPictureCount();
64130
for (int i = 0; i < numImages; i++)
65131
{
132+
var filename = $"{filenameLower}_{i}";
66133
try
67134
{
68135
using (Bitmap bmp = ss.GetImage(i))
69136
{
70-
bmp.Save(exportFolder + filenameLower + $"_{i}.png", System.Drawing.Imaging.ImageFormat.Png);
137+
bmp.Save(Path.Combine(_folderTarget, $"{filename}.png"), System.Drawing.Imaging.ImageFormat.Png);
71138
}
72139
}
73140
catch (Exception e)
74141
{
75-
Console.WriteLine($"Error for '{filenameLower}_{i}': {e}");
142+
_errorFiles.Add(filename);
143+
Console.WriteLine($"Error for '{filename}': {e}");
76144
}
77145
}
78146
}
79147

80-
static void ExportFf(string file, string filenameLower)
81-
{
148+
//void ExportFf(string file, string filenameLower)
149+
//{
150+
// currently does not support "just export all chars" AND chars also have size info.
151+
// no good format for this yet
152+
82153
//MadsPackReader r = new MadsPackReader(file);
83154
//var ff = new MadsPackFont(r.getItems()[0]);
84155
//ff.pathtoCol = coloFolder;
@@ -103,6 +174,6 @@ static void ExportFf(string file, string filenameLower)
103174
// Console.WriteLine($"Error for '{filenameLower}_{i}': {e}");
104175
// }
105176
//}
106-
}
177+
//}
107178
}
108179
}

src/BatchExport/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.1.0")]

src/MADSPack.Compression/MADSPack.Compression.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@
3232
</PropertyGroup>
3333
<ItemGroup>
3434
<Reference Include="System" />
35-
<Reference Include="System.Core" />
3635
<Reference Include="System.Drawing" />
37-
<Reference Include="System.Xml.Linq" />
38-
<Reference Include="System.Data.DataSetExtensions" />
39-
<Reference Include="Microsoft.CSharp" />
4036
<Reference Include="System.Data" />
4137
<Reference Include="System.Xml" />
4238
</ItemGroup>

src/MADSPack.Compression/MadsPackImageSS.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,40 +79,40 @@ private byte[] uncompressRLEimageData(byte[] wholeData, int offset, int length,
7979
spriteEnd = true;
8080
break;
8181
}
82-
if (cmd == 255)
82+
if (cmd == TRANSPARENT_COLOUR_INDEX)
8383
{
8484
while (x2++ < width)
85-
_outputStream.WriteByte((byte)255);
85+
_outputStream.WriteByte(TRANSPARENT_COLOUR_INDEX);
8686
newLine = true;
8787
}
8888
else
8989
if (cmd == 253)
9090
while (x2 < width)
9191
{
9292
cmd = bufStream.ReadByte();
93-
if (cmd == 255)
93+
if (cmd == TRANSPARENT_COLOUR_INDEX)
9494
{
9595
while (x2++ < width)
96-
_outputStream.WriteByte((byte)255);
96+
_outputStream.WriteByte(TRANSPARENT_COLOUR_INDEX);
9797
newLine = true;
9898
break;
9999
}
100100
int v = bufStream.ReadByte();
101101
while (cmd-- > 0)
102102
{
103103
if (x2 < width)
104-
_outputStream.WriteByte(v != 253 ? (byte)v : (byte)255);
104+
_outputStream.WriteByte(v != 253 ? (byte)v : TRANSPARENT_COLOUR_INDEX);
105105
x2++;
106106
}
107107
}
108108
else
109109
while (x2 < width)
110110
{
111111
cmd = bufStream.ReadByte();
112-
if (cmd == 255)
112+
if (cmd == TRANSPARENT_COLOUR_INDEX)
113113
{
114114
while (x2++ < width)
115-
_outputStream.WriteByte((byte)255);
115+
_outputStream.WriteByte(TRANSPARENT_COLOUR_INDEX);
116116
newLine = true;
117117
break;
118118
}
@@ -123,18 +123,18 @@ private byte[] uncompressRLEimageData(byte[] wholeData, int offset, int length,
123123
while (cmd-- > 0)
124124
{
125125
if (x2 < width)
126-
_outputStream.WriteByte(v != 253 ? (byte)v : (byte)255);
126+
_outputStream.WriteByte(v != 253 ? (byte)v : TRANSPARENT_COLOUR_INDEX);
127127
x2++;
128128
}
129129
}
130130
else
131131
{
132-
_outputStream.WriteByte(cmd != 253 ? (byte)cmd : (byte)255);
132+
_outputStream.WriteByte(cmd != 253 ? (byte)cmd : TRANSPARENT_COLOUR_INDEX);
133133
x2++;
134134
}
135135
}
136136
if (!newLine)
137-
while (bufStream.ReadByte() != 255) ;
137+
while (bufStream.ReadByte() != TRANSPARENT_COLOUR_INDEX) ;
138138
}
139139

140140
if (!spriteEnd)
@@ -150,7 +150,7 @@ private byte[] uncompressRLEimageData(byte[] wholeData, int offset, int length,
150150
{
151151
long missingLength = (long)(height * width) - _outputStream.Length;
152152
for (int i = 0; (long)i < missingLength; i++)
153-
_outputStream.WriteByte((byte)255);
153+
_outputStream.WriteByte(TRANSPARENT_COLOUR_INDEX);
154154

155155
_outputStream.Flush();
156156
}
@@ -195,7 +195,7 @@ public Bitmap GetImage(int index)
195195

196196
// @BUGFIX
197197
//int a = idx == 255 || idx == 0 ? 0 : 255;
198-
int a = idx == 255 ? 0 : 255;
198+
int a = idx == TRANSPARENT_COLOUR_INDEX ? 0 : TRANSPARENT_COLOUR_INDEX;
199199

200200
int r = this.getPaletteData()[idx * 3] * 4;
201201
int g = this.getPaletteData()[(idx * 3) + 1] * 4;
@@ -215,8 +215,6 @@ public Bitmap GetImage(int index)
215215
private short[] widthsPadded;
216216
private short pictureCount;
217217
private byte[] spriteImageData;
218-
private const int TRANSPARENT_COLOUR_INDEX = 255;
219-
public string pathtoCol;
220-
218+
private const byte TRANSPARENT_COLOUR_INDEX = 255;
221219
}
222220
}

src/MADSPack.Compression/MadsPackReader.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.Text;
54

65
namespace MADSPack.Compression
76
{
87
public class MadsPackReader
98
{
9+
private static string madsPackString = "MADSPACK";
10+
1011
/* member class not found */
1112

1213

@@ -37,9 +38,9 @@ public MadsPackReader(String filename)
3738

3839
public bool isCompressed(FileStream stream)
3940
{
40-
byte[] tempBuffer = new byte[8];
41+
byte[] tempBuffer = new byte[madsPackString.Length];
4142
stream.Seek(0, SeekOrigin.Begin);
42-
return stream.Read(tempBuffer, 0, tempBuffer.Length) == 8 && (Encoding.ASCII.GetString(tempBuffer).Trim() == "MADSPACK");
43+
return stream.Read(tempBuffer, 0, tempBuffer.Length) == 8 && (Encoding.ASCII.GetString(tempBuffer).Trim() == madsPackString);
4344
}
4445

4546
public int getCount()
@@ -118,8 +119,7 @@ public FileType getType()
118119
private MadsPackEntry[] items;
119120
private FileType type;
120121
private int count;
121-
private static string madsPackString = "MADSPACK";
122-
private FileStream mStream;
122+
private readonly FileStream mStream;
123123
}
124124

125125
public enum FileType

src/MADSPack/MADSPack.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<AssemblyName>MADSPack</AssemblyName>
1212
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<PlatformTarget>AnyCPU</PlatformTarget>

src/MADSPack/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.1.0")]

0 commit comments

Comments
 (0)