nyacronomicon/js/plugins/KN_SaveMigration.js
2026-03-12 22:39:10 -05:00

168 lines
6.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*:ja
* @target MZ
* @plugindesc セーブデータ移行・修正管理プラグイン
* @author kurogoma
* @base PluginCommonBase
*
* @help KN_SaveMigration.js
*
* セーブデータの移行・修正を一元管理するプラグインです。
*
* 【使い方】
* 1. MIGRATIONS 配列に新しい修正を追加
* 2. version は連番で管理1, 2, 3...
* 3. ロード時に未適用の修正が自動実行される
*
* 【注意】
* - 一度適用された修正は再実行されません
* - version 番号は変更しないでください
* - 新しい修正は配列の末尾に追加してください
*/
(() => {
"use strict";
//==========================================================================
// マイグレーション関数
// 1バージョンにつき1関数で定義
//==========================================================================
/**
* v1: _temporaryList をクリア & セルフスイッチAを全初期化
*/
const migrate_v1 = function() {
// _temporaryList をクリアTempS の不要な登録を削除)
if ($gameSelfSwitches._temporaryList) {
$gameSelfSwitches._temporaryList = {};
}
// セルフスイッチAを全てOFFにする
Object.keys($gameSelfSwitches._data || {}).forEach(key => {
const parts = String(key).split(',');
if (parts[2] === 'A') {
$gameSelfSwitches.setValue(key, false);
}
});
};
/**
* v2: FloorManager の旧形式availableMaps/rareMapsを削除
* 新形式mapQueues/rareMapQueueはダンジョン突入時に initialize() で再構築される
*/
const migrate_v2 = function() {
const floorManager = window.Kurogoma?.floorManager;
if (floorManager) {
// 旧形式プロパティを削除
if (floorManager.availableMaps !== undefined) {
delete floorManager.availableMaps;
console.log("[Migration v2] availableMaps を削除");
}
if (floorManager.rareMaps !== undefined) {
delete floorManager.rareMaps;
console.log("[Migration v2] rareMaps を削除");
}
}
};
/**
* v3: StillManager の旧データを削除
* スチル解放は記憶アイテム所持で判定するため、旧フラグは不要
*/
const migrate_v3 = function() {
const keys = ['_stillDefs', '_stillUnlocks', '_stillUnlockedIds'];
for (const key of keys) {
if ($gameSystem[key] !== undefined) {
delete $gameSystem[key];
console.log(`[Migration v3] ${key} を削除`);
}
}
};
// 今後の修正はここに関数を追加
// const migrate_v4 = function() { ... };
//==========================================================================
// マイグレーション定義
// version と関数を紐付ける
//==========================================================================
const MIGRATIONS = [
{ version: 1, description: "_temporaryList をクリア、セルフスイッチAを全初期化", migrate: migrate_v1 },
{ version: 2, description: "FloorManager 旧形式availableMaps/rareMapsを削除", migrate: migrate_v2 },
{ version: 3, description: "StillManager 旧データ_stillDefs/_stillUnlocks/_stillUnlockedIdsを削除", migrate: migrate_v3 },
// { version: 4, description: "xxx の修正", migrate: migrate_v4 },
];
//==========================================================================
// マイグレーション実行処理
//==========================================================================
// 最新バージョンを取得
const getLatestVersion = function() {
if (MIGRATIONS.length === 0) return 0;
return MIGRATIONS[MIGRATIONS.length - 1].version;
};
// 現在の適用済みバージョンを取得
const getCurrentVersion = function() {
return $gameSystem._migrationVersion || 0;
};
// バージョンを更新
const setCurrentVersion = function(version) {
if ($gameSystem) {
$gameSystem._migrationVersion = version;
}
};
// 未適用のマイグレーションを実行
const runPendingMigrations = function() {
const currentVersion = getCurrentVersion();
const latestVersion = getLatestVersion();
if (currentVersion >= latestVersion) {
return; // 最新なので何もしない
}
console.log(`[SaveMigration] マイグレーション実行: v${currentVersion} → v${latestVersion}`);
// 未適用の修正を順番に実行
for (const migration of MIGRATIONS) {
if (migration.version > currentVersion) {
try {
console.log(`[Migration v${migration.version}] ${migration.description}`);
migration.migrate();
} catch (e) {
console.error(`[Migration v${migration.version}] エラー:`, e);
}
}
}
// バージョンを更新
setCurrentVersion(latestVersion);
console.log(`[SaveMigration] マイグレーション完了: v${latestVersion}`);
};
//==========================================================================
// フック: ニューゲーム時に最新バージョンを設定
//==========================================================================
const _DataManager_setupNewGame = DataManager.setupNewGame;
DataManager.setupNewGame = function() {
_DataManager_setupNewGame.apply(this, arguments);
// ニューゲームは最新状態なのでマイグレーション不要
const latestVersion = getLatestVersion();
setCurrentVersion(latestVersion);
console.log(`[SaveMigration] ニューゲーム: v${latestVersion} で開始`);
};
//==========================================================================
// フック: セーブデータ読み込み後に実行
//==========================================================================
const _DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents) {
_DataManager_extractSaveContents.apply(this, arguments);
// マイグレーション実行
runPendingMigrations();
};
})();