0xDEADBEEF

RSS odkazy english edition

simufix.d

9. 1. 2021 #kód

compile

sudo apt install gdc; gdc simufix.d -o simufix

usage

simufix vehicle.pak 100 > vehicle-with-new-running-cost.pak

simufix.d

import std.stdio;
import std.file;
import std.algorithm.searching;
import std.conv;
import std.bitmanip;

void main(string[] args) {
  if (args.length < 2) {
    stderr.writeln("usage: simufix file.pak [new running cost] > new-file.pak");
    return;
  }

  auto onlyRead = args.length == 2;

  auto pakFile = args[1];
  ushort newRunningCost = onlyRead ? 0 : (args[2].to!float*100).to!ushort;

  auto data = cast(ubyte[]) read(pakFile);

  if (!data.canFind("VHCL")) {
    stderr.writeln("not a vehicle pak file");
    return;
  }

  auto vehicleSection = data.find("VHCL")[8 .. $];

  ushort v = (cast(ubyte[2]) vehicleSection[0..2]).littleEndianToNative!ushort;
  ushort vers = v & 0x8000 ? v & 0x7FFF : 0;

  //               0   1   2   3   4   5   6   7   8   9  10  11
  auto offsets = [12, 12, 12, 12, 12, 12, 14, 14, 14, 18, 20, 20];
  auto off = offsets[vers];

  auto runningCostArr = vehicleSection[2 + off .. 2 + off + 2];
  auto oldRunningCost = (cast(ubyte[2]) runningCostArr[0 .. 2]).littleEndianToNative!ushort;

  if (onlyRead) {
    stderr.writeln("file ", pakFile);
    stderr.writeln("running cost ", oldRunningCost);

  } else {
    runningCostArr = newRunningCost.nativeToLittleEndian;
    stdout.rawWrite(data);
  }
}
píše k47 (@kaja47, k47)