Overhaul with modern command lines.

This commit is contained in:
lzwdgc 2018-08-02 00:36:18 +03:00
parent dc81075ee1
commit f900353090
15 changed files with 167 additions and 192 deletions

View file

@ -71,9 +71,6 @@ projects:
dependencies: dependencies:
- common - common
unpaker:
root_dir: src/unpaker
mmm_extractor: mmm_extractor:
root_dir: src/mmm_extractor root_dir: src/mmm_extractor
dependencies: dependencies:
@ -161,6 +158,9 @@ projects:
post_target: | post_target: |
cppan_flex_bison_pair(LALR1_CPP_VARIANT_PARSER script2txt) cppan_flex_bison_pair(LALR1_CPP_VARIANT_PARSER script2txt)
unpaker:
root_dir: src/unpaker

View file

@ -18,13 +18,15 @@
#pragma once #pragma once
#include <bmp.h>
#include <tga.h>
#include <primitives/filesystem.h>
#include <assert.h> #include <assert.h>
#include <deque> #include <deque>
#include <vector> #include <vector>
#include <bmp.h>
#include <tga.h>
template <class T> template <class T>
class mat class mat
{ {
@ -69,13 +71,13 @@ public:
int getBytesLength() const { return size() * sizeof(T); } int getBytesLength() const { return size() * sizeof(T); }
int getPos(const T *const elem) const { return elem - &data[0]; } int getPos(const T *const elem) const { return elem - &data[0]; }
const std::vector<T> &getData() const { return data; } const std::vector<T> &getData() const { return data; }
std::vector<T> &getData() { return data; } std::vector<T> &getData() { return data; }
// left/right // left/right
mat mirror() mat mirror()
{ {
int cols = width; int cols = width;
int rows = height; int rows = height;
mat<uint32_t> m(width, height); mat<uint32_t> m(width, height);
for (int row = 0; row < rows; row++) for (int row = 0; row < rows; row++)
{ {
@ -87,13 +89,13 @@ public:
} }
} }
return m; return m;
} }
// up/down // up/down
mat flip() mat flip()
{ {
int cols = width; int cols = width;
int rows = height; int rows = height;
mat<uint32_t> m(width, height); mat<uint32_t> m(width, height);
for (int row = 0; row < rows; row++) for (int row = 0; row < rows; row++)
{ {
@ -111,57 +113,57 @@ private:
std::vector<T> data; std::vector<T> data;
int width; int width;
int height; int height;
}; };
inline void write_mat_bmp(const std::string &filename, int width, int height, int bits, const uint8_t *b, size_t s) inline void write_mat_bmp(const path &filename, int width, int height, int bits, const uint8_t *b, size_t s)
{ {
FILE *f = fopen(filename.c_str(), "wb"); auto f = primitives::filesystem::fopen(filename, "wb");
if (f == nullptr) if (f == nullptr)
return; return;
bmp_header h = { 0 }; bmp_header h = { 0 };
h.bfType = 0x4D42; h.bfType = 0x4D42;
h.bfSize = sizeof(bmp_header) + sizeof(bmp_info_header) + s; h.bfSize = sizeof(bmp_header) + sizeof(bmp_info_header) + s;
h.bfOffBits = sizeof(bmp_header) + sizeof(bmp_info_header); h.bfOffBits = sizeof(bmp_header) + sizeof(bmp_info_header);
bmp_info_header i = { 0 }; bmp_info_header i = { 0 };
i.biSize = sizeof(i); i.biSize = sizeof(i);
i.biWidth = width; i.biWidth = width;
i.biHeight = height; i.biHeight = height;
i.biPlanes = 1; i.biPlanes = 1;
i.biBitCount = bits; i.biBitCount = bits;
i.biCompression = 0; i.biCompression = 0;
i.biSizeImage = 0; i.biSizeImage = 0;
i.biXPelsPerMeter = 0; i.biXPelsPerMeter = 0;
i.biYPelsPerMeter = 0; i.biYPelsPerMeter = 0;
i.biClrUsed = 0; i.biClrUsed = 0;
i.biClrImportant = 0; i.biClrImportant = 0;
fwrite(&h, sizeof(bmp_header), 1, f); fwrite(&h, sizeof(bmp_header), 1, f);
fwrite(&i, sizeof(bmp_info_header), 1, f); fwrite(&i, sizeof(bmp_info_header), 1, f);
fwrite(b, s, 1, f); fwrite(b, s, 1, f);
fclose(f); fclose(f);
} }
template<class T> template<class T>
inline void write_mat_bmp(const std::string &filename, const mat<T> &m) void write_mat_bmp(const path &filename, const mat<T> &m)
{
write_mat_bmp(filename, m.getWidth(), m.getHeight(), sizeof(T) * CHAR_BIT, (const uint8_t *)&m(0, 0), m.size() * sizeof(T));
}
template<class T>
void write_mat_tga(const path &filename, const mat<T> &m)
{ {
write_mat_bmp(filename, m.getWidth(), m.getHeight(), sizeof(T) * CHAR_BIT, (const uint8_t *)&m(0, 0), m.size() * sizeof(T)); auto f = primitives::filesystem::fopen(filename, "wb");
} if (f == nullptr)
return;
template<class T> tga t;
inline void write_mat_tga(const std::string &filename, const mat<T> &m) t.width = m.getWidth();
{ t.height = m.getHeight();
FILE *f = fopen(filename.c_str(), "wb");
if (f == nullptr) // header
return; fwrite(&t, sizeof(tga), 1, f);
tga t;
t.width = m.getWidth();
t.height = m.getHeight();
// header
fwrite(&t, sizeof(tga), 1, f);
fwrite(t.label(), t.idlength, 1, f); fwrite(t.label(), t.idlength, 1, f);
// data // data
fwrite(&m(0, 0), m.size() * sizeof(T), 1, f); fwrite(&m(0, 0), m.size() * sizeof(T), 1, f);
fclose(f); fclose(f);
} }

View file

@ -25,6 +25,7 @@
#include <primitives/filesystem.h> #include <primitives/filesystem.h>
#include <primitives/executor.h> #include <primitives/executor.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -285,21 +286,19 @@ void process_lang(polygon4::Storage &s, const path &p, polygon4::String polygon4
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 3) cl::opt<path> db_fn(cl::Positional, cl::desc("<db file>"), cl::Required);
{ cl::opt<path> dir_to_lang_dbs(cl::Positional, cl::desc("<dir to lang dbs>"), cl::Required);
std::cout << "Usage: prog db.sqlite dir_to_lang_dbs" << "\n";
return 1;
}
path d = argv[2];
auto storage = polygon4::initStorage(argv[1]); cl::ParseCommandLineOptions(argc, argv);
auto storage = polygon4::initStorage(db_fn);
storage->load(); storage->load();
kv_resolved = get_kv_resolved(d, *storage.get()); kv_resolved = get_kv_resolved(dir_to_lang_dbs, *storage.get());
// to check correctness // to check correctness
process_lang(*storage.get(), d / "ru", &polygon4::LocalizedString::ru); process_lang(*storage.get(), dir_to_lang_dbs / "ru", &polygon4::LocalizedString::ru);
for (auto &f : fs::directory_iterator(d)) for (auto &f : fs::directory_iterator(dir_to_lang_dbs))
{ {
if (!fs::is_directory(f)) if (!fs::is_directory(f))
continue; continue;

View file

@ -22,12 +22,13 @@
#include <common.h> #include <common.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <fstream> #include <fstream>
void create_sql(std::string path, const db &db) void create_sql(path p, const db &db)
{ {
std::ofstream ofile(path + ".sql"); std::ofstream ofile(p += ".sql");
if (!ofile) if (!ofile)
return; return;
@ -125,14 +126,12 @@ void create_sql(std::string path, const db &db)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) cl::opt<path> db_fn(cl::Positional, cl::desc("<db file>"), cl::Required);
{
std::cout << "Usage:\n" << argv[0] << " path/to/aim_game/data/db" << "\n" << argv[0] << " path/to/aim_game/data/quest" << "\n"; cl::ParseCommandLineOptions(argc, argv);
return 1;
}
path p = argv[1];
db db; db db;
db.open(p); db.open(db_fn);
create_sql(p.string(), db); create_sql(db_fn, db);
return 0; return 0;
} }

View file

@ -26,6 +26,7 @@
#include <primitives/filesystem.h> #include <primitives/filesystem.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
using namespace std; using namespace std;
@ -66,12 +67,10 @@ void process_mmm(const path &fn)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) cl::opt<path> p(cl::Positional, cl::desc("<file.mmm or directory>"), cl::Required);
{
cout << "Usage:\n" << argv[0] << " {file.mmm,dir}" << "\n"; cl::ParseCommandLineOptions(argc, argv);
return 1;
}
path p = argv[1];
if (fs::is_regular_file(p)) if (fs::is_regular_file(p))
process_mmm(p); process_mmm(p);
else if (fs::is_directory(p)) else if (fs::is_directory(p))

View file

@ -117,14 +117,14 @@ void mmp::load(const buffer &b)
} }
} }
void mmp::load(const std::string &fn) void mmp::load(const path &fn)
{ {
filename = fn; filename = fn;
buffer b(read_file(filename)); buffer b(read_file(filename));
load(b); load(b);
} }
void mmp::loadTextureNames(const std::string &fn) void mmp::loadTextureNames(const path &fn)
{ {
std::ifstream ifile(fn); std::ifstream ifile(fn);
while (ifile) while (ifile)
@ -254,7 +254,8 @@ void mmp::process()
void mmp::writeFileInfo() void mmp::writeFileInfo()
{ {
std::ofstream ofile(filename + ".info.txt"); auto fn = filename;
std::ofstream ofile(fn += ".info.txt");
if (!ofile) if (!ofile)
return; return;
ofile << "width: " << h.width << "\n"; ofile << "width: " << h.width << "\n";
@ -270,7 +271,8 @@ void mmp::writeFileInfo()
void mmp::writeTexturesList() void mmp::writeTexturesList()
{ {
std::ofstream ofile(filename + ".textures.txt"); auto fn = filename;
std::ofstream ofile(fn += ".textures.txt");
if (!ofile) if (!ofile)
return; return;
for (auto &t : textures) for (auto &t : textures)
@ -291,8 +293,9 @@ void mmp::writeTexturesList()
void mmp::writeHeightMap() void mmp::writeHeightMap()
{ {
auto fn = filename + ".heightmap16.r16"; auto fn = filename;
FILE *f = fopen(fn.c_str(), "wb"); fn += ".heightmap16.r16";
auto f = primitives::filesystem::fopen(fn, "wb");
if (f == nullptr) if (f == nullptr)
return; return;
fwrite(&heightmap(0, 0), heightmap.size() * sizeof(decltype(heightmap)::type), 1, f); fwrite(&heightmap(0, 0), heightmap.size() * sizeof(decltype(heightmap)::type), 1, f);
@ -311,7 +314,8 @@ void mmp::writeHeightMapSegmented()
void mmp::writeTextureMap() void mmp::writeTextureMap()
{ {
auto fn = filename + ".texmap.bmp"; auto fn = filename;
fn += ".texmap.bmp";
write_mat_bmp(fn, texmap); write_mat_bmp(fn, texmap);
} }
@ -328,31 +332,36 @@ void mmp::writeTextureAlphaMaps()
break; break;
} }
} }
auto fn = filename + ".texmap." + std::to_string(t.first) + "." + std::to_string(tex_id) + ".bmp"; auto fn = filename;
fn += ".texmap." + std::to_string(t.first) + "." + std::to_string(tex_id) + ".bmp";
write_mat_bmp(fn, t.second); write_mat_bmp(fn, t.second);
} }
} }
void mmp::writeTextureMapColored() void mmp::writeTextureMapColored()
{ {
auto fn = filename + ".texmap.colored.bmp"; auto fn = filename;
fn += ".texmap.colored.bmp";
write_mat_bmp(fn, texmap_colored); write_mat_bmp(fn, texmap_colored);
} }
void mmp::writeColorMap() void mmp::writeColorMap()
{ {
auto fn = filename + ".colormap.bmp"; auto fn = filename;
fn += ".colormap.bmp";
write_mat_bmp(fn, colormap); write_mat_bmp(fn, colormap);
} }
void mmp::writeShadowMap() void mmp::writeShadowMap()
{ {
auto fn = filename + ".shadowmap.bmp"; auto fn = filename;
fn += ".shadowmap.bmp";
write_mat_bmp(fn, shadowmap); write_mat_bmp(fn, shadowmap);
} }
void mmp::writeNormalMap() void mmp::writeNormalMap()
{ {
auto fn = filename + ".normalmap.bmp"; auto fn = filename;
fn += ".normalmap.bmp";
write_mat_bmp(fn, normalmap); write_mat_bmp(fn, normalmap);
} }

