Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GameSelectionScreen.created() #849

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/migrations/FolderMigration.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import PathResolver from '../r2mm/manager/PathResolver';
import * as path from 'path';
import FsProvider from '../providers/generic/file/FsProvider';
import PathResolver from '../r2mm/manager/PathResolver';
import FileUtils from '../utils/FileUtils';
import CacheUtil from '../r2mm/mods/CacheUtil';

export default class FolderMigration {
/**
* Mod directory structure was changed when support for other games
* besides RoR2 was added. Update the dir structure if the old one is
* still in use.
*/
export class FolderMigration {

public static async needsMigration(): Promise<boolean> {
public static async needsMigration() {
const fs = FsProvider.instance;
return await fs.exists(path.join(PathResolver.ROOT, "mods"));
}

public static async runMigration(): Promise<void> {
console.log("Started migration");
public static async runMigration() {
if (!await this.needsMigration()) {
return;
}

console.log("Started legacy directory migration");
const fs = FsProvider.instance;
await CacheUtil.clean();
if ((await fs.exists(path.join(PathResolver.ROOT, "RiskOfRain2")))) {
await FileUtils.emptyDirectory(path.join(PathResolver.ROOT, "RiskOfRain2"));
await fs.rmdir(path.join(PathResolver.ROOT, "RiskOfRain2"));
const rorPath = path.join(PathResolver.ROOT, "RiskOfRain2");

if (await fs.exists(rorPath)) {
await FileUtils.emptyDirectory(rorPath);
await fs.rmdir(rorPath);
}
await fs.rename(path.join(PathResolver.ROOT, "mods"), path.join(PathResolver.ROOT, "RiskOfRain2"));
}

await fs.rename(path.join(PathResolver.ROOT, "mods"), rorPath);
console.log("Directory migration done");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the policy with console.logging in this project? My initial reaction was to clean them out. Then I thought they might help users to debug stuff. But then again, they don't have access to the console in production, do they?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging is accessible during production. Ctrl + Shift + I for r2modman, and accessible via logs in TMM.

Generally the rule I've mostly followed is that if a user can't see it, we should still be able to track progress via logs.

}
}
6 changes: 6 additions & 0 deletions src/model/game/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,10 @@ export default class GameManager {
public static unsetGame(): Game {
return this._gameList.find(value => value.internalFolderName === "RiskOfRain2")!;
}

public static findByFolderName(name?: string|null) {
return name
? this._gameList.find((game) => game.internalFolderName === name)
: undefined;
}
}
76 changes: 20 additions & 56 deletions src/pages/GameSelectionScreen.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<div class="notification is-warning is-square" v-if="runningMigration">
<div class="container">
<p>An update to the manager has occurred and needs to do background work.</p>
<p>The option to select a game will appear once the work has completed.</p>
<p>The options to select a game are disabled until the work has completed.</p>
</div>
</div>
<div class="columns">
Expand Down Expand Up @@ -180,10 +180,10 @@ import { Component, Vue } from 'vue-property-decorator';
import Game from '../model/game/Game';
import GameManager from '../model/game/GameManager';
import Hero from '../components/Hero.vue';
import FolderMigration from '../migrations/FolderMigration';
import PathResolver from '../r2mm/manager/PathResolver';
import * as path from 'path';
import FileUtils from '../utils/FileUtils';
import * as ManagerUtils from '../utils/ManagerUtils';
import ManagerSettings from '../r2mm/manager/ManagerSettings';
import { StorePlatform } from '../model/game/StorePlatform';
import { GameSelectionDisplayMode } from '../model/game/GameSelectionDisplayMode';
Expand Down Expand Up @@ -385,61 +385,25 @@ export default class GameSelectionScreen extends Vue {
}
}

created() {
const self = this;
async created() {
this.runningMigration = true;
FolderMigration.needsMigration()
.then(isMigrationRequired => {
if (!isMigrationRequired) {
this.runningMigration = false;
} else {
return FolderMigration.runMigration();
}
})
.then(() => {
this.runningMigration = false;
})
.catch((e) => {
console.log(e);
this.runningMigration = false;
})
.finally(() => {
ManagerSettings.getSingleton(GameManager.unsetGame()).then(settings => {
const lastSelectedGame = settings.getContext().global.lastSelectedGame;
const savedViewMode = settings.getContext().global.gameSelectionViewMode;
switch (savedViewMode) {
case "List": this.viewMode = GameSelectionViewMode.LIST; break;
case "Card":
case undefined:
this.viewMode = GameSelectionViewMode.CARD;
break;
}
if (lastSelectedGame !== null) {
const game = GameManager.gameList.find(value => value.internalFolderName === lastSelectedGame);
if (game !== undefined) {
this.selectedGame = game;
}
}
});
ManagerSettings.getSingleton(GameManager.unsetGame()).then(value => {
this.settings = value;
this.favourites = value.getContext().global.favouriteGames || [];
if (value.getContext().global.defaultGame !== undefined) {
if (value.getContext().global.defaultStore !== undefined) {
const game = GameManager.gameList
.find(value1 => value1.internalFolderName === value.getContext().global.defaultGame)!;

const platform = game.storePlatformMetadata.find(value1 => value1.storePlatform === value.getContext().global.defaultStore)!;

this.selectedGame = game;
this.selectedPlatform = platform.storePlatform;

this.proceed();
return;
}
}
});
})
await this.$store.dispatch('checkMigrations');
this.runningMigration = false;

this.settings = await ManagerSettings.getSingleton(GameManager.unsetGame());
const globalSettings = this.settings.getContext().global;
this.viewMode = globalSettings.gameSelectionViewMode;
MythicManiac marked this conversation as resolved.
Show resolved Hide resolved
this.favourites = globalSettings.favouriteGames ?? [];
this.selectedGame = GameManager.findByFolderName(globalSettings.lastSelectedGame) ?? null;

// Skip game selection view if valid default game & platform are set.
const {defaultGame, defaultPlatform} = ManagerUtils.getDefaults(this.settings);

if (defaultGame && defaultPlatform) {
this.selectedGame = defaultGame;
this.selectedPlatform = defaultPlatform;
this.proceed();
}
}

toggleViewMode() {
Expand Down
2 changes: 1 addition & 1 deletion src/r2mm/manager/SettingsDexieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface ManagerSettingsInterfaceGlobal_V2 {
favouriteGames: string[] | undefined;
defaultGame: string | undefined;
defaultStore: StorePlatform | undefined;
gameSelectionViewMode: string | undefined;
gameSelectionViewMode: GameSelectionViewMode;
MythicManiac marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Vue from 'vue';
import Vuex from 'vuex';
import { FolderMigration } from '../migrations/FolderMigration';

// import example from './module-example'

Expand All @@ -16,6 +17,7 @@ export default function(/* { ssrContext } */) {
localModList: [],
thunderstoreModList: [],
dismissedUpdateAll: false,
isMigrationChecked: false,
apiConnectionError: ""
},
actions: {
Expand All @@ -30,6 +32,19 @@ export default function(/* { ssrContext } */) {
},
updateApiConnectionError({commit}, err) {
commit('setApiConnectionError', err);
},
async checkMigrations({commit, state}) {
if (state.isMigrationChecked) {
return;
}

try {
await FolderMigration.runMigration();
} catch (e) {
console.error(e);
} finally {
commit('setMigrationChecked');
}
}
},
mutations: {
Expand All @@ -42,6 +57,9 @@ export default function(/* { ssrContext } */) {
dismissUpdateAll(state) {
state.dismissedUpdateAll = true;
},
setMigrationChecked(state) {
state.isMigrationChecked = true;
},
setApiConnectionError(state, err) {
state.apiConnectionError = err;
}
Expand Down
18 changes: 18 additions & 0 deletions src/utils/ManagerUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import GameManager from "../model/game/GameManager";
import ManagerSettings from "../r2mm/manager/ManagerSettings";


/**
* Return default game selection needed to skip the game selection screen.
*/
export const getDefaults = (settings: ManagerSettings) => {
const globals = settings.getContext().global;
const defaultGame = GameManager.findByFolderName(globals.defaultGame);
const platforms = defaultGame ? defaultGame.storePlatformMetadata : [];
const defaultPlat = platforms.find(x => x.storePlatform === globals.defaultStore);

return {
defaultGame,
defaultPlatform: defaultPlat ? defaultPlat.storePlatform : undefined
};
}