forked from slavidodo/opentibiaunity-datspr-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
148 lines (123 loc) · 5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Google.Protobuf;
using Google.Protobuf.Collections;
using Newtonsoft.Json.Linq;
using OpenTibiaUnity.Core.Metaflags;
using OpenTibiaUnity.Protobuf.Appearances;
namespace OpenTibiaUnity
{
// This is taken from the the OpenOpenTibiaUnity project
interface ITokenItem
{
JObject GetJObject();
}
struct AppearancesToken : ITokenItem
{
public string file;
public AppearancesToken(string file) {
this.file = file;
}
public JObject GetJObject() {
JObject obj = new JObject();
obj["type"] = "appearances";
obj["file"] = file;
return obj;
}
}
struct SpritesToken : ITokenItem
{
public string file;
public int spritetype;
public int firstspriteid;
public int lastspriteid;
public JObject GetJObject() {
JObject obj = new JObject();
obj["type"] = "sprite";
obj["file"] = file;
obj["spritetype"] = spritetype;
obj["firstspriteid"] = firstspriteid;
obj["lastspriteid"] = lastspriteid;
return obj;
}
}
static class Program
{
public const int SEGMENT_DIMENTION = 512;
public const int BITMAP_SIZE = SEGMENT_DIMENTION * SEGMENT_DIMENTION;
static void ConvertClientVersion(int fromVersion, int toVersion, bool useAlpha) {
string datFile = fromVersion.ToString() + "/Tibia.dat";
string sprFile = fromVersion.ToString() + "/Tibia.spr";
if (!File.Exists(datFile) || !File.Exists(sprFile)) {
Console.WriteLine("Tibia.dat or Tibia.spr doesn't exist");
Environment.Exit(0);
return;
}
Directory.CreateDirectory(toVersion.ToString());
string newDatFile = toVersion.ToString() + "/Tibia.dat";
string newSprFile = toVersion.ToString() + "/Tibia.spr";
var datParser = new Core.Assets.ContentData(File.ReadAllBytes(datFile), fromVersion);
byte[] result = datParser.ConvertTo(toVersion);
File.WriteAllBytes(newDatFile, result);
var sprParser = new Core.Assets.ContentSprites(File.ReadAllBytes(sprFile), fromVersion, useAlpha);
result = sprParser.ConvertTo(toVersion);
File.WriteAllBytes(newSprFile, result);
Console.WriteLine("Convertion Successfull to " + toVersion + ".");
}
static void Main(string[] args) {
int clientVersion = -1;
int buildVersion = -1;
int convertTo = -1;
bool useAlpha = false;
foreach (var arg in args) {
if (arg.StartsWith("--version=")) {
int.TryParse(arg.Substring(10), out clientVersion);
} else if (arg.StartsWith("--build-version=")) {
int.TryParse(arg.Substring(16), out buildVersion);
} else if (arg.StartsWith("--alpha=")) {
var boolstr = arg.Substring(8).ToLower();
useAlpha = boolstr == "y" || boolstr == "yes" || boolstr == "true" || boolstr == "1";
} else if (arg.StartsWith("--convert-to=")) {
convertTo = int.Parse(arg.Substring(13));
} else {
Console.WriteLine("Unknown Attribute: " + arg);
return;
}
}
if (clientVersion == -1) {
Console.WriteLine("Invalid client version.");
return;
}
if (clientVersion >= 1100 && buildVersion == -1) {
Console.WriteLine("Invalid build version.");
return;
}
if (clientVersion >= 1100)
Console.WriteLine("Loading version: {0}.{1}", clientVersion, buildVersion);
else
Console.WriteLine("Loading version: {0}", clientVersion);
Stopwatch watch = new Stopwatch();
watch.Start();
if (convertTo != -1) {
Console.WriteLine("Converting to: " + convertTo);
ConvertClientVersion(clientVersion, convertTo, useAlpha);
} else {
Core.Converter.IConverter converter = null;
if (clientVersion < 1100)
converter = new Core.Converter.LegacyConverter(clientVersion, useAlpha);
else
converter = new Core.Converter.ProtobufConverter(clientVersion, buildVersion);
var task = converter.BeginProcessing();
task.Wait();
}
watch.Stop();
double seconds = watch.ElapsedMilliseconds / (double)1000;
Console.WriteLine("Time elapsed: " + seconds + " seconds.");
}
}
}