#include<windows.h>
#include<vector>
#include<fstream>
#include<string>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using std::system;
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::ifstream;
using std::ofstream;
using std::string;
using std::ios;
bool unshaded = false;
void Help()
{
cout<< "Welcome to WoWtoWC3conv v.1.2\n"
<< "This tool was made by Maldiran, but the core jM2converter was made by Koward. It also uses MdlVis and Mdlx converter.\n\n"
<< "To convert .m2 models to .mdx you should:\n"
<< " 1.Open Command Prompt (by clicking 'Enter' now or by clicking 'Windows'+'R' and typing 'cmd')\n"
<< " 2.Drag my program to the console window, type 'space';\n"
<< " 3.Drag your .m2 model to the console window, type 'space';\n"
<< " 4.Write your new texture name (for example texture1.blp, the model will have basic texture textures/texture1.blp);\n"
<< " 5.Click enter or, if you want to convert more models, type 'space' and repeat steps 3 and 4.\n"
<< " Example command: path/WoWtoWC3converter.exe path/model1.m2 texture1.blp path/model2.m2 texture2.blp\n\n"
<< "If you aren't convering gear, than texture from point 4 will not apply (probably), but you still need to type its name;\n"
<< "If you want your new model/s to be unshaded (whole model), than at the end of line type 'true', example command:\n"
<< " path/WoWtoWC3converter.exe path/model1.m2 texture1.blp path/model2.m2 texture2.blp true\n"
<< "In this case both model1 and model2 will be unshaded.\n\n\n"
<< "To exit help window and open Command Prompt hit 'Enter'\n";
getchar();
}
void DeleteMD21(string inputfile)
{
ifstream save(inputfile, ios::app | ios::binary);
string line;
getline(save, line);
if (line.length() < 9)
{
string tem;
getline(save, tem);
line += tem;
}
if (line[3] == '1')
{
ofstream saves("temp.m2", ios::binary);
if (line[8] == 'D')
saves << 'M';
if (line[8] == '2')
saves << "MD";
if (line[8] == '1')
saves << "MD2";
for (int i = 8; i < line.length(); i++)
saves << line[i];
while (getline(save, line))
{
saves <<endl<< line;
}
saves.close();
save.close();
remove(inputfile.c_str());
rename("temp.m2", inputfile.c_str());
}
else { cout << "Not an MD21" << endl; save.close(); }
}
void Mdlchange(string inputfile, string image)
{
ifstream save(inputfile, ios::app);
ofstream saves("temp.mdl");
string line;
bool istextures = false;
bool willtextures = true;
bool ismaterials = true;
bool willunshaded = unshaded;
while (getline(save, line))
{
if (willtextures) { if (line.substr(0, 8) == "Textures") willtextures = false; istextures = true; }
else
{
if (istextures)
{
if (line == " Bitmap {")
{
getline(save, line);
if (line == " Image \"\",")
{
while (getline(save, line))
{
if (line == " }") { line.erase(); break; }
line.erase();
}
}
else saves << " Bitmap {" << endl;
}
if (line == " Image \"Cape.blp\",") line = " Image \"textures\\" + image + "\",";
if (line[0] == '}') istextures = false;
}
else
{
if (willunshaded)
{
if (line.substr(0, 9) == "Materials") ismaterials = true;
if (ismaterials)
{
if (line.substr(0, 13) == " FilterMode")
{
saves << line << endl;
getline(save, line);
if (line != " Unshaded,")
saves << " Unshaded," << endl;
}
if (line[0] == '}') willunshaded = false;
}
}
}
}
saves << line << endl;
}
saves.close();
save.close();
remove(inputfile.c_str());
rename("temp.mdl", inputfile.c_str());
}
int main(int argc, char* argv[])
{
if (argc == 1)
{
Help();
system("cmd");
return 0;
}
int k = 0;
string unsh = argv[argc - 1];
if (argc == 2)
{
if (unsh == "help")
{
Help();
return 0;
}
}
if (unsh == "true")
{
unshaded = true;
k++;
}
int j = 1;
string path = argv[0];
while (true)
{
if ((path.substr(path.size()-4, path.size())) == ".exe")
break;
path += ' ' + argv[j];
j++;
}
while (true)
{
if ((path[path.size() - 1] == '\\') || (path[path.size() - 1] == '/'))
break;
path = path.substr(0, path.size() - 1);
}
for (int i = j; i < argc - k; i += 2)
{
string temp = argv[i];
string text = argv[i + 1];
cout << text << endl;
cout << temp << endl << " {" << endl;
DeleteMD21(temp);
string command = "java -jar \"" + path + "subapps\\jM2converter.jar\" -in \"" + temp + "\" -out \"" + temp + "\" -bc";
system(command.c_str());
string isdone;
command = "\"\"" + path + "mdlvis\\MdlVis.exe\" \"" + temp + "\"\"";
cout << endl << "Now it is time to canonize model. In opened mdlvis click 'K' and 'Cltr+S' then close mdlvis and type 'done'." << endl;
system(command.c_str());
while (true)
{
cin >> isdone;
if (isdone == "done") break;
}
temp = temp.substr(0, temp.size() - 2);
command = "\"\"" + path + "subapps\\MdlxConverterEdited.exe\" \"" + temp + "mdx\"\"";
system(command.c_str());
remove((temp + "mdx").c_str());
Mdlchange((temp + "mdl"), text);
command = "\"\"" + path + "subapps\\MdlxConverterEdited.exe\" \"" + temp + "mdl\"\"";
system(command.c_str());
remove((temp + "mdl").c_str());
cout << '}' << endl << endl;
}
cout << endl << "Done" << endl;
return 0;
}