Skip to content

Commit

Permalink
Add local mod support (for ccl2)
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Feb 8, 2024
1 parent aa99222 commit f99b4da
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 156 deletions.
9 changes: 8 additions & 1 deletion assets/data/lang/sc/gui.en_US.json.patch
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
}
},
"ccmodloader": {
"areYouSure": "Are you sure you want to install: \\c[3][modName]\\c[0]?"
"areYouSure": "Are you sure you want to install: \\c[3][modName]\\c[0]?",
"onlineTab": "Online",
"selectedModsTab": "Selected",
"enabledTab": "Enabled",
"disabledTab": "Disabled",
"settingsTab": "Settings",
"installButton": "Install \\c[3][modCount]\\c[0] mods",
"noModsSelected": "No mods selected"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ export class FileCache {
private static cache: Record<string, any> = {}
private static databases: Record<string /* name */, string /* url */> = {}

static getDefaultModIconConfig() {
return {
path: 'media/gui/menu.png',
offsetX: 536,
offsetY: 160,
sizeX: 24,
sizeY: 24,
}
}

static async init() {
this.cacheDir = './assets/mod-data/CCModManager/cache'
await fs.promises.mkdir(`${this.cacheDir}`, { recursive: true })
Expand All @@ -61,6 +71,7 @@ export class FileCache {
}

static async getIconConfig(mod: ModEntry): Promise<ModIconConfig> {
if (mod.isLocal) return mod.iconConfig
if (mod.hasIcon)
return {
path: await this.getIcon(mod),
Expand All @@ -69,13 +80,7 @@ export class FileCache {
sizeX: 24,
sizeY: 24,
}
return {
path: 'media/gui/menu.png',
offsetX: 536,
offsetY: 160,
sizeX: 24,
sizeY: 24,
}
return this.getDefaultModIconConfig()
}

private static async getIcon(mod: ModEntry): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions src/gui/gui.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModMenu } from './menu'
import './menu.js'

const icons = new ig.Font('media/font/CCModManagerIcons.png', 12, ig.MultiFont.ICON_START)
const newFontIndex = sc.fontsystem.font.iconSets.length
Expand All @@ -14,7 +14,7 @@ sc.fontsystem.font.setMapping({
sc.MENU_SUBMENU.MODS = Math.max(...Object.values(sc.MENU_SUBMENU)) + 1

sc.SUB_MENU_INFO[sc.MENU_SUBMENU.MODS] = {
Clazz: ModMenu,
Clazz: sc.ModMenu,
name: 'mods',
}

Expand Down
34 changes: 20 additions & 14 deletions src/gui/list-entry-highlight.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
export {}
declare global {
interface ModListEntryHighlight extends ig.GuiElementBase {
gfx: ig.Image
ninepatch: ig.NinePatch
buttonCover: ig.NinePatch
textWidth: number
buttonWidth: number
highLightOffsetY: number
textTag: ig.ImagePattern
textTagHighlighted: ig.ImagePattern
focus: boolean
}
interface ModListEntryHighlightConstructor extends ImpactClass<ModListEntryHighlight> {
new (width: number, height: number, textWidth: number, buttonWidth: number): ModListEntryHighlight
namespace sc {
interface ModListEntryHighlight extends ig.GuiElementBase {
gfx: ig.Image
ninepatch: ig.NinePatch
buttonCover: ig.NinePatch
textWidth: number
buttonWidth: number
highLightOffsetY: number
textTag: ig.ImagePattern
textTagHighlighted: ig.ImagePattern
focus: boolean
}
interface ModListEntryHighlightConstructor extends ImpactClass<ModListEntryHighlight> {
new (width: number, height: number, textWidth: number, buttonWidth: number): ModListEntryHighlight
}
var ModListEntryHighlight: ModListEntryHighlightConstructor
}
}

export const ModListEntryHighlight: ModListEntryHighlightConstructor = ig.GuiElementBase.extend({
sc.ModListEntryHighlight = ig.GuiElementBase.extend({
gfx: new ig.Image('media/gui/CCModManager.png'),
ninepatch: new ig.NinePatch('media/gui/CCModManager.png', {
left: 3,
Expand Down Expand Up @@ -56,5 +60,7 @@ export const ModListEntryHighlight: ModListEntryHighlightConstructor = ig.GuiEle
src.addPattern(this.focus ? this.textTagHighlighted : this.textTag, 3, 3, 90, 0, this.textWidth, 13)

src.addGfx(this.gfx, this.textWidth + 3, 3, 109, this.focus ? 44 : 3, 6, 13)

// src.addColor('green', 0, 0, this.hook.size.x, this.hook.size.y)
},
})
125 changes: 74 additions & 51 deletions src/gui/list-entry.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
import { ModListEntryHighlight } from './list-entry-highlight'
import { ModMenuList } from './list'
import { ModEntry } from '../types'
import { FileCache } from '../cache'
import { databases } from '../moddb'

export interface ModListEntry extends ig.FocusGui {
ninepatch: ig.NinePatch
mod: ModEntry
nameText: sc.TextGui
description: sc.TextGui
versionText: sc.TextGui
starCount?: sc.TextGui
installRemoveButton: sc.ButtonGui
checkForUpdatesButton: sc.ButtonGui
openModSettingsButton: sc.ButtonGui
modList: ModMenuList
highlight: ModListEntryHighlight
modEntryActionButtonStart: { height: number; ninepatch: ig.NinePatch; highlight: sc.ButtonGui.Highlight }
modEntryActionButtons: sc.ButtonGui.Type & { ninepatch: ig.NinePatch }
iconGui: ig.ImageGui

askInstall(this: this): void
}
interface ModListEntryConstructor extends ImpactClass<ModListEntry> {
new (mod: ModEntry, modList: ModMenuList): ModListEntry
import { InstallQueue } from '../install-queue'
import './list-entry-highlight'
import { InstalledMods } from '../installed-mod-manager'
import { MOD_MENU_TAB_INDEXES } from './list'

declare global {
namespace sc {
export interface ModListEntry extends ig.FocusGui {
ninepatch: ig.NinePatch
mod: ModEntry
nameText: sc.TextGui
description: sc.TextGui
versionText: sc.TextGui
starCount?: sc.TextGui
installRemoveButton: sc.ButtonGui
checkForUpdatesButton: sc.ButtonGui
openModSettingsButton: sc.ButtonGui
modList: sc.ModMenuList
highlight: ModListEntryHighlight
modEntryActionButtonStart: { height: number; ninepatch: ig.NinePatch; highlight: sc.ButtonGui.Highlight }
modEntryActionButtons: sc.ButtonGui.Type & { ninepatch: ig.NinePatch }
iconGui: ig.ImageGui

onButtonPress(this: this): void
setTextGreen(this: this): void
setTextRed(this: this): void
setTextWhite(this: this): void
}
interface ModListEntryConstructor extends ImpactClass<ModListEntry> {
new (mod: ModEntry, modList: sc.ModMenuList): ModListEntry
}
var ModListEntry: ModListEntryConstructor
}
}

export const ModListEntry: ModListEntryConstructor = ig.FocusGui.extend({
sc.ModListEntry = ig.FocusGui.extend({
ninepatch: new ig.NinePatch('media/gui/CCModManager.png', {
width: 42,
height: 26,
Expand Down Expand Up @@ -97,9 +106,10 @@ export const ModListEntry: ModListEntryConstructor = ig.FocusGui.extend({
this.setSize(modList.hook.size.x - 3 /* 3 for scrollbar */, buttonSquareSize * 3 - 3)

this.nameText = new sc.TextGui(mod.name)
if (InstallQueue.has(mod)) this.setTextGreen()

const iconOffset = 25 as const
this.highlight = new ModListEntryHighlight(this.hook.size.x, this.hook.size.y, this.nameText.hook.size.x, buttonSquareSize * 3)
this.highlight = new sc.ModListEntryHighlight(this.hook.size.x, this.hook.size.y, this.nameText.hook.size.x, buttonSquareSize * 3)
this.highlight.setPos(iconOffset, 0)
this.addChildGui(this.highlight)
this.addChildGui(this.nameText)
Expand All @@ -117,12 +127,18 @@ export const ModListEntry: ModListEntryConstructor = ig.FocusGui.extend({

if (mod.stars !== undefined) {
this.starCount = new sc.TextGui(`${mod.stars}\\i[save-star]`)
// this.starCount.setAlign(ig.GUI_ALIGN.X_RIGHT, ig.GUI_ALIGN.Y_TOP)
this.starCount.setPos(517 - this.starCount.hook.size.x, 0) //this.versionText.hook.size.x + 4, 3)
this.starCount.setPos(517 - this.starCount.hook.size.x, 0)
this.addChildGui(this.starCount)
}

this.onButtonPress = () => this.askInstall()
},
setTextGreen() {
this.nameText.setText(`\\c[2]${this.mod.name}\\c[0]`)
},
setTextRed() {
this.nameText.setText(`\\c[1]${this.mod.name}\\c[0]`)
},
setTextWhite() {
this.nameText.setText(this.mod.name)
},
updateDrawables(root) {
if (this.modList.hook.currentStateName != 'HIDDEN') {
Expand All @@ -137,29 +153,36 @@ export const ModListEntry: ModListEntryConstructor = ig.FocusGui.extend({
this.parent()
this.highlight.focus = this.focus
},
askInstall() {
sc.Dialogs.showChoiceDialog(
ig.lang.get('sc.gui.menu.ccmodloader.areYouSure').replace(/\[modName\]/, this.mod.name),
sc.DIALOG_INFO_ICON.QUESTION,
[
ig.lang.get('sc.gui.dialogs.no'),
ig.LangLabel.getText({
en_US: '[nods]',
de_DE: '[nickt]',
zh_CN: '[\u70b9\u5934]<<A<<[CHANGED 2017/10/10]',
ko_KR: '[\ub044\ub355]<<A<<[CHANGED 2017/10/17]',
ja_JP: '[\u3046\u306a\u305a\u304f]<<A<<[CHANGED 2017/11/04]',
zh_TW: '[\u9ede\u982d]<<A<<[CHANGED 2017/10/10]',
langUid: 13455,
}),
],
button => {
const resp = button?.text
if (resp == '[nods]') {
databases[this.mod.database].downloadMod(this.mod.id)
onButtonPress() {
if (this.mod.isLocal) {
if (this.modList.currentTabIndex == MOD_MENU_TAB_INDEXES.ENABLED) {
if (this.mod.active) {
this.setTextRed()
InstalledMods.setModActive(this.mod, false)
} else {
this.setTextWhite()
InstalledMods.setModActive(this.mod, true)
}
} else if (this.modList.currentTabIndex == MOD_MENU_TAB_INDEXES.DISABLED) {
if (this.mod.active) {
this.setTextWhite()
InstalledMods.setModActive(this.mod, false)
} else {
this.setTextGreen()
InstalledMods.setModActive(this.mod, true)
}
} else throw new Error('wat?')
} else {
if (InstallQueue.has(this.mod)) {
InstallQueue.delete(this.mod)
sc.BUTTON_SOUND.toggle_off.play()
this.setTextWhite()
} else {
InstallQueue.add(this.mod)
sc.BUTTON_SOUND.toggle_on.play()
this.setTextGreen()
}
)
}
},
})

Expand Down
Loading

0 comments on commit f99b4da

Please sign in to comment.