2017-10-02 21:06:50 +00:00
|
|
|
#include "MiniVMac.h"
|
|
|
|
#include "Launcher.h"
|
2017-10-09 13:45:36 +00:00
|
|
|
#include "Utilities.h"
|
2019-01-19 10:46:42 +00:00
|
|
|
#include "BinaryIO.h"
|
2019-02-07 21:24:59 +00:00
|
|
|
#include "ResourceFile.h"
|
2017-10-02 21:06:50 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "hfs.h"
|
2022-09-21 04:51:10 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#define HAVE_MKTIME
|
|
|
|
#ifdef HAVE_MKTIME
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include <iostream>
|
2017-10-03 09:57:56 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2017-10-02 21:06:50 +00:00
|
|
|
|
2017-10-03 20:26:24 +00:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define ResType MacResType
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 21:06:50 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2019-02-10 01:56:37 +00:00
|
|
|
|
|
|
|
/* Adapted from http://sebastien.kirche.free.fr/python_stuff/MacOS-aliases.txt */
|
|
|
|
typedef struct
|
|
|
|
{
|
2022-09-21 04:51:10 +00:00
|
|
|
uint16_t type; /* type of data */
|
|
|
|
uint16_t size; /* length of variable-length data */
|
2019-02-10 01:56:37 +00:00
|
|
|
char data[]; /* actual data */
|
|
|
|
} VarData;
|
|
|
|
|
2022-09-21 04:51:10 +00:00
|
|
|
#pragma pack(push,1)
|
2019-02-10 01:56:37 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* Type Code: alis */
|
|
|
|
char userType[4] = {0, 0, 0, 0}; /* for application use, can be zeros */
|
2022-09-21 04:51:10 +00:00
|
|
|
uint16_t size; /* alias record size, including variable-length data */
|
|
|
|
int16_t version = htons(2); /* alias version, current 2 */
|
|
|
|
int16_t type = htons(0); /* file = 0, directory = 1 */
|
|
|
|
char volumeNameSize; /* length of volume name */
|
|
|
|
char volumeName[27]; /* volume name padded with null bytes */
|
2019-02-10 01:56:37 +00:00
|
|
|
uint32_t volumeCreationDate; /* volume creation date, seconds since 1904 */
|
2022-09-21 04:51:10 +00:00
|
|
|
uint16_t volumeSig = htons(0x4244); /* volume signature MFS = RW, HFS = BD */
|
|
|
|
int16_t volumeType = htons(5); /* [HD] = 0, Foreign = 1, Floppy: 400K, 800K, 1400K = 2-4, OtherEjectable = 5 */
|
2019-02-10 01:56:37 +00:00
|
|
|
int32_t parentDirID; /* parent directory ID, 0 for relative searches */
|
2022-09-21 04:51:10 +00:00
|
|
|
char fileNameSize; /* length of file or directory name */
|
|
|
|
char fileName[63]; /* file or directory name padded with null bytes */
|
2019-02-10 01:56:37 +00:00
|
|
|
int32_t fileNum; /* file number or directory ID */
|
|
|
|
uint32_t fileCreationDate; /* file or directory creation date, seconds since 1904 */
|
|
|
|
char typeCode[4]; /* file type */
|
|
|
|
char creatorCode[4]; /* file's creator */
|
2022-09-21 04:51:10 +00:00
|
|
|
int16_t nlvlFrom = htons(0); /* next level up from alias, used in relative searches */
|
|
|
|
int16_t nlvlTo = htons(0); /* next level down to target, ditto */
|
|
|
|
uint32_t volumeAttr = htonl(0); /* various flags (locked, ejectable), see link above */
|
|
|
|
int16_t volumeFSID = htons(0); /* file system ID for the volume, 0 for MFS, HFS */
|
|
|
|
int16_t unused = htons(0);
|
|
|
|
uint32_t unused1 = htonl(0);
|
|
|
|
uint32_t unused2 = htonl(0);
|
|
|
|
VarData vdata[1] = { /* variable-length data, see link above */
|
|
|
|
{htons(0xFFFF), htons(0)} /* end of variable-length data marker, length 0 */
|
|
|
|
};
|
2019-02-10 01:56:37 +00:00
|
|
|
} AliasData;
|
2022-09-21 04:51:10 +00:00
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
|
|
|
/* Begin code copied from libhfs/data.c */
|
|
|
|
|
|
|
|
# define TIMEDIFF 2082844800UL
|
|
|
|
|
|
|
|
static
|
|
|
|
time_t tzdiff = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NAME: calctzdiff()
|
|
|
|
* DESCRIPTION: calculate the timezone difference between local time and UTC
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
void calctzdiff(void)
|
|
|
|
{
|
|
|
|
# ifdef HAVE_MKTIME
|
|
|
|
|
|
|
|
time_t t;
|
|
|
|
int isdst;
|
|
|
|
struct tm tm;
|
|
|
|
const struct tm *tmp;
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
isdst = localtime(&t)->tm_isdst;
|
|
|
|
|
|
|
|
tmp = gmtime(&t);
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
tm = *tmp;
|
|
|
|
tm.tm_isdst = isdst;
|
|
|
|
|
|
|
|
tzdiff = t - mktime(&tm);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tzdiff = 0;
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
|
|
tzdiff = 0;
|
|
|
|
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NAME: data->mtime()
|
|
|
|
* DESCRIPTION: convert local time to MacOS time
|
|
|
|
*/
|
|
|
|
unsigned long d_mtime(time_t ltime)
|
|
|
|
{
|
|
|
|
if (tzdiff == -1)
|
|
|
|
calctzdiff();
|
|
|
|
|
|
|
|
return (unsigned long) (ltime + tzdiff) + TIMEDIFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End code copied from libhfs/data.c */
|
2019-02-10 01:56:37 +00:00
|
|
|
|
|
|
|
|
2017-10-02 21:06:50 +00:00
|
|
|
class MiniVMacLauncher : public Launcher
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
fs::path imagePath;
|
|
|
|
fs::path systemImage;
|
|
|
|
fs::path vmacDir;
|
|
|
|
fs::path vmacPath;
|
2022-07-31 15:44:15 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
bool vmacIsAppBundle;
|
|
|
|
#endif
|
2017-10-02 21:06:50 +00:00
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
hfsvol *sysvol;
|
|
|
|
hfsvol *vol;
|
2017-10-02 21:06:50 +00:00
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
void CopySystemFile(const std::string& fn, bool required);
|
2019-02-10 01:56:37 +00:00
|
|
|
uint16_t GetSystemVersion(const std::string& systemFileName);
|
2019-02-07 21:24:59 +00:00
|
|
|
void MakeAlias(const std::string& dest, const std::string& src);
|
2019-01-19 10:46:42 +00:00
|
|
|
fs::path ConvertImage(const fs::path& path);
|
2017-10-02 21:06:50 +00:00
|
|
|
public:
|
2019-01-04 02:35:32 +00:00
|
|
|
MiniVMacLauncher(po::variables_map& options);
|
|
|
|
virtual ~MiniVMacLauncher();
|
2017-10-02 21:06:50 +00:00
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
virtual bool Go(int timeout = 0);
|
|
|
|
virtual void DumpOutput();
|
2017-10-02 21:06:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-10-03 20:26:24 +00:00
|
|
|
/*
|
|
|
|
* Recursive directory copy from https://stackoverflow.com/a/39146566
|
|
|
|
*/
|
2019-01-04 02:35:32 +00:00
|
|
|
#ifdef __APPLE__
|
2017-10-03 20:26:24 +00:00
|
|
|
static void copyDirectoryRecursively(const fs::path& sourceDir, const fs::path& destinationDir)
|
|
|
|
{
|
|
|
|
if (!fs::exists(sourceDir) || !fs::is_directory(sourceDir))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Source directory " + sourceDir.string() + " does not exist or is not a directory");
|
|
|
|
}
|
|
|
|
if (fs::exists(destinationDir))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Destination directory " + destinationDir.string() + " already exists");
|
|
|
|
}
|
|
|
|
if (!fs::create_directory(destinationDir))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Cannot create destination directory " + destinationDir.string());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& dirEnt : fs::recursive_directory_iterator{sourceDir})
|
|
|
|
{
|
2019-01-19 10:46:42 +00:00
|
|
|
auto path = dirEnt.path();
|
2019-01-04 02:35:32 +00:00
|
|
|
auto relativePathStr = path.string().substr(sourceDir.string().size());
|
2019-01-19 10:46:42 +00:00
|
|
|
auto dst = destinationDir / relativePathStr;
|
|
|
|
|
|
|
|
if(fs::is_directory(path))
|
|
|
|
fs::create_directory(dst);
|
|
|
|
else
|
|
|
|
fs::copy_file(path, dst);
|
2017-10-03 20:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-04 02:35:32 +00:00
|
|
|
#endif
|
2017-10-03 20:26:24 +00:00
|
|
|
|
2019-01-19 10:46:42 +00:00
|
|
|
fs::path MiniVMacLauncher::ConvertImage(const fs::path& path)
|
|
|
|
{
|
|
|
|
fs::ifstream in(path);
|
|
|
|
|
|
|
|
in.seekg(0x40);
|
|
|
|
uint32_t diskCopyLength = longword(in);
|
|
|
|
|
|
|
|
in.seekg(0x52);
|
|
|
|
uint16_t diskCopySig = word(in);
|
|
|
|
|
|
|
|
//in.seekg(1024);
|
|
|
|
//uint16_t rawHFSSig = word(in);
|
|
|
|
|
|
|
|
in.seekg(0x54 + 1024);
|
|
|
|
uint16_t diskCopyHFSSig = word(in);
|
|
|
|
|
|
|
|
in.seekg(0, std::ios::end);
|
|
|
|
uint32_t actualSize = in.tellg();
|
|
|
|
|
|
|
|
if(diskCopySig == 0x0100 && actualSize == diskCopyLength + 0x54
|
|
|
|
&& diskCopyLength % 512 == 0 && diskCopyHFSSig == 0x4244)
|
|
|
|
{
|
|
|
|
auto outPath = tempDir / fs::unique_path();
|
|
|
|
|
|
|
|
fs::ofstream out(outPath);
|
|
|
|
|
|
|
|
in.seekg(0x54);
|
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
while(in.read(buf, sizeof(buf)), in.gcount() > 0)
|
|
|
|
out.write(buf, in.gcount());
|
|
|
|
|
|
|
|
return outPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-02 21:06:50 +00:00
|
|
|
MiniVMacLauncher::MiniVMacLauncher(po::variables_map &options)
|
2017-10-03 18:51:53 +00:00
|
|
|
: Launcher(options),
|
2017-10-02 21:06:50 +00:00
|
|
|
sysvol(NULL), vol(NULL)
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
vmacDir = fs::absolute( options["minivmac-dir"].as<std::string>() );
|
|
|
|
vmacPath = fs::absolute( options["minivmac-path"].as<std::string>(), vmacDir );
|
|
|
|
|
2022-07-31 15:44:15 +00:00
|
|
|
fs::path dataDir;
|
|
|
|
#ifdef __APPLE__
|
|
|
|
vmacIsAppBundle = vmacPath.extension().string() == ".app";
|
|
|
|
if(vmacIsAppBundle)
|
|
|
|
{
|
|
|
|
dataDir = tempDir / "minivmac.app" / "Contents" / "mnvm_dat";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
dataDir = tempDir;
|
|
|
|
}
|
|
|
|
imagePath = dataDir / "disk1.dsk";
|
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
systemImage = fs::absolute(options["system-image"].as<std::string>(), vmacDir);
|
2019-01-19 10:46:42 +00:00
|
|
|
systemImage = ConvertImage(systemImage);
|
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
std::vector<unsigned char> bootblock1(1024);
|
|
|
|
fs::ifstream(systemImage).read((char*) bootblock1.data(), 1024);
|
|
|
|
|
|
|
|
if(bootblock1[0] != 'L' || bootblock1[1] != 'K' || bootblock1[0xA] > 15)
|
|
|
|
throw std::runtime_error("Not a bootable Mac disk image: " + systemImage.string());
|
|
|
|
|
2019-02-10 01:56:37 +00:00
|
|
|
sysvol = hfs_mount(systemImage.string().c_str(),0, HFS_MODE_RDONLY);
|
|
|
|
assert(sysvol);
|
|
|
|
hfsvolent ent;
|
|
|
|
hfs_vstat(sysvol, &ent);
|
|
|
|
hfs_setcwd(sysvol, ent.blessed);
|
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
string systemFileName(bootblock1.begin() + 0xB, bootblock1.begin() + 0xB + bootblock1[0xA]);
|
2019-02-10 01:56:37 +00:00
|
|
|
uint16_t sysver = GetSystemVersion(systemFileName);
|
2019-01-04 02:35:32 +00:00
|
|
|
|
2019-02-10 01:56:37 +00:00
|
|
|
bool usesAutQuit7 = (sysver >= 0x700);
|
2019-02-11 20:20:21 +00:00
|
|
|
std::string optionsKey = usesAutQuit7 ? "autquit7-image" : "autoquit-image";
|
|
|
|
if(options.count(optionsKey) == 0)
|
|
|
|
{
|
|
|
|
std::ostringstream str;
|
|
|
|
str << "'" << optionsKey << "' not configured for Mini vMac and System version " << (sysver >> 8);
|
|
|
|
throw std::runtime_error(str.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::path autoquitImage = fs::absolute(options[optionsKey].as<std::string>(), vmacDir);
|
2019-02-10 01:56:37 +00:00
|
|
|
autoquitImage = ConvertImage(autoquitImage);
|
2019-01-04 02:35:32 +00:00
|
|
|
|
2022-07-31 15:44:15 +00:00
|
|
|
/*
|
|
|
|
Copy over the entire Mini vMac program.
|
|
|
|
Mini vMac looks for ROM (vMac.ROM) and disk images (disk1.dsk)
|
|
|
|
in the directory next to its binary or in the case of the Mac
|
|
|
|
version in the mnvm_dat directory in the Contents directory in
|
|
|
|
the app bundle.
|
|
|
|
The Mac version also ignores command line arguments.
|
|
|
|
Having our own copy in our temp directory is just simpler.
|
|
|
|
It is five times smaller than System 6, so this really does not
|
|
|
|
matter.
|
|
|
|
*/
|
|
|
|
fs::path vmacCopy;
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if(vmacIsAppBundle)
|
|
|
|
{
|
|
|
|
vmacCopy = tempDir / "minivmac.app";
|
|
|
|
copyDirectoryRecursively(vmacPath, vmacCopy);
|
|
|
|
vmacPath = vmacCopy;
|
|
|
|
boost::filesystem::create_directories(dataDir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
vmacCopy = tempDir / "minivmac";
|
|
|
|
fs::copy(vmacPath, vmacCopy);
|
|
|
|
vmacPath = vmacCopy;
|
|
|
|
}
|
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
int size = 5000*1024;
|
|
|
|
|
|
|
|
fs::ofstream(imagePath, std::ios::binary | std::ios::trunc).seekp(size-1).put(0);
|
|
|
|
hfs_format(imagePath.string().c_str(), 0, 0, "SysAndApp", 0, NULL);
|
|
|
|
|
2019-02-10 01:56:37 +00:00
|
|
|
if(!usesAutQuit7)
|
2019-01-04 02:35:32 +00:00
|
|
|
{
|
2019-02-11 20:20:21 +00:00
|
|
|
std::string finderName = std::string("AutoQuit");
|
2019-02-07 21:24:59 +00:00
|
|
|
bootblock1[0x1A] = finderName.size();
|
|
|
|
memcpy(&bootblock1[0x1B], finderName.c_str(), finderName.size());
|
2019-01-04 02:35:32 +00:00
|
|
|
bootblock1[0x5A] = 3;
|
|
|
|
memcpy(&bootblock1[0x5B],"App", 3);
|
|
|
|
}
|
2019-02-10 01:56:37 +00:00
|
|
|
fs::fstream(imagePath, std::ios::in | std::ios::out | std::ios::binary)
|
|
|
|
.write((const char*) bootblock1.data(), 1024);
|
2019-01-04 02:35:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
vol = hfs_mount(imagePath.string().c_str(), 0, HFS_MODE_RDWR);
|
|
|
|
assert(vol);
|
|
|
|
|
|
|
|
hfs_vstat(vol, &ent);
|
|
|
|
ent.blessed = hfs_getcwd(vol);
|
|
|
|
hfs_vsetattr(vol, &ent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CopySystemFile(systemFileName, true);
|
|
|
|
CopySystemFile("MacsBug", false);
|
|
|
|
|
2019-02-07 21:24:59 +00:00
|
|
|
if (usesAutQuit7)
|
|
|
|
{
|
|
|
|
CopySystemFile("Finder", true);
|
2022-09-20 08:43:20 +00:00
|
|
|
CopySystemFile("System 7.5 Update", false);
|
|
|
|
if(hfs_chdir(sysvol, "Extensions") != -1)
|
|
|
|
{
|
|
|
|
hfs_mkdir(vol, "Extensions");
|
|
|
|
if(hfs_chdir(vol, "Extensions") != -1)
|
|
|
|
{
|
|
|
|
CopySystemFile("Appearance Extension", false);
|
|
|
|
CopySystemFile("System 7 Tuner", false);
|
|
|
|
CopySystemFile("System Update", false);
|
|
|
|
hfs_chdir(vol, "::");
|
|
|
|
}
|
|
|
|
hfs_chdir(sysvol, "::");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CopySystemFile("32-Bit QuickDraw", false);
|
|
|
|
CopySystemFile("TrueType\xaa 1.0", false);
|
2019-02-07 21:24:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
{
|
|
|
|
std::ostringstream rsrcOut;
|
|
|
|
app.resources.writeFork(rsrcOut);
|
|
|
|
std::string rsrc = rsrcOut.str();
|
|
|
|
std::string& data = app.data;
|
|
|
|
|
|
|
|
hfsfile *file = hfs_create(vol, "App","APPL","????");
|
|
|
|
hfs_setfork(file, 0);
|
|
|
|
hfs_write(file, data.data(), data.size());
|
|
|
|
hfs_setfork(file, 1);
|
|
|
|
hfs_write(file, rsrc.data(), rsrc.size());
|
|
|
|
hfs_close(file);
|
|
|
|
}
|
|
|
|
|
2019-02-10 01:56:37 +00:00
|
|
|
hfs_umount(sysvol);
|
2019-01-04 02:35:32 +00:00
|
|
|
sysvol = hfs_mount(autoquitImage.string().c_str(),0, HFS_MODE_RDONLY);
|
|
|
|
if(!sysvol)
|
|
|
|
throw std::runtime_error("Cannot open disk image: " + autoquitImage.string());
|
|
|
|
assert(sysvol);
|
2019-02-07 21:24:59 +00:00
|
|
|
if (usesAutQuit7)
|
|
|
|
{
|
|
|
|
CopySystemFile("AutQuit7", true);
|
|
|
|
MakeAlias("AutQuit7 alias", "AutQuit7");
|
|
|
|
hfs_mkdir(vol, "Startup Items");
|
|
|
|
hfs_rename(vol, "AutQuit7 alias", "Startup Items");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CopySystemFile("AutoQuit", true);
|
|
|
|
}
|
2019-01-04 02:35:32 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
hfsfile *file = hfs_create(vol, "out", "TEXT", "MPS ");
|
|
|
|
hfs_close(file);
|
|
|
|
}
|
|
|
|
hfs_umount(sysvol); sysvol = NULL;
|
|
|
|
hfs_umount(vol); vol = NULL;
|
|
|
|
|
|
|
|
fs::path romFile = fs::absolute( options["minivmac-rom"].as<std::string>(), vmacDir );
|
|
|
|
|
|
|
|
fs::create_symlink(
|
|
|
|
romFile,
|
2022-07-31 15:44:15 +00:00
|
|
|
dataDir / romFile.filename() );
|
2019-01-04 02:35:32 +00:00
|
|
|
|
|
|
|
if(romFile.filename() != "vMac.ROM")
|
|
|
|
{
|
|
|
|
// If the ROM file is not named vMac.ROM, this might be for two different
|
|
|
|
// reasons.
|
|
|
|
// 1. The user didn't bother to rename it to the correct "vMac.ROM"
|
2022-07-31 15:44:15 +00:00
|
|
|
// 2. The user is using a version of Mini vMac that is not emulating
|
|
|
|
// a Macintosh Plus and has named the ROM file accordingly.
|
2019-01-04 02:35:32 +00:00
|
|
|
|
|
|
|
// To be on the safe side, provide both the user-specified name and
|
|
|
|
// the standard vMac.ROM.
|
|
|
|
|
|
|
|
fs::create_symlink(
|
|
|
|
romFile,
|
2022-07-31 15:44:15 +00:00
|
|
|
dataDir / "vMac.ROM" );
|
2019-01-04 02:35:32 +00:00
|
|
|
}
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MiniVMacLauncher::~MiniVMacLauncher()
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
if(sysvol)
|
|
|
|
hfs_umount(sysvol);
|
|
|
|
if(vol)
|
|
|
|
hfs_umount(vol);
|
2017-10-02 21:06:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:44:06 +00:00
|
|
|
void MiniVMacLauncher::CopySystemFile(const std::string &fn, bool required)
|
2017-10-02 21:06:50 +00:00
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
hfsdirent fileent;
|
|
|
|
if(hfs_stat(sysvol, fn.c_str(), &fileent) < 0)
|
|
|
|
{
|
|
|
|
if(required)
|
|
|
|
throw std::runtime_error(string("File ") + fn + " not found in disk image");
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hfsfile *in = hfs_open(sysvol, fn.c_str());
|
|
|
|
hfsfile *out = hfs_create(vol, fn.c_str(), fileent.u.file.type,fileent.u.file.creator);
|
|
|
|
|
|
|
|
std::vector<uint8_t> buffer(std::max(fileent.u.file.dsize, fileent.u.file.rsize));
|
|
|
|
hfs_setfork(in, 0);
|
|
|
|
hfs_setfork(out, 0);
|
|
|
|
hfs_read(in, buffer.data(), fileent.u.file.dsize);
|
|
|
|
hfs_write(out, buffer.data(), fileent.u.file.dsize);
|
|
|
|
hfs_setfork(in, 1);
|
|
|
|
hfs_setfork(out, 1);
|
|
|
|
hfs_read(in, buffer.data(), fileent.u.file.rsize);
|
|
|
|
hfs_write(out, buffer.data(), fileent.u.file.rsize);
|
|
|
|
hfs_close(in);
|
|
|
|
hfs_close(out);
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:24:59 +00:00
|
|
|
void MiniVMacLauncher::MakeAlias(const std::string& dest, const std::string& src)
|
|
|
|
{
|
|
|
|
hfsdirent ent;
|
|
|
|
hfsvolent vent;
|
|
|
|
|
|
|
|
hfs_stat(vol, src.c_str(), &ent);
|
|
|
|
hfs_vstat(vol, &vent);
|
|
|
|
|
|
|
|
AliasData alias;
|
2022-09-21 04:51:10 +00:00
|
|
|
alias.size = htons(sizeof(AliasData));
|
2019-02-07 21:24:59 +00:00
|
|
|
alias.volumeNameSize = strlen(vent.name);
|
2022-09-21 04:51:10 +00:00
|
|
|
memcpy(&(alias.volumeName), vent.name, alias.volumeNameSize);
|
|
|
|
memset(&(alias.volumeName[0]) + alias.volumeNameSize, 0, 27 - alias.volumeNameSize);
|
|
|
|
alias.volumeCreationDate = htonl(d_mtime(vent.crdate));
|
|
|
|
alias.parentDirID = htonl(ent.parid);
|
2019-02-07 21:24:59 +00:00
|
|
|
alias.fileNameSize = strlen(ent.name);
|
2022-09-21 04:51:10 +00:00
|
|
|
memcpy(&(alias.fileName), ent.name, alias.fileNameSize);
|
|
|
|
memset(&(alias.fileName[0]) + alias.fileNameSize, 0, 63 - alias.fileNameSize);
|
|
|
|
alias.fileNum = htonl(ent.cnid);
|
|
|
|
alias.fileCreationDate = htonl(d_mtime(ent.crdate));
|
2019-02-07 21:24:59 +00:00
|
|
|
memcpy(&(alias.typeCode), ent.u.file.type, 4);
|
|
|
|
memcpy(&(alias.creatorCode), ent.u.file.creator, 4);
|
|
|
|
|
|
|
|
std::ostringstream roalias;
|
|
|
|
Resources res;
|
|
|
|
res.addResource(Resource("alis", 0, std::string((char*)&alias, sizeof(AliasData))));
|
|
|
|
res.writeFork(roalias);
|
|
|
|
std::string ralias = roalias.str();
|
|
|
|
|
|
|
|
hfsfile *falias = hfs_create(vol, dest.c_str(), "adrp", ent.u.file.creator);
|
|
|
|
hfs_setfork(falias, 1);
|
|
|
|
hfs_write(falias, ralias.data(), ralias.size());
|
2022-09-21 04:51:10 +00:00
|
|
|
hfs_fstat(falias, &ent);
|
|
|
|
ent.fdflags |= HFS_FNDR_ISALIAS;
|
|
|
|
hfs_fsetattr(falias, &ent);
|
2019-02-07 21:24:59 +00:00
|
|
|
hfs_close(falias);
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:06:50 +00:00
|
|
|
|
2019-02-10 01:56:37 +00:00
|
|
|
uint16_t MiniVMacLauncher::GetSystemVersion(const std::string& systemFileName)
|
|
|
|
{
|
|
|
|
hfsdirent fileent;
|
|
|
|
hfs_stat(sysvol, systemFileName.c_str(), &fileent);
|
|
|
|
hfsfile* system = hfs_open(sysvol, systemFileName.c_str());
|
|
|
|
std::vector<uint8_t> buffer(fileent.u.file.rsize);
|
|
|
|
hfs_setfork(system, 1);
|
|
|
|
hfs_read(system, buffer.data(), fileent.u.file.rsize);
|
|
|
|
hfs_close(system);
|
|
|
|
std::istringstream systemResStream(std::string((char*)buffer.data(), buffer.size()));
|
|
|
|
Resources systemRes(systemResStream);
|
|
|
|
Resource vers = systemRes.resources[ResRef('vers', 1)];
|
|
|
|
return (uint16_t)vers.getData()[0] << 8 | vers.getData()[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-02 21:06:50 +00:00
|
|
|
bool MiniVMacLauncher::Go(int timeout)
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
fs::current_path(tempDir);
|
2022-07-31 15:44:15 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if(vmacIsAppBundle)
|
|
|
|
return ChildProcess("open", {"-nWa", vmacPath.string()}, timeout) == 0;
|
|
|
|
#endif
|
2019-01-04 02:35:32 +00:00
|
|
|
return ChildProcess(vmacPath.string(), {}, timeout) == 0;
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MiniVMacLauncher::DumpOutput()
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
vol = hfs_mount(imagePath.string().c_str(), 0, HFS_MODE_RDONLY);
|
|
|
|
hfsdirent fileent;
|
|
|
|
int statres = hfs_stat(vol, "out", &fileent);
|
|
|
|
if(statres)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hfsfile *out = hfs_open(vol, "out");
|
|
|
|
if(!out)
|
|
|
|
return;
|
|
|
|
std::vector<char> buffer(fileent.u.file.dsize);
|
|
|
|
hfs_setfork(out, 0);
|
|
|
|
hfs_read(out, buffer.data(), fileent.u.file.dsize);
|
|
|
|
hfs_close(out);
|
|
|
|
std::cout << string(buffer.begin(), buffer.end());
|
|
|
|
hfs_umount(vol); vol = NULL;
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MiniVMac::GetOptions(options_description &desc)
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
desc.add_options()
|
|
|
|
("minivmac-dir", po::value<std::string>(),"directory containing vMac.ROM")
|
|
|
|
("minivmac-path", po::value<std::string>()->default_value("./minivmac"),"relative path to minivmac")
|
|
|
|
("minivmac-rom", po::value<std::string>()->default_value("./vMac.ROM"),"minivmac ROM file")
|
|
|
|
("system-image", po::value<std::string>(),"path to disk image with system")
|
|
|
|
("autoquit-image", po::value<std::string>(),"path to autoquit disk image, available from the minivmac web site")
|
2019-02-07 21:24:59 +00:00
|
|
|
("autquit7-image", po::value<std::string>(),"path to autquit7 disk image, available from the minivmac web site")
|
2019-01-04 02:35:32 +00:00
|
|
|
;
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MiniVMac::CheckOptions(variables_map &options)
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
return options.count("minivmac-path") != 0
|
|
|
|
&& options.count("minivmac-dir") != 0
|
|
|
|
&& options.count("minivmac-rom") != 0
|
|
|
|
&& options.count("system-image") != 0
|
2019-02-11 20:20:21 +00:00
|
|
|
&& options.count("autoquit-image") + options.count("autquit7-image") > 0;
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Launcher> MiniVMac::MakeLauncher(variables_map &options)
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
return std::unique_ptr<Launcher>(new MiniVMacLauncher(options));
|
2017-10-02 21:06:50 +00:00
|
|
|
}
|