View file

@ -16,17 +16,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <buffer.h>
#include <color.h>
#include <mat.h>
#include <types.h>
#include <primitives/filesystem.h>
#include <map> #include <map>
#include <set> #include <set>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <buffer.h>
#include <color.h>
#include <mat.h>
#include <types.h>
using Height = float; using Height = float;
enum class HeaderSegmentType : uint32_t enum class HeaderSegmentType : uint32_t
@ -165,7 +167,7 @@ struct mmp
std::vector<segment> segments; std::vector<segment> segments;
// //
std::string filename; path filename;
int xsegs; int xsegs;
int ysegs; int ysegs;
std::map<int, int /* count */> textures; std::map<int, int /* count */> textures;
@ -186,8 +188,8 @@ struct mmp
mat<uint32_t> normalmap; mat<uint32_t> normalmap;
void load(const buffer &b); void load(const buffer &b);
void load(const std::string &filename); void load(const path &filename);
void loadTextureNames(const std::string &filename); void loadTextureNames(const path &filename);
void process(); void process();

View file

@ -20,6 +20,7 @@
#include <primitives/filesystem.h> #include <primitives/filesystem.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <iostream> #include <iostream>
#include <set> #include <set>
@ -31,18 +32,17 @@ using namespace std;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc < 2 || argc > 3) cl::opt<path> p(cl::Positional, cl::desc("<file.mmp or directory>"), cl::Required);
{ cl::opt<path> texture_ids(cl::Positional, cl::desc("<path to texture_ids.txt>"));
cout << "Usage:\n" << argv[0] << " {file.mmp,mmp_dir} [texture_ids.txt]" << "\n";
return 1;
}
auto func = [&argc, &argv](auto &p) cl::ParseCommandLineOptions(argc, argv);
auto func = [&texture_ids](auto &p)
{ {
mmp m; mmp m;
if (argc > 2) if (!texture_ids.empty())
m.loadTextureNames(argv[2]); m.loadTextureNames(texture_ids);
m.load(p.string()); m.load(p);
m.process(); m.process();
m.writeFileInfo(); m.writeFileInfo();
m.writeTexturesList(); m.writeTexturesList();
@ -56,7 +56,6 @@ int main(int argc, char *argv[])
m.writeNormalMap(); m.writeNormalMap();
}; };
path p = argv[1];
if (fs::is_regular_file(p)) if (fs::is_regular_file(p))
func(p); func(p);
else if (fs::is_directory(p)) else if (fs::is_directory(p))

