Skip to content

Commit

Permalink
fix: table key collisions not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazbot committed Mar 3, 2024
1 parent a84635e commit 38c393f
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 280 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wh3mm",
"productName": "wh3mm",
"version": "2.0.3",
"version": "2.0.4",
"description": "WH3 Mod Manager",
"main": ".webpack/main",
"scripts": {
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ import {
readFromExistingPack,
createOverwritePack,
getPackViewData,
} from "./packFileSerializer";
import * as nodePath from "path";
import { format } from "date-fns";
import {
appendPackFileCollisions,
appendPackTableCollisions,
removeFromPackFileCollisions,
removeFromPackTableCollisions,
} from "./readPacksWorker";
} from "./packFileSerializer";
import * as nodePath from "path";
import { format } from "date-fns";
import { isMainThread } from "worker_threads";
import electronLog from "electron-log";
import * as fsExtra from "fs-extra";
Expand Down
2 changes: 1 addition & 1 deletion src/packFileDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "path";
import { Worker } from "worker_threads";
import * as schema from "../schema/schema_wh3.json";
import { PackCollisions, Pack } from "./packFileTypes";
import { findPackFileCollisions, findPackTableCollisions } from "./readPacksWorker";
import { findPackFileCollisions, findPackTableCollisions } from "./packFileSerializer";

