Merge pull request #13 from paperboygold/feature/inventory-bugfixes

feat: inventory bugfixes, status polish, editable inventory fields
This commit is contained in:
Spicy Marinara
2025-10-20 02:03:06 +02:00
committed by GitHub
10 changed files with 844 additions and 67 deletions
+93
View File
@@ -17,6 +17,7 @@ import {
FEATURE_FLAGS
} from './state.js';
import { migrateInventory } from '../utils/migration.js';
import { validateStoredInventory, cleanItemString } from '../utils/security.js';
const extensionName = 'third-party/rpg-companion-sillytavern';
@@ -100,6 +101,9 @@ export function loadSettings() {
console.warn('[RPG Companion] Using default settings due to load error');
// Settings will remain at defaults from state.js
}
// Validate inventory structure (Bug #3 fix)
validateInventoryStructure(extensionSettings.userStats.inventory, 'settings');
}
/**
@@ -238,5 +242,94 @@ export function loadChatData() {
}
}
// Validate inventory structure (Bug #3 fix)
validateInventoryStructure(extensionSettings.userStats.inventory, 'chat');
// console.log('[RPG Companion] Loaded chat data:', savedData);
}
/**
* Validates and repairs inventory structure to prevent corruption.
* Ensures all v2 fields exist and are the correct type.
* Fixes Bug #3: Location disappears when switching tabs
*
* @param {Object} inventory - Inventory object to validate
* @param {string} source - Source of load ('settings' or 'chat') for logging
* @private
*/
function validateInventoryStructure(inventory, source) {
if (!inventory || typeof inventory !== 'object') {
console.error(`[RPG Companion] Invalid inventory from ${source}, resetting to defaults`);
extensionSettings.userStats.inventory = {
version: 2,
onPerson: "None",
stored: {},
assets: "None"
};
saveSettings();
return;
}
let needsSave = false;
// Ensure v2 structure
if (inventory.version !== 2) {
console.warn(`[RPG Companion] Inventory from ${source} missing version, setting to 2`);
inventory.version = 2;
needsSave = true;
}
// Validate onPerson field
if (typeof inventory.onPerson !== 'string') {
console.warn(`[RPG Companion] Invalid onPerson from ${source}, resetting to "None"`);
inventory.onPerson = "None";
needsSave = true;
} else {
// Clean items in onPerson (removes corrupted/dangerous items)
const cleanedOnPerson = cleanItemString(inventory.onPerson);
if (cleanedOnPerson !== inventory.onPerson) {
console.warn(`[RPG Companion] Cleaned corrupted items from onPerson inventory (${source})`);
inventory.onPerson = cleanedOnPerson;
needsSave = true;
}
}
// Validate stored field (CRITICAL for Bug #3)
if (!inventory.stored || typeof inventory.stored !== 'object' || Array.isArray(inventory.stored)) {
console.error(`[RPG Companion] Corrupted stored inventory from ${source}, resetting to empty object`);
inventory.stored = {};
needsSave = true;
} else {
// Validate stored object keys/values
const cleanedStored = validateStoredInventory(inventory.stored);
if (JSON.stringify(cleanedStored) !== JSON.stringify(inventory.stored)) {
console.warn(`[RPG Companion] Cleaned dangerous/invalid stored locations from ${source}`);
inventory.stored = cleanedStored;
needsSave = true;
}
}
// Validate assets field
if (typeof inventory.assets !== 'string') {
console.warn(`[RPG Companion] Invalid assets from ${source}, resetting to "None"`);
inventory.assets = "None";
needsSave = true;
} else {
// Clean items in assets (removes corrupted/dangerous items)
const cleanedAssets = cleanItemString(inventory.assets);
if (cleanedAssets !== inventory.assets) {
console.warn(`[RPG Companion] Cleaned corrupted items from assets inventory (${source})`);
inventory.assets = cleanedAssets;
needsSave = true;
}
}
// Persist repairs if needed
if (needsSave) {
console.log(`[RPG Companion] Repaired inventory structure from ${source}, saving...`);
saveSettings();
if (source === 'chat') {
saveChatData();
}
}
}