View file

@ -21,6 +21,7 @@
#include <primitives/filesystem.h> #include <primitives/filesystem.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -33,12 +34,9 @@
using namespace std; using namespace std;
// options // options
bool all_formats = false; cl::opt<bool> all_formats("a", cl::desc("all formats"));
bool silent = false; cl::opt<bool> silent("s", cl::desc("silent"));
bool printMaxPolygonBlock = false; cl::opt<bool> printMaxPolygonBlock("m", cl::desc("print max polygon block"));
path p;
bool parse_cmd(int argc, char *argv[]);
void convert_model(const path &fn) void convert_model(const path &fn)
{ {
@ -56,11 +54,10 @@ void convert_model(const path &fn)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc < 2 || !parse_cmd(argc, argv)) cl::opt<path> p(cl::Positional, cl::desc("<model file or dir>"), cl::Required);
{
printf("Usage: %s [OPTIONS] {model_file,model_dir}\n", argv[0]); cl::ParseCommandLineOptions(argc, argv);
return 1;
}
if (fs::is_regular_file(p)) if (fs::is_regular_file(p))
convert_model(p); convert_model(p);
else if (fs::is_directory(p)) else if (fs::is_directory(p))
@ -78,31 +75,3 @@ int main(int argc, char *argv[])
throw std::runtime_error("Bad fs object"); throw std::runtime_error("Bad fs object");
return 0; return 0;
} }
bool parse_cmd(int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
{
auto arg = argv[i];
if (*arg != '-')
{
if (i != argc - 1)
return false;
p = arg;
continue;
}
switch (arg[1])
{
case 'a':
all_formats = true;
break;
case 's':
silent = true;
break;
case 'm':
printMaxPolygonBlock = true;
break;
}
}
return true;
}