export async function getCompatDataWithWorker(packsData: Pack[]): Promise<PackCollisions> {
return await new Promise<PackCollisions>((resolve, reject) => {
Expand Down
243 changes: 243 additions & 0 deletions src/packFileSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
SchemaField,
SCHEMA_FIELD_TYPE,
AmendedSchemaField,
PackFileCollision,
PackTableCollision,
} from "./packFileTypes";
import clone from "just-clone";
import { emptyMovie, autoStartCustomBattleScript } from "./helperPackData";
Expand Down Expand Up @@ -1783,3 +1785,244 @@ export const readDataFromPacks = async (mods: Mod[]) => {

// console.log("num packs: " + packsData.length);
};

function findPackTableCollisionsBetweenPacks(
pack: Pack,
packTwo: Pack,
packTableCollisions: PackTableCollision[]
) {
for (const packFile of pack.packedFiles) {
if (!packFile.schemaFields) continue;
if (packFile.name.includes(".rpfm_reserved")) continue;

const dbNameMatch1 = packFile.name.match(/db\\(.*?)\\/);
// console.log("dbNameMatch1", dbNameMatch1);
if (dbNameMatch1 == null) continue;
const dbName1 = dbNameMatch1[1];
// console.log("dbName1", dbName1);
if (dbName1 == null) continue;

for (const packTwoFile of packTwo.packedFiles) {
if (!packTwoFile.schemaFields) continue;
if (packTwoFile.name.includes(".rpfm_reserved")) continue;

const dbNameMatch2 = packTwoFile.name.match(/db\\(.*?)\\/);
// console.log("dbNameMatch2", dbNameMatch2);
if (dbNameMatch2 == null) continue;
const dbName2 = dbNameMatch2[1];
// console.log("dbName2", dbName2);
if (dbName2 == null) continue;

try {
if (dbName1 === dbName2) {
// console.log("MATCHED", dbName1, dbName2);
const firstVer = getDBVersion(packFile);
const secondVer = getDBVersion(packTwoFile);
// console.log("ver", firstVer, secondVer);
if (firstVer == null || secondVer == null) continue;

// console.log("length:");
// console.log(firstVer.fields.filter((field) => field.is_key).length);
// console.log(secondVer.fields.filter((field) => field.is_key).length);

if (firstVer.fields.filter((field) => field.is_key).length > 1) continue;
if (secondVer.fields.filter((field) => field.is_key).length > 1) continue;
const firstVerKeyField = firstVer.fields.filter((field) => field.is_key)[0];

// console.log("key field", firstVerKeyField);

const v1Keys = packFile.schemaFields.filter((field) => field.isKey);
// console.log(packFile);
// console.log(packTwoFile);
// console.log(v1Keys);
if (v1Keys.length < 1) continue;
const v2Keys = packTwoFile.schemaFields.filter((field) => field.isKey);
// console.log(v2Keys);
if (v2Keys.length < 1) continue;

for (let ii = 0; ii < v1Keys.length; ii++) {
const v1Fields = v1Keys[ii].fields;
const v1 =
(v1Fields[1] && v1Fields[1].val != null && v1Fields[1].val.toString()) ||
v1Fields[0]?.val?.toString();
for (let jj = 0; jj < v2Keys.length; jj++) {
const v2Fields = v2Keys[jj].fields;
const v2 =
(v2Fields[1] && v2Fields[1].val != null && v2Fields[1].val.toString()) ||
(v2Fields[0]?.val?.toString() ?? "");

if (v1 === v2) {
if (
!packTableCollisions.some(
(collision) =>
collision.firstPackName == pack.name &&
collision.secondPackName == packTwo.name &&
collision.fileName == packFile.name &&
collision.secondFileName == packTwoFile.name &&
collision.key == firstVerKeyField.name &&
collision.value == v1
)
) {
packTableCollisions.push({
firstPackName: pack.name,
secondPackName: packTwo.name,
fileName: packFile.name,
secondFileName: packTwoFile.name,
// key: packFile.schemaFields.find((field) => field.isKey).name,
key: firstVerKeyField.name,
value: v1,
});
}

if (
!packTableCollisions.some(
(collision) =>
collision.firstPackName == packTwo.name &&
collision.secondPackName == pack.name &&
collision.fileName == packTwoFile.name &&
collision.secondFileName == packFile.name &&
collision.key == firstVerKeyField.name &&
collision.value == v1
)
) {
packTableCollisions.push({
secondPackName: pack.name,
firstPackName: packTwo.name,
secondFileName: packFile.name,
fileName: packTwoFile.name,
// key: packFile.schemaFields.find((field) => field.isKey).name,
key: firstVerKeyField.name,
value: v1,
});
}
// console.log("FOUND CONFLICT");
// console.log(pack.name, packTwo.name, packFile.name, packTwoFile.name, v1);
}
}
}
}
} catch (e) {
console.log(e);
}
}
}
}

export function removeFromPackTableCollisions(
packTableCollisions: PackTableCollision[],
removedPackName: string
) {
return packTableCollisions.filter((collision) => {
return collision.firstPackName != removedPackName && collision.secondPackName != removedPackName;
});
}

export function appendPackTableCollisions(
packsData: Pack[],
packTableCollisions: PackTableCollision[],
newPack: Pack
) {
console.time("appendPackTableCollisions");
for (let i = 0; i < packsData.length; i++) {
const pack = packsData[i];
if (pack === newPack) continue;
if (pack.name === newPack.name) continue;
if (pack.name === "data.pack" || newPack.name === "data.pack") continue;

findPackTableCollisionsBetweenPacks(pack, newPack, packTableCollisions);
}
console.timeEnd("appendPackTableCollisions");

return packTableCollisions;
}

export function findPackTableCollisions(packsData: Pack[]) {
const packTableCollisions: PackTableCollision[] = [];
console.time("compareKeys");
for (let i = 0; i < packsData.length; i++) {
const pack = packsData[i];
for (let j = i + 1; j < packsData.length; j++) {
const packTwo = packsData[j];
if (pack === packTwo) continue;
if (pack.name === packTwo.name) continue;
if (pack.name === "data.pack" || packTwo.name === "data.pack") continue;

findPackTableCollisionsBetweenPacks(pack, packTwo, packTableCollisions);
}
}

console.timeEnd("compareKeys");

return packTableCollisions;
}

export function removeFromPackFileCollisions(
packFileCollisions: PackFileCollision[],
removedPackName: string
) {
return packFileCollisions.filter((collision) => {
return collision.firstPackName != removedPackName && collision.secondPackName != removedPackName;
});
}

export function appendPackFileCollisions(
packsData: Pack[],
packFileCollisions: PackFileCollision[],
newPack: Pack
) {
for (let i = 0; i < packsData.length; i++) {
const pack = packsData[i];
if (pack === newPack) continue;
if (pack.name === newPack.name) continue;
if (pack.name === "data.pack" || newPack.name === "data.pack") continue;

findPackFileCollisionsBetweenPacks(pack, newPack, packFileCollisions);
}

return packFileCollisions;
}

function findPackFileCollisionsBetweenPacks(pack: Pack, packTwo: Pack, conflicts: PackFileCollision[]) {
for (const packFile of pack.packedFiles) {
if (packFile.name.includes(".rpfm_reserved")) continue;
for (const packTwoFile of packTwo.packedFiles) {
if (packTwoFile.name.includes(".rpfm_reserved")) continue;
if (packFile.name === packTwoFile.name) {
conflicts.push({
firstPackName: pack.name,
secondPackName: packTwo.name,
fileName: packFile.name,
});

conflicts.push({
secondPackName: pack.name,
firstPackName: packTwo.name,
fileName: packFile.name,
});
// console.log("FOUND CONFLICT");
// console.log(pack.name, packTwo.name, packFile.name);
}
}
}
}

export function findPackFileCollisions(packsData: Pack[]) {
console.time("findPackFileCollisions");
const conflicts: PackFileCollision[] = [];
for (let i = 0; i < packsData.length; i++) {
const pack = packsData[i];
for (let j = i + 1; j < packsData.length; j++) {
// for (let j = 0; j < packsData.length; j++) {
const packTwo = packsData[j];
// for (const pack of packsData) {
// for (const packTwo of packsData) {
if (pack === packTwo) continue;
if (pack.name === packTwo.name) continue;
if (pack.name === "data.pack" || packTwo.name === "data.pack") continue;

findPackFileCollisionsBetweenPacks(pack, packTwo, conflicts);
}
}
console.timeEnd("findPackFileCollisions");
return conflicts;
}
Loading

0 comments on commit 38c393f

Please sign in to comment.