Initial script2txt v2.

This commit is contained in:
lzwdgc 2024-02-01 00:58:57 +03:00
parent 3e2c9ba25f
commit 31123f11fa
5 changed files with 120 additions and 20 deletions

22
src/common/mmap.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <primitives/filesystem.h>
#include <primitives/templates2/mmap2.h>
struct stream {
primitives::templates2::mmap_file<uint8_t> &m;
uint8_t *p{m.p};
template <typename T>
operator T &() {
auto &r = *(T *)p;
p += sizeof(T);
return r;
}
template <typename T>
auto span(size_t len) {
auto s = std::span<T>((T *)p, len);
p += sizeof(T) * len;
return s;
}
};

View file

@ -45,7 +45,7 @@ inline bool replace_all(std::string &str, const std::string &from, const std::st
struct script
{
uint32_t file_size;
uint32_t unk0;
uint32_t unk0; // stack size? always 16000? section bits? magic? max size?
uint32_t raw_text_size;
uint32_t unk1;
std::vector<uint8_t> raw_text;

View file

@ -0,0 +1,92 @@
/*
* AIM script2txt2 (simpler version)
* Copyright (C) 2024 lzwdgc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mmap.h>
#include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <primitives/sw/cl.h>
#pragma pack(push, 1)
struct script {
uint32_t file_size;
uint32_t unk0; // stack size? always 16000? // section bits?
uint32_t raw_text_size;
uint32_t nlines;
};
#pragma pack(pop)
int main(int argc, char *argv[]) {
cl::opt<path> p(cl::Positional, cl::desc("<script.scr or scripts dir>"), cl::Required);
cl::ParseCommandLineOptions(argc, argv);
auto func = [](auto &&fn) {
primitives::templates2::mmap_file<uint8_t> f{fn};
stream s{f};
script scr = s;
auto text = s.span<uint8_t>(scr.raw_text_size);
uint32_t max_nlines = s;
auto rest = s.span<uint32_t>(max_nlines);
std::vector<std::string_view> lines;
lines.reserve(scr.nlines);
for (int i = 0; i < scr.nlines; ++i) {
lines.emplace_back((const char *)f.p + sizeof(script) + rest[i]);
}
int a = 5;
a++;
// write script
/*{
filename += ".txt";
std::ofstream ofile(filename);
if (ofile)
ofile << ctx.getText();
}
// write function calls
{
std::ofstream functions("functions.txt", std::ios::app);
if (functions)
{
for (auto &f : driver.functions)
{
std::string f2(f.size(), 0);
std::transform(f.begin(), f.end(), f2.begin(), tolower);
functions << f2 << "\n";
}
}
}*/
};
if (fs::is_regular_file(p)) {
func(p.string());
} else if (fs::is_directory(p)) {
auto files = enumerate_files_like(p, ".*\\.scr", false);
auto files2 = enumerate_files_like(p, ".*\\.QST", false);
files.insert(files2.begin(), files2.end());
for (auto &f : files) {
std::cout << "processing: " << f << "\n";
func(f.string());
}
} else {
throw std::runtime_error("Bad fs object");
}
return 0;
}

View file

@ -17,12 +17,12 @@
*/
#include <buffer.h>
#include <mmap.h>
#include <primitives/filesystem.h>
#include <primitives/sw/main.h>
#include <primitives/sw/settings.h>
#include <primitives/sw/cl.h>
#include <primitives/templates2/mmap2.h>
#include <algorithm>
#include <fstream>
@ -56,31 +56,16 @@ struct segment {
none = 0x0,
lzo = 0x1,
lzma = 0x2,
rlew = 0x4,
rlew = 0x4, // https://moddingwiki.shikadi.net/wiki/Id_Software_RLEW_compression
};
uint32_t unk1; // some file offset? trash?
// some file offset? trash? crc? m1 has zlib crc table (png)?
uint32_t unk1;
decode_algorithm algorithm;
uint32_t offset;
};
#pragma pack(pop)
struct stream {
primitives::templates2::mmap_file<uint8_t> &m;
uint8_t *p{m.p};
template <typename T> operator T&() {
auto &r = *(T*)p;
p += sizeof(T);
return r;
}
template <typename T> auto span(size_t len) {
auto s = std::span<T>((T*)p, len);
p += sizeof(T) * len;
return s;
}
};
void unpack_file(path fn) {
primitives::templates2::mmap_file<uint8_t> f{fn};
stream s{f};

1
sw.cpp
View file

@ -48,6 +48,7 @@ void build(Solution &s)
add_exe_with_data_manager("mmo_extractor");
add_exe_with_common("mmp_extractor") += "org.sw.demo.intel.opencv.highgui"_dep;
add_exe_with_common("mpj_loader");
add_exe_with_common("script2txt2");
add_exe_with_common("tm_converter");
add_exe("name_generator");
add_exe_with_common("save_loader");