View file

@ -219,7 +219,7 @@ void mpj::load(const buffer &b)
h.load(b); h.load(b);
} }
void mpj::load(const std::string &fn) void mpj::load(const path &fn)
{ {
filename = fn; filename = fn;
buffer b(read_file(filename)); buffer b(read_file(filename));

View file

@ -16,16 +16,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <buffer.h>
#include <objects.h>
#include <types.h>
#include <primitives/filesystem.h>
#include <map> #include <map>
#include <set> #include <set>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <buffer.h>
#include <objects.h>
#include <types.h>
enum class SegmentType : uint32_t enum class SegmentType : uint32_t
{ {
none = 0, none = 0,
@ -204,8 +206,8 @@ struct mpj
header h; header h;
// //
std::string filename; path filename;
void load(const buffer &b); void load(const buffer &b);
void load(const std::string &filename); void load(const path &filename);
}; };

View file

@ -19,6 +19,7 @@
#include "mpj.h" #include "mpj.h"
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <iostream> #include <iostream>
#include <set> #include <set>
@ -30,12 +31,11 @@ using namespace std;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) cl::opt<path> p(cl::Positional, cl::desc("<file.mpj>"), cl::Required);
{
cout << "Usage:\n" << argv[0] << " file.mpj" << "\n"; cl::ParseCommandLineOptions(argc, argv);
return 1;
}
mpj m; mpj m;
m.load(argv[1]); m.load(p);
return 0; return 0;
} }

View file

@ -20,17 +20,16 @@
#include <primitives/filesystem.h> #include <primitives/filesystem.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) cl::opt<path> p(cl::Positional, cl::desc("<file.sav or saves dir>"), cl::Required);
{
printf("Usage: %s {file.sav,saves_dir}\n", argv[0]); cl::ParseCommandLineOptions(argc, argv);
return 1;
}
//save_changes.mech_org = "ORG_PLAYER"; //save_changes.mech_org = "ORG_PLAYER";
save_changes.money = 999999999.0f; save_changes.money = 999999999.0f;
@ -47,7 +46,6 @@ int main(int argc, char *argv[])
writeFile(p.string(), save_changes.out.buf()); writeFile(p.string(), save_changes.out.buf());
}; };
path p = argv[1];
if (fs::is_regular_file(p)) if (fs::is_regular_file(p))
func(p); func(p);
else if (fs::is_directory(p)) else if (fs::is_directory(p))

View file

@ -22,6 +22,7 @@
#include <primitives/filesystem.h> #include <primitives/filesystem.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -32,11 +33,9 @@ using std::string;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) cl::opt<path> p(cl::Positional, cl::desc("<script.scr or scripts dir>"), cl::Required);
{
cout << "Usage:\n" << argv[0] << " {script.scr,scr_dir}"; cl::ParseCommandLineOptions(argc, argv);
return 1;
}
auto func = [](auto filename) auto func = [](auto filename)
{ {
@ -76,7 +75,6 @@ int main(int argc, char *argv[])
} }
}; };
path p = argv[1];
if (fs::is_regular_file(p)) if (fs::is_regular_file(p))
func(p.string()); func(p.string());
else if (fs::is_directory(p)) else if (fs::is_directory(p))

View file

@ -22,6 +22,7 @@
#include <primitives/filesystem.h> #include <primitives/filesystem.h>
#include <primitives/sw/main.h> #include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -82,12 +83,10 @@ void convert(const path &fn)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) cl::opt<path> p(cl::Positional, cl::desc("<file.tm>"), cl::Required);
{
printf("Usage: %s file.tm\n", argv[0]); cl::ParseCommandLineOptions(argc, argv);
return 1;
}
path p = argv[1];
if (fs::is_regular_file(p)) if (fs::is_regular_file(p))
convert(p); convert(p);
else if (fs::is_directory(p)) else if (fs::is_directory(p))